From 4e347aef5b754a90b2015e17619d47de1fd68d1d Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Mon, 18 Nov 2024 21:12:13 -0500 Subject: [PATCH 1/6] Option A --- .../Patches/Events/Server/Unban.cs | 98 +++++++++++++++---- 1 file changed, 79 insertions(+), 19 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs index aa367c711..e7c3b0a0b 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs @@ -5,36 +5,37 @@ // // ----------------------------------------------------------------------- +using System.Collections.Generic; +using System.Reflection.Emit; +using Exiled.API.Features.Pools; +using Exiled.Events.Attributes; +using Exiled.Events.EventArgs.Server; +using HarmonyLib; + namespace Exiled.Events.Patches.Events.Server { - using System.Collections.Generic; - using System.Reflection.Emit; - - using Exiled.API.Features.Pools; - using Exiled.Events.Attributes; - using Exiled.Events.EventArgs.Server; - using HarmonyLib; - - using static HarmonyLib.AccessTools; + using static AccessTools; /// - /// Patches - /// to add and events. + /// Patches + /// to add and events. /// [HarmonyPatch(typeof(BanHandler), nameof(BanHandler.RemoveBan))] [EventPatch(typeof(Handlers.Server), nameof(Handlers.Server.Unbanning))] [EventPatch(typeof(Handlers.Server), nameof(Handlers.Server.Unbanned))] internal class Unban { - private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + [HarmonyTranspiler] + private static IEnumerable Transpiler(IEnumerable instructions, + ILGenerator generator) { - List newInstructions = ListPool.Pool.Get(instructions); + var newInstructions = ListPool.Pool.Get(instructions); - LocalBuilder ev = generator.DeclareLocal(typeof(UnbanningEventArgs)); + var ev = generator.DeclareLocal(typeof(UnbanningEventArgs)); - Label continueLabel = generator.DefineLabel(); + var continueLabel = generator.DefineLabel(); - newInstructions.InsertRange(0, new CodeInstruction[] + newInstructions.InsertRange(0, new[] { // id new(OpCodes.Ldarg_0), @@ -64,7 +65,7 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable.Pool.Return(newInstructions); + } + + [HarmonyTranspiler] + private static IEnumerable BanHandlerTranspiler( + IEnumerable instructions, + ILGenerator generator) + { + var newInstructions = ListPool.Pool.Get(instructions); + + var ev = generator.DeclareLocal(typeof(UnbanningEventArgs)); + + var continueLabel = generator.DefineLabel(); + + const int offset = 2; + var index = newInstructions.FindIndex(instruction => + instruction.Calls(Method(typeof(BanHandler), nameof(BanHandler.CheckExpiration)))) + offset; + + + var addToUnbannedListInstruction = newInstructions[index]; + newInstructions.InsertRange(index, new[] + { + // id + new CodeInstruction(OpCodes.Ldarg, 4).MoveLabelsFrom(addToUnbannedListInstruction), + + // type + new(OpCodes.Ldarg_0), + + // true + new(OpCodes.Ldc_I4_1), + + // UnbanningEventArgs ev = new(string, BanHandler.BanType, true); + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UnbanningEventArgs))[0]), + + new(OpCodes.Dup), + new(OpCodes.Stloc_S, ev.LocalIndex), + + // Handlers.Server.OnUnbanning(ev); + new(OpCodes.Call, Method(typeof(Handlers.Server), nameof(Handlers.Server.OnUnbanning))), + + // if (!ev.IsAllowed) + // return; + new(OpCodes.Callvirt, PropertyGetter(typeof(UnbanningEventArgs), nameof(UnbanningEventArgs.IsAllowed))), + new(OpCodes.Brtrue_S, continueLabel), + + new(OpCodes.Ret) + }); + + // Add label to ldloc.1 + addToUnbannedListInstruction.WithLabels(continueLabel); + + + for (var z = 0; z < newInstructions.Count; z++) + { yield return newInstructions[z]; + } ListPool.Pool.Return(newInstructions); } From 4f19031106c0d7276f61ce8a9bbbd3f34587f3f2 Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Mon, 18 Nov 2024 21:15:40 -0500 Subject: [PATCH 2/6] Style cop happy --- .../Patches/Events/Server/Unban.cs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs index e7c3b0a0b..e641ef667 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs @@ -5,15 +5,18 @@ // // ----------------------------------------------------------------------- -using System.Collections.Generic; -using System.Reflection.Emit; -using Exiled.API.Features.Pools; -using Exiled.Events.Attributes; -using Exiled.Events.EventArgs.Server; -using HarmonyLib; +using AccessTools = HarmonyLib.AccessTools; namespace Exiled.Events.Patches.Events.Server { + using System.Collections.Generic; + using System.Reflection.Emit; + + using Exiled.API.Features.Pools; + using Exiled.Events.Attributes; + using Exiled.Events.EventArgs.Server; + using HarmonyLib; + using static AccessTools; /// @@ -26,7 +29,8 @@ namespace Exiled.Events.Patches.Events.Server internal class Unban { [HarmonyTranspiler] - private static IEnumerable Transpiler(IEnumerable instructions, + private static IEnumerable Transpiler( + IEnumerable instructions, ILGenerator generator) { var newInstructions = ListPool.Pool.Get(instructions); @@ -65,7 +69,7 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable BanHandlerTranspiler( var index = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(BanHandler), nameof(BanHandler.CheckExpiration)))) + offset; - var addToUnbannedListInstruction = newInstructions[index]; newInstructions.InsertRange(index, new[] { @@ -133,13 +136,12 @@ private static IEnumerable BanHandlerTranspiler( new(OpCodes.Callvirt, PropertyGetter(typeof(UnbanningEventArgs), nameof(UnbanningEventArgs.IsAllowed))), new(OpCodes.Brtrue_S, continueLabel), - new(OpCodes.Ret) + new(OpCodes.Ret), }); // Add label to ldloc.1 addToUnbannedListInstruction.WithLabels(continueLabel); - for (var z = 0; z < newInstructions.Count; z++) { yield return newInstructions[z]; From ce55b21a850d4506a5851d3e9bca81fd2fd14bae Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Mon, 18 Nov 2024 21:49:36 -0500 Subject: [PATCH 3/6] Sigh stylecop --- EXILED/Exiled.Events/Patches/Events/Server/Unban.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs index e641ef667..41c927e06 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs @@ -5,8 +5,6 @@ // // ----------------------------------------------------------------------- -using AccessTools = HarmonyLib.AccessTools; - namespace Exiled.Events.Patches.Events.Server { using System.Collections.Generic; @@ -17,7 +15,7 @@ namespace Exiled.Events.Patches.Events.Server using Exiled.Events.EventArgs.Server; using HarmonyLib; - using static AccessTools; + using static HarmonyLib.AccessTools; /// /// Patches From f06528fcaba7fa942ef2cd61fe9880e98765c6a7 Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Tue, 19 Nov 2024 01:10:47 -0500 Subject: [PATCH 4/6] For Yamato --- EXILED/Exiled.Events/Patches/Events/Server/Unban.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs index 41c927e06..4726b2063 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs @@ -98,17 +98,17 @@ private static IEnumerable BanHandlerTranspiler( IEnumerable instructions, ILGenerator generator) { - var newInstructions = ListPool.Pool.Get(instructions); + List newInstructions = ListPool.Pool.Get(instructions); - var ev = generator.DeclareLocal(typeof(UnbanningEventArgs)); + LocalBuilder ev = generator.DeclareLocal(typeof(UnbanningEventArgs)); - var continueLabel = generator.DefineLabel(); + Label continueLabel = generator.DefineLabel(); const int offset = 2; - var index = newInstructions.FindIndex(instruction => + int index = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(BanHandler), nameof(BanHandler.CheckExpiration)))) + offset; - var addToUnbannedListInstruction = newInstructions[index]; + CodeInstruction addToUnbannedListInstruction = newInstructions[index]; newInstructions.InsertRange(index, new[] { // id From 2a4b40097dc7cd9e2eade8435699ae20fca2b4ff Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Wed, 20 Nov 2024 10:45:54 -0500 Subject: [PATCH 5/6] Done, not tested --- EXILED/Exiled.Events/Patches/Events/Server/Unban.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs index 4726b2063..c84d6e733 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs @@ -21,11 +21,14 @@ namespace Exiled.Events.Patches.Events.Server /// Patches /// to add and events. /// - [HarmonyPatch(typeof(BanHandler), nameof(BanHandler.RemoveBan))] + + [EventPatch(typeof(Handlers.Server), nameof(Handlers.Server.Unbanning))] [EventPatch(typeof(Handlers.Server), nameof(Handlers.Server.Unbanned))] + [HarmonyPatch] internal class Unban { + [HarmonyPatch(typeof(BanHandler), nameof(BanHandler.RemoveBan))] [HarmonyTranspiler] private static IEnumerable Transpiler( IEnumerable instructions, @@ -93,6 +96,7 @@ private static IEnumerable Transpiler( ListPool.Pool.Return(newInstructions); } + [HarmonyPatch(typeof(BanHandler), nameof(BanHandler.ValidateBans), typeof(BanHandler.BanType))] [HarmonyTranspiler] private static IEnumerable BanHandlerTranspiler( IEnumerable instructions, @@ -112,7 +116,7 @@ private static IEnumerable BanHandlerTranspiler( newInstructions.InsertRange(index, new[] { // id - new CodeInstruction(OpCodes.Ldarg, 4).MoveLabelsFrom(addToUnbannedListInstruction), + new CodeInstruction(OpCodes.Ldloc, 4).MoveLabelsFrom(addToUnbannedListInstruction), // type new(OpCodes.Ldarg_0), @@ -123,6 +127,7 @@ private static IEnumerable BanHandlerTranspiler( // UnbanningEventArgs ev = new(string, BanHandler.BanType, true); new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UnbanningEventArgs))[0]), + new(OpCodes.Dup), new(OpCodes.Dup), new(OpCodes.Stloc_S, ev.LocalIndex), From bcedfcbadcf33a2ba085144cc4d246d6b1828b4d Mon Sep 17 00:00:00 2001 From: Undid-Iridium <24619207+Undid-Iridium@users.noreply.github.com> Date: Sun, 24 Nov 2024 16:30:31 -0500 Subject: [PATCH 6/6] Stylecop --- EXILED/Exiled.Events/Patches/Events/Server/Unban.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs index c84d6e733..6bdb51706 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs @@ -21,8 +21,6 @@ namespace Exiled.Events.Patches.Events.Server /// Patches /// to add and events. /// - - [EventPatch(typeof(Handlers.Server), nameof(Handlers.Server.Unbanning))] [EventPatch(typeof(Handlers.Server), nameof(Handlers.Server.Unbanned))] [HarmonyPatch] @@ -34,11 +32,11 @@ private static IEnumerable Transpiler( IEnumerable instructions, ILGenerator generator) { - var newInstructions = ListPool.Pool.Get(instructions); + List newInstructions = ListPool.Pool.Get(instructions); - var ev = generator.DeclareLocal(typeof(UnbanningEventArgs)); + LocalBuilder ev = generator.DeclareLocal(typeof(UnbanningEventArgs)); - var continueLabel = generator.DefineLabel(); + Label continueLabel = generator.DefineLabel(); newInstructions.InsertRange(0, new[] {