From ec79e07ec7b282fb8c56c9acf026fa5fed519908 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Thu, 29 Aug 2024 19:09:53 +0100 Subject: [PATCH] Only load textures that are actually used --- project/code/TMV/Mission.cs | 10 +++++----- project/code/TMV/TextureLoader.cs | 28 ++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/project/code/TMV/Mission.cs b/project/code/TMV/Mission.cs index 00aa744..c76096f 100644 --- a/project/code/TMV/Mission.cs +++ b/project/code/TMV/Mission.cs @@ -113,9 +113,9 @@ public partial class Mission : Node3D { ClearMap(); - _textureLoader = new TextureLoader(_campaignName); + _textureLoader = new TextureLoader(_installPaths, _campaignName); Timing.TimeStage("DbFile Parse", () => _file = new(FileName)); - Timing.TimeStage("Load FAM", () => UseChunk("TXLIST", LoadTextures)); + Timing.TimeStage("Register Textures", () => UseChunk("TXLIST", RegisterTextures)); Timing.TimeStage("Build WR", () => UseChunk("WREXT", BuildWrMeshes)); if (_file.Chunks.TryGetValue("BRLIST", out var brList)) @@ -501,7 +501,7 @@ public partial class Mission : Node3D return textureId; } - private void LoadTextures(TxList textureList) + private void RegisterTextures(TxList textureList) { // TODO: Use PathJoin var count = textureList.ItemCount; @@ -521,9 +521,9 @@ public partial class Mission : Node3D } path += item.Name; - if (!_textureLoader.Load(_installPaths, i, path)) + if (!_textureLoader.Register(i, path)) { - GD.Print($"Failed to load texture: {path}"); + GD.Print($"Failed to register texture: {path}"); } } diff --git a/project/code/TMV/TextureLoader.cs b/project/code/TMV/TextureLoader.cs index 8c2339e..d41cf39 100644 --- a/project/code/TMV/TextureLoader.cs +++ b/project/code/TMV/TextureLoader.cs @@ -7,13 +7,16 @@ namespace KeepersCompound.TMV; public partial class TextureLoader { + private readonly ResourcePathManager _pathManager; private readonly string _fmName; private readonly List _textureCache = new(); private readonly Dictionary _idMap = new(); + private readonly Dictionary _idPathMap = new(); private readonly Dictionary _pathMap = new(); - public TextureLoader(string fmName) + public TextureLoader(ResourcePathManager pathManager, string fmName) { + _pathManager = pathManager; _fmName = fmName; LoadDefaultTexture(); } @@ -26,10 +29,15 @@ public partial class TextureLoader _textureCache.Add(texture); } - public bool Load(ResourcePathManager installManager, int id, string path) + public bool Register(int id, string path) + { + return _idPathMap.TryAdd(id, path); + } + + private bool Load(int id, string path) { var loaded = false; - string texPath = installManager.GetTexturePath(_fmName, path); + string texPath = _pathManager.GetTexturePath(_fmName, path); if (texPath != null) { @@ -64,15 +72,23 @@ public partial class TextureLoader return null; } + // TODO: We should report load failures public ImageTexture Get(int id) { - if (!_idMap.ContainsKey(id)) + if (_idMap.TryGetValue(id, out int value)) { - return _textureCache[0]; + return _textureCache[value]; } - return _textureCache[_idMap[id]]; + + if (_idPathMap.TryGetValue(id, out var path) && Load(id, path)) + { + return _textureCache[_idMap[id]]; + } + + return _textureCache[0]; } + // TODO: Load by path :) public ImageTexture Get(string path) { if (!_pathMap.ContainsKey(path))