"""The title screen's Options entry: "Options / ZELDA NOTES Setting" reads "Options / Triforce" (nx2/prompt_assets.py title_messages, host/prompts.c title_messages). The text is label 0002 of LayoutMsg/Title_00.msbt in each language's messages (Mals/.Product.110.sarc.zs). Setup's Update rewrites that one text in the player's own files, the host serves the language the game shows. Checked here, on archives and messages made up by the test (no game file is read): - The MSBT: label 0002 is the game's word for Options, its size tag as it was, the game's separator (" / " for a rest in brackets) and TITLE_TEXT, then its end; the other texts, the labels and every other section byte for byte as they were; the TXT2 offsets, each section's size and padding to 16, and the file's size agree. In UTF-8 and UTF-16, a tag after the rest (the Japanese text's ruby) dropped with it, and a longer TITLE_TEXT too. - The SARC: every other file byte for byte the same, the files before it at their places, those after it at places of the same alignment (the bytes between them kept), the nodes and the archive's size agreeing, the names' table and the data's offset as they were. A text with no size tag, a file that is no MSBT, an archive without the title's file: None. - The size table's entries: a hashed one by the path's CRC-32, a named one over it. - title_messages end to end on a romfs the test makes (the dictionaries' pack, a size table, two languages' messages): the file is written in lower case under mals/, as a zstd frame without the dictionary, and is the rewritten archive; a language whose archive is not the known one, would not fit its size table entry, or would be larger than the game's file (a longer TITLE_TEXT, the archive grown into the entry's allowance), is not written. - The host: its table of languages is host/nn_applet.c's, in its order, each on its own Mals file; title_messages cut out and run serves the file of the language the game shows, not another language's, and nothing without the file or without the prompts folder; hos_prompts_romfs asks it for the Mals keys. python -I -B tests/test_title_label.py [--cc gcc] """ import argparse import os import re import struct import subprocess import sys import tempfile import zlib from pathlib import Path RUN_ALL_ARGS = ["--cc", "gcc"] RUN_ALL_MODULES = ["zstandard"] ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "nx2")) import prompt_assets as P # noqa: E402 SIZE_TAG = b"\x0e" + struct.pack("= now[P.TITLE_MSBT][1], "%s lost its alignment %#x or overlaps" % (name, align)) check(struct.unpack_from(" %s)" % (name, len(b), grown and len(grown))) out2 = Path(tmp) / "prompts-longer" done = P.title_messages(str(romfs), dic, str(out2), longer) check(done == [], "an archive larger than the game's file was written: %s" % done) check(not (out2 / "mals").exists() or not os.listdir(out2 / "mals"), "files written for a longer text") HARNESS = r'''#include #include #include #include #define MAX_PATH 260 #define INVALID_FILE_ATTRIBUTES 0xFFFFFFFFul typedef long LONG; static const char *folder, *language; static int has_prompts; static unsigned long GetFileAttributesA(const char *path) { FILE *f = fopen(path, "rb"); if (!f) return INVALID_FILE_ATTRIBUTES; fclose(f); return 0; } static LONG InterlockedExchange(volatile LONG *p, LONG v) { const LONG old = *p; *p = v; return old; } static void hos_log(const char *fmt, ...) { va_list a; va_start(a, fmt); vfprintf(stderr, fmt, a); va_end(a); fputc('\n', stderr); } static int setup(char *dir, size_t cap, int *face) { *face = 0; snprintf(dir, cap, "%s/", folder); return has_prompts; } const char *hos_desired_language(void) { return language; } @CUT@ /* folder, language, has prompts, key: "1 " served, "0" not */ int main(int argc, char **argv) { if (argc < 5) return 2; folder = argv[1], language = argv[2], has_prompts = atoi(argv[3]); char out[MAX_PATH] = ""; const int ok = title_messages(argv[4], out, sizeof out); printf("%d %s\n", ok, ok ? out : ""); return 0; } ''' def check_host(cc): applet = (ROOT / "host/nn_applet.c").read_text(encoding="utf-8") prompts = (ROOT / "host/prompts.c").read_text(encoding="utf-8") table = applet[applet.index("static const struct { const char *code, *name; } languages[] = {"):] codes = re.findall(r'\{"([^"]+)", "[^"]+"\}', table[:table.index("};")]) start = prompts.index("static const struct { const char *code, *mals; } title_languages[] = {") ours = re.findall(r'\{"([^"]+)", "([^"]+)"\}', prompts[start:prompts.index("};", start)]) check([c for c, _ in ours] == codes, "host/prompts.c title_languages is not nn_applet.c's languages, in order") check({m for _, m in ours} == MALS_NAMES and len(ours) == 16, "the languages' Mals files are not the game's 16") check(dict(ours)["en-US"] == "usen" and dict(ours)["ja"] == "jpja" and dict(ours)["fr-CA"] == "usfr" and dict(ours)["en-GB"] == "euen", "a language is on another's Mals file") check("const char *hos_desired_language(void) { return desired_language(); }" in applet, "host/nn_applet.c does not give prompts.c the game's language") romfs_fn = prompts[prompts.index("int hos_prompts_romfs(const char *key"):] check('if (!strncmp(key, "mals/", 5) && kn > 8 && !strcmp(key + kn - 8, ".sarc.zs")) return title_messages(key, out, cap);' in romfs_fn, "hos_prompts_romfs does not ask title_messages for the Mals files") fn = prompts.index("static int title_messages(") end = prompts.index("\n}\n", fn) + 3 harness = HARNESS.replace("@CUT@", prompts[start:end]) with tempfile.TemporaryDirectory(prefix="title-host-") as tmp: c, exe = Path(tmp) / "title.c", Path(tmp) / "title" c.write_text(harness, encoding="utf-8") subprocess.run([cc, "-std=c11", "-Wall", "-Wextra", "-Werror", str(c), "-o", str(exe)], check=True) game = Path(tmp) / "game" (game / "prompts" / "mals").mkdir(parents=True) for name in ("usen", "jpja"): (game / "prompts" / "mals" / f"{name}.product.110.sarc.zs").write_bytes(b"made by the test") def ask(lang, key, has=1): got = subprocess.run([str(exe), str(game), lang, str(has), key], check=True, capture_output=True, text=True).stdout.split() return got[1] if got[0] == "1" else None key = "mals/usen.product.110.sarc.zs" check(ask("en-US", key) == "%s/prompts/%s" % (game, key), "en-US: the US English file was not served") check(ask("ja", "mals/jpja.product.110.sarc.zs") is not None, "ja: the Japanese file was not served") check(ask("ja", key) is None, "ja: the US English file was served") check(ask("en-GB", "mals/euen.product.110.sarc.zs") is None, "en-GB: served with no file") check(ask("en-US", key, has=0) is None, "served without the prompts folder") check(ask("xx", key) is not None, "a language not in the table does not fall back to English (US)'s file") def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument("--cc", default="gcc") a = p.parse_args() n = check_msbt() k = check_sarc() check_size_table() check_end_to_end() check_host(a.cc) print("ok: %d title texts rewritten, %d archives with every other file kept, the size table, the build and the host" % (n, k)) if __name__ == "__main__": main()