diff --git a/ShapeEngine/Content/ContentManager.cs b/ShapeEngine/Content/ContentManager.cs index b362f836..243274bc 100644 --- a/ShapeEngine/Content/ContentManager.cs +++ b/ShapeEngine/Content/ContentManager.cs @@ -216,9 +216,9 @@ public bool TryGetContentPack(string root, out ContentPack? pack) public bool TryLoadTexture(string filePath, out Texture2D texture) { texture = default; - + // Normalize path to be relative to RootDirectory - string relativePath = RootDirectory != string.Empty ? Path.GetRelativePath(RootDirectory, filePath) : filePath; + string relativePath = GetRelativeContentPath(filePath); // Check cache first if (loadedTextures.TryGetValue(relativePath, out texture)) @@ -275,7 +275,7 @@ public bool TryLoadFragmentShader(string filePath, out Shader shader) shader = default; // Normalize path to be relative to RootDirectory - string relativePath = RootDirectory != string.Empty ? Path.GetRelativePath(RootDirectory, filePath) : filePath; + string relativePath = GetRelativeContentPath(filePath); // Check cache first if (loadedShaders.TryGetValue(relativePath, out shader)) @@ -329,7 +329,7 @@ public bool TryLoadVertexShader(string filePath, out Shader shader) shader = default; // Normalize path to be relative to RootDirectory - string relativePath = RootDirectory != string.Empty ? Path.GetRelativePath(RootDirectory, filePath) : filePath; + string relativePath = GetRelativeContentPath(filePath); // Check cache first if (loadedShaders.TryGetValue(relativePath, out shader)) @@ -382,7 +382,7 @@ public bool TryLoadImage(string filePath, out Image image) image = default; // Normalize path to be relative to RootDirectory - string relativePath = RootDirectory != string.Empty ? Path.GetRelativePath(RootDirectory, filePath) : filePath; + string relativePath = GetRelativeContentPath(filePath); // Check cache first if (loadedImages.TryGetValue(relativePath, out image)) @@ -436,9 +436,10 @@ public bool TryLoadImage(string filePath, out Image image) public bool TryLoadFont(string filePath, out Font font, int fontSize = 100, TextureFilter textureFilter = TextureFilter.Trilinear) { font = default; - + // Normalize path to be relative to RootDirectory - string relativePath = RootDirectory != string.Empty ? Path.GetRelativePath(RootDirectory, filePath) : filePath; + string relativePath = GetRelativeContentPath(filePath); + // Check cache first if (loadedFonts.TryGetValue(relativePath, out var fontDict)) { @@ -487,6 +488,7 @@ public bool TryLoadFont(string filePath, out Font font, int fontSize = 100, Text font = loadedFont; return true; } + /// /// Attempts to load a sound from the specified file path. /// Checks the cache, content packs, and file system in order. @@ -499,7 +501,7 @@ public bool TryLoadSound(string filePath, out Sound sound) sound = default; // Normalize path to be relative to RootDirectory - string relativePath = RootDirectory != string.Empty ? Path.GetRelativePath(RootDirectory, filePath) : filePath; + string relativePath = GetRelativeContentPath(filePath); // Check cache first if (loadedSounds.TryGetValue(relativePath, out sound)) @@ -540,6 +542,7 @@ public bool TryLoadSound(string filePath, out Sound sound) sound = loadedSound; return true; } + /// /// Attempts to load a music stream from the specified file path. /// Checks the cache, content packs, and file system in order. @@ -552,7 +555,7 @@ public bool TryLoadMusic(string filePath, out Music music) music = default; // Normalize path to be relative to RootDirectory - string relativePath = RootDirectory != string.Empty ? Path.GetRelativePath(RootDirectory, filePath) : filePath; + string relativePath = GetRelativeContentPath(filePath); // Check cache first if (loadedMusicStreams.TryGetValue(relativePath, out music)) @@ -593,6 +596,7 @@ public bool TryLoadMusic(string filePath, out Music music) music = loadedMusic; return true; } + /// /// Attempts to load a wave from the specified file path. /// Checks the cache, content packs, and file system in order. @@ -605,7 +609,7 @@ public bool TryLoadWave(string filePath, out Wave wave) wave = default; // Normalize path to be relative to RootDirectory - string relativePath = RootDirectory != string.Empty ? Path.GetRelativePath(RootDirectory, filePath) : filePath; + string relativePath = GetRelativeContentPath(filePath); // Check cache first if (loadedWaves.TryGetValue(relativePath, out wave)) @@ -646,6 +650,7 @@ public bool TryLoadWave(string filePath, out Wave wave) wave = loadedWave; return true; } + /// /// Attempts to load a text file from the specified file path. /// Checks the cache, content packs, and file system in order. @@ -658,7 +663,7 @@ public bool TryLoadText(string filePath, out string text) text = string.Empty; // Normalize path to be relative to RootDirectory - string relativePath = RootDirectory != string.Empty ? Path.GetRelativePath(RootDirectory, filePath) : filePath; + string relativePath = GetRelativeContentPath(filePath); // Check cache first if (loadedTexts.TryGetValue(relativePath, out string? cachedText)) @@ -837,25 +842,20 @@ private bool FindContentPack(string relativePath, out ContentPack? pack, out st return false; } - - /// - /// Fixes the root directory of the specified file path by ensuring it is prefixed with the ContentManager's root directory. - /// - /// The file path to fix. - /// A file path with the correct root directory prefix. - /// - /// If the is empty or does not already start with the , - /// the root directory is prepended to the path using the directory separator ("/"). - /// If the is empty, it is returned as-is. - /// If the already starts with the , it is returned unchanged. - /// - private string FixFilePathRoot(string filePath) + + private string GetRelativeContentPath(string filePath) { - if (RootDirectory == string.Empty) return filePath; + if (RootDirectory == string.Empty || filePath == string.Empty) return filePath; - if (filePath.StartsWith(RootDirectory)) return filePath; + string normalizedRoot = RootDirectory.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); + string normalizedFile = filePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); + + if (normalizedFile.StartsWith(normalizedRoot)) + { + return Path.GetRelativePath(normalizedRoot, normalizedFile); + } - return RootDirectory + "/" + filePath; + return filePath; } #endregion