fix XNBs loaded from the mod folder through the content API never being found on Mac (#278)

This commit is contained in:
Jesse Plamondon-Willard 2017-05-02 23:25:23 -04:00
parent e4357c3c7d
commit 8503bf9cd9
4 changed files with 42 additions and 26 deletions

View File

@ -14,7 +14,8 @@ For mod developers:
See [log](https://github.com/Pathoschild/SMAPI/compare/1.11...1.12).
For mod developers:
* Fixed error when using content API to load a PNG during early game init (e.g. in mod's `Entry`).
* Fixed content API error when loading a PNG during early game init (e.g. in mod's `Entry`).
* Fixed content API error when loading an XNB from the mod folder on Mac.
## 1.11
See [log](https://github.com/Pathoschild/SMAPI/compare/1.10...1.11).

View File

@ -45,7 +45,7 @@ namespace StardewModdingAPI.Framework
/// <summary>Load content from the game folder or mod folder (if not already cached), and return it. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary>
/// <typeparam name="T">The expected data type. The main supported types are <see cref="Texture2D"/> and dictionaries; other types may be supported by the game's content pipeline.</typeparam>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to an XNB file relative to the mod folder.</param>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param>
/// <param name="source">Where to search for a matching content asset.</param>
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
/// <exception cref="ContentLoadException">The content asset couldn't be loaded (e.g. because it doesn't exist).</exception>
@ -57,25 +57,22 @@ namespace StardewModdingAPI.Framework
switch (source)
{
case ContentSource.GameContent:
return this.ContentManager.Load<T>(this.StripXnbExtension(key));
return this.ContentManager.Load<T>(key);
case ContentSource.ModFolder:
// find content file
key = this.ContentManager.NormalisePathSeparators(key);
FileInfo file = new FileInfo(Path.Combine(this.ModFolderPath, key));
if (!file.Exists && file.Extension == "")
file = new FileInfo(Path.Combine(this.ModFolderPath, key + ".xnb"));
// get file
FileInfo file = this.GetModFile(key);
if (!file.Exists)
throw new ContentLoadException($"There is no file at path '{file.FullName}'.");
// get underlying asset key
string actualKey = this.GetActualAssetKey(key, source);
// get asset path
string assetPath = this.GetModAssetPath(key, file.FullName);
// load content
switch (file.Extension.ToLower())
{
case ".xnb":
return this.ContentManager.Load<T>(actualKey);
return this.ContentManager.Load<T>(assetPath);
case ".png":
// validate
@ -83,15 +80,15 @@ namespace StardewModdingAPI.Framework
throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'.");
// try cache
if (this.ContentManager.IsLoaded(actualKey))
return this.ContentManager.Load<T>(actualKey);
if (this.ContentManager.IsLoaded(assetPath))
return this.ContentManager.Load<T>(assetPath);
// fetch & cache
using (FileStream stream = File.OpenRead(file.FullName))
{
Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
texture = this.PremultiplyTransparency(texture);
this.ContentManager.Inject(actualKey, texture);
this.ContentManager.Inject(assetPath, texture);
return (T)(object)texture;
}
@ -110,7 +107,7 @@ namespace StardewModdingAPI.Framework
}
/// <summary>Get the underlying key in the game's content cache for an asset. This can be used to load custom map tilesheets, but should be avoided when you can use the content API instead. This does not validate whether the asset exists.</summary>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to an XNB file relative to the mod folder.</param>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param>
/// <param name="source">Where to search for a matching content asset.</param>
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
public string GetActualAssetKey(string key, ContentSource source)
@ -118,11 +115,11 @@ namespace StardewModdingAPI.Framework
switch (source)
{
case ContentSource.GameContent:
return this.ContentManager.NormaliseAssetName(this.StripXnbExtension(key));
return this.ContentManager.NormaliseAssetName(key);
case ContentSource.ModFolder:
string contentPath = Path.Combine(this.ModFolderPathFromContent, key);
return this.ContentManager.NormaliseAssetName(this.StripXnbExtension(contentPath));
FileInfo file = this.GetModFile(key);
return this.ContentManager.NormaliseAssetName(this.GetModAssetPath(key, file.FullName));
default:
throw new NotSupportedException($"Unknown content source '{source}'.");
@ -145,13 +142,29 @@ namespace StardewModdingAPI.Framework
throw new ArgumentException("The asset key or local path contains invalid characters.");
}
/// <summary>Strip the .xnb extension from an asset key, since it's assumed by the underlying content manager.</summary>
/// <param name="key">The asset key.</param>
private string StripXnbExtension(string key)
/// <summary>Get a file from the mod folder.</summary>
/// <param name="path">The asset path relative to the mod folder.</param>
private FileInfo GetModFile(string path)
{
if (key.EndsWith(".xnb", StringComparison.InvariantCultureIgnoreCase))
return key.Substring(0, key.Length - 4);
return key;
path = Path.Combine(this.ModFolderPath, this.ContentManager.NormalisePathSeparators(path));
FileInfo file = new FileInfo(path);
if (!file.Exists && file.Extension == "")
file = new FileInfo(Path.Combine(this.ModFolderPath, path + ".xnb"));
return file;
}
/// <summary>Get the asset path which loads a mod folder through a content manager.</summary>
/// <param name="localPath">The file path relative to the mod's folder.</param>
/// <param name="absolutePath">The absolute file path.</param>
private string GetModAssetPath(string localPath, string absolutePath)
{
#if SMAPI_FOR_WINDOWS
// XNA doesn't allow absolute asset paths, so get a path relative to the content folder
return Path.Combine(this.ModFolderPathFromContent, localPath);
#else
// MonoGame is weird about relative paths on Mac, but allows absolute paths
return absolutePath;
#endif
}
/// <summary>Get a directory path relative to a given root.</summary>

View File

@ -99,6 +99,8 @@ namespace StardewModdingAPI.Framework
public string NormaliseAssetName(string assetName)
{
assetName = this.NormalisePathSeparators(assetName);
if (assetName.EndsWith(".xnb", StringComparison.InvariantCultureIgnoreCase))
return assetName.Substring(0, assetName.Length - 4);
return this.NormaliseAssetNameForPlatform(assetName);
}

View File

@ -9,14 +9,14 @@ namespace StardewModdingAPI
{
/// <summary>Load content from the game folder or mod folder (if not already cached), and return it. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary>
/// <typeparam name="T">The expected data type. The main supported types are <see cref="Texture2D"/> and dictionaries; other types may be supported by the game's content pipeline.</typeparam>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to an XNB file relative to the mod folder.</param>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param>
/// <param name="source">Where to search for a matching content asset.</param>
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
/// <exception cref="ContentLoadException">The content asset couldn't be loaded (e.g. because it doesn't exist).</exception>
T Load<T>(string key, ContentSource source);
/// <summary>Get the underlying key in the game's content cache for an asset. This can be used to load custom map tilesheets, but should be avoided when you can use the content API instead. This does not validate whether the asset exists.</summary>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to an XNB file relative to the mod folder.</param>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param>
/// <param name="source">Where to search for a matching content asset.</param>
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
string GetActualAssetKey(string key, ContentSource source);