TRX/tools/tr1/update_gameflow

54 lines
1.5 KiB
Text
Raw Normal View History

#!/usr/bin/env python3
import json
import re
2024-09-18 00:04:27 +02:00
from pathlib import Path
2024-09-18 00:04:27 +02:00
from tr1x.paths import LIBTRX_INCLUDE_DIR, TR1X_DATA_DIR, TR1X_SRC_DIR
SHIP_DIR = TR1X_DATA_DIR / "ship"
2024-09-18 00:04:27 +02:00
GAME_STRING_DEF_PATHS = [
TR1X_SRC_DIR / "game/game_string.def",
LIBTRX_INCLUDE_DIR / "game/game_string.def",
]
2024-09-18 00:04:27 +02:00
def get_default_strings_map(paths: list[Path]) -> dict[str, str]:
result: dict[str, str] = {}
2024-09-18 00:04:27 +02:00
for path in paths:
for line in path.read_text().splitlines():
if match := re.match(
r'^GS_DEFINE\((\w+),\s*"([^"]+)"\)$', line.strip()
):
result[match.group(1)] = match.group(2)
return result
2024-09-18 00:04:27 +02:00
def postprocess_gameflow(gameflow: str, strings_map: dict[str, str]) -> str:
gameflow = re.sub(
r'^( "strings": {)[^}]*(})',
' "strings": {\n'
+ "\n".join(
f" {json.dumps(key)}: {json.dumps(value)},"
2024-09-18 00:04:27 +02:00
for key, value in sorted(strings_map.items(), key=lambda kv: kv[0])
)
+ "\n }",
gameflow,
flags=re.M | re.DOTALL,
)
return gameflow
def main() -> None:
2024-09-18 00:04:27 +02:00
strings_map = get_default_strings_map(GAME_STRING_DEF_PATHS)
assert strings_map
for gameflow_path in SHIP_DIR.rglob("*gameflow*.json*"):
2024-04-09 15:15:58 +02:00
old_gameflow = gameflow_path.read_text()
2024-09-18 00:04:27 +02:00
new_gameflow = postprocess_gameflow(old_gameflow, strings_map)
2024-04-09 15:15:58 +02:00
if new_gameflow != old_gameflow:
gameflow_path.write_text(new_gameflow)
if __name__ == "__main__":
main()