summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-05-02 23:25:23 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-05-02 23:25:23 -0400
commit8503bf9cd99e094c425212264583a3416c8a5595 (patch)
tree5cdde59dc9518d0a7f33e8fd27240277f80b5f4c /src/StardewModdingAPI/Framework
parente4357c3c7d0bb3de0494e7c3547ae0dd39ee966b (diff)
downloadSMAPI-8503bf9cd99e094c425212264583a3416c8a5595.tar.gz
SMAPI-8503bf9cd99e094c425212264583a3416c8a5595.tar.bz2
SMAPI-8503bf9cd99e094c425212264583a3416c8a5595.zip
fix XNBs loaded from the mod folder through the content API never being found on Mac (#278)
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r--src/StardewModdingAPI/Framework/ContentHelper.cs59
-rw-r--r--src/StardewModdingAPI/Framework/SContentManager.cs2
2 files changed, 38 insertions, 23 deletions
diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs
index 425e0f8c..61bd52a7 100644
--- a/src/StardewModdingAPI/Framework/ContentHelper.cs
+++ b/src/StardewModdingAPI/Framework/ContentHelper.cs
@@ -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>
diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs
index 88e1df2b..54349a91 100644
--- a/src/StardewModdingAPI/Framework/SContentManager.cs
+++ b/src/StardewModdingAPI/Framework/SContentManager.cs
@@ -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);
}