upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/rpf9dump/Program.cs
diff options
context:
space:
mode:
authorjaybeebabageyus <jaybeebabageyus@users.noreply.github.com>2026-11-22 20:52:44 +0100
committerjaybeebabageyus <jaybeebabageyus@users.noreply.github.com>2026-11-22 20:52:44 +0100
commit54bb91389d04bfd5321ab2f9065f74ee7f312d5c (patch)
tree5680762e34b997e221a2180ecd945a92bf0a3d2c /tools/rpf9dump/Program.cs
parent01e27846e4a7fecdeba421df2e26afaec4261732 (diff)
rpf9dump: fix the write-failure fixture's page-count overlap; count embedded, directory and read-I/O failures as write failures
The fixture wrote the resource page counts at 0x88/0x89, which is the texture descriptor's width field (t+0x38 at t=0x50): width read 257 and Parse rejected the record (layout 20480 vs allocation 4096), so no entry reached Export and the committed test failed before any DDS write. Move the page-count pointer to 0x40 (counts at 0x48, below the descriptor); the same binary now recovers one texture, hits the blocked DDS write, withholds manifest.json and exits 1. The test also asserts the DDS is absent, not only the return code. Review gaps closed: embedded-texture DDS writes are no longer swallowed by catch {} (decode failures count like the textures branch, write failures count into writeFail and fail the run); dictionary-directory and models _tex directory creation failures return WriteFail=1; a retryable IOException while reading a resource or decoding (CreateDds) also counts as a write failure, so a locked source no longer publishes a manifest with missing files; marker withdrawal goes through WithdrawMarker, which clears a read-only attribute first and turns any remaining delete failure into a one-line error with exit 1 instead of a traceback. Verified offline: Program.cs compiles 0 warnings/0 errors against the real Core sources with a BCnEncoder stub (BCnEncoder.Net is not cached offline, so the DLL build runs in CI); a Python mirror of Recover/ RecoverAtOffsets/TryInflate/LooksLikeDictionary/Parse/ComputeLayout recovers the committed fixture 1/1 (system 208 B, graphics 4096 B, pages 1/1, texture 'probe' 4x4, allocation 4096) through both scan paths, and rejects the pre-fix bytes with the reported mismatch (layout 20480 != allocation 4096). python -I skip path: 2 skipped, OK.
Diffstat (limited to 'tools/rpf9dump/Program.cs')
-rw-r--r--tools/rpf9dump/Program.cs45
1 files changed, 36 insertions, 9 deletions
diff --git a/tools/rpf9dump/Program.cs b/tools/rpf9dump/Program.cs
index f36edae29..e45c1e57d 100644
--- a/tools/rpf9dump/Program.cs
+++ b/tools/rpf9dump/Program.cs
@@ -53,7 +53,10 @@ if (mode == "ttpf")
53 if (fail == 0) 53 if (fail == 0)
54 Util.WriteAllText(manifest, JsonSerializer.Serialize(new { src, info.Version, Count = info.Textures.Count, info.Metadata, Textures = rows }, json)); 54 Util.WriteAllText(manifest, JsonSerializer.Serialize(new { src, info.Version, Count = info.Textures.Count, info.Metadata, Textures = rows }, json));
55 else 55 else
56 File.Delete(manifest); 56 {
57 try { WithdrawMarker(manifest); }
58 catch (Exception ex) { Console.WriteLine($" cannot withdraw the stale manifest: {ex.GetType().Name} {ex.Message}"); return 1; }
59 }
57} 60}
58else if (mode == "audio") 61else if (mode == "audio")
59{ 62{
@@ -204,14 +207,24 @@ else if (mode is "models" or "textures")
204 // unlike the decode failures above, which no rerun would change. 207 // unlike the decode failures above, which no rerun would change.
205 try { if (!File.Exists(obj)) Util.WriteAllText(obj, objText); } 208 try { if (!File.Exists(obj)) Util.WriteAllText(obj, objText); }
206 catch (Exception wex) { return new Result(null, 0, 1, 0, null, $" fail {e.Name}: {wex.GetType().Name} {wex.Message}", 1); } 209 catch (Exception wex) { return new Result(null, 0, 1, 0, null, $" fail {e.Name}: {wex.GetType().Name} {wex.Message}", 1); }
210 int wfail = 0;
207 if (m.EmbeddedTextures != null && m.EmbeddedTextureDictionaryOffset is int dOff) 211 if (m.EmbeddedTextures != null && m.EmbeddedTextureDictionaryOffset is int dOff)
208 { 212 {
209 string td = Path.Combine(sub, stem + "_tex"); Directory.CreateDirectory(td); 213 // A directory or DDS write failure leaves the embedded texture missing from disk: retryable,
214 // so it fails the run like the obj write above. Embedded decode failures count like the
215 // textures branch counts dictionary decode failures: reported, non-fatal for the run.
216 string td;
217 try { td = Path.Combine(sub, stem + "_tex"); Directory.CreateDirectory(td); }
218 catch (Exception wex) { return new Result(null, 0, 1, 0, null, $" fail {e.Name}: {wex.GetType().Name} {wex.Message}", 1); }
210 foreach (var t in m.EmbeddedTextures) 219 foreach (var t in m.EmbeddedTextures)
211 { 220 {
212 string p = Path.Combine(td, $"{t.Index:D3}_{Util.Safe(t.Name)}.dds"); 221 string p = Path.Combine(td, $"{t.Index:D3}_{Util.Safe(t.Name)}.dds");
213 if (File.Exists(p)) { embedded++; continue; } 222 if (File.Exists(p)) { embedded++; continue; }
214 try { Util.WriteAllBytes(p, Util.BgraDds(RageTextureDecoder.DecodeEmbeddedTexturePixels(sys, gfx, dOff, t.Index))); embedded++; } catch { } 223 byte[] dds;
224 try { dds = Util.BgraDds(RageTextureDecoder.DecodeEmbeddedTexturePixels(sys, gfx, dOff, t.Index)); }
225 catch { fail++; continue; }
226 try { Util.WriteAllBytes(p, dds); embedded++; }
227 catch (Exception wex) { fail++; wfail++; if (wfail <= 5) Console.WriteLine($" fail {p}: {wex.GetType().Name} {wex.Message}"); }
215 } 228 }
216 } 229 }
217 return new Result(new { e.Offset, Drawable = e.ResourceDrawableOffset, File = stem + ".obj", m.Name, Meshes = m.Meshes.Count, Verts = m.Meshes.Sum(x => x.Positions.Count), Tris = m.Meshes.Sum(x => x.Indices.Count) / 3, m.IsFragment, m.HasBoneBindings, 230 return new Result(new { e.Offset, Drawable = e.ResourceDrawableOffset, File = stem + ".obj", m.Name, Meshes = m.Meshes.Count, Verts = m.Meshes.Sum(x => x.Positions.Count), Tris = m.Meshes.Sum(x => x.Indices.Count) / 3, m.IsFragment, m.HasBoneBindings,
@@ -220,22 +233,26 @@ else if (mode is "models" or "textures")
220 Embedded = m.EmbeddedTextures?.Select(t => new { t.Index, t.Name, t.Width, t.Height, t.Format, Hash = t.NameHash.ToString("X8"), Key = t.AssetKey?.ToString("X16") }).ToArray(), 233 Embedded = m.EmbeddedTextures?.Select(t => new { t.Index, t.Name, t.Width, t.Height, t.Format, Hash = t.NameHash.ToString("X8"), Key = t.AssetKey?.ToString("X16") }).ToArray(),
221 Materials = m.Materials?.Select(x => new { x.ShaderIndex, Shader = x.ShaderHash.ToString("X8"), Preset = x.PresetHash.ToString("X8"), x.RenderBucket, 234 Materials = m.Materials?.Select(x => new { x.ShaderIndex, Shader = x.ShaderHash.ToString("X8"), Preset = x.PresetHash.ToString("X8"), x.RenderBucket,
222 Textures = x.Textures.Select(r => new { Param = r.ParameterHash.ToString("X8"), r.TextureIndex, Name = r.NameHash.ToString("X8"), Id64 = r.OpaqueId64.ToString("X16"), Tag = r.Tag.ToString("X8") }).ToArray() }).ToArray() }, 235 Textures = x.Textures.Select(r => new { Param = r.ParameterHash.ToString("X8"), r.TextureIndex, Name = r.NameHash.ToString("X8"), Id64 = r.OpaqueId64.ToString("X16"), Tag = r.Tag.ToString("X8") }).ToArray() }).ToArray() },
223 1, 0, embedded, null, null, 0); 236 1, fail, embedded, null, null, wfail);
224 } 237 }
225 else 238 else
226 { 239 {
227 var dict = RageTextureDecoder.Inspect(sys, gfx); 240 var dict = RageTextureDecoder.Inspect(sys, gfx);
228 if (dict == null) return new Result(null, 0, 1, 0, null, null, 0); 241 if (dict == null) return new Result(null, 0, 1, 0, null, null, 0);
229 string td = Path.Combine(sub, stem); Directory.CreateDirectory(td); 242 string td;
243 try { td = Path.Combine(sub, stem); Directory.CreateDirectory(td); }
244 catch (Exception wex) { return new Result(null, 0, 1, 0, null, $" fail {e.Name}: {wex.GetType().Name} {wex.Message}", 1); }
230 int wfail = 0; 245 int wfail = 0;
231 foreach (var t in dict.Textures) 246 foreach (var t in dict.Textures)
232 { 247 {
233 string p = Path.Combine(td, $"{t.Index:D3}_{Util.Safe(t.Name)}.dds"); 248 string p = Path.Combine(td, $"{t.Index:D3}_{Util.Safe(t.Name)}.dds");
234 if (File.Exists(p)) { ok++; continue; } 249 if (File.Exists(p)) { ok++; continue; }
235 // A decoder problem is counted like today and no rerun would change it; a failed DDS write 250 // A decoder problem is counted like today and no rerun would change it; a failed DDS write
236 // is retryable, so it additionally counts as a write failure and fails the run. 251 // is retryable, so it additionally counts as a write failure and fails the run. A retryable
252 // read I/O error on the resource belongs to the same retryable class.
237 byte[] dds; 253 byte[] dds;
238 try { dds = RageTextureDecoder.CreateDds(sys, gfx, t.Index); } 254 try { dds = RageTextureDecoder.CreateDds(sys, gfx, t.Index); }
255 catch (Exception ex) when (ex is IOException) { fail++; wfail++; if (wfail <= 5) Console.WriteLine($" fail {p}: {ex.GetType().Name} {ex.Message}"); continue; }
239 catch { fail++; continue; } 256 catch { fail++; continue; }
240 try { Util.WriteAllBytes(p, dds); ok++; } 257 try { Util.WriteAllBytes(p, dds); ok++; }
241 catch { fail++; wfail++; } 258 catch { fail++; wfail++; }
@@ -244,7 +261,9 @@ else if (mode is "models" or "textures")
244 ok, fail, 0, null, null, wfail); 261 ok, fail, 0, null, null, wfail);
245 } 262 }
246 } 263 }
247 catch (Exception ex) { return new Result(null, ok, fail + 1, embedded, null, $" fail {e.Name}: {ex.GetType().Name} {ex.Message}", 0); } 264 // A read I/O error while reading the resource is retryable like a write failure, so it also
265 // withholds the manifest; structural exceptions stay non-retryable.
266 catch (Exception ex) { return new Result(null, ok, fail + 1, embedded, null, $" fail {e.Name}: {ex.GetType().Name} {ex.Message}", ex is IOException ? 1 : 0); }
248 } 267 }
249 if (mode == "models") Console.WriteLine($" embedded textures {embedded:N0}"); 268 if (mode == "models") Console.WriteLine($" embedded textures {embedded:N0}");
250 writer.WriteEndArray(); 269 writer.WriteEndArray();
@@ -266,13 +285,21 @@ else if (mode is "models" or "textures")
266 else 285 else
267 { 286 {
268 Console.WriteLine($" {writeFail} write failure(s): manifest withheld; rerun retries the missing files"); 287 Console.WriteLine($" {writeFail} write failure(s): manifest withheld; rerun retries the missing files");
269 File.Delete(manifestPath); 288 try { WithdrawMarker(manifestPath); WithdrawMarker(partial); }
270 File.Delete(partial); 289 catch (Exception ex) { Console.WriteLine($" cannot withdraw the stale manifest: {ex.GetType().Name} {ex.Message}"); return 1; }
271 } 290 }
272} 291}
273Console.WriteLine($"{mode} {baseName}: ok={ok} fail={fail} skip={skip} in {sw.Elapsed.TotalSeconds:0}s"); 292Console.WriteLine($"{mode} {baseName}: ok={ok} fail={fail} skip={skip} in {sw.Elapsed.TotalSeconds:0}s");
274return (mode == "ttpf" && fail != 0) || writeFail != 0 ? 1 : 0; 293return (mode == "ttpf" && fail != 0) || writeFail != 0 ? 1 : 0;
275 294
295// A stale marker may be read-only from an earlier interrupted run; clear the attribute so the withdraw works.
296static void WithdrawMarker(string path)
297{
298 if (!File.Exists(path)) return;
299 try { File.SetAttributes(path, FileAttributes.Normal); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { }
300 File.Delete(path);
301}
302
276// One exported entry: its manifest row (null when nothing was written), counts, a failure to report in order, 303// One exported entry: its manifest row (null when nothing was written), counts, a failure to report in order,
277// and how many of its failures were output-write failures (the ones a retry can fix; decode failures are not). 304// and how many of its failures were output-write failures (the ones a retry can fix; decode failures are not).
278readonly record struct Result(object? Row, int Ok, int Fail, int Embedded, ModelDecodeFailure? DecodeFailure, string? Error, int WriteFail); 305readonly record struct Result(object? Row, int Ok, int Fail, int Embedded, ModelDecodeFailure? DecodeFailure, string? Error, int WriteFail);