Skip to content

Commit

Permalink
[Exiled::Events] Unbanning and Unbanned events (#68)
Browse files Browse the repository at this point in the history
* πŸ±β€πŸ‘€πŸ±β€πŸ‘€πŸ±β€πŸ‘€

* some fixes

* fix

* β˜ οΈπŸ’€πŸ’€πŸ’€β˜ οΈπŸ’€

Co-authored-by: IRacle <79921583+IRacle1@users.noreply.github.com>

---------

Co-authored-by: IRacle <79921583+IRacle1@users.noreply.github.com>
  • Loading branch information
VALERA771 and IRacle1 authored Aug 27, 2024
1 parent 880fde0 commit acd1b29
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
36 changes: 36 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Server/UnbannedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
ο»Ώ// -----------------------------------------------------------------------
// <copyright file="UnbannedEventArgs.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Server
{
/// <summary>
/// Contains all information after a player gets unbanned.
/// </summary>
public class UnbannedEventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="UnbannedEventArgs"/> class.
/// </summary>
/// <param name="details"><inheritdoc cref="BanDetails"/></param>
/// <param name="banType"><inheritdoc cref="BanType"/></param>
public UnbannedEventArgs(string details, BanHandler.BanType banType)
{
BanDetails = BanHandler.ProcessBanItem(details, banType);
BanType = banType;
}

/// <summary>
/// Gets the ban details.
/// </summary>
public BanDetails BanDetails { get; }

/// <summary>
/// Gets the ban type.
/// </summary>
public BanHandler.BanType BanType { get; }
}
}
43 changes: 43 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Server/UnbanningEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ο»Ώ// -----------------------------------------------------------------------
// <copyright file="UnbanningEventArgs.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Server
{
using Exiled.Events.EventArgs.Interfaces;

/// <summary>
/// Contains all information before player is unbanned.
/// </summary>
public class UnbanningEventArgs : IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="UnbanningEventArgs"/> class.
/// </summary>
/// <param name="banDetails"><inheritdoc cref="BanDetails"/></param>
/// <param name="banType"><inheritdoc cref="BanType"/></param>
/// <param name="isAllowed"><inheritdoc cref="IsAllowed"/></param>
public UnbanningEventArgs(string banDetails, BanHandler.BanType banType, bool isAllowed = true)
{
BanDetails = BanHandler.ProcessBanItem(banDetails, banType);
BanType = banType;
IsAllowed = isAllowed;
}

/// <summary>
/// Gets or sets the ban details.
/// </summary>
public BanDetails BanDetails { get; set; }

/// <summary>
/// Gets the ban type.
/// </summary>
public BanHandler.BanType BanType { get; }

/// <inheritdoc/>
public bool IsAllowed { get; set; }
}
}
22 changes: 22 additions & 0 deletions EXILED/Exiled.Events/Handlers/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public static class Server
/// </summary>
public static Event ReloadedPermissions { get; set; } = new();

/// <summary>
/// Invoked before player is being unbanned.
/// </summary>
public static Event<UnbanningEventArgs> Unbanning { get; set; } = new();

/// <summary>
/// Invoked after player is being unbanned.
/// </summary>
public static Event<UnbannedEventArgs> Unbanned { get; set; } = new();

/// <summary>
/// Called before waiting for players.
/// </summary>
Expand Down Expand Up @@ -210,5 +220,17 @@ public static class Server
/// </summary>
/// <param name="ev">The <see cref="SelectingRespawnTeamEventArgs"/> instance.</param>
public static void OnSelectingRespawnTeam(SelectingRespawnTeamEventArgs ev) => SelectingRespawnTeam.InvokeSafely(ev);

/// <summary>
/// Called before player is being unbanned.
/// </summary>
/// <param name="ev">The <see cref="UnbanningEventArgs"/> instance.</param>
public static void OnUnbanning(UnbanningEventArgs ev) => Unbanning.InvokeSafely(ev);

/// <summary>
/// Called after player is being unbanned.
/// </summary>
/// <param name="ev">The <see cref="UnbannedEventArgs"/> instance.</param>
public static void OnUnbanned(UnbannedEventArgs ev) => Unbanned.InvokeSafely(ev);
}
}
92 changes: 92 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Server/Unban.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
ο»Ώ// -----------------------------------------------------------------------
// <copyright file="Unban.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

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;

/// <summary>
/// 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))]
internal class Unban
{
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[]
{
// id
new(OpCodes.Ldarg_0),

// type
new(OpCodes.Ldarg_1),

// 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),

// id = ev.BanDetails.ToString();
new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex).WithLabels(continueLabel),
new(OpCodes.Callvirt, PropertyGetter(typeof(UnbanningEventArgs), nameof(UnbanningEventArgs.BanDetails))),
new(OpCodes.Call, Method(typeof(BanDetails), nameof(BanDetails.ToString))),
new(OpCodes.Starg_S, 1),
});

newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[]
{
// id
new(OpCodes.Ldarg_0),

// type
new(OpCodes.Ldarg_1),

// UnbannedEventArgs ev2 = new(string, BanHandler.BanType);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UnbannedEventArgs))[0]),

// Handlers.Server.OnUnbanned(ev2);
new(OpCodes.Call, Method(typeof(Handlers.Server), nameof(Handlers.Server.OnUnbanned))),
});

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

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

0 comments on commit acd1b29

Please sign in to comment.