Skip to content

Commit

Permalink
feat: Unbanned event addition (#185)
Browse files Browse the repository at this point in the history
* Option A

* Style cop happy

* Sigh stylecop

* For Yamato

* Done, not tested

* Stylecop

---------

Co-authored-by: Yamato <[email protected]>
  • Loading branch information
Undid-Iridium and louis1706 authored Jan 6, 2025
1 parent dd65890 commit 5139cfc
Showing 1 changed file with 69 additions and 6 deletions.
75 changes: 69 additions & 6 deletions EXILED/Exiled.Events/Patches/Events/Server/Unban.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ namespace Exiled.Events.Patches.Events.Server
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="BanHandler.RemoveBan"/>
/// to add <see cref="Handlers.Server.Unbanning"/> and <see cref="Handlers.Server.Unbanned"/> events.
/// Patches <see cref="BanHandler.RemoveBan" />
/// to add <see cref="Handlers.Server.Unbanning" /> and <see cref="Handlers.Server.Unbanned" /> events.
/// </summary>
[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<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
[HarmonyPatch(typeof(BanHandler), nameof(BanHandler.RemoveBan))]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> Transpiler(
IEnumerable<CodeInstruction> instructions,
ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

LocalBuilder ev = generator.DeclareLocal(typeof(UnbanningEventArgs));

Label continueLabel = generator.DefineLabel();

newInstructions.InsertRange(0, new CodeInstruction[]
newInstructions.InsertRange(0, new[]
{
// id
new(OpCodes.Ldarg_0),
Expand Down Expand Up @@ -82,8 +86,67 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Call, Method(typeof(Handlers.Server), nameof(Handlers.Server.OnUnbanned))),
});

for (int z = 0; z < newInstructions.Count; z++)
for (var z = 0; z < newInstructions.Count; z++)
{
yield return newInstructions[z];
}

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}

[HarmonyPatch(typeof(BanHandler), nameof(BanHandler.ValidateBans), typeof(BanHandler.BanType))]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> BanHandlerTranspiler(
IEnumerable<CodeInstruction> instructions,
ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.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<CodeInstruction>.Pool.Return(newInstructions);
}
Expand Down

0 comments on commit 5139cfc

Please sign in to comment.