-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix TogglingWeaponFlashlight & AimingDownSight events
- Loading branch information
Showing
5 changed files
with
188 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
Exiled.Events/Patches/Events/Player/FirearmActionEvents/Aiming.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="Aiming.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.Events.Patches.Events.Player.FirearmActionEvents | ||
{ | ||
using System.Collections.Generic; | ||
using System.Reflection.Emit; | ||
|
||
using Exiled.API.Features.Core.Generic.Pools; | ||
using Exiled.API.Features.Items; | ||
using Exiled.Events.Attributes; | ||
using Exiled.Events.EventArgs.Player; | ||
using HarmonyLib; | ||
using InventorySystem.Items.Firearms.Modules; | ||
|
||
using static HarmonyLib.AccessTools; | ||
|
||
/// <summary> | ||
/// Patches <see cref="LinearAdsModule.ServerProcessCmd" />. | ||
/// Adds <see cref="Handlers.Player.AimingDownSight" /> event. | ||
/// </summary> | ||
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.AimingDownSight))] | ||
[HarmonyPatch(typeof(LinearAdsModule), nameof(LinearAdsModule.ServerProcessCmd))] | ||
internal static class Aiming | ||
{ | ||
[HarmonyTranspiler] | ||
private static IEnumerable<CodeInstruction> OnAimStatusChanged(IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
{ | ||
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions); | ||
|
||
Label returnLabel = generator.DefineLabel(); | ||
|
||
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ret); | ||
|
||
newInstructions.InsertRange(index, new[] | ||
{ | ||
// Player.Get(this.Firearm.Owner) | ||
new CodeInstruction(OpCodes.Ldarg_0), | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(LinearAdsModule), nameof(LinearAdsModule.Firearm))), | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(InventorySystem.Items.Firearms.Firearm), nameof(InventorySystem.Items.Firearms.Firearm.Owner))), | ||
new(OpCodes.Call, Method(typeof(API.Features.Player), nameof(API.Features.Player.Get), new[] { typeof(ReferenceHub) })), | ||
|
||
// (Firearm)Item.Get(this.Firearm) | ||
new(OpCodes.Ldarg_0), | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(LinearAdsModule), nameof(LinearAdsModule.Firearm))), | ||
new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(InventorySystem.Items.Firearms.Firearm) })), | ||
new(OpCodes.Castclass, typeof(Firearm)), | ||
|
||
// this._userInput | ||
new(OpCodes.Ldfld, Field(typeof(LinearAdsModule), nameof(LinearAdsModule._userInput))), | ||
|
||
// AimingDownSightEventArgs args = new(Player, Firearm, bool) | ||
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(AimingDownSightEventArgs))[0]), | ||
|
||
// Player.OnAimingDownSight(args) | ||
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnAimingDownSight))), | ||
}); | ||
|
||
newInstructions[newInstructions.Count - 1].labels.Add(returnLabel); | ||
|
||
foreach (CodeInstruction instruction in newInstructions) | ||
yield return instruction; | ||
|
||
ListPool<CodeInstruction>.Pool.Return(newInstructions); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
Exiled.Events/Patches/Events/Player/FirearmActionEvents/ToggleWeaponFlashlight.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ToggleWeaponFlashlight.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.Events.Patches.Events.Player.FirearmActionEvents | ||
{ | ||
using System.Collections.Generic; | ||
using System.Reflection.Emit; | ||
|
||
using Exiled.API.Features.Core.Generic.Pools; | ||
using Exiled.API.Features.Items; | ||
using Exiled.Events.Attributes; | ||
using Exiled.Events.EventArgs.Player; | ||
using HarmonyLib; | ||
using InventorySystem.Items.Firearms.Attachments; | ||
|
||
using static HarmonyLib.AccessTools; | ||
|
||
/// <summary> | ||
/// Patches <see cref="FlashlightAttachment.ServerSendStatus" />. | ||
/// Adds <see cref="Handlers.Player.TogglingWeaponFlashlight" /> & <see cref="Handlers.Player.ToggledWeaponFlashlight" /> event. | ||
/// </summary> | ||
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.TogglingWeaponFlashlight))] | ||
[HarmonyPatch(typeof(FlashlightAttachment), nameof(FlashlightAttachment.ServerSendStatus))] | ||
internal static class ToggleWeaponFlashlight | ||
{ | ||
[HarmonyTranspiler] | ||
private static IEnumerable<CodeInstruction> OnToggleWeaponFlashlight(IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
{ | ||
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions); | ||
|
||
Label returnLabel = generator.DefineLabel(); | ||
LocalBuilder ev = generator.DeclareLocal(typeof(TogglingWeaponFlashlightEventArgs)); | ||
LocalBuilder firearm = generator.DeclareLocal(typeof(Firearm)); | ||
LocalBuilder player = generator.DeclareLocal(typeof(API.Features.Player)); | ||
LocalBuilder status = generator.DeclareLocal(typeof(bool)); | ||
|
||
newInstructions.InsertRange(0, new CodeInstruction[] | ||
{ | ||
// Player.Get(this.Firearm.Owner) | ||
new(OpCodes.Ldarg_0), | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(FlashlightAttachment), nameof(FlashlightAttachment.Firearm))), | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(InventorySystem.Items.Firearms.Firearm), nameof(InventorySystem.Items.Firearms.Firearm.Owner))), | ||
new(OpCodes.Call, Method(typeof(API.Features.Player), nameof(API.Features.Player.Get), new[] { typeof(ReferenceHub) })), | ||
new(OpCodes.Stloc_S, player.LocalIndex), | ||
|
||
// (Firearm)Item.Get(this.Firearm) | ||
new(OpCodes.Ldarg_0), | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(FlashlightAttachment), nameof(FlashlightAttachment.Firearm))), | ||
new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(InventorySystem.Items.Firearms.Firearm) })), | ||
new(OpCodes.Castclass, typeof(Firearm)), | ||
new(OpCodes.Stloc_S, firearm.LocalIndex), | ||
|
||
// status | ||
new(OpCodes.Ldarg_1), | ||
|
||
// true | ||
new(OpCodes.Ldc_I4_1), | ||
|
||
// TogglingWeaponFlashlightEventArgs args = new(Player, Firearm, bool, bool) | ||
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(TogglingWeaponFlashlightEventArgs))[0]), | ||
new(OpCodes.Dup), | ||
new(OpCodes.Dup), | ||
new(OpCodes.Stloc_S, ev.LocalIndex), | ||
|
||
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnTogglingWeaponFlashlight))), | ||
|
||
// if (!args.IsAllowed) | ||
// return; | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(TogglingWeaponFlashlightEventArgs), nameof(TogglingWeaponFlashlightEventArgs.IsAllowed))), | ||
new(OpCodes.Brfalse_S, returnLabel), | ||
|
||
// status = NewState | ||
new(OpCodes.Ldloc_S, ev.LocalIndex), | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(TogglingWeaponFlashlightEventArgs), nameof(TogglingWeaponFlashlightEventArgs.NewState))), | ||
new(OpCodes.Starg_S, 1), | ||
new(OpCodes.Ldarg_1), | ||
new(OpCodes.Stloc_S, status.LocalIndex), | ||
}); | ||
|
||
newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[] | ||
{ | ||
// Player | ||
new(OpCodes.Ldloc_S, player.LocalIndex), | ||
|
||
// Firearm | ||
new(OpCodes.Ldloc_S, firearm.LocalIndex), | ||
|
||
// status | ||
new(OpCodes.Ldloc_S, status.LocalIndex), | ||
|
||
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ToggledWeaponFlashlightEventArgs))[0]), | ||
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnToggledWeaponFlashlight))), | ||
}); | ||
|
||
newInstructions[newInstructions.Count - 1].labels.Add(returnLabel); | ||
|
||
foreach (CodeInstruction instruction in newInstructions) | ||
yield return instruction; | ||
|
||
ListPool<CodeInstruction>.Pool.Return(newInstructions); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters