From 9fd76815a2306c90076804359e90b314fe0bfec2 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 24 Aug 2024 17:10:08 +0100 Subject: [PATCH] Use new resource manager in mission and textureloader --- project/code/TMV/Mission.cs | 16 +++-- project/code/TMV/TextureLoader.cs | 101 ++++++++++-------------------- 2 files changed, 42 insertions(+), 75 deletions(-) diff --git a/project/code/TMV/Mission.cs b/project/code/TMV/Mission.cs index 52cd884..4523457 100644 --- a/project/code/TMV/Mission.cs +++ b/project/code/TMV/Mission.cs @@ -1,4 +1,5 @@ using Godot; +using KeepersCompound.LGS; using KeepersCompound.LGS.Database; using KeepersCompound.LGS.Database.Chunks; using KeepersCompound.TMV.UI; @@ -38,17 +39,19 @@ public partial class Mission : Node3D [Export] public bool Dump = false; - - InstallPaths _installPaths; + ResourcePathManager _installPaths; DbFile _file; TextureLoader _textureLoader; public override void _Ready() { + var extractPath = ProjectSettings.GlobalizePath($"user://extracted/tmp"); + _installPaths = new ResourcePathManager(extractPath); var missionSelector = GetNode("%MissionSelector") as MissionSelector; missionSelector.LoadMission += (string rootPath, string missionPath) => { - _installPaths = new InstallPaths(rootPath); + var inited = _installPaths.Init(rootPath); + GD.Print($"Inited paths: {inited}"); FileName = missionPath; Build = true; }; @@ -91,7 +94,8 @@ public partial class Mission : Node3D { ClearMap(); - _textureLoader = new TextureLoader(_installPaths.famPath, FileName.GetBaseDir()); + var fmName = FileName.GetBaseDir().GetFile(); + _textureLoader = new TextureLoader(fmName); _file = new(FileName); UseChunk("TXLIST", LoadTextures); UseChunk("WREXT", BuildWrMeshes); @@ -441,7 +445,7 @@ public partial class Mission : Node3D for (var i = 0; i < count; i++) { var item = textureList.Items[i]; - var path = "/"; + var path = ""; for (var j = 0; j < item.Tokens.Length; j++) { var token = item.Tokens[j]; @@ -454,7 +458,7 @@ public partial class Mission : Node3D } path += item.Name; - if (!_textureLoader.Load(i, path)) + if (!_textureLoader.Load(_installPaths, i, path)) { GD.Print($"Failed to load texture: {path}"); } diff --git a/project/code/TMV/TextureLoader.cs b/project/code/TMV/TextureLoader.cs index db6293e..98d6eaa 100644 --- a/project/code/TMV/TextureLoader.cs +++ b/project/code/TMV/TextureLoader.cs @@ -1,45 +1,21 @@ using System.Collections.Generic; -using System.IO; -using System.IO.Compression; using System.Linq; using Godot; +using KeepersCompound.LGS; namespace KeepersCompound.TMV; public partial class TextureLoader { - private readonly string _userTexturesPath; - private readonly string _rootFamPath; // TODO: Load from installation resources - private readonly string _fmPath; - private readonly Dictionary _rootTexturePaths = new(); - private readonly Dictionary _fmTexturePaths = new(); - + private readonly string _fmName; private readonly List _textureCache = new(); private readonly Dictionary _idMap = new(); private readonly Dictionary _pathMap = new(); - public TextureLoader(string rootFamPath, string fmPath) + public TextureLoader(string fmName) { - _rootFamPath = rootFamPath; - _fmPath = fmPath; - _userTexturesPath = ProjectSettings.GlobalizePath($"user://textures/tmp"); - - ExtractRootFamFiles(); + _fmName = fmName; LoadDefaultTexture(); - RegisterTexturePaths(_fmPath, _fmTexturePaths); - RegisterTexturePaths(_userTexturesPath, _rootTexturePaths); - } - - private void ExtractRootFamFiles() - { - var dir = new DirectoryInfo(_userTexturesPath); - if (dir.Exists) - { - dir.Delete(true); - } - - var zip = ZipFile.OpenRead(_rootFamPath); - zip.ExtractToDirectory(_userTexturesPath.PathJoin("fam")); } private void LoadDefaultTexture() @@ -50,46 +26,28 @@ public partial class TextureLoader _textureCache.Add(texture); } - private static void RegisterTexturePaths(string rootDir, Dictionary map) - { - // TODO: Load DDS BMP GIF CEL - string[] validExtensions = { "png", "tga", "pcx", "gif" }; - - var famOptions = new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive }; - var textureOptions = new EnumerationOptions - { - MatchCasing = MatchCasing.CaseInsensitive, - RecurseSubdirectories = true, - }; - - foreach (var dirPath in Directory.EnumerateDirectories(rootDir, "fam", famOptions)) - { - foreach (var path in Directory.EnumerateFiles(dirPath, "*", textureOptions)) - { - if (validExtensions.Contains(path.GetExtension().ToLower())) - { - // TODO: This only adds the first one found rather than the highest priority - var key = path.TrimPrefix(rootDir).GetBaseName().ToLower(); - map.TryAdd(key, path); - } - } - } - - GD.Print($"Registered {map.Count} texture paths at : {rootDir}"); - } - - public bool Load(int id, string path) + public bool Load(ResourcePathManager installManager, int id, string path) { var loaded = false; - if (_fmTexturePaths.TryGetValue(path.ToLower(), out var fmTexPath)) + string texPath; + if (_fmName != null) { - _textureCache.Add(LoadTexture(fmTexPath)); - loaded = true; + texPath = installManager.GetTexturePath(_fmName, path); + texPath ??= installManager.GetTexturePath(path); } - else if (_rootTexturePaths.TryGetValue(path.ToLower(), out var rootTexPath)) + else { - _textureCache.Add(LoadTexture(rootTexPath)); - loaded = true; + 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; @@ -101,13 +59,18 @@ public partial class TextureLoader public static ImageTexture LoadTexture(string path) { var ext = path.GetExtension().ToLower(); - var texture = ext switch + string[] validExtensions = { "png", "tga", "pcx", "gif" }; + if (validExtensions.Contains(ext)) { - "pcx" => LoadPcx(path), - "gif" => LoadGif(path), - _ => ImageTexture.CreateFromImage(Image.LoadFromFile(path)), - }; - return texture; + var texture = ext switch + { + "pcx" => LoadPcx(path), + "gif" => LoadGif(path), + _ => ImageTexture.CreateFromImage(Image.LoadFromFile(path)), + }; + return texture; + } + return null; } public ImageTexture Get(int id)