summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-02-25 01:07:32 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-02-25 01:07:32 -0500
commitd70d449c5c99e68677e83c66bdaf300be6b71ee2 (patch)
treed0658ed05ec32fbb01be5fa174df782093e5fec1 /src/SMAPI
parent6fc1bf47136a620b3d270af6307426080b7d1ab2 (diff)
downloadSMAPI-d70d449c5c99e68677e83c66bdaf300be6b71ee2.tar.gz
SMAPI-d70d449c5c99e68677e83c66bdaf300be6b71ee2.tar.bz2
SMAPI-d70d449c5c99e68677e83c66bdaf300be6b71ee2.zip
fix issue where replacing an asset via asset.AsImage() or asset.AsDictionary() has no effect
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Framework/Content/AssetData.cs12
-rw-r--r--src/SMAPI/Framework/Content/AssetDataForDictionary.cs5
-rw-r--r--src/SMAPI/Framework/Content/AssetDataForImage.cs5
-rw-r--r--src/SMAPI/Framework/Content/AssetDataForObject.cs8
4 files changed, 21 insertions, 9 deletions
diff --git a/src/SMAPI/Framework/Content/AssetData.cs b/src/SMAPI/Framework/Content/AssetData.cs
index 1ab9eebd..0a86f54d 100644
--- a/src/SMAPI/Framework/Content/AssetData.cs
+++ b/src/SMAPI/Framework/Content/AssetData.cs
@@ -7,6 +7,13 @@ namespace StardewModdingAPI.Framework.Content
internal class AssetData<TValue> : AssetInfo, IAssetData<TValue>
{
/*********
+ ** Properties
+ *********/
+ /// <summary>A callback to invoke when the data is replaced (if any).</summary>
+ private readonly Action<TValue> OnDataReplaced;
+
+
+ /*********
** Accessors
*********/
/// <summary>The content data being read.</summary>
@@ -21,10 +28,12 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="assetName">The normalised asset name being read.</param>
/// <param name="data">The content data being read.</param>
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
- public AssetData(string locale, string assetName, TValue data, Func<string, string> getNormalisedPath)
+ /// <param name="onDataReplaced">A callback to invoke when the data is replaced (if any).</param>
+ public AssetData(string locale, string assetName, TValue data, Func<string, string> getNormalisedPath, Action<TValue> onDataReplaced)
: base(locale, assetName, data.GetType(), getNormalisedPath)
{
this.Data = data;
+ this.OnDataReplaced = onDataReplaced;
}
/// <summary>Replace the entire content value with the given value. This is generally not recommended, since it may break compatibility with other mods or different versions of the game.</summary>
@@ -39,6 +48,7 @@ namespace StardewModdingAPI.Framework.Content
throw new InvalidCastException($"Can't replace loaded asset of type {this.GetFriendlyTypeName(this.DataType)} with value of type {this.GetFriendlyTypeName(value.GetType())}. The new type must be compatible to prevent game errors.");
this.Data = value;
+ this.OnDataReplaced?.Invoke(value);
}
}
}
diff --git a/src/SMAPI/Framework/Content/AssetDataForDictionary.cs b/src/SMAPI/Framework/Content/AssetDataForDictionary.cs
index e9b29b12..7b80875f 100644
--- a/src/SMAPI/Framework/Content/AssetDataForDictionary.cs
+++ b/src/SMAPI/Framework/Content/AssetDataForDictionary.cs
@@ -15,8 +15,9 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="assetName">The normalised asset name being read.</param>
/// <param name="data">The content data being read.</param>
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
- public AssetDataForDictionary(string locale, string assetName, IDictionary<TKey, TValue> data, Func<string, string> getNormalisedPath)
- : base(locale, assetName, data, getNormalisedPath) { }
+ /// <param name="onDataReplaced">A callback to invoke when the data is replaced (if any).</param>
+ public AssetDataForDictionary(string locale, string assetName, IDictionary<TKey, TValue> data, Func<string, string> getNormalisedPath, Action<IDictionary<TKey, TValue>> onDataReplaced)
+ : base(locale, assetName, data, getNormalisedPath, onDataReplaced) { }
/// <summary>Add or replace an entry in the dictionary.</summary>
/// <param name="key">The entry key.</param>
diff --git a/src/SMAPI/Framework/Content/AssetDataForImage.cs b/src/SMAPI/Framework/Content/AssetDataForImage.cs
index 45c5588b..c665484f 100644
--- a/src/SMAPI/Framework/Content/AssetDataForImage.cs
+++ b/src/SMAPI/Framework/Content/AssetDataForImage.cs
@@ -15,8 +15,9 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="assetName">The normalised asset name being read.</param>
/// <param name="data">The content data being read.</param>
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
- public AssetDataForImage(string locale, string assetName, Texture2D data, Func<string, string> getNormalisedPath)
- : base(locale, assetName, data, getNormalisedPath) { }
+ /// <param name="onDataReplaced">A callback to invoke when the data is replaced (if any).</param>
+ public AssetDataForImage(string locale, string assetName, Texture2D data, Func<string, string> getNormalisedPath, Action<Texture2D> onDataReplaced)
+ : base(locale, assetName, data, getNormalisedPath, onDataReplaced) { }
/// <summary>Overwrite part of the image.</summary>
/// <param name="source">The image to patch into the content.</param>
diff --git a/src/SMAPI/Framework/Content/AssetDataForObject.cs b/src/SMAPI/Framework/Content/AssetDataForObject.cs
index f30003e4..90f9e2d4 100644
--- a/src/SMAPI/Framework/Content/AssetDataForObject.cs
+++ b/src/SMAPI/Framework/Content/AssetDataForObject.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
@@ -16,7 +16,7 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="data">The content data being read.</param>
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
public AssetDataForObject(string locale, string assetName, object data, Func<string, string> getNormalisedPath)
- : base(locale, assetName, data, getNormalisedPath) { }
+ : base(locale, assetName, data, getNormalisedPath, onDataReplaced: null) { }
/// <summary>Construct an instance.</summary>
/// <param name="info">The asset metadata.</param>
@@ -31,14 +31,14 @@ namespace StardewModdingAPI.Framework.Content
/// <exception cref="InvalidOperationException">The content being read isn't a dictionary.</exception>
public IAssetDataForDictionary<TKey, TValue> AsDictionary<TKey, TValue>()
{
- return new AssetDataForDictionary<TKey, TValue>(this.Locale, this.AssetName, this.GetData<IDictionary<TKey, TValue>>(), this.GetNormalisedPath);
+ return new AssetDataForDictionary<TKey, TValue>(this.Locale, this.AssetName, this.GetData<IDictionary<TKey, TValue>>(), this.GetNormalisedPath, this.ReplaceWith);
}
/// <summary>Get a helper to manipulate the data as an image.</summary>
/// <exception cref="InvalidOperationException">The content being read isn't an image.</exception>
public IAssetDataForImage AsImage()
{
- return new AssetDataForImage(this.Locale, this.AssetName, this.GetData<Texture2D>(), this.GetNormalisedPath);
+ return new AssetDataForImage(this.Locale, this.AssetName, this.GetData<Texture2D>(), this.GetNormalisedPath, this.ReplaceWith);
}
/// <summary>Get the data as a given type.</summary>