Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FlashBang Showing hitmarker with flash when it's should not #2526

Merged
merged 3 commits into from
May 10, 2024
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
7 changes: 5 additions & 2 deletions Exiled.Events/EventArgs/Map/ExplodingGrenadeEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,18 @@ public ExplodingGrenadeEventArgs(Footprint thrower, Vector3 position, ExplosionG
/// <param name="grenade">
/// <inheritdoc cref="Projectile" />
/// </param>
/// <param name="targetsToAffect">
/// <inheritdoc cref="TargetsToAffect" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public ExplodingGrenadeEventArgs(Player thrower, EffectGrenade grenade, bool isAllowed = true)
public ExplodingGrenadeEventArgs(Player thrower, EffectGrenade grenade, List<Player> targetsToAffect, bool isAllowed = true)
{
Player = thrower ?? Server.Host;
Projectile = (EffectGrenadeProjectile)Pickup.Get(grenade);
Position = Projectile.Position;
TargetsToAffect = ListPool<Player>.Pool.Get(Player.List);
TargetsToAffect = ListPool<Player>.Pool.Get(targetsToAffect ?? new());
IsAllowed = isAllowed;
}

Expand Down
9 changes: 6 additions & 3 deletions Exiled.Events/Patches/Events/Map/BreakingScp2176.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// this
new(OpCodes.Ldarg_0),

// Is Allowed
// null
new(OpCodes.Ldnull),

// true
new(OpCodes.Ldc_I4_1),

// new ExplodingGrenadeEventArgs(Player, EffectGrenade)
new(OpCodes.Newobj, DeclaredConstructor(typeof(ExplodingGrenadeEventArgs), new[] { typeof(Player), typeof(EffectGrenade), typeof(bool) })),
// new ExplodingGrenadeEventArgs(Player, EffectGrenade, List<Player>, bool)
new(OpCodes.Newobj, DeclaredConstructor(typeof(ExplodingGrenadeEventArgs), new[] { typeof(Player), typeof(EffectGrenade), typeof(List<Player>), typeof(bool) })),
new(OpCodes.Dup),

// Handlers.Map.OnExplodingGrenade(ev);
Expand Down
59 changes: 32 additions & 27 deletions Exiled.Events/Patches/Events/Map/ExplodingFlashGrenade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Exiled.Events.Patches.Events.Map
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
Expand Down Expand Up @@ -35,59 +36,63 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

// Immediately return
Label returnLabel = generator.DefineLabel();
Label ignoreLabel = generator.DefineLabel();

int offset = 4;
int index = newInstructions.FindLastIndex(instruction => instruction.Calls(PropertyGetter(typeof(Keyframe), nameof(Keyframe.time)))) + offset;
int offset = 1;
int index = newInstructions.FindLastIndex(instruction => instruction.StoresField(Field(typeof(FlashbangGrenade), nameof(FlashbangGrenade._hitPlayerCount)))) + offset;

newInstructions.InsertRange(
index,
new[]
{
// FlashbangGrenade
// ExplodingFlashGrenade.ProcessEvent(this, num)
new CodeInstruction(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Ldarg_0),

// Processes ExplodingGrenadeEventArgs and stores flashed players count
new CodeInstruction(OpCodes.Ldloc_0),
new(OpCodes.Call, Method(typeof(ExplodingFlashGrenade), nameof(ProcessEvent))),
new(OpCodes.Stfld, Field(typeof(FlashbangGrenade), nameof(FlashbangGrenade._hitPlayerCount))),
new(OpCodes.Br_S, returnLabel),

// ignore the foreach since exiled overwrite it
new(OpCodes.Br_S, ignoreLabel),
});

newInstructions[newInstructions.FindLastIndex(i => i.opcode == OpCodes.Ble_S) - 3].WithLabels(returnLabel);
newInstructions[newInstructions.FindLastIndex(i => i.opcode == OpCodes.Ble_S) - 3].WithLabels(ignoreLabel);

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

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

private static int ProcessEvent(FlashbangGrenade instance)
private static void ProcessEvent(FlashbangGrenade instance, float distance)
{
ExplodingGrenadeEventArgs explodingGrenadeEvent = new ExplodingGrenadeEventArgs(Player.Get(instance.PreviousOwner.Hub), instance);
List<Player> targetToAffect = ListPool<Player>.Pool.Get();
foreach (ReferenceHub referenceHub in ReferenceHub.AllHubs)
{
Player player = Player.Get(referenceHub);
if ((instance.transform.position - referenceHub.transform.position).sqrMagnitude <= distance)
continue;
if (!ExiledEvents.Instance.Config.CanFlashbangsAffectThrower && instance.PreviousOwner.SameLife(new(referenceHub)))
continue;
if (!IndividualFriendlyFire.CheckFriendlyFirePlayer(instance.PreviousOwner, player.ReferenceHub))
continue;
if (!Physics.Linecast(instance.transform.position, player.Position, instance._blindingMask))
continue;

targetToAffect.Add(Player.Get(referenceHub));
}

ExplodingGrenadeEventArgs explodingGrenadeEvent = new(Player.Get(instance.PreviousOwner.Hub), instance, targetToAffect);

ListPool<Player>.Pool.Return(targetToAffect);

Handlers.Map.OnExplodingGrenade(explodingGrenadeEvent);

if (!explodingGrenadeEvent.IsAllowed)
return 0;
return;

int size = 0;
foreach (Player player in explodingGrenadeEvent.TargetsToAffect)
{
if (!ExiledEvents.Instance.Config.CanFlashbangsAffectThrower && explodingGrenadeEvent.Player == player)
continue;

if (IndividualFriendlyFire.CheckFriendlyFirePlayer(instance.PreviousOwner, player.ReferenceHub))
{
instance.ProcessPlayer(player.ReferenceHub);
size++;
}
instance.ProcessPlayer(player.ReferenceHub);
}

return size;
}

private static List<Player> ConvertHubs(List<ReferenceHub> hubs) => hubs.Select(Player.Get).ToList();
}
}
Loading