summaryrefslogtreecommitdiff
path: root/src/SMAPI/Events/AssetRequestedEventArgs.cs
blob: b17250b0641a237b7b2e6565ceb2659eb1afaf77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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)
            );
        }
    }
}