disable mod-level asset caching (#644)

This fixes an issue where some asset references could be shared between content managers, causing changes to propagate unintentionally.
This commit is contained in:
Jesse Plamondon-Willard 2019-05-30 17:40:21 -04:00
parent 202ba23dcc
commit b9dec73469
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
7 changed files with 261 additions and 205 deletions

View File

@ -20,7 +20,6 @@ These changes have not been released yet.
* Fixed lag when a mod invalidates many NPC portraits/sprites at once. * Fixed lag when a mod invalidates many NPC portraits/sprites at once.
* Fixed map reloads resetting tilesheet seasons. * Fixed map reloads resetting tilesheet seasons.
* Fixed outdoor tilesheets being seasonalised when added to an indoor location. * Fixed outdoor tilesheets being seasonalised when added to an indoor location.
* Fixed mods able to directly load (and in some cases edit) a different mod's local assets using internal asset key forwarding.
* For modders: * For modders:
* Added support for content pack translations. * Added support for content pack translations.
@ -52,6 +51,8 @@ Released 13 September 2019 for Stardew Valley 1.3.36.
* For modders: * For modders:
* `this.Monitor.Log` now defaults to the `Trace` log level instead of `Debug`. The change will only take effect when you recompile the mod. * `this.Monitor.Log` now defaults to the `Trace` log level instead of `Debug`. The change will only take effect when you recompile the mod.
* Fixed 'location list changed' verbose log not correctly listing changes. * Fixed 'location list changed' verbose log not correctly listing changes.
* Fixed mods able to directly load (and in some cases edit) a different mod's local assets using internal asset key forwarding.
* Fixed changes to a map loaded by a mod being persisted across content managers.
## 2.11.2 ## 2.11.2
Released 23 April 2019 for Stardew Valley 1.3.36. Released 23 April 2019 for Stardew Valley 1.3.36.

View File

@ -176,16 +176,15 @@ namespace StardewModdingAPI.Framework
/// <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 internal SMAPI asset key.</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>
public T LoadAndCloneManagedAsset<T>(string internalKey, string contentManagerID, string relativePath, LocalizedContentManager.LanguageCode language) public T LoadManagedAsset<T>(string internalKey, string contentManagerID, string relativePath, LocalizedContentManager.LanguageCode language)
{ {
// get content manager // get content manager
IContentManager contentManager = this.ContentManagers.FirstOrDefault(p => p.IsNamespaced && p.Name == contentManagerID); IContentManager contentManager = this.ContentManagers.FirstOrDefault(p => p.IsNamespaced && p.Name == contentManagerID);
if (contentManager == null) if (contentManager == null)
throw new InvalidOperationException($"The '{contentManagerID}' prefix isn't handled by any mod."); throw new InvalidOperationException($"The '{contentManagerID}' prefix isn't handled by any mod.");
// get cloned asset // get fresh asset
T data = contentManager.Load<T>(internalKey, language); return contentManager.Load<T>(relativePath, language, useCache: false);
return contentManager.CloneIfPossible(data);
} }
/// <summary>Purge assets from the cache that match one of the interceptors.</summary> /// <summary>Purge assets from the cache that match one of the interceptors.</summary>

View File

@ -6,7 +6,6 @@ using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Content; using StardewModdingAPI.Framework.Content;
using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Framework.Reflection;
@ -38,6 +37,9 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <summary>The language enum values indexed by locale code.</summary> /// <summary>The language enum values indexed by locale code.</summary>
protected IDictionary<string, LanguageCode> LanguageCodes { get; } protected IDictionary<string, LanguageCode> LanguageCodes { get; }
/// <summary>A list of disposable assets.</summary>
private readonly List<WeakReference<IDisposable>> Disposables = new List<WeakReference<IDisposable>>();
/********* /*********
** Accessors ** Accessors
@ -88,54 +90,32 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <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>
public override T Load<T>(string assetName) public override T Load<T>(string assetName)
{ {
return this.Load<T>(assetName, LocalizedContentManager.CurrentLanguageCode); return this.Load<T>(assetName, this.Language, useCache: true);
} }
/// <summary>Load an asset that has been processed by the content pipeline.</summary>
/// <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="language">The language code for which to load content.</param>
public override T Load<T>(string assetName, LanguageCode language)
{
return this.Load<T>(assetName, language, useCache: true);
}
/// <summary>Load an asset that has been processed by the content pipeline.</summary>
/// <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="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>
public abstract T Load<T>(string assetName, LocalizedContentManager.LanguageCode language, bool useCache);
/// <summary>Load the base asset without localisation.</summary> /// <summary>Load the base asset without localisation.</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 path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
[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) public override T LoadBase<T>(string assetName)
{ {
return this.Load<T>(assetName, LanguageCode.en); return this.Load<T>(assetName, LanguageCode.en, useCache: true);
}
/// <summary>Inject an asset into the cache.</summary>
/// <typeparam name="T">The type of asset to inject.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <param name="value">The asset value.</param>
/// <param name="language">The language code for which to inject the asset.</param>
public virtual void Inject<T>(string assetName, T value, LanguageCode language)
{
assetName = this.AssertAndNormaliseAssetName(assetName);
this.Cache[assetName] = value;
}
/// <summary>Get a copy of the given asset if supported.</summary>
/// <typeparam name="T">The asset type.</typeparam>
/// <param name="asset">The asset to clone.</param>
public T CloneIfPossible<T>(T asset)
{
switch (asset as object)
{
case Texture2D source:
{
int[] pixels = new int[source.Width * source.Height];
source.GetData(pixels);
Texture2D clone = new Texture2D(source.GraphicsDevice, source.Width, source.Height);
clone.SetData(pixels);
return (T)(object)clone;
}
case Dictionary<string, string> source:
return (T)(object)new Dictionary<string, string>(source);
case Dictionary<int, string> source:
return (T)(object)new Dictionary<int, string>(source);
default:
return asset;
}
} }
/// <summary>Perform any cleanup needed when the locale changes.</summary> /// <summary>Perform any cleanup needed when the locale changes.</summary>
@ -228,11 +208,28 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <param name="isDisposing">Whether the content manager is being disposed (rather than finalized).</param> /// <param name="isDisposing">Whether the content manager is being disposed (rather than finalized).</param>
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
// ignore if disposed
if (this.IsDisposed) if (this.IsDisposed)
return; return;
this.IsDisposed = true; this.IsDisposed = true;
// dispose uncached assets
foreach (WeakReference<IDisposable> reference in this.Disposables)
{
if (reference.TryGetTarget(out IDisposable disposable))
{
try
{
disposable.Dispose();
}
catch { /* ignore dispose errors */ }
}
}
this.Disposables.Clear();
// raise event
this.OnDisposing(this); this.OnDisposing(this);
base.Dispose(isDisposing); base.Dispose(isDisposing);
} }
@ -249,23 +246,26 @@ namespace StardewModdingAPI.Framework.ContentManagers
/********* /*********
** Private methods ** Private methods
*********/ *********/
/// <summary>Get the locale codes (like <c>ja-JP</c>) used in asset keys.</summary> /// <summary>Load an asset file directly from the underlying content manager.</summary>
private IDictionary<LanguageCode, string> GetKeyLocales() /// <typeparam name="T">The type of asset to load.</typeparam>
/// <param name="assetName">The normalised asset key.</param>
/// <param name="useCache">Whether to read/write the loaded asset to the asset cache.</param>
protected virtual T RawLoad<T>(string assetName, bool useCache)
{ {
// create locale => code map return useCache
IDictionary<LanguageCode, string> map = new Dictionary<LanguageCode, string>(); ? base.LoadBase<T>(assetName)
foreach (LanguageCode code in Enum.GetValues(typeof(LanguageCode))) : base.ReadAsset<T>(assetName, disposable => this.Disposables.Add(new WeakReference<IDisposable>(disposable)));
map[code] = this.GetLocale(code);
return map;
} }
/// <summary>Get the asset name from a cache key.</summary> /// <summary>Inject an asset into the cache.</summary>
/// <param name="cacheKey">The input cache key.</param> /// <typeparam name="T">The type of asset to inject.</typeparam>
private string GetAssetName(string cacheKey) /// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <param name="value">The asset value.</param>
/// <param name="language">The language code for which to inject the asset.</param>
protected virtual void Inject<T>(string assetName, T value, LanguageCode language)
{ {
this.ParseCacheKey(cacheKey, out string assetName, out string _); assetName = this.AssertAndNormaliseAssetName(assetName);
return assetName; this.Cache[assetName] = value;
} }
/// <summary>Parse a cache key into its component parts.</summary> /// <summary>Parse a cache key into its component parts.</summary>
@ -298,5 +298,24 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <summary>Get whether an asset has already been loaded.</summary> /// <summary>Get whether an asset has already been loaded.</summary>
/// <param name="normalisedAssetName">The normalised asset name.</param> /// <param name="normalisedAssetName">The normalised asset name.</param>
protected abstract bool IsNormalisedKeyLoaded(string normalisedAssetName); protected abstract bool IsNormalisedKeyLoaded(string normalisedAssetName);
/// <summary>Get the locale codes (like <c>ja-JP</c>) used in asset keys.</summary>
private IDictionary<LanguageCode, string> GetKeyLocales()
{
// create locale => code map
IDictionary<LanguageCode, string> map = new Dictionary<LanguageCode, string>();
foreach (LanguageCode code in Enum.GetValues(typeof(LanguageCode)))
map[code] = this.GetLocale(code);
return map;
}
/// <summary>Get the asset name from a cache key.</summary>
/// <param name="cacheKey">The input cache key.</param>
private string GetAssetName(string cacheKey)
{
this.ParseCacheKey(cacheKey, out string assetName, out string _);
return assetName;
}
} }
} }

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework.Content;
using StardewModdingAPI.Framework.Content; using StardewModdingAPI.Framework.Content;
using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Framework.Reflection;
@ -59,7 +60,8 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <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 path relative to the loader root directory, not including the <c>.xnb</c> extension.</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>
public override T Load<T>(string assetName, LanguageCode language) /// <param name="useCache">Whether to read/write the loaded asset to the asset cache.</param>
public override T Load<T>(string assetName, LocalizedContentManager.LanguageCode language, bool useCache)
{ {
// raise first-load callback // raise first-load callback
if (GameContentManager.IsFirstLoad) if (GameContentManager.IsFirstLoad)
@ -71,17 +73,18 @@ namespace StardewModdingAPI.Framework.ContentManagers
// normalise asset name // normalise asset name
assetName = this.AssertAndNormaliseAssetName(assetName); assetName = this.AssertAndNormaliseAssetName(assetName);
if (this.TryParseExplicitLanguageAssetKey(assetName, out string newAssetName, out LanguageCode newLanguage)) if (this.TryParseExplicitLanguageAssetKey(assetName, out string newAssetName, out LanguageCode newLanguage))
return this.Load<T>(newAssetName, newLanguage); return this.Load<T>(newAssetName, newLanguage, useCache);
// get from cache // get from cache
if (this.IsLoaded(assetName)) if (useCache && this.IsLoaded(assetName))
return base.Load<T>(assetName, language); return this.RawLoad<T>(assetName, language, useCache: true);
// get managed asset // get managed asset
if (this.Coordinator.TryParseManagedAssetKey(assetName, out string contentManagerID, out string relativePath)) if (this.Coordinator.TryParseManagedAssetKey(assetName, out string contentManagerID, out string relativePath))
{ {
T managedAsset = this.Coordinator.LoadAndCloneManagedAsset<T>(assetName, contentManagerID, relativePath, language); T managedAsset = this.Coordinator.LoadManagedAsset<T>(assetName, contentManagerID, relativePath, language);
this.Inject(assetName, managedAsset, language); if (useCache)
this.Inject(assetName, managedAsset, language);
return managedAsset; return managedAsset;
} }
@ -91,7 +94,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
{ {
this.Monitor.Log($"Broke loop while loading asset '{assetName}'.", 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}", LogLevel.Trace); this.Monitor.Log($"Bypassing mod loaders for this asset. Stack trace:\n{Environment.StackTrace}", LogLevel.Trace);
data = base.Load<T>(assetName, language); data = this.RawLoad<T>(assetName, language, useCache);
} }
else else
{ {
@ -101,7 +104,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
IAssetInfo info = new AssetInfo(locale, assetName, typeof(T), this.AssertAndNormaliseAssetName); IAssetInfo info = new AssetInfo(locale, assetName, typeof(T), this.AssertAndNormaliseAssetName);
IAssetData asset = IAssetData asset =
this.ApplyLoader<T>(info) this.ApplyLoader<T>(info)
?? new AssetDataForObject(info, base.Load<T>(assetName, language), this.AssertAndNormaliseAssetName); ?? new AssetDataForObject(info, this.RawLoad<T>(assetName, language, useCache), this.AssertAndNormaliseAssetName);
asset = this.ApplyEditors<T>(info, asset); asset = this.ApplyEditors<T>(info, asset);
return (T)asset.Data; return (T)asset.Data;
}); });
@ -112,39 +115,6 @@ namespace StardewModdingAPI.Framework.ContentManagers
return data; return data;
} }
/// <summary>Inject an asset into the cache.</summary>
/// <typeparam name="T">The type of asset to inject.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <param name="value">The asset value.</param>
/// <param name="language">The language code for which to inject the asset.</param>
public override void Inject<T>(string assetName, T value, LanguageCode language)
{
// handle explicit language in asset name
{
if (this.TryParseExplicitLanguageAssetKey(assetName, out string newAssetName, out LanguageCode newLanguage))
{
this.Inject(newAssetName, value, newLanguage);
return;
}
}
base.Inject(assetName, value, language);
// track whether the injected asset is translatable for is-loaded lookups
string keyWithLocale = $"{assetName}.{this.GetLocale(language)}";
if (this.Cache.ContainsKey(keyWithLocale))
{
this.IsLocalisableLookup[assetName] = true;
this.IsLocalisableLookup[keyWithLocale] = true;
}
else if (this.Cache.ContainsKey(assetName))
{
this.IsLocalisableLookup[assetName] = false;
this.IsLocalisableLookup[keyWithLocale] = false;
}
else
this.Monitor.Log($"Asset '{assetName}' could not be found in the cache immediately after injection.", LogLevel.Error);
}
/// <summary>Perform any cleanup needed when the locale changes.</summary> /// <summary>Perform any cleanup needed when the locale changes.</summary>
public override void OnLocaleChanged() public override void OnLocaleChanged()
{ {
@ -199,6 +169,72 @@ namespace StardewModdingAPI.Framework.ContentManagers
return false; return false;
} }
/// <summary>Inject an asset into the cache.</summary>
/// <typeparam name="T">The type of asset to inject.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <param name="value">The asset value.</param>
/// <param name="language">The language code for which to inject the asset.</param>
protected override void Inject<T>(string assetName, T value, LanguageCode language)
{
// handle explicit language in asset name
{
if (this.TryParseExplicitLanguageAssetKey(assetName, out string newAssetName, out LanguageCode newLanguage))
{
this.Inject(newAssetName, value, newLanguage);
return;
}
}
base.Inject(assetName, value, language);
// track whether the injected asset is translatable for is-loaded lookups
string keyWithLocale = $"{assetName}.{this.GetLocale(language)}";
if (this.Cache.ContainsKey(keyWithLocale))
{
this.IsLocalisableLookup[assetName] = true;
this.IsLocalisableLookup[keyWithLocale] = true;
}
else if (this.Cache.ContainsKey(assetName))
{
this.IsLocalisableLookup[assetName] = false;
this.IsLocalisableLookup[keyWithLocale] = false;
}
else
this.Monitor.Log($"Asset '{assetName}' could not be found in the cache immediately after injection.", LogLevel.Error);
}
/// <summary>Load an asset file directly from the underlying content manager.</summary>
/// <typeparam name="T">The type of asset to load.</typeparam>
/// <param name="assetName">The normalised asset key.</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>
/// <remarks>Derived from <see cref="LocalizedContentManager.Load{T}(string, LocalizedContentManager.LanguageCode)"/>.</remarks>
private T RawLoad<T>(string assetName, LanguageCode language, bool useCache)
{
// try translated asset
if (language != LocalizedContentManager.LanguageCode.en)
{
string translatedKey = $"{assetName}.{this.GetLocale(language)}";
if (!this.IsLocalisableLookup.TryGetValue(translatedKey, out bool isTranslatable) || isTranslatable)
{
try
{
T obj = base.RawLoad<T>(translatedKey, useCache);
this.IsLocalisableLookup[assetName] = true;
this.IsLocalisableLookup[translatedKey] = true;
return obj;
}
catch (ContentLoadException)
{
this.IsLocalisableLookup[assetName] = false;
this.IsLocalisableLookup[translatedKey] = false;
}
}
}
// try base asset
return base.RawLoad<T>(assetName, useCache);
}
/// <summary>Parse an asset key that contains an explicit language into its asset name and language, if applicable.</summary> /// <summary>Parse an asset key that contains an explicit language into its asset name and language, if applicable.</summary>
/// <param name="rawAsset">The asset key to parse.</param> /// <param name="rawAsset">The asset key to parse.</param>
/// <param name="assetName">The asset name without the language code.</param> /// <param name="assetName">The asset name without the language code.</param>
@ -260,7 +296,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
T data; T data;
try try
{ {
data = this.CloneIfPossible(loader.Load<T>(info)); data = loader.Load<T>(info);
this.Monitor.Log($"{mod.DisplayName} loaded asset '{info.AssetName}'.", LogLevel.Trace); this.Monitor.Log($"{mod.DisplayName} loaded asset '{info.AssetName}'.", LogLevel.Trace);
} }
catch (Exception ex) catch (Exception ex)

