From c48eb018f5ae9798df998ef379433809478514a0 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 24 Aug 2024 13:37:21 +0100 Subject: [PATCH] Merge model related chunks from gam file --- project/code/TMV/Mission.cs | 43 ++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/project/code/TMV/Mission.cs b/project/code/TMV/Mission.cs index 2202c95..52cd884 100644 --- a/project/code/TMV/Mission.cs +++ b/project/code/TMV/Mission.cs @@ -5,6 +5,7 @@ using KeepersCompound.TMV.UI; using RectpackSharp; using System; using System.Collections.Generic; +using System.IO; namespace KeepersCompound.TMV; @@ -95,13 +96,45 @@ public partial class Mission : Node3D UseChunk("TXLIST", LoadTextures); UseChunk("WREXT", BuildWrMeshes); if ( - _file.Chunks.TryGetValue("BRLIST", out var brList) && - _file.Chunks.TryGetValue("P$ModelName", out var modelNames) && - _file.Chunks.TryGetValue("L$MetaProp", out var metaPropLinks) && - _file.Chunks.TryGetValue("LD$MetaProp", out var metaPropLinkData) + _file.Chunks.TryGetValue("BRLIST", out var brListRaw) && + _file.Chunks.TryGetValue("P$ModelName", out var modelNamesRaw) && + _file.Chunks.TryGetValue("L$MetaProp", out var metaPropLinksRaw) && + _file.Chunks.TryGetValue("LD$MetaProp", out var metaPropLinkDataRaw) ) { - PlaceObjects((BrList)brList, (PropertyModelName)modelNames, (LinkChunk)metaPropLinks, (LinkDataMetaProp)metaPropLinkData); + var brList = (BrList)brListRaw; + var modelNames = (PropertyModelName)modelNamesRaw; + var metaPropLinks = (LinkChunk)metaPropLinksRaw; + var metaPropLinkData = (LinkDataMetaProp)metaPropLinkDataRaw; + + // TODO: Do this somewhere else lol + if (_file.Chunks.TryGetValue("GAM_FILE", out var gamFileChunk)) + { + GD.Print("GAM_FILE detected"); + + var options = new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive }; + var name = ((GamFile)gamFileChunk).fileName; + GD.Print($"Searching for GAM: {FileName.GetBaseDir()}/{name}"); + var paths = Directory.GetFiles(FileName.GetBaseDir(), name, options); + GD.Print($"Found paths: {paths.Length}"); + if (!paths.IsEmpty()) + { + GD.Print($"Attempting to load GAM at: {paths[0]}"); + var gamFile = new DbFile(paths[0]); + if (gamFile.Chunks.TryGetValue("P$ModelName", out var gamChunk1) && + gamFile.Chunks.TryGetValue("L$MetaProp", out var gamChunk2) && + gamFile.Chunks.TryGetValue("LD$MetaProp", out var gamChunk3)) + { + GD.Print($"Pre-Merged chunks: {modelNames.properties.Count} {metaPropLinks.links.Count} {metaPropLinkData.linkData.Count}"); + modelNames.properties.AddRange(((PropertyModelName)gamChunk1).properties); + metaPropLinks.links.AddRange(((LinkChunk)gamChunk2).links); + metaPropLinkData.linkData.AddRange(((LinkDataMetaProp)gamChunk3).linkData); + GD.Print($"Post-Merged chunks: {modelNames.properties.Count} {metaPropLinks.links.Count} {metaPropLinkData.linkData.Count}"); + } + } + } + + PlaceObjects(brList, modelNames, metaPropLinks, metaPropLinkData); } }