summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Framework/Networking/SGalaxyNetServer.cs63
-rw-r--r--src/SMAPI/Framework/Networking/SLidgrenServer.cs59
-rw-r--r--src/SMAPI/Framework/SCore.cs4
-rw-r--r--src/SMAPI/Framework/SMultiplayer.cs19
-rw-r--r--src/SMAPI/Patches/GalaxyNetServerPatch.cs106
-rw-r--r--src/SMAPI/Patches/LidgrenServerPatch.cs59
-rw-r--r--src/SMAPI/StardewModdingAPI.csproj3
7 files changed, 83 insertions, 230 deletions
diff --git a/src/SMAPI/Framework/Networking/SGalaxyNetServer.cs b/src/SMAPI/Framework/Networking/SGalaxyNetServer.cs
new file mode 100644
index 00000000..2fc92737
--- /dev/null
+++ b/src/SMAPI/Framework/Networking/SGalaxyNetServer.cs
@@ -0,0 +1,63 @@
+using System;
+using System.Diagnostics.CodeAnalysis;
+using System.IO;
+using Galaxy.Api;
+using StardewValley.Network;
+using StardewValley.SDKs;
+
+namespace StardewModdingAPI.Framework.Networking
+{
+ /// <summary>A multiplayer server used to connect to an incoming player. This is an implementation of <see cref="LidgrenServer"/> that adds support for SMAPI's metadata context exchange.</summary>
+ internal class SGalaxyNetServer : GalaxyNetServer
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>A callback to raise when receiving a message. This receives the incoming message, a method to send a message, and a callback to run the default logic.</summary>
+ private readonly Action<IncomingMessage, Action<OutgoingMessage>, Action> OnProcessingMessage;
+
+ /// <summary>SMAPI's implementation of the game's core multiplayer logic.</summary>
+ private readonly SMultiplayer Multiplayer;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="gameServer">The underlying game server.</param>
+ /// <param name="multiplayer">SMAPI's implementation of the game's core multiplayer logic.</param>
+ /// <param name="onProcessingMessage">A callback to raise when receiving a message. This receives the incoming message, a method to send a message, and a callback to run the default logic.</param>
+ public SGalaxyNetServer(IGameServer gameServer, SMultiplayer multiplayer, Action<IncomingMessage, Action<OutgoingMessage>, Action> onProcessingMessage)
+ : base(gameServer)
+ {
+ this.Multiplayer = multiplayer;
+ this.OnProcessingMessage = onProcessingMessage;
+ }
+
+ /// <summary>Read and process a message from the client.</summary>
+ /// <param name="peer">The Galaxy peer ID.</param>
+ /// <param name="messageStream">The data to process.</param>
+ [SuppressMessage("ReSharper", "AccessToDisposedClosure", Justification = "The callback is invoked synchronously.")]
+ protected override void onReceiveMessage(GalaxyID peer, Stream messageStream)
+ {
+ using (IncomingMessage message = new IncomingMessage())
+ using (BinaryReader reader = new BinaryReader(messageStream))
+ {
+ message.Read(reader);
+ this.OnProcessingMessage(message, outgoing => this.sendMessage(peer, outgoing), () =>
+ {
+ if (this.peers.ContainsLeft(message.FarmerID) && (long)this.peers[message.FarmerID] == (long)peer.ToUint64())
+ {
+ this.gameServer.processIncomingMessage(message);
+ }
+ else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction)
+ {
+ NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader);
+ GalaxyID capturedPeer = new GalaxyID(peer.ToUint64());
+ this.gameServer.checkFarmhandRequest(Convert.ToString(peer.ToUint64()), farmer, msg => this.sendMessage(capturedPeer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64());
+ }
+ });
+ }
+ }
+ }
+}
diff --git a/src/SMAPI/Framework/Networking/SLidgrenServer.cs b/src/SMAPI/Framework/Networking/SLidgrenServer.cs
index 36f96bc3..37e546a1 100644
--- a/src/SMAPI/Framework/Networking/SLidgrenServer.cs
+++ b/src/SMAPI/Framework/Networking/SLidgrenServer.cs
@@ -1,11 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
-using System.Reflection;
using Lidgren.Network;
-using StardewModdingAPI.Framework.Reflection;
-using StardewModdingAPI.Patches;
-using StardewValley;
using StardewValley.Network;
namespace StardewModdingAPI.Framework.Networking
@@ -16,46 +12,36 @@ namespace StardewModdingAPI.Framework.Networking
/*********
** Properties
*********/
-
- /// <summary>The constructor for the internal <c>NetBufferReadStream</c> type.</summary>
- private readonly ConstructorInfo NetBufferReadStreamConstructor = SLidgrenServer.GetNetBufferReadStreamConstructor();
-
- /// <summary>A method which reads farmer data from the given binary reader.</summary>
- private readonly Func<BinaryReader, NetFarmerRoot> ReadFarmer;
+ /// <summary>SMAPI's implementation of the game's core multiplayer logic.</summary>
+ private readonly SMultiplayer Multiplayer;
/// <summary>A callback to raise when receiving a message. This receives the incoming message, a method to send a message, and a callback to run the default logic.</summary>
private readonly Action<IncomingMessage, Action<OutgoingMessage>, Action> OnProcessingMessage;
- /// <summary>The peer connections.</summary>
- private readonly Bimap<long, NetConnection> Peers;
-
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
+ /// <param name="multiplayer">SMAPI's implementation of the game's core multiplayer logic.</param>
/// <param name="gameServer">The underlying game server.</param>
- /// <param name="reflection">Simplifies access to private code.</param>
- /// <param name="readFarmer">A method which reads farmer data from the given binary reader.</param>
/// <param name="onProcessingMessage">A callback to raise when receiving a message. This receives the incoming message, a method to send a message, and a callback to run the default logic.</param>
- public SLidgrenServer(IGameServer gameServer, Reflector reflection, Func<BinaryReader, NetFarmerRoot> readFarmer, Action<IncomingMessage, Action<OutgoingMessage>, Action> onProcessingMessage)
+ public SLidgrenServer(IGameServer gameServer, SMultiplayer multiplayer, Action<IncomingMessage, Action<OutgoingMessage>, Action> onProcessingMessage)
: base(gameServer)
{
- this.ReadFarmer = readFarmer;
+ this.Multiplayer = multiplayer;
this.OnProcessingMessage = onProcessingMessage;
- this.Peers = reflection.GetField<Bimap<long, NetConnection>>(this, "peers").GetValue();
}
/// <summary>Parse a data message from a client.</summary>
/// <param name="rawMessage">The raw network message to parse.</param>
- /// <remarks>This is an implementation of <see cref="LidgrenServer.parseDataMessageFromClient"/> which calls <see cref="OnProcessingMessage"/>. This method is invoked via <see cref="LidgrenServerPatch.Prefix_LidgrenServer_ParseDataMessageFromClient"/>.</remarks>
[SuppressMessage("ReSharper", "AccessToDisposedClosure", Justification = "The callback is invoked synchronously.")]
- public bool ParseDataMessageFromClient(NetIncomingMessage rawMessage)
+ protected override void parseDataMessageFromClient(NetIncomingMessage rawMessage)
{
// add hook to call multiplayer core
NetConnection peer = rawMessage.SenderConnection;
using (IncomingMessage message = new IncomingMessage())
- using (Stream readStream = (Stream)this.NetBufferReadStreamConstructor.Invoke(new object[] { rawMessage }))
+ using (Stream readStream = new NetBufferReadStream(rawMessage))
using (BinaryReader reader = new BinaryReader(readStream))
{
while (rawMessage.LengthBits - rawMessage.Position >= 8)
@@ -63,39 +49,16 @@ namespace StardewModdingAPI.Framework.Networking
message.Read(reader);
this.OnProcessingMessage(message, outgoing => this.sendMessage(rawMessage.SenderConnection, outgoing), () =>
{
- if (this.Peers.ContainsLeft(message.FarmerID) && this.Peers[message.FarmerID] == peer)
+ if (this.peers.ContainsLeft(message.FarmerID) && this.peers[message.FarmerID] == peer)
this.gameServer.processIncomingMessage(message);
- else if (message.MessageType == Multiplayer.playerIntroduction)
+ else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction)
{
- NetFarmerRoot farmer = this.ReadFarmer(message.Reader);
- this.gameServer.checkFarmhandRequest("", farmer, msg => this.sendMessage(peer, msg), () => this.Peers[farmer.Value.UniqueMultiplayerID] = peer);
+ NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader);
+ this.gameServer.checkFarmhandRequest("", farmer, msg => this.sendMessage(peer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = peer);
}
});
}
}
-
- return false;
- }
-
-
- /*********
- ** Private methods
- *********/
- /// <summary>Get the constructor for the internal <c>NetBufferReadStream</c> type.</summary>
- private static ConstructorInfo GetNetBufferReadStreamConstructor()
- {
- // get type
- string typeName = $"StardewValley.Network.NetBufferReadStream, {Constants.GameAssemblyName}";
- Type type = Type.GetType(typeName);
- if (type == null)
- throw new InvalidOperationException($"Can't find type: {typeName}");
-
- // get constructor
- ConstructorInfo constructor = type.GetConstructor(new[] { typeof(NetBuffer) });
- if (constructor == null)
- throw new InvalidOperationException($"Can't find constructor for type: {typeName}");
-
- return constructor;
}
}
}
diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs
index 6ad118ce..128659c7 100644
--- a/src/SMAPI/Framework/SCore.cs
+++ b/src/SMAPI/Framework/SCore.cs
@@ -169,9 +169,7 @@ namespace StardewModdingAPI.Framework
// apply game patches
new GamePatcher(this.Monitor).Apply(
- new DialogueErrorPatch(this.MonitorForGame, this.Reflection),
- new LidgrenServerPatch(),
- new GalaxyNetServerPatch(() => this.GameInstance.Multiplayer)
+ new DialogueErrorPatch(this.MonitorForGame, this.Reflection)
);
}
diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs
index 1777a261..629fce1d 100644
--- a/src/SMAPI/Framework/SMultiplayer.cs
+++ b/src/SMAPI/Framework/SMultiplayer.cs
@@ -48,9 +48,6 @@ namespace StardewModdingAPI.Framework
/// <summary>Manages SMAPI events.</summary>
private readonly EventManager EventManager;
- /// <summary>The players who are currently disconnecting.</summary>
- private readonly IList<long> DisconnectingFarmers;
-
/// <summary>A callback to invoke when a mod message is received.</summary>
private readonly Action<ModMessageModel> OnModMessageReceived;
@@ -83,8 +80,6 @@ namespace StardewModdingAPI.Framework
this.ModRegistry = modRegistry;
this.Reflection = reflection;
this.OnModMessageReceived = onModMessageReceived;
-
- this.DisconnectingFarmers = reflection.GetField<List<long>>(this, "disconnectingFarmers").GetValue();
}
/// <summary>Handle sync messages from other players and perform other initial sync logic.</summary>
@@ -135,14 +130,14 @@ namespace StardewModdingAPI.Framework
case LidgrenServer _:
{
IGameServer gameServer = this.Reflection.GetField<IGameServer>(server, "gameServer").GetValue();
- return new SLidgrenServer(gameServer, this.Reflection, this.readFarmer, this.OnServerProcessingMessage);
+ return new SLidgrenServer(gameServer, this, this.OnServerProcessingMessage);
}
- //case GalaxyNetServer _:
- // {
- // IGameServer gameServer = this.Reflection.GetField<IGameServer>(server, "gameServer").GetValue();
- // return new SGalaxyNetServer(gameServer, this.Reflection, this.OnServerProcessingMessage);
- // }
+ case GalaxyNetServer _:
+ {
+ IGameServer gameServer = this.Reflection.GetField<IGameServer>(server, "gameServer").GetValue();
+ return new SGalaxyNetServer(gameServer, this, this.OnServerProcessingMessage);
+ }
default:
return server;
@@ -323,7 +318,7 @@ namespace StardewModdingAPI.Framework
/// <summary>Remove players who are disconnecting.</summary>
protected override void removeDisconnectedFarmers()
{
- foreach (long playerID in this.DisconnectingFarmers)
+ foreach (long playerID in this.disconnectingFarmers)
{
if (this.Peers.TryGetValue(playerID, out MultiplayerPeer peer))
{
diff --git a/src/SMAPI/Patches/GalaxyNetServerPatch.cs b/src/SMAPI/Patches/GalaxyNetServerPatch.cs
deleted file mode 100644
index e01ac329..00000000
--- a/src/SMAPI/Patches/GalaxyNetServerPatch.cs
+++ /dev/null
@@ -1,106 +0,0 @@
-using System;
-using System.Diagnostics.CodeAnalysis;
-using System.IO;
-using System.Reflection;
-using Galaxy.Api;
-using Harmony;
-using StardewModdingAPI.Framework;
-using StardewModdingAPI.Framework.Patching;
-using StardewValley.Network;
-
-namespace StardewModdingAPI.Patches
-{
- /// <summary>A Harmony patch to let SMAPI override <see cref="GalaxyNetServerPatch"/> methods.</summary>
- internal class GalaxyNetServerPatch : IHarmonyPatch
- {
- /*********
- ** Properties
- *********/
- /// <summary>SMAPI's implementation of the game's core multiplayer logic.</summary>
- private static Lazy<SMultiplayer> Multiplayer;
-
- /// <summary>The name of the internal GalaxyNetServer class.</summary>
- private static readonly string ServerTypeName = $"StardewValley.SDKs.GalaxyNetServer, {Constants.GameAssemblyName}";
-
- /// <summary>The method which sends an arbitrary message.</summary>
- private static MethodInfo SendMessageMethod;
-
-
- /*********
- ** Accessors
- *********/
- /// <summary>A unique name for this patch.</summary>
- public string Name => $"{nameof(GalaxyNetServerPatch)}";
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="multiplayer">SMAPI's implementation of the game's core multiplayer logic.</param>
- public GalaxyNetServerPatch(Func<SMultiplayer> multiplayer)
- {
- // init
- GalaxyNetServerPatch.Multiplayer = new Lazy<SMultiplayer>(multiplayer);
-
- // get server.sendMessage method
- Type type = Type.GetType(GalaxyNetServerPatch.ServerTypeName);
- if (type == null)
- throw new InvalidOperationException($"Can't find type '{GalaxyNetServerPatch.ServerTypeName}'.");
- GalaxyNetServerPatch.SendMessageMethod = type.GetMethod("sendMessage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new[] { typeof(GalaxyID), typeof(OutgoingMessage) }, null);
- if (GalaxyNetServerPatch.SendMessageMethod == null)
- throw new InvalidOperationException($"Can't find method 'sendMessage' on '{GalaxyNetServerPatch.ServerTypeName}'.");
- }
-
- /// <summary>Apply the Harmony patch.</summary>
- /// <param name="harmony">The Harmony instance.</param>
- public void Apply(HarmonyInstance harmony)
- {
- // override parseDataMessageFromClient
- {
- MethodInfo method = AccessTools.Method(Type.GetType($"StardewValley.SDKs.GalaxyNetServer, {Constants.GameAssemblyName}"), "onReceiveMessage");
- MethodInfo prefix = AccessTools.Method(this.GetType(), nameof(GalaxyNetServerPatch.Prefix_GalaxyNetServer_OnReceiveMessage));
- harmony.Patch(method, new HarmonyMethod(prefix), null);
- }
- }
-
-
- /*********
- ** Private methods
- *********/
- /// <summary>The method to call instead of the <see cref="GalaxyNetServer.onReceiveMessage"/> method.</summary>
- /// <param name="__instance">The instance being patched.</param>
- /// <param name="peer">The Galaxy peer ID.</param>
- /// <param name="messageStream">The data to process.</param>
- /// <param name="___peers">The private <c>peers</c> field on the <paramref name="__instance"/> instance.</param>
- /// <param name="___gameServer">The private <c>gameServer</c> field on the <paramref name="__instance"/> instance.</param>
- /// <returns>Returns whether to execute the original method.</returns>
- /// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
- [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")]
- private static bool Prefix_GalaxyNetServer_OnReceiveMessage(Server __instance, GalaxyID peer, Stream messageStream, Bimap<long, ulong> ___peers, IGameServer ___gameServer)
- {
- SMultiplayer multiplayer = GalaxyNetServerPatch.Multiplayer.Value;
-
- using (IncomingMessage message = new IncomingMessage())
- using (BinaryReader reader = new BinaryReader(messageStream))
- {
- message.Read(reader);
- multiplayer.OnServerProcessingMessage(message, outgoing => GalaxyNetServerPatch.SendMessageMethod.Invoke(__instance, new object[] { peer, outgoing }), () =>
- {
- if (___peers.ContainsLeft(message.FarmerID) && (long)___peers[message.FarmerID] == (long)peer.ToUint64())
- {
- ___gameServer.processIncomingMessage(message);
- }
- else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction)
- {
- NetFarmerRoot farmer = multiplayer.readFarmer(message.Reader);
- GalaxyID capturedPeer = new GalaxyID(peer.ToUint64());
- ___gameServer.checkFarmhandRequest(Convert.ToString(peer.ToUint64()), farmer, msg => GalaxyNetServerPatch.SendMessageMethod.Invoke(__instance, new object[] { capturedPeer, msg }), () => ___peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64());
- }
- });
- }
-
- return false;
- }
- }
-}
diff --git a/src/SMAPI/Patches/LidgrenServerPatch.cs b/src/SMAPI/Patches/LidgrenServerPatch.cs
deleted file mode 100644
index 47acd4c4..00000000
--- a/src/SMAPI/Patches/LidgrenServerPatch.cs
+++ /dev/null
@@ -1,59 +0,0 @@
-using System.Diagnostics.CodeAnalysis;
-using System.Reflection;
-using Harmony;
-using Lidgren.Network;
-using StardewModdingAPI.Framework.Networking;
-using StardewModdingAPI.Framework.Patching;
-using StardewValley.Network;
-
-namespace StardewModdingAPI.Patches
-{
- /// <summary>A Harmony patch to let SMAPI override <see cref="LidgrenServer"/> methods.</summary>
- internal class LidgrenServerPatch : IHarmonyPatch
- {
- /*********
- ** Accessors
- *********/
- /// <summary>A unique name for this patch.</summary>
- public string Name => $"{nameof(LidgrenServerPatch)}";
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Apply the Harmony patch.</summary>
- /// <param name="harmony">The Harmony instance.</param>
- public void Apply(HarmonyInstance harmony)
- {
- // override parseDataMessageFromClient
- {
- MethodInfo method = AccessTools.Method(typeof(LidgrenServer), "parseDataMessageFromClient");
- MethodInfo prefix = AccessTools.Method(this.GetType(), nameof(LidgrenServerPatch.Prefix_LidgrenServer_ParseDataMessageFromClient));
- harmony.Patch(method, new HarmonyMethod(prefix), null);
- }
- }
-
-
- /*********
- ** Private methods
- *********/
- /// <summary>The method to call instead of the <see cref="LidgrenServer.parseDataMessageFromClient"/> method.</summary>
- /// <param name="__instance">The instance being patched.</param>
- /// <param name="dataMsg">The raw network message to parse.</param>
- /// <param name="___peers">The private <c>peers</c> field on the <paramref name="__instance"/> instance.</param>
- /// <param name="___gameServer">The private <c>gameServer</c> field on the <paramref name="__instance"/> instance.</param>
- /// <returns>Returns whether to execute the original method.</returns>
- /// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
- [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")]
- private static bool Prefix_LidgrenServer_ParseDataMessageFromClient(LidgrenServer __instance, NetIncomingMessage dataMsg, Bimap<long, NetConnection> ___peers, IGameServer ___gameServer)
- {
- if (__instance is SLidgrenServer smapiServer)
- {
- smapiServer.ParseDataMessageFromClient(dataMsg);
- return false;
- }
-
- return true;
- }
- }
-}
diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj
index f16087bc..70fe2bed 100644
--- a/src/SMAPI/StardewModdingAPI.csproj
+++ b/src/SMAPI/StardewModdingAPI.csproj
@@ -182,6 +182,7 @@
<Compile Include="Framework\Networking\RemoteContextModel.cs" />
<Compile Include="Framework\Networking\RemoteContextModModel.cs" />
<Compile Include="Framework\Networking\SGalaxyNetClient.cs" />
+ <Compile Include="Framework\Networking\SGalaxyNetServer.cs" />
<Compile Include="Framework\Networking\SLidgrenClient.cs" />
<Compile Include="Framework\Networking\SLidgrenServer.cs" />
<Compile Include="Framework\SCore.cs" />
@@ -324,8 +325,6 @@
<Compile Include="Metadata\InstructionMetadata.cs" />
<Compile Include="Mod.cs" />
<Compile Include="Patches\DialogueErrorPatch.cs" />
- <Compile Include="Patches\GalaxyNetServerPatch.cs" />
- <Compile Include="Patches\LidgrenServerPatch.cs" />
<Compile Include="PatchMode.cs" />
<Compile Include="GamePlatform.cs" />
<Compile Include="Program.cs" />