diff --git a/release-notes.md b/release-notes.md index fde9316f..22df8f70 100644 --- a/release-notes.md +++ b/release-notes.md @@ -5,8 +5,9 @@ See [log](https://github.com/Pathoschild/SMAPI/compare/1.9...2.0). For mod developers: -* Added content events and an API which let you intercept XNB content as it's loaded, then - dynamically replace data entries or patch images. +* Added `ContentEvents.AssetLoading` event with a helper which lets you intercept the XNB content + load, and dynamically adjust or replace the content being loaded (including support for patching + images). --> ## 1.9 @@ -29,7 +30,7 @@ For players: * Fixed errors in console command handlers causing the game to crash. For mod developers: -* Added `SaveEvents.AfterReturnToTitle` and `TimeEvents.AfterDayStarted` events. +* Added `SaveEvents.AfterReturnToTitle`, `TimeEvents.AfterDayStarted`, and `ContentEvents.AfterLocaleChanged` events. * Added a simpler API for console commands (see `helper.ConsoleCommands`). * Added `GetPrivateProperty` reflection helper. * SMAPI now writes XNA input enums (`Buttons` and `Keys`) to JSON as strings automatically, so mods no longer need to add a `StringEnumConverter` themselves for those. diff --git a/src/StardewModdingAPI/Events/ContentEvents.cs b/src/StardewModdingAPI/Events/ContentEvents.cs index cc07f242..558fc070 100644 --- a/src/StardewModdingAPI/Events/ContentEvents.cs +++ b/src/StardewModdingAPI/Events/ContentEvents.cs @@ -24,6 +24,9 @@ namespace StardewModdingAPI.Events /********* ** Events *********/ + /// Raised after the content language changes. + public static event EventHandler> AfterLocaleChanged; + /// Raised when an XNB file is being read into the cache. Mods can change the data here before it's cached. public static event EventHandler AssetLoading; @@ -40,6 +43,15 @@ namespace StardewModdingAPI.Events ContentEvents.Monitor = monitor; } + /// Raise an event. + /// Encapsulates monitoring and logging. + /// The previous locale. + /// The current locale. + internal static void InvokeAfterLocaleChanged(IMonitor monitor, string oldLocale, string newLocale) + { + monitor.SafelyRaiseGenericEvent($"{nameof(ContentEvents)}.{nameof(ContentEvents.AfterLocaleChanged)}", ContentEvents.AfterLocaleChanged?.GetInvocationList(), null, new EventArgsValueChanged(oldLocale, newLocale)); + } + /// Raise an event. /// Encapsulates monitoring and logging. /// Encapsulates access and changes to content being read from a data file. diff --git a/src/StardewModdingAPI/Events/EventArgsIntChanged.cs b/src/StardewModdingAPI/Events/EventArgsIntChanged.cs index 31079730..0c742d12 100644 --- a/src/StardewModdingAPI/Events/EventArgsIntChanged.cs +++ b/src/StardewModdingAPI/Events/EventArgsIntChanged.cs @@ -9,11 +9,10 @@ namespace StardewModdingAPI.Events ** Accessors *********/ /// The previous value. - public int NewInt { get; } - - /// The current value. public int PriorInt { get; } + /// The current value. + public int NewInt { get; } /********* ** Public methods diff --git a/src/StardewModdingAPI/Events/EventArgsValueChanged.cs b/src/StardewModdingAPI/Events/EventArgsValueChanged.cs new file mode 100644 index 00000000..1d25af49 --- /dev/null +++ b/src/StardewModdingAPI/Events/EventArgsValueChanged.cs @@ -0,0 +1,31 @@ +using System; + +namespace StardewModdingAPI.Events +{ + /// Event arguments for a field that changed value. + /// The value type. + public class EventArgsValueChanged : EventArgs + { + /********* + ** Accessors + *********/ + /// The previous value. + public T PriorValue { get; } + + /// The current value. + public T NewValue { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The previous value. + /// The current value. + public EventArgsValueChanged(T priorValue, T newValue) + { + this.PriorValue = priorValue; + this.NewValue = newValue; + } + } +} \ No newline at end of file diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index aa22d572..5edb103e 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -159,6 +159,9 @@ namespace StardewModdingAPI.Framework /// The player character at last check. public SFarmer PreviousFarmer { get; private set; } + /// The previous content locale. + public LocalizedContentManager.LanguageCode? PreviousLocale { get; private set; } + /// An index incremented on every tick and reset every 60th tick (0–59). public int CurrentUpdateTick { get; private set; } @@ -1079,6 +1082,17 @@ namespace StardewModdingAPI.Framework /// Detect changes since the last update ticket and trigger mod events. private void UpdateEventCalls() { + // content locale changed event + if (this.PreviousLocale != LocalizedContentManager.CurrentLanguageCode) + { + var oldValue = this.PreviousLocale; + var newValue = LocalizedContentManager.CurrentLanguageCode; + + if (oldValue != null) + ContentEvents.InvokeAfterLocaleChanged(this.Monitor, oldValue.ToString(), newValue.ToString()); + this.PreviousLocale = newValue; + } + // save loaded event if (Game1.hasLoadedGame && !SaveGame.IsProcessing/*still loading save*/ && this.AfterLoadTimer >= 0) { diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 40f964b9..727da30f 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -119,6 +119,7 @@ +