From 1ec5b85ee7e6d1d34296d23436b3de890a51a77b Mon Sep 17 00:00:00 2001 From: Mariki Date: Wed, 23 Oct 2024 22:14:06 +0300 Subject: [PATCH 1/5] Added Transpiler For ProcessConnetionRequest, Changed Preath event args and handlers.Player.Preauth --- .../Player/PreAuthenticatingEventArgs.cs | 2 +- Exiled.Events/Handlers/Player.cs | 26 +----- .../Events/Player/PreAuthenticating.cs | 82 +++++++++++++++++++ 3 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 Exiled.Events/Patches/Events/Player/PreAuthenticating.cs diff --git a/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs b/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs index 0b10296f2a..9ccc35e912 100644 --- a/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs +++ b/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs @@ -103,7 +103,7 @@ public PreAuthenticatingEventArgs( /// /// Gets a value indicating whether the player can be authenticated or not. /// - public bool IsAllowed { get; private set; } = true; + public bool IsAllowed { get; set; } = true; /// /// Gets or sets the cached that is returned back to the NwPluginAPI. diff --git a/Exiled.Events/Handlers/Player.cs b/Exiled.Events/Handlers/Player.cs index e81c94cf3c..b5fd7df639 100644 --- a/Exiled.Events/Handlers/Player.cs +++ b/Exiled.Events/Handlers/Player.cs @@ -1120,30 +1120,8 @@ public static void OnItemRemoved(ReferenceHub referenceHub, InventorySystem.Item /// /// Called before pre-authenticating a . /// - /// - /// - /// - /// - /// - /// - /// - /// - /// Returns the instance. - [PluginEvent(ServerEventType.PlayerPreauth)] - public PreauthCancellationData OnPreAuthenticating( - string userId, - string ipAddress, - long expiration, - CentralAuthPreauthFlags flags, - string country, - byte[] signature, - LiteNetLib.ConnectionRequest request, - int readerStartPosition) - { - PreAuthenticatingEventArgs ev = new(userId, ipAddress, expiration, flags, country, signature, request, readerStartPosition); - PreAuthenticating.InvokeSafely(ev); + /// instance. + public static void OnPreAuthenticating(PreAuthenticatingEventArgs ev) => PreAuthenticating.InvokeSafely(ev); - return ev.CachedPreauthData; - } } } diff --git a/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs new file mode 100644 index 0000000000..6a5c50d9ab --- /dev/null +++ b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs @@ -0,0 +1,82 @@ + + +namespace Exiled.Events.Patches.Events.Player +{ + using System; + using System.Collections.Generic; + using System.Reflection.Emit; + + using API.Features.Pools; + using Exiled.API.Features; + using Exiled.Events.Attributes; + using Exiled.Events.EventArgs.Player; + using HarmonyLib; + using Hazards; + using LiteNetLib; + using static HarmonyLib.AccessTools; + + /// + /// Patches . + /// Adds the event. + /// + /// + [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.PreAuthenticating))] + [HarmonyPatch(typeof(CustomLiteNetLib4MirrorTransport), nameof(CustomLiteNetLib4MirrorTransport.ProcessConnectionRequest))] + internal static class PreAuthenticating + { + private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + Label ret = generator.DefineLabel(); + + newInstructions[newInstructions.Count - 1].labels.Add(ret); + LocalBuilder ev = generator.DeclareLocal(typeof(PreAuthenticatingEventArgs)); + int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ldstr && instruction.operand == "{0};{1};{2};{3}"); + + newInstructions.InsertRange( + index, + new CodeInstruction[] + { + + new CodeInstruction(OpCodes.Ldloc_S, 10), + //ipaddress + new CodeInstruction(OpCodes.Ldloc_S, 15), + //expiration + new CodeInstruction(OpCodes.Ldloc_S, 11), + //flags + new CodeInstruction(OpCodes.Ldloc_S, 12), + //country + new CodeInstruction(OpCodes.Ldloc_S, 13), + //signature + new CodeInstruction(OpCodes.Ldloc_S, 14), + //request + new CodeInstruction(OpCodes.Ldarg_1), + //position + new CodeInstruction(OpCodes.Ldloc_S, 9), + + //PreAuthenticatingEventArgs ev = new (userid, ipaddress, expiration, flags, country, signature, request, position) + new CodeInstruction(OpCodes.Newobj, GetDeclaredConstructors(typeof(PreAuthenticatingEventArgs))[0]), + new CodeInstruction(OpCodes.Dup), + new CodeInstruction(OpCodes.Stloc_S, ev.LocalIndex), + + new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Handlers.Player), nameof(Handlers.Player.OnPreAuthenticating))), + new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex), + // if ev.IsAllowed==false + + + new CodeInstruction(OpCodes.Callvirt, PropertyGetter(typeof(PreAuthenticatingEventArgs), nameof(PreAuthenticatingEventArgs.IsAllowed))), + new CodeInstruction(OpCodes.Brfalse_S, ret), + + + + }); + + + for (int z = 0; z < newInstructions.Count; z++) + yield return newInstructions[z]; + + ListPool.Pool.Return(newInstructions); + } + } +} From d771b6531238f481540791455e9bf389fe63b7e6 Mon Sep 17 00:00:00 2001 From: Mariki Date: Thu, 24 Oct 2024 18:30:48 +0300 Subject: [PATCH 2/5] Fixed blanked lines and etc --- .../Events/Player/PreAuthenticating.cs | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs index 6a5c50d9ab..960dced567 100644 --- a/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs +++ b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs @@ -1,4 +1,9 @@ - +// ----------------------------------------------------------------------- +// +// Copyright (c) Exiled Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- namespace Exiled.Events.Patches.Events.Player { @@ -19,7 +24,6 @@ namespace Exiled.Events.Patches.Events.Player /// Patches . /// Adds the event. /// - /// [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.PreAuthenticating))] [HarmonyPatch(typeof(CustomLiteNetLib4MirrorTransport), nameof(CustomLiteNetLib4MirrorTransport.ProcessConnectionRequest))] internal static class PreAuthenticating @@ -38,38 +42,33 @@ private static IEnumerable Transpiler(IEnumerable Date: Thu, 24 Oct 2024 18:32:44 +0300 Subject: [PATCH 3/5] Removed some CodeInstruction stuff --- .../Patches/Events/Player/PreAuthenticating.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs index 960dced567..aba86a97e1 100644 --- a/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs +++ b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs @@ -59,16 +59,16 @@ private static IEnumerable Transpiler(IEnumerable Date: Sat, 26 Oct 2024 18:09:43 +0300 Subject: [PATCH 4/5] MB build fix --- .../Events/Player/PreAuthenticating.cs | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs index aba86a97e1..dd1edba667 100644 --- a/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs +++ b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs @@ -12,12 +12,15 @@ namespace Exiled.Events.Patches.Events.Player using System.Reflection.Emit; using API.Features.Pools; - using Exiled.API.Features; using Exiled.Events.Attributes; using Exiled.Events.EventArgs.Player; + using HarmonyLib; + using Hazards; + using LiteNetLib; + using static HarmonyLib.AccessTools; /// @@ -33,8 +36,8 @@ private static IEnumerable Transpiler(IEnumerable newInstructions = ListPool.Pool.Get(instructions); Label ret = generator.DefineLabel(); - newInstructions[newInstructions.Count - 1].labels.Add(ret); + LocalBuilder ev = generator.DeclareLocal(typeof(PreAuthenticatingEventArgs)); int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ldstr && instruction.operand == "{0};{1};{2};{3}"); @@ -42,36 +45,35 @@ private static IEnumerable Transpiler(IEnumerable Date: Sat, 26 Oct 2024 18:19:13 +0300 Subject: [PATCH 5/5] fixed build --- Exiled.Events/Handlers/Player.cs | 1 - .../Patches/Events/Player/PreAuthenticating.cs | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Exiled.Events/Handlers/Player.cs b/Exiled.Events/Handlers/Player.cs index b5fd7df639..d9bebcd9b1 100644 --- a/Exiled.Events/Handlers/Player.cs +++ b/Exiled.Events/Handlers/Player.cs @@ -1122,6 +1122,5 @@ public static void OnItemRemoved(ReferenceHub referenceHub, InventorySystem.Item /// /// instance. public static void OnPreAuthenticating(PreAuthenticatingEventArgs ev) => PreAuthenticating.InvokeSafely(ev); - } } diff --git a/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs index dd1edba667..84968d88bf 100644 --- a/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs +++ b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs @@ -10,7 +10,7 @@ namespace Exiled.Events.Patches.Events.Player using System; using System.Collections.Generic; using System.Reflection.Emit; - + using API.Features.Pools; using Exiled.Events.Attributes; using Exiled.Events.EventArgs.Player; @@ -18,7 +18,6 @@ namespace Exiled.Events.Patches.Events.Player using HarmonyLib; using Hazards; - using LiteNetLib; using static HarmonyLib.AccessTools; @@ -39,7 +38,7 @@ private static IEnumerable Transpiler(IEnumerable instruction.opcode == OpCodes.Ldstr && instruction.operand == "{0};{1};{2};{3}"); + int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ldstr && instruction.operand == (object)"{0};{1};{2};{3}"); newInstructions.InsertRange( index, @@ -47,29 +46,40 @@ private static IEnumerable Transpiler(IEnumerable