Skip texture dictionaries when scanning for drawable dictionaries #9

Manually merged
github merged 1 commit from gh-pr/9/head into gh-pr/9/base 2026-11-26 19:04:48 +00:00
Owner

@⁠cxchhh on GitHub · 2026-11-22 03:51 UTC · original
cxchhh:codex/skip-texture-dictionaries → main · +37 −4 · 2 files · 1 commits · merged 2026-11-22 09:44 UTC

Why

LooksLikeDrawableDictionary checks only the dictionary header (page map, key and value arrays, matching counts). Texture dictionaries have the same header, so every texture dictionary becomes a model candidate. Probe then rejects it, because its values are textures and not embedded drawables. The scan reports these as candidates that do not recover:

Archive Candidates 917e9b9 Candidates with this PR Recovered (both)
textures.rpf 1,356 1 0
data.rpf 2,402 2,327 2,327
pack/shared_minigame000 1,070 1,052 1,052
pack/core_restofworld 1,024 873 873
levels.rpf 206,981 205,972 205,944
packs.rpf, pack/story000, story000_restofworld 28,034 28,034 28,033
Ten archives 240,867 238,259 238,229

Recovered resources, decoded models (350,251, counting each dictionary child), vertices and triangles are identical with and without this PR in every archive. Only the 2,608 candidates that never recover go away, so the candidates that fail drop from 2,638 to 30. Every scan, recovery and decode was run on the 1.0.2.1 package.

In levels.rpf, 1,037 candidates fail at 917e9b9. 1,009 of them are texture dictionaries rather than models that fail to decode. That is the same count as the 1,009 tail-page rejects reported in ItsNotRexed/Leonida#29.

Change

  • LooksLikeDrawableDictionary also requires the first value to pass LooksLikeDrawableHeader(…, embedded: true). If the bytes it is given do not reach the first value, the header alone decides, as before, and Probe still checks every child.
  • New DrawableDictionaryCheckBytes returns how many system bytes are needed to see the first value's header.
  • The scanner inflates up to that point instead of only 176 bytes. This takes two steps at most: the value array entry, then the drawable header. It is capped at 64 KiB, and is done only for pages that already look like a dictionary.

Drawables, fragments and drawable dictionaries are recovered exactly as before; see the table.

Tests

main ignores Tests/, so there is no test project in the PR. A fixture is below; it builds a two-child drawable dictionary and the same dictionary with a non-drawable first value. It checks:

  • the header check, and that the second dictionary is not a model resource;
  • the byte counts the scanner needs, both before and after the value array;
  • that the scanner finds the first dictionary and skips the second, which needs inflation past 176 bytes;
  • recovery and decoding of the first dictionary.

Without the change it fails at "a dictionary of non-drawables is not a drawable dictionary". The fixture is generated in code and contains no game data.

Copy both files to Tests/DictionaryCandidateSmoke/ and run dotnet run --project Tests/DictionaryCandidateSmoke -c Release.

Tests/DictionaryCandidateSmoke/DictionaryCandidateSmoke.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="../../Core/RpfPreview.Core.csproj" />
  </ItemGroup>
</Project>
Tests/DictionaryCandidateSmoke/Program.cs
using System.Buffers.Binary;
using System.IO.Compression;
using System.Text;
using RpfPreview.Core;

// Texture dictionaries share the drawable dictionary header. Only a dictionary whose first value is an embedded
// drawable is a model candidate. The fixture is generated in code (two one-triangle drawables); no game data.
const long CandidateOffset = 4096 + 16;
string[] names = new[] { "tile_b", "tile_a" }.OrderBy(Hash).ToArray();
var good = MakeDictionary(names, names.Select(Hash).ToArray());
// Same header, but the first value is not an embedded drawable (it has a page map of its own), like a texture.
var textures = MakeDictionary(names, names.Select(Hash).ToArray());
BinaryPrimitives.WriteUInt64LittleEndian(textures.System.AsSpan(textures.FirstDrawable + 8), 0x50000100);

