From 9c53a254d50718fee3b8043bb0b8bb840557e82f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 25 Feb 2017 15:22:45 -0500 Subject: add prototype content event + helper to manipulate XNB data (#173) --- src/StardewModdingAPI/Events/ContentEvents.cs | 69 +++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/StardewModdingAPI/Events/ContentEvents.cs (limited to 'src/StardewModdingAPI/Events') 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 +{ + /// Events raised when the game loads content. + [Obsolete("This is an undocumented experimental API and may change without warning.")] + 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(); + + + /********* + ** Events + *********/ + /// Raised when an XNB file is being read into the cache. Mods can change the data here before it's cached. + public static event EventHandler AssetLoading; + + + /********* + ** 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. + /// Encapsulates access and changes to content being read from a data file. + 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); + } + + /// Get whether there are any listeners. + internal static bool HasAssetLoadingListeners() + { + return ContentEvents.AssetLoading != null; + } + } +} -- cgit