summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-03-26 14:07:16 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-03-26 14:07:16 -0400
commit4c64f9f644c2349d2ca2407ce3aff736ba3fc354 (patch)
tree61528364330c2c2d9551da7543a8d83e4fedfaba /src/SMAPI/Framework/ContentManagers/ModContentManager.cs
parentad8912047beaf84ce34f4918703d55841be13ff0 (diff)
downloadSMAPI-4c64f9f644c2349d2ca2407ce3aff736ba3fc354.tar.gz
SMAPI-4c64f9f644c2349d2ca2407ce3aff736ba3fc354.tar.bz2
SMAPI-4c64f9f644c2349d2ca2407ce3aff736ba3fc354.zip
rewrite content loading to allow handling locale variants (#766, #786, #812)
The game's content pipeline automatically loads localized variants if present. For example, it will try to load "Maps/cave.fr-FR", then "Maps/cave_international", then "Maps/cave". The old content API obfuscates this logic and treats them as interchangeable, which causes edge cases like bundle corruption (#812). This commit rewrites the loading logic to match the game logic when using the new content events, while maintaining the legacy behavior for the old IAssetLoader/IAssetEditor interfaces that'll be removed in SMAPI 4.0.0.
Diffstat (limited to 'src/SMAPI/Framework/ContentManagers/ModContentManager.cs')
-rw-r--r--src/SMAPI/Framework/ContentManagers/ModContentManager.cs32
1 files changed, 8 insertions, 24 deletions
diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
index 90836fcf..375b5e0e 100644
--- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
+++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
@@ -61,6 +61,8 @@ namespace StardewModdingAPI.Framework.ContentManagers
this.GameContentManager = gameContentManager;
this.JsonHelper = jsonHelper;
this.ModName = modName;
+
+ this.TryLocalizeKeys = false;
}
/// <inheritdoc />
@@ -80,14 +82,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
}
/// <inheritdoc />
- public override T Load<T>(string assetName, LanguageCode language)
- {
- IAssetName parsedName = this.Coordinator.ParseAssetName(assetName);
- return this.Load<T>(parsedName, language, useCache: false);
- }
-
- /// <inheritdoc />
- public override T Load<T>(IAssetName assetName, LanguageCode language, bool useCache)
+ public override T LoadExact<T>(IAssetName assetName, bool useCache)
{
// disable caching
// This is necessary to avoid assets being shared between content managers, which can
@@ -97,11 +92,6 @@ namespace StardewModdingAPI.Framework.ContentManagers
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 != this.DefaultLanguage)
- throw new InvalidOperationException("Localized assets aren't supported by the mod content manager.");
-
// resolve managed asset key
{
if (this.Coordinator.TryParseManagedAssetKey(assetName.Name, out string contentManagerID, out IAssetName relativePath))
@@ -130,14 +120,14 @@ namespace StardewModdingAPI.Framework.ContentManagers
{
// 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.
- string loadName = assetName.Name[..^".xnb".Length];
+ IAssetName loadName = this.Coordinator.ParseAssetName(assetName.Name[..^".xnb".Length]);
// load asset
asset = this.RawLoad<T>(loadName, useCache: false);
if (asset is Map map)
{
- map.assetPath = loadName;
- this.FixTilesheetPaths(map, relativeMapPath: loadName, fixEagerPathPrefixes: true);
+ map.assetPath = loadName.Name;
+ this.FixTilesheetPaths(map, relativeMapPath: loadName.Name, fixEagerPathPrefixes: true);
}
}
break;
@@ -201,17 +191,11 @@ namespace StardewModdingAPI.Framework.ContentManagers
}
// track & return asset
- this.TrackAsset(assetName, asset, language, useCache);
+ this.TrackAsset(assetName, asset, useCache);
return asset;
}
/// <inheritdoc />
- public override bool IsLoaded(IAssetName assetName, LanguageCode language)
- {
- return this.Cache.ContainsKey(assetName.Name);
- }
-
- /// <inheritdoc />
public override LocalizedContentManager CreateTemporary()
{
throw new NotSupportedException("Can't create a temporary mod content manager.");
@@ -371,7 +355,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
IAssetName contentKey = this.Coordinator.ParseAssetName(this.GetContentKeyForTilesheetImageSource(relativePath));
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.LoadLocalized<Texture2D>(contentKey, this.GameContentManager.Language, useCache: true); // no need to bypass cache here, since we're not storing the asset
assetName = contentKey;
return true;
}