From 12a8b4f06df9a270e3d206a05a15917a2f50e6be Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Thu, 5 Sep 2024 18:49:55 +0100 Subject: [PATCH] Reduce duplicate code in Resource Path Manager --- project/code/LGS/ResourcePathManager.cs | 187 +++++++++--------------- project/code/TMV/Mission.cs | 25 +++- project/code/TMV/ModelLoader.cs | 7 +- project/code/TMV/TextureLoader.cs | 5 +- project/code/TMV/UI/MissionSelector.cs | 5 +- project/code/TMV/UI/ModelSelector.cs | 6 +- 6 files changed, 98 insertions(+), 137 deletions(-) diff --git a/project/code/LGS/ResourcePathManager.cs b/project/code/LGS/ResourcePathManager.cs index 84c8e83..34e61c3 100644 --- a/project/code/LGS/ResourcePathManager.cs +++ b/project/code/LGS/ResourcePathManager.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; @@ -5,7 +6,14 @@ using System.Linq; namespace KeepersCompound.LGS; -// TODO: Merge the two versions of GetXXX and handle null campaign string as OM +public enum ResourceType +{ + Mission, + Object, + ObjectTexture, + Texture, +} + public class ResourcePathManager { private record CampaignResources @@ -14,6 +22,33 @@ public class ResourcePathManager public Dictionary texturePathMap; public Dictionary objectPathMap; public Dictionary objectTexturePathMap; + + public List GetResourceNames(ResourceType type) + { + List keys = type switch + { + ResourceType.Mission => [.. missionPathMap.Keys], + ResourceType.Object => [.. objectPathMap.Keys], + ResourceType.ObjectTexture => [.. objectTexturePathMap.Keys], + ResourceType.Texture => [.. texturePathMap.Keys], + _ => throw new ArgumentOutOfRangeException(nameof(type)), + }; + keys.Sort(); + return keys; + } + + public string GetResourcePath(ResourceType type, string name) + { + var map = type switch + { + ResourceType.Mission => missionPathMap, + ResourceType.Object => objectPathMap, + ResourceType.ObjectTexture => objectTexturePathMap, + ResourceType.Texture => texturePathMap, + _ => throw new ArgumentOutOfRangeException(nameof(type)), + }; + return map.TryGetValue(name, out var resourcePath) ? resourcePath : null; + } } private bool _initialised = false; @@ -102,146 +137,57 @@ public class ResourcePathManager return names; } - public string GetMissionPath(string campaignName, string missionName) + public List GetResourceNames(ResourceType type, string campaignName) { - if (!_initialised) return null; + if (!_initialised) + { + throw new InvalidOperationException("Resource Path Manager hasn't been initialised."); + } if (campaignName == null || campaignName == "") { - if (_omResources.missionPathMap.TryGetValue(missionName, out var omPath)) - { - return omPath; - } - } - else if ( - _fmResources.TryGetValue(campaignName, out var campaign) && - campaign.missionPathMap.TryGetValue(missionName, out var fmPath)) - { - return fmPath; - } - - return null; - } - - public List GetMissionNames(string campaignName) - { - if (!_initialised) return null; - - if (campaignName == null || campaignName == "") - { - var names = new List(_omResources.missionPathMap.Keys); - names.Sort(); - return names; + return _omResources.GetResourceNames(type); } else if (_fmResources.TryGetValue(campaignName, out var campaign)) { - var names = new List(campaign.missionPathMap.Keys); - names.Sort(); - return names; + return campaign.GetResourceNames(type); } - return null; + + throw new ArgumentException("No campaign found with given name", nameof(campaignName)); } - public List GetModelNames(string campaignName) + public (string, string) GetResourcePath( + ResourceType type, + string campaignName, + string resourceName) { - if (!_initialised) return null; - - if (campaignName == null || campaignName == "") + if (!_initialised) { - var names = new List(_omResources.objectPathMap.Keys); - names.Sort(); - return names; + throw new InvalidOperationException("Resource Path Manager hasn't been initialised."); + } + + resourceName = resourceName.ToLower(); + var omResourcePath = _omResources.GetResourcePath(type, resourceName); + + if (campaignName == null || campaignName == "" && omResourcePath != null) + { + return ("", omResourcePath); } else if (_fmResources.TryGetValue(campaignName, out var campaign)) { - var names = new List(campaign.objectPathMap.Keys); - names.Sort(); - return names; - } - return null; - } - - // TODO: OMs fail to find a path, but FMs using OM textures do find them - // The OMs still fail even if I put them in a folder with FMs that work=?? - public string GetTexturePath(string campaignName, string textureName) - { - if (!_initialised) return null; - - textureName = textureName.ToLower(); - if (campaignName == null || campaignName == "") - { - if (_omResources.texturePathMap.TryGetValue(textureName, out var path)) + var fmResourcePath = campaign.GetResourcePath(type, resourceName); + if (fmResourcePath != null) { - return path; + return (campaignName, fmResourcePath); } - } - else if (_fmResources.TryGetValue(campaignName, out var campaign)) - { - if (campaign.texturePathMap.TryGetValue(textureName, out var fmPath)) + else if (omResourcePath != null) { - return fmPath; - } - else if (_omResources.texturePathMap.TryGetValue(textureName, out var omPath)) - { - return omPath; + return ("", omResourcePath); } } - return null; - } - - public string GetObjectPath(ref string campaignName, string objectName) - { - if (!_initialised) return null; - - objectName = objectName.ToLower(); - if (campaignName == null || campaignName == "") - { - if (_omResources.objectPathMap.TryGetValue(objectName, out var omPath)) - { - return omPath; - } - } - if (_fmResources.TryGetValue(campaignName, out var campaign)) - { - if (campaign.objectPathMap.TryGetValue(objectName, out var fmPath)) - { - return fmPath; - } - else if (_omResources.objectPathMap.TryGetValue(objectName, out var omPath)) - { - campaignName = ""; - return omPath; - } - } - return null; - } - - public string GetObjectTexturePath(string campaignName, string textureName) - { - if (!_initialised) return null; - - textureName = Path.GetFileNameWithoutExtension(textureName).ToLower(); - if (campaignName == null || campaignName == "") - { - if (_omResources.objectTexturePathMap.TryGetValue(textureName, out var path)) - { - return path; - } - } - else if (_fmResources.TryGetValue(campaignName, out var campaign)) - { - if (campaign.objectTexturePathMap.TryGetValue(textureName, out var fmPath)) - { - return fmPath; - } - else if (_omResources.objectTexturePathMap.TryGetValue(textureName, out var omPath)) - { - return omPath; - } - } - - return null; + // throw new ArgumentException($"No resource found with given type and name: {type}, {resourceName}", nameof(resourceName)); + return (null, null); } private static Dictionary GetObjectTexturePaths(string root) @@ -272,7 +218,6 @@ public class ResourcePathManager return pathMap; } - // TODO: Handle object textures? private static Dictionary GetTexturePaths(string root) { string[] validExtensions = { ".dds", ".png", ".tga", ".pcx", ".gif", ".bmp", ".cel", }; diff --git a/project/code/TMV/Mission.cs b/project/code/TMV/Mission.cs index 5c0f1e5..4422b4c 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; @@ -58,7 +59,13 @@ public partial class Mission : Node3D { _campaignName = campaign; _missionName = mission; - FileName = Context.Instance.PathManager.GetMissionPath(campaign, mission); + var pathManager = Context.Instance.PathManager; + var (newCampaign, missionPath) = pathManager.GetResourcePath(ResourceType.Mission, campaign, mission); + if (newCampaign != campaign) + { + GD.Print($"Didn't find campaign mission: ({campaign}, {mission})"); + } + FileName = missionPath; Build = true; }; } @@ -260,16 +267,20 @@ public partial class Mission : Node3D return false; } - var pathManager = Context.Instance.PathManager; path = prop.value; + + var pathManager = Context.Instance.PathManager; if (path.StartsWith("fam", StringComparison.OrdinalIgnoreCase)) { - path = pathManager.GetTexturePath(_campaignName, path); - return path != null; + var resType = ResourceType.Texture; + path = pathManager.GetResourcePath(resType, _campaignName, prop.value).Item2; + } + else + { + var resType = ResourceType.ObjectTexture; + var resName = Path.GetFileNameWithoutExtension(prop.value); + path = pathManager.GetResourcePath(resType, _campaignName, resName).Item2; } - - // gonna assume obj lol - path = pathManager.GetObjectTexturePath(_campaignName, path); return path != null; } diff --git a/project/code/TMV/ModelLoader.cs b/project/code/TMV/ModelLoader.cs index edae4c0..0b750a6 100644 --- a/project/code/TMV/ModelLoader.cs +++ b/project/code/TMV/ModelLoader.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.IO; using Godot; using KeepersCompound.LGS; @@ -39,7 +40,8 @@ public class ModelLoader public static MeshInstance3D LoadModel(ResourcePathManager pathManager, ref string campaignName, string modelName) { - var modelPath = pathManager.GetObjectPath(ref campaignName, modelName); + var (newCampaignName, modelPath) = pathManager.GetResourcePath(ResourceType.Object, campaignName, modelName); + campaignName = newCampaignName; if (modelPath == null) { return null; @@ -56,7 +58,8 @@ public class ModelLoader { if (material.Type == 0) { - var path = pathManager.GetObjectTexturePath(campaignName, material.Name); + var resName = Path.GetFileNameWithoutExtension(material.Name); + var (_, path) = pathManager.GetResourcePath(ResourceType.ObjectTexture, campaignName, resName); if (path == null) { path = "user://textures/jorge.png"; diff --git a/project/code/TMV/TextureLoader.cs b/project/code/TMV/TextureLoader.cs index ffb9925..57cf727 100644 --- a/project/code/TMV/TextureLoader.cs +++ b/project/code/TMV/TextureLoader.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; +using System.IO; using System.Linq; using Godot; +using KeepersCompound.LGS; namespace KeepersCompound.TMV; @@ -34,7 +36,8 @@ public partial class TextureLoader private bool Load(int id, string path) { var loaded = false; - string texPath = Context.Instance.PathManager.GetTexturePath(_fmName, path); + var pathManager = Context.Instance.PathManager; + var (_, texPath) = pathManager.GetResourcePath(ResourceType.Texture, _fmName, path); if (texPath != null) { diff --git a/project/code/TMV/UI/MissionSelector.cs b/project/code/TMV/UI/MissionSelector.cs index dcca2b3..482366c 100644 --- a/project/code/TMV/UI/MissionSelector.cs +++ b/project/code/TMV/UI/MissionSelector.cs @@ -1,4 +1,5 @@ using Godot; +using KeepersCompound.LGS; namespace KeepersCompound.TMV.UI; @@ -75,8 +76,8 @@ public partial class MissionSelector : Control _LoadButton.Disabled = true; var pathManager = Context.Instance.PathManager; - var campaignName = _Campaigns.GetItemText((int)idx); - var missionNames = pathManager.GetMissionNames(idx == 0 ? null : campaignName); + var campaignName = idx == 0 ? null : _Campaigns.GetItemText((int)idx); + var missionNames = pathManager.GetResourceNames(ResourceType.Mission, campaignName); foreach (var mission in missionNames) { _Missions.AddItem(mission); diff --git a/project/code/TMV/UI/ModelSelector.cs b/project/code/TMV/UI/ModelSelector.cs index f713faf..71c8fb4 100644 --- a/project/code/TMV/UI/ModelSelector.cs +++ b/project/code/TMV/UI/ModelSelector.cs @@ -1,7 +1,5 @@ -using System.IO; -using System.IO.Compression; -using System.Linq; using Godot; +using KeepersCompound.LGS; namespace KeepersCompound.TMV.UI; @@ -83,7 +81,7 @@ public partial class ModelSelector : Control var pathManager = Context.Instance.PathManager; var campaignName = idx == 0 ? null : _Campaigns.GetItemText((int)idx); - var modelNames = pathManager.GetModelNames(campaignName); + var modelNames = pathManager.GetResourceNames(ResourceType.Object, campaignName); foreach (var model in modelNames) { _Models.AddItem(model);