From 0fdb09f5f9ef463dbf867c3a46ce680706305f0e Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 1 Jan 2021 11:51:57 -0500 Subject: [PATCH] fix network messages not using same JSON serializer settings (#745) --- docs/release-notes.md | 1 + src/SMAPI.Toolkit/Serialization/JsonHelper.cs | 6 ++++++ src/SMAPI/Events/ModMessageReceivedEventArgs.cs | 10 ++++++++-- src/SMAPI/Framework/SCore.cs | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/docs/release-notes.md b/docs/release-notes.md index dbc8e905..dcd638a3 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -14,6 +14,7 @@ * For modders: * When a mod is blocked by SMAPI's internal compatibility list, the `TRACE` messages while loading it now indicates that and specifies the reason. + * Message data from the `ModMessageReceived` event now uses the same serializer settings as the rest of SMAPI. This mainly adds support for sending crossplatform `Color`, `Point`, `Vector2`, `Rectangle`, and `SemanticVersion` fields through network messages. ## 3.8.1 Released 26 December 2020 for Stardew Valley 1.5.1 or later. diff --git a/src/SMAPI.Toolkit/Serialization/JsonHelper.cs b/src/SMAPI.Toolkit/Serialization/JsonHelper.cs index 031afbb0..00db9903 100644 --- a/src/SMAPI.Toolkit/Serialization/JsonHelper.cs +++ b/src/SMAPI.Toolkit/Serialization/JsonHelper.cs @@ -132,5 +132,11 @@ namespace StardewModdingAPI.Toolkit.Serialization { return JsonConvert.SerializeObject(model, formatting, this.JsonSettings); } + + /// Get a low-level JSON serializer matching the . + public JsonSerializer GetSerializer() + { + return JsonSerializer.CreateDefault(this.JsonSettings); + } } } diff --git a/src/SMAPI/Events/ModMessageReceivedEventArgs.cs b/src/SMAPI/Events/ModMessageReceivedEventArgs.cs index d4370028..d75a7540 100644 --- a/src/SMAPI/Events/ModMessageReceivedEventArgs.cs +++ b/src/SMAPI/Events/ModMessageReceivedEventArgs.cs @@ -1,5 +1,6 @@ using System; using StardewModdingAPI.Framework.Networking; +using StardewModdingAPI.Toolkit.Serialization; namespace StardewModdingAPI.Events { @@ -12,6 +13,9 @@ namespace StardewModdingAPI.Events /// The underlying message model. private readonly ModMessageModel Message; + /// The JSON helper used to deserialize models. + private readonly JsonHelper JsonHelper; + /********* ** Accessors @@ -31,16 +35,18 @@ namespace StardewModdingAPI.Events *********/ /// Construct an instance. /// The received message. - internal ModMessageReceivedEventArgs(ModMessageModel message) + /// The JSON helper used to deserialize models. + internal ModMessageReceivedEventArgs(ModMessageModel message, JsonHelper jsonHelper) { this.Message = message; + this.JsonHelper = jsonHelper; } /// Read the message data into the given model type. /// The message model type. public TModel ReadAs() { - return this.Message.Data.ToObject(); + return this.Message.Data.ToObject(this.JsonHelper.GetSerializer()); } } } diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 27540442..5dc33828 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -1128,7 +1128,7 @@ namespace StardewModdingAPI.Framework modIDs.Remove(message.FromModID); // don't send a broadcast back to the sender // raise events - this.EventManager.ModMessageReceived.Raise(new ModMessageReceivedEventArgs(message), mod => mod != null && modIDs.Contains(mod.Manifest.UniqueID)); + this.EventManager.ModMessageReceived.Raise(new ModMessageReceivedEventArgs(message, this.Toolkit.JsonHelper), mod => mod != null && modIDs.Contains(mod.Manifest.UniqueID)); } /// Constructor a content manager to read game content files.