TRX/tools/generate_init

55 lines
1.1 KiB
Text
Raw Normal View History

#!/usr/bin/env python3
import io
import json
from pathlib import Path
import pyjson5
2021-10-19 16:56:11 +02:00
REPO_DIR = Path(__file__).parent.parent
2021-11-17 12:08:20 +01:00
BIN_DIR = REPO_DIR / "bin"
2021-10-19 16:56:11 +02:00
SRC_DIR = REPO_DIR / "src"
2021-11-17 12:08:20 +01:00
CONFIG_DIR = BIN_DIR / "cfg"
BODY_PREFIX = """
#include "game/gameflow.h"
#include "global/types.h"
#include <stddef.h>
// THIS IS AN AUTOGENERATED FILE.
2022-05-13 00:56:28 +02:00
// To generate it, run tools/generate_init.
#ifndef VERSION
#define VERSION "T1M (unknown version)"
#endif
const char *g_T1MVersion = VERSION;
GAMEFLOW_DEFAULT_STRING g_GameFlowDefaultStrings[] = {"""
BODY_SUFFIX = """ { 0, NULL },
};
"""
def main() -> None:
gf = pyjson5.loads(
2021-10-19 16:56:11 +02:00
(CONFIG_DIR / "Tomb1Main_gameflow.json5").read_text(encoding="utf-8")
)
with io.StringIO() as handle:
print(BODY_PREFIX.lstrip(), file=handle)
for key, value in gf["strings"].items():
print(f" {{ GS_{key}, {json.dumps(value)} }},", file=handle)
print(BODY_SUFFIX.rstrip(), file=handle)
new_text = handle.getvalue()
path = SRC_DIR / "init.c"
if path.read_text() != new_text:
path.write_text(new_text)
if __name__ == "__main__":
main()