From 3df97bf2663d911e718d3d06b0d982a595ef5513 Mon Sep 17 00:00:00 2001
From: Mariki <68015763+Marikider@users.noreply.github.com>
Date: Mon, 28 Oct 2024 23:18:53 +0300
Subject: [PATCH] Recoded Player.PreAuthenticating via Patch (#2786)
---
.../Player/PreAuthenticatingEventArgs.cs | 2 +-
Exiled.Events/Handlers/Player.cs | 27 +-----
.../Events/Player/PreAuthenticating.cs | 93 +++++++++++++++++++
3 files changed, 96 insertions(+), 26 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..d9bebcd9b1 100644
--- a/Exiled.Events/Handlers/Player.cs
+++ b/Exiled.Events/Handlers/Player.cs
@@ -1120,30 +1120,7 @@ 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);
-
- return ev.CachedPreauthData;
- }
+ /// 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
new file mode 100644
index 0000000000..84968d88bf
--- /dev/null
+++ b/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs
@@ -0,0 +1,93 @@
+// -----------------------------------------------------------------------
+//
+// Copyright (c) Exiled Team. All rights reserved.
+// Licensed under the CC BY-SA 3.0 license.
+//
+// -----------------------------------------------------------------------
+
+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;
+
+ 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 == (object)"{0};{1};{2};{3}");
+
+ newInstructions.InsertRange(
+ index,
+ new CodeInstruction[]
+ {
+ // userid
+ new CodeInstruction(OpCodes.Ldloc_S, 10),
+
+ // ipaddress
+ new (OpCodes.Ldloc_S, 15),
+
+ // expiration
+ new (OpCodes.Ldloc_S, 11),
+
+ // flags
+ new (OpCodes.Ldloc_S, 12),
+
+ // country
+ new (OpCodes.Ldloc_S, 13),
+
+ // signature
+ new (OpCodes.Ldloc_S, 14),
+
+ // request
+ new (OpCodes.Ldarg_1),
+
+ // position
+ new (OpCodes.Ldloc_S, 9),
+
+ // PreAuthenticatingEventArgs ev = new (userid, ipaddress, expiration, flags, country, signature, request, position)
+ new (OpCodes.Newobj, GetDeclaredConstructors(typeof(PreAuthenticatingEventArgs))[0]),
+ new (OpCodes.Dup),
+ new (OpCodes.Stloc_S, ev.LocalIndex),
+
+ // OnPreAuthenticating(ev)
+ new (OpCodes.Call, AccessTools.Method(typeof(Handlers.Player), nameof(Handlers.Player.OnPreAuthenticating))),
+ new (OpCodes.Ldloc_S, ev.LocalIndex),
+
+ // ev.isallowed
+ new (OpCodes.Callvirt, PropertyGetter(typeof(PreAuthenticatingEventArgs), nameof(PreAuthenticatingEventArgs.IsAllowed))),
+
+ // ret
+ new (OpCodes.Brfalse_S, ret),
+ });
+
+ for (int z = 0; z < newInstructions.Count; z++)
+ yield return newInstructions[z];
+
+ ListPool.Pool.Return(newInstructions);
+ }
+ }
+}