diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2018-12-07 13:40:44 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2018-12-07 13:40:44 -0500 |
commit | a78b1935928919694dfe8de823a1accd6d222732 (patch) | |
tree | 3f17b6087cf2749e52c1e237de17e2e9addb6c06 /src/SMAPI/Framework/ContentManagers | |
parent | 4cd9eda1591c3908bf80b60c2902491a7595ee27 (diff) | |
parent | 8901218418693d610a17b22fe789ba6279f63446 (diff) | |
download | SMAPI-a78b1935928919694dfe8de823a1accd6d222732.tar.gz SMAPI-a78b1935928919694dfe8de823a1accd6d222732.tar.bz2 SMAPI-a78b1935928919694dfe8de823a1accd6d222732.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/ContentManagers')
3 files changed, 40 insertions, 11 deletions
diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index 18aae05b..724a6e1c 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -32,12 +32,12 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <summary>Whether the content coordinator has been disposed.</summary> private bool IsDisposed; - /// <summary>The language enum values indexed by locale code.</summary> - private readonly IDictionary<string, LanguageCode> LanguageCodes; - /// <summary>A callback to invoke when the content manager is being disposed.</summary> private readonly Action<BaseContentManager> OnDisposing; + /// <summary>The language enum values indexed by locale code.</summary> + protected IDictionary<string, LanguageCode> LanguageCodes { get; } + /********* ** Accessors @@ -200,23 +200,25 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <summary>Purge matched assets from the cache.</summary> /// <param name="predicate">Matches the asset keys to invalidate.</param> /// <param name="dispose">Whether to dispose invalidated assets. This should only be <c>true</c> when they're being invalidated as part of a dispose, to avoid crashing the game.</param> - /// <returns>Returns the number of invalidated assets.</returns> - public IEnumerable<string> InvalidateCache(Func<string, Type, bool> predicate, bool dispose = false) + /// <returns>Returns the invalidated asset names and types.</returns> + public IEnumerable<Tuple<string, Type>> InvalidateCache(Func<string, Type, bool> predicate, bool dispose = false) { - HashSet<string> removeAssetNames = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase); + Dictionary<string, Type> removeAssetNames = new Dictionary<string, Type>(StringComparer.InvariantCultureIgnoreCase); this.Cache.Remove((key, type) => { this.ParseCacheKey(key, out string assetName, out _); - if (removeAssetNames.Contains(assetName) || predicate(assetName, type)) + if (removeAssetNames.ContainsKey(assetName)) + return true; + if (predicate(assetName, type)) { - removeAssetNames.Add(assetName); + removeAssetNames[assetName] = type; return true; } return false; }); - return removeAssetNames; + return removeAssetNames.Select(p => Tuple.Create(p.Key, p.Value)); } /// <summary>Dispose held resources.</summary> diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs index a53840bc..4f3b6fbc 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using StardewModdingAPI.Framework.Content; +using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Framework.Utilities; using StardewValley; @@ -52,7 +53,10 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <param name="language">The language code for which to load content.</param> public override T Load<T>(string assetName, LanguageCode language) { + // normalise asset name assetName = this.AssertAndNormaliseAssetName(assetName); + if (this.TryParseExplicitLanguageAssetKey(assetName, out string newAssetName, out LanguageCode newLanguage)) + return this.Load<T>(newAssetName, newLanguage); // get from cache if (this.IsLoaded(assetName)) @@ -124,6 +128,29 @@ namespace StardewModdingAPI.Framework.ContentManagers return false; } + /// <summary>Parse an asset key that contains an explicit language into its asset name and language, if applicable.</summary> + /// <param name="rawAsset">The asset key to parse.</param> + /// <param name="assetName">The asset name without the language code.</param> + /// <param name="language">The language code removed from the asset name.</param> + private bool TryParseExplicitLanguageAssetKey(string rawAsset, out string assetName, out LanguageCode language) + { + if (string.IsNullOrWhiteSpace(rawAsset)) + throw new SContentLoadException("The asset key is empty."); + + // extract language code + int splitIndex = rawAsset.LastIndexOf('.'); + if (splitIndex != -1 && this.LanguageCodes.TryGetValue(rawAsset.Substring(splitIndex + 1), out language)) + { + assetName = rawAsset.Substring(0, splitIndex); + return true; + } + + // no explicit language code found + assetName = rawAsset; + language = this.Language; + return false; + } + /// <summary>Load the initial asset from the registered <see cref="Loaders"/>.</summary> /// <param name="info">The basic asset metadata.</param> /// <returns>Returns the loaded asset metadata, or <c>null</c> if no loader matched.</returns> diff --git a/src/SMAPI/Framework/ContentManagers/IContentManager.cs b/src/SMAPI/Framework/ContentManagers/IContentManager.cs index 1eb8b0ac..17618edd 100644 --- a/src/SMAPI/Framework/ContentManagers/IContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/IContentManager.cs @@ -80,7 +80,7 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <summary>Purge matched assets from the cache.</summary> /// <param name="predicate">Matches the asset keys to invalidate.</param> /// <param name="dispose">Whether to dispose invalidated assets. This should only be <c>true</c> when they're being invalidated as part of a dispose, to avoid crashing the game.</param> - /// <returns>Returns the number of invalidated assets.</returns> - IEnumerable<string> InvalidateCache(Func<string, Type, bool> predicate, bool dispose = false); + /// <returns>Returns the invalidated asset names and types.</returns> + IEnumerable<Tuple<string, Type>> InvalidateCache(Func<string, Type, bool> predicate, bool dispose = false); } } |