Skip to content

Commit

Permalink
added the RecontainingEvent with the patch
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxWorn3365 committed Aug 1, 2024
1 parent 04bffe8 commit 2e9f87f
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
50 changes: 50 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Scp079/RecontainingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// -----------------------------------------------------------------------
// <copyright file="RecontainingEventArgs.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.Scp079
{
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;

/// <summary>
/// Contains information before SCP-079 gets recontained.
/// </summary>
public class RecontainingEventArgs : IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="RecontainingEventArgs" /> class.
/// </summary>
/// <param name="recontainer">The <see cref="BreakableWindow"/> istance.</param>
public RecontainingEventArgs(BreakableWindow recontainer)
{
Recontainer = Player.Get(recontainer.LastAttacker);
}

/// <summary>
/// Initializes a new instance of the <see cref="RecontainingEventArgs" /> class.
/// </summary>
/// <param name="recontainer">The player that triggered the SCP-079 recontaining event.</param>
public RecontainingEventArgs(ReferenceHub recontainer)
{
Recontainer = Player.Get(recontainer);
}

/// <summary>
/// Gets the Player that started the recontainment process.<br></br>
/// Can be null if <see cref="IsAutomatic"/> is true.
/// </summary>
public Player Recontainer { get; }

/// <inheritdoc/>
public bool IsAllowed { get; set; } = true;

/// <summary>
/// Gets a value indicating whether or not the recontained has been made automatically or by triggering the proccess.
/// </summary>
public bool IsAutomatic => Recontainer is null;
}
}
11 changes: 11 additions & 0 deletions EXILED/Exiled.Events/Handlers/Scp079.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public static class Scp079
/// </summary>
public static Event<ChangingSpeakerStatusEventArgs> ChangingSpeakerStatus { get; set; } = new();

/// <summary>
/// Invoked before SCP-079 recontainment.
/// </summary>
public static Event<RecontainingEventArgs> Recontaining { get; set; } = new();

/// <summary>
/// Invoked after SCP-079 recontainment.
/// </summary>
Expand Down Expand Up @@ -125,6 +130,12 @@ public static class Scp079
/// <param name="ev">The <see cref="ChangingSpeakerStatusEventArgs" /> instance.</param>
public static void OnChangingSpeakerStatus(ChangingSpeakerStatusEventArgs ev) => ChangingSpeakerStatus.InvokeSafely(ev);

/// <summary>
/// Called before SCP-079 is recontained.
/// </summary>
/// <param name="ev">The <see cref="RecontainingEventArgs" /> instance.</param>
public static void OnRecontaining(RecontainingEventArgs ev) => Recontaining.InvokeSafely(ev);

/// <summary>
/// Called after SCP-079 is recontained.
/// </summary>
Expand Down
65 changes: 65 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Scp079/Recontaining.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// -----------------------------------------------------------------------
// <copyright file="Recontaining.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.Scp079
{
using System.Collections.Generic;
using System.Reflection.Emit;

using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Scp079;
using Exiled.Events.Handlers;

using HarmonyLib;

using PlayerRoles.PlayableScps.Scp079;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Scp079Recontainer.Recontain" />.
/// Adds the <see cref="Scp079.Recontaining" /> event.
/// </summary>
[EventPatch(typeof(Scp079), nameof(Scp079.Recontaining))]
[HarmonyPatch(typeof(Scp079Recontainer), nameof(Scp079Recontainer.Recontain))]
internal class Recontaining
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
int index = 0;
List<CodeInstruction> newInstructions = new(instructions);

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

Label proceed = generator.DefineLabel();

newInstructions.InsertRange(index, new CodeInstruction[]
{
// RecontainingEventArgs ev = new(ReferenceHub "attacker")
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, PropertyGetter(typeof(Scp079Recontainer), nameof(Scp079Recontainer._activatorGlass))),
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(RecontainingEventArgs))[0]),
new(OpCodes.Starg_S, ev.LocalIndex),

// Call event
new(OpCodes.Ldarg_S, ev.LocalIndex),
new(OpCodes.Call, Method(typeof(Scp079), nameof(Scp079.OnRecontaining))),

// if (!ev.IsAllowed) return;
new(OpCodes.Ldarg_S, ev.LocalIndex),
new(OpCodes.Callvirt, Method(typeof(RecontainingEventArgs), nameof(RecontainingEventArgs.IsAllowed))),
new(OpCodes.Brtrue_S, proceed),

new(OpCodes.Ret),

new CodeInstruction(OpCodes.Nop).WithLabels(proceed),
});

return newInstructions;
}
}
}

0 comments on commit 2e9f87f

Please sign in to comment.