Skip to content

Commit

Permalink
Useless Event (939 Placed Amnestic Cloud) (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
x3rt authored Aug 7, 2024
1 parent 259cabb commit 26d1fcc
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// -----------------------------------------------------------------------
// <copyright file="PlacedAmnesticCloudEventArgs.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.Scp939
{
using API.Features;
using API.Features.Hazards;
using Interfaces;
using PlayerRoles.PlayableScps.Scp939;

using Scp939Role = API.Features.Roles.Scp939Role;

/// <summary>
/// Contains all information after SCP-939 used its amnestic cloud ability.
/// </summary>
public class PlacedAmnesticCloudEventArgs : IScp939Event
{
/// <summary>
/// Initializes a new instance of the <see cref="PlacedAmnesticCloudEventArgs" /> class.
/// </summary>
/// <param name="hub">
/// <inheritdoc cref="ReferenceHub" />
/// </param>
/// <param name="cloud">
/// <inheritdoc cref="PlayerRoles.PlayableScps.Scp939.Scp939AmnesticCloudInstance" />
/// </param>
public PlacedAmnesticCloudEventArgs(ReferenceHub hub, Scp939AmnesticCloudInstance cloud)
{
Player = Player.Get(hub);
AmnesticCloud = new AmnesticCloudHazard(cloud);
Scp939 = Player.Role.As<Scp939Role>();
}

/// <summary>
/// Gets the player who's controlling SCP-939.
/// </summary>
public Player Player { get; }

/// <summary>
/// Gets the <see cref="AmnesticCloudHazard"/> instance.
/// </summary>
public AmnesticCloudHazard AmnesticCloud { get; }

/// <inheritdoc/>
public Scp939Role Scp939 { get; }
}
}
11 changes: 11 additions & 0 deletions EXILED/Exiled.Events/Handlers/Scp939.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public static class Scp939
/// </summary>
public static Event<PlacingAmnesticCloudEventArgs> PlacingAmnesticCloud { get; set; } = new();

/// <summary>
/// Invoked after SCP-939 used its amnestic cloud ability.
/// </summary>
public static Event<PlacedAmnesticCloudEventArgs> PlacedAmnesticCloud { get; set; } = new();

/// <summary>
/// Invoked before SCP-939 plays a stolen voice.
/// </summary>
Expand Down Expand Up @@ -81,6 +86,12 @@ public static class Scp939
/// <param name="ev">The <see cref="PlacingAmnesticCloudEventArgs" /> instance.</param>
public static void OnPlacingAmnesticCloud(PlacingAmnesticCloudEventArgs ev) => PlacingAmnesticCloud.InvokeSafely(ev);

/// <summary>
/// Called after SCP-939 used its amnestic cloud ability.
/// </summary>
/// <param name="ev">The <see cref="PlacedAmnesticCloudEventArgs" /> instance.</param>
public static void OnPlacedAmnesticCloud(PlacedAmnesticCloudEventArgs ev) => PlacedAmnesticCloud.InvokeSafely(ev);

/// <summary>
/// Called before SCP-939 plays a stolen voice.
/// </summary>
Expand Down
73 changes: 73 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Scp939/PlacedAmnesticCloud.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// -----------------------------------------------------------------------
// <copyright file="PlacedAmnesticCloud.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.Scp939
{
using System.Collections.Generic;
using System.Reflection.Emit;

using Exiled.API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Scp939;
using Exiled.Events.Handlers;
using HarmonyLib;
using PlayerRoles.PlayableScps.Scp939;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Scp939AmnesticCloudAbility.OnStateEnabled" />
/// to add the <see cref="Scp939.PlacedAmnesticCloud" /> event.
/// </summary>
[EventPatch(typeof(Scp939), nameof(Scp939.PlacedAmnesticCloud))]
[HarmonyPatch(typeof(Scp939AmnesticCloudAbility), nameof(Scp939AmnesticCloudAbility.OnStateEnabled))]
internal static class PlacedAmnesticCloud
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

LocalBuilder cloud = generator.DeclareLocal(typeof(Scp939AmnesticCloudInstance));

const int offset = -2;
int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Callvirt && x.OperandIs(Method(typeof(Scp939AmnesticCloudInstance), nameof(Scp939AmnesticCloudInstance.ServerSetup)))) + offset;

newInstructions.InsertRange(
index,
new[]
{
// Scp939AmnesticCloudInstance cloud = Object.Instantiate<Scp939AmnesticCloudInstance>(this._instancePrefab)
new CodeInstruction(OpCodes.Dup),
new CodeInstruction(OpCodes.Stloc_S, cloud),
});

index = newInstructions.Count - 1;

// Scp939.OnPlacedAmnesticCloud(new PlacedAmnesticCloudEventArgs(this.Owner, cloud));
newInstructions.InsertRange(
index,
new[]
{
// this.Owner
new CodeInstruction(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp939AmnesticCloudAbility), nameof(Scp939AmnesticCloudAbility.Owner))),

// cloud
new CodeInstruction(OpCodes.Ldloc_S, cloud),

// Scp939.OnPlacedAmnesticCloud(new PlacedAmnesticCloudEventArgs(this.Owner, cloud));
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(PlacedAmnesticCloudEventArgs))[0]),
new(OpCodes.Call, Method(typeof(Scp939), nameof(Scp939.OnPlacedAmnesticCloud))),
});

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

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

0 comments on commit 26d1fcc

Please sign in to comment.