"""Check that a save which still holds the first adult night's time lock is repaired outside Hyrule Field. The game holds the clock at 20:00 while `HrlField_isFirstNightTime_Adult` (MurmurHash3 0x8592aa82) is 1 and quest 0x4dbca27f is not Ready (f_8347c020, called from f_803edf70). The flag is only meaningful in Hyrule Field's first adult night; a save in the Market, the castle or anywhere else with the flag still 1 can never leave night. `oot_inventory_refresh` runs twice a second while a save is loaded and reads the map for it, so the repair lives there: outside Hyrule Field, clear the flag with the game's own Bool and log it once. This cuts the real `oot_inventory_refresh` (and `oot_first_night_fix` when the source has it) out of host/hos_main.c and runs it against made-up save state, with no game and no romfs. Without the repair the flag stays 1 and the first case fails; with it the flag is cleared. Run it with: python -I tests/test_oot_first_night.py --cc gcc """ import argparse import os import re import subprocess import tempfile from pathlib import Path RUN_ALL_ARGS = ["--cc", "gcc"] ROOT = Path(__file__).resolve().parents[1] FIXTURE = r''' #include #include #include #include typedef struct { uint64_t x[32]; } Ctx; static int flag = 1; /* HrlField_isFirstNightTime_Adult */ static int child = 1; /* Sequence_IsChildhood: 1 child, 0 adult */ static int event_playing; /* the game's event manager has an active event */ static const char *map_name = "Market_Child"; static int cleared; static void hos_log(const char *fmt, ...) { (void)fmt; } static void rx_option_set(int id, int value) { (void)id; (void)value; } static int oot_item_get(Ctx *c, const char *name) { (void)c; (void)name; return 0; } static int oot_pouch_count(Ctx *c, const char *name) { (void)c; (void)name; return 0; } /* (a save is loaded) */ static int oot_gamedata_bool(Ctx *c, uint32_t hash, int *value) { (void)c; if (hash == 0xcea220d3u) { *value = child; return 1; } if (hash == 0x8592aa82u) { *value = flag; return 1; } return 0; } __attribute__((unused)) static int oot_gamedata_set_bool(Ctx *c, uint32_t hash, int value) { (void)c; if (hash != 0x8592aa82u) return 0; if (flag != value) cleared++; flag = value; return 1; } static int oot_gamedata_string(Ctx *c, uint32_t hash, char *buf, size_t size) { (void)c; if (hash != 0x1d6189dau) return 0; snprintf(buf, size, "%s", map_name); return 1; } /* (the event manager, and its +104: the event playing) */ #define OOT_EVENTMGR_SLOT 0x84c9e090ULL __attribute__((unused)) static uint64_t oot_singleton(Ctx *c, uint64_t slot) { (void)c; return slot == OOT_EVENTMGR_SLOT ? 0x1000 : 0; } __attribute__((unused)) static uint64_t recomp_load64(Ctx *c, uint64_t a) { (void)c; return a == 0x1000 + 104 ? (uint64_t)event_playing : 0; } #define OOT_CHEAT_ITEM_BASE 100 #define OOT_CHEAT_AGE_ID 200 enum { HOS_OPT_SIM_TURF_AREA_TEXT = 410, HOS_OPT_SIM_TURF_AREA }; /* (vk_api.h's: the area, for the Simulated turf page) */ #define OOT_ITEM_COUNT 2 static const char *const oot_item_names[OOT_ITEM_COUNT] = {"Item_A", "Item_B"}; // FIX // REFRESH static void check(const char *what, int want_flag, int want_cleared) { if (flag != want_flag || cleared != want_cleared) { fprintf(stderr, "FAIL: %s: flag %d (want %d), clears %d (want %d)\n", what, flag, want_flag, cleared, want_cleared); exit(1); } } int main(void) { Ctx c = {0}; int64_t now = 0; #ifdef SWITCH_OFF /* HOS_OOT_FIRST_NIGHT_FIX=0: the repair is off for the whole run. */ flag = 1, map_name = "Market_Child", cleared = 0; oot_inventory_refresh(&c, now); check("the switch off leaves the flag", 1, 0); puts("PASS: HOS_OOT_FIRST_NIGHT_FIX=0 leaves the flag alone"); return 0; #else /* The report's state: the adult first-night flag left set in a save that is in the Market. */ flag = 1, map_name = "Market_Child", cleared = 0; oot_inventory_refresh(&c, now); check("the flag is cleared outside Hyrule Field", 0, 1); /* Hyrule Field is where the first night belongs: the repair must not touch it. */ now += 500000000LL; flag = 1, map_name = "HyruleField", cleared = 0; oot_inventory_refresh(&c, now); check("the flag is left alone in Hyrule Field", 1, 0); /* A save that is already clear is not written again. */ now += 500000000LL; flag = 0, map_name = "Market_Child", cleared = 0; oot_inventory_refresh(&c, now); check("a clear flag is left clear", 0, 0); /* No map name this refresh: nothing to decide on. */ now += 500000000LL; flag = 1, map_name = "", cleared = 0; oot_inventory_refresh(&c, now); check("an empty map leaves the flag", 1, 0); /* An event running (the escape's cutscene, say) leaves the flag for a later refresh. */ now += 500000000LL; flag = 1, map_name = "Market_Child", event_playing = 1, cleared = 0; oot_inventory_refresh(&c, now); check("an event running leaves the flag", 1, 0); /* The event over, the next refresh clears it. */ now += 500000000LL; event_playing = 0, cleared = 0; oot_inventory_refresh(&c, now); check("after the event it clears", 0, 1); /* A map change after the Field: the next refresh clears it. */ now += 500000000LL; flag = 1, map_name = "Market_Child", cleared = 0; oot_inventory_refresh(&c, now); check("a later map clears it", 0, 1); puts("PASS: the first adult night's time lock is cleared outside Hyrule Field, and only there"); return 0; #endif } ''' def cut(source, pattern, what): match = re.search(pattern, source, re.M | re.S) if not match: raise SystemExit("could not find %s in host/hos_main.c" % what) return match.group() def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--cc", default="gcc") args = parser.parse_args() host = (ROOT / "host/hos_main.c").read_text(encoding="utf-8") refresh = cut(host, r"^static void oot_inventory_refresh\(Ctx \*c, int64_t now\) \{.*?^\}", "oot_inventory_refresh") fix = re.search(r"^static int oot_first_night_fix\(Ctx \*c, const char \*map\) \{.*?^\}", host, re.M | re.S) body = FIXTURE.replace("// FIX", fix.group() if fix else "").replace("// REFRESH", refresh) with tempfile.TemporaryDirectory(prefix="oot-first-night-") as tmp: source = Path(tmp) / "test.c" source.write_text(body, encoding="utf-8") env = {k: v for k, v in os.environ.items() if k != "HOS_OOT_FIRST_NIGHT_FIX"} for name, flags, switch in (("on", [], {}), ("off", ["-DSWITCH_OFF"], {"HOS_OOT_FIRST_NIGHT_FIX": "0"})): binary = Path(tmp) / ("test-%s.exe" % name) subprocess.run([args.cc, "-std=gnu11", "-Wall", "-Wextra", "-Werror"] + flags + [str(source), "-o", str(binary)], check=True) subprocess.run([str(binary)], check=True, env=dict(env, **switch)) if __name__ == "__main__": main()