diff --git a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs index 0700f5419..66310a212 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/Unban.cs @@ -18,15 +18,19 @@ namespace Exiled.Events.Patches.Events.Server using static HarmonyLib.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))] + [HarmonyPatch] internal class Unban { - private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + [HarmonyPatch(typeof(BanHandler), nameof(BanHandler.RemoveBan))] + [HarmonyTranspiler] + private static IEnumerable Transpiler( + IEnumerable instructions, + ILGenerator generator) { List newInstructions = ListPool.Pool.Get(instructions); @@ -34,7 +38,7 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable.Pool.Return(newInstructions); + } + + [HarmonyPatch(typeof(BanHandler), nameof(BanHandler.ValidateBans), typeof(BanHandler.BanType))] + [HarmonyTranspiler] + private static IEnumerable BanHandlerTranspiler( + IEnumerable instructions, + ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + LocalBuilder ev = generator.DeclareLocal(typeof(UnbanningEventArgs)); + + Label continueLabel = generator.DefineLabel(); + + const int offset = 2; + int index = newInstructions.FindIndex(instruction => + instruction.Calls(Method(typeof(BanHandler), nameof(BanHandler.CheckExpiration)))) + offset; + + CodeInstruction addToUnbannedListInstruction = newInstructions[index]; + newInstructions.InsertRange(index, new[] + { + // id + new CodeInstruction(OpCodes.Ldloc, 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.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); }