summaryrefslogtreecommitdiff
path: root/src/SMAPI/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/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/Events')
-rw-r--r--src/SMAPI/Events/AssetRequestedEventArgs.cs94
-rw-r--r--src/SMAPI/Events/IContentEvents.cs17
-rw-r--r--src/SMAPI/Events/IModEvents.cs3
3 files changed, 114 insertions, 0 deletions
diff --git a/src/SMAPI/Events/AssetRequestedEventArgs.cs b/src/SMAPI/Events/AssetRequestedEventArgs.cs
new file mode 100644
index 00000000..b17250b0
--- /dev/null
+++ b/src/SMAPI/Events/AssetRequestedEventArgs.cs
@@ -0,0 +1,94 @@
+using System;
+using System.Collections.Generic;
+using Microsoft.Xna.Framework.Graphics;
+using StardewModdingAPI.Framework;
+using StardewModdingAPI.Framework.Content;
+using xTile;
+
+namespace StardewModdingAPI.Events
+{
+ /// <summary>Event arguments for an <see cref="IContentEvents.AssetRequested"/> event.</summary>
+ public class AssetRequestedEventArgs : EventArgs
+ {
+ /*********
+ ** Fields
+ *********/
+ /// <summary>The mod handling the event.</summary>
+ private readonly IModMetadata Mod;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The name of the asset being requested.</summary>
+ public IAssetName Name { get; }
+
+ /// <summary>The load operations requested by the event handler.</summary>
+ internal IList<AssetLoadOperation> LoadOperations { get; } = new List<AssetLoadOperation>();
+
+ /// <summary>The edit operations requested by the event handler.</summary>
+ internal IList<AssetEditOperation> EditOperations { get; } = new List<AssetEditOperation>();
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="mod">The mod handling the event.</param>
+ /// <param name="name">The name of the asset being requested.</param>
+ internal AssetRequestedEventArgs(IModMetadata mod, IAssetName name)
+ {
+ this.Mod = mod;
+ this.Name = name;
+ }
+
+ /// <summary>Provide the initial instance for the asset, instead of trying to load it from the game's <c>Content</c> folder.</summary>
+ /// <param name="load">Get the initial instance of an asset.</param>
+ /// <remarks>
+ /// Usage notes:
+ /// <list type="bullet">
+ /// <item>The asset doesn't need to exist in the game's <c>Content</c> folder. If any mod loads the asset, the game will see it as an existing asset as if it was in that folder.</item>
+ /// <item>Each asset can logically only have one initial instance. If multiple loads apply at the same time, SMAPI will raise an error and ignore all of them. If you're making changes to the existing asset instead of replacing it, you should use <see cref="Edit"/> instead to avoid those limitations and improve mod compatibility.</item>
+ /// </list>
+ /// </remarks>
+ public void LoadFrom(Func<object> load)
+ {
+ this.LoadOperations.Add(
+ new AssetLoadOperation(this.Mod, _ => load())
+ );
+ }
+
+ /// <summary>Provide the initial instance for the asset from a file in your mod folder, instead of trying to load it from the game's <c>Content</c> folder.</summary>
+ /// <typeparam name="TAsset">The expected data type. The main supported types are <see cref="Map"/>, <see cref="Texture2D"/>, dictionaries, and lists; other types may be supported by the game's content pipeline.</typeparam>
+ /// <param name="relativePath">The relative path to the file in your mod folder.</param>
+ /// <remarks>
+ /// Usage notes:
+ /// <list type="bullet">
+ /// <item>The asset doesn't need to exist in the game's <c>Content</c> folder. If any mod loads the asset, the game will see it as an existing asset as if it was in that folder.</item>
+ /// <item>Each asset can logically only have one initial instance. If multiple loads apply at the same time, SMAPI will raise an error and ignore all of them. If you're making changes to the existing asset instead of replacing it, you should use <see cref="Edit"/> instead to avoid those limitations and improve mod compatibility.</item>
+ /// </list>
+ /// </remarks>
+ public void LoadFromModFile<TAsset>(string relativePath)
+ {
+ this.LoadOperations.Add(
+ new AssetLoadOperation(this.Mod, _ => this.Mod.Mod.Helper.Content.Load<TAsset>(relativePath))
+ );
+ }
+
+ /// <summary>Edit the asset after it's loaded.</summary>
+ /// <param name="apply">Apply changes to the asset.</param>
+ /// <remarks>
+ /// Usage notes:
+ /// <list type="bullet">
+ /// <item>Editing an asset which doesn't exist has no effect. This is applied after the asset is loaded from the game's <c>Content</c> folder, or from any mod's <see cref="LoadFrom"/> or <see cref="LoadFromModFile{TAsset}"/>.</item>
+ /// <item>You can apply any number of edits to the asset. Each edit will be applied on top of the previous one (i.e. it'll see the merged asset from all previous edits as its input).</item>
+ /// </list>
+ /// </remarks>
+ public void Edit(Action<IAssetData> apply)
+ {
+ this.EditOperations.Add(
+ new AssetEditOperation(this.Mod, apply)
+ );
+ }
+ }
+}
diff --git a/src/SMAPI/Events/IContentEvents.cs b/src/SMAPI/Events/IContentEvents.cs
new file mode 100644
index 00000000..feaf9c0a
--- /dev/null
+++ b/src/SMAPI/Events/IContentEvents.cs
@@ -0,0 +1,17 @@
+using System;
+using StardewValley;
+
+namespace StardewModdingAPI.Events
+{
+ /// <summary>Events related to assets loaded from the content pipeline (including data, maps, and textures).</summary>
+ public interface IContentEvents
+ {
+ /// <summary>Raised when an asset is being requested from the content pipeline.</summary>
+ /// <remarks>
+ /// The asset isn't necessarily being loaded yet (e.g. the game may be checking if it exists). Mods can register the changes they want to apply using methods on the <paramref name="e"/> parameter. These will be applied when the asset is actually loaded.
+ ///
+ /// If the asset is requested multiple times in the same tick (e.g. once to check if it exists and once to load it), SMAPI might only raise the event once and reuse the cached result.
+ /// </remarks>
+ event EventHandler<AssetRequestedEventArgs> AssetRequested;
+ }
+}
diff --git a/src/SMAPI/Events/IModEvents.cs b/src/SMAPI/Events/IModEvents.cs
index 1f892b31..2603961b 100644
--- a/src/SMAPI/Events/IModEvents.cs
+++ b/src/SMAPI/Events/IModEvents.cs
@@ -3,6 +3,9 @@ namespace StardewModdingAPI.Events
/// <summary>Manages access to events raised by SMAPI.</summary>
public interface IModEvents
{
+ /// <summary>Events related to assets loaded from the content pipeline (including data, maps, and textures).</summary>
+ IContentEvents Content { get; }
+
/// <summary>Events related to UI and drawing to the screen.</summary>
IDisplayEvents Display { get; }