Reduce duplicate code in Resource Path Manager
This commit is contained in:
parent
63daccb3c1
commit
12a8b4f06d
|
@ -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<string, string> texturePathMap;
|
||||
public Dictionary<string, string> objectPathMap;
|
||||
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;
|
||||
|
@ -102,146 +137,57 @@ public class ResourcePathManager
|
|||
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 (_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<string> GetMissionNames(string campaignName)
|
||||
{
|
||||
if (!_initialised) return null;
|
||||
|
||||
if (campaignName == null || campaignName == "")
|
||||
{
|
||||
var names = new List<string>(_omResources.missionPathMap.Keys);
|
||||
names.Sort();
|
||||
return names;
|
||||
return _omResources.GetResourceNames(type);
|
||||
}
|
||||
else if (_fmResources.TryGetValue(campaignName, out var campaign))
|
||||
{
|
||||
var names = new List<string>(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<string> 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<string>(_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<string>(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<string, string> GetObjectTexturePaths(string root)
|
||||
|
@ -272,7 +218,6 @@ public class ResourcePathManager
|
|||
return pathMap;
|
||||
}
|
||||
|
||||
// TODO: Handle object textures?
|
||||
private static Dictionary<string, string> GetTexturePaths(string root)
|
||||
{
|
||||
string[] validExtensions = { ".dds", ".png", ".tga", ".pcx", ".gif", ".bmp", ".cel", };
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue