summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-05-25 21:35:43 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-05-25 21:35:43 -0400
commit7f210cd7b0717cd0ca242ceb730846847a356796 (patch)
tree62007c790eee4701f896ad39bdcd989c6a689263 /src/StardewModdingAPI/Framework
parenta47ca7e3910d532a3468f2ff222c9c1ed28514c4 (diff)
downloadSMAPI-7f210cd7b0717cd0ca242ceb730846847a356796.tar.gz
SMAPI-7f210cd7b0717cd0ca242ceb730846847a356796.tar.bz2
SMAPI-7f210cd7b0717cd0ca242ceb730846847a356796.zip
fix tilesheets for local XNB maps too
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r--src/StardewModdingAPI/Framework/ContentHelper.cs116
1 files changed, 67 insertions, 49 deletions
diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs
index e15f0dcf..2a590609 100644
--- a/src/StardewModdingAPI/Framework/ContentHelper.cs
+++ b/src/StardewModdingAPI/Framework/ContentHelper.cs
@@ -80,61 +80,30 @@ namespace StardewModdingAPI.Framework
{
// XNB file
case ".xnb":
- return this.ContentManager.Load<T>(assetPath);
+ {
+ T asset = this.ContentManager.Load<T>(assetPath);
+ if (asset is Map)
+ this.FixLocalMapTilesheets(asset as Map, key);
+ return asset;
+ }
// unpacked map
case ".tbin":
- // validate
- if (typeof(T) != typeof(Map))
- throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Map)}'.");
-
- // fetch & cache
- FormatManager formatManager = FormatManager.Instance;
- Map map = formatManager.LoadMap(file.FullName);
- if (map.TileSheets.Any())
{
- string relativeMapFolder = Path.GetDirectoryName(key) ?? ""; // folder path containing the map, relative to the mod folder
- foreach (TileSheet tilesheet in map.TileSheets)
- {
- // check for tilesheet relative to map
- {
- string localKey = Path.Combine(relativeMapFolder, tilesheet.ImageSource);
- FileInfo localFile = this.GetModFile(localKey);
- if (localFile.Exists)
- {
- try
- {
- this.Load<Texture2D>(localKey);
- }
- catch (Exception ex)
- {
- throw new ContentLoadException($"{this.ModName} failed loading map '{key}' from {source} because the local '{tilesheet.ImageSource}' tilesheet couldn't be loaded.", ex);
- }
- tilesheet.ImageSource = this.GetActualAssetKey(localKey);
- continue;
- }
- }
-
- // fallback to game content
- string contentKey = tilesheet.ImageSource;
- if (contentKey.EndsWith(".png"))
- contentKey = contentKey.Substring(0, contentKey.Length - 4);
- try
- {
- this.ContentManager.Load<Texture2D>(contentKey);
- }
- catch (Exception ex)
- {
- throw new ContentLoadException($"{this.ModName} failed loading map '{key}' from {source} because the '{tilesheet.ImageSource}' tilesheet couldn't be found relative to either map file or the game's content folder.", ex);
- }
- tilesheet.ImageSource = contentKey;
- }
+ // validate
+ if (typeof(T) != typeof(Map))
+ throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Map)}'.");
+
+ // fetch & cache
+ FormatManager formatManager = FormatManager.Instance;
+ Map map = formatManager.LoadMap(file.FullName);
+ this.FixLocalMapTilesheets(map, key);
+
+ // inject map
+ this.ContentManager.Inject(assetPath, map);
+ return (T)(object)map;
}
- // inject map
- this.ContentManager.Inject(assetPath, map);
- return (T)(object)map;
-
// unpacked image
case ".png":
// validate
@@ -188,6 +157,55 @@ namespace StardewModdingAPI.Framework
/*********
** Private methods
*********/
+ /// <summary>Fix the tilesheets for a map loaded from the mod folder.</summary>
+ /// <param name="map">The map whose tilesheets to fix.</param>
+ /// <param name="mapKey">The map asset key within the mod folder.</param>
+ /// <exception cref="ContentLoadException">The map tilesheets could not be loaded.</exception>
+ private void FixLocalMapTilesheets(Map map, string mapKey)
+ {
+ if (!map.TileSheets.Any())
+ return;
+
+ string relativeMapFolder = Path.GetDirectoryName(mapKey) ?? ""; // folder path containing the map, relative to the mod folder
+ foreach (TileSheet tilesheet in map.TileSheets)
+ {
+ // check for tilesheet relative to map
+ {
+ string localKey = Path.Combine(relativeMapFolder, tilesheet.ImageSource);
+ FileInfo localFile = this.GetModFile(localKey);
+ if (localFile.Exists)
+ {
+ try
+ {
+ this.Load<Texture2D>(localKey);
+ }
+ catch (Exception ex)
+ {
+ throw new ContentLoadException($"{this.ModName} failed loading map '{mapKey}' from {ContentSource.ModFolder} because the local '{tilesheet.ImageSource}' tilesheet couldn't be loaded.", ex);
+ }
+ tilesheet.ImageSource = this.GetActualAssetKey(localKey);
+ continue;
+ }
+ }
+
+ // fallback to game content
+ {
+ string contentKey = tilesheet.ImageSource;
+ if (contentKey.EndsWith(".png"))
+ contentKey = contentKey.Substring(0, contentKey.Length - 4);
+ try
+ {
+ this.ContentManager.Load<Texture2D>(contentKey);
+ }
+ catch (Exception ex)
+ {
+ throw new ContentLoadException($"{this.ModName} failed loading map '{mapKey}' from {ContentSource.ModFolder} because the '{tilesheet.ImageSource}' tilesheet couldn't be found relative to either map file or the game's content folder.", ex);
+ }
+ tilesheet.ImageSource = contentKey;
+ }
+ }
+ }
+
/// <summary>Assert that the given key has a valid format.</summary>
/// <param name="key">The asset key to check.</param>
/// <exception cref="ArgumentException">The asset key is empty or contains invalid characters.</exception>