From 9e115b690615293f55ce7c455099fc0b0c2c6760 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 7 Sep 2024 09:06:20 +0100 Subject: [PATCH] Refacor object hierarchy creation to be more maintainable and extensible --- project/code/LGS/Database/ObjectHierarchy.cs | 148 ++++++------------- 1 file changed, 49 insertions(+), 99 deletions(-) diff --git a/project/code/LGS/Database/ObjectHierarchy.cs b/project/code/LGS/Database/ObjectHierarchy.cs index a56ff91..3042c5f 100644 --- a/project/code/LGS/Database/ObjectHierarchy.cs +++ b/project/code/LGS/Database/ObjectHierarchy.cs @@ -1,4 +1,5 @@ +using System; using System.Collections.Generic; using KeepersCompound.LGS.Database.Chunks; @@ -35,120 +36,69 @@ public class ObjectHierarchy { _objects = new Dictionary(); - if (db.Chunks.TryGetValue("P$ModelName", out var modelNamesRaw) && - db.Chunks.TryGetValue("P$Scale", out var scalesRaw) && - db.Chunks.TryGetValue("P$RenderTyp", out var renderTypesRaw) && - db.Chunks.TryGetValue("P$OTxtRepr0", out var txtRepl0Raw) && - db.Chunks.TryGetValue("P$OTxtRepr1", out var txtRepl1Raw) && - db.Chunks.TryGetValue("P$OTxtRepr2", out var txtRepl2Raw) && - db.Chunks.TryGetValue("P$OTxtRepr3", out var txtRepl3Raw) && - db.Chunks.TryGetValue("P$RenderAlp", out var renderAlphaRaw) && - db.Chunks.TryGetValue("L$MetaProp", out var metaPropLinksRaw) && - db.Chunks.TryGetValue("LD$MetaProp", out var metaPropLinkDataRaw)) + T GetMergedChunk(string name) where T : IMergable { - var modelNames = (PropertyChunk)modelNamesRaw; - var scales = (PropertyChunk)scalesRaw; - var renderTypes = (PropertyChunk)renderTypesRaw; - var txtRepl0s = (PropertyChunk)txtRepl0Raw; - var txtRepl1s = (PropertyChunk)txtRepl1Raw; - var txtRepl2s = (PropertyChunk)txtRepl2Raw; - var txtRepl3s = (PropertyChunk)txtRepl3Raw; - var renderAlpha = (PropertyChunk)renderAlphaRaw; - var metaPropLinks = (LinkChunk)metaPropLinksRaw; - var metaPropLinkData = (LinkDataMetaProp)metaPropLinkDataRaw; - - // Merge gam chunks - if (gam != null && - gam.Chunks.TryGetValue("P$ModelName", out var gamModelNames) && - gam.Chunks.TryGetValue("P$Scale", out var gamScales) && - gam.Chunks.TryGetValue("P$RenderTyp", out var gamRenderTypes) && - gam.Chunks.TryGetValue("P$OTxtRepr0", out var gamTxtRepl0s) && - gam.Chunks.TryGetValue("P$OTxtRepr1", out var gamTxtRepl1s) && - gam.Chunks.TryGetValue("P$OTxtRepr2", out var gamTxtRepl2s) && - gam.Chunks.TryGetValue("P$OTxtRepr3", out var gamTxtRepl3s) && - gam.Chunks.TryGetValue("P$RenderAlp", out var gamRenderAlpha) && - gam.Chunks.TryGetValue("L$MetaProp", out var gamLinks) && - gam.Chunks.TryGetValue("LD$MetaProp", out var gamLinkData)) + if (db.Chunks.TryGetValue(name, out var rawChunk)) { - modelNames.properties.AddRange(((PropertyChunk)gamModelNames).properties); - scales.properties.AddRange(((PropertyChunk)gamScales).properties); - renderTypes.properties.AddRange(((PropertyChunk)gamRenderTypes).properties); - txtRepl0s.properties.AddRange(((PropertyChunk)gamTxtRepl0s).properties); - txtRepl1s.properties.AddRange(((PropertyChunk)gamTxtRepl1s).properties); - txtRepl2s.properties.AddRange(((PropertyChunk)gamTxtRepl2s).properties); - txtRepl3s.properties.AddRange(((PropertyChunk)gamTxtRepl3s).properties); - renderAlpha.properties.AddRange(((PropertyChunk)gamRenderAlpha).properties); - metaPropLinks.links.AddRange(((LinkChunk)gamLinks).links); - metaPropLinkData.linkData.AddRange(((LinkDataMetaProp)gamLinkData).linkData); + var chunk = (T)rawChunk; + if (gam != null && gam.Chunks.TryGetValue(name, out var rawGamChunk)) + { + var gamChunk = (T)rawGamChunk; + chunk.Merge(gamChunk); + } + return chunk; } - // Add parentages - var length = metaPropLinks.links.Count; - for (var i = 0; i < length; i++) - { - var link = metaPropLinks.links[i]; - var linkData = metaPropLinkData.linkData[i]; - var childId = link.source; - var parentId = link.destination; - if (!_objects.ContainsKey(childId)) - { - _objects.Add(childId, new DarkObject(childId)); - } - if (!_objects.ContainsKey(parentId)) - { - _objects.Add(parentId, new DarkObject(parentId)); - } + throw new ArgumentException($"No chunk with name ({name}) found", nameof(name)); + } - if (linkData.priority == 0) - { - _objects[childId].parentId = parentId; - } + // Add parentages + var metaPropLinks = GetMergedChunk("L$MetaProp"); + var metaPropLinkData = GetMergedChunk("LD$MetaProp"); + var length = metaPropLinks.links.Count; + for (var i = 0; i < length; i++) + { + var link = metaPropLinks.links[i]; + var linkData = metaPropLinkData.linkData[i]; + var childId = link.source; + var parentId = link.destination; + if (!_objects.ContainsKey(childId)) + { + _objects.Add(childId, new DarkObject(childId)); + } + if (!_objects.ContainsKey(parentId)) + { + _objects.Add(parentId, new DarkObject(parentId)); } - void TryAddProp(string propName, Property prop) + if (linkData.priority == 0) + { + _objects[childId].parentId = parentId; + } + } + + void AddProp(string name) where T : Property, new() + { + var chunk = GetMergedChunk>(name); + foreach (var prop in chunk.properties) { var id = prop.objectId; if (!_objects.ContainsKey(id)) { _objects.Add(id, new DarkObject(id)); } - _objects[id].properties.TryAdd(propName, prop); - } - - // TODO: Find a way to do this better - foreach (var prop in modelNames.properties) - { - TryAddProp("P$ModelName", prop); - } - foreach (var prop in scales.properties) - { - TryAddProp("P$Scale", prop); - } - foreach (var prop in renderTypes.properties) - { - TryAddProp("P$RenderTyp", prop); - } - foreach (var prop in txtRepl0s.properties) - { - TryAddProp("P$OTxtRepr0", prop); - } - foreach (var prop in txtRepl1s.properties) - { - TryAddProp("P$OTxtRepr1", prop); - } - foreach (var prop in txtRepl2s.properties) - { - TryAddProp("P$OTxtRepr2", prop); - } - foreach (var prop in txtRepl3s.properties) - { - TryAddProp("P$OTxtRepr3", prop); - } - foreach (var prop in renderAlpha.properties) - { - TryAddProp("P$RenderAlp", prop); + _objects[id].properties.TryAdd(name, prop); } } + + AddProp("P$ModelName"); + AddProp("P$Scale"); + AddProp("P$RenderTyp"); + AddProp("P$OTxtRepr0"); + AddProp("P$OTxtRepr1"); + AddProp("P$OTxtRepr2"); + AddProp("P$OTxtRepr3"); + AddProp("P$RenderAlp"); } public T GetProperty(int objectId, string propName) where T : Property