Check(RageModelDecoder.LooksLikeDrawableDictionary(good.System), "drawable dictionary");
Check(!RageModelDecoder.LooksLikeDrawableDictionary(textures.System), "a dictionary of non-drawables is not a drawable dictionary");
Check(!RageModelDecoder.LooksLikeResource(textures.System), "a dictionary of non-drawables is not a model resource");
Check(RageModelDecoder.DrawableDictionaryCheckBytes(good.System) == good.FirstDrawable + 176, "bytes needed to check the first value");
Check(RageModelDecoder.DrawableDictionaryCheckBytes(good.System.AsSpan(0, 176)) == 0x180 + 8, "value array lies beyond the first 176 bytes");
Check(RageModelDecoder.LooksLikeDrawableDictionary(good.System.AsSpan(0, 176)), "the header alone decides when the first value is out of reach");
Check(RageModelDecoder.Decode(good.System, good.Graphics) is { IsDictionaryChild: true }, "a dictionary child decodes");

var directory = Directory.CreateTempSubdirectory("rpf9-dictionary-candidate-test-");
string path = Path.Combine(directory.FullName, "dictionary.rpf");
void WriteArchive((byte[] System, byte[] Graphics, int FirstDrawable) fixture)
{
    using var file = File.Create(path);
    var header = new byte[64]; "9FPR"u8.CopyTo(header); file.Write(header);
    file.Position = CandidateOffset;
    foreach (var segment in new[] { fixture.System, fixture.Graphics })
    {
        using (var z = new ZLibStream(file, CompressionLevel.Optimal, leaveOpen: true)) z.Write(segment);
        file.Write(new byte[(int)((-file.Position) & 15)]);
    }
}
try
{
    // The scanner has to inflate past the first 176 bytes to reach the value array and the first value.
    WriteArchive(textures);
    Check(Rpf9ModelRecovery.FindCandidateOffsets(path).Count == 0, "the scanner skips a dictionary of non-drawables");
    WriteArchive(good);
    var candidates = Rpf9ModelRecovery.FindCandidateOffsets(path);
    Check(candidates.SequenceEqual([CandidateOffset]), "the scanner finds the drawable dictionary");
    Check(Rpf9ModelRecovery.RecoverAtOffsets(path, new FileInfo(path).Length, candidates).Count > 0, "recovery returns the dictionary");
}
finally
{
    string cache = RecoveryIndexCache.CachePath("models", path);
    if (File.Exists(cache)) File.Delete(cache);
    File.Delete(path);
    directory.Delete();
}
Console.WriteLine("Dictionary candidate checks passed.");

static void Check(bool condition, string what)
{
    if (!condition) throw new Exception("Failed: " + what);
}

static uint Hash(string name)
{
    uint h = 0;
    foreach (byte c in Encoding.UTF8.GetBytes(name.ToLowerInvariant())) { h += c; h += h << 10; h ^= h >> 6; }
    h += h << 3; h ^= h >> 11; h += h << 15;
    return h;
}

