Only load textures that are actually used

This commit is contained in:
Jarrod Doyle 2024-08-29 19:09:53 +01:00
parent 30bfd05073
commit ec79e07ec7
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 27 additions and 11 deletions

View File

@ -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>("TXLIST", LoadTextures));
Timing.TimeStage("Register Textures", () => UseChunk<TxList>("TXLIST", RegisterTextures));
Timing.TimeStage("Build WR", () => UseChunk<WorldRep>("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}");
}
}

View File

@ -7,13 +7,16 @@ namespace KeepersCompound.TMV;
public partial class TextureLoader
{
private readonly ResourcePathManager _pathManager;
private readonly string _fmName;
private readonly List<ImageTexture> _textureCache = new();
private readonly Dictionary<int, int> _idMap = new();
private readonly Dictionary<int, string> _idPathMap = new();
private readonly Dictionary<string, int> _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];
}
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))