using System; using System.Collections.Generic; using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.Content; using xTile; namespace StardewModdingAPI.Events { /// Event arguments for an event. public class AssetRequestedEventArgs : EventArgs { /********* ** Fields *********/ /// The mod handling the event. private IModMetadata? Mod; /// Get the mod metadata for a content pack, if it's a valid content pack for the mod. private readonly Func GetOnBehalfOf; /// The asset info being requested. private readonly IAssetInfo AssetInfo; /********* ** Accessors *********/ /// The name of the asset being requested. public IAssetName Name => this.AssetInfo.Name; /// The with any locale codes stripped. /// For example, if contains a locale like Data/Bundles.fr-FR, this will be the name without locale like Data/Bundles. If the name has no locale, this field is equivalent. public IAssetName NameWithoutLocale => this.AssetInfo.NameWithoutLocale; /// The requested data type. public Type DataType => this.AssetInfo.DataType; /// The load operations requested by the event handler. internal List LoadOperations { get; } = new(); /// The edit operations requested by the event handler. internal List EditOperations { get; } = new(); /********* ** Public methods *********/ /// Construct an instance. /// The asset info being requested. /// Get the mod metadata for a content pack, if it's a valid content pack for the mod. internal AssetRequestedEventArgs(IAssetInfo assetInfo, Func getOnBehalfOf) { this.AssetInfo = assetInfo; this.GetOnBehalfOf = getOnBehalfOf; } /// Set the mod handling the event. /// The mod handling the event. internal void SetMod(IModMetadata mod) { this.Mod = mod; } /// Provide the initial instance for the asset, instead of trying to load it from the game's Content folder. /// Get the initial instance of an asset. /// If there are multiple loads that apply to the same asset, the priority with which this one should be applied. /// The content pack ID on whose behalf you're applying the change. This is only valid for content packs for your mod. /// /// Usage notes: /// /// The asset doesn't need to exist in the game's Content folder. If any mod loads the asset, the game will see it as an existing asset as if it was in that folder. /// Each asset can logically only have one initial instance. If multiple loads apply at the same time, SMAPI will use the parameter to decide what happens. If you're making changes to the existing asset instead of replacing it, you should use instead to avoid those limitations and improve mod compatibility. /// /// public void LoadFrom(Func load, AssetLoadPriority priority, string? onBehalfOf = null) { IModMetadata mod = this.GetMod(); this.LoadOperations.Add( new AssetLoadOperation( Mod: mod, OnBehalfOf: this.GetOnBehalfOf(mod, onBehalfOf, "load assets"), Priority: priority, GetData: _ => load() ) ); } /// Provide the initial instance for the asset from a file in your mod folder, instead of trying to load it from the game's Content folder. /// The expected data type. The main supported types are , , dictionaries, and lists; other types may be supported by the game's content pipeline. /// The relative path to the file in your mod folder. /// If there are multiple loads that apply to the same asset, the priority with which this one should be applied. /// /// Usage notes: /// /// The asset doesn't need to exist in the game's Content folder. If any mod loads the asset, the game will see it as an existing asset as if it was in that folder. /// 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 instead to avoid those limitations and improve mod compatibility. /// /// public void LoadFromModFile(string relativePath, AssetLoadPriority priority) where TAsset : notnull { IModMetadata mod = this.GetMod(); this.LoadOperations.Add( new AssetLoadOperation( Mod: mod, OnBehalfOf: null, Priority: priority, GetData: _ => mod.Mod!.Helper.ModContent.Load(relativePath) ) ); } /// Edit the asset after it's loaded. /// Apply changes to the asset. /// If there are multiple edits that apply to the same asset, the priority with which this one should be applied. /// The content pack ID on whose behalf you're applying the change. This is only valid for content packs for your mod. /// /// Usage notes: /// /// Editing an asset which doesn't exist has no effect. This is applied after the asset is loaded from the game's Content folder, or from any mod's or . /// 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). /// /// public void Edit(Action apply, AssetEditPriority priority = AssetEditPriority.Default, string? onBehalfOf = null) { IModMetadata mod = this.GetMod(); this.EditOperations.Add( new AssetEditOperation( Mod: mod, Priority: priority, OnBehalfOf: this.GetOnBehalfOf(mod, onBehalfOf, "edit assets"), ApplyEdit: apply ) ); } /********* ** Private methods *********/ /// Get the mod handling the event. /// This instance hasn't been initialized with the mod metadata yet. private IModMetadata GetMod() { return this.Mod ?? throw new InvalidOperationException($"This {nameof(AssetRequestedEventArgs)} instance hasn't been initialized yet."); } } }