Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Event StartingRecallEventArgs's IsAllowed issue. #2235

Merged
merged 21 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 63 additions & 66 deletions Exiled.Events/EventArgs/Scp049/StartingRecallEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,63 @@
// -----------------------------------------------------------------------
// <copyright file="StartingRecallEventArgs.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.Scp049
{
using API.Features;
using Exiled.API.Features.Roles;
using Interfaces;

/// <summary>
/// Contains all information before SCP-049 begins reviving a player.
/// </summary>
public class StartingRecallEventArgs : IScp049Event, IRagdollEvent, IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="StartingRecallEventArgs" /> class.
/// </summary>
/// <param name="target">
/// <inheritdoc cref="Target" />
/// </param>
/// <param name="scp049">
/// <inheritdoc cref="Player" />
/// </param>
/// <param name="ragdoll">
/// <inheritdoc cref="Ragdoll" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public StartingRecallEventArgs(Player target, Player scp049, Ragdoll ragdoll, bool isAllowed = true)
{
Target = target;
Player = scp049;
Scp049 = Player.Role.As<Scp049Role>();
Ragdoll = ragdoll;
IsAllowed = isAllowed;
}

/// <inheritdoc/>
public Scp049Role Scp049 { get; }

/// <summary>
/// Gets the player who's getting revived.
/// </summary>
public Player Target { get; }

/// <summary>
/// Gets or sets a value indicating whether the revived can begin.
/// </summary>
public bool IsAllowed { get; set; }

/// <summary>
/// Gets the player who is controlling SCP-049.
/// </summary>
public Player Player { get; }

/// <summary>
/// Gets the Ragdoll who's getting revived.
/// </summary>
public Ragdoll Ragdoll { get; }
}
}
// -----------------------------------------------------------------------
// <copyright file="StartingRecallEventArgs.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.Scp049
{
using API.Features;
using Exiled.API.Features.Roles;
using Interfaces;

/// <summary>
/// Contains all information before SCP-049 begins recalling a player.
/// </summary>
public class StartingRecallEventArgs : IScp049Event, IRagdollEvent, IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="StartingRecallEventArgs" /> class.
/// </summary>
/// <param name="player">
/// <inheritdoc cref="Player"/>
/// </param>
/// <param name="ragdoll">
/// <inheritdoc cref="Ragdoll"/>
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed"/>
/// </param>
public StartingRecallEventArgs(Player player, Ragdoll ragdoll, bool isAllowed = true)
{
Ragdoll = ragdoll;
Target = Ragdoll.Owner;
Scp049 = player.Role.As<Scp049Role>();
Player = player;
IsAllowed = isAllowed;
}

/// <summary>
/// Gets the player who's getting recalled.
/// </summary>
public Player Target { get; }

/// <summary>
/// Gets or sets a value indicating whether the recall can begin.
/// </summary>
public bool IsAllowed { get; set; }

/// <summary>
/// Gets the player who is controlling SCP-049.
/// </summary>
public Player Player { get; }

/// <inheritdoc/>
public Scp049Role Scp049 { get; }

/// <summary>
/// Gets the Ragdoll who's getting recalled.
/// </summary>
public Ragdoll Ragdoll { get; }
}
}
153 changes: 74 additions & 79 deletions Exiled.Events/Patches/Events/Scp049/StartingRecall.cs
Original file line number Diff line number Diff line change
@@ -1,79 +1,74 @@
// -----------------------------------------------------------------------
// <copyright file="StartingRecall.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.Scp049
{
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features;
using API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Scp049;

using HarmonyLib;

using PlayerRoles.PlayableScps.Scp049;
using PlayerRoles.Ragdolls;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Scp049ResurrectAbility.ServerValidateBegin" />.
/// Adds the <see cref="Handlers.Scp049.StartingRecall" /> event.
/// </summary>
[EventPatch(typeof(Handlers.Scp049), nameof(Handlers.Scp049.StartingRecall))]
[HarmonyPatch(typeof(Scp049ResurrectAbility), nameof(Scp049ResurrectAbility.ServerValidateBegin))]
internal static class StartingRecall
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label retLabel = generator.DefineLabel();

newInstructions.InsertRange(0, new CodeInstruction[]
{
// Player target = Player.Get(ragdoll.Info.OwnerHub);
new(OpCodes.Ldarg_1),
new(OpCodes.Ldfld, Field(typeof(BasicRagdoll), nameof(BasicRagdoll.Info))),
new(OpCodes.Ldfld, Field(typeof(RagdollData), nameof(RagdollData.OwnerHub))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// Player player = Player.Get(this.Owner);
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp049ResurrectAbility), nameof(Scp049ResurrectAbility.Owner))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// Ragdoll doll = Rangdoll.Get(ragdoll);
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(Ragdoll), nameof(Ragdoll.Get), new[] { typeof(BasicRagdoll) })),

// true
new(OpCodes.Ldc_I4_1),

// StartingRecallEventArgs ev = new(target, player, doll, true);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(StartingRecallEventArgs))[0]),
new(OpCodes.Dup),

// Handlers.Scp049.OnStartingRecall(ev);
new(OpCodes.Call, Method(typeof(Handlers.Scp049), nameof(Handlers.Scp049.OnStartingRecall))),

// if (!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(StartingRecallEventArgs), nameof(StartingRecallEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, retLabel),
});
newInstructions[newInstructions.Count - 1].WithLabels(retLabel);

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

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}
// -----------------------------------------------------------------------
// <copyright file="StartingRecall.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.Scp049
{
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features;
using API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Scp049;
using HarmonyLib;
using PlayerRoles.PlayableScps.Scp049;
using PlayerRoles.Ragdolls;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Scp049ResurrectAbility.ServerValidateBegin" />.
/// Adds the <see cref="Handlers.Scp049.StartingRecall" /> event.
/// </summary>
[EventPatch(typeof(Handlers.Scp049), nameof(Handlers.Scp049.StartingRecall))]
[HarmonyPatch(typeof(Scp049ResurrectAbility), nameof(Scp049ResurrectAbility.ServerValidateBegin))]
internal static class StartingRecall
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label continueLabel = generator.DefineLabel();

newInstructions.InsertRange(0, new CodeInstruction[]
{
// Player player = Player.Get(this.Owner);
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp049ResurrectAbility), nameof(Scp049ResurrectAbility.Owner))),
new(OpCodes.Call, Method(typeof(Exiled.API.Features.Player), nameof(Exiled.API.Features.Player.Get), new[] { typeof(ReferenceHub) })),

// Ragdoll doll = Ragdoll.Get(ragdoll);
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(Ragdoll), nameof(Ragdoll.Get), new[] { typeof(BasicRagdoll) })),

// true
new(OpCodes.Ldc_I4_1),

// StartingRecallEventArgs ev = new StartingRecallEventArgs(player, doll, true);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(StartingRecallEventArgs))[0]),
new(OpCodes.Dup),

// Handlers.Scp049.OnStartingRecall(ev);
new(OpCodes.Call, Method(typeof(Handlers.Scp049), nameof(Handlers.Scp049.OnStartingRecall))),

// if (!ev.IsAllowed)
// return 1;
// else
// continue;
new(OpCodes.Callvirt, PropertyGetter(typeof(StartingRecallEventArgs), nameof(StartingRecallEventArgs.IsAllowed))),
new(OpCodes.Brtrue_S, continueLabel),
new(OpCodes.Ldc_I4_1),
new(OpCodes.Ret),
new CodeInstruction(OpCodes.Nop).WithLabels(continueLabel),
});
XKaguya marked this conversation as resolved.
Show resolved Hide resolved

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

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