From 87d5288287520f750651667839ebbe7bbeb97bba Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 6 May 2022 18:05:40 -0400 Subject: fix content managers' LoadBaseString not recognizing localized mod assets --- .../ContentManagers/BaseContentManager.cs | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/SMAPI/Framework/ContentManagers') diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index db2934a0..e4695588 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -110,6 +110,26 @@ namespace StardewModdingAPI.Framework.ContentManagers return this.Load(assetName, LanguageCode.en); } + /// + [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse", Justification = "Copied as-is from game code")] + public sealed override string LoadBaseString(string path) + { + try + { + // copied as-is from LocalizedContentManager.LoadBaseString + // This is only changed to call this.Load instead of base.Load, to support mod assets + this.ParseStringPath(path, out string assetName, out string key); + Dictionary strings = this.Load>(assetName, LanguageCode.en); + return strings != null && strings.ContainsKey(key) + ? this.GetString(strings, key) + : path; + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed loading string path '{path}' from '{this.Name}'.", ex); + } + } + /// public sealed override T Load(string assetName) { @@ -331,5 +351,34 @@ namespace StardewModdingAPI.Framework.ContentManagers // avoid hard disposable references; see remarks on the field this.BaseDisposableReferences.Clear(); } + + /**** + ** Private methods copied from the game code + ****/ +#pragma warning disable CS1574 // can't be resolved: the reference is valid but private + /// Parse a string path like assetName:key. + /// The string path. + /// The extracted asset name. + /// The extracted entry key. + /// The string path is not in a valid format. + /// This is copied as-is from . + private void ParseStringPath(string path, out string assetName, out string key) + { + int length = path.IndexOf(':'); + assetName = length != -1 ? path.Substring(0, length) : throw new ContentLoadException("Unable to parse string path: " + path); + key = path.Substring(length + 1, path.Length - length - 1); + } + + /// Get a string value from a dictionary asset. + /// The asset to read. + /// The string key to find. + /// This is copied as-is from . + private string GetString(Dictionary strings, string key) + { + return strings.TryGetValue(key + ".desktop", out string? str) + ? str + : strings[key]; + } +#pragma warning restore CS1574 } } -- cgit