use records for asset edit operations

This commit is contained in:
Jesse Plamondon-Willard 2022-05-08 18:50:07 -04:00
parent 5f2e83969a
commit f8f8b23799
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
5 changed files with 40 additions and 123 deletions

View File

@ -72,10 +72,10 @@ namespace StardewModdingAPI.Events
{
this.LoadOperations.Add(
new AssetLoadOperation(
mod: this.Mod,
priority: priority,
onBehalfOf: this.GetOnBehalfOf(this.Mod, onBehalfOf, "load assets"),
getData: _ => load()
Mod: this.Mod,
OnBehalfOf: this.GetOnBehalfOf(this.Mod, onBehalfOf, "load assets"),
Priority: priority,
GetData: _ => load()
)
);
}
@ -96,10 +96,10 @@ namespace StardewModdingAPI.Events
{
this.LoadOperations.Add(
new AssetLoadOperation(
mod: this.Mod,
priority: priority,
onBehalfOf: null,
_ => this.Mod.Mod!.Helper.ModContent.Load<TAsset>(relativePath)
Mod: this.Mod,
OnBehalfOf: null,
Priority: priority,
GetData: _ => this.Mod.Mod!.Helper.ModContent.Load<TAsset>(relativePath)
)
);
}
@ -119,10 +119,10 @@ namespace StardewModdingAPI.Events
{
this.EditOperations.Add(
new AssetEditOperation(
mod: this.Mod,
priority: priority,
onBehalfOf: this.GetOnBehalfOf(this.Mod, onBehalfOf, "edit assets"),
apply
Mod: this.Mod,
Priority: priority,
OnBehalfOf: this.GetOnBehalfOf(this.Mod, onBehalfOf, "edit assets"),
ApplyEdit: apply
)
);
}

View File

@ -4,38 +4,9 @@ using StardewModdingAPI.Events;
namespace StardewModdingAPI.Framework.Content
{
/// <summary>An edit to apply to an asset when it's requested from the content pipeline.</summary>
internal class AssetEditOperation
{
/*********
** Accessors
*********/
/// <summary>The mod applying the edit.</summary>
public IModMetadata Mod { get; }
/// <summary>If there are multiple edits that apply to the same asset, the priority with which this one should be applied.</summary>
public AssetEditPriority Priority { get; }
/// <summary>The content pack on whose behalf the edit is being applied, if any.</summary>
public IModMetadata? OnBehalfOf { get; }
/// <summary>Apply the edit to an asset.</summary>
public Action<IAssetData> ApplyEdit { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="mod">The mod applying the edit.</param>
/// <param name="priority">If there are multiple edits 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 edit is being applied, if any.</param>
/// <param name="applyEdit">Apply the edit to an asset.</param>
public AssetEditOperation(IModMetadata mod, AssetEditPriority priority, IModMetadata? onBehalfOf, Action<IAssetData> applyEdit)
{
this.Mod = mod;
this.Priority = priority;
this.OnBehalfOf = onBehalfOf;
this.ApplyEdit = applyEdit;
}
}
/// <param name="Mod">The mod applying the edit.</param>
/// <param name="Priority">If there are multiple edits 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 edit is being applied, if any.</param>
/// <param name="ApplyEdit">Apply the edit to an asset.</param>
internal record AssetEditOperation(IModMetadata Mod, AssetEditPriority Priority, IModMetadata? OnBehalfOf, Action<IAssetData> ApplyEdit);
}

View File

@ -4,38 +4,9 @@ using StardewModdingAPI.Events;
namespace StardewModdingAPI.Framework.Content
{
/// <summary>An operation which provides the initial instance of an asset when it's requested from the content pipeline.</summary>
internal class AssetLoadOperation
{
/*********
** Accessors
*********/
/// <summary>The mod loading the asset.</summary>
public IModMetadata Mod { get; }
/// <summary>The content pack on whose behalf the asset is being loaded, if any.</summary>
public IModMetadata? OnBehalfOf { 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; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="mod">The mod applying the edit.</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, AssetLoadPriority priority, IModMetadata? onBehalfOf, Func<IAssetInfo, object> getData)
{
this.Mod = mod;
this.Priority = priority;
this.OnBehalfOf = onBehalfOf;
this.GetData = getData;
}
}
/// <param name="Mod">The mod applying the edit.</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>
internal record AssetLoadOperation(IModMetadata Mod, IModMetadata? OnBehalfOf, AssetLoadPriority Priority, Func<IAssetInfo, object> GetData);
}

View File

@ -1,33 +1,8 @@
namespace StardewModdingAPI.Framework.Content
{
/// <summary>A set of operations to apply to an asset for a given <see cref="IAssetEditor"/> or <see cref="IAssetLoader"/> implementation.</summary>
internal class AssetOperationGroup
{
/*********
** Accessors
*********/
/// <summary>The mod applying the changes.</summary>
public IModMetadata Mod { get; }
/// <summary>The load operations to apply.</summary>
public AssetLoadOperation[] LoadOperations { get; }
/// <summary>The edit operations to apply.</summary>
public AssetEditOperation[] EditOperations { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="mod">The mod applying the changes.</param>
/// <param name="loadOperations">The load operations to apply.</param>
/// <param name="editOperations">The edit operations to apply.</param>
public AssetOperationGroup(IModMetadata mod, AssetLoadOperation[] loadOperations, AssetEditOperation[] editOperations)
{
this.Mod = mod;
this.LoadOperations = loadOperations;
this.EditOperations = editOperations;
}
}
/// <param name="Mod">The mod applying the changes.</param>
/// <param name="LoadOperations">The load operations to apply.</param>
/// <param name="EditOperations">The edit operations to apply.</param>
internal record AssetOperationGroup(IModMetadata Mod, AssetLoadOperation[] LoadOperations, AssetEditOperation[] EditOperations);
}

View File

@ -615,19 +615,19 @@ namespace StardewModdingAPI.Framework
editor: loader.Data,
dataType: info.DataType,
createGroup: () => new AssetOperationGroup(
mod: loader.Mod,
loadOperations: new[]
Mod: loader.Mod,
LoadOperations: new[]
{
new AssetLoadOperation(
mod: loader.Mod,
priority: AssetLoadPriority.Exclusive,
onBehalfOf: null,
getData: assetInfo => loader.Data.Load<T>(
Mod: loader.Mod,
OnBehalfOf: null,
Priority: AssetLoadPriority.Exclusive,
GetData: assetInfo => loader.Data.Load<T>(
this.GetLegacyAssetInfo(assetInfo)
)
)
},
editOperations: Array.Empty<AssetEditOperation>()
EditOperations: Array.Empty<AssetEditOperation>()
)
);
}
@ -674,15 +674,15 @@ namespace StardewModdingAPI.Framework
editor: editor.Data,
dataType: info.DataType,
createGroup: () => new AssetOperationGroup(
mod: editor.Mod,
loadOperations: Array.Empty<AssetLoadOperation>(),
editOperations: new[]
Mod: editor.Mod,
LoadOperations: Array.Empty<AssetLoadOperation>(),
EditOperations: new[]
{
new AssetEditOperation(
mod: editor.Mod,
priority: priority,
onBehalfOf: null,
applyEdit: assetData => editor.Data.Edit<T>(
Mod: editor.Mod,
OnBehalfOf: null,
Priority: priority,
ApplyEdit: assetData => editor.Data.Edit<T>(
this.GetLegacyAssetData(assetData)
)
)