From ad8912047beaf84ce34f4918703d55841be13ff0 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 26 Mar 2022 01:43:40 -0400 Subject: add asset edit priority (#766) --- src/SMAPI/Events/AssetEditPriority.cs | 16 ++++++++++++++++ src/SMAPI/Events/AssetRequestedEventArgs.cs | 4 +++- src/SMAPI/Framework/Content/AssetEditOperation.cs | 8 +++++++- src/SMAPI/Framework/ContentCoordinator.cs | 1 + .../Framework/ContentManagers/GameContentManager.cs | 7 +++++-- 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 src/SMAPI/Events/AssetEditPriority.cs (limited to 'src') diff --git a/src/SMAPI/Events/AssetEditPriority.cs b/src/SMAPI/Events/AssetEditPriority.cs new file mode 100644 index 00000000..d41dfd7d --- /dev/null +++ b/src/SMAPI/Events/AssetEditPriority.cs @@ -0,0 +1,16 @@ +namespace StardewModdingAPI.Events +{ + /// The priority for an asset edit when multiple apply for the same asset. + /// You can also specify arbitrary intermediate values, like AssetLoadPriority.Low + 5. + public enum AssetEditPriority + { + /// This edit should be applied before (i.e. 'under') edits. + Early = -1000, + + /// The default priority. + Default = 0, + + /// This edit should be applied after (i.e. 'on top of') edits. + Late = 1000 + } +} diff --git a/src/SMAPI/Events/AssetRequestedEventArgs.cs b/src/SMAPI/Events/AssetRequestedEventArgs.cs index 9e2cde7f..4d9ee236 100644 --- a/src/SMAPI/Events/AssetRequestedEventArgs.cs +++ b/src/SMAPI/Events/AssetRequestedEventArgs.cs @@ -100,6 +100,7 @@ namespace StardewModdingAPI.Events /// 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: @@ -108,11 +109,12 @@ namespace StardewModdingAPI.Events /// 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, string onBehalfOf = null) + public void Edit(Action apply, AssetEditPriority priority = AssetEditPriority.Default, string onBehalfOf = null) { this.EditOperations.Add( new AssetEditOperation( mod: this.Mod, + priority: priority, onBehalfOf: this.GetOnBehalfOf(this.Mod, onBehalfOf, "edit assets"), apply ) diff --git a/src/SMAPI/Framework/Content/AssetEditOperation.cs b/src/SMAPI/Framework/Content/AssetEditOperation.cs index 14db231c..818209fa 100644 --- a/src/SMAPI/Framework/Content/AssetEditOperation.cs +++ b/src/SMAPI/Framework/Content/AssetEditOperation.cs @@ -1,4 +1,5 @@ using System; +using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Content { @@ -11,6 +12,9 @@ namespace StardewModdingAPI.Framework.Content /// The mod applying the edit. public IModMetadata Mod { get; } + /// If there are multiple edits that apply to the same asset, the priority with which this one should be applied. + public AssetEditPriority Priority { get; } + /// The content pack on whose behalf the edit is being applied, if any. public IModMetadata OnBehalfOf { get; } @@ -23,11 +27,13 @@ namespace StardewModdingAPI.Framework.Content *********/ /// Construct an instance. /// The mod applying the edit. + /// If there are multiple edits that apply to the same asset, the priority with which this one should be applied. /// The content pack on whose behalf the edit is being applied, if any. /// Apply the edit to an asset. - public AssetEditOperation(IModMetadata mod, IModMetadata onBehalfOf, Action applyEdit) + public AssetEditOperation(IModMetadata mod, AssetEditPriority priority, IModMetadata onBehalfOf, Action applyEdit) { this.Mod = mod; + this.Priority = priority; this.OnBehalfOf = onBehalfOf; this.ApplyEdit = applyEdit; } diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index fc137e50..8e7465de 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -640,6 +640,7 @@ namespace StardewModdingAPI.Framework { new AssetEditOperation( mod: editor.Mod, + priority: AssetEditPriority.Default, onBehalfOf: null, applyEdit: assetData => editor.Data.Edit(assetData) ) diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs index 16eddb00..a121f4c0 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -82,9 +82,12 @@ namespace StardewModdingAPI.Framework.ContentManagers AssetLoadOperation[] loaders = this.GetLoaders(info).ToArray(); if (!this.AssertMaxOneRequiredLoader(info, loaders, out string error)) + { this.Monitor.Log(error, LogLevel.Warn); + return false; + } - return loaders.Length == 1; + return loaders.Any(); } /// @@ -334,7 +337,7 @@ namespace StardewModdingAPI.Framework.ContentManagers } // edit asset - AssetEditOperation[] editors = this.GetEditors(info).ToArray(); + AssetEditOperation[] editors = this.GetEditors(info).OrderBy(p => p.Priority).ToArray(); foreach (AssetEditOperation editor in editors) { IModMetadata mod = editor.Mod; -- cgit