fix network messages not using same JSON serializer settings (#745)
This commit is contained in:
parent
f95292953f
commit
0fdb09f5f9
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
* For modders:
|
* 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.
|
* 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
|
## 3.8.1
|
||||||
Released 26 December 2020 for Stardew Valley 1.5.1 or later.
|
Released 26 December 2020 for Stardew Valley 1.5.1 or later.
|
||||||
|
|
|
@ -132,5 +132,11 @@ namespace StardewModdingAPI.Toolkit.Serialization
|
||||||
{
|
{
|
||||||
return JsonConvert.SerializeObject(model, formatting, this.JsonSettings);
|
return JsonConvert.SerializeObject(model, formatting, this.JsonSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Get a low-level JSON serializer matching the <see cref="JsonSettings"/>.</summary>
|
||||||
|
public JsonSerializer GetSerializer()
|
||||||
|
{
|
||||||
|
return JsonSerializer.CreateDefault(this.JsonSettings);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using StardewModdingAPI.Framework.Networking;
|
using StardewModdingAPI.Framework.Networking;
|
||||||
|
using StardewModdingAPI.Toolkit.Serialization;
|
||||||
|
|
||||||
namespace StardewModdingAPI.Events
|
namespace StardewModdingAPI.Events
|
||||||
{
|
{
|
||||||
|
@ -12,6 +13,9 @@ namespace StardewModdingAPI.Events
|
||||||
/// <summary>The underlying message model.</summary>
|
/// <summary>The underlying message model.</summary>
|
||||||
private readonly ModMessageModel Message;
|
private readonly ModMessageModel Message;
|
||||||
|
|
||||||
|
/// <summary>The JSON helper used to deserialize models.</summary>
|
||||||
|
private readonly JsonHelper JsonHelper;
|
||||||
|
|
||||||
|
|
||||||
/*********
|
/*********
|
||||||
** Accessors
|
** Accessors
|
||||||
|
@ -31,16 +35,18 @@ namespace StardewModdingAPI.Events
|
||||||
*********/
|
*********/
|
||||||
/// <summary>Construct an instance.</summary>
|
/// <summary>Construct an instance.</summary>
|
||||||
/// <param name="message">The received message.</param>
|
/// <param name="message">The received message.</param>
|
||||||
internal ModMessageReceivedEventArgs(ModMessageModel message)
|
/// <param name="jsonHelper">The JSON helper used to deserialize models.</param>
|
||||||
|
internal ModMessageReceivedEventArgs(ModMessageModel message, JsonHelper jsonHelper)
|
||||||
{
|
{
|
||||||
this.Message = message;
|
this.Message = message;
|
||||||
|
this.JsonHelper = jsonHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Read the message data into the given model type.</summary>
|
/// <summary>Read the message data into the given model type.</summary>
|
||||||
/// <typeparam name="TModel">The message model type.</typeparam>
|
/// <typeparam name="TModel">The message model type.</typeparam>
|
||||||
public TModel ReadAs<TModel>()
|
public TModel ReadAs<TModel>()
|
||||||
{
|
{
|
||||||
return this.Message.Data.ToObject<TModel>();
|
return this.Message.Data.ToObject<TModel>(this.JsonHelper.GetSerializer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1128,7 +1128,7 @@ namespace StardewModdingAPI.Framework
|
||||||
modIDs.Remove(message.FromModID); // don't send a broadcast back to the sender
|
modIDs.Remove(message.FromModID); // don't send a broadcast back to the sender
|
||||||
|
|
||||||
// raise events
|
// 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Constructor a content manager to read game content files.</summary>
|
/// <summary>Constructor a content manager to read game content files.</summary>
|
||||||
|
|
Loading…
Reference in New Issue