summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r--src/StardewModdingAPI/Events/ContentEvents.cs34
-rw-r--r--src/StardewModdingAPI/Framework/SContentManager.cs26
2 files changed, 37 insertions, 23 deletions
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<EventArgsValueChanged<string>> AfterLocaleChanged;
/// <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;
+ public static event EventHandler<IContentEventHelper> AfterAssetLoaded;
/*********
@@ -52,13 +52,28 @@ namespace StardewModdingAPI.Events
monitor.SafelyRaiseGenericEvent($"{nameof(ContentEvents)}.{nameof(ContentEvents.AfterLocaleChanged)}", ContentEvents.AfterLocaleChanged?.GetInvocationList(), null, new EventArgsValueChanged<string>(oldLocale, newLocale));
}
- /// <summary>Raise an <see cref="AssetLoading"/> event.</summary>
+ /// <summary>Raise an <see cref="AfterAssetLoaded"/> 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)
+ 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
+ *********/
+ /// <summary>Raise a 'experimental API' warning for each mod using the content API.</summary>
+ /// <param name="handlers">The event handlers.</param>
+ 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);
- }
-
- /// <summary>Get whether there are any <see cref="AssetLoading"/> listeners.</summary>
- 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
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
public override T Load<T>(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<T>(assetName);
- // intercept load
+ // load data
T data = base.Load<T>(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);
}
+ /// <summary>Get whether an asset has already been loaded.</summary>
+ /// <param name="normalisedAssetName">The normalised asset name.</param>
+ private bool IsLoaded(string normalisedAssetName)
+ {
+ return this.Cache.ContainsKey(normalisedAssetName);
+ }
+
/// <summary>Get the locale for which the asset name was saved, if any.</summary>
/// <param name="normalisedAssetName">The normalised asset name.</param>
- /// <param name="cache">The cache to search.</param>
- private string GetCacheLocale(string normalisedAssetName, IDictionary<string, object> cache)
+ private string GetCacheLocale(string normalisedAssetName)
{
string locale = this.GetKeyLocale.Invoke<string>();
- return this.Cache.ContainsKey($"{normalisedAssetName}.{this.GetKeyLocale.Invoke<string>()}")
+ return this.Cache.ContainsKey($"{normalisedAssetName}.{locale}")
? locale
: null;
}