Skip to content

Commit

Permalink
new ev + previous role
Browse files Browse the repository at this point in the history
  • Loading branch information
VALERA771 committed Aug 3, 2024
1 parent 81c3652 commit 3f404c9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
11 changes: 10 additions & 1 deletion EXILED/Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,18 @@ public PlayerPermissions RemoteAdminPermissions
public Role Role
{
get => role ??= Role.Create(RoleManager.CurrentRole);
internal set => role = value;
internal set
{
PreviousRole = role;
role = value;
}
}

/// <summary>
/// Gets the role that player had before changing role.
/// </summary>
public Role PreviousRole { get; private set; }

/// <summary>
/// Gets or sets the player's SCP preferences.
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Player/EscapedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// -----------------------------------------------------------------------
// <copyright file="EscapedEventArgs.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.Player
{
using Exiled.API.Enums;
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;

/// <summary>
/// Contains all information after player has escaped.
/// </summary>
public class EscapedEventArgs : IPlayerEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="EscapedEventArgs"/> class.
/// </summary>
/// <param name="player"><inheritdoc cref="Player"/></param>
/// <param name="escapeScenario"><inheritdoc cref="EscapeScenario"/></param>
public EscapedEventArgs(Player player, EscapeScenario escapeScenario)
{
Player = player;
EscapeScenario = escapeScenario;
}

/// <inheritdoc/>
public Player Player { get; }

/// <summary>
/// Gets the type of escape.
/// </summary>
public EscapeScenario EscapeScenario { get; }
}
}
11 changes: 11 additions & 0 deletions EXILED/Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ public class Player
/// </summary>
public static Event<EscapingEventArgs> Escaping { get; set; } = new();

/// <summary>
/// Invoked after a <see cref="API.Features.Player"/> escapes.
/// </summary>
public static Event<EscapedEventArgs> Escaped { get; set; } = new();

/// <summary>
/// Invoked before a <see cref="API.Features.Player"/> begins speaking to the intercom.
/// </summary>
Expand Down Expand Up @@ -710,6 +715,12 @@ public class Player
/// <param name="ev">The <see cref="EscapingEventArgs"/> instance.</param>
public static void OnEscaping(EscapingEventArgs ev) => Escaping.InvokeSafely(ev);

/// <summary>
/// Called after a <see cref="API.Features.Player"/> escapes.
/// </summary>
/// <param name="ev">The <see cref="EscapedEventArgs"/> instance.</param>
public static void OnEscaped(EscapedEventArgs ev) => Escaped.InvokeSafely(ev);

/// <summary>
/// Called before a <see cref="API.Features.Player"/> begins speaking to the intercom.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -----------------------------------------------------------------------
// <copyright file="Escaping.cs" company="Exiled Team">
// <copyright file="EscapingAndEscaped.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
Expand Down Expand Up @@ -29,11 +29,12 @@ namespace Exiled.Events.Patches.Events.Player
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Escape.ServerHandlePlayer(ReferenceHub)"/> for <see cref="Handlers.Player.Escaping" />.
/// Patches <see cref="Escape.ServerHandlePlayer(ReferenceHub)"/> for <see cref="Handlers.Player.Escaping" /> and <see cref="Handlers.Player.Escaped"/>.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.Escaping))]
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.Escaped))]
[HarmonyPatch(typeof(Escape), nameof(Escape.ServerHandlePlayer))]
internal static class Escaping
internal static class EscapingAndEscaped
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
Expand Down Expand Up @@ -98,9 +99,28 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
// GrantAllTickets(ev)
new CodeInstruction(OpCodes.Ldloc, ev.LocalIndex).WithLabels(labels),
new(OpCodes.Call, Method(typeof(Escaping), nameof(GrantAllTickets))),
new(OpCodes.Call, Method(typeof(EscapingAndEscaped), nameof(GrantAllTickets))),
});

newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[]
{
// loading EscapingEventArgs instance 2 times
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Ldloc_S, ev.LocalIndex),

// ev.Player
new(OpCodes.Callvirt, PropertyGetter(typeof(EscapingEventArgs), nameof(EscapingEventArgs.Player))),

// ev.EscapeScenario
new(OpCodes.Callvirt, PropertyGetter(typeof(EscapingEventArgs), nameof(EscapingEventArgs.EscapeScenario))),

// EscapedEventArgs ev2 = new(ev.Player, ev.EscapeScenario);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(EscapedEventArgs))[0]),

// Handlers.Player.OnEscaped(ev);
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnEscaped))),
});

newInstructions[newInstructions.Count - 1].WithLabels(returnLabel);

for (int z = 0; z < newInstructions.Count; z++)
Expand Down

0 comments on commit 3f404c9

Please sign in to comment.