From 363f5aeef2dac07299d8d37229fd205a8dff7a61 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 10 Mar 2017 20:48:54 -0500 Subject: rename content event for consistency, simplify usage (#173) --- src/StardewModdingAPI/Events/ContentEvents.cs | 34 +++++++++++++--------- src/StardewModdingAPI/Framework/SContentManager.cs | 26 +++++++++++------ 2 files changed, 37 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Events/ContentEvents.cs b/src/StardewModdingAPI/Events/ContentEvents.cs index 558fc070..f19a33e2 100644 --- a/src/StardewModdingAPI/Events/ContentEvents.cs +++ b/src/StardewModdingAPI/Events/ContentEvents.cs @@ -28,7 +28,7 @@ namespace StardewModdingAPI.Events 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. - public static event EventHandler AssetLoading; + public static event EventHandler AfterAssetLoaded; /********* @@ -52,13 +52,28 @@ namespace StardewModdingAPI.Events monitor.SafelyRaiseGenericEvent($"{nameof(ContentEvents)}.{nameof(ContentEvents.AfterLocaleChanged)}", ContentEvents.AfterLocaleChanged?.GetInvocationList(), null, new EventArgsValueChanged(oldLocale, newLocale)); } - /// Raise an event. + /// 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) + internal static void InvokeAfterAssetLoaded(IMonitor monitor, IContentEventHelper contentHelper) { - // raise warning about experimental API - foreach (Delegate handler in ContentEvents.AssetLoading.GetInvocationList()) + if (ContentEvents.AfterAssetLoaded != null) + { + Delegate[] handlers = ContentEvents.AfterAssetLoaded.GetInvocationList(); + ContentEvents.RaiseDeprecationWarning(handlers); + monitor.SafelyRaiseGenericEvent($"{nameof(ContentEvents)}.{nameof(ContentEvents.AfterAssetLoaded)}", handlers, null, contentHelper); + } + } + + + /********* + ** Private methods + *********/ + /// Raise a 'experimental API' warning for each mod using the content API. + /// The event handlers. + private static void RaiseDeprecationWarning(Delegate[] handlers) + { + foreach (Delegate handler in handlers) { string modName = ContentEvents.ModRegistry.GetModFrom(handler) ?? "An unknown mod"; if (!ContentEvents.WarnedMods.Contains(modName)) @@ -67,15 +82,6 @@ namespace StardewModdingAPI.Events 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; } } } diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index 97472d53..e4b9d74e 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -80,18 +80,20 @@ namespace StardewModdingAPI.Framework /// The asset path relative to the loader root directory, not including the .xnb extension. public override T Load(string assetName) { - // normalise asset name so can override the cache value later + // get normalised metadata assetName = this.NormaliseAssetName(assetName); + string cacheLocale = this.GetCacheLocale(assetName); - // skip if no event handlers or already loaded - if (!ContentEvents.HasAssetLoadingListeners() || this.Cache.ContainsKey(assetName)) + // skip if already loaded + if (this.IsLoaded(assetName)) return base.Load(assetName); - // intercept load + // load data T data = base.Load(assetName); - string cacheLocale = this.GetCacheLocale(assetName, this.Cache); + + // let mods intercept content IContentEventHelper helper = new ContentEventHelper(cacheLocale, assetName, data, this.NormaliseAssetName); - ContentEvents.InvokeAssetLoading(this.Monitor, helper); + ContentEvents.InvokeAfterAssetLoaded(this.Monitor, helper); this.Cache[assetName] = helper.Data; return (T)helper.Data; } @@ -112,13 +114,19 @@ namespace StardewModdingAPI.Framework return this.NormaliseAssetNameForPlatform(assetName); } + /// Get whether an asset has already been loaded. + /// The normalised asset name. + private bool IsLoaded(string normalisedAssetName) + { + return this.Cache.ContainsKey(normalisedAssetName); + } + /// Get the locale for which the asset name was saved, if any. /// The normalised asset name. - /// The cache to search. - private string GetCacheLocale(string normalisedAssetName, IDictionary cache) + private string GetCacheLocale(string normalisedAssetName) { string locale = this.GetKeyLocale.Invoke(); - return this.Cache.ContainsKey($"{normalisedAssetName}.{this.GetKeyLocale.Invoke()}") + return this.Cache.ContainsKey($"{normalisedAssetName}.{locale}") ? locale : null; } -- cgit