summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-03-03 20:57:52 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-03-03 20:57:52 -0500
commit4991a25d4633e146ead43872277d67685cb2e23b (patch)
treea214bf40d8fd2f228bff69e2b2a6b75a161f999f
parentc3c6fa1187987d0aff50a714e64b3b3ea3820ef0 (diff)
downloadSMAPI-4991a25d4633e146ead43872277d67685cb2e23b.tar.gz
SMAPI-4991a25d4633e146ead43872277d67685cb2e23b.tar.bz2
SMAPI-4991a25d4633e146ead43872277d67685cb2e23b.zip
add content language changed event (#243)
-rw-r--r--release-notes.md7
-rw-r--r--src/StardewModdingAPI/Events/ContentEvents.cs12
-rw-r--r--src/StardewModdingAPI/Events/EventArgsIntChanged.cs5
-rw-r--r--src/StardewModdingAPI/Events/EventArgsValueChanged.cs31
-rw-r--r--src/StardewModdingAPI/Framework/SGame.cs14
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.csproj1
6 files changed, 64 insertions, 6 deletions
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
*********/
+ /// <summary>Raised after the content language changes.</summary>
+ public static event EventHandler<EventArgsValueChanged<string>> AfterLocaleChanged;
+
/// <summary>Raised when an XNB file is being read into the cache. Mods can change the data here before it's cached.</summary>
public static event EventHandler<IContentEventHelper> AssetLoading;
@@ -40,6 +43,15 @@ namespace StardewModdingAPI.Events
ContentEvents.Monitor = monitor;
}
+ /// <summary>Raise an <see cref="AfterLocaleChanged"/> event.</summary>
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ /// <param name="oldLocale">The previous locale.</param>
+ /// <param name="newLocale">The current locale.</param>
+ internal static void InvokeAfterLocaleChanged(IMonitor monitor, string oldLocale, string newLocale)
+ {
+ monitor.SafelyRaiseGenericEvent($"{nameof(ContentEvents)}.{nameof(ContentEvents.AfterLocaleChanged)}", ContentEvents.AfterLocaleChanged?.GetInvocationList(), null, new EventArgsValueChanged<string>(oldLocale, newLocale));
+ }
+
/// <summary>Raise an <see cref="AssetLoading"/> event.</summary>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
/// <param name="contentHelper">Encapsulates access and changes to content being read from a data file.</param>
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
*********/
/// <summary>The previous value.</summary>
- public int NewInt { get; }
-
- /// <summary>The current value.</summary>
public int PriorInt { get; }
+ /// <summary>The current value.</summary>
+ 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
+{
+ /// <summary>Event arguments for a field that changed value.</summary>
+ /// <typeparam name="T">The value type.</typeparam>
+ public class EventArgsValueChanged<T> : EventArgs
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The previous value.</summary>
+ public T PriorValue { get; }
+
+ /// <summary>The current value.</summary>
+ public T NewValue { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="priorValue">The previous value.</param>
+ /// <param name="newValue">The current value.</param>
+ 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
/// <summary>The player character at last check.</summary>
public SFarmer PreviousFarmer { get; private set; }
+ /// <summary>The previous content locale.</summary>
+ public LocalizedContentManager.LanguageCode? PreviousLocale { get; private set; }
+
/// <summary>An index incremented on every tick and reset every 60th tick (0–59).</summary>
public int CurrentUpdateTick { get; private set; }
@@ -1079,6 +1082,17 @@ namespace StardewModdingAPI.Framework
/// <summary>Detect changes since the last update ticket and trigger mod events.</summary>
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 @@
<Compile Include="Advanced\IConfigFile.cs" />
<Compile Include="Command.cs" />
<Compile Include="Events\ContentEvents.cs" />
+ <Compile Include="Events\EventArgsValueChanged.cs" />
<Compile Include="Framework\Command.cs" />
<Compile Include="Config.cs" />
<Compile Include="Constants.cs" />