Preload obejct texture paths and use jorge for failed model textures

This commit is contained in:
Jarrod Doyle 2024-08-29 18:36:36 +01:00
parent 74bde133bb
commit 30bfd05073
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 51 additions and 35 deletions

View File

@ -13,6 +13,7 @@ public class ResourcePathManager
public Dictionary<string, string> missionPathMap; public Dictionary<string, string> missionPathMap;
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;
} }
private bool _initialised = false; private bool _initialised = false;
@ -53,12 +54,14 @@ public class ResourcePathManager
} }
ZipFile.OpenRead(objPath).ExtractToDirectory(objExtractPath); ZipFile.OpenRead(objPath).ExtractToDirectory(objExtractPath);
var objectPathMap = GetObjectPaths(_extractionPath); var objectPathMap = GetObjectPaths(_extractionPath);
var objectTexturePathMap = GetObjectTexturePaths(_extractionPath);
_omResources = new CampaignResources _omResources = new CampaignResources
{ {
missionPathMap = omsMap, missionPathMap = omsMap,
texturePathMap = texturePathMap, texturePathMap = texturePathMap,
objectPathMap = objectPathMap, objectPathMap = objectPathMap,
objectTexturePathMap = objectTexturePathMap,
}; };
} }
@ -66,13 +69,17 @@ public class ResourcePathManager
_fmResources = new Dictionary<string, CampaignResources>(); _fmResources = new Dictionary<string, CampaignResources>();
foreach (var (campaign, missionPathMap) in fmsMap) foreach (var (campaign, missionPathMap) in fmsMap)
{ {
var texturePathMap = GetTexturePaths(Path.Join(fmsDir, campaign)); var root = Path.Join(fmsDir, campaign);
var objectPathMap = GetObjectPaths(Path.Join(fmsDir, campaign)); var texturePathMap = GetTexturePaths(root);
var objectPathMap = GetObjectPaths(root);
var objectTexturePathMap = GetObjectTexturePaths(root);
var resource = new CampaignResources var resource = new CampaignResources
{ {
missionPathMap = missionPathMap, missionPathMap = missionPathMap,
texturePathMap = texturePathMap, texturePathMap = texturePathMap,
objectPathMap = objectPathMap, objectPathMap = objectPathMap,
objectTexturePathMap = objectTexturePathMap,
}; };
_fmResources.Add(campaign, resource); _fmResources.Add(campaign, resource);
_fmsDir = fmsDir; _fmsDir = fmsDir;
@ -191,49 +198,59 @@ public class ResourcePathManager
return null; return null;
} }
// TODO: Store these as part of the resources public string GetObjectTexturePath(string campaignName, string textureName)
public string GetObjectTexturePath(string campaignName, string modelName, string textureName)
{ {
var options = new EnumerationOptions 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)
{
string[] validExtensions = { ".dds", ".png", ".tga", ".pcx", ".gif", ".bmp", ".cel", };
var dirOptions = new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive };
var texOptions = new EnumerationOptions
{ {
MatchCasing = MatchCasing.CaseInsensitive, MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = true, RecurseSubdirectories = true,
}; };
textureName = textureName.ToLower(); var pathMap = new Dictionary<string, string>();
var omDir = Path.Join(_extractionPath, "obj"); foreach (var dir in Directory.EnumerateDirectories(root, "obj", dirOptions))
var omPaths = Directory.GetFiles(omDir, textureName, options);
if (campaignName == null || campaignName == "")
{ {
if (omPaths.Length > 0) foreach (var path in Directory.EnumerateFiles(dir, "*", texOptions))
{ {
return omPaths[0]; var ext = Path.GetExtension(path);
if (validExtensions.Contains(ext.ToLower()))
{
var key = Path.GetFileNameWithoutExtension(path).ToLower();
pathMap.TryAdd(key, path);
} }
} }
else if (_fmResources.TryGetValue(campaignName, out var campaign))
{
// TODO: This is a fucking mess
// Basically we need to handle when the campaign doesn't have a models folder lol
if (campaign.objectPathMap.TryGetValue(modelName, out var modelPath))
{
var dir = Path.GetDirectoryName(modelPath);
var fmPaths = Directory.GetFiles(dir, textureName, options);
if (fmPaths.Length > 0)
{
return fmPaths[0];
}
else if (omPaths.Length > 0)
{
return omPaths[0];
}
}
else if (omPaths.Length > 0)
{
return omPaths[0];
}
} }
return null; return pathMap;
} }
// TODO: Handle object textures? // TODO: Handle object textures?

View File

@ -56,12 +56,11 @@ public class ModelLoader
{ {
if (material.Type == 0) if (material.Type == 0)
{ {
var path = pathManager.GetObjectTexturePath(campaignName, modelName, material.Name); var path = pathManager.GetObjectTexturePath(campaignName, material.Name);
if (path == null) if (path == null)
{ {
// TODO: JORGE path = "user://textures/jorge.png";
GD.Print($"Failed to load model texture: {material.Name}"); GD.Print($"Failed to load model texture: {material.Name}");
continue;
} }
materials.Add(new StandardMaterial3D materials.Add(new StandardMaterial3D