static (byte[] System, byte[] Graphics, int FirstDrawable) MakeDictionary(string[] names, uint[] keys)
{
    const int Pages = 0x100, Hashes = 0x140, Values = 0x180, Base = 0x400, Stride = 0x800;
    const int PositionOffset = 96, VertexCount = 3, IndexLength = 48, GraphicsStride = 256;
    int n = names.Length;
    byte[] system = new byte[Base + n * Stride], graphics = new byte[n * GraphicsStride];
    void S(int at, int address) => BinaryPrimitives.WriteUInt64LittleEndian(system.AsSpan(at), 0x50000000UL + (uint)address);
    void G(int at, int address) => BinaryPrimitives.WriteUInt64LittleEndian(system.AsSpan(at), 0x60000000UL + (uint)address);
    void U16(int at, ushort value) => BinaryPrimitives.WriteUInt16LittleEndian(system.AsSpan(at), value);
    void U32(byte[] bytes, int at, uint value) => BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(at), value);
    void F32(byte[] bytes, int at, float value) => BinaryPrimitives.WriteSingleLittleEndian(bytes.AsSpan(at), value);

    // Dictionary header: page map, no parent, one reference, keys and values with count/capacity pairs.
    S(8, Pages); BinaryPrimitives.WriteUInt64LittleEndian(system.AsSpan(24), 1);
    S(32, Hashes); U16(40, (ushort)n); U16(42, (ushort)n);
    S(48, Values); U16(56, (ushort)n); U16(58, (ushort)n);
    system[Pages + 8] = system[Pages + 9] = 1;
    for (int k = 0; k < n; k++)
    {
        int d = Base + k * Stride, g = k * GraphicsStride;
        int name = d + 0x180, shader = d + 0x1C0, lod = d + 0x200, models = d + 0x220, part = d + 0x240, geometries = d + 0x280;
        int bounds = d + 0x2A0, geometry = d + 0x300, vb = d + 0x3A0, descriptor = d + 0x460, ib = d + 0x480, fvf = d + 0x500;
        U32(system, Hashes + k * 4, keys[k]); S(Values + k * 8, d);
        Encoding.ASCII.GetBytes(names[k] + ".#dr").CopyTo(system, name);
        // Embedded drawable: no page map of its own. Entry k spans x in [0, k + 1].
        S(d + 16, shader); S(d + 80, lod); S(d + 168, name);
        F32(system, d + 44, 2);
        F32(system, d + 64, k + 1); F32(system, d + 68, 1); F32(system, d + 72, 1);
        F32(system, bounds + 16, k + 1); F32(system, bounds + 20, 1); F32(system, bounds + 24, 1);
        S(lod, models); U16(lod + 8, 1); U16(lod + 10, 1); S(models, part);
        S(part + 8, geometries); U16(part + 16, 1); U16(part + 18, 1); U16(part + 54, 1); S(part + 24, bounds);
        S(geometries, geometry); S(geometry + 8, vb); S(geometry + 64, descriptor);
        U32(system, vb + 8, PositionOffset + VertexCount * 12); U16(vb + 12, 1); G(vb + 24, g); S(vb + 64, fvf);
        U32(system, fvf, PositionOffset);
        system[fvf + 0xD0] = 12; system[fvf + 0x104] = 6;
        U32(system, fvf + 0x138, (VertexCount << 10) | 1);
        U32(graphics, g, 0x63);
        F32(graphics, g + PositionOffset + 12, k + 1);
        F32(graphics, g + PositionOffset + 24, k + 1); F32(graphics, g + PositionOffset + 28, 1); F32(graphics, g + PositionOffset + 32, 1);
        int index = g + 160;
        S(descriptor, ib); U16(descriptor + 8, 1); U16(descriptor + 10, 1);
        U32(system, descriptor + 12, VertexCount << 8);
        U32(system, ib + 8, IndexLength / 4); U16(ib + 12, 4); G(ib + 24, index);
        U32(graphics, index, 0x80000001);
        U32(graphics, index + 4, 1); U32(graphics, index + 8, VertexCount);
        U32(graphics, index + 12, VertexCount - 1);
        U32(graphics, index + 28, (2u << 14) | (1u << 7));
        graphics[index + 34] = 1; graphics[index + 36] = 2;
    }
    return (system, graphics, Base);
}

Testing environment

Tested on Linux x64 with .NET SDK 8.0.428, CPU only. Core builds with warnings as errors. I could not build the Windows app on this machine, and the app code is unchanged.

AI-assisted: an AI agent (Claude, Anthropic) did the investigation, code, tests and measurements on the author's behalf. No game data is included here.

🤖 Generated with Claude Code

