From d513ff1e8b2e4005ff588abdc947742fb2e2bc1f Mon Sep 17 00:00:00 2001 From: Yamato Date: Thu, 26 Dec 2024 17:32:25 +0100 Subject: [PATCH 1/6] ItemAddReason.AdminCommand has been choose --- EXILED/Exiled.API/Features/Player.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EXILED/Exiled.API/Features/Player.cs b/EXILED/Exiled.API/Features/Player.cs index 778f94133..8bf776590 100644 --- a/EXILED/Exiled.API/Features/Player.cs +++ b/EXILED/Exiled.API/Features/Player.cs @@ -2781,7 +2781,7 @@ public void AddItem(Firearm item, IEnumerable identifiers) /// The of the item to be added. /// The reason the item was added. /// The that was added. - public Item AddItem(Pickup pickup, ItemAddReason addReason = ItemAddReason.Undefined) => Item.Get(Inventory.ServerAddItem(pickup.Type, addReason, pickup.Serial, pickup.Base)); + public Item AddItem(Pickup pickup, ItemAddReason addReason = ItemAddReason.AdminCommand) => Item.Get(Inventory.ServerAddItem(pickup.Type, addReason, pickup.Serial, pickup.Base)); /// /// Adds an item to the player's inventory. @@ -2791,7 +2791,7 @@ public void AddItem(Firearm item, IEnumerable identifiers) /// The that was added. public Item AddItem(FirearmPickup pickup, IEnumerable identifiers) { - Firearm firearm = Item.Get(Inventory.ServerAddItem(pickup.Type, ItemAddReason.Undefined, pickup.Serial, pickup.Base)); + Firearm firearm = Item.Get(Inventory.ServerAddItem(pickup.Type, ItemAddReason.AdminCommand, pickup.Serial, pickup.Base)); if (identifiers is not null) firearm.AddAttachment(identifiers); From b1920da33203d5b49f005a3d7909ab42b5fd1932 Mon Sep 17 00:00:00 2001 From: Yamato <66829532+louis1706@users.noreply.github.com> Date: Thu, 26 Dec 2024 19:24:20 +0100 Subject: [PATCH 2/6] fix: RoomLightControllersValue Fix (#346) * RoomLightControllersValue Fix * remove non used using --- EXILED/Exiled.API/Features/Room.cs | 2 - .../Generic/RoomLightControllersList.cs | 39 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs diff --git a/EXILED/Exiled.API/Features/Room.cs b/EXILED/Exiled.API/Features/Room.cs index f0440ad64..4efae3acd 100644 --- a/EXILED/Exiled.API/Features/Room.cs +++ b/EXILED/Exiled.API/Features/Room.cs @@ -517,8 +517,6 @@ private void InternalCreate() Log.Error($"[ROOMTYPE UNKNOWN] {this} Name : {gameObject?.name} Shape : {Identifier?.Shape}"); #endif - RoomLightControllersValue.AddRange(gameObject.GetComponentsInChildren()); - RoomLightControllers = RoomLightControllersValue.AsReadOnly(); GetComponentsInChildren().ForEach(component => diff --git a/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs b/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs new file mode 100644 index 000000000..129585e26 --- /dev/null +++ b/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs @@ -0,0 +1,39 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.Patches.Generic +{ + using Exiled.API.Features; +#pragma warning disable SA1313 +#pragma warning disable SA1402 + + using HarmonyLib; + + /// + /// Patch for adding to list. + /// + [HarmonyPatch(typeof(RoomLightController), nameof(RoomLightController.Start))] + internal class RoomLightControllersList + { + private static void Postfix(RoomLightController __instance) + { + Room.Get(__instance.Room).RoomLightControllersValue.Add(__instance); + } + } + + /// + /// Patch for removing to list. + /// + [HarmonyPatch(typeof(RoomLightController), nameof(RoomLightController.OnDestroy))] + internal class RoomLightControllersList2 + { + private static void Postfix(RoomLightController __instance) + { + Room.Get(__instance.Room).RoomLightControllersValue.Remove(__instance); + } + } +} \ No newline at end of file From a963c878131eeec176d686389927b8bad92694db Mon Sep 17 00:00:00 2001 From: IRacle <79921583+IRacle1@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:25:57 +0300 Subject: [PATCH 3/6] fix(feat theoretically): Many bugfixes (#338) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: item related stuff * ah * test * fix: roundend IsAllowed * Фикс DryFire ивента * revert * little fix --- EXILED/Exiled.API/Features/Items/Armor.cs | 6 +- .../Features/Items/ExplosiveGrenade.cs | 4 +- EXILED/Exiled.API/Features/Items/Firearm.cs | 4 +- .../Exiled.API/Features/Items/FlashGrenade.cs | 4 +- EXILED/Exiled.API/Features/Items/Item.cs | 20 ++++- EXILED/Exiled.API/Features/Items/Jailbird.cs | 4 +- EXILED/Exiled.API/Features/Items/Keycard.cs | 4 +- EXILED/Exiled.API/Features/Items/Scp244.cs | 4 +- EXILED/Exiled.API/Features/Items/Usable.cs | 4 +- .../Features/Pickups/BodyArmorPickup.cs | 3 +- .../Features/Pickups/FirearmPickup.cs | 9 +- EXILED/Exiled.Events/Handlers/Player.cs | 2 +- .../Events/Player/ChangingRoleAndSpawned.cs | 13 ++- .../Patches/Events/Player/DryFire.cs | 2 +- .../Patches/Events/Server/RoundEnd.cs | 4 +- .../FixOnAddedBeingCallAfterOnRemoved.cs | 82 +++++++++++++------ 16 files changed, 109 insertions(+), 60 deletions(-) diff --git a/EXILED/Exiled.API/Features/Items/Armor.cs b/EXILED/Exiled.API/Features/Items/Armor.cs index 6fe2bc097..238de555e 100644 --- a/EXILED/Exiled.API/Features/Items/Armor.cs +++ b/EXILED/Exiled.API/Features/Items/Armor.cs @@ -150,7 +150,6 @@ public IEnumerable CategoryLimits { Weight = Weight, StaminaUseMultiplier = StaminaUseMultiplier, - RemoveExcessOnDrop = RemoveExcessOnDrop, CategoryLimits = CategoryLimits, StaminaRegenMultiplier = StaminaRegenMultiplier, AmmoLimits = AmmoLimits, @@ -159,14 +158,13 @@ public IEnumerable CategoryLimits }; /// - internal override void ReadPickupInfo(Pickup pickup) + internal override void ReadPickupInfoBefore(Pickup pickup) { - base.ReadPickupInfo(pickup); + base.ReadPickupInfoBefore(pickup); if (pickup is Pickups.BodyArmorPickup armorPickup) { HelmetEfficacy = armorPickup.HelmetEfficacy; VestEfficacy = armorPickup.VestEfficacy; - RemoveExcessOnDrop = armorPickup.RemoveExcessOnDrop; StaminaUseMultiplier = armorPickup.StaminaUseMultiplier; StaminaRegenMultiplier = armorPickup.StaminaRegenMultiplier; AmmoLimits = armorPickup.AmmoLimits; diff --git a/EXILED/Exiled.API/Features/Items/ExplosiveGrenade.cs b/EXILED/Exiled.API/Features/Items/ExplosiveGrenade.cs index 1ee536c43..b550c1345 100644 --- a/EXILED/Exiled.API/Features/Items/ExplosiveGrenade.cs +++ b/EXILED/Exiled.API/Features/Items/ExplosiveGrenade.cs @@ -162,9 +162,9 @@ public ExplosionGrenadeProjectile SpawnActive(Vector3 position, Player owner = n }; /// - internal override void ReadPickupInfo(Pickup pickup) + internal override void ReadPickupInfoBefore(Pickup pickup) { - base.ReadPickupInfo(pickup); + base.ReadPickupInfoBefore(pickup); if (pickup is ExplosiveGrenadePickup explosiveGrenadePickup) { MaxRadius = explosiveGrenadePickup.MaxRadius; diff --git a/EXILED/Exiled.API/Features/Items/Firearm.cs b/EXILED/Exiled.API/Features/Items/Firearm.cs index 98cd780c2..e77622d67 100644 --- a/EXILED/Exiled.API/Features/Items/Firearm.cs +++ b/EXILED/Exiled.API/Features/Items/Firearm.cs @@ -699,9 +699,9 @@ internal override void ChangeOwner(Player oldOwner, Player newOwner) } /// - internal override void ReadPickupInfo(Pickup pickup) + internal override void ReadPickupInfoBefore(Pickup pickup) { - base.ReadPickupInfo(pickup); + base.ReadPickupInfoBefore(pickup); if (pickup is FirearmPickup firearmPickup) { diff --git a/EXILED/Exiled.API/Features/Items/FlashGrenade.cs b/EXILED/Exiled.API/Features/Items/FlashGrenade.cs index ccebfa394..867d11b3a 100644 --- a/EXILED/Exiled.API/Features/Items/FlashGrenade.cs +++ b/EXILED/Exiled.API/Features/Items/FlashGrenade.cs @@ -139,9 +139,9 @@ public FlashbangProjectile SpawnActive(Vector3 position, Player owner = null) public override string ToString() => $"{Type} ({Serial}) [{Weight}] *{Scale}* |{FuseTime}|"; /// - internal override void ReadPickupInfo(Pickup pickup) + internal override void ReadPickupInfoBefore(Pickup pickup) { - base.ReadPickupInfo(pickup); + base.ReadPickupInfoBefore(pickup); if (pickup is FlashGrenadePickup flashGrenadePickup) { MinimalDurationEffect = flashGrenadePickup.MinimalDurationEffect; diff --git a/EXILED/Exiled.API/Features/Items/Item.cs b/EXILED/Exiled.API/Features/Items/Item.cs index e5207c99f..c55a6e75b 100644 --- a/EXILED/Exiled.API/Features/Items/Item.cs +++ b/EXILED/Exiled.API/Features/Items/Item.cs @@ -429,12 +429,30 @@ internal virtual void ChangeOwner(Player oldOwner, Player newOwner) /// Helper method for saving data between items and pickups. /// /// -related data to give to the . - internal virtual void ReadPickupInfo(Pickup pickup) + /// + /// Analog to , but it is called before item initialization. + /// . + /// + /// + internal virtual void ReadPickupInfoBefore(Pickup pickup) { if (pickup is not null) { Scale = pickup.Scale; } } + + /// + /// Helper method for saving data between items and pickups. + /// + /// -related data to give to the . + /// + /// Analog to , but it is called after item initialization. + /// . + /// + /// + internal virtual void ReadPickupInfoAfter(Pickup pickup) + { + } } } diff --git a/EXILED/Exiled.API/Features/Items/Jailbird.cs b/EXILED/Exiled.API/Features/Items/Jailbird.cs index b7a48eae6..6d7ac7d32 100644 --- a/EXILED/Exiled.API/Features/Items/Jailbird.cs +++ b/EXILED/Exiled.API/Features/Items/Jailbird.cs @@ -185,9 +185,9 @@ public void Break() public override string ToString() => $"{Type} ({Serial}) [{Weight}] *{Scale}*"; /// - internal override void ReadPickupInfo(Pickup pickup) + internal override void ReadPickupInfoBefore(Pickup pickup) { - base.ReadPickupInfo(pickup); + base.ReadPickupInfoBefore(pickup); if (pickup is JailbirdPickup jailbirdPickup) { MeleeDamage = jailbirdPickup.MeleeDamage; diff --git a/EXILED/Exiled.API/Features/Items/Keycard.cs b/EXILED/Exiled.API/Features/Items/Keycard.cs index 6bd7ca93a..0bd390018 100644 --- a/EXILED/Exiled.API/Features/Items/Keycard.cs +++ b/EXILED/Exiled.API/Features/Items/Keycard.cs @@ -69,9 +69,9 @@ public KeycardPermissions Permissions public override string ToString() => $"{Type} ({Serial}) [{Weight}] *{Scale}* |{Permissions}|"; /// - internal override void ReadPickupInfo(Pickup pickup) + internal override void ReadPickupInfoBefore(Pickup pickup) { - base.ReadPickupInfo(pickup); + base.ReadPickupInfoBefore(pickup); if (pickup is KeycardPickup keycardPickup) { Permissions = keycardPickup.Permissions; diff --git a/EXILED/Exiled.API/Features/Items/Scp244.cs b/EXILED/Exiled.API/Features/Items/Scp244.cs index 0cfea6c3a..9f9c73530 100644 --- a/EXILED/Exiled.API/Features/Items/Scp244.cs +++ b/EXILED/Exiled.API/Features/Items/Scp244.cs @@ -119,9 +119,9 @@ public override Pickup CreatePickup(Vector3 position, Quaternion? rotation = nul public override string ToString() => $"{Type} ({Serial}) [{Weight}] *{Scale}* -{Primed}-"; /// - internal override void ReadPickupInfo(Pickup pickup) + internal override void ReadPickupInfoBefore(Pickup pickup) { - base.ReadPickupInfo(pickup); + base.ReadPickupInfoBefore(pickup); if (pickup is Scp244Pickup scp244) { Health = scp244.Health; diff --git a/EXILED/Exiled.API/Features/Items/Usable.cs b/EXILED/Exiled.API/Features/Items/Usable.cs index 6ba68e2cb..65c858b74 100644 --- a/EXILED/Exiled.API/Features/Items/Usable.cs +++ b/EXILED/Exiled.API/Features/Items/Usable.cs @@ -136,9 +136,9 @@ public virtual void Use() } /// - internal override void ReadPickupInfo(Pickup pickup) + internal override void ReadPickupInfoBefore(Pickup pickup) { - base.ReadPickupInfo(pickup); + base.ReadPickupInfoBefore(pickup); if (pickup is UsablePickup usablePickup) { UseTime = usablePickup.UseTime; diff --git a/EXILED/Exiled.API/Features/Pickups/BodyArmorPickup.cs b/EXILED/Exiled.API/Features/Pickups/BodyArmorPickup.cs index 47cfb108c..1b6376353 100644 --- a/EXILED/Exiled.API/Features/Pickups/BodyArmorPickup.cs +++ b/EXILED/Exiled.API/Features/Pickups/BodyArmorPickup.cs @@ -73,6 +73,7 @@ internal BodyArmorPickup(ItemType type) /// /// Gets or sets a value indicating whether excess ammo should be removed when the armor is dropped. /// + [Obsolete("Propetry is internal, and controls armor remove logic for clearing inventory")] public bool RemoveExcessOnDrop { get; set; } /// @@ -132,7 +133,6 @@ internal override void ReadItemInfo(Item item) { helmetEfficacy = armoritem.HelmetEfficacy; vestEfficacy = armoritem.VestEfficacy; - RemoveExcessOnDrop = armoritem.RemoveExcessOnDrop; StaminaUseMultiplier = armoritem.StaminaUseMultiplier; StaminaRegenMultiplier = armoritem.StaminaRegenMultiplier; AmmoLimits = armoritem.AmmoLimits; @@ -148,7 +148,6 @@ protected override void InitializeProperties(ItemBase itemBase) { helmetEfficacy = armoritem.HelmetEfficacy; vestEfficacy = armoritem.VestEfficacy; - RemoveExcessOnDrop = !armoritem.DontRemoveExcessOnDrop; StaminaUseMultiplier = armoritem._staminaUseMultiplier; StaminaRegenMultiplier = armoritem.StaminaRegenMultiplier; AmmoLimits = armoritem.AmmoLimits.Select(limit => (ArmorAmmoLimit)limit); diff --git a/EXILED/Exiled.API/Features/Pickups/FirearmPickup.cs b/EXILED/Exiled.API/Features/Pickups/FirearmPickup.cs index 3db97dac8..feb8d4c57 100644 --- a/EXILED/Exiled.API/Features/Pickups/FirearmPickup.cs +++ b/EXILED/Exiled.API/Features/Pickups/FirearmPickup.cs @@ -138,9 +138,12 @@ public override void Spawn() /// internal override void ReadItemInfo(Items.Item item) { - Items.Firearm firearm = (Items.Firearm)item; - MaxAmmo = firearm.PrimaryMagazine.ConstantMaxAmmo; - AmmoDrain = firearm.AmmoDrain; + if (item is Items.Firearm firearm) + { + MaxAmmo = firearm.PrimaryMagazine.ConstantMaxAmmo; + AmmoDrain = firearm.AmmoDrain; + } + base.ReadItemInfo(item); } diff --git a/EXILED/Exiled.Events/Handlers/Player.cs b/EXILED/Exiled.Events/Handlers/Player.cs index d87675450..59519b21e 100644 --- a/EXILED/Exiled.Events/Handlers/Player.cs +++ b/EXILED/Exiled.Events/Handlers/Player.cs @@ -1024,7 +1024,7 @@ public static void OnItemAdded(ReferenceHub referenceHub, InventorySystem.Items. { ItemAddedEventArgs ev = new(referenceHub, itemBase, pickupBase); - ev.Item.ReadPickupInfo(ev.Pickup); + ev.Item.ReadPickupInfoAfter(ev.Pickup); ev.Player.ItemsValue.Add(ev.Item); diff --git a/EXILED/Exiled.Events/Patches/Events/Player/ChangingRoleAndSpawned.cs b/EXILED/Exiled.Events/Patches/Events/Player/ChangingRoleAndSpawned.cs index 47f1ede44..d31306a20 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/ChangingRoleAndSpawned.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/ChangingRoleAndSpawned.cs @@ -220,16 +220,13 @@ private static void ChangeInventory(ChangingRoleEventArgs ev) inventory.SendAmmoNextFrame = true; } - if (!StartingInventories.DefinedInventories.TryGetValue(ev.NewRole, out InventoryRoleInfo value)) - return; - - foreach (KeyValuePair item in value.Ammo) - inventory.ServerAddAmmo(item.Key, item.Value); + foreach (KeyValuePair ammo in ev.Ammo) + inventory.ServerAddAmmo(ammo.Key, ammo.Value); - for (int i = 0; i < value.Items.Length; i++) + foreach (ItemType item in ev.Items) { - ItemBase arg = inventory.ServerAddItem(value.Items[i], ItemAddReason.StartingItem, 0); - InventoryItemProvider.OnItemProvided?.Invoke(ev.Player.ReferenceHub, arg); + ItemBase itemBase = inventory.ServerAddItem(item, ItemAddReason.StartingItem); + InventoryItemProvider.OnItemProvided?.Invoke(ev.Player.ReferenceHub, itemBase); } InventoryItemProvider.InventoriesToReplenish.Enqueue(ev.Player.ReferenceHub); diff --git a/EXILED/Exiled.Events/Patches/Events/Player/DryFire.cs b/EXILED/Exiled.Events/Patches/Events/Player/DryFire.cs index 0c58fe435..59b209e10 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/DryFire.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/DryFire.cs @@ -67,7 +67,7 @@ private static IEnumerable Transpiler(IEnumerable x.Calls(PropertyGetter(typeof(AutomaticActionModule), nameof(AutomaticActionModule.Cocked)))) + offset; + int index = newInstructions.FindLastIndex(x => x.Calls(PropertyGetter(typeof(AutomaticActionModule), nameof(AutomaticActionModule.Cocked)))) + offset; newInstructions.InsertRange(index, GetInstructions(newInstructions[index], ret)); diff --git a/EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs b/EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs index 41f13b628..afe065a74 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs @@ -112,7 +112,7 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable @@ -30,36 +36,64 @@ internal class FixOnAddedBeingCallAfterOnRemoved private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) { List newInstructions = ListPool.Pool.Get(instructions); - /* - // Modify this - itemBase2.OnAdded(pickup); - Action onItemAdded = InventoryExtensions.OnItemAdded; - if (onItemAdded != null) - { - onItemAdded(inv._hub, itemBase2, pickup); - } - // To this - Action onItemAdded = InventoryExtensions.OnItemAdded; - if (onItemAdded != null) + + Label continueLabel = generator.DefineLabel(); + + int offset = -1; + int index = newInstructions.FindLastIndex(instruction => instruction.Calls(PropertyGetter(typeof(NetworkBehaviour), nameof(NetworkBehaviour.isLocalPlayer)))) + offset; + + // set label for code right after OnAdded/OnItemAdded, to skip that part for ammo + Label afterAmmoLabel = newInstructions[index].labels[0]; + + offset = -2; + index = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(ItemBase), nameof(ItemBase.OnAdded)))) + offset; + + newInstructions.InsertRange( + index, + new[] { - onItemAdded(inv._hub, itemBase2, pickup); - } - itemBase2.OnAdded(pickup); - */ - int opCodesToMove = 3; - int offset = -2; - int indexOnAdded = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(ItemBase), nameof(ItemBase.OnAdded)))) + offset; + // CallBefore(itemBase, pickup) + new CodeInstruction(OpCodes.Ldloc_1), + new(OpCodes.Ldarg_S, 4), + new(OpCodes.Call, Method(typeof(FixOnAddedBeingCallAfterOnRemoved), nameof(FixOnAddedBeingCallAfterOnRemoved.CallBefore))), - offset = 1; - int indexInvoke = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(Action), nameof(Action.Invoke)))) + offset; + // if (itemBase is not AmmoItem) + // skip; + new CodeInstruction(OpCodes.Ldloc_1), + new(OpCodes.Isinst, typeof(AmmoItem)), + new(OpCodes.Brfalse_S, continueLabel), - newInstructions.InsertRange(indexInvoke, newInstructions.GetRange(indexOnAdded, opCodesToMove)); - newInstructions[indexInvoke].MoveLabelsFrom(newInstructions[indexInvoke + opCodesToMove]); - newInstructions.RemoveRange(indexOnAdded, opCodesToMove); + // call help method for inverse call + // InverseCall(itemBase, inv._hub, pickup) + new(OpCodes.Ldloc_1), + new(OpCodes.Ldarg_0), + new(OpCodes.Ldfld, Field(typeof(Inventory), nameof(Inventory._hub))), + new(OpCodes.Ldarg_S, 4), + new(OpCodes.Call, Method(typeof(FixOnAddedBeingCallAfterOnRemoved), nameof(FixOnAddedBeingCallAfterOnRemoved.InverseCall))), + + // move after basegame OnAdded/OnItemAdded + new(OpCodes.Br_S, afterAmmoLabel), + + new CodeInstruction(OpCodes.Nop).WithLabels(continueLabel), + }); for (int z = 0; z < newInstructions.Count; z++) yield return newInstructions[z]; + ListPool.Pool.Return(newInstructions); } + + private static void InverseCall(ItemBase item, ReferenceHub referenceHub, ItemPickupBase pickup) + { + Exiled.API.Extensions.ReflectionExtensions.InvokeStaticEvent(typeof(InventoryExtensions), nameof(InventoryExtensions.OnItemAdded), new object[] { referenceHub, item, pickup }); + item.OnAdded(pickup); + } + + private static void CallBefore(ItemBase itemBase, ItemPickupBase pickupBase) + { + Item item = Item.Get(itemBase); + Pickup pickup = Pickup.Get(pickupBase); + item.ReadPickupInfoBefore(pickup); + } } } From b212f0c5c9863690bdfbaec72a1fafbf788fd61a Mon Sep 17 00:00:00 2001 From: Yamato <66829532+louis1706@users.noreply.github.com> Date: Thu, 26 Dec 2024 19:26:11 +0100 Subject: [PATCH 4/6] fix: Reloading/Unloading weapon events (#343) * ReloadingWeapon * unloading + refactor * sigma moment --------- Co-authored-by: IRacle --- .../Player/ReloadingWeaponEventArgs.cs | 15 ++--- .../Player/UnloadingWeaponEventArgs.cs | 13 +--- .../Patches/Events/Player/ReloadingWeapon.cs | 66 +++++++++++++++++++ .../Patches/Events/Player/UnloadingWeapon.cs | 66 +++++++++++++++++++ EXILED/Exiled.Example/Events/PlayerHandler.cs | 3 +- 5 files changed, 140 insertions(+), 23 deletions(-) create mode 100644 EXILED/Exiled.Events/Patches/Events/Player/ReloadingWeapon.cs create mode 100644 EXILED/Exiled.Events/Patches/Events/Player/UnloadingWeapon.cs diff --git a/EXILED/Exiled.Events/EventArgs/Player/ReloadingWeaponEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ReloadingWeaponEventArgs.cs index 0dde302a9..e35fc9f41 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/ReloadingWeaponEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/ReloadingWeaponEventArgs.cs @@ -20,26 +20,19 @@ public class ReloadingWeaponEventArgs : IPlayerEvent, IFirearmEvent, IDeniableEv /// /// Initializes a new instance of the class. /// - /// - /// - /// /// /// /// - /// - /// - /// - public ReloadingWeaponEventArgs(Player player, Firearm firearm, bool isAllowed = true) + public ReloadingWeaponEventArgs(InventorySystem.Items.Firearms.Firearm firearm) { - Firearm = firearm; - Player = player; - IsAllowed = isAllowed; + Firearm = Item.Get(firearm); + Player = Firearm.Owner; } /// /// Gets or sets a value indicating whether the weapon can be reloaded. /// - public bool IsAllowed { get; set; } + public bool IsAllowed { get; set; } = true; /// /// Gets the being reloaded. diff --git a/EXILED/Exiled.Events/EventArgs/Player/UnloadingWeaponEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/UnloadingWeaponEventArgs.cs index a5c8df5f0..a7f8b6326 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/UnloadingWeaponEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/UnloadingWeaponEventArgs.cs @@ -20,26 +20,19 @@ public class UnloadingWeaponEventArgs : IPlayerEvent, IFirearmEvent, IDeniableEv /// /// Initializes a new instance of the class. /// - /// - /// - /// /// /// /// - /// - /// - /// - public UnloadingWeaponEventArgs(Player player, Firearm firearm, bool isAllowed = true) + public UnloadingWeaponEventArgs(Firearm firearm) { Firearm = firearm; - Player = player; - IsAllowed = isAllowed; + Player = firearm.Owner; } /// /// Gets or sets a value indicating whether the weapon can be unloaded. /// - public bool IsAllowed { get; set; } + public bool IsAllowed { get; set; } = true; /// /// Gets the being unloaded. diff --git a/EXILED/Exiled.Events/Patches/Events/Player/ReloadingWeapon.cs b/EXILED/Exiled.Events/Patches/Events/Player/ReloadingWeapon.cs new file mode 100644 index 000000000..fea606842 --- /dev/null +++ b/EXILED/Exiled.Events/Patches/Events/Player/ReloadingWeapon.cs @@ -0,0 +1,66 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.Patches.Events.Player +{ + using System.Collections.Generic; + using System.Reflection.Emit; + + using API.Features.Pools; + + using Exiled.API.Extensions; + using Exiled.Events.Attributes; + using Exiled.Events.EventArgs.Player; + using HarmonyLib; + using InventorySystem.Items.Firearms.Modules; + + using static HarmonyLib.AccessTools; + + /// + /// Patches . + /// Adds the event. + /// + [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.ReloadingWeapon))] + [HarmonyPatch(typeof(AnimatorReloaderModuleBase), nameof(AnimatorReloaderModuleBase.ServerProcessCmd))] + internal static class ReloadingWeapon + { + private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + int offset = 2; + int index = newInstructions.FindIndex(x => x.Calls(Method(typeof(IReloadUnloadValidatorModule), nameof(IReloadUnloadValidatorModule.ValidateReload)))) + offset; + + Label skip = (Label)newInstructions[index - 1].operand; + newInstructions.InsertRange( + index, + new[] + { + // player + new CodeInstruction(OpCodes.Ldarg_0), + new(OpCodes.Callvirt, PropertyGetter(typeof(AnimatorReloaderModuleBase), nameof(AnimatorReloaderModuleBase.Firearm))), + + // ReloadingWeaponEventArgs ev = new(firearm) + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ReloadingWeaponEventArgs))[0]), + new(OpCodes.Dup), + + // Player.OnReloadingWeapon(ev) + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnReloadingWeapon))), + + // if (!ev.IsAllowed) + // goto skip; + new(OpCodes.Callvirt, PropertyGetter(typeof(ReloadingWeaponEventArgs), nameof(ReloadingWeaponEventArgs.IsAllowed))), + new(OpCodes.Brfalse, skip), + }); + + for (int z = 0; z < newInstructions.Count; z++) + yield return newInstructions[z]; + + ListPool.Pool.Return(newInstructions); + } + } +} diff --git a/EXILED/Exiled.Events/Patches/Events/Player/UnloadingWeapon.cs b/EXILED/Exiled.Events/Patches/Events/Player/UnloadingWeapon.cs new file mode 100644 index 000000000..1600dcca0 --- /dev/null +++ b/EXILED/Exiled.Events/Patches/Events/Player/UnloadingWeapon.cs @@ -0,0 +1,66 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.Patches.Events.Player +{ + using System.Collections.Generic; + using System.Reflection.Emit; + + using API.Features.Pools; + + using Exiled.API.Extensions; + using Exiled.Events.Attributes; + using Exiled.Events.EventArgs.Player; + using HarmonyLib; + using InventorySystem.Items.Firearms.Modules; + + using static HarmonyLib.AccessTools; + + /// + /// Patches . + /// Adds the event. + /// + [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.UnloadingWeapon))] + [HarmonyPatch(typeof(AnimatorReloaderModuleBase), nameof(AnimatorReloaderModuleBase.ServerProcessCmd))] + internal static class UnloadingWeapon + { + private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + int offset = 2; + int index = newInstructions.FindIndex(x => x.Calls(Method(typeof(IReloadUnloadValidatorModule), nameof(IReloadUnloadValidatorModule.ValidateUnload)))) + offset; + + Label skip = (Label)newInstructions[index - 1].operand; + newInstructions.InsertRange( + index, + new[] + { + // player + new CodeInstruction(OpCodes.Ldarg_0), + new(OpCodes.Callvirt, PropertyGetter(typeof(AnimatorReloaderModuleBase), nameof(AnimatorReloaderModuleBase.Firearm))), + + // UnloadingWeaponEventArgs ev = new(firearm) + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UnloadingWeaponEventArgs))[0]), + new(OpCodes.Dup), + + // Player.OnUnloadingWeapon(ev) + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnUnloadingWeapon))), + + // if (!ev.IsAllowed) + // goto skip; + new(OpCodes.Callvirt, PropertyGetter(typeof(UnloadingWeaponEventArgs), nameof(UnloadingWeaponEventArgs.IsAllowed))), + new(OpCodes.Brfalse, skip), + }); + + for (int z = 0; z < newInstructions.Count; z++) + yield return newInstructions[z]; + + ListPool.Pool.Return(newInstructions); + } + } +} diff --git a/EXILED/Exiled.Example/Events/PlayerHandler.cs b/EXILED/Exiled.Example/Events/PlayerHandler.cs index 77cebae44..cf00dbc0b 100644 --- a/EXILED/Exiled.Example/Events/PlayerHandler.cs +++ b/EXILED/Exiled.Example/Events/PlayerHandler.cs @@ -157,8 +157,7 @@ public void OnUsingItem(UsingItemEventArgs ev) /// public void OnShooting(ShootingEventArgs ev) { - // Log.Info($"{ev.Player.Nickname} is shooting a {ev.Player.CurrentItem.Type}! Target Pos: {ev.ShotPosition} Target object ID: {ev.TargetNetId} Allowed: {ev.IsAllowed}"); - // TODO we need to fix this cuz... we need to fix it + Log.Info($"{ev.Player.Nickname} is shooting a {ev.Player.CurrentItem.Type}! Target Pos: {ev.ClaimedTarget?.Position} Direction: {ev.Direction} Allowed: {ev.IsAllowed}"); } /// From ad253907fce18e2bb7c5e5a0b81f274d7bb00d80 Mon Sep 17 00:00:00 2001 From: Cosmos Zvezdochkin <66907231+CosmosZvezdo4kin@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:26:49 +0300 Subject: [PATCH 5/6] fix: Player.IsSpeaking and TeslaGate.IsPlayerInHurtRange (#341) * VoiceModule.IsSpeaking is client-side lol use ServerIsSending instead * Fix TeslaGate.IsPlayerInHurtRange --------- Co-authored-by: Yamato <66829532+louis1706@users.noreply.github.com> --- EXILED/Exiled.API/Features/Player.cs | 2 +- EXILED/Exiled.API/Features/TeslaGate.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/EXILED/Exiled.API/Features/Player.cs b/EXILED/Exiled.API/Features/Player.cs index 8bf776590..be25a2823 100644 --- a/EXILED/Exiled.API/Features/Player.cs +++ b/EXILED/Exiled.API/Features/Player.cs @@ -789,7 +789,7 @@ public bool IsIntercomMuted /// /// Gets a value indicating whether the player is speaking. /// - public bool IsSpeaking => Role is Roles.IVoiceRole voiceRole && voiceRole.VoiceModule.IsSpeaking; + public bool IsSpeaking => Role is Roles.IVoiceRole voiceRole && voiceRole.VoiceModule.ServerIsSending; /// /// Gets the player's voice color. diff --git a/EXILED/Exiled.API/Features/TeslaGate.cs b/EXILED/Exiled.API/Features/TeslaGate.cs index 90595f7cd..ddc04ac79 100644 --- a/EXILED/Exiled.API/Features/TeslaGate.cs +++ b/EXILED/Exiled.API/Features/TeslaGate.cs @@ -261,7 +261,7 @@ public void ForceTrigger() /// /// The to check. /// if the given is in the hurt range of the tesla gate; otherwise, . - public bool IsPlayerInHurtRange(Player player) => player is not null && Vector3.Distance(Position, player.Position) <= Base.sizeOfTrigger * 2.2f; + public bool IsPlayerInHurtRange(Player player) => player is not null && Base.killers.Any(x => new Bounds(x.transform.position, Base.sizeOfKiller).Contains(player.Position)); /// /// Gets a value indicating whether the is in the idle range of a specific tesla gate. From f7aa841ee1bad64faa9b5727301f82d883154a3f Mon Sep 17 00:00:00 2001 From: Yamato <66829532+louis1706@users.noreply.github.com> Date: Thu, 26 Dec 2024 19:28:02 +0100 Subject: [PATCH 6/6] RespawningTeam Fix Index (#342) --- EXILED/Exiled.Events/Patches/Events/Server/RespawningTeam.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Server/RespawningTeam.cs b/EXILED/Exiled.Events/Patches/Events/Server/RespawningTeam.cs index 76afb884d..37a470d8a 100644 --- a/EXILED/Exiled.Events/Patches/Events/Server/RespawningTeam.cs +++ b/EXILED/Exiled.Events/Patches/Events/Server/RespawningTeam.cs @@ -19,6 +19,7 @@ namespace Exiled.Events.Patches.Events.Server using Exiled.Events.Handlers; using HarmonyLib; using PlayerRoles; + using Respawning.NamingRules; using Respawning.Waves; using static HarmonyLib.AccessTools; @@ -37,8 +38,8 @@ private static IEnumerable Transpiler(IEnumerable newInstructions = ListPool.Pool.Get(instructions); - int offset = 1; - int index = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(SpawnableWaveBase), nameof(SpawnableWaveBase.PopulateQueue)))) + offset; + int offset = -2; + int index = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(NamingRulesManager), nameof(NamingRulesManager.TryGetNamingRule)))) + offset; LocalBuilder ev = generator.DeclareLocal(typeof(RespawningTeamEventArgs));