diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-04-15 21:45:27 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-14 17:14:32 -0400 |
commit | 161618d0d92bad5b31e2780c8899c73339d38b62 (patch) | |
tree | 55ad574614dbff4396cc244f5215580c161eab60 /src/SMAPI/Framework | |
parent | 6c220453e16c6cb5ad150b61cf02685a97557b3c (diff) | |
download | SMAPI-161618d0d92bad5b31e2780c8899c73339d38b62.tar.gz SMAPI-161618d0d92bad5b31e2780c8899c73339d38b62.tar.bz2 SMAPI-161618d0d92bad5b31e2780c8899c73339d38b62.zip |
fix cache miss when not playing in English (#634)
Diffstat (limited to 'src/SMAPI/Framework')
5 files changed, 32 insertions, 13 deletions
diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index b2b3769b..f54262b8 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -103,11 +103,11 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <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> - public void Inject<T>(string assetName, T value) + /// <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> diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs index 55cf15ec..6ce50f00 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -81,7 +81,7 @@ namespace StardewModdingAPI.Framework.ContentManagers if (this.Coordinator.TryParseManagedAssetKey(assetName, out string contentManagerID, out string relativePath)) { T managedAsset = this.Coordinator.LoadAndCloneManagedAsset<T>(assetName, contentManagerID, relativePath, language); - this.Inject(assetName, managedAsset); + this.Inject(assetName, managedAsset, language); return managedAsset; } @@ -108,10 +108,30 @@ namespace StardewModdingAPI.Framework.ContentManagers } // update cache & return data - this.Inject(assetName, data); + this.Inject(assetName, data, language); 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) + { + base.Inject(assetName, value, language); + + // track whether the injected asset is translatable for is-loaded lookups + bool isTranslated = this.TryParseExplicitLanguageAssetKey(assetName, out _, out _); + string localisedKey = isTranslated ? assetName : $"{assetName}.{language}"; + if (this.Cache.ContainsKey(localisedKey)) + this.IsLocalisableLookup[localisedKey] = true; + else if (!isTranslated && this.Cache.ContainsKey(assetName)) + this.IsLocalisableLookup[localisedKey] = 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> public override void OnLocaleChanged() { @@ -154,11 +174,11 @@ namespace StardewModdingAPI.Framework.ContentManagers return this.Cache.ContainsKey(normalisedAssetName); // translated - string localeKey = $"{normalisedAssetName}.{this.GetLocale(this.GetCurrentLanguage())}"; - if (this.IsLocalisableLookup.TryGetValue(localeKey, out bool localisable)) + string localisedKey = $"{normalisedAssetName}.{this.GetLocale(this.GetCurrentLanguage())}"; + if (this.IsLocalisableLookup.TryGetValue(localisedKey, out bool localisable)) { return localisable - ? this.Cache.ContainsKey(localeKey) + ? this.Cache.ContainsKey(localisedKey) : this.Cache.ContainsKey(normalisedAssetName); } diff --git a/src/SMAPI/Framework/ContentManagers/IContentManager.cs b/src/SMAPI/Framework/ContentManagers/IContentManager.cs index 66ef9181..4c362b0a 100644 --- a/src/SMAPI/Framework/ContentManagers/IContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/IContentManager.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using Microsoft.Xna.Framework.Content; using StardewModdingAPI.Framework.Exceptions; @@ -45,7 +44,8 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <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> - void Inject<T>(string assetName, T value); + /// <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> @@ -63,7 +63,6 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <summary>Assert that the given key has a valid format and return a normalised form consistent with the underlying cache.</summary> /// <param name="assetName">The asset key to check.</param> /// <exception cref="SContentLoadException">The asset key is empty or contains invalid characters.</exception> - [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")] string AssertAndNormaliseAssetName(string assetName); /// <summary>Get the current content locale.</summary> diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index 2c50ec04..1df7d107 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -57,7 +57,7 @@ namespace StardewModdingAPI.Framework.ContentManagers if (contentManagerID != this.Name) { T data = this.Coordinator.LoadAndCloneManagedAsset<T>(assetName, contentManagerID, relativePath, language); - this.Inject(assetName, data); + this.Inject(assetName, data, language); return data; } @@ -127,7 +127,7 @@ namespace StardewModdingAPI.Framework.ContentManagers { Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream); texture = this.PremultiplyTransparency(texture); - this.Inject(internalKey, texture); + this.Inject(internalKey, texture, language); return (T)(object)texture; } diff --git a/src/SMAPI/Framework/ModHelpers/ContentHelper.cs b/src/SMAPI/Framework/ModHelpers/ContentHelper.cs index 8b86fdeb..e02748d2 100644 --- a/src/SMAPI/Framework/ModHelpers/ContentHelper.cs +++ b/src/SMAPI/Framework/ModHelpers/ContentHelper.cs @@ -126,7 +126,7 @@ namespace StardewModdingAPI.Framework.ModHelpers this.FixCustomTilesheetPaths(map, relativeMapPath: key); // inject map - this.ModContentManager.Inject(internalKey, map); + this.ModContentManager.Inject(internalKey, map, this.CurrentLocaleConstant); return (T)(object)map; } |