update for asset name format change

MonoGame uses Linux-style paths for assets on all platforms, which breaks the previous equivalence between path and asset name formats.
This commit is contained in:
Jesse Plamondon-Willard 2021-08-12 21:26:10 -04:00
parent 727d75ae72
commit 8321cbf6eb
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
3 changed files with 7 additions and 17 deletions

View File

@ -182,18 +182,14 @@ namespace SMAPI.Tests.Utilities
[TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))] [TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))]
public void NormalizeAssetName(SamplePath path) public void NormalizeAssetName(SamplePath path)
{ {
if (Path.IsPathRooted(path.OriginalPath) || path.OriginalPath.StartsWith("/") || path.OriginalPath.StartsWith("\\")) if (Path.IsPathRooted(path.OriginalPath) || path.OriginalPath.StartsWith('/') || path.OriginalPath.StartsWith('\\'))
Assert.Ignore("Absolute paths can't be used as asset names."); Assert.Ignore("Absolute paths can't be used as asset names.");
// act // act
string normalized = PathUtilities.NormalizeAssetName(path.OriginalPath); string normalized = PathUtilities.NormalizeAssetName(path.OriginalPath);
// assert // assert
#if SMAPI_FOR_WINDOWS Assert.AreEqual(path.NormalizedOnUnix, normalized); // MonoGame uses the Linux format
Assert.AreEqual(path.NormalizedOnWindows, normalized);
#else
Assert.AreEqual(path.NormalizedOnUnix, normalized);
#endif
} }
/**** /****

View File

@ -27,7 +27,7 @@ namespace StardewModdingAPI.Toolkit.Utilities
public static readonly char PreferredPathSeparator = Path.DirectorySeparatorChar; public static readonly char PreferredPathSeparator = Path.DirectorySeparatorChar;
/// <summary>The preferred directory separator character in an asset key.</summary> /// <summary>The preferred directory separator character in an asset key.</summary>
public static readonly char PreferredAssetSeparator = PathUtilities.PreferredPathSeparator; public static readonly char PreferredAssetSeparator = '/';
/********* /*********

View File

@ -17,9 +17,6 @@ namespace StardewModdingAPI.Framework.Content
/// <summary>The underlying asset cache.</summary> /// <summary>The underlying asset cache.</summary>
private readonly IDictionary<string, object> Cache; private readonly IDictionary<string, object> Cache;
/// <summary>Applies platform-specific asset key normalization so it's consistent with the underlying cache.</summary>
private readonly Func<string, string> NormalizeAssetNameForPlatform;
/********* /*********
** Accessors ** Accessors
@ -47,11 +44,7 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="reflection">Simplifies access to private game code.</param> /// <param name="reflection">Simplifies access to private game code.</param>
public ContentCache(LocalizedContentManager contentManager, Reflector reflection) public ContentCache(LocalizedContentManager contentManager, Reflector reflection)
{ {
// init
this.Cache = reflection.GetField<Dictionary<string, object>>(contentManager, "loadedAssets").GetValue(); this.Cache = reflection.GetField<Dictionary<string, object>>(contentManager, "loadedAssets").GetValue();
// get key normalization logic
this.NormalizeAssetNameForPlatform = PathUtilities.NormalizePath; //this.NormalizeAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load<T> logic
} }
/**** /****
@ -68,23 +61,24 @@ namespace StardewModdingAPI.Framework.Content
/**** /****
** Normalize ** Normalize
****/ ****/
/// <summary>Normalize path separators in a file path. For asset keys, see <see cref="NormalizeKey"/> instead.</summary> /// <summary>Normalize path separators in an asset name.</summary>
/// <param name="path">The file path to normalize.</param> /// <param name="path">The file path to normalize.</param>
[Pure] [Pure]
public string NormalizePathSeparators(string path) public string NormalizePathSeparators(string path)
{ {
return PathUtilities.NormalizePath(path); return PathUtilities.NormalizeAssetName(path);
} }
/// <summary>Normalize a cache key so it's consistent with the underlying cache.</summary> /// <summary>Normalize a cache key so it's consistent with the underlying cache.</summary>
/// <param name="key">The asset key.</param> /// <param name="key">The asset key.</param>
/// <remarks>This is equivalent to <see cref="NormalizePathSeparators"/> with added file extension logic.</remarks>
[Pure] [Pure]
public string NormalizeKey(string key) public string NormalizeKey(string key)
{ {
key = this.NormalizePathSeparators(key); key = this.NormalizePathSeparators(key);
return key.EndsWith(".xnb", StringComparison.OrdinalIgnoreCase) return key.EndsWith(".xnb", StringComparison.OrdinalIgnoreCase)
? key.Substring(0, key.Length - 4) ? key.Substring(0, key.Length - 4)
: this.NormalizeAssetNameForPlatform(key); : key;
} }
/**** /****