migrate more internal code to IAssetName (#766)

This commit is contained in:
Jesse Plamondon-Willard 2022-03-05 15:31:06 -05:00
parent e82406a845
commit b0d8b23c2c
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
8 changed files with 132 additions and 150 deletions

View File

@ -136,7 +136,7 @@ namespace StardewModdingAPI.Framework
); );
this.ContentManagers.Add(contentManagerForAssetPropagation); this.ContentManagers.Add(contentManagerForAssetPropagation);
this.VanillaContentManager = new LocalizedContentManager(serviceProvider, rootDirectory); this.VanillaContentManager = new LocalizedContentManager(serviceProvider, rootDirectory);
this.CoreAssets = new CoreAssetPropagator(this.MainContentManager, contentManagerForAssetPropagation, this.Monitor, reflection, aggressiveMemoryOptimizations); this.CoreAssets = new CoreAssetPropagator(this.MainContentManager, contentManagerForAssetPropagation, this.Monitor, reflection, aggressiveMemoryOptimizations, this.ParseAssetName);
this.LocaleCodes = new Lazy<Dictionary<string, LocalizedContentManager.LanguageCode>>(() => this.GetLocaleCodes(customLanguages: Enumerable.Empty<ModLanguage>())); this.LocaleCodes = new Lazy<Dictionary<string, LocalizedContentManager.LanguageCode>>(() => this.GetLocaleCodes(customLanguages: Enumerable.Empty<ModLanguage>()));
} }
@ -260,8 +260,8 @@ namespace StardewModdingAPI.Framework
} }
/// <summary>Get whether this asset is mapped to a mod folder.</summary> /// <summary>Get whether this asset is mapped to a mod folder.</summary>
/// <param name="key">The asset key.</param> /// <param name="key">The asset name.</param>
public bool IsManagedAssetKey(string key) public bool IsManagedAssetKey(IAssetName key)
{ {
return key.StartsWith(this.ManagedPrefix); return key.StartsWith(this.ManagedPrefix);
} }
@ -269,9 +269,9 @@ namespace StardewModdingAPI.Framework
/// <summary>Parse a managed SMAPI asset key which maps to a mod folder.</summary> /// <summary>Parse a managed SMAPI asset key which maps to a mod folder.</summary>
/// <param name="key">The asset key.</param> /// <param name="key">The asset key.</param>
/// <param name="contentManagerID">The unique name for the content manager which should load this asset.</param> /// <param name="contentManagerID">The unique name for the content manager which should load this asset.</param>
/// <param name="relativePath">The relative path within the mod folder.</param> /// <param name="relativePath">The asset name within the mod folder.</param>
/// <returns>Returns whether the asset was parsed successfully.</returns> /// <returns>Returns whether the asset was parsed successfully.</returns>
public bool TryParseManagedAssetKey(string key, out string contentManagerID, out string relativePath) public bool TryParseManagedAssetKey(string key, out string contentManagerID, out IAssetName relativePath)
{ {
contentManagerID = null; contentManagerID = null;
relativePath = null; relativePath = null;
@ -285,7 +285,7 @@ namespace StardewModdingAPI.Framework
if (parts.Length != 3) // managed key prefix, mod id, relative path if (parts.Length != 3) // managed key prefix, mod id, relative path
return false; return false;
contentManagerID = Path.Combine(parts[0], parts[1]); contentManagerID = Path.Combine(parts[0], parts[1]);
relativePath = parts[2]; relativePath = this.ParseAssetName(parts[2]);
return true; return true;
} }
@ -299,8 +299,8 @@ namespace StardewModdingAPI.Framework
/// <summary>Get a copy of an asset from a mod folder.</summary> /// <summary>Get a copy of an asset from a mod folder.</summary>
/// <typeparam name="T">The asset type.</typeparam> /// <typeparam name="T">The asset type.</typeparam>
/// <param name="contentManagerID">The unique name for the content manager which should load this asset.</param> /// <param name="contentManagerID">The unique name for the content manager which should load this asset.</param>
/// <param name="relativePath">The internal SMAPI asset key.</param> /// <param name="relativePath">The asset name within the mod folder.</param>
public T LoadManagedAsset<T>(string contentManagerID, string relativePath) public T LoadManagedAsset<T>(string contentManagerID, IAssetName relativePath)
{ {
// get content manager // get content manager
IContentManager contentManager = this.ContentManagerLock.InReadLock(() => IContentManager contentManager = this.ContentManagerLock.InReadLock(() =>
@ -404,7 +404,7 @@ namespace StardewModdingAPI.Framework
/// <summary>Get all loaded instances of an asset name.</summary> /// <summary>Get all loaded instances of an asset name.</summary>
/// <param name="assetName">The asset name.</param> /// <param name="assetName">The asset name.</param>
[SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "This method is provided for Content Patcher.")] [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "This method is provided for Content Patcher.")]
public IEnumerable<object> GetLoadedValues(string assetName) public IEnumerable<object> GetLoadedValues(IAssetName assetName)
{ {
return this.ContentManagerLock.InReadLock(() => return this.ContentManagerLock.InReadLock(() =>
{ {

View File

@ -92,38 +92,32 @@ namespace StardewModdingAPI.Framework.ContentManagers
this.BaseDisposableReferences = reflection.GetField<List<IDisposable>>(this, "disposableAssets").GetValue(); this.BaseDisposableReferences = reflection.GetField<List<IDisposable>>(this, "disposableAssets").GetValue();
} }
/// <inheritdoc />
[Obsolete("This method is implemented for the base game and should not be used directly. To load an asset from the underlying content manager directly, use " + nameof(BaseContentManager.RawLoad) + " instead.")]
public override T LoadBase<T>(string assetName)
{
return this.Load<T>(assetName, LanguageCode.en);
}
/// <inheritdoc /> /// <inheritdoc />
public override T Load<T>(string assetName) public override T Load<T>(string assetName)
{ {
return this.Load<T>(assetName, this.Language, useCache: true); return this.Load<T>(assetName, this.Language);
} }
/// <inheritdoc /> /// <inheritdoc />
public override T Load<T>(string assetName, LanguageCode language) public override T Load<T>(string assetName, LanguageCode language)
{ {
return this.Load<T>(assetName, language, useCache: true); IAssetName parsedName = this.Coordinator.ParseAssetName(assetName);
return this.Load<T>(parsedName, language, useCache: true);
} }
/// <inheritdoc /> /// <inheritdoc />
public abstract T Load<T>(string assetName, LocalizedContentManager.LanguageCode language, bool useCache); public abstract T Load<T>(IAssetName assetName, LanguageCode language, bool useCache);
/// <inheritdoc />
[Obsolete("This method is implemented for the base game and should not be used directly. To load an asset from the underlying content manager directly, use " + nameof(BaseContentManager.RawLoad) + " instead.")]
public override T LoadBase<T>(string assetName)
{
return this.Load<T>(assetName, LanguageCode.en, useCache: true);
}
/// <inheritdoc /> /// <inheritdoc />
public virtual void OnLocaleChanged() { } public virtual void OnLocaleChanged() { }
/// <inheritdoc />
[Pure]
public string NormalizePathSeparators(string path)
{
return this.Cache.NormalizePathSeparators(path);
}
/// <inheritdoc /> /// <inheritdoc />
[SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")] [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")]
public string AssertAndNormalizeAssetName(string assetName) public string AssertAndNormalizeAssetName(string assetName)
@ -154,11 +148,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
} }
/// <inheritdoc /> /// <inheritdoc />
public bool IsLoaded(string assetName, LanguageCode language) public abstract bool IsLoaded(IAssetName assetName, LanguageCode language);
{
assetName = this.Cache.NormalizeKey(assetName);
return this.IsNormalizedKeyLoaded(assetName, language);
}
/**** /****
** Cache invalidation ** Cache invalidation
@ -233,6 +223,14 @@ namespace StardewModdingAPI.Framework.ContentManagers
/********* /*********
** Private methods ** Private methods
*********/ *********/
/// <summary>Normalize path separators in a file path. For asset keys, see <see cref="AssertAndNormalizeAssetName"/> instead.</summary>
/// <param name="path">The file path to normalize.</param>
[Pure]
protected string NormalizePathSeparators(string path)
{
return this.Cache.NormalizePathSeparators(path);
}
/// <summary>Load an asset file directly from the underlying content manager.</summary> /// <summary>Load an asset file directly from the underlying content manager.</summary>
/// <typeparam name="T">The type of asset to load.</typeparam> /// <typeparam name="T">The type of asset to load.</typeparam>
/// <param name="assetName">The normalized asset key.</param> /// <param name="assetName">The normalized asset key.</param>
@ -250,26 +248,18 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <param name="value">The asset value.</param> /// <param name="value">The asset value.</param>
/// <param name="language">The language code for which to inject the asset.</param> /// <param name="language">The language code for which to inject the asset.</param>
/// <param name="useCache">Whether to save the asset to the asset cache.</param> /// <param name="useCache">Whether to save the asset to the asset cache.</param>
protected virtual void TrackAsset<T>(string assetName, T value, LanguageCode language, bool useCache) protected virtual void TrackAsset<T>(IAssetName assetName, T value, LanguageCode language, bool useCache)
{ {
// track asset key // track asset key
if (value is Texture2D texture) if (value is Texture2D texture)
texture.Name = assetName; texture.Name = assetName.Name;
// cache asset // cache asset
if (useCache) if (useCache)
{ this.Cache[assetName.Name] = value;
assetName = this.AssertAndNormalizeAssetName(assetName);
this.Cache[assetName] = value;
}
// avoid hard disposable references; see remarks on the field // avoid hard disposable references; see remarks on the field
this.BaseDisposableReferences.Clear(); this.BaseDisposableReferences.Clear();
} }
/// <summary>Get whether an asset has already been loaded.</summary>
/// <param name="normalizedAssetName">The normalized asset name.</param>
/// <param name="language">The language to check.</param>
protected abstract bool IsNormalizedKeyLoaded(string normalizedAssetName, LanguageCode language);
} }
} }

