mirror of
https://github.com/LostArtefacts/TRX.git
synced 2025-04-28 20:58:07 +03:00
46 lines
1.3 KiB
Python
Executable file
46 lines
1.3 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
import json
|
|
import re
|
|
|
|
from tr1x.paths import TR1X_DATA_DIR, TR1X_SRC_DIR
|
|
|
|
SHIP_DIR = TR1X_DATA_DIR / "ship"
|
|
GAME_STRING_DEF_PATH = TR1X_SRC_DIR / "game/game_string.def"
|
|
|
|
|
|
def get_default_string_map() -> dict[str, str]:
|
|
result: dict[str, str] = {}
|
|
for line in GAME_STRING_DEF_PATH.read_text().splitlines():
|
|
if match := re.match(r'^GS_DEFINE\((\w+),\s*"([^"]+)"\)$', line.strip()):
|
|
result[match.group(1)] = match.group(2)
|
|
return result
|
|
|
|
|
|
def postprocess_gameflow(gameflow: str, string_map: dict[str, str]) -> str:
|
|
gameflow = re.sub(
|
|
r'^( "strings": {)[^}]*(})',
|
|
' "strings": {\n'
|
|
+ "\n".join(
|
|
f" {json.dumps(key)}: {json.dumps(value)},"
|
|
for key, value in string_map.items()
|
|
)
|
|
+ "\n }",
|
|
gameflow,
|
|
flags=re.M | re.DOTALL,
|
|
)
|
|
return gameflow
|
|
|
|
|
|
def main() -> None:
|
|
string_map = get_default_string_map()
|
|
assert string_map
|
|
|
|
for gameflow_path in SHIP_DIR.rglob("*gameflow*.json*"):
|
|
old_gameflow = gameflow_path.read_text()
|
|
new_gameflow = postprocess_gameflow(old_gameflow, string_map)
|
|
if new_gameflow != old_gameflow:
|
|
gameflow_path.write_text(new_gameflow)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|