diff --git a/src/StardewModdingAPI/Events/ContentEvents.cs b/src/StardewModdingAPI/Events/ContentEvents.cs
index 8fa9ae3c..4b4e2ad0 100644
--- a/src/StardewModdingAPI/Events/ContentEvents.cs
+++ b/src/StardewModdingAPI/Events/ContentEvents.cs
@@ -1,6 +1,4 @@
using System;
-using System.Collections.Generic;
-using System.Diagnostics.CodeAnalysis;
using StardewModdingAPI.Framework;
namespace StardewModdingAPI.Events
@@ -8,21 +6,6 @@ namespace StardewModdingAPI.Events
/// Events raised when the game loads content.
public static class ContentEvents
{
- /*********
- ** Properties
- *********/
- /// Tracks the installed mods.
- private static ModRegistry ModRegistry;
-
- /// Encapsulates monitoring and logging.
- private static IMonitor Monitor;
-
- /// The mods using the experimental API for which a warning has been raised.
- private static readonly HashSet WarnedMods = new HashSet();
-
- /// The backing field for .
- [SuppressMessage("ReSharper", "InconsistentNaming")]
- private static event EventHandler _AfterAssetLoaded;
/*********
** Events
@@ -30,35 +13,10 @@ namespace StardewModdingAPI.Events
/// Raised after the content language changes.
public static event EventHandler> AfterLocaleChanged;
- /// Raised when an XNB file is being read into the cache. Mods can change the data here before it's cached.
-#if EXPERIMENTAL
- public
-#else
- internal
-#endif
- static event EventHandler AfterAssetLoaded
- {
- add
- {
- ContentEvents.RaiseContentExperimentalWarning();
- ContentEvents._AfterAssetLoaded += value;
- }
- remove => ContentEvents._AfterAssetLoaded -= value;
- }
-
/*********
** Internal methods
*********/
- /// Injects types required for backwards compatibility.
- /// Tracks the installed mods.
- /// Encapsulates monitoring and logging.
- internal static void Shim(ModRegistry modRegistry, IMonitor monitor)
- {
- ContentEvents.ModRegistry = modRegistry;
- ContentEvents.Monitor = monitor;
- }
-
/// Raise an event.
/// Encapsulates monitoring and logging.
/// The previous locale.
@@ -67,28 +25,5 @@ namespace StardewModdingAPI.Events
{
monitor.SafelyRaiseGenericEvent($"{nameof(ContentEvents)}.{nameof(ContentEvents.AfterLocaleChanged)}", ContentEvents.AfterLocaleChanged?.GetInvocationList(), null, new EventArgsValueChanged(oldLocale, newLocale));
}
-
- /// Raise an event.
- /// Encapsulates monitoring and logging.
- /// Encapsulates access and changes to content being read from a data file.
- internal static void InvokeAfterAssetLoaded(IMonitor monitor, IContentEventHelper contentHelper)
- {
- monitor.SafelyRaiseGenericEvent($"{nameof(ContentEvents)}.{nameof(ContentEvents.AfterAssetLoaded)}", ContentEvents._AfterAssetLoaded?.GetInvocationList(), null, contentHelper);
- }
-
-
- /*********
- ** Private methods
- *********/
- /// Raise an 'experimental API' warning for a mod using the content API.
- private static void RaiseContentExperimentalWarning()
- {
- string modName = ContentEvents.ModRegistry.GetModFromStack() ?? "An unknown mod";
- if (!ContentEvents.WarnedMods.Contains(modName))
- {
- ContentEvents.WarnedMods.Add(modName);
- ContentEvents.Monitor.Log($"{modName} used the undocumented and experimental content API, which may change or be removed without warning.", LogLevel.Warn);
- }
- }
}
}
diff --git a/src/StardewModdingAPI/Framework/Content/AssetData.cs b/src/StardewModdingAPI/Framework/Content/AssetData.cs
new file mode 100644
index 00000000..1ab9eebd
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/Content/AssetData.cs
@@ -0,0 +1,44 @@
+using System;
+
+namespace StardewModdingAPI.Framework.Content
+{
+ /// Base implementation for a content helper which encapsulates access and changes to content being read from a data file.
+ /// The interface value type.
+ internal class AssetData : AssetInfo, IAssetData
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// The content data being read.
+ public TValue Data { get; protected set; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// Construct an instance.
+ /// The content's locale code, if the content is localised.
+ /// The normalised asset name being read.
+ /// The content data being read.
+ /// Normalises an asset key to match the cache key.
+ public AssetData(string locale, string assetName, TValue data, Func getNormalisedPath)
+ : base(locale, assetName, data.GetType(), getNormalisedPath)
+ {
+ this.Data = data;
+ }
+
+ /// 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.
+ /// The new content value.
+ /// The is null.
+ /// The 's type is not compatible with the loaded asset's type.
+ public void ReplaceWith(TValue value)
+ {
+ if (value == null)
+ throw new ArgumentNullException(nameof(value), "Can't set a loaded asset to a null value.");
+ if (!this.DataType.IsInstanceOfType(value))
+ 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;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs b/src/StardewModdingAPI/Framework/Content/AssetDataForDictionary.cs
similarity index 85%
rename from src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs
rename to src/StardewModdingAPI/Framework/Content/AssetDataForDictionary.cs
index 26f059e4..e9b29b12 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs
+++ b/src/StardewModdingAPI/Framework/Content/AssetDataForDictionary.cs
@@ -5,7 +5,7 @@ using System.Linq;
namespace StardewModdingAPI.Framework.Content
{
/// Encapsulates access and changes to dictionary content being read from a data file.
- internal class ContentEventHelperForDictionary : ContentEventData>, IContentEventHelperForDictionary
+ internal class AssetDataForDictionary : AssetData>, IAssetDataForDictionary
{
/*********
** Public methods
@@ -15,7 +15,7 @@ namespace StardewModdingAPI.Framework.Content
/// The normalised asset name being read.
/// The content data being read.
/// Normalises an asset key to match the cache key.
- public ContentEventHelperForDictionary(string locale, string assetName, IDictionary data, Func getNormalisedPath)
+ public AssetDataForDictionary(string locale, string assetName, IDictionary data, Func getNormalisedPath)
: base(locale, assetName, data, getNormalisedPath) { }
/// Add or replace an entry in the dictionary.
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs b/src/StardewModdingAPI/Framework/Content/AssetDataForImage.cs
similarity index 94%
rename from src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs
rename to src/StardewModdingAPI/Framework/Content/AssetDataForImage.cs
index da30590b..45c5588b 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs
+++ b/src/StardewModdingAPI/Framework/Content/AssetDataForImage.cs
@@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace StardewModdingAPI.Framework.Content
{
/// Encapsulates access and changes to dictionary content being read from a data file.
- internal class ContentEventHelperForImage : ContentEventData, IContentEventHelperForImage
+ internal class AssetDataForImage : AssetData, IAssetDataForImage
{
/*********
** Public methods
@@ -15,7 +15,7 @@ namespace StardewModdingAPI.Framework.Content
/// The normalised asset name being read.
/// The content data being read.
/// Normalises an asset key to match the cache key.
- public ContentEventHelperForImage(string locale, string assetName, Texture2D data, Func getNormalisedPath)
+ public AssetDataForImage(string locale, string assetName, Texture2D data, Func getNormalisedPath)
: base(locale, assetName, data, getNormalisedPath) { }
/// Overwrite part of the image.
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventHelper.cs b/src/StardewModdingAPI/Framework/Content/AssetDataForObject.cs
similarity index 77%
rename from src/StardewModdingAPI/Framework/Content/ContentEventHelper.cs
rename to src/StardewModdingAPI/Framework/Content/AssetDataForObject.cs
index 9bf1ea17..af2f54ae 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelper.cs
+++ b/src/StardewModdingAPI/Framework/Content/AssetDataForObject.cs
@@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace StardewModdingAPI.Framework.Content
{
/// Encapsulates access and changes to content being read from a data file.
- internal class ContentEventHelper : ContentEventData