From 3fa86a233f63f123d76cb0b3c4a70cb8284161ed Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 5 Oct 2024 14:57:40 +0100 Subject: [PATCH] Use TryGetChunk everywhere --- .../Database/ObjectHierarchy.cs | 18 ++++++++---------- KeepersCompound.Lightmapper/Program.cs | 11 +++++------ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/KeepersCompound.LGS/Database/ObjectHierarchy.cs b/KeepersCompound.LGS/Database/ObjectHierarchy.cs index a66888c..40f6500 100644 --- a/KeepersCompound.LGS/Database/ObjectHierarchy.cs +++ b/KeepersCompound.LGS/Database/ObjectHierarchy.cs @@ -38,19 +38,17 @@ public class ObjectHierarchy T GetMergedChunk(string name) where T : IMergable { - if (db.Chunks.TryGetValue(name, out var rawChunk)) + if (!db.TryGetChunk(name, out var chunk)) { - var chunk = (T)rawChunk; - if (gam != null && gam.Chunks.TryGetValue(name, out var rawGamChunk)) - { - var gamChunk = (T)rawGamChunk; - gamChunk.Merge(chunk); - return gamChunk; - } - return chunk; + throw new ArgumentException($"No chunk with name ({name}) found", nameof(name)); } - throw new ArgumentException($"No chunk with name ({name}) found", nameof(name)); + if (gam != null && gam.TryGetChunk(name, out var gamChunk)) + { + gamChunk.Merge(chunk); + return gamChunk; + } + return chunk; } // Add parentages diff --git a/KeepersCompound.Lightmapper/Program.cs b/KeepersCompound.Lightmapper/Program.cs index 4c31aac..924ea81 100644 --- a/KeepersCompound.Lightmapper/Program.cs +++ b/KeepersCompound.Lightmapper/Program.cs @@ -58,9 +58,8 @@ class Program var hierarchy = Timing.TimeStage("Build Hierarchy", () => BuildHierarchy(misPath, mis)); // Build embree mesh - if (!mis.Chunks.TryGetValue("WREXT", out var wrRaw)) + if (!mis.TryGetChunk("WREXT", out var worldRep)) return; - var worldRep = (WorldRep)wrRaw; var scene = Timing.TimeStage("Build Scene", () => { var rt = new Raytracer(); @@ -70,9 +69,9 @@ class Program }); // For each lightmap pixel cast against all the brush and object lights - if (!mis.Chunks.TryGetValue("RENDPARAMS", out var rendParamsRaw)) + if (!mis.TryGetChunk("RENDPARAMS", out var rendParams)) return; - var ambient = ((RendParams)rendParamsRaw).ambientLight * 255; + var ambient = rendParams.ambientLight * 255; var lights = Timing.TimeStage("Gather Lights", () => BuildLightList(mis, hierarchy, campaign)); Timing.TimeStage("Light", () => CastSceneParallel(scene, worldRep, [.. lights], ambient)); @@ -279,11 +278,11 @@ class Program private static ObjectHierarchy BuildHierarchy(string misPath, DbFile misFile) { ObjectHierarchy objHierarchy; - if (misFile.Chunks.TryGetValue("GAM_FILE", out var gamFileChunk)) + if (misFile.TryGetChunk("GAM_FILE", out var gamFile)) { var dir = Path.GetDirectoryName(misPath); var options = new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive }; - var name = ((GamFile)gamFileChunk).fileName; + var name = gamFile.fileName; var paths = Directory.GetFiles(dir!, name, options); if (paths.Length > 0) {