diff --git a/EXILED/Exiled.API/Features/DamageHandlers/GenericDamageHandler.cs b/EXILED/Exiled.API/Features/DamageHandlers/GenericDamageHandler.cs index b146521a55..2008936bea 100644 --- a/EXILED/Exiled.API/Features/DamageHandlers/GenericDamageHandler.cs +++ b/EXILED/Exiled.API/Features/DamageHandlers/GenericDamageHandler.cs @@ -111,7 +111,7 @@ public GenericDamageHandler(Player player, Player attacker, float damage, Damage case DamageType.MicroHid: InventorySystem.Items.MicroHID.MicroHIDItem microHidOwner = new(); microHidOwner.Owner = attacker.ReferenceHub; - Base = new MicroHidDamageHandler(microHidOwner, damage); + Base = new MicroHidDamageHandler(damage, microHidOwner); break; case DamageType.Explosion: Base = new ExplosionDamageHandler(attacker.Footprint, UnityEngine.Vector3.zero, damage, 0, ExplosionType.Grenade); diff --git a/EXILED/Exiled.API/Features/Items/Jailbird.cs b/EXILED/Exiled.API/Features/Items/Jailbird.cs index a560abba55..b7a48eae68 100644 --- a/EXILED/Exiled.API/Features/Items/Jailbird.cs +++ b/EXILED/Exiled.API/Features/Items/Jailbird.cs @@ -11,6 +11,7 @@ namespace Exiled.API.Features.Items using Exiled.API.Features.Pickups; using Exiled.API.Interfaces; + using InventorySystem.Items; using InventorySystem.Items.Autosync; using InventorySystem.Items.Jailbird; using Mirror; @@ -152,13 +153,14 @@ public float GetDamage(JailbirdWearState wearState) public void Break() { WearState = JailbirdWearState.Broken; - using (new AutosyncRpc(Base, true, out NetworkWriter networkWriter)) + ItemIdentifier identifier = new(Base); + using (new AutosyncRpc(identifier, out NetworkWriter networkWriter)) { networkWriter.WriteByte(0); networkWriter.WriteByte((byte)JailbirdWearState.Broken); } - using (new AutosyncRpc(Base, true, out NetworkWriter networkWriter2)) + using (new AutosyncRpc(identifier, out NetworkWriter networkWriter2)) { networkWriter2.WriteByte(1); } diff --git a/EXILED/Exiled.API/Features/Items/MicroHid.cs b/EXILED/Exiled.API/Features/Items/MicroHid.cs index 80bde07088..aaf29b6689 100644 --- a/EXILED/Exiled.API/Features/Items/MicroHid.cs +++ b/EXILED/Exiled.API/Features/Items/MicroHid.cs @@ -7,9 +7,15 @@ namespace Exiled.API.Features.Items { - using Exiled.API.Interfaces; + using System; + using System.Reflection; + using Exiled.API.Features.Pickups; + using Exiled.API.Interfaces; using InventorySystem.Items.MicroHID; + using InventorySystem.Items.MicroHID.Modules; + + using Random = UnityEngine.Random; /// /// A wrapper class for . @@ -24,6 +30,11 @@ public MicroHid(MicroHIDItem itemBase) : base(itemBase) { Base = itemBase; + + EnergyManager = Base.EnergyManager; + BrokenModule = Base.BrokenSync; + InputModule = Base.InputSync; + CycleController = Base.CycleController; } /// @@ -34,13 +45,34 @@ internal MicroHid() { } + /// + /// Gets the of the MicroHID. + /// + public EnergyManagerModule EnergyManager { get; } + + /// + /// Gets the of the MicroHID. + /// + public BrokenSyncModule BrokenModule { get; } + + /// + /// Gets the of the MicroHID. + /// + public InputSyncModule InputModule { get; } + + /// + /// Gets the of the MicroHID. + /// + public CycleController CycleController { get; } + /// /// Gets or sets the remaining energy in the MicroHID. /// + /// Maximum energy is 1. Minimum energy is 0. public float Energy { - get => Base.RemainingEnergy; - set => Base.RemainingEnergy = value; + get => EnergyManager.Energy; + set => EnergyManager.ServerSetEnergy(Serial, value); } /// @@ -49,23 +81,128 @@ public float Energy public new MicroHIDItem Base { get; } /// - /// Gets or sets the . + /// Gets or sets a value indicating whether the MicroHID is broken. + /// + public bool IsBroken + { + get => BrokenModule.Broken; + set => BrokenModule.ServerSetBroken(Serial, value); + } + + /// + /// Gets a time when this was broken. + /// + /// A time when this was broken, or 0 if it is not broken. + public float BrokeTime => BrokenSyncModule.TryGetBrokenElapsed(Serial, out float time) ? time : 0; + + /// + /// Gets or sets the . + /// + public MicroHidPhase State + { + get => CycleController.Phase; + set => CycleController.Phase = value; + } + + /// + /// Gets or sets progress of winging up. + /// + /// A value between 0 and 1. + public float WindUpProgress + { + get => CycleController.ServerWindUpProgress; + set => CycleController.ServerWindUpProgress = value; + } + + /// + /// Gets or sets the last received . + /// + public MicroHidFiringMode LastFiringMode + { + get => CycleController.LastFiringMode; + set => CycleController.LastFiringMode = value; + } + + /// + /// Gets or sets the last received . /// - public HidState State + public InputSyncModule.SyncData LastReceived { - get => Base.State; - set => Base.State = value; + get => InputModule._lastReceived; + set => InputModule._lastReceived = value; } + /// + /// Gets a value indicating whether the is . + /// + public bool IsPrimary => InputModule.Primary; + /// /// Starts firing the MicroHID. /// - public void Fire() => Base.Fire(); + /// Fire mode. + public void Fire(MicroHidFiringMode firingMode = MicroHidFiringMode.PrimaryFire) + { + switch (firingMode) + { + case MicroHidFiringMode.PrimaryFire: + if (TryGetFireController(MicroHidFiringMode.PrimaryFire, out PrimaryFireModeModule primaryFireModeModule)) + primaryFireModeModule.ServerFire(); + break; + case MicroHidFiringMode.ChargeFire: + if (TryGetFireController(MicroHidFiringMode.ChargeFire, out ChargeFireModeModule chargeFireModeModule)) + chargeFireModeModule.ServerFire(); + break; + default: + if (TryGetFireController(MicroHidFiringMode.BrokenFire, out BrokenFireModeModule brokenFireModeModule)) + brokenFireModeModule.ServerFire(); + break; + } + } /// /// Recharges the MicroHID. /// - public void Recharge() => Base.Recharge(); + public void Recharge() + { + if (IsBroken) + Energy = Random.value; + else + Energy = 1; + } + + /// + /// Explodes the MicroHID. + /// + public void Explode() + { + if (TryGetFireController(MicroHidFiringMode.ChargeFire, out ChargeFireModeModule module)) + module.ServerExplode(); + } + + /// + /// Tries to get a assosiated with the specified . + /// + /// Target firing mode. + /// Found module or null. + /// Type of module. + /// true if module was found, false otherwise. + public bool TryGetFireController(MicroHidFiringMode firingMode, out T module) + where T : FiringModeControllerModule + { + if (CycleController._firingModeControllers.Count == 0) + CycleController.RecacheFiringModes(Base); + + module = (T)CycleController._firingModeControllers.Find(x => x.AssignedMode == firingMode); + return module != null; + } + + /// + /// Tries to get a assosiated with the last . + /// + /// Found module or null. + /// true if module was found, false otherwise. + public bool TryGetLastFireController(out FiringModeControllerModule module) => TryGetFireController(LastFiringMode, out module); /// /// Clones current object. @@ -82,5 +219,16 @@ public HidState State /// /// A string containing MicroHid-related data. public override string ToString() => $"{Type} ({Serial}) [{Weight}] *{Scale}* |{Energy}| -{State}-"; + + /// + internal override void ReadPickupInfo(Pickup pickup) + { + base.ReadPickupInfo(pickup); + + if (pickup.Is(out Pickups.MicroHIDPickup microHidPickup)) + { + Energy = microHidPickup.Energy; + } + } } } \ No newline at end of file diff --git a/EXILED/Exiled.API/Features/Pickups/MicroHIDPickup.cs b/EXILED/Exiled.API/Features/Pickups/MicroHIDPickup.cs index 4298fece54..87e21bf58d 100644 --- a/EXILED/Exiled.API/Features/Pickups/MicroHIDPickup.cs +++ b/EXILED/Exiled.API/Features/Pickups/MicroHIDPickup.cs @@ -7,7 +7,9 @@ namespace Exiled.API.Features.Pickups { + using Exiled.API.Features.Items; using Exiled.API.Interfaces; + using InventorySystem.Items.MicroHID.Modules; using BaseMicroHID = InventorySystem.Items.MicroHID.MicroHIDPickup; @@ -40,19 +42,118 @@ internal MicroHIDPickup() /// public new BaseMicroHID Base { get; } + /// + /// Gets the of this . + /// + public CycleController CycleController => Base._cycleController; + /// /// Gets or sets the MicroHID Energy Level. /// - public float Energy + public float Energy { get; set; } + + /// + /// Gets or sets the . + /// + public MicroHidPhase State + { + get => CycleController.Phase; + set => CycleController.Phase = value; + } + + /// + /// Gets or sets progress of winging up. + /// + /// A value between 0 and 1. + public float WindUpProgress + { + get => CycleController.ServerWindUpProgress; + set => CycleController.ServerWindUpProgress = value; + } + + /// + /// Gets or sets the last received . + /// + public MicroHidFiringMode LastFiringMode { - get => Base.NetworkEnergy; - set => Base.NetworkEnergy = value; + get => CycleController.LastFiringMode; + set => CycleController.LastFiringMode = value; } + /// + /// Starts firing the MicroHID. + /// + /// Fire mode. + public void Fire(MicroHidFiringMode firingMode = MicroHidFiringMode.PrimaryFire) + { + switch (firingMode) + { + case MicroHidFiringMode.PrimaryFire: + if (TryGetFireController(MicroHidFiringMode.PrimaryFire, out PrimaryFireModeModule primaryFireModeModule)) + primaryFireModeModule.ServerFire(); + break; + case MicroHidFiringMode.ChargeFire: + if (TryGetFireController(MicroHidFiringMode.ChargeFire, out ChargeFireModeModule chargeFireModeModule)) + chargeFireModeModule.ServerFire(); + break; + default: + if (TryGetFireController(MicroHidFiringMode.BrokenFire, out BrokenFireModeModule brokenFireModeModule)) + brokenFireModeModule.ServerFire(); + break; + } + } + + /// + /// Explodes the MicroHID. + /// + public void Explode() + { + if (TryGetFireController(MicroHidFiringMode.ChargeFire, out ChargeFireModeModule module)) + module.ServerExplode(); + } + + /// + /// Tries to get a assosiated with the specified . + /// + /// Target firing mode. + /// Found module or null. + /// Type of module. + /// true if module was found, false otherwise. + public bool TryGetFireController(MicroHidFiringMode firingMode, out T module) + where T : FiringModeControllerModule + { + if (CycleController._firingModeControllers.Count == 0) + { + module = null; + return false; + } + + module = (T)CycleController._firingModeControllers.Find(x => x.AssignedMode == firingMode); + return module != null; + } + + /// + /// Tries to get a assosiated with the last . + /// + /// Found module or null. + /// true if module was found, false otherwise. + public bool TryGetLastFireController(out FiringModeControllerModule module) => TryGetFireController(LastFiringMode, out module); + /// /// Returns the MicroHIDPickup in a human readable format. /// /// A string containing MicroHIDPickup related data. public override string ToString() => $"{Type} ({Serial}) [{Weight}] *{Scale}* |{Energy}|"; + + /// + internal override void ReadItemInfo(Item item) + { + base.ReadItemInfo(item); + + if (item.Is(out MicroHid microHid)) + { + Energy = microHid.Energy; + } + } } } diff --git a/EXILED/Exiled.CustomItems/API/Features/CustomGrenade.cs b/EXILED/Exiled.CustomItems/API/Features/CustomGrenade.cs index bf3c48d376..48d725ce7e 100644 --- a/EXILED/Exiled.CustomItems/API/Features/CustomGrenade.cs +++ b/EXILED/Exiled.CustomItems/API/Features/CustomGrenade.cs @@ -94,7 +94,7 @@ public virtual Pickup Throw(Vector3 position, float force, float weight, float f NetworkServer.Spawn(thrownProjectile.gameObject); thrownProjectile.InfoReceivedHook(default, newInfo); if (thrownProjectile.TryGetComponent(out Rigidbody component)) - throwable.Base.PropelBody(component, throwable.Base.FullThrowSettings.StartTorque, ThrowableNetworkHandler.GetLimitedVelocity(velocity), force, throwable.Base.FullThrowSettings.UpwardsFactor); + throwable.Base.PropelBody(component, throwable.Base.FullThrowSettings.StartTorque, ThrowableNetworkHandler.GetLimitedVelocity(velocity)); thrownProjectile.ServerActivate(); return Pickup.Get(thrownProjectile); diff --git a/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs index 0387241371..1274608ef3 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs @@ -7,42 +7,35 @@ namespace Exiled.Events.EventArgs.Player { + using System; + using API.Features; using API.Features.Items; - using Interfaces; - using InventorySystem.Items.MicroHID; + using InventorySystem.Items.MicroHID.Modules; /// /// Contains all information before MicroHID state is changed. /// - public class ChangingMicroHIDStateEventArgs : IPlayerEvent, IDeniableEvent + public class ChangingMicroHIDStateEventArgs : IDeniableEvent, IItemEvent { /// /// Initializes a new instance of the class. /// - /// - /// - /// /// /// /// - /// - /// - /// - /// - /// + /// + /// /// /// /// /// - public ChangingMicroHIDStateEventArgs(Player player, MicroHIDItem microHID, HidState oldState, HidState newState, bool isAllowed = true) + public ChangingMicroHIDStateEventArgs(Item microHID, MicroHidPhase newPhase, bool isAllowed = true) { - Player = player; - MicroHID = Item.Get(microHID); - OldState = oldState; - NewState = newState; + MicroHID = microHID.As(); + NewPhase = newPhase; IsAllowed = isAllowed; } @@ -51,24 +44,17 @@ public ChangingMicroHIDStateEventArgs(Player player, MicroHIDItem microHID, HidS /// public MicroHid MicroHID { get; } - /// - /// Gets the old MicroHID state. - /// - public HidState OldState { get; } - /// /// Gets or sets the new MicroHID state. /// - public HidState NewState { get; set; } + public MicroHidPhase NewPhase { get; set; } /// /// Gets or sets a value indicating whether the MicroHID state can be changed. /// public bool IsAllowed { get; set; } - /// - /// Gets the player who's using the MicroHID. - /// - public Player Player { get; } + /// + public Item Item => MicroHID; } } \ No newline at end of file diff --git a/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs index d4203283f7..1d207cd801 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs @@ -17,32 +17,24 @@ namespace Exiled.Events.EventArgs.Player /// /// Contains all information before MicroHID energy is changed. /// - public class UsingMicroHIDEnergyEventArgs : IPlayerEvent, IDeniableEvent, IItemEvent + public class UsingMicroHIDEnergyEventArgs : IDeniableEvent, IItemEvent { /// /// Initializes a new instance of the class. /// - /// - /// - /// /// /// /// - /// - /// - /// /// /// /// /// /// /// - public UsingMicroHIDEnergyEventArgs(Player player, MicroHIDItem microHIDitem, HidState currentState, float drain, bool isAllowed = true) + public UsingMicroHIDEnergyEventArgs(MicroHIDItem microHIDitem, float drain, bool isAllowed = true) { - Player = player; MicroHID = Item.Get(microHIDitem); - CurrentState = currentState; - Drain = drain; + Drain = MicroHID.Energy - drain; IsAllowed = isAllowed; } @@ -54,11 +46,6 @@ public UsingMicroHIDEnergyEventArgs(Player player, MicroHIDItem microHIDitem, Hi /// public Item Item => MicroHID; - /// - /// Gets the current state of the MicroHID. - /// - public HidState CurrentState { get; } - /// /// Gets or sets the MicroHID energy drain. /// @@ -68,10 +55,5 @@ public UsingMicroHIDEnergyEventArgs(Player player, MicroHIDItem microHIDitem, Hi /// Gets or sets a value indicating whether the MicroHID energy can be changed. /// public bool IsAllowed { get; set; } - - /// - /// Gets the player who's using the MicroHID. - /// - public Player Player { get; } } } \ No newline at end of file diff --git a/EXILED/Exiled.Events/Patches/Events/Map/FillingLocker.cs b/EXILED/Exiled.Events/Patches/Events/Map/FillingLocker.cs index a884b6c887..0816e0934e 100644 --- a/EXILED/Exiled.Events/Patches/Events/Map/FillingLocker.cs +++ b/EXILED/Exiled.Events/Patches/Events/Map/FillingLocker.cs @@ -37,7 +37,7 @@ private static IEnumerable Transpiler(IEnumerable instruction.Calls(Method(typeof(HashSet), nameof(HashSet.Add), new[] { typeof(ItemPickupBase) }))) + offset; + int index = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(List), nameof(List.Add), new[] { typeof(ItemPickupBase) }))) + offset; newInstructions.InsertRange( index, diff --git a/EXILED/Exiled.Events/Patches/Events/Player/ChangingMicroHIDState.cs b/EXILED/Exiled.Events/Patches/Events/Player/ChangingMicroHIDState.cs index f203bf3497..12f83e0c6c 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/ChangingMicroHIDState.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/ChangingMicroHIDState.cs @@ -8,89 +8,72 @@ namespace Exiled.Events.Patches.Events.Player { using System.Collections.Generic; + using System.Linq; using System.Reflection.Emit; - using API.Features; + using API.Features.Items; using API.Features.Pools; using Exiled.Events.Attributes; using Exiled.Events.EventArgs.Player; - using HarmonyLib; - using InventorySystem.Items.MicroHID; + using InventorySystem.Items.MicroHID.Modules; using static HarmonyLib.AccessTools; /// - /// Patches . + /// Patches . /// Adds the event. /// [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.ChangingMicroHIDState))] - [HarmonyPatch(typeof(MicroHIDItem), nameof(MicroHIDItem.ExecuteServerside))] + [HarmonyPatch(typeof(CycleController), nameof(CycleController.Phase), MethodType.Setter)] internal static class ChangingMicroHIDState { private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) { List newInstructions = ListPool.Pool.Get(instructions); - Label skipLabel = generator.DefineLabel(); - Label continueLabel = generator.DefineLabel(); + Label returnLabel = generator.DefineLabel(); LocalBuilder ev = generator.DeclareLocal(typeof(ChangingMicroHIDStateEventArgs)); - List instructionsToAdd = new() - { - // Player.Get(this.Owner); - new CodeInstruction(OpCodes.Ldarg_0), - new CodeInstruction(OpCodes.Callvirt, PropertyGetter(typeof(MicroHIDItem), nameof(MicroHIDItem.Owner))), - new CodeInstruction(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })), - - // this - new CodeInstruction(OpCodes.Ldarg_0), + int offset = 1; + int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Ret) + offset; - // state - new CodeInstruction(OpCodes.Ldloc_0), + newInstructions.InsertRange(index, new[] + { + // Item.Get(this.Serial); + new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]), + new(OpCodes.Ldfld, Field(typeof(CycleController), nameof(CycleController.Serial))), + new(OpCodes.Call, GetDeclaredMethods(typeof(Item)).Find(x => !x.IsGenericMethod && x.IsStatic && x.GetParameters().FirstOrDefault()?.ParameterType == typeof(ushort))), - // this.State - new CodeInstruction(OpCodes.Ldarg_0), - new CodeInstruction(OpCodes.Ldfld, Field(typeof(MicroHIDItem), nameof(MicroHIDItem.State))), + // value + new(OpCodes.Ldarg_1), // true - new CodeInstruction(OpCodes.Ldc_I4_1), + new(OpCodes.Ldc_I4_1), - // ChangingMicroHIDStateEventArgs ev = new(Player, MicroHIDItem, HidState, HidState, bool) - new CodeInstruction(OpCodes.Newobj, GetDeclaredConstructors(typeof(ChangingMicroHIDStateEventArgs))[0]), - new CodeInstruction(OpCodes.Dup), - new CodeInstruction(OpCodes.Dup), - new CodeInstruction(OpCodes.Stloc_S, ev.LocalIndex), + // ChangerMicroHIDStateEventArgs ev = new(Item.Get(this.Serial), value, true); + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ChangingMicroHIDStateEventArgs))[0]), + new(OpCodes.Dup), + new(OpCodes.Dup), + new(OpCodes.Stloc_S, ev.LocalIndex), // Handlers.Player.OnChangingMicroHIDState(ev); - new CodeInstruction(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnChangingMicroHIDState))), + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnChangingMicroHIDState))), // if (!ev.IsAllowed) - // goto skipLabel; - new CodeInstruction(OpCodes.Callvirt, PropertyGetter(typeof(ChangingMicroHIDStateEventArgs), nameof(ChangingMicroHIDStateEventArgs.IsAllowed))), - new CodeInstruction(OpCodes.Brfalse_S, skipLabel), - - // this.State = ev.NewState - new CodeInstruction(OpCodes.Ldarg_0), - new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex), - new CodeInstruction(OpCodes.Callvirt, PropertyGetter(typeof(ChangingMicroHIDStateEventArgs), nameof(ChangingMicroHIDStateEventArgs.NewState))), - new CodeInstruction(OpCodes.Stfld, Field(typeof(MicroHIDItem), nameof(MicroHIDItem.State))), - new CodeInstruction(OpCodes.Br, continueLabel), - - // this.State = state; - new CodeInstruction(OpCodes.Ldarg_0).WithLabels(skipLabel), - new CodeInstruction(OpCodes.Ldloc_0), - new CodeInstruction(OpCodes.Stfld, Field(typeof(MicroHIDItem), nameof(MicroHIDItem.State))), + // return; + new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingMicroHIDStateEventArgs), nameof(ChangingMicroHIDStateEventArgs.IsAllowed))), + new(OpCodes.Brfalse_S, returnLabel), - new CodeInstruction(OpCodes.Nop).WithLabels(continueLabel), - }; - - int offset = 1; + // value = ev.NewPhase; + new(OpCodes.Ldarg_S, ev.LocalIndex), + new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingMicroHIDStateEventArgs), nameof(ChangingMicroHIDStateEventArgs.NewPhase))), + new(OpCodes.Starg_S, 1), + }); - foreach (CodeInstruction instruction in newInstructions.FindAll(i => i.StoresField(Field(typeof(MicroHIDItem), nameof(MicroHIDItem.State))))) - newInstructions.InsertRange(newInstructions.IndexOf(instruction) + offset, instructionsToAdd); + newInstructions[newInstructions.Count - 1].labels.Add(returnLabel); for (int z = 0; z < newInstructions.Count; z++) yield return newInstructions[z]; diff --git a/EXILED/Exiled.Events/Patches/Events/Player/InteractingLocker.cs b/EXILED/Exiled.Events/Patches/Events/Player/InteractingLocker.cs index f64e2ed46a..9024c74122 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/InteractingLocker.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/InteractingLocker.cs @@ -49,7 +49,7 @@ private static IEnumerable Transpiler(IEnumerable - /// Patches . + /// Patches . /// Adds the event. /// [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.UsingMicroHIDEnergy))] - [HarmonyPatch(typeof(MicroHIDItem), nameof(MicroHIDItem.ExecuteServerside))] + [HarmonyPatch(typeof(EnergyManagerModule), nameof(EnergyManagerModule.EquipUpdate))] internal static class UsingMicroHIDEnergy { private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) @@ -39,50 +37,43 @@ private static IEnumerable Transpiler(IEnumerable instruction.Calls(Method(typeof(Mathf), nameof(Mathf.Clamp01)))) + offset; - - newInstructions.InsertRange( - index, - new CodeInstruction[] - { - // Player.Get(base.Owner) - new(OpCodes.Ldarg_0), - new(OpCodes.Callvirt, PropertyGetter(typeof(MicroHIDItem), nameof(MicroHIDItem.Owner))), - new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })), - - // this - new(OpCodes.Ldarg_0), - - // currentState - new(OpCodes.Ldarg_0), - new(OpCodes.Ldfld, Field(typeof(MicroHIDItem), nameof(MicroHIDItem.State))), - - // num - new(OpCodes.Ldloc_2), - - // true - new(OpCodes.Ldc_I4_1), - - // UsingMicroHIDEnergyEventArgs ev = new(Player, MicroHIDItem, HidState, float, bool) - new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UsingMicroHIDEnergyEventArgs))[0]), - new(OpCodes.Dup), - new(OpCodes.Dup), - new(OpCodes.Stloc_S, ev.LocalIndex), - - // Handlers.Player.UsingMicroHIDEnergy(ev) - new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnUsingMicroHIDEnergy))), - - // if (!ev.IsAllowed) - // return; - new(OpCodes.Callvirt, PropertyGetter(typeof(UsingMicroHIDEnergyEventArgs), nameof(UsingMicroHIDEnergyEventArgs.IsAllowed))), - new(OpCodes.Brfalse_S, returnLabel), - - // num = ev.Drain - new(OpCodes.Ldloc_S, ev.LocalIndex), - new(OpCodes.Call, PropertyGetter(typeof(UsingMicroHIDEnergyEventArgs), nameof(UsingMicroHIDEnergyEventArgs.Drain))), - new(OpCodes.Stloc_2), - }); + int offset = -1; + int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldarg_0) + offset; + + newInstructions.InsertRange(index, new CodeInstruction[] + { + // this.MicroHID + new(OpCodes.Ldarg_0), + new(OpCodes.Callvirt, PropertyGetter(typeof(EnergyManagerModule), nameof(EnergyManagerModule.MicroHid))), + + // energy + new(OpCodes.Ldloc_0), + + // true + new(OpCodes.Ldc_I4_1), + + // UsingMicroHIDEnergyEventArgs ev = new(this.MicroHID, energy, true); + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UsingMicroHIDEnergyEventArgs))[0]), + new(OpCodes.Dup), + new(OpCodes.Dup), + new(OpCodes.Stloc_S, ev.LocalIndex), + + // Handlers.Player.OnUsingMicroHIDEnergy(ev); + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnUsingMicroHIDEnergy))), + + // if (!ev.IsAllowed) + // return; + new(OpCodes.Callvirt, PropertyGetter(typeof(UsingMicroHIDEnergyEventArgs), nameof(UsingMicroHIDEnergyEventArgs.IsAllowed))), + new(OpCodes.Brfalse_S, returnLabel), + + // energy = this.Energy - ev.Drain; + new(OpCodes.Ldloc_S, ev.LocalIndex), + new(OpCodes.Ldarg_0), + new(OpCodes.Callvirt, PropertyGetter(typeof(EnergyManagerModule), nameof(EnergyManagerModule.Energy))), + new(OpCodes.Callvirt, PropertyGetter(typeof(UsingMicroHIDEnergyEventArgs), nameof(UsingMicroHIDEnergyEventArgs.Drain))), + new(OpCodes.Sub), + new(OpCodes.Stloc_0), + }); newInstructions[newInstructions.Count - 1].WithLabels(returnLabel); diff --git a/EXILED/Exiled.Events/Patches/Events/Scp3114/Slapped.cs b/EXILED/Exiled.Events/Patches/Events/Scp3114/Slapped.cs index c1f9ce53fd..b49b32622b 100644 --- a/EXILED/Exiled.Events/Patches/Events/Scp3114/Slapped.cs +++ b/EXILED/Exiled.Events/Patches/Events/Scp3114/Slapped.cs @@ -48,9 +48,9 @@ private static IEnumerable Transpiler(IEnumerable), nameof(ScpAttackAbilityBase._syncAttack))), + new(OpCodes.Callvirt, PropertyGetter(typeof(ScpAttackAbilityBase), nameof(ScpAttackAbilityBase.LastAttackResult))), // Player::Get(primaryTarget) new(OpCodes.Ldloc_1), diff --git a/EXILED/Exiled.Events/Patches/Events/Scp939/PlayingFootstep.cs b/EXILED/Exiled.Events/Patches/Events/Scp939/PlayingFootstep.cs index 37184ee4fc..94d40a79fc 100644 --- a/EXILED/Exiled.Events/Patches/Events/Scp939/PlayingFootstep.cs +++ b/EXILED/Exiled.Events/Patches/Events/Scp939/PlayingFootstep.cs @@ -24,7 +24,7 @@ namespace Exiled.Events.Patches.Events.Scp939 using static HarmonyLib.AccessTools; /// - /// Patches + /// Patches /// to add the event. /// [EventPatch(typeof(Scp939), nameof(Scp939.PlayingFootstep))] diff --git a/EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs b/EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs index e890ef80df..bd8a33d545 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs @@ -66,7 +66,7 @@ private static IEnumerable Transpiler(IEnumerable), nameof(HashSet.Contains))), new(OpCodes.Brtrue_S, jmp), }); diff --git a/EXILED/Exiled.Events/Patches/Fixes/LockerFixes.cs b/EXILED/Exiled.Events/Patches/Fixes/LockerFixes.cs index f4c195bcb8..47cc0809bb 100644 --- a/EXILED/Exiled.Events/Patches/Fixes/LockerFixes.cs +++ b/EXILED/Exiled.Events/Patches/Fixes/LockerFixes.cs @@ -48,7 +48,7 @@ private static IEnumerable Transpiler(IEnumerable