summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Events/AssetLoadPriority.cs19
-rw-r--r--src/SMAPI/Events/AssetRequestedEventArgs.cs14
-rw-r--r--src/SMAPI/Framework/Content/AssetLoadOperation.cs11
-rw-r--r--src/SMAPI/Framework/ContentCoordinator.cs2
-rw-r--r--src/SMAPI/Framework/ContentManagers/GameContentManager.cs10
5 files changed, 38 insertions, 18 deletions
diff --git a/src/SMAPI/Events/AssetLoadPriority.cs b/src/SMAPI/Events/AssetLoadPriority.cs
new file mode 100644
index 00000000..e07b5a40
--- /dev/null
+++ b/src/SMAPI/Events/AssetLoadPriority.cs
@@ -0,0 +1,19 @@
+namespace StardewModdingAPI.Events
+{
+ /// <summary>The priority for an asset load when multiple apply for the same asset.</summary>
+ /// <remarks>If multiple non-<see cref="Exclusive"/> loads have the same priority, the one registered first will be selected. You can also specify arbitrary intermediate values, like <c>AssetLoadPriority.Low + 5</c>.</remarks>
+ public enum AssetLoadPriority
+ {
+ /// <summary>This load is optional and can safely be skipped if there are higher-priority loads.</summary>
+ Low = -1000,
+
+ /// <summary>The load is optional and can safely be skipped if there are higher-priority loads, but it should still be preferred over any <see cref="Low"/>-priority loads.</summary>
+ Medium = 0,
+
+ /// <summary>The load is optional and can safely be skipped if there are higher-priority loads, but it should still be preferred over any <see cref="Low"/>- or <see cref="Medium"/>-priority loads.</summary>
+ High = 1000,
+
+ /// <summary>The load is not optional. If more than one loader has <see cref="Exclusive"/> priority, SMAPI will log an error and ignore all of them.</summary>
+ Exclusive = int.MaxValue
+ }
+}
diff --git a/src/SMAPI/Events/AssetRequestedEventArgs.cs b/src/SMAPI/Events/AssetRequestedEventArgs.cs
index 9942079b..d022a4de 100644
--- a/src/SMAPI/Events/AssetRequestedEventArgs.cs
+++ b/src/SMAPI/Events/AssetRequestedEventArgs.cs
@@ -49,21 +49,21 @@ namespace StardewModdingAPI.Events
/// <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>
+ /// <param name="priority">If there are multiple loads that apply to the same asset, the priority with which this one should be applied.</param>
/// <param name="onBehalfOf">The content pack ID on whose behalf you're applying the change. This is only valid for content packs for your mod.</param>
- /// <param name="allowSkipOnConflict">When there are multiple loads that apply to the same asset, this indicates whether this one can be skipped to resolve the conflict. If all loads allow skipping, the first one that was registered will be applied. If this is false, SMAPI will raise an error and apply none of them.</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 use the <paramref name="allowSkipOnConflict"/> parameter to decide what happens. 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>
+ /// <item>Each asset can logically only have one initial instance. If multiple loads apply at the same time, SMAPI will use the <paramref name="priority"/> parameter to decide what happens. 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, string onBehalfOf = null, bool allowSkipOnConflict = false)
+ public void LoadFrom(Func<object> load, AssetLoadPriority priority, string onBehalfOf = null)
{
this.LoadOperations.Add(
new AssetLoadOperation(
mod: this.Mod,
- allowSkipOnConflict: allowSkipOnConflict,
+ priority: priority,
onBehalfOf: this.GetOnBehalfOf(this.Mod, onBehalfOf, "load assets"),
getData: _ => load()
)
@@ -73,7 +73,7 @@ namespace StardewModdingAPI.Events
/// <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>
- /// <param name="allowSkipOnConflict">When there are multiple loads that apply to the same asset, this indicates whether this one can be skipped to resolve the conflict. If all loads allow skipping, the first one that was registered will be applied. If this is false, SMAPI will raise an error and apply none of them.</param>
+ /// <param name="priority">If there are multiple loads that apply to the same asset, the priority with which this one should be applied.</param>
/// <remarks>
/// Usage notes:
/// <list type="bullet">
@@ -81,12 +81,12 @@ namespace StardewModdingAPI.Events
/// <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, bool allowSkipOnConflict = false)
+ public void LoadFromModFile<TAsset>(string relativePath, AssetLoadPriority priority)
{
this.LoadOperations.Add(
new AssetLoadOperation(
mod: this.Mod,
- allowSkipOnConflict: allowSkipOnConflict,
+ priority: priority,
onBehalfOf: null,
_ => this.Mod.Mod.Helper.Content.Load<TAsset>(relativePath))
);
diff --git a/src/SMAPI/Framework/Content/AssetLoadOperation.cs b/src/SMAPI/Framework/Content/AssetLoadOperation.cs
index 36baf1aa..b12958d6 100644
--- a/src/SMAPI/Framework/Content/AssetLoadOperation.cs
+++ b/src/SMAPI/Framework/Content/AssetLoadOperation.cs
@@ -1,4 +1,5 @@
using System;
+using StardewModdingAPI.Events;
namespace StardewModdingAPI.Framework.Content
{
@@ -14,8 +15,8 @@ namespace StardewModdingAPI.Framework.Content
/// <summary>The content pack on whose behalf the asset is being loaded, if any.</summary>
public IModMetadata OnBehalfOf { get; }
- /// <summary>Whether to allow skipping this operation to resolve a load conflict.</summary>
- public bool AllowSkipOnConflict { get; }
+ /// <summary>If there are multiple loads that apply to the same asset, the priority with which this one should be applied.</summary>
+ public AssetLoadPriority Priority { get; }
/// <summary>Load the initial value for an asset.</summary>
public Func<IAssetInfo, object> GetData { get; }
@@ -26,13 +27,13 @@ namespace StardewModdingAPI.Framework.Content
*********/
/// <summary>Construct an instance.</summary>
/// <param name="mod">The mod applying the edit.</param>
- /// <param name="allowSkipOnConflict">Whether to allow skipping this operation to resolve a load conflict.</param>
+ /// <param name="priority">If there are multiple loads that apply to the same asset, the priority with which this one should be applied.</param>
/// <param name="onBehalfOf">The content pack on whose behalf the asset is being loaded, if any.</param>
/// <param name="getData">Load the initial value for an asset.</param>
- public AssetLoadOperation(IModMetadata mod, bool allowSkipOnConflict, IModMetadata onBehalfOf, Func<IAssetInfo, object> getData)
+ public AssetLoadOperation(IModMetadata mod, AssetLoadPriority priority, IModMetadata onBehalfOf, Func<IAssetInfo, object> getData)
{
this.Mod = mod;
- this.AllowSkipOnConflict = allowSkipOnConflict;
+ this.Priority = priority;
this.OnBehalfOf = onBehalfOf;
this.GetData = getData;
}
diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs
index 43cebcbe..144832b2 100644
--- a/src/SMAPI/Framework/ContentCoordinator.cs
+++ b/src/SMAPI/Framework/ContentCoordinator.cs
@@ -608,7 +608,7 @@ namespace StardewModdingAPI.Framework
{
new AssetLoadOperation(
mod: loader.Mod,
- allowSkipOnConflict: false,
+ priority: AssetLoadPriority.Exclusive,
onBehalfOf: null,
getData: assetInfo => loader.Data.Load<T>(assetInfo)
)
diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs
index b3e98648..16eddb00 100644
--- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs
+++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs
@@ -6,6 +6,7 @@ using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
+using StardewModdingAPI.Events;
using StardewModdingAPI.Framework.Content;
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Reflection;
@@ -275,7 +276,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
// find matching loader
AssetLoadOperation loader;
{
- AssetLoadOperation[] loaders = this.GetLoaders<T>(info).ToArray();
+ AssetLoadOperation[] loaders = this.GetLoaders<T>(info).OrderByDescending(p => p.Priority).ToArray();
if (!this.AssertMaxOneRequiredLoader(info, loaders, out string error))
{
@@ -283,9 +284,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
return null;
}
- loader =
- loaders.FirstOrDefault(p => !p.AllowSkipOnConflict)
- ?? loaders.FirstOrDefault();
+ loader = loaders.FirstOrDefault();
}
// no loader found
@@ -396,7 +395,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <returns>Returns true if only one loader will apply, else false.</returns>
private bool AssertMaxOneRequiredLoader(IAssetInfo info, AssetLoadOperation[] loaders, out string error)
{
- AssetLoadOperation[] required = loaders.Where(p => !p.AllowSkipOnConflict).ToArray();
+ AssetLoadOperation[] required = loaders.Where(p => p.Priority == AssetLoadPriority.Exclusive).ToArray();
if (required.Length <= 1)
{
error = null;
@@ -405,6 +404,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
string[] loaderNames = required
.Select(p => p.Mod.DisplayName + this.GetOnBehalfOfLabel(p.OnBehalfOf))
+ .OrderBy(p => p)
.Distinct()
.ToArray();
string errorPhrase = loaderNames.Length > 1