summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-06 18:06:47 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-06 18:06:47 -0400
commitb834ed7ef5095203529f8b77aee3f25f5387fbcc (patch)
treeeca5df4cc0f4d4e7aa18ef949aeb0a9cd9ec9617 /src/SMAPI/Framework
parent87d5288287520f750651667839ebbe7bbeb97bba (diff)
downloadSMAPI-b834ed7ef5095203529f8b77aee3f25f5387fbcc.tar.gz
SMAPI-b834ed7ef5095203529f8b77aee3f25f5387fbcc.tar.bz2
SMAPI-b834ed7ef5095203529f8b77aee3f25f5387fbcc.zip
fix errors reading empty JSON files
Diffstat (limited to 'src/SMAPI/Framework')
-rw-r--r--src/SMAPI/Framework/SMultiplayer.cs29
1 files changed, 24 insertions, 5 deletions
diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs
index e41e7edc..2badcbbf 100644
--- a/src/SMAPI/Framework/SMultiplayer.cs
+++ b/src/SMAPI/Framework/SMultiplayer.cs
@@ -9,6 +9,7 @@ using StardewModdingAPI.Events;
using StardewModdingAPI.Framework.Events;
using StardewModdingAPI.Framework.Networking;
using StardewModdingAPI.Framework.Reflection;
+using StardewModdingAPI.Internal;
using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Utilities;
using StardewValley;
@@ -489,8 +490,8 @@ namespace StardewModdingAPI.Framework
private RemoteContextModel? ReadContext(BinaryReader reader)
{
string data = reader.ReadString();
- RemoteContextModel model = this.JsonHelper.Deserialize<RemoteContextModel>(data);
- return model.ApiVersion != null
+ RemoteContextModel? model = this.JsonHelper.Deserialize<RemoteContextModel>(data);
+ return model?.ApiVersion != null
? model
: null; // no data available for vanilla players
}
@@ -499,13 +500,31 @@ namespace StardewModdingAPI.Framework
/// <param name="message">The raw message to parse.</param>
private void ReceiveModMessage(IncomingMessage message)
{
- // parse message
+ // read message JSON
string json = message.Reader.ReadString();
- ModMessageModel model = this.JsonHelper.Deserialize<ModMessageModel>(json);
- HashSet<long> playerIDs = new HashSet<long>(model.ToPlayerIDs ?? this.GetKnownPlayerIDs());
if (this.LogNetworkTraffic)
this.Monitor.Log($"Received message: {json}.");
+ // deserialize model
+ ModMessageModel? model;
+ try
+ {
+ model = this.JsonHelper.Deserialize<ModMessageModel>(json);
+ if (model is null)
+ {
+ this.Monitor.Log($"Received invalid mod message from {message.FarmerID}.\nRaw message data: {json}");
+ return;
+ }
+ }
+ catch (Exception ex)
+ {
+ this.Monitor.Log($"Received invalid mod message from {message.FarmerID}.\nRaw message data: {json}\nError details: {ex.GetLogSummary()}");
+ return;
+ }
+
+ // get player IDs
+ HashSet<long> playerIDs = new HashSet<long>(model.ToPlayerIDs ?? this.GetKnownPlayerIDs());
+
// notify local mods
if (playerIDs.Contains(Game1.player.UniqueMultiplayerID))
this.OnModMessageReceived(model);