From 161618d0d92bad5b31e2780c8899c73339d38b62 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 15 Apr 2019 21:45:27 -0400 Subject: fix cache miss when not playing in English (#634) --- .../ContentManagers/BaseContentManager.cs | 4 +-- .../ContentManagers/GameContentManager.cs | 30 ++++++++++++++++++---- .../Framework/ContentManagers/IContentManager.cs | 5 ++-- .../Framework/ContentManagers/ModContentManager.cs | 4 +-- src/SMAPI/Framework/ModHelpers/ContentHelper.cs | 2 +- 5 files changed, 32 insertions(+), 13 deletions(-) (limited to 'src/SMAPI/Framework') 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 /// The type of asset to inject. /// The asset path relative to the loader root directory, not including the .xnb extension. /// The asset value. - public void Inject(string assetName, T value) + /// The language code for which to inject the asset. + public virtual void Inject(string assetName, T value, LanguageCode language) { assetName = this.AssertAndNormaliseAssetName(assetName); this.Cache[assetName] = value; - } /// Get a copy of the given asset if supported. 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(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; } + /// Inject an asset into the cache. + /// The type of asset to inject. + /// The asset path relative to the loader root directory, not including the .xnb extension. + /// The asset value. + /// The language code for which to inject the asset. + public override void Inject(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); + } + /// Perform any cleanup needed when the locale changes. 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 /// The type of asset to inject. /// The asset path relative to the loader root directory, not including the .xnb extension. /// The asset value. - void Inject(string assetName, T value); + /// The language code for which to inject the asset. + void Inject(string assetName, T value, LocalizedContentManager.LanguageCode language); /// Get a copy of the given asset if supported. /// The asset type. @@ -63,7 +63,6 @@ namespace StardewModdingAPI.Framework.ContentManagers /// Assert that the given key has a valid format and return a normalised form consistent with the underlying cache. /// The asset key to check. /// The asset key is empty or contains invalid characters. - [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")] string AssertAndNormaliseAssetName(string assetName); /// Get the current content locale. 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(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; } -- cgit