summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Events
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-03-23 01:06:11 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-03-23 01:06:11 -0400
commit584725bb8e554e314843315facca1fd15868bee4 (patch)
treefdb4ecc4a9f190e7e4018fc3334ad8a4bec53e6d /src/SMAPI/Framework/Events
parentb07d2340a9a6da22ee0fd95f2c6ccca3939cb7ab (diff)
downloadSMAPI-584725bb8e554e314843315facca1fd15868bee4.tar.gz
SMAPI-584725bb8e554e314843315facca1fd15868bee4.tar.bz2
SMAPI-584725bb8e554e314843315facca1fd15868bee4.zip
add initial AssetRequested content event (#766)
Diffstat (limited to 'src/SMAPI/Framework/Events')
-rw-r--r--src/SMAPI/Framework/Events/EventManager.cs11
-rw-r--r--src/SMAPI/Framework/Events/ManagedEvent.cs10
-rw-r--r--src/SMAPI/Framework/Events/ModContentEvents.cs29
-rw-r--r--src/SMAPI/Framework/Events/ModEvents.cs4
4 files changed, 52 insertions, 2 deletions
diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs
index fa4d564d..8142f00e 100644
--- a/src/SMAPI/Framework/Events/EventManager.cs
+++ b/src/SMAPI/Framework/Events/EventManager.cs
@@ -11,6 +11,13 @@ namespace StardewModdingAPI.Framework.Events
** Events
*********/
/****
+ ** Content
+ ****/
+ /// <inheritdoc cref="IContentEvents.AssetRequested" />
+ public readonly ManagedEvent<AssetRequestedEventArgs> AssetRequested;
+
+
+ /****
** Display
****/
/// <inheritdoc cref="IDisplayEvents.MenuChanged" />
@@ -189,7 +196,9 @@ namespace StardewModdingAPI.Framework.Events
return new ManagedEvent<TEventArgs>($"{typeName}.{eventName}", modRegistry, isPerformanceCritical);
}
- // init events (new)
+ // init events
+ this.AssetRequested = ManageEventOf<AssetRequestedEventArgs>(nameof(IModEvents.Content), nameof(IContentEvents.AssetRequested));
+
this.MenuChanged = ManageEventOf<MenuChangedEventArgs>(nameof(IModEvents.Display), nameof(IDisplayEvents.MenuChanged));
this.Rendering = ManageEventOf<RenderingEventArgs>(nameof(IModEvents.Display), nameof(IDisplayEvents.Rendering), isPerformanceCritical: true);
this.Rendered = ManageEventOf<RenderedEventArgs>(nameof(IModEvents.Display), nameof(IDisplayEvents.Rendered), isPerformanceCritical: true);
diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs
index a200393d..154ef659 100644
--- a/src/SMAPI/Framework/Events/ManagedEvent.cs
+++ b/src/SMAPI/Framework/Events/ManagedEvent.cs
@@ -100,6 +100,14 @@ namespace StardewModdingAPI.Framework.Events
/// <param name="match">A lambda which returns true if the event should be raised for the given mod.</param>
public void Raise(TEventArgs args, Func<IModMetadata, bool> match = null)
{
+ this.Raise((_, invoke) => invoke(args), match);
+ }
+
+ /// <summary>Raise the event and notify all handlers.</summary>
+ /// <param name="invoke">Invoke an event handler. This receives the mod which registered the handler, and should invoke the callback with the event arguments to pass it.</param>
+ /// <param name="match">A lambda which returns true if the event should be raised for the given mod.</param>
+ public void Raise(Action<IModMetadata, Action<TEventArgs>> invoke, Func<IModMetadata, bool> match = null)
+ {
// skip if no handlers
if (this.Handlers.Count == 0)
return;
@@ -128,7 +136,7 @@ namespace StardewModdingAPI.Framework.Events
try
{
- handler.Handler.Invoke(null, args);
+ invoke(handler.SourceMod, args => handler.Handler.Invoke(null, args));
}
catch (Exception ex)
{
diff --git a/src/SMAPI/Framework/Events/ModContentEvents.cs b/src/SMAPI/Framework/Events/ModContentEvents.cs
new file mode 100644
index 00000000..b4d4279c
--- /dev/null
+++ b/src/SMAPI/Framework/Events/ModContentEvents.cs
@@ -0,0 +1,29 @@
+using System;
+using StardewModdingAPI.Events;
+
+namespace StardewModdingAPI.Framework.Events
+{
+ /// <inheritdoc cref="IContentEvents" />
+ internal class ModContentEvents : ModEventsBase, IContentEvents
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <inheritdoc />
+ public event EventHandler<AssetRequestedEventArgs> AssetRequested
+ {
+ add => this.EventManager.AssetRequested.Add(value, this.Mod);
+ remove => this.EventManager.AssetRequested.Remove(value);
+ }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="mod">The mod which uses this instance.</param>
+ /// <param name="eventManager">The underlying event manager.</param>
+ internal ModContentEvents(IModMetadata mod, EventManager eventManager)
+ : base(mod, eventManager) { }
+ }
+}
diff --git a/src/SMAPI/Framework/Events/ModEvents.cs b/src/SMAPI/Framework/Events/ModEvents.cs
index 0c365d42..1fb3482c 100644
--- a/src/SMAPI/Framework/Events/ModEvents.cs
+++ b/src/SMAPI/Framework/Events/ModEvents.cs
@@ -9,6 +9,9 @@ namespace StardewModdingAPI.Framework.Events
** Accessors
*********/
/// <inheritdoc />
+ public IContentEvents Content { get; }
+
+ /// <inheritdoc />
public IDisplayEvents Display { get; }
/// <inheritdoc />
@@ -38,6 +41,7 @@ namespace StardewModdingAPI.Framework.Events
/// <param name="eventManager">The underlying event manager.</param>
public ModEvents(IModMetadata mod, EventManager eventManager)
{
+ this.Content = new ModContentEvents(mod, eventManager);
this.Display = new ModDisplayEvents(mod, eventManager);
this.GameLoop = new ModGameLoopEvents(mod, eventManager);
this.Input = new ModInputEvents(mod, eventManager);