From 5622e3b319cf7a312537f2a555fee9e67628b1cd Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 24 Aug 2017 20:44:44 -0400 Subject: fix map tilesheet load not handling seasonal variations (#352) --- .../Framework/ModHelpers/ContentHelper.cs | 92 +++++++++++++++++----- 1 file changed, 72 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Framework/ModHelpers/ContentHelper.cs b/src/StardewModdingAPI/Framework/ModHelpers/ContentHelper.cs index ffa78ff6..21201970 100644 --- a/src/StardewModdingAPI/Framework/ModHelpers/ContentHelper.cs +++ b/src/StardewModdingAPI/Framework/ModHelpers/ContentHelper.cs @@ -216,47 +216,99 @@ namespace StardewModdingAPI.Framework.ModHelpers /// The map tilesheets could not be loaded. private void FixLocalMapTilesheets(Map map, string mapKey) { + // check map info if (!map.TileSheets.Any()) return; - string relativeMapFolder = Path.GetDirectoryName(mapKey) ?? ""; // folder path containing the map, relative to the mod folder + + // fix tilesheets foreach (TileSheet tilesheet in map.TileSheets) { - // check for tilesheet relative to map + string imageSource = tilesheet.ImageSource; + + // get seasonal name (if applicable) + string seasonalImageSource = null; + if(Game1.currentSeason != null && Game1.currentSeason != "spring") + { + string filename = Path.GetFileName(imageSource); + string dirPath = imageSource.Substring(0, imageSource.LastIndexOf(filename)); + if (filename.StartsWith("spring_")) + seasonalImageSource = dirPath + Game1.currentSeason + "_" + filename.Substring("spring_".Length); + } + + // load best match + try { - string localKey = Path.Combine(relativeMapFolder, tilesheet.ImageSource); - FileInfo localFile = this.GetModFile(localKey); - if (localFile.Exists) + string key = + this.TryLoadTilesheetImageSource(relativeMapFolder, seasonalImageSource) + ?? this.TryLoadTilesheetImageSource(relativeMapFolder, imageSource); + if (key != null) { - try - { - this.Load(localKey); - } - catch (Exception ex) - { - throw new ContentLoadException($"The local '{tilesheet.ImageSource}' tilesheet couldn't be loaded.", ex); - } - tilesheet.ImageSource = this.GetActualAssetKey(localKey); + tilesheet.ImageSource = key; continue; } } + 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); + } + + // none found + throw new ContentLoadException($"The '{imageSource}' tilesheet couldn't be loaded relative to either map file or the game's content folder."); + } + } + + /// Load a tilesheet image source if the file exists. + /// The folder path containing the map, relative to the mod folder. + /// The tilesheet image source to load. + /// Returns the loaded asset key (if it was loaded successfully). + private string TryLoadTilesheetImageSource(string relativeMapFolder, string imageSource) + { + if (imageSource == null) + return null; + + // check for tilesheet relative to map + { + string localKey = Path.Combine(relativeMapFolder, imageSource); + FileInfo localFile = this.GetModFile(localKey); + if (localFile.Exists) + { + try + { + this.Load(localKey); + } + catch (Exception ex) + { + throw new ContentLoadException($"The local '{imageSource}' tilesheet couldn't be loaded.", ex); + } + + return this.GetActualAssetKey(localKey); + } + } + + // fallback to game content + { + string contentKey = imageSource.EndsWith(".png") + ? imageSource.Substring(0, imageSource.Length - 4) + : imageSource; - // fallback to game content + FileInfo file = new FileInfo(Path.Combine(this.ContentManager.FullRootDirectory, contentKey + ".xnb")); + if (file.Exists) { - string contentKey = tilesheet.ImageSource; - if (contentKey.EndsWith(".png")) - contentKey = contentKey.Substring(0, contentKey.Length - 4); try { this.ContentManager.Load(contentKey); } catch (Exception ex) { - throw new ContentLoadException($"The '{tilesheet.ImageSource}' tilesheet couldn't be loaded relative to either map file or the game's content folder.", ex); + throw new ContentLoadException($"The '{imageSource}' tilesheet couldn't be loaded relative to either map file or the game's content folder.", ex); } - tilesheet.ImageSource = contentKey; + return contentKey; } } + + // not found + return null; } /// Assert that the given key has a valid format. -- cgit