View File

@ -63,7 +63,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
} }
/// <inheritdoc /> /// <inheritdoc />
public override T Load<T>(string assetName, LocalizedContentManager.LanguageCode language, bool useCache) public override T Load<T>(IAssetName assetName, LanguageCode language, bool useCache)
{ {
// raise first-load callback // raise first-load callback
if (GameContentManager.IsFirstLoad) if (GameContentManager.IsFirstLoad)
@ -73,49 +73,62 @@ namespace StardewModdingAPI.Framework.ContentManagers
} }
// normalize asset name // normalize asset name
IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); if (assetName.LanguageCode.HasValue)
if (parsedName.LanguageCode.HasValue) return this.Load<T>(this.Coordinator.ParseAssetName(assetName.BaseName), assetName.LanguageCode.Value, useCache);
return this.Load<T>(parsedName.BaseName, parsedName.LanguageCode.Value, useCache);
// get from cache // get from cache
if (useCache && this.IsLoaded(parsedName.Name, language)) if (useCache && this.IsLoaded(assetName, language))
return this.RawLoad<T>(parsedName.Name, language, useCache: true); return this.RawLoad<T>(assetName, language, useCache: true);
// get managed asset // get managed asset
if (this.Coordinator.TryParseManagedAssetKey(parsedName.Name, out string contentManagerID, out string relativePath)) if (this.Coordinator.TryParseManagedAssetKey(assetName.Name, out string contentManagerID, out IAssetName relativePath))
{ {
T managedAsset = this.Coordinator.LoadManagedAsset<T>(contentManagerID, relativePath); T managedAsset = this.Coordinator.LoadManagedAsset<T>(contentManagerID, relativePath);
this.TrackAsset(parsedName.Name, managedAsset, language, useCache); this.TrackAsset(assetName, managedAsset, language, useCache);
return managedAsset; return managedAsset;
} }
// load asset // load asset
T data; T data;
if (this.AssetsBeingLoaded.Contains(parsedName.Name)) if (this.AssetsBeingLoaded.Contains(assetName.Name))
{ {
this.Monitor.Log($"Broke loop while loading asset '{parsedName.Name}'.", LogLevel.Warn); this.Monitor.Log($"Broke loop while loading asset '{assetName}'.", LogLevel.Warn);
this.Monitor.Log($"Bypassing mod loaders for this asset. Stack trace:\n{Environment.StackTrace}"); this.Monitor.Log($"Bypassing mod loaders for this asset. Stack trace:\n{Environment.StackTrace}");
data = this.RawLoad<T>(parsedName.Name, language, useCache); data = this.RawLoad<T>(assetName, language, useCache);
} }
else else
{ {
data = this.AssetsBeingLoaded.Track(parsedName.Name, () => data = this.AssetsBeingLoaded.Track(assetName.Name, () =>
{ {
string locale = this.GetLocale(language); string locale = this.GetLocale(language);
IAssetInfo info = new AssetInfo(locale, parsedName, typeof(T), this.AssertAndNormalizeAssetName); IAssetInfo info = new AssetInfo(locale, assetName, typeof(T), this.AssertAndNormalizeAssetName);
IAssetData asset = IAssetData asset =
this.ApplyLoader<T>(info) this.ApplyLoader<T>(info)
?? new AssetDataForObject(info, this.RawLoad<T>(parsedName.Name, language, useCache), this.AssertAndNormalizeAssetName); ?? new AssetDataForObject(info, this.RawLoad<T>(assetName, language, useCache), this.AssertAndNormalizeAssetName);
asset = this.ApplyEditors<T>(info, asset); asset = this.ApplyEditors<T>(info, asset);
return (T)asset.Data; return (T)asset.Data;
}); });
} }
// update cache & return data // update cache & return data
this.TrackAsset(parsedName.Name, data, language, useCache); this.TrackAsset(assetName, data, language, useCache);
return data; return data;
} }
/// <inheritdoc />
public override bool IsLoaded(IAssetName assetName, LanguageCode language)
{
string cachedKey = null;
bool localized =
language != LanguageCode.en
&& !this.Coordinator.IsManagedAssetKey(assetName)
&& this.LocalizedAssetNames.TryGetValue(assetName.Name, out cachedKey);
return localized
? this.Cache.ContainsKey(cachedKey)
: this.Cache.ContainsKey(assetName.Name);
}
/// <inheritdoc /> /// <inheritdoc />
public override void OnLocaleChanged() public override void OnLocaleChanged()
{ {
@ -153,28 +166,13 @@ namespace StardewModdingAPI.Framework.ContentManagers
** Private methods ** Private methods
*********/ *********/
/// <inheritdoc /> /// <inheritdoc />
protected override bool IsNormalizedKeyLoaded(string normalizedAssetName, LanguageCode language) protected override void TrackAsset<T>(IAssetName assetName, T value, LanguageCode language, bool useCache)
{
string cachedKey = null;
bool localized =
language != LocalizedContentManager.LanguageCode.en
&& !this.Coordinator.IsManagedAssetKey(normalizedAssetName)
&& this.LocalizedAssetNames.TryGetValue(normalizedAssetName, out cachedKey);
return localized
? this.Cache.ContainsKey(cachedKey)
: this.Cache.ContainsKey(normalizedAssetName);
}
/// <inheritdoc />
protected override void TrackAsset<T>(string assetName, T value, LanguageCode language, bool useCache)
{ {
// handle explicit language in asset name // handle explicit language in asset name
{ {
IAssetName parsedName = this.Coordinator.ParseAssetName(assetName); if (assetName.LanguageCode.HasValue)
if (parsedName.LanguageCode.HasValue)
{ {
this.TrackAsset(parsedName.BaseName, value, parsedName.LanguageCode.Value, useCache); this.TrackAsset(this.Coordinator.ParseAssetName(assetName.BaseName), value, assetName.LanguageCode.Value, useCache);
return; return;
} }
} }
@ -188,16 +186,16 @@ namespace StardewModdingAPI.Framework.ContentManagers
// doesn't change the instance stored in the cache, e.g. using `asset.ReplaceWith`. // doesn't change the instance stored in the cache, e.g. using `asset.ReplaceWith`.
if (useCache) if (useCache)
{ {
string translatedKey = $"{assetName}.{this.GetLocale(language)}"; IAssetName translatedKey = new AssetName(assetName.Name, this.GetLocale(language), language);
base.TrackAsset(assetName, value, language, useCache: true); base.TrackAsset(assetName, value, language, useCache: true);
if (this.Cache.ContainsKey(translatedKey)) if (this.Cache.ContainsKey(translatedKey.Name))
base.TrackAsset(translatedKey, value, language, useCache: true); base.TrackAsset(translatedKey, value, language, useCache: true);
// track whether the injected asset is translatable for is-loaded lookups // track whether the injected asset is translatable for is-loaded lookups
if (this.Cache.ContainsKey(translatedKey)) if (this.Cache.ContainsKey(translatedKey.Name))
this.LocalizedAssetNames[assetName] = translatedKey; this.LocalizedAssetNames[assetName.Name] = translatedKey.Name;
else if (this.Cache.ContainsKey(assetName)) else if (this.Cache.ContainsKey(assetName.Name))
this.LocalizedAssetNames[assetName] = assetName; this.LocalizedAssetNames[assetName.Name] = assetName.Name;
else else
this.Monitor.Log($"Asset '{assetName}' could not be found in the cache immediately after injection.", LogLevel.Error); this.Monitor.Log($"Asset '{assetName}' could not be found in the cache immediately after injection.", LogLevel.Error);
} }
@ -209,32 +207,32 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <param name="language">The language code for which to load content.</param> /// <param name="language">The language code for which to load content.</param>
/// <param name="useCache">Whether to read/write the loaded asset to the asset cache.</param> /// <param name="useCache">Whether to read/write the loaded asset to the asset cache.</param>
/// <remarks>Derived from <see cref="LocalizedContentManager.Load{T}(string, LocalizedContentManager.LanguageCode)"/>.</remarks> /// <remarks>Derived from <see cref="LocalizedContentManager.Load{T}(string, LocalizedContentManager.LanguageCode)"/>.</remarks>
private T RawLoad<T>(string assetName, LanguageCode language, bool useCache) private T RawLoad<T>(IAssetName assetName, LanguageCode language, bool useCache)
{ {
try try
{ {
// use cached key // use cached key
if (language == this.Language && this.LocalizedAssetNames.TryGetValue(assetName, out string cachedKey)) if (language == this.Language && this.LocalizedAssetNames.TryGetValue(assetName.Name, out string cachedKey))
return base.RawLoad<T>(cachedKey, useCache); return base.RawLoad<T>(cachedKey, useCache);
// try translated key // try translated key
if (language != LocalizedContentManager.LanguageCode.en) if (language != LanguageCode.en)
{ {
string translatedKey = $"{assetName}.{this.GetLocale(language)}"; string translatedKey = $"{assetName}.{this.GetLocale(language)}";
try try
{ {
T obj = base.RawLoad<T>(translatedKey, useCache); T obj = base.RawLoad<T>(translatedKey, useCache);
this.LocalizedAssetNames[assetName] = translatedKey; this.LocalizedAssetNames[assetName.Name] = translatedKey;
return obj; return obj;
} }
catch (ContentLoadException) catch (ContentLoadException)
{ {
this.LocalizedAssetNames[assetName] = assetName; this.LocalizedAssetNames[assetName.Name] = assetName.Name;
} }
} }
// try base asset // try base asset
return base.RawLoad<T>(assetName, useCache); return base.RawLoad<T>(assetName.Name, useCache);
} }
catch (ContentLoadException ex) when (ex.InnerException is FileNotFoundException innerEx && innerEx.InnerException == null) catch (ContentLoadException ex) when (ex.InnerException is FileNotFoundException innerEx && innerEx.InnerException == null)
{ {

View File

@ -25,7 +25,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
: base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, onLoadingFirstAsset, aggressiveMemoryOptimizations) { } : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, onLoadingFirstAsset, aggressiveMemoryOptimizations) { }
/// <inheritdoc /> /// <inheritdoc />
public override T Load<T>(string assetName, LanguageCode language, bool useCache) public override T Load<T>(IAssetName assetName, LanguageCode language, bool useCache)
{ {
T data = base.Load<T>(assetName, language, useCache); T data = base.Load<T>(assetName, language, useCache);

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Exceptions;
using StardewValley; using StardewValley;
@ -31,15 +30,10 @@ namespace StardewModdingAPI.Framework.ContentManagers
*********/ *********/
/// <summary>Load an asset that has been processed by the content pipeline.</summary> /// <summary>Load an asset that has been processed by the content pipeline.</summary>
/// <typeparam name="T">The type of asset to load.</typeparam> /// <typeparam name="T">The type of asset to load.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param> /// <param name="assetName">The asset name relative to the loader root directory.</param>
/// <param name="language">The language code for which to load content.</param> /// <param name="language">The language code for which to load content.</param>
/// <param name="useCache">Whether to read/write the loaded asset to the asset cache.</param> /// <param name="useCache">Whether to read/write the loaded asset to the asset cache.</param>
T Load<T>(string assetName, LocalizedContentManager.LanguageCode language, bool useCache); T Load<T>(IAssetName assetName, LocalizedContentManager.LanguageCode language, bool useCache);
/// <summary>Normalize path separators in a file path. For asset keys, see <see cref="AssertAndNormalizeAssetName"/> instead.</summary>
/// <param name="path">The file path to normalize.</param>
[Pure]
string NormalizePathSeparators(string path);
/// <summary>Assert that the given key has a valid format and return a normalized form consistent with the underlying cache.</summary> /// <summary>Assert that the given key has a valid format and return a normalized form consistent with the underlying cache.</summary>
/// <param name="assetName">The asset key to check.</param> /// <param name="assetName">The asset key to check.</param>
@ -56,7 +50,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <summary>Get whether the content manager has already loaded and cached the given asset.</summary> /// <summary>Get whether the content manager has already loaded and cached the given asset.</summary>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param> /// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <param name="language">The language.</param> /// <param name="language">The language.</param>
bool IsLoaded(string assetName, LocalizedContentManager.LanguageCode language); bool IsLoaded(IAssetName assetName, LocalizedContentManager.LanguageCode language);
/// <summary>Purge matched assets from the cache.</summary> /// <summary>Purge matched assets from the cache.</summary>
/// <param name="predicate">Matches the asset keys to invalidate.</param> /// <param name="predicate">Matches the asset keys to invalidate.</param>

View File

@ -66,21 +66,19 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <inheritdoc /> /// <inheritdoc />
public override T Load<T>(string assetName) public override T Load<T>(string assetName)
{ {
return this.Load<T>(assetName, this.DefaultLanguage, useCache: false); return this.Load<T>(assetName, this.DefaultLanguage);
} }
/// <inheritdoc /> /// <inheritdoc />
public override T Load<T>(string assetName, LanguageCode language) public override T Load<T>(string assetName, LanguageCode language)
{ {
return this.Load<T>(assetName, language, useCache: false); IAssetName parsedName = this.Coordinator.ParseAssetName(assetName);
return this.Load<T>(parsedName, language, useCache: false);
} }
/// <inheritdoc /> /// <inheritdoc />
public override T Load<T>(string assetName, LanguageCode language, bool useCache) public override T Load<T>(IAssetName assetName, LanguageCode language, bool useCache)
{ {
// normalize key
IAssetName parsedName = this.Coordinator.ParseAssetName(assetName);
// disable caching // disable caching
// This is necessary to avoid assets being shared between content managers, which can // This is necessary to avoid assets being shared between content managers, which can
// cause changes to an asset through one content manager affecting the same asset in // cause changes to an asset through one content manager affecting the same asset in
@ -96,21 +94,21 @@ namespace StardewModdingAPI.Framework.ContentManagers
// resolve managed asset key // resolve managed asset key
{ {
if (this.Coordinator.TryParseManagedAssetKey(parsedName.Name, out string contentManagerID, out string relativePath)) if (this.Coordinator.TryParseManagedAssetKey(assetName.Name, out string contentManagerID, out IAssetName relativePath))
{ {
if (contentManagerID != this.Name) if (contentManagerID != this.Name)
throw new SContentLoadException($"Can't load managed asset key '{parsedName}' through content manager '{this.Name}' for a different mod."); throw new SContentLoadException($"Can't load managed asset key '{assetName}' through content manager '{this.Name}' for a different mod.");
parsedName = this.Coordinator.ParseAssetName(relativePath); assetName = relativePath;
} }
} }
// get local asset // get local asset
SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"Failed loading asset '{parsedName}' from {this.Name}: {reasonPhrase}"); SContentLoadException GetContentError(string reasonPhrase) => new($"Failed loading asset '{assetName}' from {this.Name}: {reasonPhrase}");
T asset; T asset;
try try
{ {
// get file // get file
FileInfo file = this.GetModFile(parsedName.Name); FileInfo file = this.GetModFile(assetName.Name);
if (!file.Exists) if (!file.Exists)
throw GetContentError("the specified path doesn't exist."); throw GetContentError("the specified path doesn't exist.");
@ -122,7 +120,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
{ {
// the underlying content manager adds a .xnb extension implicitly, so // the underlying content manager adds a .xnb extension implicitly, so
// we need to strip it here to avoid trying to load a '.xnb.xnb' file. // we need to strip it here to avoid trying to load a '.xnb.xnb' file.
string loadName = parsedName.Name[..^".xnb".Length]; string loadName = assetName.Name[..^".xnb".Length];
// load asset // load asset
asset = this.RawLoad<T>(loadName, useCache: false); asset = this.RawLoad<T>(loadName, useCache: false);
@ -177,8 +175,8 @@ namespace StardewModdingAPI.Framework.ContentManagers
// fetch & cache // fetch & cache
FormatManager formatManager = FormatManager.Instance; FormatManager formatManager = FormatManager.Instance;
Map map = formatManager.LoadMap(file.FullName); Map map = formatManager.LoadMap(file.FullName);
map.assetPath = parsedName.Name; map.assetPath = assetName.Name;
this.FixTilesheetPaths(map, relativeMapPath: parsedName.Name, fixEagerPathPrefixes: false); this.FixTilesheetPaths(map, relativeMapPath: assetName.Name, fixEagerPathPrefixes: false);
asset = (T)(object)map; asset = (T)(object)map;
} }
break; break;
@ -189,14 +187,20 @@ namespace StardewModdingAPI.Framework.ContentManagers
} }
catch (Exception ex) when (!(ex is SContentLoadException)) catch (Exception ex) when (!(ex is SContentLoadException))
{ {
throw new SContentLoadException($"The content manager failed loading content asset '{parsedName}' from {this.Name}.", ex); throw new SContentLoadException($"The content manager failed loading content asset '{assetName}' from {this.Name}.", ex);
} }
// track & return asset // track & return asset
this.TrackAsset(parsedName.Name, asset, language, useCache); this.TrackAsset(assetName, asset, language, useCache);
return asset; return asset;
} }
/// <inheritdoc />
public override bool IsLoaded(IAssetName assetName, LanguageCode language)
{
return this.Cache.ContainsKey(assetName.Name);
}
/// <inheritdoc /> /// <inheritdoc />
public override LocalizedContentManager CreateTemporary() public override LocalizedContentManager CreateTemporary()
{ {
@ -206,23 +210,19 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <summary>Get the underlying key in the game's content cache for an asset. This does not validate whether the asset exists.</summary> /// <summary>Get the underlying key in the game's content cache for an asset. This does not validate whether the asset exists.</summary>
/// <param name="key">The local path to a content file relative to the mod folder.</param> /// <param name="key">The local path to a content file relative to the mod folder.</param>
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception> /// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
public string GetInternalAssetKey(string key) public IAssetName GetInternalAssetKey(string key)
{ {
FileInfo file = this.GetModFile(key); FileInfo file = this.GetModFile(key);
string relativePath = PathUtilities.GetRelativePath(this.RootDirectory, file.FullName); string relativePath = PathUtilities.GetRelativePath(this.RootDirectory, file.FullName);
return Path.Combine(this.Name, relativePath); string internalKey = Path.Combine(this.Name, relativePath);
return this.Coordinator.ParseAssetName(internalKey);
} }
/********* /*********
** Private methods ** Private methods
*********/ *********/
/// <inheritdoc />
protected override bool IsNormalizedKeyLoaded(string normalizedAssetName, LanguageCode language)
{
return this.Cache.ContainsKey(normalizedAssetName);
}
/// <summary>Get a file from the mod folder.</summary> /// <summary>Get a file from the mod folder.</summary>
/// <param name="path">The asset path relative to the content folder.</param> /// <param name="path">The asset path relative to the content folder.</param>
private FileInfo GetModFile(string path) private FileInfo GetModFile(string path)
@ -304,15 +304,15 @@ namespace StardewModdingAPI.Framework.ContentManagers
// load best match // load best match
try try
{ {
if (!this.TryGetTilesheetAssetName(relativeMapFolder, imageSource, out string assetName, out string error)) if (!this.TryGetTilesheetAssetName(relativeMapFolder, imageSource, out IAssetName assetName, out string error))
throw new SContentLoadException($"{errorPrefix} {error}"); throw new SContentLoadException($"{errorPrefix} {error}");
if (assetName != tilesheet.ImageSource) if (!assetName.IsEquivalentTo(tilesheet.ImageSource))
this.Monitor.VerboseLog($" Mapped tilesheet '{tilesheet.ImageSource}' to '{assetName}'."); this.Monitor.VerboseLog($" Mapped tilesheet '{tilesheet.ImageSource}' to '{assetName}'.");
tilesheet.ImageSource = assetName; tilesheet.ImageSource = assetName.Name;
} }
catch (Exception ex) when (!(ex is SContentLoadException)) catch (Exception ex) when (ex is not SContentLoadException)
{ {
throw new SContentLoadException($"{errorPrefix} The tilesheet couldn't be loaded.", ex); throw new SContentLoadException($"{errorPrefix} The tilesheet couldn't be loaded.", ex);
} }
@ -326,7 +326,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <param name="error">A message indicating why the file couldn't be loaded.</param> /// <param name="error">A message indicating why the file couldn't be loaded.</param>
/// <returns>Returns whether the asset name was found.</returns> /// <returns>Returns whether the asset name was found.</returns>
/// <remarks>See remarks on <see cref="FixTilesheetPaths"/>.</remarks> /// <remarks>See remarks on <see cref="FixTilesheetPaths"/>.</remarks>
private bool TryGetTilesheetAssetName(string modRelativeMapFolder, string relativePath, out string assetName, out string error) private bool TryGetTilesheetAssetName(string modRelativeMapFolder, string relativePath, out IAssetName assetName, out string error)
{ {
assetName = null; assetName = null;
error = null; error = null;
@ -334,7 +334,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
// nothing to do // nothing to do
if (string.IsNullOrWhiteSpace(relativePath)) if (string.IsNullOrWhiteSpace(relativePath))
{ {
assetName = relativePath; assetName = null;
return true; return true;
} }
@ -358,7 +358,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
} }
// get from game assets // get from game assets
string contentKey = this.GetContentKeyForTilesheetImageSource(relativePath); IAssetName contentKey = this.Coordinator.ParseAssetName(this.GetContentKeyForTilesheetImageSource(relativePath));
try try
{ {
this.GameContentManager.Load<Texture2D>(contentKey, this.Language, useCache: true); // no need to bypass cache here, since we're not storing the asset this.GameContentManager.Load<Texture2D>(contentKey, this.Language, useCache: true); // no need to bypass cache here, since we're not storing the asset
@ -374,7 +374,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
// if the content file doesn't exist, that doesn't mean the error here is a // if the content file doesn't exist, that doesn't mean the error here is a
// content-not-found error. Unfortunately XNA doesn't provide a good way to // content-not-found error. Unfortunately XNA doesn't provide a good way to
// detect the error type. // detect the error type.
if (this.GetContentFolderFileExists(contentKey)) if (this.GetContentFolderFileExists(contentKey.Name))
throw; throw;
} }

View File

@ -80,16 +80,18 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <inheritdoc /> /// <inheritdoc />
public T Load<T>(string key, ContentSource source = ContentSource.ModFolder) public T Load<T>(string key, ContentSource source = ContentSource.ModFolder)
{ {
IAssetName assetName = this.ContentCore.ParseAssetName(key);
try try
{ {
this.AssertAndNormalizeAssetName(key); this.AssertAndNormalizeAssetName(key);
switch (source) switch (source)
{ {
case ContentSource.GameContent: case ContentSource.GameContent:
return this.GameContentManager.Load<T>(key, this.CurrentLocaleConstant, useCache: false); return this.GameContentManager.Load<T>(assetName, this.CurrentLocaleConstant, useCache: false);
case ContentSource.ModFolder: case ContentSource.ModFolder:
return this.ModContentManager.Load<T>(key, Constants.DefaultLanguage, useCache: false); return this.ModContentManager.Load<T>(assetName, Constants.DefaultLanguage, useCache: false);
default: default:
throw new SContentLoadException($"{this.ModName} failed loading content asset '{key}' from {source}: unknown content source '{source}'."); throw new SContentLoadException($"{this.ModName} failed loading content asset '{key}' from {source}: unknown content source '{source}'.");
@ -117,7 +119,7 @@ namespace StardewModdingAPI.Framework.ModHelpers
return this.GameContentManager.AssertAndNormalizeAssetName(key); return this.GameContentManager.AssertAndNormalizeAssetName(key);
case ContentSource.ModFolder: case ContentSource.ModFolder:
return this.ModContentManager.GetInternalAssetKey(key); return this.ModContentManager.GetInternalAssetKey(key).Name;
default: default:
throw new NotSupportedException($"Unknown content source '{source}'."); throw new NotSupportedException($"Unknown content source '{source}'.");

View File

@ -45,8 +45,8 @@ namespace StardewModdingAPI.Metadata
/// <summary>Whether to enable more aggressive memory optimizations.</summary> /// <summary>Whether to enable more aggressive memory optimizations.</summary>
private readonly bool AggressiveMemoryOptimizations; private readonly bool AggressiveMemoryOptimizations;
/// <summary>Normalizes an asset key to match the cache key and assert that it's valid.</summary> /// <summary>Parse a raw asset name.</summary>
private readonly Func<string, string> AssertAndNormalizeAssetName; private readonly Func<string, IAssetName> ParseAssetName;
/// <summary>Optimized bucket categories for batch reloading assets.</summary> /// <summary>Optimized bucket categories for batch reloading assets.</summary>
private enum AssetBucket private enum AssetBucket
@ -71,15 +71,15 @@ namespace StardewModdingAPI.Metadata
/// <param name="monitor">Writes messages to the console.</param> /// <param name="monitor">Writes messages to the console.</param>
/// <param name="reflection">Simplifies access to private code.</param> /// <param name="reflection">Simplifies access to private code.</param>
/// <param name="aggressiveMemoryOptimizations">Whether to enable more aggressive memory optimizations.</param> /// <param name="aggressiveMemoryOptimizations">Whether to enable more aggressive memory optimizations.</param>
public CoreAssetPropagator(LocalizedContentManager mainContent, GameContentManagerForAssetPropagation disposableContent, IMonitor monitor, Reflector reflection, bool aggressiveMemoryOptimizations) /// <param name="parseAssetName">Parse a raw asset name.</param>
public CoreAssetPropagator(LocalizedContentManager mainContent, GameContentManagerForAssetPropagation disposableContent, IMonitor monitor, Reflector reflection, bool aggressiveMemoryOptimizations, Func<string, IAssetName> parseAssetName)
{ {
this.MainContentManager = mainContent; this.MainContentManager = mainContent;
this.DisposableContentManager = disposableContent; this.DisposableContentManager = disposableContent;
this.Monitor = monitor; this.Monitor = monitor;
this.Reflection = reflection; this.Reflection = reflection;
this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations; this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations;
this.ParseAssetName = parseAssetName;
this.AssertAndNormalizeAssetName = disposableContent.AssertAndNormalizeAssetName;
} }
/// <summary>Reload one of the game's core assets (if applicable).</summary> /// <summary>Reload one of the game's core assets (if applicable).</summary>
@ -955,13 +955,12 @@ namespace StardewModdingAPI.Metadata
private void ReloadNpcSprites(IEnumerable<IAssetName> keys, IDictionary<IAssetName, bool> propagated) private void ReloadNpcSprites(IEnumerable<IAssetName> keys, IDictionary<IAssetName, bool> propagated)
{ {
// get NPCs // get NPCs
IDictionary<string, IAssetName> lookup = keys.ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
var characters = var characters =
( (
from npc in this.GetCharacters() from npc in this.GetCharacters()
let key = this.NormalizeAssetNameIgnoringEmpty(npc.Sprite?.Texture?.Name) let key = this.ParseAssetNameOrNull(npc.Sprite?.Texture?.Name)
where key != null && lookup.ContainsKey(key) where key != null && propagated.ContainsKey(key)
select new { Npc = npc, AssetName = lookup[key] } select new { Npc = npc, AssetName = key }
) )
.ToArray(); .ToArray();
if (!characters.Any()) if (!characters.Any())
@ -981,26 +980,25 @@ namespace StardewModdingAPI.Metadata
private void ReloadNpcPortraits(IEnumerable<IAssetName> keys, IDictionary<IAssetName, bool> propagated) private void ReloadNpcPortraits(IEnumerable<IAssetName> keys, IDictionary<IAssetName, bool> propagated)
{ {
// get NPCs // get NPCs
IDictionary<string, IAssetName> lookup = keys.ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
var characters = var characters =
( (
from npc in this.GetCharacters() from npc in this.GetCharacters()
where npc.isVillager() where npc.isVillager()
let key = this.NormalizeAssetNameIgnoringEmpty(npc.Portrait?.Name) let key = this.ParseAssetNameOrNull(npc.Portrait?.Name)
where key != null && lookup.ContainsKey(key) where key != null && propagated.ContainsKey(key)
select new { Npc = npc, AssetName = lookup[key] } select new { Npc = npc, AssetName = key }
) )
.ToList(); .ToList();
// special case: Gil is a private NPC field on the AdventureGuild class (only used for the portrait) // special case: Gil is a private NPC field on the AdventureGuild class (only used for the portrait)
{ {
string gilKey = this.NormalizeAssetNameIgnoringEmpty("Portraits/Gil"); IAssetName gilKey = this.ParseAssetName("Portraits/Gil");
if (lookup.TryGetValue(gilKey, out IAssetName assetName)) if (propagated.ContainsKey(gilKey))
{ {
GameLocation adventureGuild = Game1.getLocationFromName("AdventureGuild"); GameLocation adventureGuild = Game1.getLocationFromName("AdventureGuild");
if (adventureGuild != null) if (adventureGuild != null)
characters.Add(new { Npc = this.Reflection.GetField<NPC>(adventureGuild, "Gil").GetValue(), AssetName = assetName }); characters.Add(new { Npc = this.Reflection.GetField<NPC>(adventureGuild, "Gil").GetValue(), AssetName = gilKey });
} }
} }
@ -1234,12 +1232,12 @@ namespace StardewModdingAPI.Metadata
/// <summary>Normalize an asset key to match the cache key and assert that it's valid, but don't raise an error for null or empty values.</summary> /// <summary>Normalize an asset key to match the cache key and assert that it's valid, but don't raise an error for null or empty values.</summary>
/// <param name="path">The asset key to normalize.</param> /// <param name="path">The asset key to normalize.</param>
private string NormalizeAssetNameIgnoringEmpty(string path) private IAssetName ParseAssetNameOrNull(string path)
{ {
if (string.IsNullOrWhiteSpace(path)) if (string.IsNullOrWhiteSpace(path))
return null; return null;
return this.AssertAndNormalizeAssetName(path); return this.ParseAssetName(path);
} }
/// <summary>Get the segments in a path (e.g. 'a/b' is 'a' and 'b').</summary> /// <summary>Get the segments in a path (e.g. 'a/b' is 'a' and 'b').</summary>