Use TryGetChunk everywhere

This commit is contained in:
Jarrod Doyle 2024-10-05 14:57:40 +01:00
parent ab738203d6
commit 3fa86a233f
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 13 additions and 16 deletions

View File

@ -38,19 +38,17 @@ public class ObjectHierarchy
T GetMergedChunk<T>(string name) where T : IMergable
{
if (db.Chunks.TryGetValue(name, out var rawChunk))
if (!db.TryGetChunk<T>(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<T>(name, out var gamChunk))
{
gamChunk.Merge(chunk);
return gamChunk;
}
return chunk;
}
// Add parentages

View File

@ -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<WorldRep>("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>("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<GamFile>("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)
{