add asset type to AssetRequested event (#766)

This commit is contained in:
Jesse Plamondon-Willard 2022-03-27 12:16:28 -04:00
parent 03efea2667
commit d864f2ed77
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
9 changed files with 24 additions and 13 deletions

View File

@ -30,6 +30,9 @@ namespace StardewModdingAPI.Events
/// <remarks>For example, if <see cref="Name"/> contains a locale like <c>Data/Bundles.fr-FR</c>, this will be the name without locale like <c>Data/Bundles</c>. If the name has no locale, this field is equivalent.</remarks>
public IAssetName NameWithoutLocale { get; }
/// <summary>The requested data type.</summary>
public Type DataType { get; }
/// <summary>The load operations requested by the event handler.</summary>
internal IList<AssetLoadOperation> LoadOperations { get; } = new List<AssetLoadOperation>();
@ -43,13 +46,15 @@ namespace StardewModdingAPI.Events
/// <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>
/// <param name="dataType">The requested data type.</param>
/// <param name="nameWithoutLocale">The <paramref name="name"/> with any locale codes stripped.</param>
/// <param name="getOnBehalfOf">Get the mod metadata for a content pack, if it's a valid content pack for the mod.</param>
internal AssetRequestedEventArgs(IModMetadata mod, IAssetName name, IAssetName nameWithoutLocale, Func<IModMetadata, string, string, IModMetadata> getOnBehalfOf)
internal AssetRequestedEventArgs(IModMetadata mod, IAssetName name, IAssetName nameWithoutLocale, Type dataType, Func<IModMetadata, string, string, IModMetadata> getOnBehalfOf)
{
this.Mod = mod;
this.Name = name;
this.NameWithoutLocale = nameWithoutLocale;
this.DataType = dataType;
this.GetOnBehalfOf = getOnBehalfOf;
}

View File

@ -315,9 +315,10 @@ namespace StardewModdingAPI.Framework
}
/// <summary>Get whether an asset from a mod folder exists.</summary>
/// <typeparam name="T">The expected asset type.</typeparam>
/// <param name="contentManagerID">The unique name for the content manager which should load this asset.</param>
/// <param name="assetName">The asset name within the mod folder.</param>
public bool DoesManagedAssetExist(string contentManagerID, IAssetName assetName)
public bool DoesManagedAssetExist<T>(string contentManagerID, IAssetName assetName)
{
// get content manager
IContentManager contentManager = this.ContentManagerLock.InReadLock(() =>
@ -327,7 +328,7 @@ namespace StardewModdingAPI.Framework
throw new InvalidOperationException($"The '{contentManagerID}' prefix isn't handled by any mod.");
// get whether the asset exists
return contentManager.DoesAssetExist(assetName);
return contentManager.DoesAssetExist<T>(assetName);
}
/// <summary>Get a copy of an asset from a mod folder.</summary>

View File

@ -97,7 +97,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
}
/// <inheritdoc />
public virtual bool DoesAssetExist(IAssetName assetName)
public virtual bool DoesAssetExist<T>(IAssetName assetName)
{
return this.Cache.ContainsKey(assetName.Name);
}

View File

@ -58,9 +58,9 @@ namespace StardewModdingAPI.Framework.ContentManagers
}
/// <inheritdoc />
public override bool DoesAssetExist(IAssetName assetName)
public override bool DoesAssetExist<T>(IAssetName assetName)
{
if (base.DoesAssetExist(assetName))
if (base.DoesAssetExist<T>(assetName))
return true;
// vanilla asset
@ -69,11 +69,11 @@ namespace StardewModdingAPI.Framework.ContentManagers
// managed asset
if (this.Coordinator.TryParseManagedAssetKey(assetName.Name, out string contentManagerID, out IAssetName relativePath))
return this.Coordinator.DoesManagedAssetExist(contentManagerID, relativePath);
return this.Coordinator.DoesManagedAssetExist<T>(contentManagerID, relativePath);
// custom asset from a loader
string locale = this.GetLocale();
IAssetInfo info = new AssetInfo(locale, assetName, typeof(object), this.AssertAndNormalizeAssetName);
IAssetInfo info = new AssetInfo(locale, assetName, typeof(T), this.AssertAndNormalizeAssetName);
AssetLoadOperation[] loaders = this.GetLoaders<object>(info).ToArray();
if (!this.AssertMaxOneRequiredLoader(info, loaders, out string error))

View File

@ -29,8 +29,9 @@ namespace StardewModdingAPI.Framework.ContentManagers
** Methods
*********/
/// <summary>Get whether an asset exists and can be loaded.</summary>
/// <typeparam name="T">The expected asset type.</typeparam>
/// <param name="assetName">The normalized asset name.</param>
bool DoesAssetExist(IAssetName assetName);
bool DoesAssetExist<T>(IAssetName assetName);
/// <summary>Load an asset through the content pipeline, using a localized variant of the <paramref name="assetName"/> if available.</summary>
/// <typeparam name="T">The type of asset to load.</typeparam>

View File

@ -66,9 +66,9 @@ namespace StardewModdingAPI.Framework.ContentManagers
}
/// <inheritdoc />
public override bool DoesAssetExist(IAssetName assetName)
public override bool DoesAssetExist<T>(IAssetName assetName)
{
if (base.DoesAssetExist(assetName))
if (base.DoesAssetExist<T>(assetName))
return true;
FileInfo file = this.GetModFile(assetName.Name);

View File

@ -44,9 +44,11 @@ namespace StardewModdingAPI.Framework.ModHelpers
public LocalizedContentManager.LanguageCode CurrentLocaleConstant => this.GameContentManager.Language;
/// <summary>The observable implementation of <see cref="AssetEditors"/>.</summary>
[Obsolete]
internal ObservableCollection<IAssetEditor> ObservableAssetEditors { get; } = new();
/// <summary>The observable implementation of <see cref="AssetLoaders"/>.</summary>
[Obsolete]
internal ObservableCollection<IAssetLoader> ObservableAssetLoaders { get; } = new();
/// <inheritdoc />

View File

@ -1151,7 +1151,7 @@ namespace StardewModdingAPI.Framework
this.EventManager.AssetRequested.Raise(
invoke: (mod, invoke) =>
{
AssetRequestedEventArgs args = new(mod, asset.Name, asset.NameWithoutLocale, this.GetOnBehalfOfContentPack);
AssetRequestedEventArgs args = new(mod, asset.Name, asset.NameWithoutLocale, asset.DataType, this.GetOnBehalfOfContentPack);
invoke(args);

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Content;
using StardewModdingAPI.Events;
using StardewValley;
using xTile;
@ -16,9 +16,11 @@ namespace StardewModdingAPI
** Accessors
*********/
/// <summary>Interceptors which provide the initial versions of matching content assets.</summary>
[Obsolete($"Use {nameof(IMod.Helper)}.{nameof(IModHelper.Events)}.{nameof(IModEvents.Content)} instead. This interface will be removed in SMAPI 4.0.0.")]
IList<IAssetLoader> AssetLoaders { get; }
/// <summary>Interceptors which edit matching content assets after they're loaded.</summary>
[Obsolete($"Use {nameof(IMod.Helper)}.{nameof(IModHelper.Events)}.{nameof(IModEvents.Content)} instead. This interface will be removed in SMAPI 4.0.0.")]
IList<IAssetEditor> AssetEditors { get; }
/// <summary>The game's current locale code (like <c>pt-BR</c>).</summary>