mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 22:08:40 +00:00
Models and textures no longer open the archive document. The entry list comes from this run's scan or from the index cache, and every resource is read from its own compression blocks (tools/rpf9dump/Segments.cs), which mirrors the checks ArchiveReader.ReadResourceData makes. That makes a raw slice of an archive exportable: a window cut out of levels.rpf has no 9FPR header, so there was nothing to open before, and the mode simply failed on it. --start/--length scan one window instead of the whole file. A sub-range is never written to the index cache, which is keyed by the file rather than by the window; the manifest records Scan.Start and Scan.Length for it. The manifest gains MeshTris, MeshModels and MeshGeoms: the per-geometry triangles and the (model, geometry) pair the decoder puts in each mesh name, in the order of the OBJ's `o` groups. A dehydrated BLAS leaf stores that pair as its geometry index (docs/bvh.md 4a), so a matching tool can filter candidates on the manifest instead of reading every OBJ. Measured: one 1 GiB slice of levels.rpf scans, recovers, decodes and exports 1,228 models in about 2 s. The project builds clean with no warnings; tests/Rpf9DumpDiagnostics covers the segment reader (compressed and raw blocks, a block past the end, a short block, an entry without blocks) and the mesh key parsing, and still passes.
48 lines
2.7 KiB
C#
48 lines
2.7 KiB
C#
// Helpers shared by rpf9dump's exporters (linked into tests/Rpf9DumpDiagnostics too).
|
|
using RpfPreview.Core;
|
|
|
|
public static class Util
|
|
{
|
|
// Every output is written to <path>.partial and renamed into place. The exporters skip any file that already
|
|
// exists, and make_game.py takes manifest.json / audio_manifest.json as proof a step finished, so a file cut
|
|
// short by a stopped run or a full disk must never appear under its final name.
|
|
public static void WriteAtomic(string path, Action<string> write)
|
|
{
|
|
string tmp = path + ".partial";
|
|
if (File.Exists(tmp)) File.Delete(tmp); // left by a stopped run; some writers open with CreateNew
|
|
try { write(tmp); File.Move(tmp, path, true); }
|
|
finally { if (File.Exists(tmp)) File.Delete(tmp); }
|
|
}
|
|
public static void WriteAllBytes(string path, byte[] data) => WriteAtomic(path, t => File.WriteAllBytes(t, data));
|
|
public static void WriteAllText(string path, string text) => WriteAtomic(path, t => File.WriteAllText(t, text));
|
|
|
|
// The decoder names every geometry mesh `model_{mi}_geometry_{gi}`, where mi is the position of the model in the
|
|
// drawable's primary-LOD model list and gi the position of the geometry in that model - the pair a dehydrated
|
|
// BLAS leaf stores as its geometry index (docs/bvh.md §4a). Other spellings give -1.
|
|
public static (int Model, int Geometry) MeshKey(string name)
|
|
{
|
|
string[] parts = name.Split('_');
|
|
if (parts.Length == 4 && parts[0] == "model" && parts[2] == "geometry" &&
|
|
int.TryParse(parts[1], System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out int model) &&
|
|
int.TryParse(parts[3], System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out int geometry))
|
|
return (model, geometry);
|
|
return (-1, -1);
|
|
}
|
|
|
|
public static string Safe(string s)
|
|
{
|
|
var bad = Path.GetInvalidFileNameChars();
|
|
var c = s.Select(ch => bad.Contains(ch) || char.IsControl(ch) ? '_' : ch).ToArray();
|
|
var v = new string(c).Trim(' ', '.'); return v.Length == 0 ? "x" : v[..Math.Min(120, v.Length)];
|
|
}
|
|
// Uncompressed 32-bit BGRA DDS (legacy header, A8R8G8B8 masks).
|
|
public static byte[] BgraDds(RageTexturePixels px)
|
|
{
|
|
var o = new byte[128 + px.Bgra32.Length];
|
|
void U(int at, uint v) => BitConverter.TryWriteBytes(o.AsSpan(at), v);
|
|
U(0, 0x20534444); U(4, 124); U(8, 0x100F); U(12, (uint)px.Height); U(16, (uint)px.Width); U(20, (uint)(px.Width * 4));
|
|
U(76, 32); U(80, 0x41); U(88, 32); U(92, 0x00FF0000); U(96, 0x0000FF00); U(100, 0x000000FF); U(104, 0xFF000000); U(108, 0x1000);
|
|
px.Bgra32.CopyTo(o, 128); return o;
|
|
}
|
|
}
|