summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/SMAPI/Framework/Networking/SGalaxyNetServer.cs96
-rw-r--r--src/SMAPI/Framework/SCore.cs3
-rw-r--r--src/SMAPI/Framework/SMultiplayer.cs10
-rw-r--r--src/SMAPI/Patches/GalaxyNetServerPatch.cs106
-rw-r--r--src/SMAPI/StardewModdingAPI.csproj2
5 files changed, 114 insertions, 103 deletions
diff --git a/src/SMAPI/Framework/Networking/SGalaxyNetServer.cs b/src/SMAPI/Framework/Networking/SGalaxyNetServer.cs
deleted file mode 100644
index 99eae8ad..00000000
--- a/src/SMAPI/Framework/Networking/SGalaxyNetServer.cs
+++ /dev/null
@@ -1,96 +0,0 @@
-using System;
-using System.IO;
-using Galaxy.Api;
-using StardewModdingAPI.Framework.Reflection;
-using StardewValley;
-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>The peer connections.</summary>
- private readonly Bimap<long, ulong> Peers;
-
- /// <summary>The underlying net server.</summary>
- private readonly IReflectedField<GalaxySocket> Server;
-
- /// <summary>The underlying method which handles incoming connections.</summary>
- private readonly Action<GalaxyID> BaseReceiveConnection;
-
- /// <summary>The underlying method which handles incoming disconnections.</summary>
- private readonly Action<GalaxyID> BaseReceiveDisconnect;
-
- /// <summary>The underlying method which handles incoming errors.</summary>
- private readonly Action<string> BaseReceiveError;
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="gameServer">The underlying game server.</param>
- /// <param name="reflection">Simplifies access to private code.</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, Reflector reflection, Action<IncomingMessage, Action<OutgoingMessage>, Action> onProcessingMessage)
- : base(gameServer)
- {
- this.OnProcessingMessage = onProcessingMessage;
- this.Peers = reflection.GetField<Bimap<long, ulong>>(this, "peers").GetValue();
- this.Server = reflection.GetField<GalaxySocket>(this, "server");
-
- this.BaseReceiveConnection = (Action<GalaxyID>)Delegate.CreateDelegate(typeof(Action<GalaxyID>), this, reflection.GetMethod(this, "onReceiveConnection").MethodInfo);
- this.BaseReceiveDisconnect = (Action<GalaxyID>)Delegate.CreateDelegate(typeof(Action<GalaxyID>), this, reflection.GetMethod(this, "onReceiveDisconnect").MethodInfo);
- this.BaseReceiveError = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), this, reflection.GetMethod(this, "onReceiveError").MethodInfo);
- }
-
- /// <summary>Receive and process messages from the client.</summary>
- public override void receiveMessages()
- {
- GalaxySocket server = this.Server.GetValue();
- if (server == null)
- return;
-
- server.Receive(this.BaseReceiveConnection, this.OnReceiveMessage, this.BaseReceiveDisconnect, this.BaseReceiveError);
- server.Heartbeat(server.LobbyMembers());
- foreach (GalaxyID connection in server.Connections)
- {
- if (server.GetPingWith(connection) > 30000L)
- server.Kick(connection);
- }
- }
-
- /// <summary>Read and process a message from the client.</summary>
- /// <param name="peerID">The Galaxy peer ID.</param>
- /// <param name="data">The data to process.</param>
- private void OnReceiveMessage(GalaxyID peerID, Stream data)
- {
- using (IncomingMessage message = new IncomingMessage())
- using (BinaryReader reader = new BinaryReader(data))
- {
- message.Read(reader);
- this.OnProcessingMessage(message, outgoing => this.sendMessage(peerID, outgoing), () =>
- {
- if (this.Peers.ContainsLeft(message.FarmerID) && (long)this.Peers[message.FarmerID] == (long)peerID.ToUint64())
- {
- this.gameServer.processIncomingMessage(message);
- }
- else if (message.MessageType == Multiplayer.playerIntroduction)
- {
- NetFarmerRoot farmer = Game1.multiplayer.readFarmer(message.Reader);
- GalaxyID capturedPeer = new GalaxyID(peerID.ToUint64());
- this.gameServer.checkFarmhandRequest(Convert.ToString(peerID.ToUint64()), farmer, msg => this.sendMessage(capturedPeer, msg), () => this.Peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64());
- }
- });
- }
- }
- }
-}
diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs
index 890058b0..6ad118ce 100644
--- a/src/SMAPI/Framework/SCore.cs
+++ b/src/SMAPI/Framework/SCore.cs
@@ -170,7 +170,8 @@ namespace StardewModdingAPI.Framework
// apply game patches
new GamePatcher(this.Monitor).Apply(
new DialogueErrorPatch(this.MonitorForGame, this.Reflection),
- new LidgrenServerPatch()
+ new LidgrenServerPatch(),
+ new GalaxyNetServerPatch(() => this.GameInstance.Multiplayer)
);
}
diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs
index 5a8aa3e5..1777a261 100644
--- a/src/SMAPI/Framework/SMultiplayer.cs
+++ b/src/SMAPI/Framework/SMultiplayer.cs
@@ -138,11 +138,11 @@ namespace StardewModdingAPI.Framework
return new SLidgrenServer(gameServer, this.Reflection, this.readFarmer, 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.Reflection, this.OnServerProcessingMessage);
+ // }
default:
return server;
diff --git a/src/SMAPI/Patches/GalaxyNetServerPatch.cs b/src/SMAPI/Patches/GalaxyNetServerPatch.cs
new file mode 100644
index 00000000..e01ac329
--- /dev/null
+++ b/src/SMAPI/Patches/GalaxyNetServerPatch.cs
@@ -0,0 +1,106 @@
+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/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj
index 29c9f7fa..f16087bc 100644
--- a/src/SMAPI/StardewModdingAPI.csproj
+++ b/src/SMAPI/StardewModdingAPI.csproj
@@ -182,7 +182,6 @@
<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" />
@@ -325,6 +324,7 @@
<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" />