From 2a25c84167dc4386d4e3764249376e350392c85c Mon Sep 17 00:00:00 2001 From: js6pak Date: Mon, 18 Jan 2021 14:54:43 +0100 Subject: [PATCH] RpcLocalHandling --- Reactor.Example/ExampleRpc.cs | 2 +- Reactor/CustomRpc.cs | 18 +++++++++++++++--- Reactor/Patches/ModdedHandshake.cs | 6 +++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Reactor.Example/ExampleRpc.cs b/Reactor.Example/ExampleRpc.cs index 0d99a1c..5ea298f 100644 --- a/Reactor.Example/ExampleRpc.cs +++ b/Reactor.Example/ExampleRpc.cs @@ -19,7 +19,7 @@ namespace Reactor.Example } } - public override bool ShouldHandleLocally => true; + public override RpcLocalHandling LocalHandling => RpcLocalHandling.Before; public override void Write(MessageWriter writer, Data data) { diff --git a/Reactor/CustomRpc.cs b/Reactor/CustomRpc.cs index ef5c8e0..4b6a542 100644 --- a/Reactor/CustomRpc.cs +++ b/Reactor/CustomRpc.cs @@ -9,6 +9,13 @@ using Hazel; namespace Reactor { + public enum RpcLocalHandling + { + None, + Before, + After + } + public abstract class UnsafeCustomRpc { public int? Id { get; internal set; } @@ -18,7 +25,7 @@ namespace Reactor public abstract Type InnerNetObjectType { get; } - public abstract bool ShouldHandleLocally { get; } + public abstract RpcLocalHandling LocalHandling { get; } protected UnsafeCustomRpc(BasePlugin plugin) { @@ -106,7 +113,7 @@ namespace Reactor throw new InvalidOperationException("Can't send unregistered CustomRpc"); } - if (customRpc.ShouldHandleLocally) + if (customRpc.LocalHandling == RpcLocalHandling.Before) { customRpc.UnsafeHandle(netObject, data); } @@ -118,7 +125,7 @@ namespace Reactor }; writer.Write(customRpc.PluginId); - writer.Write(customRpc.Id.Value); + writer.Write(customRpc.Id!.Value); customRpc.UnsafeWrite(writer, data); if (immediately) @@ -129,6 +136,11 @@ namespace Reactor { writer.EndMessage(); } + + if (customRpc.LocalHandling == RpcLocalHandling.After) + { + customRpc.UnsafeHandle(netObject, data); + } } [HarmonyPatch] diff --git a/Reactor/Patches/ModdedHandshake.cs b/Reactor/Patches/ModdedHandshake.cs index 2094daf..47034f8 100644 --- a/Reactor/Patches/ModdedHandshake.cs +++ b/Reactor/Patches/ModdedHandshake.cs @@ -64,10 +64,10 @@ namespace Reactor.Patches handshake.WritePacked(plugins.Count); - foreach (var plugin in plugins) + foreach (var (pluginId, plugin) in plugins) { - handshake.Write(plugin.Key); - handshake.Write(plugin.Value.Metadata.Version.ToString()); + handshake.Write(pluginId); + handshake.Write(plugin.Metadata.Version.ToString()); } __result = handshake.ToByteArray(false);