From c79601ad76c25c58e3810a746cfff1be6edba1e1 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 18 Apr 2018 20:22:50 -0400 Subject: update for Stardew Valley 1.3.0.32 (#453) --- src/SMAPI/Framework/ContentCore.cs | 46 ++++++++----------------------- src/SMAPI/Framework/ContentManagerShim.cs | 5 ++-- src/SMAPI/Framework/SGame.cs | 19 ++++--------- 3 files changed, 20 insertions(+), 50 deletions(-) diff --git a/src/SMAPI/Framework/ContentCore.cs b/src/SMAPI/Framework/ContentCore.cs index 9da18481..ef001f79 100644 --- a/src/SMAPI/Framework/ContentCore.cs +++ b/src/SMAPI/Framework/ContentCore.cs @@ -47,9 +47,6 @@ namespace StardewModdingAPI.Framework /// A lookup which indicates whether the asset is localisable (i.e. the filename contains the locale), if previously loaded. private readonly IDictionary IsLocalisableLookup; - /// The locale codes used in asset keys indexed by enum value. - private readonly IDictionary Locales; - /// The language enum values indexed by locale code. private readonly IDictionary LanguageCodes; @@ -94,21 +91,19 @@ namespace StardewModdingAPI.Framework /// The service provider to use to locate services. /// The root directory to search for content. /// The current culture for which to localise content. - /// The current language code for which to localise content. /// Encapsulates monitoring and logging. /// Simplifies access to private code. - public ContentCore(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, string languageCodeOverride, IMonitor monitor, Reflector reflection) + public ContentCore(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, IMonitor monitor, Reflector reflection) { // init this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor)); - this.Content = new LocalizedContentManager(serviceProvider, rootDirectory, currentCulture, languageCodeOverride); + this.Content = new LocalizedContentManager(serviceProvider, rootDirectory, currentCulture); this.Cache = new ContentCache(this.Content, reflection); this.ModContentPrefix = this.GetAssetNameFromFilePath(Constants.ModPath); // get asset data this.CoreAssets = new CoreAssetPropagator(this.NormaliseAssetName, reflection); - this.Locales = this.GetKeyLocales(reflection); - this.LanguageCodes = this.Locales.ToDictionary(p => p.Value, p => p.Key, StringComparer.InvariantCultureIgnoreCase); + this.LanguageCodes = this.GetKeyLocales().ToDictionary(p => p.Value, p => p.Key, StringComparer.InvariantCultureIgnoreCase); this.IsLocalisableLookup = reflection.GetField>(this.Content, "_localizedAsset").GetValue(); } @@ -117,7 +112,7 @@ namespace StardewModdingAPI.Framework /// The root directory to search for content (or null. for the default) public ContentManagerShim CreateContentManager(string name, string rootDirectory = null) { - return new ContentManagerShim(this, name, this.Content.ServiceProvider, rootDirectory ?? this.Content.RootDirectory, this.Content.CurrentCulture, this.Content.LanguageCodeOverride); + return new ContentManagerShim(this, name, this.Content.ServiceProvider, rootDirectory ?? this.Content.RootDirectory, this.Content.CurrentCulture); } /**** @@ -177,7 +172,7 @@ namespace StardewModdingAPI.Framework /// The language. public string GetLocale(LocalizedContentManager.LanguageCode language) { - return this.Locales[language]; + return this.Content.LanguageCodeString(language); } /// Get whether the content manager has already loaded and cached the given asset. @@ -413,31 +408,14 @@ namespace StardewModdingAPI.Framework } /// Get the locale codes (like ja-JP) used in asset keys. - /// Simplifies access to private game code. - private IDictionary GetKeyLocales(Reflector reflection) + private IDictionary GetKeyLocales() { - string previousOverride = this.Content.LanguageCodeOverride; - - try - { - // temporarily disable language override - this.Content.LanguageCodeOverride = null; + // create locale => code map + IDictionary map = new Dictionary(); + foreach (LocalizedContentManager.LanguageCode code in Enum.GetValues(typeof(LocalizedContentManager.LanguageCode))) + map[code] = this.Content.LanguageCodeString(code); - // create locale => code map - IReflectedMethod languageCodeString = reflection.GetMethod(this.Content, "languageCodeString"); - IDictionary map = new Dictionary(); - foreach (LocalizedContentManager.LanguageCode code in Enum.GetValues(typeof(LocalizedContentManager.LanguageCode))) - { - map[code] = languageCodeString.Invoke(code); - } - - return map; - } - finally - { - // restore previous settings - this.Content.LanguageCodeOverride = previousOverride; - } + return map; } /// Get the asset name from a cache key. @@ -486,7 +464,7 @@ namespace StardewModdingAPI.Framework return false; return localisable - ? this.Cache.ContainsKey($"{normalisedAssetName}.{this.Locales[this.Content.GetCurrentLanguage()]}") + ? this.Cache.ContainsKey($"{normalisedAssetName}.{this.GetLocale(this.Content.GetCurrentLanguage())}") : this.Cache.ContainsKey(normalisedAssetName); } diff --git a/src/SMAPI/Framework/ContentManagerShim.cs b/src/SMAPI/Framework/ContentManagerShim.cs index 2791eb78..66754fd7 100644 --- a/src/SMAPI/Framework/ContentManagerShim.cs +++ b/src/SMAPI/Framework/ContentManagerShim.cs @@ -30,9 +30,8 @@ namespace StardewModdingAPI.Framework /// The service provider to use to locate services. /// The root directory to search for content. /// The current culture for which to localise content. - /// The current language code for which to localise content. - public ContentManagerShim(ContentCore contentCore, string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, string languageCodeOverride) - : base(serviceProvider, rootDirectory, currentCulture, languageCodeOverride) + public ContentManagerShim(ContentCore contentCore, string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture) + : base(serviceProvider, rootDirectory, currentCulture) { this.ContentCore = contentCore; this.Name = name; diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index be98aeb1..c33aa084 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -131,15 +131,8 @@ namespace StardewModdingAPI.Framework /// A callback to invoke after the game finishes initialising. private readonly Action OnGameInitialised; - /**** - ** Private wrappers - ****/ /// Simplifies access to private game code. - private static Reflector Reflection; - - // ReSharper disable ArrangeStaticMemberQualifier, ArrangeThisQualifier, InconsistentNaming - private static StringBuilder _debugStringBuilder => SGame.Reflection.GetField(typeof(Game1), nameof(_debugStringBuilder)).GetValue(); - // ReSharper restore ArrangeStaticMemberQualifier, ArrangeThisQualifier, InconsistentNaming + private readonly Reflector Reflection; /********* @@ -166,10 +159,10 @@ namespace StardewModdingAPI.Framework this.Monitor = monitor; this.Events = eventManager; this.FirstUpdate = true; - SGame.Reflection = reflection; + this.Reflection = reflection; this.OnGameInitialised = onGameInitialised; if (this.ContentCore == null) // shouldn't happen since CreateContentManager is called first, but let's init here just in case - this.ContentCore = new ContentCore(this.Content.ServiceProvider, this.Content.RootDirectory, Thread.CurrentThread.CurrentUICulture, null, this.Monitor, reflection); + this.ContentCore = new ContentCore(this.Content.ServiceProvider, this.Content.RootDirectory, Thread.CurrentThread.CurrentUICulture, this.Monitor, reflection); // set XNA option required by Stardew Valley Game1.graphics.GraphicsProfile = GraphicsProfile.HiDef; @@ -187,7 +180,7 @@ namespace StardewModdingAPI.Framework // Don't depend on anything being initialised at this point. if (this.ContentCore == null) { - this.ContentCore = new ContentCore(serviceProvider, rootDirectory, Thread.CurrentThread.CurrentUICulture, null, SGame.MonitorDuringInitialisation, SGame.ReflectorDuringInitialisation); + this.ContentCore = new ContentCore(serviceProvider, rootDirectory, Thread.CurrentThread.CurrentUICulture, SGame.MonitorDuringInitialisation, SGame.ReflectorDuringInitialisation); SGame.MonitorDuringInitialisation = null; } return this.ContentCore.CreateContentManager("(generated)", rootDirectory); @@ -609,7 +602,7 @@ namespace StardewModdingAPI.Framework // recover sprite batch try { - if (Game1.spriteBatch.IsOpen(SGame.Reflection)) + if (Game1.spriteBatch.IsOpen(this.Reflection)) { this.Monitor.Log("Recovering sprite batch from error...", LogLevel.Trace); Game1.spriteBatch.End(); @@ -1206,7 +1199,7 @@ namespace StardewModdingAPI.Framework overlayTempSprite.draw(Game1.spriteBatch, true, 0, 0, 1f); if (Game1.debugMode) { - StringBuilder debugStringBuilder = SGame._debugStringBuilder; + StringBuilder debugStringBuilder = Game1._debugStringBuilder; debugStringBuilder.Clear(); if (Game1.panMode) { -- cgit