From d1d8a2eba396513e2f65b36d5a9029f5dca2f0de Mon Sep 17 00:00:00 2001 From: maro Date: Sun, 13 Apr 2025 18:07:27 -0400 Subject: [PATCH 1/5] 049-2 HitEvents --- .../EventArgs/Scp0492/HitEventArgs.cs | 55 ++++++++++++++ EXILED/Exiled.Events/Handlers/Scp0492.cs | 11 +++ .../Patches/Events/Scp0492/Hit.cs | 71 +++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs create mode 100644 EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs diff --git a/EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs new file mode 100644 index 0000000000..149a11a67b --- /dev/null +++ b/EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs @@ -0,0 +1,55 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +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; + + /// + /// Contains all information after zombie sends an attack. + /// + public class HitEventArgs : IScp0492Event + { + /// + /// Initializes a new instance of the class. + /// + /// + /// the result of the attack. + /// the list of players who are getting hit. + public HitEventArgs(ReferenceHub player, AttackResult result, HashSet playerHits) + { + Player = Player.Get(player); + Scp0492 = Player.Role.As(); + Result = result; + PlayersAffected = playerHits.Select(Player.Get).ToList().AsReadOnly(); + } + + /// + /// Gets the player who is controlling SCP-049-2. + /// + public Player Player { get; } + + /// + public Scp0492Role Scp0492 { get; } + + /// + /// Gets the attack result for the server. + /// + public AttackResult Result { get; } + + /// + /// Gets the attack result for the server. + /// + public IReadOnlyCollection PlayersAffected { get; } + } +} \ No newline at end of file diff --git a/EXILED/Exiled.Events/Handlers/Scp0492.cs b/EXILED/Exiled.Events/Handlers/Scp0492.cs index 0d03a3727f..b98c08fd15 100644 --- a/EXILED/Exiled.Events/Handlers/Scp0492.cs +++ b/EXILED/Exiled.Events/Handlers/Scp0492.cs @@ -17,6 +17,11 @@ namespace Exiled.Events.Handlers /// public class Scp0492 { + /// + /// Invoked after a player triggers the attack. + /// + public static Event Hit { get; set; } = new (); + /// /// Invoked before a player triggers the bloodlust effect for 049-2. /// @@ -32,6 +37,12 @@ public class Scp0492 /// public static Event ConsumingCorpse { get; set; } = new(); + /// + /// Called after a player triggers the attack. + /// + /// The instance. + public static void OnHit(HitEventArgs ev) => Hit.InvokeSafely(ev); + /// /// Called before a player triggers the bloodlust effect for 049-2. /// diff --git a/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs b/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs new file mode 100644 index 0000000000..fe6d3ea602 --- /dev/null +++ b/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs @@ -0,0 +1,71 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +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; + + /// + /// Patches ScpAttackAbilityBase.ServerPerformAttack + /// to add event. + /// + [EventPatch(typeof(Handlers.Scp0492), nameof(Handlers.Scp0492.Hit))] + [HarmonyPatch(typeof(ScpAttackAbilityBase), nameof(ScpAttackAbilityBase.ServerPerformAttack))] + public class Hit + { + private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.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), nameof(StandardSubroutine.Owner))), + + // this.LastAttackResult + new CodeInstruction(OpCodes.Ldarg_0), + new(OpCodes.Call, PropertyGetter(typeof(ScpAttackAbilityBase), nameof(ScpAttackAbilityBase.LastAttackResult))), + + // this.DetectedPlayers + new CodeInstruction(OpCodes.Ldarg_0), + new(OpCodes.Ldfld, Field(typeof(ScpAttackAbilityBase), nameof(ScpAttackAbilityBase.DetectedPlayers))), + + // HitEventArgs ev = new(ReferenceHub, AttackResult, HashSet) + 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.Pool.Return(newInstructions); + } + } +} \ No newline at end of file From 40591cd9d5d7d78bb1768128b607c52cccf899b1 Mon Sep 17 00:00:00 2001 From: maro Date: Mon, 14 Apr 2025 14:37:01 -0400 Subject: [PATCH 2/5] 939Check --- .../EventArgs/Scp0492/HitEventArgs.cs | 4 ++-- .../Patches/Events/Scp0492/Hit.cs | 20 ++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs index 149a11a67b..5a387f4e0f 100644 --- a/EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs @@ -26,9 +26,9 @@ public class HitEventArgs : IScp0492Event /// /// the result of the attack. /// the list of players who are getting hit. - public HitEventArgs(ReferenceHub player, AttackResult result, HashSet playerHits) + public HitEventArgs(Player player, AttackResult result, HashSet playerHits) { - Player = Player.Get(player); + Player = player; Scp0492 = Player.Role.As(); Result = result; PlayersAffected = playerHits.Select(Player.Get).ToList().AsReadOnly(); diff --git a/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs b/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs index fe6d3ea602..ed75efa1e8 100644 --- a/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs +++ b/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs @@ -10,6 +10,7 @@ namespace Exiled.Events.Patches.Events.Scp0492 using System.Collections.Generic; using System.Reflection.Emit; + using API.Features; using API.Features.Pools; using Attributes; using Exiled.Events.EventArgs.Scp0492; @@ -20,6 +21,8 @@ namespace Exiled.Events.Patches.Events.Scp0492 using static HarmonyLib.AccessTools; + using Scp939Role = PlayerRoles.PlayableScps.Scp939.Scp939Role; + /// /// Patches ScpAttackAbilityBase.ServerPerformAttack /// to add event. @@ -34,16 +37,24 @@ private static IEnumerable Transpiler(IEnumerable x.opcode == OpCodes.Ret) + offset; + int index = newInstructions.FindLastIndex(x => x.opcode == OpCodes.Ret); + + Label skipEventLabel = generator.DefineLabel(); newInstructions.InsertRange( index, new[] { - // base.Owner + // if(this is ScpAttackAbilityBase) + // goto skip; + new CodeInstruction(OpCodes.Ldarg_0), + new CodeInstruction(OpCodes.Isinst, typeof(ScpAttackAbilityBase)), + new CodeInstruction(OpCodes.Brtrue_S, skipEventLabel), + + // Player.Get(base.Owner); new CodeInstruction(OpCodes.Ldarg_0), new(OpCodes.Call, PropertyGetter(typeof(StandardSubroutine), nameof(StandardSubroutine.Owner))), + new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })), // this.LastAttackResult new CodeInstruction(OpCodes.Ldarg_0), @@ -60,6 +71,9 @@ private static IEnumerable Transpiler(IEnumerable Date: Wed, 16 Apr 2025 12:26:56 -0400 Subject: [PATCH 3/5] optimize transpiler --- EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs b/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs index ed75efa1e8..664892d76d 100644 --- a/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs +++ b/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs @@ -35,8 +35,6 @@ private static IEnumerable Transpiler(IEnumerable newInstructions = ListPool.Pool.Get(instructions); - LocalBuilder ev = generator.DeclareLocal(typeof(HitEventArgs)); - int index = newInstructions.FindLastIndex(x => x.opcode == OpCodes.Ret); Label skipEventLabel = generator.DefineLabel(); @@ -64,10 +62,8 @@ private static IEnumerable Transpiler(IEnumerable), nameof(ScpAttackAbilityBase.DetectedPlayers))), - // HitEventArgs ev = new(ReferenceHub, AttackResult, HashSet) + // new(ReferenceHub, AttackResult, HashSet) 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))), From 9679019b44bfe15be3df5192815230989d68b460 Mon Sep 17 00:00:00 2001 From: maro Date: Fri, 18 Apr 2025 10:57:13 -0400 Subject: [PATCH 4/5] changed hitevent to generic for player --- .../{Scp0492 => Player}/HitEventArgs.cs | 15 +++-------- EXILED/Exiled.Events/Handlers/Player.cs | 11 ++++++++ EXILED/Exiled.Events/Handlers/Scp0492.cs | 10 -------- .../Patches/Events/{Scp0492 => Player}/Hit.cs | 25 +++++-------------- 4 files changed, 21 insertions(+), 40 deletions(-) rename EXILED/Exiled.Events/EventArgs/{Scp0492 => Player}/HitEventArgs.cs (79%) rename EXILED/Exiled.Events/Patches/Events/{Scp0492 => Player}/Hit.cs (73%) diff --git a/EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/HitEventArgs.cs similarity index 79% rename from EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs rename to EXILED/Exiled.Events/EventArgs/Player/HitEventArgs.cs index 5a387f4e0f..d9d18adc0e 100644 --- a/EXILED/Exiled.Events/EventArgs/Scp0492/HitEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/HitEventArgs.cs @@ -5,20 +5,19 @@ // // ----------------------------------------------------------------------- -namespace Exiled.Events.EventArgs.Scp0492 +namespace Exiled.Events.EventArgs.Player { using System.Collections.Generic; using System.Linq; using API.Features; - using Exiled.API.Features.Roles; using Interfaces; using PlayerRoles.PlayableScps.Subroutines; /// - /// Contains all information after zombie sends an attack. + /// Contains all information after player sends an attack as an SCP. /// - public class HitEventArgs : IScp0492Event + public class HitEventArgs : IPlayerEvent { /// /// Initializes a new instance of the class. @@ -29,18 +28,12 @@ public class HitEventArgs : IScp0492Event public HitEventArgs(Player player, AttackResult result, HashSet playerHits) { Player = player; - Scp0492 = Player.Role.As(); Result = result; PlayersAffected = playerHits.Select(Player.Get).ToList().AsReadOnly(); } - /// - /// Gets the player who is controlling SCP-049-2. - /// - public Player Player { get; } - /// - public Scp0492Role Scp0492 { get; } + public Player Player { get; } /// /// Gets the attack result for the server. diff --git a/EXILED/Exiled.Events/Handlers/Player.cs b/EXILED/Exiled.Events/Handlers/Player.cs index 60cbd55ff6..e272c42f3c 100644 --- a/EXILED/Exiled.Events/Handlers/Player.cs +++ b/EXILED/Exiled.Events/Handlers/Player.cs @@ -26,6 +26,11 @@ namespace Exiled.Events.Handlers /// public class Player { + /// + /// Invoked after a player triggers the attack as an SCP. + /// + public static Event Hit { get; set; } = new (); + /// /// Invoked before authenticating a . /// @@ -1294,5 +1299,11 @@ public static void OnItemRemoved(ReferenceHub referenceHub, InventorySystem.Item /// /// instance. public static void OnPreAuthenticating(PreAuthenticatingEventArgs ev) => PreAuthenticating.InvokeSafely(ev); + + /// + /// Called after a player triggers the melee attack as an SCP. + /// + /// The instance. + public static void OnHit(HitEventArgs ev) => Hit.InvokeSafely(ev); } } diff --git a/EXILED/Exiled.Events/Handlers/Scp0492.cs b/EXILED/Exiled.Events/Handlers/Scp0492.cs index b98c08fd15..dea8928b83 100644 --- a/EXILED/Exiled.Events/Handlers/Scp0492.cs +++ b/EXILED/Exiled.Events/Handlers/Scp0492.cs @@ -17,10 +17,6 @@ namespace Exiled.Events.Handlers /// public class Scp0492 { - /// - /// Invoked after a player triggers the attack. - /// - public static Event Hit { get; set; } = new (); /// /// Invoked before a player triggers the bloodlust effect for 049-2. @@ -37,12 +33,6 @@ public class Scp0492 /// public static Event ConsumingCorpse { get; set; } = new(); - /// - /// Called after a player triggers the attack. - /// - /// The instance. - public static void OnHit(HitEventArgs ev) => Hit.InvokeSafely(ev); - /// /// Called before a player triggers the bloodlust effect for 049-2. /// diff --git a/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs b/EXILED/Exiled.Events/Patches/Events/Player/Hit.cs similarity index 73% rename from EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs rename to EXILED/Exiled.Events/Patches/Events/Player/Hit.cs index 664892d76d..2002397b50 100644 --- a/EXILED/Exiled.Events/Patches/Events/Scp0492/Hit.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/Hit.cs @@ -5,7 +5,7 @@ // // ----------------------------------------------------------------------- -namespace Exiled.Events.Patches.Events.Scp0492 +namespace Exiled.Events.Patches.Events.Player { using System.Collections.Generic; using System.Reflection.Emit; @@ -13,7 +13,7 @@ namespace Exiled.Events.Patches.Events.Scp0492 using API.Features; using API.Features.Pools; using Attributes; - using Exiled.Events.EventArgs.Scp0492; + using Exiled.Events.EventArgs.Player; using HarmonyLib; using PlayerRoles.PlayableScps.Scp049.Zombies; using PlayerRoles.PlayableScps.Subroutines; @@ -21,13 +21,11 @@ namespace Exiled.Events.Patches.Events.Scp0492 using static HarmonyLib.AccessTools; - using Scp939Role = PlayerRoles.PlayableScps.Scp939.Scp939Role; - /// /// Patches ScpAttackAbilityBase.ServerPerformAttack - /// to add event. + /// to add event. /// - [EventPatch(typeof(Handlers.Scp0492), nameof(Handlers.Scp0492.Hit))] + [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.Hit))] [HarmonyPatch(typeof(ScpAttackAbilityBase), nameof(ScpAttackAbilityBase.ServerPerformAttack))] public class Hit { @@ -37,18 +35,10 @@ private static IEnumerable Transpiler(IEnumerable x.opcode == OpCodes.Ret); - Label skipEventLabel = generator.DefineLabel(); - newInstructions.InsertRange( index, new[] { - // if(this is ScpAttackAbilityBase) - // goto skip; - new CodeInstruction(OpCodes.Ldarg_0), - new CodeInstruction(OpCodes.Isinst, typeof(ScpAttackAbilityBase)), - new CodeInstruction(OpCodes.Brtrue_S, skipEventLabel), - // Player.Get(base.Owner); new CodeInstruction(OpCodes.Ldarg_0), new(OpCodes.Call, PropertyGetter(typeof(StandardSubroutine), nameof(StandardSubroutine.Owner))), @@ -65,11 +55,8 @@ private static IEnumerable Transpiler(IEnumerable) new(OpCodes.Newobj, GetDeclaredConstructors(typeof(HitEventArgs))[0]), - // Handlers.Scp049.OnHit(ev) - new(OpCodes.Call, Method(typeof(Handlers.Scp0492), nameof(Handlers.Scp0492.OnHit))), - - // skip: - new CodeInstruction(OpCodes.Nop).WithLabels(skipEventLabel), + // Handlers.Player.OnHit(ev) + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnHit))), }); for (int z = 0; z < newInstructions.Count; z++) From 16a62b5b45a2c3158030e4f0f09e24b35c0b91a4 Mon Sep 17 00:00:00 2001 From: maro Date: Fri, 18 Apr 2025 11:02:28 -0400 Subject: [PATCH 5/5] linefix --- EXILED/Exiled.Events/Handlers/Scp0492.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/EXILED/Exiled.Events/Handlers/Scp0492.cs b/EXILED/Exiled.Events/Handlers/Scp0492.cs index dea8928b83..0d03a3727f 100644 --- a/EXILED/Exiled.Events/Handlers/Scp0492.cs +++ b/EXILED/Exiled.Events/Handlers/Scp0492.cs @@ -17,7 +17,6 @@ namespace Exiled.Events.Handlers /// public class Scp0492 { - /// /// Invoked before a player triggers the bloodlust effect for 049-2. ///