TRX/tools/generate_init

68 lines
1.7 KiB
Text
Raw Normal View History

#!/usr/bin/env python3
import argparse
import io
import json
from pathlib import Path
try:
import pyjson5
except ImportError:
import json5 as pyjson5
2021-10-19 16:56:11 +02:00
REPO_DIR = Path(__file__).parent.parent
2024-03-23 13:34:59 +01:00
SHIP_DIR = REPO_DIR / "data/ship"
2021-10-19 16:56:11 +02:00
SRC_DIR = REPO_DIR / "src"
2024-03-23 13:34:59 +01:00
CONFIG_DIR = SHIP_DIR / "cfg"
2023-09-26 16:22:29 +02:00
GAMEFLOW_PATH = CONFIG_DIR / "TR1X_gameflow.json5"
BODY_PREFIX = """
#include "game/gameflow.h"
#include "global/types.h"
#include <stddef.h>
2023-09-26 16:22:29 +02:00
const char *g_TR1XVersion = "{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()