**@⁠cxchhh** on GitHub · 2026-11-22 03:51 UTC · [original](https://github.com/breadbbbb/rpf9-explorer/pull/9) `cxchhh:codex/skip-texture-dictionaries` → `main` · +37 −4 · 2 files · 1 commits · merged 2026-11-22 09:44 UTC ## Why `LooksLikeDrawableDictionary` checks only the dictionary header (page map, key and value arrays, matching counts). Texture dictionaries have the same header, so every texture dictionary becomes a model candidate. `Probe` then rejects it, because its values are textures and not embedded drawables. The scan reports these as candidates that do not recover: | Archive | Candidates `917e9b9` | Candidates with this PR | Recovered (both) | |---|---:|---:|---:| | `textures.rpf` | 1,356 | 1 | 0 | | `data.rpf` | 2,402 | 2,327 | 2,327 | | `pack/shared_minigame000` | 1,070 | 1,052 | 1,052 | | `pack/core_restofworld` | 1,024 | 873 | 873 | | `levels.rpf` | 206,981 | 205,972 | 205,944 | | `packs.rpf`, `pack/story000`, `story000_restofworld` | 28,034 | 28,034 | 28,033 | | **Ten archives** | **240,867** | **238,259** | **238,229** | Recovered resources, decoded models (350,251, counting each dictionary child), vertices and triangles are identical with and without this PR in every archive. Only the 2,608 candidates that never recover go away, so the candidates that fail drop from 2,638 to 30. Every scan, recovery and decode was run on the 1.0.2.1 package. In `levels.rpf`, 1,037 candidates fail at `917e9b9`. 1,009 of them are texture dictionaries rather than models that fail to decode. That is the same count as the 1,009 tail-page rejects reported in ItsNotRexed/Leonida#29. ## Change - `LooksLikeDrawableDictionary` also requires the first value to pass `LooksLikeDrawableHeader(…, embedded: true)`. If the bytes it is given do not reach the first value, the header alone decides, as before, and `Probe` still checks every child. - New `DrawableDictionaryCheckBytes` returns how many system bytes are needed to see the first value's header. - The scanner inflates up to that point instead of only 176 bytes. This takes two steps at most: the value array entry, then the drawable header. It is capped at 64 KiB, and is done only for pages that already look like a dictionary. Drawables, fragments and drawable dictionaries are recovered exactly as before; see the table. ## Tests `main` ignores `Tests/`, so there is no test project in the PR. A fixture is below; it builds a two-child drawable dictionary and the same dictionary with a non-drawable first value. It checks: - the header check, and that the second dictionary is not a model resource; - the byte counts the scanner needs, both before and after the value array; - that the scanner finds the first dictionary and skips the second, which needs inflation past 176 bytes; - recovery and decoding of the first dictionary. Without the change it fails at "a dictionary of non-drawables is not a drawable dictionary". The fixture is generated in code and contains no game data. Copy both files to `Tests/DictionaryCandidateSmoke/` and run `dotnet run --project Tests/DictionaryCandidateSmoke -c Release`. <details><summary><code>Tests/DictionaryCandidateSmoke/DictionaryCandidateSmoke.csproj</code></summary> ```xml <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <ProjectReference Include="../../Core/RpfPreview.Core.csproj" /> </ItemGroup> </Project> ``` </details> <details><summary><code>Tests/DictionaryCandidateSmoke/Program.cs</code></summary> ```csharp using System.Buffers.Binary; using System.IO.Compression; using System.Text; using RpfPreview.Core; // Texture dictionaries share the drawable dictionary header. Only a dictionary whose first value is an embedded // drawable is a model candidate. The fixture is generated in code (two one-triangle drawables); no game data. const long CandidateOffset = 4096 + 16; string[] names = new[] { "tile_b", "tile_a" }.OrderBy(Hash).ToArray(); var good = MakeDictionary(names, names.Select(Hash).ToArray()); // Same header, but the first value is not an embedded drawable (it has a page map of its own), like a texture. var textures = MakeDictionary(names, names.Select(Hash).ToArray()); BinaryPrimitives.WriteUInt64LittleEndian(textures.System.AsSpan(textures.FirstDrawable + 8), 0x50000100); Check(RageModelDecoder.LooksLikeDrawableDictionary(good.System), "drawable dictionary"); Check(!RageModelDecoder.LooksLikeDrawableDictionary(textures.System), "a dictionary of non-drawables is not a drawable dictionary"); Check(!RageModelDecoder.LooksLikeResource(textures.System), "a dictionary of non-drawables is not a model resource"); Check(RageModelDecoder.DrawableDictionaryCheckBytes(good.System) == good.FirstDrawable + 176, "bytes needed to check the first value"); Check(RageModelDecoder.DrawableDictionaryCheckBytes(good.System.AsSpan(0, 176)) == 0x180 + 8, "value array lies beyond the first 176 bytes"); Check(RageModelDecoder.LooksLikeDrawableDictionary(good.System.AsSpan(0, 176)), "the header alone decides when the first value is out of reach"); Check(RageModelDecoder.Decode(good.System, good.Graphics) is { IsDictionaryChild: true }, "a dictionary child decodes"); var directory = Directory.CreateTempSubdirectory("rpf9-dictionary-candidate-test-"); string path = Path.Combine(directory.FullName, "dictionary.rpf"); void WriteArchive((byte[] System, byte[] Graphics, int FirstDrawable) fixture) { using var file = File.Create(path); var header = new byte[64]; "9FPR"u8.CopyTo(header); file.Write(header); file.Position = CandidateOffset; foreach (var segment in new[] { fixture.System, fixture.Graphics }) { using (var z = new ZLibStream(file, CompressionLevel.Optimal, leaveOpen: true)) z.Write(segment); file.Write(new byte[(int)((-file.Position) & 15)]); } } try { // The scanner has to inflate past the first 176 bytes to reach the value array and the first value. WriteArchive(textures); Check(Rpf9ModelRecovery.FindCandidateOffsets(path).Count == 0, "the scanner skips a dictionary of non-drawables"); WriteArchive(good); var candidates = Rpf9ModelRecovery.FindCandidateOffsets(path); Check(candidates.SequenceEqual([CandidateOffset]), "the scanner finds the drawable dictionary"); Check(Rpf9ModelRecovery.RecoverAtOffsets(path, new FileInfo(path).Length, candidates).Count > 0, "recovery returns the dictionary"); } finally { string cache = RecoveryIndexCache.CachePath("models", path); if (File.Exists(cache)) File.Delete(cache); File.Delete(path); directory.Delete(); } Console.WriteLine("Dictionary candidate checks passed."); static void Check(bool condition, string what) { if (!condition) throw new Exception("Failed: " + what); } static uint Hash(string name) { uint h = 0; foreach (byte c in Encoding.UTF8.GetBytes(name.ToLowerInvariant())) { h += c; h += h << 10; h ^= h >> 6; } h += h << 3; h ^= h >> 11; h += h << 15; return h; } static (byte[] System, byte[] Graphics, int FirstDrawable) MakeDictionary(string[] names, uint[] keys) { const int Pages = 0x100, Hashes = 0x140, Values = 0x180, Base = 0x400, Stride = 0x800; const int PositionOffset = 96, VertexCount = 3, IndexLength = 48, GraphicsStride = 256; int n = names.Length; byte[] system = new byte[Base + n * Stride], graphics = new byte[n * GraphicsStride]; void S(int at, int address) => BinaryPrimitives.WriteUInt64LittleEndian(system.AsSpan(at), 0x50000000UL + (uint)address); void G(int at, int address) => BinaryPrimitives.WriteUInt64LittleEndian(system.AsSpan(at), 0x60000000UL + (uint)address); void U16(int at, ushort value) => BinaryPrimitives.WriteUInt16LittleEndian(system.AsSpan(at), value); void U32(byte[] bytes, int at, uint value) => BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(at), value); void F32(byte[] bytes, int at, float value) => BinaryPrimitives.WriteSingleLittleEndian(bytes.AsSpan(at), value); // Dictionary header: page map, no parent, one reference, keys and values with count/capacity pairs. S(8, Pages); BinaryPrimitives.WriteUInt64LittleEndian(system.AsSpan(24), 1); S(32, Hashes); U16(40, (ushort)n); U16(42, (ushort)n); S(48, Values); U16(56, (ushort)n); U16(58, (ushort)n); system[Pages + 8] = system[Pages + 9] = 1; for (int k = 0; k < n; k++) { int d = Base + k * Stride, g = k * GraphicsStride; int name = d + 0x180, shader = d + 0x1C0, lod = d + 0x200, models = d + 0x220, part = d + 0x240, geometries = d + 0x280; int bounds = d + 0x2A0, geometry = d + 0x300, vb = d + 0x3A0, descriptor = d + 0x460, ib = d + 0x480, fvf = d + 0x500; U32(system, Hashes + k * 4, keys[k]); S(Values + k * 8, d); Encoding.ASCII.GetBytes(names[k] + ".#dr").CopyTo(system, name); // Embedded drawable: no page map of its own. Entry k spans x in [0, k + 1]. S(d + 16, shader); S(d + 80, lod); S(d + 168, name); F32(system, d + 44, 2); F32(system, d + 64, k + 1); F32(system, d + 68, 1); F32(system, d + 72, 1); F32(system, bounds + 16, k + 1); F32(system, bounds + 20, 1); F32(system, bounds + 24, 1); S(lod, models); U16(lod + 8, 1); U16(lod + 10, 1); S(models, part); S(part + 8, geometries); U16(part + 16, 1); U16(part + 18, 1); U16(part + 54, 1); S(part + 24, bounds); S(geometries, geometry); S(geometry + 8, vb); S(geometry + 64, descriptor); U32(system, vb + 8, PositionOffset + VertexCount * 12); U16(vb + 12, 1); G(vb + 24, g); S(vb + 64, fvf); U32(system, fvf, PositionOffset); system[fvf + 0xD0] = 12; system[fvf + 0x104] = 6; U32(system, fvf + 0x138, (VertexCount << 10) | 1); U32(graphics, g, 0x63); F32(graphics, g + PositionOffset + 12, k + 1); F32(graphics, g + PositionOffset + 24, k + 1); F32(graphics, g + PositionOffset + 28, 1); F32(graphics, g + PositionOffset + 32, 1); int index = g + 160; S(descriptor, ib); U16(descriptor + 8, 1); U16(descriptor + 10, 1); U32(system, descriptor + 12, VertexCount << 8); U32(system, ib + 8, IndexLength / 4); U16(ib + 12, 4); G(ib + 24, index); U32(graphics, index, 0x80000001); U32(graphics, index + 4, 1); U32(graphics, index + 8, VertexCount); U32(graphics, index + 12, VertexCount - 1); U32(graphics, index + 28, (2u << 14) | (1u << 7)); graphics[index + 34] = 1; graphics[index + 36] = 2; } return (system, graphics, Base); } ``` </details> ## Testing environment Tested on Linux x64 with .NET SDK 8.0.428, CPU only. `Core` builds with warnings as errors. I could not build the Windows app on this machine, and the app code is unchanged. AI-assisted: an AI agent (Claude, Anthropic) did the investigation, code, tests and measurements on the author's behalf. No game data is included here. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Texture dictionaries share the drawable dictionary header, so the header
check made every texture dictionary a model candidate that the probe then
rejected. In levels.rpf these are the 1,009 candidates that never recover;
textures.rpf has 1,355 of them and no models.

Require the first value to be an embedded drawable. The scanner inflates
up to that value's header (two steps at most, capped at 64 KiB) to check
it; when the value lies further in, the header decides and the per-child
checks reject the dictionary as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:04:47 +00:00
github manually merged commit 82b2341e58 into gh-pr/9/base 2026-11-26 19:04:48 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.