From dac21226d275fca4ce4880ab9e15104f7987c133 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 1 Sep 2017 21:05:08 -0400 Subject: fix IAssetLoader instances not able to load a map tilesheet if it doesn't also exist in the content folder (#352) --- .../Framework/ModHelpers/ContentHelper.cs | 39 ++++++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'src/StardewModdingAPI') diff --git a/src/StardewModdingAPI/Framework/ModHelpers/ContentHelper.cs b/src/StardewModdingAPI/Framework/ModHelpers/ContentHelper.cs index 21201970..d24124e0 100644 --- a/src/StardewModdingAPI/Framework/ModHelpers/ContentHelper.cs +++ b/src/StardewModdingAPI/Framework/ModHelpers/ContentHelper.cs @@ -228,7 +228,7 @@ namespace StardewModdingAPI.Framework.ModHelpers // get seasonal name (if applicable) string seasonalImageSource = null; - if(Game1.currentSeason != null && Game1.currentSeason != "spring") + if (Game1.currentSeason != null && Game1.currentSeason != "spring") { string filename = Path.GetFileName(imageSource); string dirPath = imageSource.Substring(0, imageSource.LastIndexOf(filename)); @@ -292,19 +292,23 @@ namespace StardewModdingAPI.Framework.ModHelpers ? imageSource.Substring(0, imageSource.Length - 4) : imageSource; - FileInfo file = new FileInfo(Path.Combine(this.ContentManager.FullRootDirectory, contentKey + ".xnb")); - if (file.Exists) + try { - try - { - this.ContentManager.Load(contentKey); - } - catch (Exception ex) - { - throw new ContentLoadException($"The '{imageSource}' tilesheet couldn't be loaded relative to either map file or the game's content folder.", ex); - } + this.Load(contentKey, ContentSource.GameContent); return contentKey; } + catch + { + // ignore file-not-found errors + // TODO: while it's useful to suppress a asset-not-found error here to avoid + // confusion, this is a pretty naive approach. Even if the file doesn't exist, + // the file may have been loaded through an IAssetLoader which failed. So even + // if the content file doesn't exist, that doesn't mean the error here is a + // content-not-found error. Unfortunately XNA doesn't provide a good way to + // detect the error type. + if (this.GetContentFolderFile(contentKey).Exists) + throw; + } } // not found @@ -342,6 +346,19 @@ namespace StardewModdingAPI.Framework.ModHelpers return file; } + /// Get a file from the game's content folder. + /// The asset key. + private FileInfo GetContentFolderFile(string key) + { + // get file path + string path = Path.Combine(this.ContentManager.FullRootDirectory, key); + if (!path.EndsWith(".xnb")) + path += ".xnb"; + + // get file + return new FileInfo(path); + } + /// 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. -- cgit