From 3a9ea66a20136400d3268ea2e314eacd40d06231 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 26 Mar 2022 17:37:01 -0400 Subject: update asset propagation for new content API (#766) --- src/SMAPI/Framework/ContentCoordinator.cs | 5 +--- .../ContentManagers/BaseContentManager.cs | 11 ++++---- .../ContentManagers/GameContentManager.cs | 29 ---------------------- .../Framework/ContentManagers/IContentManager.cs | 3 --- 4 files changed, 6 insertions(+), 42 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index a4d29068..ee8b6893 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -230,12 +230,9 @@ namespace StardewModdingAPI.Framework /// Perform any updates needed when the locale changes. public void OnLocaleChanged() { - // reload affected content + // reset baseline cache this.ContentManagerLock.InReadLock(() => { - foreach (IContentManager contentManager in this.ContentManagers) - contentManager.OnLocaleChanged(); - this.VanillaContentManager.Unload(); }); } diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index b1ace259..1ca84792 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -137,16 +137,18 @@ namespace StardewModdingAPI.Framework.ContentManagers try { - this.LoadExact(localizedName, useCache: useCache); + T data = this.LoadExact(localizedName, useCache: useCache); LocalizedContentManager.localizedAssetNames[assetName.Name] = localizedName.Name; + return data; } catch (ContentLoadException) { localizedName = new AssetName(assetName.BaseName + "_international", null, null); try { - this.LoadExact(localizedName, useCache: useCache); + T data = this.LoadExact(localizedName, useCache: useCache); LocalizedContentManager.localizedAssetNames[assetName.Name] = localizedName.Name; + return data; } catch (ContentLoadException) { @@ -158,16 +160,13 @@ namespace StardewModdingAPI.Framework.ContentManagers // use cached key string rawName = LocalizedContentManager.localizedAssetNames[assetName.Name]; if (assetName.Name != rawName) - assetName = this.Coordinator.ParseAssetName(assetName.Name); + assetName = this.Coordinator.ParseAssetName(rawName); return this.LoadExact(assetName, useCache: useCache); } /// public abstract T LoadExact(IAssetName assetName, bool useCache); - /// - public virtual void OnLocaleChanged() { } - /// [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")] public string AssertAndNormalizeAssetName(string assetName) diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs index 500c0191..0fcad30a 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -25,9 +25,6 @@ namespace StardewModdingAPI.Framework.ContentManagers /// The assets currently being intercepted by instances. This is used to prevent infinite loops when a loader loads a new asset. private readonly ContextHash AssetsBeingLoaded = new(); - /// Maps asset names to their localized form, like LooseSprites\Billboard => LooseSprites\Billboard.fr-FR (localized) or Maps\AnimalShop => Maps\AnimalShop (not localized). - private IDictionary LocalizedAssetNames => LocalizedContentManager.localizedAssetNames; - /// Whether the next load is the first for any game content manager. private static bool IsFirstLoad = true; @@ -139,32 +136,6 @@ namespace StardewModdingAPI.Framework.ContentManagers return data; } - /// - public override void OnLocaleChanged() - { - base.OnLocaleChanged(); - - // find assets for which a translatable version was loaded - HashSet removeAssetNames = new HashSet(StringComparer.OrdinalIgnoreCase); - foreach (string key in this.LocalizedAssetNames.Where(p => p.Key != p.Value).Select(p => p.Key)) - { - IAssetName assetName = this.Coordinator.ParseAssetName(key); - removeAssetNames.Add(assetName.BaseName); - } - - // invalidate translatable assets - string[] invalidated = this - .InvalidateCache((key, _) => - removeAssetNames.Contains(key) - || removeAssetNames.Contains(this.Coordinator.ParseAssetName(key).BaseName) - ) - .Select(p => p.Key) - .OrderBy(p => p, StringComparer.OrdinalIgnoreCase) - .ToArray(); - if (invalidated.Any()) - this.Monitor.Log($"Invalidated {invalidated.Length} asset names: {string.Join(", ", invalidated)} for locale change."); - } - /// public override LocalizedContentManager CreateTemporary() { diff --git a/src/SMAPI/Framework/ContentManagers/IContentManager.cs b/src/SMAPI/Framework/ContentManagers/IContentManager.cs index 4de9a8c3..774b20d9 100644 --- a/src/SMAPI/Framework/ContentManagers/IContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/IContentManager.cs @@ -65,8 +65,5 @@ namespace StardewModdingAPI.Framework.ContentManagers /// Whether to dispose invalidated assets. This should only be true when they're being invalidated as part of a dispose, to avoid crashing the game. /// Returns the invalidated asset names and instances. IDictionary InvalidateCache(Func predicate, bool dispose = false); - - /// Perform any cleanup needed when the locale changes. - void OnLocaleChanged(); } } -- cgit