From 8503bf9cd99e094c425212264583a3416c8a5595 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 2 May 2017 23:25:23 -0400 Subject: fix XNBs loaded from the mod folder through the content API never being found on Mac (#278) --- src/StardewModdingAPI/Framework/ContentHelper.cs | 59 +++++++++++++--------- src/StardewModdingAPI/Framework/SContentManager.cs | 2 + 2 files changed, 38 insertions(+), 23 deletions(-) (limited to 'src/StardewModdingAPI/Framework') 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 /// Load content from the game folder or mod folder (if not already cached), and return it. When loading a .png file, this must be called outside the game's draw loop. /// The expected data type. The main supported types are and dictionaries; other types may be supported by the game's content pipeline. - /// The asset key to fetch (if the is ), or the local path to an XNB file relative to the mod folder. + /// The asset key to fetch (if the is ), or the local path to a content file relative to the mod folder. /// Where to search for a matching content asset. /// The is empty or contains invalid characters. /// The content asset couldn't be loaded (e.g. because it doesn't exist). @@ -57,25 +57,22 @@ namespace StardewModdingAPI.Framework switch (source) { case ContentSource.GameContent: - return this.ContentManager.Load(this.StripXnbExtension(key)); + return this.ContentManager.Load(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(actualKey); + return this.ContentManager.Load(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(actualKey); + if (this.ContentManager.IsLoaded(assetPath)) + return this.ContentManager.Load(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 } /// 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. - /// The asset key to fetch (if the is ), or the local path to an XNB file relative to the mod folder. + /// The asset key to fetch (if the is ), or the local path to a content file relative to the mod folder. /// Where to search for a matching content asset. /// The is empty or contains invalid characters. 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."); } - /// Strip the .xnb extension from an asset key, since it's assumed by the underlying content manager. - /// The asset key. - private string StripXnbExtension(string key) + /// Get a file from the mod folder. + /// The asset path relative to the mod folder. + 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; + } + + /// Get the asset path which loads a mod folder through a content manager. + /// The file path relative to the mod's folder. + /// The absolute file path. + 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 } /// Get a directory path relative to a given root. 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); } -- cgit