fix map tilesheet load not handling seasonal variations (#352)

This commit is contained in:
Jesse Plamondon-Willard 2017-08-24 20:44:44 -04:00
parent 8df1ab7e11
commit 5622e3b319
2 changed files with 77 additions and 21 deletions

View File

@ -26,6 +26,10 @@ For mod developers:
For power users: For power users:
* Added command-line arguments to the SMAPI installer so it can be scripted. * Added command-line arguments to the SMAPI installer so it can be scripted.
## 1.15.4
For players:
* Internal fixes to support Entoarox Framework and the upcoming SMAPI 2.0 release.
## 1.15.3 ## 1.15.3
For players: For players:
* Fixed mods being marked as duplicate incorrectly in some cases. * Fixed mods being marked as duplicate incorrectly in some cases.

View File

@ -216,15 +216,60 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <exception cref="ContentLoadException">The map tilesheets could not be loaded.</exception> /// <exception cref="ContentLoadException">The map tilesheets could not be loaded.</exception>
private void FixLocalMapTilesheets(Map map, string mapKey) private void FixLocalMapTilesheets(Map map, string mapKey)
{ {
// check map info
if (!map.TileSheets.Any()) if (!map.TileSheets.Any())
return; return;
string relativeMapFolder = Path.GetDirectoryName(mapKey) ?? ""; // folder path containing the map, relative to the mod folder string relativeMapFolder = Path.GetDirectoryName(mapKey) ?? ""; // folder path containing the map, relative to the mod folder
// fix tilesheets
foreach (TileSheet tilesheet in map.TileSheets) foreach (TileSheet tilesheet in map.TileSheets)
{ {
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 key =
this.TryLoadTilesheetImageSource(relativeMapFolder, seasonalImageSource)
?? this.TryLoadTilesheetImageSource(relativeMapFolder, imageSource);
if (key != null)
{
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.");
}
}
/// <summary>Load a tilesheet image source if the file exists.</summary>
/// <param name="relativeMapFolder">The folder path containing the map, relative to the mod folder.</param>
/// <param name="imageSource">The tilesheet image source to load.</param>
/// <returns>Returns the loaded asset key (if it was loaded successfully).</returns>
private string TryLoadTilesheetImageSource(string relativeMapFolder, string imageSource)
{
if (imageSource == null)
return null;
// check for tilesheet relative to map // check for tilesheet relative to map
{ {
string localKey = Path.Combine(relativeMapFolder, tilesheet.ImageSource); string localKey = Path.Combine(relativeMapFolder, imageSource);
FileInfo localFile = this.GetModFile(localKey); FileInfo localFile = this.GetModFile(localKey);
if (localFile.Exists) if (localFile.Exists)
{ {
@ -234,29 +279,36 @@ namespace StardewModdingAPI.Framework.ModHelpers
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new ContentLoadException($"The local '{tilesheet.ImageSource}' tilesheet couldn't be loaded.", ex); throw new ContentLoadException($"The local '{imageSource}' tilesheet couldn't be loaded.", ex);
} }
tilesheet.ImageSource = this.GetActualAssetKey(localKey);
continue; return this.GetActualAssetKey(localKey);
} }
} }
// fallback to game content // fallback to game content
{ {
string contentKey = tilesheet.ImageSource; string contentKey = imageSource.EndsWith(".png")
if (contentKey.EndsWith(".png")) ? imageSource.Substring(0, imageSource.Length - 4)
contentKey = contentKey.Substring(0, contentKey.Length - 4); : imageSource;
FileInfo file = new FileInfo(Path.Combine(this.ContentManager.FullRootDirectory, contentKey + ".xnb"));
if (file.Exists)
{
try try
{ {
this.ContentManager.Load<Texture2D>(contentKey); this.ContentManager.Load<Texture2D>(contentKey);
} }
catch (Exception ex) 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;
} }
/// <summary>Assert that the given key has a valid format.</summary> /// <summary>Assert that the given key has a valid format.</summary>