using System.Collections.Generic; using System.Linq; using Godot; using KeepersCompound.LGS; namespace KeepersCompound.TMV; public partial class TextureLoader { private readonly string _fmName; private readonly List _textureCache = new(); private readonly Dictionary _idMap = new(); private readonly Dictionary _pathMap = new(); public TextureLoader(string fmName) { _fmName = fmName; LoadDefaultTexture(); } private void LoadDefaultTexture() { // TODO: This should be a resource loaded from RES const string path = "user://textures/jorge.png"; var texture = ImageTexture.CreateFromImage(Image.LoadFromFile(path)); _textureCache.Add(texture); } public bool Load(ResourcePathManager installManager, int id, string path) { var loaded = false; string texPath; if (_fmName != null) { texPath = installManager.GetTexturePath(_fmName, path); texPath ??= installManager.GetTexturePath(path); } else { texPath = installManager.GetTexturePath(path); } if (texPath != null) { var texture = LoadTexture(texPath); if (texture != null) { _textureCache.Add(texture); loaded = true; } } var index = loaded ? _textureCache.Count - 1 : 0; _idMap.TryAdd(id, index); _pathMap.TryAdd(path, index); return loaded; } public static ImageTexture LoadTexture(string path) { var ext = path.GetExtension().ToLower(); string[] validExtensions = { "png", "tga", "pcx", "gif" }; if (validExtensions.Contains(ext)) { var texture = ext switch { "pcx" => LoadPcx(path), "gif" => LoadGif(path), _ => ImageTexture.CreateFromImage(Image.LoadFromFile(path)), }; return texture; } return null; } public ImageTexture Get(int id) { if (!_idMap.ContainsKey(id)) { return _textureCache[0]; } return _textureCache[_idMap[id]]; } public ImageTexture Get(string path) { if (!_pathMap.ContainsKey(path)) { return _textureCache[0]; } return _textureCache[_pathMap[path]]; } }