Reduce duplicate code in Resource Path Manager

This commit is contained in:
Jarrod Doyle 2024-09-05 18:49:55 +01:00
parent 63daccb3c1
commit 12a8b4f06d
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
6 changed files with 98 additions and 137 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
@ -5,7 +6,14 @@ using System.Linq;
namespace KeepersCompound.LGS; 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 public class ResourcePathManager
{ {
private record CampaignResources private record CampaignResources
@ -14,6 +22,33 @@ public class ResourcePathManager
public Dictionary<string, string> texturePathMap; public Dictionary<string, string> texturePathMap;
public Dictionary<string, string> objectPathMap; public Dictionary<string, string> objectPathMap;
public Dictionary<string, string> objectTexturePathMap; public Dictionary<string, string> objectTexturePathMap;
public List<string> GetResourceNames(ResourceType type)
{
List<string> 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; private bool _initialised = false;
@ -102,146 +137,57 @@ public class ResourcePathManager
return names; return names;
} }
public string GetMissionPath(string campaignName, string missionName) public List<string> 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 (campaignName == null || campaignName == "")
{ {
if (_omResources.missionPathMap.TryGetValue(missionName, out var omPath)) return _omResources.GetResourceNames(type);
{
return omPath;
}
}
else if (
_fmResources.TryGetValue(campaignName, out var campaign) &&
campaign.missionPathMap.TryGetValue(missionName, out var fmPath))
{
return fmPath;
}
return null;
}
public List<string> GetMissionNames(string campaignName)
{
if (!_initialised) return null;
if (campaignName == null || campaignName == "")
{
var names = new List<string>(_omResources.missionPathMap.Keys);
names.Sort();
return names;
} }
else if (_fmResources.TryGetValue(campaignName, out var campaign)) else if (_fmResources.TryGetValue(campaignName, out var campaign))
{ {
var names = new List<string>(campaign.missionPathMap.Keys); return campaign.GetResourceNames(type);
names.Sort();
return names;
}
return null;
} }
public List<string> GetModelNames(string campaignName) throw new ArgumentException("No campaign found with given name", nameof(campaignName));
{ }
if (!_initialised) return null;
if (campaignName == null || campaignName == "") public (string, string) GetResourcePath(
ResourceType type,
string campaignName,
string resourceName)
{ {
var names = new List<string>(_omResources.objectPathMap.Keys); if (!_initialised)
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)) else if (_fmResources.TryGetValue(campaignName, out var campaign))
{ {
var names = new List<string>(campaign.objectPathMap.Keys); var fmResourcePath = campaign.GetResourcePath(type, resourceName);
names.Sort(); if (fmResourcePath != null)
return names; {
return (campaignName, fmResourcePath);
} }
return null; else if (omResourcePath != 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; return ("", omResourcePath);
textureName = textureName.ToLower();
if (campaignName == null || campaignName == "")
{
if (_omResources.texturePathMap.TryGetValue(textureName, out var path))
{
return path;
}
}
else if (_fmResources.TryGetValue(campaignName, out var campaign))
{
if (campaign.texturePathMap.TryGetValue(textureName, out var fmPath))
{
return fmPath;
}
else if (_omResources.texturePathMap.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);
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;
} }
private static Dictionary<string, string> GetObjectTexturePaths(string root) private static Dictionary<string, string> GetObjectTexturePaths(string root)
@ -272,7 +218,6 @@ public class ResourcePathManager
return pathMap; return pathMap;
} }
// TODO: Handle object textures?
private static Dictionary<string, string> GetTexturePaths(string root) private static Dictionary<string, string> GetTexturePaths(string root)
{ {
string[] validExtensions = { ".dds", ".png", ".tga", ".pcx", ".gif", ".bmp", ".cel", }; string[] validExtensions = { ".dds", ".png", ".tga", ".pcx", ".gif", ".bmp", ".cel", };

View File

@ -1,4 +1,5 @@
using Godot; using Godot;
using KeepersCompound.LGS;
using KeepersCompound.LGS.Database; using KeepersCompound.LGS.Database;
using KeepersCompound.LGS.Database.Chunks; using KeepersCompound.LGS.Database.Chunks;
using KeepersCompound.TMV.UI; using KeepersCompound.TMV.UI;
@ -58,7 +59,13 @@ public partial class Mission : Node3D
{ {
_campaignName = campaign; _campaignName = campaign;
_missionName = mission; _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; Build = true;
}; };
} }
@ -260,16 +267,20 @@ public partial class Mission : Node3D
return false; return false;
} }
var pathManager = Context.Instance.PathManager;
path = prop.value; path = prop.value;
var pathManager = Context.Instance.PathManager;
if (path.StartsWith("fam", StringComparison.OrdinalIgnoreCase)) if (path.StartsWith("fam", StringComparison.OrdinalIgnoreCase))
{ {
path = pathManager.GetTexturePath(_campaignName, path); var resType = ResourceType.Texture;
return path != null; 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; return path != null;
} }

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using Godot; using Godot;
using KeepersCompound.LGS; using KeepersCompound.LGS;
@ -39,7 +40,8 @@ public class ModelLoader
public static MeshInstance3D LoadModel(ResourcePathManager pathManager, ref string campaignName, string modelName) 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) if (modelPath == null)
{ {
return null; return null;
@ -56,7 +58,8 @@ public class ModelLoader
{ {
if (material.Type == 0) 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) if (path == null)
{ {
path = "user://textures/jorge.png"; path = "user://textures/jorge.png";

View File

@ -1,6 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using Godot; using Godot;
using KeepersCompound.LGS;
namespace KeepersCompound.TMV; namespace KeepersCompound.TMV;
@ -34,7 +36,8 @@ public partial class TextureLoader
private bool Load(int id, string path) private bool Load(int id, string path)
{ {
var loaded = false; 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) if (texPath != null)
{ {

View File

@ -1,4 +1,5 @@
using Godot; using Godot;
using KeepersCompound.LGS;
namespace KeepersCompound.TMV.UI; namespace KeepersCompound.TMV.UI;
@ -75,8 +76,8 @@ public partial class MissionSelector : Control
_LoadButton.Disabled = true; _LoadButton.Disabled = true;
var pathManager = Context.Instance.PathManager; var pathManager = Context.Instance.PathManager;
var campaignName = _Campaigns.GetItemText((int)idx); var campaignName = idx == 0 ? null : _Campaigns.GetItemText((int)idx);
var missionNames = pathManager.GetMissionNames(idx == 0 ? null : campaignName); var missionNames = pathManager.GetResourceNames(ResourceType.Mission, campaignName);
foreach (var mission in missionNames) foreach (var mission in missionNames)
{ {
_Missions.AddItem(mission); _Missions.AddItem(mission);

View File

@ -1,7 +1,5 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using Godot; using Godot;
using KeepersCompound.LGS;
namespace KeepersCompound.TMV.UI; namespace KeepersCompound.TMV.UI;
@ -83,7 +81,7 @@ public partial class ModelSelector : Control
var pathManager = Context.Instance.PathManager; var pathManager = Context.Instance.PathManager;
var campaignName = idx == 0 ? null : _Campaigns.GetItemText((int)idx); 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) foreach (var model in modelNames)
{ {
_Models.AddItem(model); _Models.AddItem(model);