Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 55 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// -----------------------------------------------------------------------
// <copyright file="HitEventArgs.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Scp0492
{
using System.Collections.Generic;
using System.Linq;

using API.Features;
using Exiled.API.Features.Roles;
using Interfaces;
using PlayerRoles.PlayableScps.Subroutines;

/// <summary>
/// Contains all information after zombie sends an attack.
/// </summary>
public class HitEventArgs : IScp0492Event
{
/// <summary>
/// Initializes a new instance of the <see cref="HitEventArgs"/> class.
/// </summary>
/// <param name="player"> <inheritdoc cref="Player"/></param>
/// <param name="result"> the result of the attack.</param>
/// <param name="playerHits"> the list of players who are getting hit.</param>
public HitEventArgs(ReferenceHub player, AttackResult result, HashSet<ReferenceHub> playerHits)
{
Player = Player.Get(player);
Scp0492 = Player.Role.As<Scp0492Role>();
Result = result;
PlayersAffected = playerHits.Select(Player.Get).ToList().AsReadOnly();
}

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

/// <inheritdoc />
public Scp0492Role Scp0492 { get; }

/// <summary>
/// Gets the attack result for the server.
/// </summary>
public AttackResult Result { get; }

/// <summary>
/// Gets the attack result for the server.
/// </summary>
public IReadOnlyCollection<Player> PlayersAffected { get; }
}
}
11 changes: 11 additions & 0 deletions EXILED/Exiled.Events/Handlers/Scp0492.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace Exiled.Events.Handlers
/// </summary>
public class Scp0492
{
/// <summary>
/// Invoked after a player triggers the attack.
/// </summary>
public static Event<HitEventArgs> Hit { get; set; } = new ();

/// <summary>
/// Invoked before a player triggers the bloodlust effect for 049-2.
/// </summary>
Expand All @@ -32,6 +37,12 @@ public class Scp0492
/// </summary>
public static Event<ConsumingCorpseEventArgs> ConsumingCorpse { get; set; } = new();

/// <summary>
/// Called after a player triggers the attack.
/// </summary>
/// <param name="ev">The <see cref="HitEventArgs"/> instance.</param>
public static void OnHit(HitEventArgs ev) => Hit.InvokeSafely(ev);

/// <summary>
/// Called before a player triggers the bloodlust effect for 049-2.
/// </summary>
Expand Down
71 changes: 71 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// -----------------------------------------------------------------------
// <copyright file="Hit.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Events.Scp0492
{
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features.Pools;
using Attributes;
using Exiled.Events.EventArgs.Scp0492;
using HarmonyLib;
using PlayerRoles.PlayableScps.Scp049.Zombies;
using PlayerRoles.PlayableScps.Subroutines;
using PlayerRoles.Subroutines;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches ScpAttackAbilityBase.ServerPerformAttack
/// to add <see cref="Handlers.Scp0492.Hit" /> event.
/// </summary>
[EventPatch(typeof(Handlers.Scp0492), nameof(Handlers.Scp0492.Hit))]
[HarmonyPatch(typeof(ScpAttackAbilityBase<ZombieRole>), nameof(ScpAttackAbilityBase<ZombieRole>.ServerPerformAttack))]
public class Hit
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

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

int offset = -1;
int index = newInstructions.FindLastIndex(x => x.opcode == OpCodes.Ret) + offset;

newInstructions.InsertRange(
index,
new[]
{
// base.Owner
new CodeInstruction(OpCodes.Ldarg_0),
new(OpCodes.Call, PropertyGetter(typeof(StandardSubroutine<ZombieRole>), nameof(StandardSubroutine<ZombieRole>.Owner))),

// this.LastAttackResult
new CodeInstruction(OpCodes.Ldarg_0),
new(OpCodes.Call, PropertyGetter(typeof(ScpAttackAbilityBase<ZombieRole>), nameof(ScpAttackAbilityBase<ZombieRole>.LastAttackResult))),

// this.DetectedPlayers
new CodeInstruction(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(ScpAttackAbilityBase<ZombieRole>), nameof(ScpAttackAbilityBase<ZombieRole>.DetectedPlayers))),

// HitEventArgs ev = new(ReferenceHub, AttackResult, HashSet<ReferenceHub>)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(HitEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),

// Handlers.Scp049.OnHit(ev)
new(OpCodes.Call, Method(typeof(Handlers.Scp0492), nameof(Handlers.Scp0492.OnHit))),
});

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

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