TRX/tools/generate_init
Marcin Kurczewski d87837d951 build: always bump git version; remove init.c
Rebuilding the exe was not updating the git version shown inside the
game, which caused me some issues when I was working on the project
across two machines. This new approach forces meson to always reconsider
the current version, and rebuild relevant autogenerated files:
version.rc and init.c prior to linking the .exe.

Furthermore, the versioned init.c was removed. The existing users who
choose to build without Docker will continue to see fallback
information, but the mechanism was changed from a versioned file to a
special environment variable that's set by the Docker builds.

This change should require no action other than a clean project rebuild
on the part of the users.
2023-08-31 23:31:46 +02:00

64 lines
1.6 KiB
Python
Executable file

#!/usr/bin/env python3
import argparse
import io
import json
from pathlib import Path
import pyjson5
REPO_DIR = Path(__file__).parent.parent
BIN_DIR = REPO_DIR / "bin"
SRC_DIR = REPO_DIR / "src"
CONFIG_DIR = BIN_DIR / "cfg"
GAMEFLOW_PATH = CONFIG_DIR / "Tomb1Main_gameflow.json5"
BODY_PREFIX = """
#include "game/gameflow.h"
#include "global/types.h"
#include <stddef.h>
const char *g_T1MVersion = "{version}";
GAMEFLOW_DEFAULT_STRING g_GameFlowDefaultStrings[] = {{"""
BODY_SUFFIX = """ { 0, NULL },
};
"""
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--version-file", type=Path)
parser.add_argument('-o', "--output", type=Path)
return parser.parse_args()
def get_init_c(version: str) -> str:
gameflow = pyjson5.loads(GAMEFLOW_PATH.read_text(encoding="utf-8"))
with io.StringIO() as handle:
print(BODY_PREFIX.format(version=version).lstrip(), file=handle)
for key, value in gameflow["strings"].items():
print(f" {{ GS_{key}, {json.dumps(value)} }},", file=handle)
print(BODY_SUFFIX.rstrip(), file=handle)
return handle.getvalue()
def update_init_c(output_path: Path, version: str) -> None:
new_text = get_init_c(version=version)
if not output_path.exists() or output_path.read_text() != new_text:
output_path.write_text(new_text)
def main() -> None:
args = parse_args()
if args.version_file and args.version_file.exists():
version = args.version_file.read_text().strip()
else:
version = ""
update_init_c(output_path=args.output, version=version)
if __name__ == "__main__":
main()