diff options
Diffstat (limited to 'src/SMAPI/Framework/Networking')
5 files changed, 70 insertions, 35 deletions
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,41 +14,39 @@ namespace StardewModdingAPI.Framework.Networking ** Origin ****/ /// <summary>The unique ID of the player who broadcast the message.</summary> - public long FromPlayerID { get; set; } + public long FromPlayerID { get; } /// <summary>The unique ID of the mod which broadcast the message.</summary> - public string FromModID { get; set; } + public string FromModID { get; } /**** ** Destination ****/ /// <summary>The players who should receive the message.</summary> - public long[] ToPlayerIDs { get; set; } + public long[]? ToPlayerIDs { get; init; } /// <summary>The mods which should receive the message, or <c>null</c> for all mods.</summary> - public string[] ToModIDs { get; set; } + public string[]? ToModIDs { get; } /// <summary>A message type which receiving mods can use to decide whether it's the one they want to handle, like <c>SetPlayerLocation</c>. This doesn't need to be globally unique, since mods should check the originating mod ID.</summary> - public string Type { get; set; } + public string Type { get; } /// <summary>The custom mod data being broadcast.</summary> - public JToken Data { get; set; } + public JToken Data { get; } /********* ** Public methods *********/ /// <summary>Construct an instance.</summary> - public ModMessageModel() { } - - /// <summary>Construct an instance.</summary> /// <param name="fromPlayerID">The unique ID of the player who broadcast the message.</param> /// <param name="fromModID">The unique ID of the mod which broadcast the message.</param> /// <param name="toPlayerIDs">The players who should receive the message, or <c>null</c> for all players.</param> /// <param name="toModIDs">The mods which should receive the message, or <c>null</c> for all mods.</param> /// <param name="type">A message type which receiving mods can use to decide whether it's the one they want to handle, like <c>SetPlayerLocation</c>. This doesn't need to be globally unique, since mods should check the originating mod ID.</param> /// <param name="data">The custom mod data being broadcast.</param> - 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; } /// <inheritdoc /> - public ISemanticVersion GameVersion { get; } + public ISemanticVersion? GameVersion { get; } /// <inheritdoc /> - public ISemanticVersion ApiVersion { get; } + public ISemanticVersion? ApiVersion { get; } /// <inheritdoc /> public IEnumerable<IMultiplayerPeerMod> Mods { get; } @@ -57,11 +55,12 @@ namespace StardewModdingAPI.Framework.Networking /// <param name="model">The metadata to copy.</param> /// <param name="sendMessage">A method which sends a message to the peer.</param> /// <param name="isHost">Whether this is a connection to the host player.</param> - public MultiplayerPeer(long playerID, int? screenID, RemoteContextModel model, Action<OutgoingMessage> sendMessage, bool isHost) + public MultiplayerPeer(long playerID, int? screenID, RemoteContextModel? model, Action<OutgoingMessage> 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<IMultiplayerPeerMod>(); + this.SendMessageImpl = sendMessage; } /// <inheritdoc /> - 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 *********/ /// <summary>Construct an instance.</summary> /// <param name="mod">The mod metadata.</param> + [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 { /// <summary>Metadata about an installed mod exchanged with connected computers.</summary> public class RemoteContextModModel { - /// <summary>The mod's display name.</summary> - public string Name { get; set; } - + /********* + ** Accessors + *********/ /// <summary>The unique mod ID.</summary> - public string ID { get; set; } + public string ID { get; } + + /// <summary>The mod's display name.</summary> + public string Name { get; } /// <summary>The mod version.</summary> - public ISemanticVersion Version { get; set; } + public ISemanticVersion Version { get; } + + + /********* + ** Accessors + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="id">The unique mod ID.</param> + /// <param name="name">The mod's display name.</param> + /// <param name="version">The mod version.</param> + 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 *********/ /// <summary>Whether this player is the host player.</summary> - public bool IsHost { get; set; } + public bool IsHost { get; } - /// <summary>The game's platform version.</summary> - public GamePlatform Platform { get; set; } + /// <summary>The game's platform.</summary> + public GamePlatform Platform { get; } /// <summary>The installed version of Stardew Valley.</summary> - public ISemanticVersion GameVersion { get; set; } + public ISemanticVersion? GameVersion { get; } /// <summary>The installed version of SMAPI.</summary> - public ISemanticVersion ApiVersion { get; set; } + public ISemanticVersion? ApiVersion { get; } /// <summary>The installed mods.</summary> - public RemoteContextModModel[] Mods { get; set; } + public RemoteContextModModel[] Mods { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="isHost">Whether this player is the host player.</param> + /// <param name="platform">The game's platform.</param> + /// <param name="gameVersion">The installed version of Stardew Valley.</param> + /// <param name="apiVersion">The installed version of SMAPI.</param> + /// <param name="mods">The installed mods.</param> + 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<RemoteContextModModel>(); + } } } |