"""Check that a save holding the first adult night's time lock where the game can never let it go is repaired, and only there. The game holds the clock at 20:00 while the Bool 0x8592aa82 is 1 and quest 0x4dbca27f (Meet_DekuTree) is past its step Ready (f_8347c020, called from f_803edf70). 0x8592aa82 is `HrlField_HasEnteredFirstTime_Adult` by its MurmurHash3 (it was named HrlField_isFirstNightTime_Adult here, which is 0xf1024b60). Sheik's cutscene sets it the first time Link becomes an adult (Dm_TimeTemple_0055) and the adult Market's gate into Hyrule Field clears it on arrival (Dm_SY_BancChangeArrive_HyruleField_02): the night is the adult Market's until then, the game's own. A child holding it (no child's way out clears it) or an adult in Hyrule Field's map holding it (out without that arrival) is held for good. `oot_inventory_refresh` runs twice a second while a save is loaded and reads the map and age for it, so the repair lives there: clear the flag for those two through the game's own Bool setter (the live value and the save's copy: the live byte alone came back as 1 at the next load) and log it once; an adult anywhere else keeps it. 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. 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_HasEnteredFirstTime_Adult, the live value */ static int saved = 1; /* (its copy in the save) */ static int child = 1; /* Sequence_IsChildhood: 1 child, 0 adult */ static int age_known = 1; static int event_playing; /* the game's event manager has an active event */ static const char *map_name = "Market_Child"; static int cleared, live_only; 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 && age_known) { *value = child; return 1; } if (hash == 0x8592aa82u) { *value = flag; return 1; } return 0; } /* (the live byte alone: never for this flag, the save would keep 1) */ __attribute__((unused)) static int oot_gamedata_set_bool(Ctx *c, uint32_t hash, int value) { (void)c, (void)value; if (hash == 0x8592aa82u) live_only++; return 0; } 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; the game data manager) */ #define OOT_EVENTMGR_SLOT 0x84c9e090ULL #define OOT_GAMEDATA_SLOT 0x84c9e180ULL __attribute__((unused)) static uint64_t oot_singleton(Ctx *c, uint64_t slot) { (void)c; return slot == OOT_EVENTMGR_SLOT ? 0x1000 : slot == OOT_GAMEDATA_SLOT ? 0x2000 : 0; } __attribute__((unused)) static uint64_t recomp_load64(Ctx *c, uint64_t a) { (void)c; return a == 0x1000 + 104 ? (uint64_t)event_playing : 0; } /* (the game's SetBool: the live value and the save's copy) */ __attribute__((unused)) static int oot_gd_set(Ctx *c, uint64_t mgr, uint32_t hash, uint32_t value, int is_bool) { (void)c; if (mgr != 0x2000 || hash != 0x8592aa82u || !is_bool) return 0; if (flag != (int)value) cleared++; flag = saved = (int)value; return 1; } #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 || saved != want_flag || cleared != want_cleared || live_only) { fprintf(stderr, "FAIL: %s: flag %d saved %d (want %d), clears %d (want %d), live-only writes %d\n", what, flag, saved, want_flag, cleared, want_cleared, live_only); exit(1); } } static void at(Ctx *c, int64_t *now, int set, int is_child, const char *map) { *now += 500000000LL; flag = saved = set, child = is_child, map_name = map, cleared = 0; oot_inventory_refresh(c, *now); } 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. */ at(&c, &now, 1, 1, "Market_Child"); check("the switch off leaves the flag", 1, 0); at(&c, &now, 1, 0, "HyruleField_Adult"); check("the switch off leaves the flag in the Field", 1, 0); puts("PASS: HOS_OOT_FIRST_NIGHT_FIX=0 leaves the flag alone"); return 0; #else /* #1064: a child back from his first adult visit before the gate, in the Market: no child's way out clears it. */ at(&c, &now, 1, 1, "Market_Child"); check("a child in the Market", 0, 1); at(&c, &now, 1, 1, "HyruleField_Child"); check("a child in the Field", 0, 1); /* The report's save: an adult at Lon Lon Ranch, in Hyrule Field's map, held at 20:00 for good. */ at(&c, &now, 1, 0, "HyruleField_Adult"); check("an adult in the Field", 0, 1); /* The first night itself: an adult in the Market or the Temple of Time keeps it, the gate's arrival clears it. */ at(&c, &now, 1, 0, "Market_Adult"); check("an adult in the Market", 1, 0); at(&c, &now, 1, 0, "GanonCastle"); check("an adult outside Ganon's castle", 1, 0); /* Age not known: nothing to decide on. */ age_known = 0; at(&c, &now, 1, 1, "Market_Child"); check("the age not known", 1, 0); age_known = 1; /* A save that is already clear is not written again. */ at(&c, &now, 0, 1, "Market_Child"); check("a clear flag is left clear", 0, 0); /* No map name this refresh: nothing to decide on. */ at(&c, &now, 1, 1, ""); check("an empty map leaves the flag", 1, 0); /* An event running (the gate's own arrival, say) leaves the flag for a later refresh... */ event_playing = 1; at(&c, &now, 1, 0, "HyruleField_Adult"); check("an event running leaves the flag", 1, 0); /* ...and the event over, the next refresh clears it, if the event has not. */ now += 500000000LL; event_playing = 0, cleared = 0; oot_inventory_refresh(&c, now); check("after the event it clears", 0, 1); /* Twice a second: a refresh within the half second does nothing. */ flag = saved = 1, cleared = 0; oot_inventory_refresh(&c, now + 100000000LL); check("within the half second", 1, 0); puts("PASS: the first adult night's time lock is cleared for a child and for an adult in Hyrule Field, through the " "game's setter, and kept for an adult in the Market"); 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) # (the adult's map repair is asked from the same place: tests/test_oot_adult_map.py checks it; here it finds no flag) maps = re.search(r"^static int oot_adult_map_fix\(Ctx \*c, const char \*map\) \{.*?^\}", host, re.M | re.S) fixes = "\n\n".join(m.group() for m in (fix, maps) if m) body = FIXTURE.replace("// FIX", fixes).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()