Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions ShapeEngine/Content/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -487,6 +488,7 @@ public bool TryLoadFont(string filePath, out Font font, int fontSize = 100, Text
font = loadedFont;
return true;
}

/// <summary>
/// Attempts to load a sound from the specified file path.
/// Checks the cache, content packs, and file system in order.
Expand All @@ -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))
Expand Down Expand Up @@ -540,6 +542,7 @@ public bool TryLoadSound(string filePath, out Sound sound)
sound = loadedSound;
return true;
}

/// <summary>
/// Attempts to load a music stream from the specified file path.
/// Checks the cache, content packs, and file system in order.
Expand All @@ -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))
Expand Down Expand Up @@ -593,6 +596,7 @@ public bool TryLoadMusic(string filePath, out Music music)
music = loadedMusic;
return true;
}

/// <summary>
/// Attempts to load a wave from the specified file path.
/// Checks the cache, content packs, and file system in order.
Expand All @@ -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))
Expand Down Expand Up @@ -646,6 +650,7 @@ public bool TryLoadWave(string filePath, out Wave wave)
wave = loadedWave;
return true;
}

/// <summary>
/// Attempts to load a text file from the specified file path.
/// Checks the cache, content packs, and file system in order.
Expand All @@ -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))
Expand Down Expand Up @@ -837,25 +842,20 @@ private bool FindContentPack(string relativePath, out ContentPack? pack, out st
return false;
}


/// <summary>
/// Fixes the root directory of the specified file path by ensuring it is prefixed with the ContentManager's root directory.
/// </summary>
/// <param name="filePath">The file path to fix.</param>
/// <returns>A file path with the correct root directory prefix.</returns>
/// <remarks>
/// If the <paramref name="filePath"/> is empty or does not already start with the <see cref="RootDirectory"/>,
/// the root directory is prepended to the path using the directory separator ("/").
/// If the <paramref name="filePath"/> is empty, it is returned as-is.
/// If the <paramref name="filePath"/> already starts with the <see cref="RootDirectory"/>, it is returned unchanged.
/// </remarks>
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

Expand Down
Loading