diff --git a/notes/systems/filesystem.md b/notes/systems/filesystem.md new file mode 100644 index 0000000..4581e62 --- /dev/null +++ b/notes/systems/filesystem.md @@ -0,0 +1,67 @@ +# Filesystem and packed archives (Repentance+ 1.9.7.17) + +EXACT = read in code (address given). `tools/import.py` implements all of it and was checked against every entry's +stored checksum (all 22 archives in the install, 100%). + +## Mounting and override order + +- `FontKerningPair::IsaacStartup @ 0x009aa040` loads archives from the table at `0xbfae60` (entries `{char* path, + uint* expectedChecksums, u16 count}`; a matching checksum only sets a per-file "official" flag), loop at `0x009aa990`: + animations, config, fonts, graphics, music, rooms, sfx, videos (`resources/packed/*.a`), afterbirth, afterbirthp, + repentance_de, _es, _jp, _kr, _ru, _zh, _fr (`packed/*.a`). Then `LoadArchiveFile("secret.a", 0, 0)` at `0x009aa9b2`. +- Not mounted: `repentance.a` (pre-Rep+ leftover, paths under `resources-dlc3/`, now merged into afterbirthp.a), + `afterbirth_jp/kr.a`, `afterbirthp_jp/kr.a`, and `keeper.a` (a file inside afterbirthp.a, no reference in the exe). +- `KAGE_Filesys_FileManager::LoadArchiveFile @ 0x00a179c0`: path through `FileSystem::ResolvePath`, then + `open_disk_file`, else `open_archive_file`. So an archive can live inside another: `secret.a` is + `resources/secret.a` in afterbirthp.a. At most 32 archives (paths at `DAT_00c37a14`, type byte at `DAT_00c37a18`). + Files go into one 0x8000-slot table (`DAT_00c37b18`), linear probing from `primary & 0x7fff`. An entry with the same + (primary, secondary) replaces the old one, so **later archives override earlier ones**. +- `FileSystem::Open @ 0x00a178d0`: `ResolvePath @ 0x00a17180` -> `find_mount @ 0x00a16c60`, which walks the mounts + last to first and, per mount, checks the archive table (mount prefix + path) **before** the mount's disk FileMap + (`FileMap::scan_directory @ 0x00a687f0`). Mounts come from `reload_resource_paths @ 0x009abbd0` / + `resources_mount_language @ 0x009ab970`: `resources`, then `resources.` (searched first). + So a packed file beats a loose file in `/resources`, and a language file beats the base one. + Mods redirect paths separately (`ModManager::TryRedirectPath @ 0x008f5ad0`). + +## Archive layout (`LoadArchiveFile @ 0x00a179c0`) + +- Header, 14 bytes: `"ARCH000"`, u8 type, u32 index offset, u16 file count. +- Index at the offset, 20 bytes per file: u32 primary hash, u32 secondary hash, u32 data offset, u32 size (unpacked), + u32 checksum. Names are not stored. +- Checksum: `c = 0xababeb98`, then per little-endian dword of the unpacked data `c = ror(c, 1) + dword`. Read in 0x200-byte + blocks, and a last partial dword is padded with stale bytes of the previous block (zero in the first). + +## Path hashes + +Paths are mount-prefixed (`resources/items.xml`, `resources.de/gfx/...`), ASCII-lowercased, `\` -> `/`. +- primary `GetPrimaryFilePathHash @ 0x00a159d0`: djb2, `h = 5381; h = h * 33 + c`. +- secondary `FUN_00a15ab0`: `h = 0x5bb2220e; h = (h ^ c) * 0x1000193` (FNV-1a with another seed). +- Lookup: `FileSystem::open_archive_file @ 0x00a17f40`. + +## Compression types + +`ArchivedFile` ctor `FUN_00a68a50`, reset/seek `FUN_00a68c50`, `refill_buffer @ 0x00a68cf0 / 0x00a69090` +(1 KiB output buffer), read `FUN_00a69510`. +- **0** (graphics, music, sfx, videos): `(size + 3) & ~3` bytes. Key `k = secondary ^ 0xf9524287 | 1`. Per dword: + `d ^= k`, then by `k & 15`: 2 reverses the 4 bytes, 9 swaps the bytes of each 16-bit half, 13 swaps the halves. + Then `k ^= k << 8; k ^= k >> 9; k ^= k << 23`. +- **1** LZW (animations, config, fonts, rooms): chunks `[u32 n <= 0x7ff][n bytes]`, each unpacking to <= 1 KiB. + `lzw_decompress @ 0x00a85320` (bit reader `FUN_00a85090`, string emit `FUN_00a85020`, init `FUN_00a850f0`, + encoder `lzw_compress @ 0x00a85140`): MSB-first codes, 8 bits wide at first, the width grows while + `nextCode >= 1 << width`, and the dictionary resets at 0xfff entries (followed by an 8-bit literal). The dictionary + carries across chunks. The first code of a chunk adds no entry. +- **2** miniz (afterbirth*, repentance_*): chunks `[u32 n | last << 31][n bytes]`, `FUN_00a89f80`. One raw-deflate stream + (`tinfl_decompress`, more-input flag unless last) until the first non-last chunk of exactly 0x400 bytes. That chunk and + all after it are stored, XORed with an ISAAC keystream (`FUN_00a89d70`): `FUN_00aa93e0(secondary)` fills randrsl with + 256 PCG32 outputs (state hi = s, lo = `((s << 15 ^ s) << 8) ^ (s >> 9) ^ s`, multiplier 0x5851f42d4c957f2d, + increment 0x7f, XSH-RR output), then standard `isaac_randinit @ 0x00aa9580` and `isaac` (`FUN_00aa94a0`). Words are + used from randrsl[0] upward, little-endian, and a new round runs after 256. +- **5** (secret.a only): bytes XORed with a fixed 128-byte key (built on the stack in `refill_buffer`), index + `position & 0x7f`. The 10 files are still high-entropy afterwards and their names are unknown. + +## XML parser + +The game's rapidxml never throws: `xml_document::parse @ 0x004155d0` / `parse_node @ 0x00416040` set `g_xmlParseError` +and keep going. Closing tag names are not checked, multiple roots are kept (ambush.xml, fxlayers.xml, info_display.xml, +seedmenu.xml), and bosspools.xml ends with `` for ``. Loaders walk every attribute +(`ItemConfig::load_locust @ 0x0072b230`), so a repeated attribute's last value wins (locusts.xml ids 441, 483 `scale`). diff --git a/src/data.cpp b/src/data.cpp index e064a75..f0e22d2 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -1,11 +1,15 @@ #include "data.h" +#include std::string dataDirectory = "resources/"; // Where a game path lives once imported: its converted JSON under the data directory. +// Paths are lowercased with forward slashes, as the game does before every lookup. std::string data_path(std::string_view path) { - return dataDirectory + std::string(path) + ".json"; + std::string normalized(path); + for (char& letter : normalized) letter = letter == '\\' ? '/' : (char)std::tolower((unsigned char)letter); + return dataDirectory + normalized + ".json"; } // Loads a converted document, leaving isLoaded false when it is missing or unreadable.