Handle different path separators

This commit is contained in:
Jarrod Doyle 2024-09-07 08:01:15 +01:00
parent 354155db20
commit 963ab721cd
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 31 additions and 18 deletions

View File

@ -62,6 +62,11 @@ public class ResourcePathManager
_extractionPath = extractionPath; _extractionPath = extractionPath;
} }
public static string ConvertSeparator(string path)
{
return path.Replace('\\', '/');
}
public bool Init(string installPath) public bool Init(string installPath)
{ {
// TODO: This can be done less awkwardly with the resource paths // TODO: This can be done less awkwardly with the resource paths
@ -156,6 +161,7 @@ public class ResourcePathManager
throw new ArgumentException("No campaign found with given name", nameof(campaignName)); throw new ArgumentException("No campaign found with given name", nameof(campaignName));
} }
// This expects resourceName to already have it's separator converted
public (string, string) GetResourcePath( public (string, string) GetResourcePath(
ResourceType type, ResourceType type,
string campaignName, string campaignName,
@ -206,11 +212,12 @@ public class ResourcePathManager
{ {
foreach (var path in Directory.EnumerateFiles(dir, "*", texOptions)) foreach (var path in Directory.EnumerateFiles(dir, "*", texOptions))
{ {
var ext = Path.GetExtension(path); var convertedPath = ConvertSeparator(path);
var ext = Path.GetExtension(convertedPath);
if (validExtensions.Contains(ext.ToLower())) if (validExtensions.Contains(ext.ToLower()))
{ {
var key = Path.GetFileNameWithoutExtension(path).ToLower(); var key = Path.GetFileNameWithoutExtension(convertedPath).ToLower();
pathMap.TryAdd(key, path); pathMap.TryAdd(key, convertedPath);
} }
} }
} }
@ -234,11 +241,12 @@ public class ResourcePathManager
{ {
foreach (var path in Directory.EnumerateFiles(dir, "*", textureOptions)) foreach (var path in Directory.EnumerateFiles(dir, "*", textureOptions))
{ {
var ext = Path.GetExtension(path); var convertedPath = ConvertSeparator(path);
var ext = Path.GetExtension(convertedPath);
if (validExtensions.Contains(ext.ToLower())) if (validExtensions.Contains(ext.ToLower()))
{ {
var key = Path.GetRelativePath(root, path)[..^ext.Length].ToLower(); var key = Path.GetRelativePath(root, convertedPath)[..^ext.Length].ToLower();
pathMap.TryAdd(key, path); pathMap.TryAdd(key, convertedPath);
} }
} }
} }
@ -260,8 +268,9 @@ public class ResourcePathManager
{ {
foreach (var path in Directory.EnumerateFiles(dir, "*.bin", binOptions)) foreach (var path in Directory.EnumerateFiles(dir, "*.bin", binOptions))
{ {
var key = Path.GetRelativePath(dir, path).ToLower(); var convertedPath = ConvertSeparator(path);
pathMap.TryAdd(key, path); var key = Path.GetRelativePath(dir, convertedPath).ToLower();
pathMap.TryAdd(key, convertedPath);
} }
} }
@ -277,8 +286,8 @@ public class ResourcePathManager
{ {
if (line.StartsWith("load_path")) if (line.StartsWith("load_path"))
{ {
// TODO: Do we only need to replace on systems with a different path separator? // TODO: This can have multiple paths I think
var path = line.Split(" ")[1].Replace("\\", "/"); var path = ConvertSeparator(line.Split(" ")[1]);
omsPath = Path.GetFullPath(root + path); omsPath = Path.GetFullPath(root + path);
break; break;
} }
@ -294,8 +303,9 @@ public class ResourcePathManager
}; };
foreach (var path in Directory.GetFiles(omsPath, "*.mis", searchOptions)) foreach (var path in Directory.GetFiles(omsPath, "*.mis", searchOptions))
{ {
var baseName = Path.GetFileName(path).ToLower(); var convertedPath = ConvertSeparator(path);
map.Add(baseName, path); var baseName = Path.GetFileName(convertedPath).ToLower();
map.Add(baseName, convertedPath);
} }
return true; return true;
@ -329,7 +339,7 @@ public class ResourcePathManager
if (line.StartsWith("fm_path")) if (line.StartsWith("fm_path"))
{ {
// TODO: I think this can technically contain multiple paths // TODO: I think this can technically contain multiple paths
var path = line.Split(" ")[1].Replace("\\", "/"); var path = ConvertSeparator(line.Split(" ")[1]);
fmsPath = Path.GetFullPath(root + path); fmsPath = Path.GetFullPath(root + path);
break; break;
} }
@ -344,10 +354,11 @@ public class ResourcePathManager
var campaignMap = new Dictionary<string, string>(); var campaignMap = new Dictionary<string, string>();
foreach (var path in Directory.GetFiles(dir)) foreach (var path in Directory.GetFiles(dir))
{ {
if (extensions.Contains(Path.GetExtension(path).ToLower())) var convertedPath = ConvertSeparator(path);
if (extensions.Contains(Path.GetExtension(convertedPath).ToLower()))
{ {
var baseName = Path.GetFileName(path).ToLower(); var baseName = Path.GetFileName(convertedPath).ToLower();
campaignMap.Add(baseName, path); campaignMap.Add(baseName, ConvertSeparator(path));
} }
} }

View File

@ -278,7 +278,8 @@ public partial class Mission : Node3D
else else
{ {
var resType = ResourceType.ObjectTexture; var resType = ResourceType.ObjectTexture;
var resName = Path.GetFileNameWithoutExtension(prop.value); var convertedValue = ResourcePathManager.ConvertSeparator(prop.value);
var resName = Path.GetFileNameWithoutExtension(convertedValue);
path = pathManager.GetResourcePath(resType, _campaignName, resName).Item2; path = pathManager.GetResourcePath(resType, _campaignName, resName).Item2;
} }

View File

@ -52,7 +52,8 @@ public class ModelLoader
{ {
if (material.Type == 0) if (material.Type == 0)
{ {
var resName = Path.GetFileNameWithoutExtension(material.Name); var convertedName = ResourcePathManager.ConvertSeparator(material.Name);
var resName = Path.GetFileNameWithoutExtension(convertedName);
var (_, path) = pathManager.GetResourcePath(ResourceType.ObjectTexture, campaignName, resName); var (_, path) = pathManager.GetResourcePath(ResourceType.ObjectTexture, campaignName, resName);
if (path == null) if (path == null)
{ {