diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-25 15:22:45 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-25 15:22:45 -0500 |
commit | 9c53a254d50718fee3b8043bb0b8bb840557e82f (patch) | |
tree | 7308d7a4b5c3fb80f60614c935b968f848b240f4 /src/StardewModdingAPI/Events | |
parent | 2151625898fcad388a29c17f92de4bf26c2c091d (diff) | |
download | SMAPI-9c53a254d50718fee3b8043bb0b8bb840557e82f.tar.gz SMAPI-9c53a254d50718fee3b8043bb0b8bb840557e82f.tar.bz2 SMAPI-9c53a254d50718fee3b8043bb0b8bb840557e82f.zip |
add prototype content event + helper to manipulate XNB data (#173)
Diffstat (limited to 'src/StardewModdingAPI/Events')
-rw-r--r-- | src/StardewModdingAPI/Events/ContentEvents.cs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Events/ContentEvents.cs b/src/StardewModdingAPI/Events/ContentEvents.cs new file mode 100644 index 00000000..cc07f242 --- /dev/null +++ b/src/StardewModdingAPI/Events/ContentEvents.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using StardewModdingAPI.Framework; + +namespace StardewModdingAPI.Events +{ + /// <summary>Events raised when the game loads content.</summary> + [Obsolete("This is an undocumented experimental API and may change without warning.")] + public static class ContentEvents + { + /********* + ** Properties + *********/ + /// <summary>Tracks the installed mods.</summary> + private static ModRegistry ModRegistry; + + /// <summary>Encapsulates monitoring and logging.</summary> + private static IMonitor Monitor; + + /// <summary>The mods using the experimental API for which a warning has been raised.</summary> + private static readonly HashSet<string> WarnedMods = new HashSet<string>(); + + + /********* + ** Events + *********/ + /// <summary>Raised when an XNB file is being read into the cache. Mods can change the data here before it's cached.</summary> + public static event EventHandler<IContentEventHelper> AssetLoading; + + + /********* + ** Internal methods + *********/ + /// <summary>Injects types required for backwards compatibility.</summary> + /// <param name="modRegistry">Tracks the installed mods.</param> + /// <param name="monitor">Encapsulates monitoring and logging.</param> + internal static void Shim(ModRegistry modRegistry, IMonitor monitor) + { + ContentEvents.ModRegistry = modRegistry; + ContentEvents.Monitor = monitor; + } + + /// <summary>Raise an <see cref="AssetLoading"/> event.</summary> + /// <param name="monitor">Encapsulates monitoring and logging.</param> + /// <param name="contentHelper">Encapsulates access and changes to content being read from a data file.</param> + internal static void InvokeAssetLoading(IMonitor monitor, IContentEventHelper contentHelper) + { + // raise warning about experimental API + foreach (Delegate handler in ContentEvents.AssetLoading.GetInvocationList()) + { + string modName = ContentEvents.ModRegistry.GetModFrom(handler) ?? "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); + } + } + + // raise event + monitor.SafelyRaiseGenericEvent($"{nameof(ContentEvents)}.{nameof(ContentEvents.AssetLoading)}", ContentEvents.AssetLoading?.GetInvocationList(), null, contentHelper); + } + + /// <summary>Get whether there are any <see cref="AssetLoading"/> listeners.</summary> + internal static bool HasAssetLoadingListeners() + { + return ContentEvents.AssetLoading != null; + } + } +} |