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:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 15:14:30 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 15:14:30 -0500
commit5fe56876948596fefb186cee3ed06323c0b48e4d (patch)
tree4759609c79c562bef48d4db7cc671a842e6c98ce /tools/rpf9dump/Program.cs
parent276ac7b56c365a5fda22a1c50a7cdc2549ce2c9b (diff)
parent54bb91389d04bfd5321ab2f9065f74ee7f312d5c (diff)
Merge pull request #3064 from jaybeebabageyus
Diffstat (limited to 'tools/rpf9dump/Program.cs')
-rw-r--r--tools/rpf9dump/Program.cs84
1 files changed, 67 insertions, 17 deletions
diff --git a/tools/rpf9dump/Program.cs b/tools/rpf9dump/Program.cs
index aba410a73..f090e3cfd 100644
--- a/tools/rpf9dump/Program.cs
+++ b/tools/rpf9dump/Program.cs
@@ -34,7 +34,7 @@ var sw = Stopwatch.StartNew();
34string baseName = Path.GetFileNameWithoutExtension(src); 34string baseName = Path.GetFileNameWithoutExtension(src);
35string outDir = Path.Combine(outRoot, baseName); Directory.CreateDirectory(outDir); 35string outDir = Path.Combine(outRoot, baseName); Directory.CreateDirectory(outDir);
36long length = new FileInfo(src).Length; 36long length = new FileInfo(src).Length;
37int ok = 0, fail = 0, skip = 0; 37int ok = 0, fail = 0, skip = 0, writeFail = 0;
38// Every mode below works on all cores. Core's readers open their own read handle per call, entry and file names 38// Every mode below works on all cores. Core's readers open their own read handle per call, entry and file names
39// come from offsets and indexes, and results are put back in source order, so outputs match a one-thread run. 39// come from offsets and indexes, and results are put back in source order, so outputs match a one-thread run.
40// ponytail: every core, no flag; add --jobs if someone needs the machine during a build. 40// ponytail: every core, no flag; add --jobs if someone needs the machine during a build.
@@ -76,7 +76,10 @@ if (mode == "ttpf")
76 if (fail == 0) 76 if (fail == 0)
77 Util.WriteAllText(manifest, JsonSerializer.Serialize(new { src, info.Version, Count = info.Textures.Count, info.Metadata, Textures = rows }, json)); 77 Util.WriteAllText(manifest, JsonSerializer.Serialize(new { src, info.Version, Count = info.Textures.Count, info.Metadata, Textures = rows }, json));
78 else 78 else
79 File.Delete(manifest); 79 {
80 try { WithdrawMarker(manifest); }
81 catch (Exception ex) { Console.WriteLine($" cannot withdraw the stale manifest: {ex.GetType().Name} {ex.Message}"); return 1; }
82 }
80} 83}
81else if (mode == "audio") 84else if (mode == "audio")
82{ 85{
@@ -239,7 +242,7 @@ else if (mode is "models" or "textures")
239 for (int i = 0; i < n; i++) 242 for (int i = 0; i < n; i++)
240 { 243 {
241 var r = results[i]; results[i] = default; 244 var r = results[i]; results[i] = default;
242 ok += r.Ok; fail += r.Fail; embedded += r.Embedded; 245 ok += r.Ok; fail += r.Fail; embedded += r.Embedded; writeFail += r.WriteFail;
243 if (r.DecodeFailure is { } reason) 246 if (r.DecodeFailure is { } reason)
244 { 247 {
245 int count = modelDecodeFailures.GetValueOrDefault(reason.Code) + 1; 248 int count = modelDecodeFailures.GetValueOrDefault(reason.Code) + 1;
@@ -266,17 +269,31 @@ else if (mode is "models" or "textures")
266 { 269 {
267 var m = RageModelDecoder.Decode(sys, gfx, e.ResourceDrawableOffset); 270 var m = RageModelDecoder.Decode(sys, gfx, e.ResourceDrawableOffset);
268 if (m == null) 271 if (m == null)
269 return new Result(null, 0, 1, 0, ModelDecodeFailure.Explain(sys, gfx.Length), null); 272 return new Result(null, 0, 1, 0, ModelDecodeFailure.Explain(sys, gfx.Length), null, 0);
270 string obj = Path.Combine(sub, stem + ".obj"); 273 string obj = Path.Combine(sub, stem + ".obj");
271 if (!File.Exists(obj)) Util.WriteAllText(obj, RageModelDecoder.CreateObj(m)); 274 string objText = RageModelDecoder.CreateObj(m);
275 // A failed obj write means the model is missing from disk: fatal for the run (retryable),
276 // unlike the decode failures above, which no rerun would change.
277 try { if (!File.Exists(obj)) Util.WriteAllText(obj, objText); }
278 catch (Exception wex) { return new Result(null, 0, 1, 0, null, $" fail {e.Name}: {wex.GetType().Name} {wex.Message}", 1); }
279 int wfail = 0;
272 if (m.EmbeddedTextures != null && m.EmbeddedTextureDictionaryOffset is int dOff) 280 if (m.EmbeddedTextures != null && m.EmbeddedTextureDictionaryOffset is int dOff)
273 { 281 {
274 string td = Path.Combine(sub, stem + "_tex"); Directory.CreateDirectory(td); 282 // A directory or DDS write failure leaves the embedded texture missing from disk: retryable,
283 // so it fails the run like the obj write above. Embedded decode failures count like the
284 // textures branch counts dictionary decode failures: reported, non-fatal for the run.
285 string td;
286 try { td = Path.Combine(sub, stem + "_tex"); Directory.CreateDirectory(td); }
287 catch (Exception wex) { return new Result(null, 0, 1, 0, null, $" fail {e.Name}: {wex.GetType().Name} {wex.Message}", 1); }
275 foreach (var t in m.EmbeddedTextures) 288 foreach (var t in m.EmbeddedTextures)
276 { 289 {
277 string p = Path.Combine(td, $"{t.Index:D3}_{Util.Safe(t.Name)}.dds"); 290 string p = Path.Combine(td, $"{t.Index:D3}_{Util.Safe(t.Name)}.dds");
278 if (File.Exists(p)) { embedded++; continue; } 291 if (File.Exists(p)) { embedded++; continue; }
279 try { Util.WriteAllBytes(p, Util.BgraDds(RageTextureDecoder.DecodeEmbeddedTexturePixels(sys, gfx, dOff, t.Index))); embedded++; } catch { } 292 byte[] dds;
293 try { dds = Util.BgraDds(RageTextureDecoder.DecodeEmbeddedTexturePixels(sys, gfx, dOff, t.Index)); }
294 catch { fail++; continue; }
295 try { Util.WriteAllBytes(p, dds); embedded++; }
296 catch (Exception wex) { fail++; wfail++; if (wfail <= 5) Console.WriteLine($" fail {p}: {wex.GetType().Name} {wex.Message}"); }
280 } 297 }
281 } 298 }
282 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, 299 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,
@@ -291,23 +308,37 @@ else if (mode is "models" or "textures")
291 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(), 308 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(),
292 Materials = m.Materials?.Select(x => new { x.ShaderIndex, Shader = x.ShaderHash.ToString("X8"), Preset = x.PresetHash.ToString("X8"), x.RenderBucket, 309 Materials = m.Materials?.Select(x => new { x.ShaderIndex, Shader = x.ShaderHash.ToString("X8"), Preset = x.PresetHash.ToString("X8"), x.RenderBucket,
293 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() }, 310 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() },
294 1, 0, embedded, null, null); 311 1, fail, embedded, null, null, wfail);
295 } 312 }
296 else 313 else
297 { 314 {
298 var dict = RageTextureDecoder.Inspect(sys, gfx); 315 var dict = RageTextureDecoder.Inspect(sys, gfx);
299 if (dict == null) return new Result(null, 0, 1, 0, null, null); 316 if (dict == null) return new Result(null, 0, 1, 0, null, null, 0);
300 string td = Path.Combine(sub, stem); Directory.CreateDirectory(td); 317 string td;
318 try { td = Path.Combine(sub, stem); Directory.CreateDirectory(td); }
319 catch (Exception wex) { return new Result(null, 0, 1, 0, null, $" fail {e.Name}: {wex.GetType().Name} {wex.Message}", 1); }
320 int wfail = 0;
301 foreach (var t in dict.Textures) 321 foreach (var t in dict.Textures)
302 { 322 {
303 string p = Path.Combine(td, $"{t.Index:D3}_{Util.Safe(t.Name)}.dds"); 323 string p = Path.Combine(td, $"{t.Index:D3}_{Util.Safe(t.Name)}.dds");
304 try { if (!File.Exists(p)) Util.WriteAllBytes(p, RageTextureDecoder.CreateDds(sys, gfx, t.Index)); ok++; } catch { fail++; } 324 if (File.Exists(p)) { ok++; continue; }
325 // A decoder problem is counted like today and no rerun would change it; a failed DDS write
326 // is retryable, so it additionally counts as a write failure and fails the run. A retryable
327 // read I/O error on the resource belongs to the same retryable class.
328 byte[] dds;
329 try { dds = RageTextureDecoder.CreateDds(sys, gfx, t.Index); }
330 catch (Exception ex) when (ex is IOException) { fail++; wfail++; if (wfail <= 5) Console.WriteLine($" fail {p}: {ex.GetType().Name} {ex.Message}"); continue; }
331 catch { fail++; continue; }
332 try { Util.WriteAllBytes(p, dds); ok++; }
333 catch { fail++; wfail++; }
305 } 334 }
306 return new Result(new { e.Offset, Dir = stem, Textures = dict.Textures.Select(t => new { t.Index, t.Name, t.Width, t.Height, t.Format, t.MipCount, Hash = t.NameHash.ToString("X8"), Key = t.AssetKey?.ToString("X16") }).ToArray() }, 335 return new Result(new { e.Offset, Dir = stem, Textures = dict.Textures.Select(t => new { t.Index, t.Name, t.Width, t.Height, t.Format, t.MipCount, Hash = t.NameHash.ToString("X8"), Key = t.AssetKey?.ToString("X16") }).ToArray() },
307 ok, fail, 0, null, null); 336 ok, fail, 0, null, null, wfail);
308 } 337 }
309 } 338 }
310 catch (Exception ex) { return new Result(null, ok, fail + 1, embedded, null, $" fail {e.Name}: {ex.GetType().Name} {ex.Message}"); } 339 // A read I/O error while reading the resource is retryable like a write failure, so it also
340 // withholds the manifest; structural exceptions stay non-retryable.
341 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); }
311 } 342 }
312 if (mode == "models") Console.WriteLine($" embedded textures {embedded:N0}"); 343 if (mode == "models") Console.WriteLine($" embedded textures {embedded:N0}");
313 writer.WriteEndArray(); 344 writer.WriteEndArray();
@@ -321,10 +352,29 @@ else if (mode is "models" or "textures")
321 writer.WriteEndObject(); 352 writer.WriteEndObject();
322 writer.Dispose(); // flushes; the later using-dispose is a no-op 353 writer.Dispose(); // flushes; the later using-dispose is a no-op
323 mstream.Dispose(); 354 mstream.Dispose();
324 File.Move(partial, manifestPath, true); 355 // The manifest is the completion marker make_game resumes on. Like ttpf, keep the successful exports for
356 // a retry, but a run with write failures must not mark the stage finished: withdraw a stale marker and
357 // drop the partial instead of publishing it.
358 if (writeFail == 0)
359 File.Move(partial, manifestPath, true);
360 else
361 {
362 Console.WriteLine($" {writeFail} write failure(s): manifest withheld; rerun retries the missing files");
363 try { WithdrawMarker(manifestPath); WithdrawMarker(partial); }
364 catch (Exception ex) { Console.WriteLine($" cannot withdraw the stale manifest: {ex.GetType().Name} {ex.Message}"); return 1; }
365 }
325} 366}
326Console.WriteLine($"{mode} {baseName}: ok={ok} fail={fail} skip={skip} in {sw.Elapsed.TotalSeconds:0}s"); 367Console.WriteLine($"{mode} {baseName}: ok={ok} fail={fail} skip={skip} in {sw.Elapsed.TotalSeconds:0}s");
327return mode == "ttpf" && fail != 0 ? 1 : 0; 368return (mode == "ttpf" && fail != 0) || writeFail != 0 ? 1 : 0;
369
370// A stale marker may be read-only from an earlier interrupted run; clear the attribute so the withdraw works.
371static void WithdrawMarker(string path)
372{
373 if (!File.Exists(path)) return;
374 try { File.SetAttributes(path, FileAttributes.Normal); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { }
375 File.Delete(path);
376}
328 377
329// One exported entry: its manifest row (null when nothing was written), counts, and a failure to report in order. 378// One exported entry: its manifest row (null when nothing was written), counts, a failure to report in order,
330readonly record struct Result(object? Row, int Ok, int Fail, int Embedded, ModelDecodeFailure? DecodeFailure, string? Error); 379// and how many of its failures were output-write failures (the ones a retry can fix; decode failures are not).
380readonly record struct Result(object? Row, int Ok, int Fail, int Embedded, ModelDecodeFailure? DecodeFailure, string? Error, int WriteFail);