From 15acbc8f230dd2c4ba394960cdcc12a22a831bbf Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 14 Nov 2018 01:36:43 -0500 Subject: patch GalaxyNetServer to support context sync pending game code changes to make it public (#480) --- src/SMAPI/Patches/GalaxyNetServerPatch.cs | 106 ++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/SMAPI/Patches/GalaxyNetServerPatch.cs (limited to 'src/SMAPI/Patches') 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 +{ + /// A Harmony patch to let SMAPI override methods. + internal class GalaxyNetServerPatch : IHarmonyPatch + { + /********* + ** Properties + *********/ + /// SMAPI's implementation of the game's core multiplayer logic. + private static Lazy Multiplayer; + + /// The name of the internal GalaxyNetServer class. + private static readonly string ServerTypeName = $"StardewValley.SDKs.GalaxyNetServer, {Constants.GameAssemblyName}"; + + /// The method which sends an arbitrary message. + private static MethodInfo SendMessageMethod; + + + /********* + ** Accessors + *********/ + /// A unique name for this patch. + public string Name => $"{nameof(GalaxyNetServerPatch)}"; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// SMAPI's implementation of the game's core multiplayer logic. + public GalaxyNetServerPatch(Func multiplayer) + { + // init + GalaxyNetServerPatch.Multiplayer = new Lazy(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}'."); + } + + /// Apply the Harmony patch. + /// The Harmony instance. + 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 + *********/ + /// The method to call instead of the method. + /// The instance being patched. + /// The Galaxy peer ID. + /// The data to process. + /// The private peers field on the instance. + /// The private gameServer field on the instance. + /// Returns whether to execute the original method. + /// This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")] + private static bool Prefix_GalaxyNetServer_OnReceiveMessage(Server __instance, GalaxyID peer, Stream messageStream, Bimap ___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; + } + } +} -- cgit