Skip to content

Commit

Permalink
Stalk events fix & PlaceBulletHole decal fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xNexusACS committed Dec 6, 2024
1 parent 0475412 commit 00a919e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 69 deletions.
14 changes: 11 additions & 3 deletions Exiled.Events/EventArgs/Map/PlacingBulletHoleEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
namespace Exiled.Events.EventArgs.Map
{
using API.Features;

using Exiled.API.Features.Items;
using Interfaces;

using UnityEngine;

/// <summary>
Expand All @@ -27,11 +26,15 @@ public class PlacingBulletHoleEventArgs : IPlayerEvent, IDeniableEvent
/// <param name="hit">
/// <inheritdoc cref="RaycastHit" />
/// </param>
public PlacingBulletHoleEventArgs(Player owner, RaycastHit hit)
/// <param name="firearm">
/// <inheritdoc cref="Firearm" />
/// </param>
public PlacingBulletHoleEventArgs(Player owner, RaycastHit hit, InventorySystem.Items.Firearms.Firearm firearm)
{
Player = owner;
Position = hit.point;
Rotation = Quaternion.LookRotation(hit.normal);
Firearm = Item.Get(firearm).As<Firearm>();
}

/// <summary>
Expand All @@ -53,5 +56,10 @@ public PlacingBulletHoleEventArgs(Player owner, RaycastHit hit)
/// Gets the decal owner.
/// </summary>
public Player Player { get; }

/// <summary>
/// Gets the firearm that triggered the event.
/// </summary>
public Firearm Firearm { get; }
}
}
2 changes: 1 addition & 1 deletion Exiled.Events/Patches/Events/Map/ExplodingFragGrenade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Exiled.Events.Patches.Events.Map
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="ExplosionGrenade.Explode(Footprint, Vector3, ExplosionGrenade)"/>.
/// Patches <see cref="ExplosionGrenade.Explode(Footprint, Vector3, ExplosionGrenade, ExplosionType)"/>.
/// Adds the <see cref="Handlers.Map.ExplodingGrenade"/> event.
/// </summary>
[EventPatch(typeof(Handlers.Map), nameof(Handlers.Map.ExplodingGrenade))]
Expand Down
19 changes: 10 additions & 9 deletions Exiled.Events/Patches/Events/Map/PlacingBulletHole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,24 @@ namespace Exiled.Events.Patches.Events.Map
using System.Reflection.Emit;

using API.Features.Core.Generic.Pools;

using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Map;

using Handlers;

using HarmonyLib;

using InventorySystem.Items.Firearms;
using InventorySystem.Items.Firearms.Modules;

using UnityEngine;

using static HarmonyLib.AccessTools;

using Player = API.Features.Player;

/// <summary>
/// Patches <see cref="StandardHitregBase.PlaceBulletholeDecal" />.
/// Patches <see cref="ImpactEffectsModule.ServerSendImpactDecal" />.
/// Adds the <see cref="Map.PlacingBulletHole" /> event.
/// </summary>
[EventPatch(typeof(Map), nameof(Map.PlacingBulletHole))]
[HarmonyPatch(typeof(StandardHitregBase), nameof(StandardHitregBase.PlaceBulletholeDecal))]
[HarmonyPatch(typeof(ImpactEffectsModule), nameof(ImpactEffectsModule.ServerSendImpactDecal))]
internal static class PlacingBulletHole
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
Expand All @@ -50,11 +46,16 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
// Player.Get(this.Hub)
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(SingleBulletHitreg), nameof(SingleBulletHitreg.Hub))),
new(OpCodes.Callvirt, PropertyGetter(typeof(ImpactEffectsModule), nameof(ImpactEffectsModule.Firearm))),
new(OpCodes.Callvirt, PropertyGetter(typeof(Firearm), nameof(Firearm.Owner))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// hit
new(OpCodes.Ldarg_2),
new(OpCodes.Ldarg_1),

// this.Firearm
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(ImpactEffectsModule), nameof(ImpactEffectsModule.Firearm))),

