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, IContentEventHelper + internal class AssetDataForObject : AssetData, IAssetData { /********* ** Public methods @@ -15,23 +15,23 @@ 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 ContentEventHelper(string locale, string assetName, object data, Func getNormalisedPath) + public AssetDataForObject(string locale, string assetName, object data, Func getNormalisedPath) : base(locale, assetName, data, getNormalisedPath) { } /// Get a helper to manipulate the data as a dictionary. /// The expected dictionary key. /// The expected dictionary balue. /// The content being read isn't a dictionary. - public IContentEventHelperForDictionary AsDictionary() + public IAssetDataForDictionary AsDictionary() { - return new ContentEventHelperForDictionary(this.Locale, this.AssetName, this.GetData>(), this.GetNormalisedPath); + return new AssetDataForDictionary(this.Locale, this.AssetName, this.GetData>(), this.GetNormalisedPath); } /// Get a helper to manipulate the data as an image. /// The content being read isn't an image. - public IContentEventHelperForImage AsImage() + public IAssetDataForImage AsImage() { - return new ContentEventHelperForImage(this.Locale, this.AssetName, this.GetData(), this.GetNormalisedPath); + return new AssetDataForImage(this.Locale, this.AssetName, this.GetData(), this.GetNormalisedPath); } /// Get the data as a given type. diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventData.cs b/src/StardewModdingAPI/Framework/Content/AssetInfo.cs similarity index 53% rename from src/StardewModdingAPI/Framework/Content/ContentEventData.cs rename to src/StardewModdingAPI/Framework/Content/AssetInfo.cs index 1a1779d4..08bc3a03 100644 --- a/src/StardewModdingAPI/Framework/Content/ContentEventData.cs +++ b/src/StardewModdingAPI/Framework/Content/AssetInfo.cs @@ -4,9 +4,7 @@ using Microsoft.Xna.Framework.Graphics; 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 ContentEventData : EventArgs, IContentEventData + internal class AssetInfo : IAssetInfo { /********* ** Properties @@ -24,9 +22,6 @@ namespace StardewModdingAPI.Framework.Content /// The normalised asset name being read. The format may change between platforms; see to compare with a known path. public string AssetName { get; } - /// The content data being read. - public TValue Data { get; protected set; } - /// The content data type. public Type DataType { get; } @@ -37,23 +32,13 @@ namespace StardewModdingAPI.Framework.Content /// Construct an instance. /// The content's locale code, if the content is localised. /// The normalised asset name being read. - /// The content data being read. + /// The content type being read. /// Normalises an asset key to match the cache key. - public ContentEventData(string locale, string assetName, TValue data, Func getNormalisedPath) - : this(locale, assetName, data, data.GetType(), getNormalisedPath) { } - - /// Construct an instance. - /// The content's locale code, if the content is localised. - /// The normalised asset name being read. - /// The content data being read. - /// The content data type being read. - /// Normalises an asset key to match the cache key. - public ContentEventData(string locale, string assetName, TValue data, Type dataType, Func getNormalisedPath) + public AssetInfo(string locale, string assetName, Type type, Func getNormalisedPath) { this.Locale = locale; this.AssetName = assetName; - this.Data = data; - this.DataType = dataType; + this.DataType = type; this.GetNormalisedPath = getNormalisedPath; } @@ -65,20 +50,6 @@ namespace StardewModdingAPI.Framework.Content return this.AssetName.Equals(path, StringComparison.InvariantCultureIgnoreCase); } - /// 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; - } - /********* ** Protected methods diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs index 7fd5e803..f4b541e9 100644 --- a/src/StardewModdingAPI/Framework/ContentHelper.cs +++ b/src/StardewModdingAPI/Framework/ContentHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; @@ -32,6 +33,13 @@ namespace StardewModdingAPI.Framework private readonly string ModName; + /********* + ** Accessors + *********/ + /// Editors which change content assets after they're loaded. + internal IList AssetEditors { get; } = new List(); + + /********* ** Public methods *********/ diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index acd3e108..38457862 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -3,11 +3,9 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; -using System.Threading; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using StardewModdingAPI.AssemblyRewriters; -using StardewModdingAPI.Events; using StardewModdingAPI.Framework.Content; using StardewModdingAPI.Framework.Reflection; using StardewValley; @@ -42,6 +40,9 @@ namespace StardewModdingAPI.Framework /********* ** Accessors *********/ + /// Implementations which change assets after they're loaded. + internal IDictionary> Editors { get; } = new Dictionary>(); + /// The absolute path to the . public string FullRootDirectory => Path.Combine(Constants.ExecutionPath, this.RootDirectory); @@ -49,13 +50,6 @@ namespace StardewModdingAPI.Framework /********* ** Public methods *********/ - /// Construct an instance. - /// The service provider to use to locate services. - /// The root directory to search for content. - /// Encapsulates monitoring and logging. - public SContentManager(IServiceProvider serviceProvider, string rootDirectory, IMonitor monitor) - : this(serviceProvider, rootDirectory, Thread.CurrentThread.CurrentUICulture, null, monitor) { } - /// Construct an instance. /// The service provider to use to locate services. /// The root directory to search for content. @@ -66,8 +60,8 @@ namespace StardewModdingAPI.Framework : base(serviceProvider, rootDirectory, currentCulture, languageCodeOverride) { // initialise - this.Monitor = monitor; IReflectionHelper reflection = new ReflectionHelper(); + this.Monitor = monitor; // get underlying fields for interception this.Cache = reflection.GetPrivateField>(this, "loadedAssets").GetValue(); @@ -125,14 +119,20 @@ namespace StardewModdingAPI.Framework if (this.IsNormalisedKeyLoaded(assetName)) return base.Load(assetName); - // load data - T data = base.Load(assetName); - // let mods intercept content - IContentEventHelper helper = new ContentEventHelper(cacheLocale, assetName, data, this.NormaliseAssetName); - ContentEvents.InvokeAfterAssetLoaded(this.Monitor, helper); - this.Cache[assetName] = helper.Data; - return (T)helper.Data; + IAssetInfo info = new AssetInfo(cacheLocale, assetName, typeof(T), this.NormaliseAssetName); + Lazy data = new Lazy(() => new AssetDataForObject(info.Locale, info.AssetName, base.Load(assetName), this.NormaliseAssetName)); + if (this.TryOverrideAssetLoad(info, data, out T result)) + { + if (result == null) + throw new InvalidCastException($"Can't override asset '{assetName}' with a null value."); + + this.Cache[assetName] = result; + return result; + } + + // fallback to default behavior + return base.Load(assetName); } /// Inject an asset into the cache. @@ -171,5 +171,35 @@ namespace StardewModdingAPI.Framework ? locale : null; } + + /// Try to override an asset being loaded. + /// The asset type. + /// The asset metadata. + /// The loaded asset data. + /// The asset to use instead. + /// Returns whether the asset should be overridden by . + private bool TryOverrideAssetLoad(IAssetInfo info, Lazy data, out T result) + { + bool edited = false; + + // apply editors + foreach (var modEditors in this.Editors) + { + IModMetadata mod = modEditors.Key; + foreach (IAssetEditor editor in modEditors.Value) + { + if (!editor.CanEdit(info)) + continue; + + this.Monitor.Log($"{mod.DisplayName} intercepted {info.AssetName}.", LogLevel.Trace); + editor.Edit(data.Value); + edited = true; + } + } + + // return result + result = edited ? (T)data.Value.Data : default(T); + return edited; + } } } diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index 602a522b..e4c2a233 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -29,6 +30,9 @@ namespace StardewModdingAPI.Framework /**** ** SMAPI state ****/ + /// Encapsulates monitoring and logging. + private readonly IMonitor Monitor; + /// The maximum number of consecutive attempts SMAPI should make to recover from a draw error. private readonly Countdown DrawCrashTimer = new Countdown(60); // 60 ticks = roughly one second @@ -48,8 +52,6 @@ namespace StardewModdingAPI.Framework /// Whether the game's zoom level is at 100% (i.e. nothing should be scaled). public bool ZoomLevelIsOne => Game1.options.zoomLevel.Equals(1.0f); - /// Encapsulates monitoring and logging. - private readonly IMonitor Monitor; /**** ** Game state @@ -189,7 +191,7 @@ namespace StardewModdingAPI.Framework // 2. Since Game1.content isn't initialised yet, and we need one main instance to // support custom map tilesheets, detect when Game1.content is being initialised // and use the same instance. - this.Content = new SContentManager(this.Content.ServiceProvider, this.Content.RootDirectory, this.Monitor); + this.Content = new SContentManager(this.Content.ServiceProvider, this.Content.RootDirectory, Thread.CurrentThread.CurrentUICulture, null, this.Monitor); } /**** @@ -206,7 +208,7 @@ namespace StardewModdingAPI.Framework return mainContentManager; // build new instance - return new SContentManager(this.Content.ServiceProvider, this.Content.RootDirectory, this.Monitor); + return new SContentManager(this.Content.ServiceProvider, this.Content.RootDirectory, Thread.CurrentThread.CurrentUICulture, null, this.Monitor); } /// The method called when the game is updating its state. This happens roughly 60 times per second. diff --git a/src/StardewModdingAPI/IAssetData.cs b/src/StardewModdingAPI/IAssetData.cs new file mode 100644 index 00000000..c3021144 --- /dev/null +++ b/src/StardewModdingAPI/IAssetData.cs @@ -0,0 +1,47 @@ +using System; + +namespace StardewModdingAPI +{ + /// Generic metadata and methods for a content asset being loaded. + /// The expected data type. + public interface IAssetData : IAssetInfo + { + /********* + ** Accessors + *********/ + /// The content data being read. + TValue Data { get; } + + + /********* + ** Public methods + *********/ + /// 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. + void ReplaceWith(TValue value); + } + + /// Generic metadata and methods for a content asset being loaded. + public interface IAssetData : IAssetData + { + /********* + ** Public methods + *********/ + /// Get a helper to manipulate the data as a dictionary. + /// The expected dictionary key. + /// The expected dictionary value. + /// The content being read isn't a dictionary. + IAssetDataForDictionary AsDictionary(); + + /// Get a helper to manipulate the data as an image. + /// The content being read isn't an image. + IAssetDataForImage AsImage(); + + /// Get the data as a given type. + /// The expected data type. + /// The data can't be converted to . + TData GetData(); + } +} diff --git a/src/StardewModdingAPI/IContentEventHelperForDictionary.cs b/src/StardewModdingAPI/IAssetDataForDictionary.cs similarity index 90% rename from src/StardewModdingAPI/IContentEventHelperForDictionary.cs rename to src/StardewModdingAPI/IAssetDataForDictionary.cs index 2f9d5a65..53c24346 100644 --- a/src/StardewModdingAPI/IContentEventHelperForDictionary.cs +++ b/src/StardewModdingAPI/IAssetDataForDictionary.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace StardewModdingAPI { /// Encapsulates access and changes to dictionary content being read from a data file. - public interface IContentEventHelperForDictionary : IContentEventData> + public interface IAssetDataForDictionary : IAssetData> { /********* ** Public methods diff --git a/src/StardewModdingAPI/IContentEventHelperForImage.cs b/src/StardewModdingAPI/IAssetDataForImage.cs similarity index 94% rename from src/StardewModdingAPI/IContentEventHelperForImage.cs rename to src/StardewModdingAPI/IAssetDataForImage.cs index 1158c868..4584a20e 100644 --- a/src/StardewModdingAPI/IContentEventHelperForImage.cs +++ b/src/StardewModdingAPI/IAssetDataForImage.cs @@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics; namespace StardewModdingAPI { /// Encapsulates access and changes to dictionary content being read from a data file. - public interface IContentEventHelperForImage : IContentEventData + public interface IAssetDataForImage : IAssetData { /********* ** Public methods diff --git a/src/StardewModdingAPI/IAssetEditor.cs b/src/StardewModdingAPI/IAssetEditor.cs new file mode 100644 index 00000000..b66ec15e --- /dev/null +++ b/src/StardewModdingAPI/IAssetEditor.cs @@ -0,0 +1,17 @@ +namespace StardewModdingAPI +{ + /// Edits a loaded content asset. + public interface IAssetEditor + { + /********* + ** Public methods + *********/ + /// Get whether this instance can edit the given asset. + /// Basic metadata about the asset being loaded. + bool CanEdit(IAssetInfo asset); + + /// Edit a matched asset. + /// A helper which encapsulates metadata about an asset and enables changes to it. + void Edit(IAssetData asset); + } +} diff --git a/src/StardewModdingAPI/IContentEventData.cs b/src/StardewModdingAPI/IAssetInfo.cs similarity index 51% rename from src/StardewModdingAPI/IContentEventData.cs rename to src/StardewModdingAPI/IAssetInfo.cs index 7e2d4df1..dc65a750 100644 --- a/src/StardewModdingAPI/IContentEventData.cs +++ b/src/StardewModdingAPI/IAssetInfo.cs @@ -1,10 +1,9 @@ -using System; +using System; namespace StardewModdingAPI { - /// Generic metadata and methods for a content asset being loaded. - /// The expected data type. - public interface IContentEventData + /// Basic metadata for a content asset. + public interface IAssetInfo { /********* ** Accessors @@ -15,9 +14,6 @@ namespace StardewModdingAPI /// The normalised asset name being read. The format may change between platforms; see to compare with a known path. string AssetName { get; } - /// The content data being read. - TValue Data { get; } - /// The content data type. Type DataType { get; } @@ -28,11 +24,5 @@ namespace StardewModdingAPI /// Get whether the asset name being loaded matches a given name after normalisation. /// The expected asset path, relative to the game's content folder and without the .xnb extension or locale suffix (like 'Data\ObjectInformation'). bool IsAssetName(string path); - - /// 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. - void ReplaceWith(TValue value); } } diff --git a/src/StardewModdingAPI/IContentEventHelper.cs b/src/StardewModdingAPI/IContentEventHelper.cs deleted file mode 100644 index 421a1e06..00000000 --- a/src/StardewModdingAPI/IContentEventHelper.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; - -namespace StardewModdingAPI -{ - /// Encapsulates access and changes to content being read from a data file. - public interface IContentEventHelper : IContentEventData - { - /********* - ** Public methods - *********/ - /// Get a helper to manipulate the data as a dictionary. - /// The expected dictionary key. - /// The expected dictionary balue. - /// The content being read isn't a dictionary. - IContentEventHelperForDictionary AsDictionary(); - - /// Get a helper to manipulate the data as an image. - /// The content being read isn't an image. - IContentEventHelperForImage AsImage(); - - /// Get the data as a given type. - /// The expected data type. - /// The data can't be converted to . - TData GetData(); - } -} diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 7b843748..4fbd35dc 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -330,7 +330,6 @@ namespace StardewModdingAPI Config.Shim(this.DeprecationManager); Log.Shim(this.DeprecationManager, this.GetSecondaryMonitor("legacy mod"), this.ModRegistry); Mod.Shim(this.DeprecationManager); - ContentEvents.Shim(this.ModRegistry, this.Monitor); GameEvents.Shim(this.DeprecationManager); PlayerEvents.Shim(this.DeprecationManager); TimeEvents.Shim(this.DeprecationManager); @@ -489,7 +488,8 @@ namespace StardewModdingAPI this.Monitor.Log("Detecting common issues...", LogLevel.Trace); bool issuesFound = false; - // object format (commonly broken by outdated mods) + + // object format (commonly broken by outdated files) { // detect issues bool hasObjectIssues = false; @@ -689,11 +689,14 @@ namespace StardewModdingAPI // initialise loaded mods foreach (IModMetadata metadata in this.ModRegistry.GetMods()) { + // add interceptors + if (metadata.Mod.Helper.Content is ContentHelper helper) + this.ContentManager.Editors[metadata] = helper.AssetEditors; + + // call entry method try { IMod mod = metadata.Mod; - - // call entry methods (mod as Mod)?.Entry(); // deprecated since 1.0 mod.Entry(mod.Helper); diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 77d3b12b..1f2bd4bb 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -97,6 +97,7 @@ + @@ -136,10 +137,10 @@ - - - - + + + + @@ -156,11 +157,12 @@ + + - - - - + + +