From 4adf8611131a5d86b15f017a42a0366837d14528 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 13 Apr 2022 21:07:43 -0400 Subject: enable nullable annotations in the rest of SMAPI core (#837) --- src/SMAPI/Framework/Networking/ModMessageModel.cs | 21 ++++++-------- src/SMAPI/Framework/Networking/MultiplayerPeer.cs | 16 ++++++----- .../Framework/Networking/MultiplayerPeerMod.cs | 5 ++-- .../Framework/Networking/RemoteContextModModel.cs | 30 +++++++++++++++----- .../Framework/Networking/RemoteContextModel.cs | 33 +++++++++++++++++----- 5 files changed, 70 insertions(+), 35 deletions(-) (limited to 'src/SMAPI/Framework/Networking') diff --git a/src/SMAPI/Framework/Networking/ModMessageModel.cs b/src/SMAPI/Framework/Networking/ModMessageModel.cs index 4e7d01eb..01672714 100644 --- a/src/SMAPI/Framework/Networking/ModMessageModel.cs +++ b/src/SMAPI/Framework/Networking/ModMessageModel.cs @@ -1,6 +1,5 @@ -#nullable disable - using System.Linq; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace StardewModdingAPI.Framework.Networking @@ -15,33 +14,30 @@ namespace StardewModdingAPI.Framework.Networking ** Origin ****/ /// The unique ID of the player who broadcast the message. - public long FromPlayerID { get; set; } + public long FromPlayerID { get; } /// The unique ID of the mod which broadcast the message. - public string FromModID { get; set; } + public string FromModID { get; } /**** ** Destination ****/ /// The players who should receive the message. - public long[] ToPlayerIDs { get; set; } + public long[]? ToPlayerIDs { get; init; } /// The mods which should receive the message, or null for all mods. - public string[] ToModIDs { get; set; } + public string[]? ToModIDs { get; } /// A message type which receiving mods can use to decide whether it's the one they want to handle, like SetPlayerLocation. This doesn't need to be globally unique, since mods should check the originating mod ID. - public string Type { get; set; } + public string Type { get; } /// The custom mod data being broadcast. - public JToken Data { get; set; } + public JToken Data { get; } /********* ** Public methods *********/ - /// Construct an instance. - public ModMessageModel() { } - /// Construct an instance. /// The unique ID of the player who broadcast the message. /// The unique ID of the mod which broadcast the message. @@ -49,7 +45,8 @@ namespace StardewModdingAPI.Framework.Networking /// The mods which should receive the message, or null for all mods. /// A message type which receiving mods can use to decide whether it's the one they want to handle, like SetPlayerLocation. This doesn't need to be globally unique, since mods should check the originating mod ID. /// The custom mod data being broadcast. - public ModMessageModel(long fromPlayerID, string fromModID, long[] toPlayerIDs, string[] toModIDs, string type, JToken data) + [JsonConstructor] + public ModMessageModel(long fromPlayerID, string fromModID, long[]? toPlayerIDs, string[]? toModIDs, string type, JToken data) { this.FromPlayerID = fromPlayerID; this.FromModID = fromModID; diff --git a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs index 8ee5c309..b37c1e89 100644 --- a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs +++ b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Linq; @@ -39,10 +37,10 @@ namespace StardewModdingAPI.Framework.Networking public GamePlatform? Platform { get; } /// - public ISemanticVersion GameVersion { get; } + public ISemanticVersion? GameVersion { get; } /// - public ISemanticVersion ApiVersion { get; } + public ISemanticVersion? ApiVersion { get; } /// public IEnumerable Mods { get; } @@ -57,11 +55,12 @@ namespace StardewModdingAPI.Framework.Networking /// The metadata to copy. /// A method which sends a message to the peer. /// Whether this is a connection to the host player. - public MultiplayerPeer(long playerID, int? screenID, RemoteContextModel model, Action sendMessage, bool isHost) + public MultiplayerPeer(long playerID, int? screenID, RemoteContextModel? model, Action sendMessage, bool isHost) { this.PlayerID = playerID; this.ScreenID = screenID; this.IsHost = isHost; + if (model != null) { this.Platform = model.Platform; @@ -69,13 +68,16 @@ namespace StardewModdingAPI.Framework.Networking this.ApiVersion = model.ApiVersion; this.Mods = model.Mods.Select(mod => new MultiplayerPeerMod(mod)).ToArray(); } + else + this.Mods = Array.Empty(); + this.SendMessageImpl = sendMessage; } /// - public IMultiplayerPeerMod GetMod(string id) + public IMultiplayerPeerMod? GetMod(string? id) { - if (string.IsNullOrWhiteSpace(id) || this.Mods == null || !this.Mods.Any()) + if (string.IsNullOrWhiteSpace(id) || !this.Mods.Any()) return null; id = id.Trim(); diff --git a/src/SMAPI/Framework/Networking/MultiplayerPeerMod.cs b/src/SMAPI/Framework/Networking/MultiplayerPeerMod.cs index 6fdb9e54..1e150508 100644 --- a/src/SMAPI/Framework/Networking/MultiplayerPeerMod.cs +++ b/src/SMAPI/Framework/Networking/MultiplayerPeerMod.cs @@ -1,4 +1,4 @@ -#nullable disable +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Framework.Networking { @@ -22,10 +22,11 @@ namespace StardewModdingAPI.Framework.Networking *********/ /// Construct an instance. /// The mod metadata. + [SuppressMessage("ReSharper", "ConstantConditionalAccessQualifier", Justification = "The ID shouldn't be null, but we should handle it to avoid an error just in case.")] public MultiplayerPeerMod(RemoteContextModModel mod) { this.Name = mod.Name; - this.ID = mod.ID?.Trim(); + this.ID = mod.ID?.Trim() ?? string.Empty; this.Version = mod.Version; } } diff --git a/src/SMAPI/Framework/Networking/RemoteContextModModel.cs b/src/SMAPI/Framework/Networking/RemoteContextModModel.cs index 0383576c..7571acba 100644 --- a/src/SMAPI/Framework/Networking/RemoteContextModModel.cs +++ b/src/SMAPI/Framework/Networking/RemoteContextModModel.cs @@ -1,17 +1,33 @@ -#nullable disable - namespace StardewModdingAPI.Framework.Networking { /// Metadata about an installed mod exchanged with connected computers. public class RemoteContextModModel { - /// The mod's display name. - public string Name { get; set; } - + /********* + ** Accessors + *********/ /// The unique mod ID. - public string ID { get; set; } + public string ID { get; } + + /// The mod's display name. + public string Name { get; } /// The mod version. - public ISemanticVersion Version { get; set; } + public ISemanticVersion Version { get; } + + + /********* + ** Accessors + *********/ + /// Construct an instance. + /// The unique mod ID. + /// The mod's display name. + /// The mod version. + public RemoteContextModModel(string id, string name, ISemanticVersion version) + { + this.ID = id; + this.Name = name; + this.Version = version; + } } } diff --git a/src/SMAPI/Framework/Networking/RemoteContextModel.cs b/src/SMAPI/Framework/Networking/RemoteContextModel.cs index 37fafa67..7d53e732 100644 --- a/src/SMAPI/Framework/Networking/RemoteContextModel.cs +++ b/src/SMAPI/Framework/Networking/RemoteContextModel.cs @@ -1,4 +1,4 @@ -#nullable disable +using System; namespace StardewModdingAPI.Framework.Networking { @@ -9,18 +9,37 @@ namespace StardewModdingAPI.Framework.Networking ** Accessors *********/ /// Whether this player is the host player. - public bool IsHost { get; set; } + public bool IsHost { get; } - /// The game's platform version. - public GamePlatform Platform { get; set; } + /// The game's platform. + public GamePlatform Platform { get; } /// The installed version of Stardew Valley. - public ISemanticVersion GameVersion { get; set; } + public ISemanticVersion? GameVersion { get; } /// The installed version of SMAPI. - public ISemanticVersion ApiVersion { get; set; } + public ISemanticVersion? ApiVersion { get; } /// The installed mods. - public RemoteContextModModel[] Mods { get; set; } + public RemoteContextModModel[] Mods { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Whether this player is the host player. + /// The game's platform. + /// The installed version of Stardew Valley. + /// The installed version of SMAPI. + /// The installed mods. + public RemoteContextModel(bool isHost, GamePlatform platform, ISemanticVersion gameVersion, ISemanticVersion apiVersion, RemoteContextModModel[]? mods) + { + this.IsHost = isHost; + this.Platform = platform; + this.GameVersion = gameVersion; + this.ApiVersion = apiVersion; + this.Mods = mods ?? Array.Empty(); + } } } -- cgit