// PlacingBulletHole ev = new(Player, RaycastHit)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(PlacingBulletHoleEventArgs))[0]),
Expand Down
29 changes: 10 additions & 19 deletions Exiled.Events/Patches/Events/Scp106/ExitStalking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace Exiled.Events.Patches.Events.Scp106
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Scp106StalkAbility.ServerProcessCmd"/>.
/// Patches <see cref="Scp106StalkAbility.UpdateServerside"/>.
/// To add the <see cref="Handlers.Scp106.ExitStalking"/> event.
/// </summary>
[EventPatch(typeof(Handlers.Scp106), nameof(Handlers.Scp106.ExitStalking))]
[HarmonyPatch(typeof(Scp106StalkAbility), nameof(Scp106StalkAbility.IsActive), MethodType.Setter)]
[HarmonyPatch(typeof(Scp106StalkAbility), nameof(Scp106StalkAbility.UpdateServerside))]
public class ExitStalking
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
Expand All @@ -34,22 +34,17 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

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

Label continueLabel = generator.DefineLabel();
Label returnLabel = generator.DefineLabel();
int offset = -3;
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Newobj) + offset;

const int offset = -2;
int index = newInstructions.FindIndex(instruction => instruction.OperandIs(Method(typeof(Scp106StalkAbility), nameof(Scp106StalkAbility.ServerSetStalk), new[] { typeof(bool) }))) + offset;
newInstructions.InsertRange(
index,
new CodeInstruction[]
new[]
{
// if (value is false) continue;
new CodeInstruction(OpCodes.Ldarg_1).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Ldc_I4_0),
new(OpCodes.Beq_S, continueLabel),

// Player.Get(this.Owner);
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp106HuntersAtlasAbility), nameof(Scp106HuntersAtlasAbility.Owner))),
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp106StalkAbility), nameof(Scp106StalkAbility.Owner))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// true
Expand All @@ -70,14 +65,10 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Brfalse_S, returnLabel),
});

offset = -3;
index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Newobj) + offset;
newInstructions[index].labels.Add(continueLabel);

newInstructions[newInstructions.Count - 1].labels.Add(returnLabel);

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

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
Expand Down
44 changes: 7 additions & 37 deletions Exiled.Events/Patches/Events/Scp106/Stalking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
LocalBuilder ev = generator.DeclareLocal(typeof(StalkingEventArgs));

Label returnLabel = generator.DefineLabel();
int offset = 2;
int index = newInstructions.FindIndex(instruction => instruction.operand == (object)PropertyGetter(typeof(Scp106StalkAbility), nameof(Scp106StalkAbility.IsActive))) + offset;
const int offset = -1;
int index = newInstructions.FindIndex(instruction => instruction.Calls(PropertyGetter(typeof(Scp106VigorAbilityBase), nameof(Scp106VigorAbilityBase.VigorAmount)))) + offset;
newInstructions.InsertRange(
index,
new CodeInstruction[]
new[]
{
// Player.Get(this.Owner);
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp106HuntersAtlasAbility), nameof(Scp106HuntersAtlasAbility.Owner))),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp106StalkAbility), nameof(Scp106StalkAbility.Owner))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// true
Expand All @@ -64,38 +64,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Brfalse_S, returnLabel),
});

offset = 2;
index = newInstructions.FindIndex(instruction => instruction.operand == (object)PropertySetter(typeof(Scp106StalkAbility), nameof(Scp106StalkAbility.IsActive))) + offset;
newInstructions.InsertRange(
index,
new CodeInstruction[]
{
// Player.Get(this.Owner);
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp106HuntersAtlasAbility), nameof(Scp106HuntersAtlasAbility.Owner))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// true
new(OpCodes.Ldc_I4_1),

// StalkingEventArgs ev = new(Player, isAllowed)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(StalkingEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),

// Handlers.Scp106.OnStalking(ev)
new(OpCodes.Call, Method(typeof(Handlers.Scp106), nameof(Handlers.Scp106.OnStalking))),

// if (!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(StalkingEventArgs), nameof(StalkingEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, returnLabel),
});

// replace "base.Vigor.VigorAmount < 0.25f" with "base.Vigor.VigorAmount < ev.MinimumVigor"
offset = 0;
index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ldc_R4) + offset;
index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ldc_R4);
newInstructions.RemoveAt(index);

newInstructions.InsertRange(
Expand All @@ -109,8 +79,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

newInstructions[newInstructions.Count - 1].WithLabels(returnLabel);

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

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

0 comments on commit 00a919e

Please sign in to comment.