summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Framework/ContentManagers/BaseContentManager.cs4
-rw-r--r--src/SMAPI/Framework/ContentManagers/GameContentManager.cs30
-rw-r--r--src/SMAPI/Framework/ContentManagers/IContentManager.cs5
-rw-r--r--src/SMAPI/Framework/ContentManagers/ModContentManager.cs4
-rw-r--r--src/SMAPI/Framework/ModHelpers/ContentHelper.cs2
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;
}