Skip to content
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
48 changes: 48 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Player/HitEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// -----------------------------------------------------------------------
// <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.Player
{
using System.Collections.Generic;
using System.Linq;

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

/// <summary>
/// Contains all information after player sends an attack as an SCP.
/// </summary>
public class HitEventArgs : IPlayerEvent
{
/// <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(Player player, AttackResult result, HashSet<ReferenceHub> playerHits)
{
Player = player;
Result = result;
PlayersAffected = playerHits.Select(Player.Get).ToList().AsReadOnly();
}

/// <inheritdoc />
public Player Player { 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/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ namespace Exiled.Events.Handlers
/// </summary>
public class Player
{
/// <summary>
/// Invoked after a player triggers the attack as an SCP.
/// </summary>
public static Event<HitEventArgs> Hit { get; set; } = new ();

/// <summary>
/// Invoked before authenticating a <see cref="API.Features.Player"/>.
/// </summary>
Expand Down Expand Up @@ -1294,5 +1299,11 @@ public static void OnItemRemoved(ReferenceHub referenceHub, InventorySystem.Item
/// </summary>
/// <param name="ev"><The cref="PreAuthenticatingEventArgs"/> instance.</param>
public static void OnPreAuthenticating(PreAuthenticatingEventArgs ev) => PreAuthenticating.InvokeSafely(ev);

/// <summary>
/// Called after a player triggers the melee attack as an SCP.
/// </summary>
/// <param name="ev">The <see cref="HitEventArgs"/> instance.</param>
public static void OnHit(HitEventArgs ev) => Hit.InvokeSafely(ev);
}
}
68 changes: 68 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Player/Hit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// -----------------------------------------------------------------------
// <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.Player
{
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features;
using API.Features.Pools;
using Attributes;
using Exiled.Events.EventArgs.Player;
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.Player.Hit" /> event.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.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);

int index = newInstructions.FindLastIndex(x => x.opcode == OpCodes.Ret);

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

// 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))),

// new(ReferenceHub, AttackResult, HashSet<ReferenceHub>)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(HitEventArgs))[0]),

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

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

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