2024-04-09 14:09:05 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import json
|
|
|
|
import re
|
2024-09-18 00:04:27 +02:00
|
|
|
from pathlib import Path
|
2024-04-09 14:09:05 +02:00
|
|
|
|
2024-09-18 00:04:27 +02:00
|
|
|
from tr1x.paths import LIBTRX_INCLUDE_DIR, TR1X_DATA_DIR, TR1X_SRC_DIR
|
2024-04-09 14:09:05 +02:00
|
|
|
|
2024-04-30 00:37:50 +02:00
|
|
|
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-04-09 14:09:05 +02:00
|
|
|
|
|
|
|
|
2024-09-18 00:04:27 +02:00
|
|
|
def get_default_strings_map(paths: list[Path]) -> dict[str, str]:
|
2024-04-09 14:09:05 +02:00
|
|
|
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)
|
2024-04-09 14:09:05 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2024-09-18 00:04:27 +02:00
|
|
|
def postprocess_gameflow(gameflow: str, strings_map: dict[str, str]) -> str:
|
2024-04-09 14:09:05 +02:00
|
|
|
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])
|
2024-04-09 14:09:05 +02:00
|
|
|
)
|
|
|
|
+ "\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
|
2024-04-09 14:09:05 +02:00
|
|
|
|
|
|
|
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)
|
2024-04-09 14:09:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|