View File

@ -29,28 +29,12 @@ namespace StardewModdingAPI.Framework.ContentManagers
/********* /*********
** Methods ** Methods
*********/ *********/
/// <summary>Load an asset that has been processed by the content pipeline.</summary>
/// <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>
T Load<T>(string assetName);
/// <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 path relative to the loader root directory, not including the <c>.xnb</c> extension.</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>
T Load<T>(string assetName, LocalizedContentManager.LanguageCode language); /// <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);
/// <summary>Inject an asset into the cache.</summary>
/// <typeparam name="T">The type of asset to inject.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <param name="value">The asset value.</param>
/// <param name="language">The language code for which to inject the asset.</param>
void Inject<T>(string assetName, T value, LocalizedContentManager.LanguageCode language);
/// <summary>Get a copy of the given asset if supported.</summary>
/// <typeparam name="T">The asset type.</typeparam>
/// <param name="asset">The asset to clone.</param>
T CloneIfPossible<T>(T asset);
/// <summary>Perform any cleanup needed when the locale changes.</summary> /// <summary>Perform any cleanup needed when the locale changes.</summary>
void OnLocaleChanged(); void OnLocaleChanged();

View File

@ -29,6 +29,9 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <summary>The game content manager used for map tilesheets not provided by the mod.</summary> /// <summary>The game content manager used for map tilesheets not provided by the mod.</summary>
private readonly IContentManager GameContentManager; private readonly IContentManager GameContentManager;
/// <summary>The language code for language-agnostic mod assets.</summary>
private const LanguageCode NoLanguage = LanguageCode.en;
/********* /*********
** Public methods ** Public methods
@ -51,27 +54,115 @@ namespace StardewModdingAPI.Framework.ContentManagers
this.JsonHelper = jsonHelper; this.JsonHelper = jsonHelper;
} }
/// <summary>Load an asset that has been processed by the content pipeline.</summary>
/// <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>
public override T Load<T>(string assetName)
{
return this.Load<T>(assetName, ModContentManager.NoLanguage, useCache: false);
}
/// <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 path relative to the loader root directory, not including the <c>.xnb</c> extension.</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>
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);
}
/// <summary>Load an asset that has been processed by the content pipeline.</summary>
/// <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="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>
public override T Load<T>(string assetName, LanguageCode language, bool useCache)
{ {
assetName = this.AssertAndNormaliseAssetName(assetName); assetName = this.AssertAndNormaliseAssetName(assetName);
// disable caching
// 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
// others (or even fresh content managers). See https://www.patreon.com/posts/27247161
// for more background info.
if (useCache)
throw new InvalidOperationException("Mod content managers don't support asset caching.");
// disable language handling
// Mod files don't support automatic translation logic, so this should never happen.
if (language != ModContentManager.NoLanguage)
throw new InvalidOperationException("Caching is not supported by the mod content manager.");
// resolve managed asset key // resolve managed asset key
if (this.Coordinator.TryParseManagedAssetKey(assetName, out string contentManagerID, out string relativePath))
{ {
if (contentManagerID != this.Name) if (this.Coordinator.TryParseManagedAssetKey(assetName, out string contentManagerID, out string relativePath))
throw new SContentLoadException($"Can't load managed asset key '{assetName}' through content manager '{this.Name}' for a different mod."); {
assetName = relativePath; if (contentManagerID != this.Name)
throw new SContentLoadException($"Can't load managed asset key '{assetName}' through content manager '{this.Name}' for a different mod.");
assetName = relativePath;
}
} }
// get local asset // get local asset
string internalKey = this.GetInternalAssetKey(assetName); SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"Failed loading asset '{assetName}' from {this.Name}: {reasonPhrase}");
if (this.IsLoaded(internalKey)) try
return base.Load<T>(internalKey, language); {
return this.LoadImpl<T>(internalKey, this.Name, assetName, this.Language); // get file
FileInfo file = this.GetModFile(assetName);
if (!file.Exists)
throw GetContentError("the specified path doesn't exist.");
// load content
switch (file.Extension.ToLower())
{
// XNB file
case ".xnb":
return this.RawLoad<T>(assetName, useCache: false);
// unpacked data
case ".json":
{
if (!this.JsonHelper.ReadJsonFileIfExists(file.FullName, out T data))
throw GetContentError("the JSON file is invalid."); // should never happen since we check for file existence above
return data;
}
// unpacked image
case ".png":
// validate
if (typeof(T) != typeof(Texture2D))
throw GetContentError($"can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'.");
// fetch & cache
using (FileStream stream = File.OpenRead(file.FullName))
{
Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
texture = this.PremultiplyTransparency(texture);
return (T)(object)texture;
}
// unpacked map
case ".tbin":
// validate
if (typeof(T) != typeof(Map))
throw GetContentError($"can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Map)}'.");
// fetch & cache
FormatManager formatManager = FormatManager.Instance;
Map map = formatManager.LoadMap(file.FullName);
this.FixCustomTilesheetPaths(map, relativeMapPath: assetName);
return (T)(object)map;
default:
throw GetContentError($"unknown file extension '{file.Extension}'; must be one of '.json', '.png', '.tbin', or '.xnb'.");
}
}
catch (Exception ex) when (!(ex is SContentLoadException))
{
if (ex.GetInnermostException() is DllNotFoundException dllEx && dllEx.Message == "libgdiplus.dylib")
throw GetContentError("couldn't find libgdiplus, which is needed to load mod images. Make sure Mono is installed and you're running the game through the normal launcher.");
throw new SContentLoadException($"The content manager failed loading content asset '{assetName}' from {this.Name}.", ex);
}
} }
/// <summary>Create a new content manager for temporary use.</summary> /// <summary>Create a new content manager for temporary use.</summary>
@ -101,80 +192,6 @@ namespace StardewModdingAPI.Framework.ContentManagers
return this.Cache.ContainsKey(normalisedAssetName); return this.Cache.ContainsKey(normalisedAssetName);
} }
/// <summary>Load a local mod asset.</summary>
/// <typeparam name="T">The type of asset to load.</typeparam>
/// <param name="cacheKey">The mod asset cache key to save.</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="language">The language code for which to load content.</param>
private T LoadImpl<T>(string cacheKey, string contentManagerID, string relativePath, LanguageCode language)
{
SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"Failed loading asset '{relativePath}' from {contentManagerID}: {reasonPhrase}");
try
{
// get file
FileInfo file = this.GetModFile(relativePath);
if (!file.Exists)
throw GetContentError("the specified path doesn't exist.");
// load content
switch (file.Extension.ToLower())
{
// XNB file
case ".xnb":
return base.Load<T>(relativePath, language);
// unpacked data
case ".json":
{
if (!this.JsonHelper.ReadJsonFileIfExists(file.FullName, out T data))
throw GetContentError("the JSON file is invalid."); // should never happen since we check for file existence above
return data;
}
// unpacked image
case ".png":
// validate
if (typeof(T) != typeof(Texture2D))
throw GetContentError($"can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'.");
// fetch & cache
using (FileStream stream = File.OpenRead(file.FullName))
{
Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
texture = this.PremultiplyTransparency(texture);
this.Inject(cacheKey, texture, language);
return (T)(object)texture;
}
// unpacked map
case ".tbin":
// validate
if (typeof(T) != typeof(Map))
throw GetContentError($"can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Map)}'.");
// fetch & cache
FormatManager formatManager = FormatManager.Instance;
Map map = formatManager.LoadMap(file.FullName);
this.FixCustomTilesheetPaths(map, relativeMapPath: relativePath);
// inject map
this.Inject(cacheKey, map, this.Language);
return (T)(object)map;
default:
throw GetContentError($"unknown file extension '{file.Extension}'; must be one of '.json', '.png', '.tbin', or '.xnb'.");
}
}
catch (Exception ex) when (!(ex is SContentLoadException))
{
if (ex.GetInnermostException() is DllNotFoundException dllEx && dllEx.Message == "libgdiplus.dylib")
throw GetContentError("couldn't find libgdiplus, which is needed to load mod images. Make sure Mono is installed and you're running the game through the normal launcher.");
throw new SContentLoadException($"The content manager failed loading content asset '{relativePath}' from {contentManagerID}.", ex);
}
}
/// <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)
@ -318,7 +335,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
try try
{ {
this.GameContentManager.Load<Texture2D>(contentKey); this.GameContentManager.Load<Texture2D>(contentKey, this.Language, useCache: true); // no need to bypass cache here, since we're not storing the asset
return contentKey; return contentKey;
} }
catch catch

View File

@ -91,10 +91,10 @@ namespace StardewModdingAPI.Framework.ModHelpers
switch (source) switch (source)
{ {
case ContentSource.GameContent: case ContentSource.GameContent:
return this.GameContentManager.Load<T>(key); return this.GameContentManager.Load<T>(key, this.CurrentLocaleConstant, useCache: false);
case ContentSource.ModFolder: case ContentSource.ModFolder:
return this.ModContentManager.Load<T>(key); return this.ModContentManager.Load<T>(key, this.CurrentLocaleConstant, 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}'.");