From aebdd4d7c11bf2f1c2c054f7c80b899674650f14 Mon Sep 17 00:00:00 2001 From: Yamato Date: Thu, 1 Aug 2024 02:03:02 +0200 Subject: [PATCH 01/12] `Item::::Get()` and `Pickup::Get()` --- EXILED/Exiled.API/Features/Items/ExplosiveGrenade.cs | 2 +- EXILED/Exiled.API/Features/Items/FlashGrenade.cs | 2 +- EXILED/Exiled.API/Features/Items/Item.cs | 11 +++++++++-- EXILED/Exiled.API/Features/Items/Scp018.cs | 2 +- EXILED/Exiled.API/Features/Items/Scp2176.cs | 2 +- EXILED/Exiled.API/Features/Items/Scp330.cs | 4 ++-- EXILED/Exiled.API/Features/Items/Throwable.cs | 2 +- EXILED/Exiled.API/Features/Pickups/Pickup.cs | 9 +++++++++ EXILED/Exiled.API/Features/Player.cs | 4 ++-- .../EventArgs/Map/ExplodingGrenadeEventArgs.cs | 4 ++-- .../EventArgs/Player/CancellingItemUseEventArgs.cs | 2 +- .../Player/ChangingMicroHIDStateEventArgs.cs | 2 +- .../EventArgs/Player/ChangingRadioPresetEventArgs.cs | 2 +- .../EventArgs/Player/ThrowingRequestEventArgs.cs | 2 +- .../EventArgs/Player/ThrownProjectileEventArgs.cs | 4 ++-- .../EventArgs/Player/TogglingFlashlightEventArgs.cs | 2 +- .../EventArgs/Player/TogglingRadioEventArgs.cs | 2 +- .../EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs | 2 +- .../EventArgs/Player/UsingRadioBatteryEventArgs.cs | 2 +- .../EventArgs/Scp244/UsingScp244EventArgs.cs | 2 +- .../EventArgs/Scp330/DroppingScp330EventArgs.cs | 2 +- .../Patches/Events/Player/FirearmRequestReceived.cs | 2 +- 22 files changed, 42 insertions(+), 26 deletions(-) diff --git a/EXILED/Exiled.API/Features/Items/ExplosiveGrenade.cs b/EXILED/Exiled.API/Features/Items/ExplosiveGrenade.cs index 8f9aa165c..0002d5d76 100644 --- a/EXILED/Exiled.API/Features/Items/ExplosiveGrenade.cs +++ b/EXILED/Exiled.API/Features/Items/ExplosiveGrenade.cs @@ -119,7 +119,7 @@ public ExplosionGrenadeProjectile SpawnActive(Vector3 position, Player owner = n ipb.Info = new PickupSyncInfo(Type, Weight, ItemSerialGenerator.GenerateNext()); - ExplosionGrenadeProjectile grenade = (ExplosionGrenadeProjectile)Pickup.Get(ipb); + ExplosionGrenadeProjectile grenade = Pickup.Get(ipb); grenade.Base.gameObject.SetActive(true); diff --git a/EXILED/Exiled.API/Features/Items/FlashGrenade.cs b/EXILED/Exiled.API/Features/Items/FlashGrenade.cs index e2d009ac7..979784f2c 100644 --- a/EXILED/Exiled.API/Features/Items/FlashGrenade.cs +++ b/EXILED/Exiled.API/Features/Items/FlashGrenade.cs @@ -100,7 +100,7 @@ public FlashbangProjectile SpawnActive(Vector3 position, Player owner = null) ipb.Info = new PickupSyncInfo(Type, Weight, ItemSerialGenerator.GenerateNext()); - FlashbangProjectile grenade = (FlashbangProjectile)Pickup.Get(ipb); + FlashbangProjectile grenade = Pickup.Get(ipb); grenade.Base.gameObject.SetActive(true); diff --git a/EXILED/Exiled.API/Features/Items/Item.cs b/EXILED/Exiled.API/Features/Items/Item.cs index fd0bba35a..28e48d2ef 100644 --- a/EXILED/Exiled.API/Features/Items/Item.cs +++ b/EXILED/Exiled.API/Features/Items/Item.cs @@ -25,7 +25,6 @@ namespace Exiled.API.Features.Items using InventorySystem.Items.Radio; using InventorySystem.Items.ThrowableProjectiles; using InventorySystem.Items.ToggleableLights; - using InventorySystem.Items.ToggleableLights.Flashlight; using InventorySystem.Items.Usables; using InventorySystem.Items.Usables.Scp1576; using InventorySystem.Items.Usables.Scp244; @@ -33,7 +32,6 @@ namespace Exiled.API.Features.Items using UnityEngine; using BaseConsumable = InventorySystem.Items.Usables.Consumable; - using Object = UnityEngine.Object; /// /// A wrapper class for . @@ -219,6 +217,15 @@ public static Item Get(ItemBase itemBase) }; } + /// + /// Gets an existing or creates a new instance of one. + /// + /// The to convert into an item. + /// The specified type. + /// The item wrapper for the given . + public static T Get(ItemBase itemBase) + where T : Item => Get(itemBase) as T; + /// /// Gets the Item belonging to the specified serial. /// diff --git a/EXILED/Exiled.API/Features/Items/Scp018.cs b/EXILED/Exiled.API/Features/Items/Scp018.cs index 57f8122c1..7c08c7c6d 100644 --- a/EXILED/Exiled.API/Features/Items/Scp018.cs +++ b/EXILED/Exiled.API/Features/Items/Scp018.cs @@ -86,7 +86,7 @@ public Scp018Projectile SpawnActive(Vector3 position, Player owner = null) ipb.Info = new PickupSyncInfo(Type, Weight, ItemSerialGenerator.GenerateNext()); - Scp018Projectile grenade = (Scp018Projectile)Pickup.Get(ipb); + Scp018Projectile grenade = Pickup.Get(ipb); grenade.Base.gameObject.SetActive(true); diff --git a/EXILED/Exiled.API/Features/Items/Scp2176.cs b/EXILED/Exiled.API/Features/Items/Scp2176.cs index 412c371d6..0ca9fc54d 100644 --- a/EXILED/Exiled.API/Features/Items/Scp2176.cs +++ b/EXILED/Exiled.API/Features/Items/Scp2176.cs @@ -71,7 +71,7 @@ public Scp2176Projectile SpawnActive(Vector3 position, Player owner = null) ipb.Info = new PickupSyncInfo(Type, Weight, ItemSerialGenerator.GenerateNext()); - Scp2176Projectile grenade = (Scp2176Projectile)Pickup.Get(ipb); + Scp2176Projectile grenade = Pickup.Get(ipb); grenade.Base.gameObject.SetActive(true); diff --git a/EXILED/Exiled.API/Features/Items/Scp330.cs b/EXILED/Exiled.API/Features/Items/Scp330.cs index d0a7e3b9c..689929f12 100644 --- a/EXILED/Exiled.API/Features/Items/Scp330.cs +++ b/EXILED/Exiled.API/Features/Items/Scp330.cs @@ -197,7 +197,7 @@ public IEnumerable DropCandy(CandyKindID type, bool dropAll = fals ipb.Info = new(Type, Weight, ItemSerialGenerator.GenerateNext()); - Scp330Pickup pickup = (Scp330Pickup)Pickup.Get(ipb); + Scp330Pickup pickup = Pickup.Get(ipb); if (exposedType is not CandyKindID.None) pickup.ExposedCandy = exposedType; @@ -218,7 +218,7 @@ public IEnumerable DropCandy(CandyKindID type, bool dropAll = fals ipb.Info = new(Type, Weight, ItemSerialGenerator.GenerateNext()); - Scp330Pickup pickup = (Scp330Pickup)Pickup.Get(ipb); + Scp330Pickup pickup = Pickup.Get(ipb); if (exposedType is not CandyKindID.None) pickup.ExposedCandy = exposedType; diff --git a/EXILED/Exiled.API/Features/Items/Throwable.cs b/EXILED/Exiled.API/Features/Items/Throwable.cs index 4946d1722..4de522295 100644 --- a/EXILED/Exiled.API/Features/Items/Throwable.cs +++ b/EXILED/Exiled.API/Features/Items/Throwable.cs @@ -29,7 +29,7 @@ public Throwable(ThrowableItem itemBase) { Base = itemBase; Base.Projectile.gameObject.SetActive(false); - Projectile = (Projectile)Pickup.Get(Object.Instantiate(Base.Projectile)); + Projectile = Pickup.Get(Object.Instantiate(Base.Projectile)); Base.Projectile.gameObject.SetActive(true); Projectile.Serial = Serial; } diff --git a/EXILED/Exiled.API/Features/Pickups/Pickup.cs b/EXILED/Exiled.API/Features/Pickups/Pickup.cs index 70db62a8a..2c25b88e1 100644 --- a/EXILED/Exiled.API/Features/Pickups/Pickup.cs +++ b/EXILED/Exiled.API/Features/Pickups/Pickup.cs @@ -338,6 +338,15 @@ public static Pickup Get(ItemPickupBase pickupBase) }; } + /// + /// Gets an existing or creates a new instance of one. + /// + /// The to convert into an item. + /// The specified type. + /// The item wrapper for the given . + public static T Get(ItemPickupBase itemBase) + where T : Pickup => Get(itemBase) as T; + /// /// Gets the given a . /// diff --git a/EXILED/Exiled.API/Features/Player.cs b/EXILED/Exiled.API/Features/Player.cs index 4991e13d4..d44a570e2 100644 --- a/EXILED/Exiled.API/Features/Player.cs +++ b/EXILED/Exiled.API/Features/Player.cs @@ -958,7 +958,7 @@ public Item CurrentItem /// /// Gets the armor that the player is currently wearing. Value will be if the player is not wearing any armor. /// - public Armor CurrentArmor => Inventory.TryGetBodyArmor(out BodyArmor armor) ? (Armor)Item.Get(armor) : null; + public Armor CurrentArmor => Inventory.TryGetBodyArmor(out BodyArmor armor) ? Item.Get(armor) : null; /// /// Gets the class. @@ -2582,7 +2582,7 @@ public void AddItem(Firearm item, IEnumerable identifiers) /// The that was added. public Item AddItem(FirearmPickup pickup, IEnumerable identifiers) { - Firearm firearm = (Firearm)Item.Get(Inventory.ServerAddItem(pickup.Type, pickup.Serial, pickup.Base)); + Firearm firearm = Item.Get(Inventory.ServerAddItem(pickup.Type, pickup.Serial, pickup.Base)); if (identifiers is not null) firearm.AddAttachment(identifiers); diff --git a/EXILED/Exiled.Events/EventArgs/Map/ExplodingGrenadeEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Map/ExplodingGrenadeEventArgs.cs index 28b7b30f0..a0dbd7920 100644 --- a/EXILED/Exiled.Events/EventArgs/Map/ExplodingGrenadeEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Map/ExplodingGrenadeEventArgs.cs @@ -37,7 +37,7 @@ public class ExplodingGrenadeEventArgs : IPlayerEvent, IDeniableEvent public ExplodingGrenadeEventArgs(Footprint thrower, Vector3 position, ExplosionGrenade grenade, Collider[] targets) { Player = Player.Get(thrower.Hub); - Projectile = (EffectGrenadeProjectile)Pickup.Get(grenade); + Projectile = Pickup.Get(grenade); Position = position; TargetsToAffect = ListPool.Pool.Get(); @@ -97,7 +97,7 @@ public ExplodingGrenadeEventArgs(Footprint thrower, Vector3 position, ExplosionG public ExplodingGrenadeEventArgs(Player thrower, EffectGrenade grenade, List targetsToAffect, bool isAllowed = true) { Player = thrower ?? Server.Host; - Projectile = (EffectGrenadeProjectile)Pickup.Get(grenade); + Projectile = Pickup.Get(grenade); Position = Projectile.Position; TargetsToAffect = ListPool.Pool.Get(targetsToAffect ?? new()); IsAllowed = isAllowed; diff --git a/EXILED/Exiled.Events/EventArgs/Player/CancellingItemUseEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/CancellingItemUseEventArgs.cs index ac07393e7..f3f623f40 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/CancellingItemUseEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/CancellingItemUseEventArgs.cs @@ -27,7 +27,7 @@ public class CancellingItemUseEventArgs : IPlayerEvent, IDeniableEvent, IUsableE public CancellingItemUseEventArgs(Player player, UsableItem item) { Player = player; - Usable = Item.Get(item) is Usable usable ? usable : null; + Usable = Item.Get(item); } /// diff --git a/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs index 4cb941b0a..2b9bacdbf 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs @@ -40,7 +40,7 @@ public class ChangingMicroHIDStateEventArgs : IPlayerEvent, IDeniableEvent public ChangingMicroHIDStateEventArgs(Player player, MicroHIDItem microHID, HidState oldState, HidState newState, bool isAllowed = true) { Player = player; - MicroHID = (MicroHid)Item.Get(microHID); + MicroHID = Item.Get(microHID); OldState = oldState; NewState = newState; IsAllowed = isAllowed; diff --git a/EXILED/Exiled.Events/EventArgs/Player/ChangingRadioPresetEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ChangingRadioPresetEventArgs.cs index 7b59f2fb9..70af9b581 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/ChangingRadioPresetEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/ChangingRadioPresetEventArgs.cs @@ -44,7 +44,7 @@ public class ChangingRadioPresetEventArgs : IPlayerEvent, IItemEvent, IDeniableE public ChangingRadioPresetEventArgs(Player player, RadioItem item, RadioRangeLevel oldValue, RadioRangeLevel newValue, bool isAllowed = true) { Player = player; - Radio = (Radio)Item.Get(item); + Radio = Item.Get(item); OldValue = (RadioRange)oldValue; NewValue = (RadioRange)newValue; IsAllowed = isAllowed; diff --git a/EXILED/Exiled.Events/EventArgs/Player/ThrowingRequestEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ThrowingRequestEventArgs.cs index 273763fda..089695333 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/ThrowingRequestEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/ThrowingRequestEventArgs.cs @@ -28,7 +28,7 @@ public class ThrowingRequestEventArgs : IPlayerEvent, IItemEvent public ThrowingRequestEventArgs(Player player, ThrowableItem item, ThrowableNetworkHandler.RequestType request) { Player = player; - Throwable = (Throwable)Item.Get(item); + Throwable = Item.Get(item); RequestType = (ThrowRequest)request; } diff --git a/EXILED/Exiled.Events/EventArgs/Player/ThrownProjectileEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ThrownProjectileEventArgs.cs index aaf654d42..26984b839 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/ThrownProjectileEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/ThrownProjectileEventArgs.cs @@ -29,8 +29,8 @@ public class ThrownProjectileEventArgs : IPlayerEvent, IItemEvent, IPickupEvent public ThrownProjectileEventArgs(ThrownProjectile projectile, Player player, ThrowableItem item) { Player = player; - Throwable = (Throwable)Item.Get(item); - Projectile = (Projectile)Pickup.Get(projectile); + Throwable = Item.Get(item); + Projectile = Pickup.Get(projectile); } /// diff --git a/EXILED/Exiled.Events/EventArgs/Player/TogglingFlashlightEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/TogglingFlashlightEventArgs.cs index b8990f85d..3a87a66a1 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/TogglingFlashlightEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/TogglingFlashlightEventArgs.cs @@ -35,7 +35,7 @@ public class TogglingFlashlightEventArgs : IPlayerEvent, IDeniableEvent, IItemEv public TogglingFlashlightEventArgs(ReferenceHub hub, ToggleableLightItemBase flashlight, bool newState) { Player = Player.Get(hub); - Flashlight = (Flashlight)Item.Get(flashlight); + Flashlight = Item.Get(flashlight); initialState = newState; NewState = newState; } diff --git a/EXILED/Exiled.Events/EventArgs/Player/TogglingRadioEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/TogglingRadioEventArgs.cs index 67db54505..38d0fe09b 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/TogglingRadioEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/TogglingRadioEventArgs.cs @@ -36,7 +36,7 @@ public class TogglingRadioEventArgs : IPlayerEvent, IDeniableEvent, IItemEvent public TogglingRadioEventArgs(Player player, RadioItem radio, bool newState, bool isAllowed = true) { Player = player; - Radio = (Radio)Item.Get(radio); + Radio = Item.Get(radio); NewState = newState; IsAllowed = isAllowed; } diff --git a/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs index d98f014ed..402d20e1b 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs @@ -40,7 +40,7 @@ public class UsingMicroHIDEnergyEventArgs : IPlayerEvent, IDeniableEvent, IItemE public UsingMicroHIDEnergyEventArgs(Player player, MicroHIDItem microHIDitem, HidState currentState, float drain, bool isAllowed = true) { Player = player; - MicroHID = (MicroHid)Item.Get(microHIDitem); + MicroHID = Item.Get(microHIDitem); CurrentState = currentState; Drain = drain; IsAllowed = isAllowed; diff --git a/EXILED/Exiled.Events/EventArgs/Player/UsingRadioBatteryEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/UsingRadioBatteryEventArgs.cs index 90369883f..1f07dadde 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/UsingRadioBatteryEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/UsingRadioBatteryEventArgs.cs @@ -36,7 +36,7 @@ public class UsingRadioBatteryEventArgs : IPlayerEvent, IDeniableEvent, IItemEve /// public UsingRadioBatteryEventArgs(RadioItem radio, Player player, float drain, bool isAllowed = true) { - Radio = (Radio)Item.Get(radio); + Radio = Item.Get(radio); Player = player; Drain = drain; IsAllowed = isAllowed; diff --git a/EXILED/Exiled.Events/EventArgs/Scp244/UsingScp244EventArgs.cs b/EXILED/Exiled.Events/EventArgs/Scp244/UsingScp244EventArgs.cs index c3077777c..5dca87a99 100644 --- a/EXILED/Exiled.Events/EventArgs/Scp244/UsingScp244EventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Scp244/UsingScp244EventArgs.cs @@ -33,7 +33,7 @@ public class UsingScp244EventArgs : IPlayerEvent, IDeniableEvent /// public UsingScp244EventArgs(Scp244Item scp244, Player player, bool isAllowed = true) { - Scp244 = (Scp244)Item.Get(scp244); + Scp244 = Item.Get(scp244); Player = player; IsAllowed = isAllowed; } diff --git a/EXILED/Exiled.Events/EventArgs/Scp330/DroppingScp330EventArgs.cs b/EXILED/Exiled.Events/EventArgs/Scp330/DroppingScp330EventArgs.cs index 5aabbd226..509550074 100644 --- a/EXILED/Exiled.Events/EventArgs/Scp330/DroppingScp330EventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Scp330/DroppingScp330EventArgs.cs @@ -34,7 +34,7 @@ public class DroppingScp330EventArgs : IPlayerEvent, IDeniableEvent public DroppingScp330EventArgs(Player player, Scp330Bag scp330, CandyKindID candy) { Player = player; - Scp330 = (Scp330)Item.Get(scp330); + Scp330 = Item.Get(scp330); Candy = candy; } diff --git a/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs b/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs index 6ebd931f4..db0f0b35c 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs @@ -65,7 +65,7 @@ private static IEnumerable Transpiler(IEnumerable(hub); // if (Firearm == null) // return; new CodeInstruction(OpCodes.Ldloc_1), From 6a50ca0cc3e169c9ba007754a2d820d1299508c7 Mon Sep 17 00:00:00 2001 From: Yamato Date: Thu, 1 Aug 2024 02:04:18 +0200 Subject: [PATCH 02/12] Revert doc change --- .../Patches/Events/Player/FirearmRequestReceived.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs b/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs index db0f0b35c..6ebd931f4 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs @@ -65,7 +65,7 @@ private static IEnumerable Transpiler(IEnumerable(hub); + // Firearm firearm = (Firearm)Item.Get(hub); // if (Firearm == null) // return; new CodeInstruction(OpCodes.Ldloc_1), From 9feefbc178be69e5948b25b06879120fdc506ea1 Mon Sep 17 00:00:00 2001 From: Yamato Date: Thu, 1 Aug 2024 02:09:09 +0200 Subject: [PATCH 03/12] Add `Hazard::Get()` --- EXILED/Exiled.API/Features/Hazards/Hazard.cs | 9 +++++++++ EXILED/Exiled.API/Features/Map.cs | 2 +- EXILED/Exiled.API/Features/TeslaGate.cs | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/EXILED/Exiled.API/Features/Hazards/Hazard.cs b/EXILED/Exiled.API/Features/Hazards/Hazard.cs index 6861d1a68..71f7e4acf 100644 --- a/EXILED/Exiled.API/Features/Hazards/Hazard.cs +++ b/EXILED/Exiled.API/Features/Hazards/Hazard.cs @@ -123,6 +123,15 @@ public static Hazard Get(EnvironmentalHazard environmentalHazard) => _ => new Hazard(environmentalHazard) }; + /// + /// Gets the by . + /// + /// The to convert into an hazard. + /// The specified type. + /// The hazard wrapper for the given . + public static T Get(EnvironmentalHazard itemBase) + where T : Hazard => Get(itemBase) as T; + /// /// Gets the hazard by the room where it's located. /// diff --git a/EXILED/Exiled.API/Features/Map.cs b/EXILED/Exiled.API/Features/Map.cs index 14f6d8214..c27f78840 100644 --- a/EXILED/Exiled.API/Features/Map.cs +++ b/EXILED/Exiled.API/Features/Map.cs @@ -299,7 +299,7 @@ public static TantrumHazard PlaceTantrum(Vector3 position, bool isActive = true) NetworkServer.Spawn(tantrum.gameObject); - return Hazard.Get(tantrum).Cast(); + return Hazard.Get(tantrum); } /// diff --git a/EXILED/Exiled.API/Features/TeslaGate.cs b/EXILED/Exiled.API/Features/TeslaGate.cs index 7ebdf6480..023d7a1b1 100644 --- a/EXILED/Exiled.API/Features/TeslaGate.cs +++ b/EXILED/Exiled.API/Features/TeslaGate.cs @@ -178,7 +178,7 @@ public bool UseInstantBurst /// /// Gets a of which contains all the tantrums to destroy. /// - public IEnumerable TantrumsToDestroy => Base.TantrumsToBeDestroyed.Select(x => Hazard.Get(x) as TantrumHazard); + public IEnumerable TantrumsToDestroy => Base.TantrumsToBeDestroyed.Select(x => Hazard.Get(x)); /// /// Gets a of which contains all the players inside the hurt range. From 1b6fe4cfd984b86b4e5ff87e962df681ed470ef8 Mon Sep 17 00:00:00 2001 From: Yamato Date: Thu, 1 Aug 2024 02:09:44 +0200 Subject: [PATCH 04/12] doc fix --- EXILED/Exiled.API/Features/Pickups/Pickup.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EXILED/Exiled.API/Features/Pickups/Pickup.cs b/EXILED/Exiled.API/Features/Pickups/Pickup.cs index 2c25b88e1..ef87757d8 100644 --- a/EXILED/Exiled.API/Features/Pickups/Pickup.cs +++ b/EXILED/Exiled.API/Features/Pickups/Pickup.cs @@ -341,9 +341,9 @@ public static Pickup Get(ItemPickupBase pickupBase) /// /// Gets an existing or creates a new instance of one. /// - /// The to convert into an item. + /// The to convert into an pickup. /// The specified type. - /// The item wrapper for the given . + /// The pickup wrapper for the given . public static T Get(ItemPickupBase itemBase) where T : Pickup => Get(itemBase) as T; From 8df24b979cfc124f8140a778b1fe38f667e32e9c Mon Sep 17 00:00:00 2001 From: Yamato Date: Thu, 1 Aug 2024 02:17:38 +0200 Subject: [PATCH 05/12] `Door::Get()` --- EXILED/Exiled.API/Features/Doors/Door.cs | 57 ++++++++++++++----- EXILED/Exiled.API/Features/Hazards/Hazard.cs | 6 +- EXILED/Exiled.API/Features/Lift.cs | 4 +- EXILED/Exiled.API/Features/Pickups/Pickup.cs | 6 +- .../Scp096/StartPryingGateEventArgs.cs | 2 +- .../Exiled.Events/Patches/Generic/DoorList.cs | 2 +- 6 files changed, 53 insertions(+), 24 deletions(-) diff --git a/EXILED/Exiled.API/Features/Doors/Door.cs b/EXILED/Exiled.API/Features/Doors/Door.cs index 584dc3fb0..338ee1900 100644 --- a/EXILED/Exiled.API/Features/Doors/Door.cs +++ b/EXILED/Exiled.API/Features/Doors/Door.cs @@ -14,7 +14,9 @@ namespace Exiled.API.Features.Doors using Exiled.API.Enums; using Exiled.API.Extensions; using Exiled.API.Features.Core; + using Exiled.API.Features.Hazards; using Exiled.API.Interfaces; + using global::Hazards; using Interactables.Interobjects; using Interactables.Interobjects.DoorUtils; using MEC; @@ -309,6 +311,31 @@ public static Door Get(DoorVariant doorVariant) return DoorVariantToDoor[doorVariant]; } + /// + /// Gets the by . + /// + /// The to convert into an door. + /// The specified type. + /// The door wrapper for the given . + public static T Get(DoorVariant doorVariant) + where T : Door => Get(doorVariant) as T; + + /// + /// Gets a given the specified . + /// + /// The to search for. + /// The with the given or if not found. + public static Door Get(DoorType doorType) => List.FirstOrDefault(x => x.Type == doorType); + + /// + /// Gets the by . + /// + /// The to convert into an door. + /// The specified type. + /// The door wrapper for the given . + public static T Get(DoorType doorType) + where T : Door => Get(doorType) as T; + /// /// Gets a given the specified name. /// @@ -320,6 +347,15 @@ public static Door Get(string name) return nameExtension is null ? null : Get(nameExtension.TargetDoor); } + /// + /// Gets the by . + /// + /// The name to search for. + /// The specified type. + /// The door wrapper for the given . + public static T Get(string name) + where T : Door => Get(name) as T; + /// /// Gets the door object associated with a specific , or creates a new one if there isn't one. /// @@ -327,20 +363,6 @@ public static Door Get(string name) /// The with the given name or if not found. public static Door Get(GameObject gameObject) => gameObject is null ? null : Get(gameObject.GetComponentInChildren()); - /// - /// Gets a of filtered based on a predicate. - /// - /// The condition to satify. - /// A of which contains elements that satify the condition. - public static IEnumerable Get(Func predicate) => List.Where(predicate); - - /// - /// Gets a given the specified . - /// - /// The to search for. - /// The with the given or if not found. - public static Door Get(DoorType doorType) => List.FirstOrDefault(x => x.Type == doorType); - /// /// Returns the closest to the given . /// @@ -367,6 +389,13 @@ public static Door Random(ZoneType type = ZoneType.Unspecified, bool onlyUnbroke return doors[UnityEngine.Random.Range(0, doors.Count)]; } + /// + /// Gets a of filtered based on a predicate. + /// + /// The condition to satify. + /// A of which contains elements that satify the condition. + public static IEnumerable Get(Func predicate) => List.Where(predicate); + /// /// Locks all doors given the specified . /// diff --git a/EXILED/Exiled.API/Features/Hazards/Hazard.cs b/EXILED/Exiled.API/Features/Hazards/Hazard.cs index 71f7e4acf..ce5942161 100644 --- a/EXILED/Exiled.API/Features/Hazards/Hazard.cs +++ b/EXILED/Exiled.API/Features/Hazards/Hazard.cs @@ -126,11 +126,11 @@ public static Hazard Get(EnvironmentalHazard environmentalHazard) => /// /// Gets the by . /// - /// The to convert into an hazard. + /// The to convert into an hazard. /// The specified type. /// The hazard wrapper for the given . - public static T Get(EnvironmentalHazard itemBase) - where T : Hazard => Get(itemBase) as T; + public static T Get(EnvironmentalHazard environmentalHazard) + where T : Hazard => Get(environmentalHazard) as T; /// /// Gets the hazard by the room where it's located. diff --git a/EXILED/Exiled.API/Features/Lift.cs b/EXILED/Exiled.API/Features/Lift.cs index 944e5bd77..6a939bcdd 100644 --- a/EXILED/Exiled.API/Features/Lift.cs +++ b/EXILED/Exiled.API/Features/Lift.cs @@ -76,7 +76,7 @@ internal Lift(ElevatorChamber elevator) /// /// Gets a value of the internal doors list. /// - public IReadOnlyCollection Doors => internalDoorsList.Select(x => Door.Get(x).As()).ToList(); + public IReadOnlyCollection Doors => internalDoorsList.Select(x => Door.Get(x)).ToList(); /// /// Gets a of in the . @@ -201,7 +201,7 @@ public float AnimationTime /// /// Gets the . /// - public Doors.ElevatorDoor CurrentDestination => Door.Get(Base.CurrentDestination).As(); + public Doors.ElevatorDoor CurrentDestination => Door.Get(Base.CurrentDestination); /// /// Gets a of which contains all the instances from the specified . diff --git a/EXILED/Exiled.API/Features/Pickups/Pickup.cs b/EXILED/Exiled.API/Features/Pickups/Pickup.cs index ef87757d8..0f038c1ec 100644 --- a/EXILED/Exiled.API/Features/Pickups/Pickup.cs +++ b/EXILED/Exiled.API/Features/Pickups/Pickup.cs @@ -341,11 +341,11 @@ public static Pickup Get(ItemPickupBase pickupBase) /// /// Gets an existing or creates a new instance of one. /// - /// The to convert into an pickup. + /// The to convert into an pickup. /// The specified type. /// The pickup wrapper for the given . - public static T Get(ItemPickupBase itemBase) - where T : Pickup => Get(itemBase) as T; + public static T Get(ItemPickupBase pickupBase) + where T : Pickup => Get(pickupBase) as T; /// /// Gets the given a . diff --git a/EXILED/Exiled.Events/EventArgs/Scp096/StartPryingGateEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Scp096/StartPryingGateEventArgs.cs index f40e1e874..be8cef0ce 100644 --- a/EXILED/Exiled.Events/EventArgs/Scp096/StartPryingGateEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Scp096/StartPryingGateEventArgs.cs @@ -36,7 +36,7 @@ public StartPryingGateEventArgs(Player player, PryableDoor gate, bool isAllowed { Player = player; Scp096 = player.Role.As(); - Gate = Door.Get(gate).As(); + Gate = Door.Get(gate); IsAllowed = isAllowed; } diff --git a/EXILED/Exiled.Events/Patches/Generic/DoorList.cs b/EXILED/Exiled.Events/Patches/Generic/DoorList.cs index 1b63ec329..4f1ce6e33 100644 --- a/EXILED/Exiled.Events/Patches/Generic/DoorList.cs +++ b/EXILED/Exiled.Events/Patches/Generic/DoorList.cs @@ -50,7 +50,7 @@ private static void Postfix(DoorVariant __instance) foreach (DoorVariant subDoor in checkpoint.Base.SubDoors) { subDoor.RegisterRooms(); - BreakableDoor targetDoor = Door.Get(subDoor).Cast(); + BreakableDoor targetDoor = Door.Get(subDoor); targetDoor.ParentCheckpointDoor = checkpoint; checkpoint.SubDoorsValue.Add(targetDoor); From f2b225d17e64fa3b142c77a4ce753325803a2531 Mon Sep 17 00:00:00 2001 From: Yamato Date: Thu, 1 Aug 2024 02:30:49 +0200 Subject: [PATCH 06/12] AdminToy.Get() --- EXILED/Exiled.API/Features/Toys/AdminToy.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/EXILED/Exiled.API/Features/Toys/AdminToy.cs b/EXILED/Exiled.API/Features/Toys/AdminToy.cs index fdbe03da1..bbffd7196 100644 --- a/EXILED/Exiled.API/Features/Toys/AdminToy.cs +++ b/EXILED/Exiled.API/Features/Toys/AdminToy.cs @@ -132,6 +132,15 @@ public bool IsStatic /// The corresponding instance. public static AdminToy Get(AdminToyBase adminToyBase) => Map.Toys.FirstOrDefault(x => x.AdminToyBase == adminToyBase); + /// + /// Gets the by . + /// + /// The to convert into an admintoy. + /// The specified type. + /// The admintoy wrapper for the given . + public static T Get(AdminToyBase adminToyBase) + where T : AdminToy => Get(adminToyBase) as T; + /// /// Spawns the toy into the game. Use to remove it. /// From db53ca7d5c47c48139d483ed567b4ee8cac62062 Mon Sep 17 00:00:00 2001 From: Yamato Date: Thu, 1 Aug 2024 16:09:24 +0200 Subject: [PATCH 07/12] More implementation --- EXILED/Exiled.API/Features/Items/Item.cs | 34 +++++++++++++++ EXILED/Exiled.API/Features/Pickups/Pickup.cs | 43 +++++++++++++++++++ .../Pickups/Projectiles/Projectile.cs | 43 ++++++++++++++++--- 3 files changed, 115 insertions(+), 5 deletions(-) diff --git a/EXILED/Exiled.API/Features/Items/Item.cs b/EXILED/Exiled.API/Features/Items/Item.cs index 28e48d2ef..cef0b5017 100644 --- a/EXILED/Exiled.API/Features/Items/Item.cs +++ b/EXILED/Exiled.API/Features/Items/Item.cs @@ -286,6 +286,40 @@ ItemType.KeycardGuard or ItemType.KeycardJanitor or ItemType.KeycardO5 or ItemTy _ => new Item(type), }; + /// + /// Creates a new with the proper inherited subclass. + /// + /// Based on the , the returned can be casted into a subclass to gain more control over the object. + ///
- Usable items (Adrenaline, Medkit, Painkillers, SCP-207, SCP-268, and SCP-500) should be casted to the class. + ///
- All valid ammo should be casted to the class. + ///
- All valid firearms (not including the Micro HID) should be casted to the class. + ///
- All valid keycards should be casted to the class. + ///
- All valid armor should be casted to the class. + ///
- Explosive grenades and SCP-018 should be casted to the class. + ///
- Flash grenades should be casted to the class. + ///
+ /// + ///
The following have their own respective classes: + ///
- Flashlights can be casted to . + ///
- Radios can be casted to . + ///
- The Micro HID can be casted to . + ///
- SCP-244 A and B variants can be casted to . + ///
- SCP-330 can be casted to . + ///
- SCP-2176 can be casted to the class. + ///
- SCP-1576 can be casted to the class. + ///
- Jailbird can be casted to the class. + ///
+ /// + /// Items that are not listed above do not have a subclass, and can only use the base class. + /// + ///
+ /// The of the item to create. + /// The who owns the item by default. + /// The specified type. + /// The created. This can be cast as a subclass. + public static Item Create(ItemType type, Player owner = null) + where T : Item => Create(type, owner) as T; + /// /// Gives this item to a . /// diff --git a/EXILED/Exiled.API/Features/Pickups/Pickup.cs b/EXILED/Exiled.API/Features/Pickups/Pickup.cs index 0f038c1ec..3f7373dee 100644 --- a/EXILED/Exiled.API/Features/Pickups/Pickup.cs +++ b/EXILED/Exiled.API/Features/Pickups/Pickup.cs @@ -496,6 +496,36 @@ public static IEnumerable Get(IEnumerable gameObjects) _ => new Pickup(type), }; + /// + /// Creates and returns a new with the proper inherited subclass. + /// + /// Based on the , the returned can be cast into a subclass to gain more control over the object. + ///
- All valid ammo should be cast to the class. + ///
- All valid firearms (not including the Micro HID) should be cast to the class. + ///
- All valid keycards should be cast to the class. + ///
- All valid armor should be cast to the class. + ///
- All grenades and throwables (not including SCP-018 and SCP-2176) should be cast to the class. + ///
+ /// + ///
The following have their own respective classes: + ///
- Radios can be cast to . + ///
- The Micro HID can be cast to . + ///
- SCP-244 A and B variants can be cast to . + ///
- SCP-330 can be cast to . + ///
- SCP-018 can be cast to . + ///
- SCP-2176 can be cast to . + ///
+ /// + /// Items that are not listed above do not have a subclass, and can only use the base class. + /// + ///
+ /// The of the pickup. + /// The specified type. + /// The created . + /// + public static Pickup Create(ItemType type) + where T : Pickup => Create(type) as T; + /// /// Creates and spawns a . /// @@ -507,6 +537,19 @@ public static IEnumerable Get(IEnumerable gameObjects) /// public static Pickup CreateAndSpawn(ItemType type, Vector3 position, Quaternion rotation, Player previousOwner = null) => Create(type).Spawn(position, rotation, previousOwner); + /// + /// Creates and spawns a . + /// + /// The of the pickup. + /// The position to spawn the at. + /// The rotation to spawn the . + /// An optional previous owner of the item. + /// The specified type. + /// The . See documentation of for more information on casting. + /// + public static Pickup CreateAndSpawn(ItemType type, Vector3 position, Quaternion rotation, Player previousOwner = null) + where T : Pickup => CreateAndSpawn(type, position, rotation, previousOwner) as T; + /// /// Spawns a . /// diff --git a/EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs b/EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs index cd18059bc..5406d6414 100644 --- a/EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs +++ b/EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs @@ -83,17 +83,37 @@ internal Projectile(ItemType type) /// Projectile that are not listed will cause an Exception. /// ///
- /// The of the pickup. - /// The created . + /// The of the projectile. + /// The created . public static Projectile Create(ProjectileType projectiletype) => projectiletype switch { ProjectileType.Scp018 => new Scp018Projectile(), ProjectileType.Flashbang => new FlashbangProjectile(), ProjectileType.Scp2176 => new Scp2176Projectile(), ProjectileType.FragGrenade => new ExplosionGrenadeProjectile(ItemType.GrenadeHE), - _ => throw new System.Exception($"ProjectileType does not contain a valid value : {projectiletype}"), + _ => throw new Exception($"ProjectileType does not contain a valid value : {projectiletype}"), }; + /// + /// Creates and returns a new with the proper inherited subclass. + /// + /// Based on the , the returned can be casted into a subclass to gain more control over the object. + ///
The following have their own respective classes: + ///
- FragGrenade can be casted to . + ///
- Flashbang can be casted to . + ///
- Scp018 A and B variants can be casted to . + ///
- Scp2176 can be casted to . + ///
+ /// + /// Projectile that are not listed will cause an Exception. + /// + ///
+ /// The of the projectile. + /// The specified type. + /// The created . + public static Projectile Create(ProjectileType projectiletype) + where T : Projectile => Create(projectiletype) as T; + /// /// Spawns a . /// @@ -110,14 +130,27 @@ public static Projectile Spawn(Projectile pickup, Vector3 position, Quaternion r /// /// Creates and spawns a . /// - /// The of the pickup. + /// The of the projectile. /// The position to spawn the at. /// The rotation to spawn the . /// Whether the should be in active state after spawn. /// An optional previous owner of the item. - /// The . See documentation of for more information on casting. + /// The . See documentation of for more information on casting. public static Projectile CreateAndSpawn(ProjectileType type, Vector3 position, Quaternion rotation, bool shouldBeActive = true, Player previousOwner = null) => Create(type).Spawn(position, rotation, shouldBeActive, previousOwner); + /// + /// Creates and spawns a . + /// + /// The of the projectile. + /// The position to spawn the at. + /// The rotation to spawn the . + /// Whether the should be in active state after spawn. + /// An optional previous owner of the item. + /// The specified type. + /// The . See documentation of for more information on casting. + public static Projectile CreateAndSpawn(ProjectileType type, Vector3 position, Quaternion rotation, bool shouldBeActive = true, Player previousOwner = null) + where T : Projectile => CreateAndSpawn(type, position, rotation, shouldBeActive, previousOwner) as T; + /// /// Activates the current . /// From 4a2cbaf83fa5dcdc4de31548102e70a55e382c1c Mon Sep 17 00:00:00 2001 From: Yamato Date: Fri, 2 Aug 2024 01:53:05 +0200 Subject: [PATCH 08/12] WeirdFix --- EXILED/Exiled.CustomItems/Patches/PlayerInventorySee.cs | 3 ++- .../Patches/Events/Item/ChangingAttachments.cs | 3 ++- EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs | 3 ++- EXILED/Exiled.Events/Patches/Events/Player/DroppingItem.cs | 3 ++- .../Patches/Events/Player/FirearmRequestReceived.cs | 3 ++- EXILED/Exiled.Events/Patches/Fixes/GrenadePropertiesFix.cs | 3 ++- EXILED/Exiled.Events/Patches/Generic/PickupControlPatch.cs | 7 ++++--- 7 files changed, 16 insertions(+), 9 deletions(-) diff --git a/EXILED/Exiled.CustomItems/Patches/PlayerInventorySee.cs b/EXILED/Exiled.CustomItems/Patches/PlayerInventorySee.cs index 8f35712fb..1620477ba 100644 --- a/EXILED/Exiled.CustomItems/Patches/PlayerInventorySee.cs +++ b/EXILED/Exiled.CustomItems/Patches/PlayerInventorySee.cs @@ -8,6 +8,7 @@ namespace Exiled.CustomItems.Patches { using System.Collections.Generic; + using System.Linq; using System.Reflection; using System.Reflection.Emit; @@ -52,7 +53,7 @@ private static IEnumerable Transpiler(IEnumerable !x.IsGenericMethod && x.Name is nameof(Item.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(ItemBase))), new(OpCodes.Dup), new(OpCodes.Stloc_S, item.LocalIndex), new(OpCodes.Brfalse_S, continueLabel), diff --git a/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs b/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs index 3e49274a0..0cd597092 100644 --- a/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs +++ b/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs @@ -8,6 +8,7 @@ namespace Exiled.Events.Patches.Events.Item { using System.Collections.Generic; + using System.Linq; using System.Reflection.Emit; using API.Features; @@ -69,7 +70,7 @@ private static IEnumerable Transpiler(IEnumerable !x.IsGenericMethod && x.Name is nameof(Item.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(InventorySystem.Items.ItemBase))), new(OpCodes.Castclass, typeof(Firearm)), // AttachmentsChangeRequest::AttachmentsCode diff --git a/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs b/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs index cc77eea7b..b440b1749 100644 --- a/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs +++ b/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs @@ -8,6 +8,7 @@ namespace Exiled.Events.Patches.Events.Map { using System.Collections.Generic; + using System.Linq; using System.Reflection.Emit; using Exiled.API.Features.Pickups; @@ -61,7 +62,7 @@ private static IEnumerable Transpiler(IEnumerable), "Item")), new(OpCodes.Ldloc_S, pickup.LocalIndex), - new(OpCodes.Call, Method(typeof(Pickup), nameof(Pickup.Get), new[] { typeof(ItemPickupBase) })), + new(OpCodes.Call, GetDeclaredMethods(typeof(Pickup)).First(x => !x.IsGenericMethod && x.Name is nameof(Pickup.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(ItemPickupBase))), new(OpCodes.Newobj, GetDeclaredConstructors(typeof(Scp244SpawningEventArgs))[0]), new(OpCodes.Dup), diff --git a/EXILED/Exiled.Events/Patches/Events/Player/DroppingItem.cs b/EXILED/Exiled.Events/Patches/Events/Player/DroppingItem.cs index 0e0311532..119f7d1df 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/DroppingItem.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/DroppingItem.cs @@ -8,6 +8,7 @@ namespace Exiled.Events.Patches.Events.Player { using System.Collections.Generic; + using System.Linq; using System.Reflection.Emit; using API.Features.Pools; @@ -119,7 +120,7 @@ private static IEnumerable Transpiler(IEnumerable !x.IsGenericMethod && x.Name is nameof(Item.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(InventorySystem.Items.ItemBase))), // ev.IsThrown new(OpCodes.Ldloc_S, ev.LocalIndex), diff --git a/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs b/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs index 6ebd931f4..ff0dceace 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs @@ -8,6 +8,7 @@ namespace Exiled.Events.Patches.Events.Player { using System.Collections.Generic; + using System.Linq; using System.Reflection; using System.Reflection.Emit; @@ -69,7 +70,7 @@ private static IEnumerable Transpiler(IEnumerable !x.IsGenericMethod && x.Name is nameof(API.Features.Items.Item.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(ItemBase))), new(OpCodes.Isinst, typeof(Firearm)), new(OpCodes.Dup), new(OpCodes.Stloc_S, firearm.LocalIndex), diff --git a/EXILED/Exiled.Events/Patches/Fixes/GrenadePropertiesFix.cs b/EXILED/Exiled.Events/Patches/Fixes/GrenadePropertiesFix.cs index 8c37c4f9a..e37fb8993 100644 --- a/EXILED/Exiled.Events/Patches/Fixes/GrenadePropertiesFix.cs +++ b/EXILED/Exiled.Events/Patches/Fixes/GrenadePropertiesFix.cs @@ -8,6 +8,7 @@ namespace Exiled.Events.Patches.Fixes { using System.Collections.Generic; + using System.Linq; using System.Reflection.Emit; using API.Features.Items; @@ -58,7 +59,7 @@ private static IEnumerable Transpiler(IEnumerable !x.IsGenericMethod && x.Name is nameof(Item.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(ItemBase))), new(OpCodes.Isinst, typeof(Throwable)), new(OpCodes.Dup), new(OpCodes.Stloc_S, throwable.LocalIndex), diff --git a/EXILED/Exiled.Events/Patches/Generic/PickupControlPatch.cs b/EXILED/Exiled.Events/Patches/Generic/PickupControlPatch.cs index c76a8f247..fc319c474 100644 --- a/EXILED/Exiled.Events/Patches/Generic/PickupControlPatch.cs +++ b/EXILED/Exiled.Events/Patches/Generic/PickupControlPatch.cs @@ -9,6 +9,7 @@ namespace Exiled.Events.Patches.Generic { using System; using System.Collections.Generic; + using System.Linq; using System.Reflection.Emit; using API.Features.Pickups; @@ -48,11 +49,11 @@ private static IEnumerable Transpiler( { // pickup = Pickup.Get(pickupBase); new(OpCodes.Ldloc_0), - new(OpCodes.Call, Method(typeof(Pickup), nameof(Pickup.Get), new[] { typeof(ItemPickupBase) })), + new(OpCodes.Call, GetDeclaredMethods(typeof(Pickup)).First(x => !x.IsGenericMethod && x.Name is nameof(Pickup.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(ItemPickupBase))), // Item.Get(itemBase); new(OpCodes.Ldarg_0), - new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(ItemBase) })), + new(OpCodes.Call, GetDeclaredMethods(typeof(Item)).First(x => !x.IsGenericMethod && x.Name is nameof(Item.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(ItemBase))), // pickup.ReadItemInfo(item); new(OpCodes.Callvirt, Method(typeof(Pickup), nameof(Pickup.ReadItemInfo))), @@ -79,7 +80,7 @@ private static IEnumerable Transpiler(IEnumerable !x.IsGenericMethod && x.Name is nameof(Pickup.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(ItemPickupBase))), new(OpCodes.Pop), }); From 7ab1c4bff7f27be1b2bd7263c37d5119c2fce09b Mon Sep 17 00:00:00 2001 From: Yamato Date: Fri, 2 Aug 2024 11:05:55 +0200 Subject: [PATCH 09/12] simplify Scp244Spawning patch and AddedComment & NO IL error --- .../EventArgs/Map/Scp244SpawningEventArgs.cs | 13 +++--- .../Patches/Events/Map/Scp244Spawning.cs | 41 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/EXILED/Exiled.Events/EventArgs/Map/Scp244SpawningEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Map/Scp244SpawningEventArgs.cs index 1ed101269..fd6a6fa76 100644 --- a/EXILED/Exiled.Events/EventArgs/Map/Scp244SpawningEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Map/Scp244SpawningEventArgs.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- // // Copyright (c) Exiled Team. All rights reserved. // Licensed under the CC BY-SA 3.0 license. @@ -10,6 +10,8 @@ namespace Exiled.Events.EventArgs.Map using API.Features; using Exiled.API.Features.Pickups; using Interfaces; + using InventorySystem.Items.Usables.Scp244; + using MapGeneration; /// /// Contains all information up to spawning Scp244. @@ -25,11 +27,10 @@ public class Scp244SpawningEventArgs : IRoomEvent, IPickupEvent, IDeniableEvent /// /// /// - public Scp244SpawningEventArgs(Room room, Pickup scp244Pickup) + public Scp244SpawningEventArgs(RoomIdentifier room, Scp244DeployablePickup scp244Pickup) { - Room = room; - Pickup = scp244Pickup; - Scp244Pickup = scp244Pickup.As(); + Room = Room.Get(room); + Scp244Pickup = Pickup.Get(scp244Pickup); } /// @@ -38,7 +39,7 @@ public Scp244SpawningEventArgs(Room room, Pickup scp244Pickup) public Room Room { get; } /// - public Pickup Pickup { get; } + public Pickup Pickup => Scp244Pickup; /// /// Gets a value indicating the pickup being spawning. diff --git a/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs b/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs index b440b1749..aa487a514 100644 --- a/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs +++ b/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs @@ -36,50 +36,49 @@ private static IEnumerable Transpiler(IEnumerable newInstructions = ListPool.Pool.Get(instructions); - Label continueLabel = generator.DefineLabel(); - LocalBuilder pickup = generator.DeclareLocal(typeof(ItemPickupBase)); - int index = newInstructions.FindLastIndex(i => i.opcode == OpCodes.Dup); - - newInstructions.RemoveRange(index, 1); - - int offset = 1; - index = newInstructions.FindIndex(i => i.opcode == OpCodes.Dup) + offset; - newInstructions.InsertRange(index, new CodeInstruction[] - { - new(OpCodes.Dup), - new(OpCodes.Stloc_S, pickup.LocalIndex), - }); + Label continueLabel = generator.DefineLabel(); - offset = -2; - index = newInstructions.FindIndex(i => i.Calls(Method(typeof(NetworkServer), nameof(NetworkServer.Spawn), new[] { typeof(GameObject), typeof(NetworkConnection) }))) + offset; + int offset = -2; + int index = newInstructions.FindIndex(i => i.Calls(Method(typeof(NetworkServer), nameof(NetworkServer.Spawn), new[] { typeof(GameObject), typeof(NetworkConnection) }))) + offset; newInstructions.InsertRange(index, new[] { - new CodeInstruction(OpCodes.Ldsfld, Field(typeof(Scp244Spawner), nameof(Scp244Spawner.CompatibleRooms))).MoveLabelsFrom(newInstructions[index]), + // save Pickup from the stack + new CodeInstruction(OpCodes.Stloc_S, pickup.LocalIndex).MoveLabelsFrom(newInstructions[index]), + + // Scp244Spawner.CompatibleRooms[num] + new(OpCodes.Ldsfld, Field(typeof(Scp244Spawner), nameof(Scp244Spawner.CompatibleRooms))), new(OpCodes.Ldloc_0), new(OpCodes.Callvirt, PropertyGetter(typeof(List), "Item")), - new(OpCodes.Ldloc_S, pickup.LocalIndex), - new(OpCodes.Call, GetDeclaredMethods(typeof(Pickup)).First(x => !x.IsGenericMethod && x.Name is nameof(Pickup.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(ItemPickupBase))), + // scp244DeployablePickup + new(OpCodes.Ldloc_2), + // Scp244SpawningEventArgs ev = new(Room, Scp244DeployablePickup) new(OpCodes.Newobj, GetDeclaredConstructors(typeof(Scp244SpawningEventArgs))[0]), new(OpCodes.Dup), new(OpCodes.Call, Method(typeof(Handlers.Map), nameof(Handlers.Map.OnScp244Spawning))), + // if (ev.IsAllowed) goto continueLabel; new(OpCodes.Call, PropertyGetter(typeof(Scp244SpawningEventArgs), nameof(Scp244SpawningEventArgs.IsAllowed))), new(OpCodes.Brtrue_S, continueLabel), - new(OpCodes.Ldloc_S, pickup.LocalIndex), + // scp244DeployablePickup.gameObject.Destroy() + // return; + new(OpCodes.Ldloc_2), new(OpCodes.Callvirt, PropertyGetter(typeof(ItemPickupBase), nameof(ItemPickupBase.gameObject))), new(OpCodes.Call, Method(typeof(NetworkServer), nameof(NetworkServer.Destroy))), new(OpCodes.Ret), - new CodeInstruction(OpCodes.Nop).WithLabels(continueLabel), - new(OpCodes.Ldloc_S, pickup.LocalIndex), + // load pickup back into the stack + new CodeInstruction(OpCodes.Ldloc_S, pickup.LocalIndex).WithLabels(continueLabel), }); + for (int z = 0; z < newInstructions.Count; z++) + API.Features.Log.Info($"[{z}] {newInstructions[z].opcode} {newInstructions[z].operand} : {newInstructions[z].labels.Count}"); + for (int z = 0; z < newInstructions.Count; z++) yield return newInstructions[z]; From 2f123998cde70f683fcedfb7af07d476b6905e37 Mon Sep 17 00:00:00 2001 From: Yamato Date: Fri, 2 Aug 2024 11:08:02 +0200 Subject: [PATCH 10/12] Remove Log.info --- EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs b/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs index aa487a514..10b7b0eb4 100644 --- a/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs +++ b/EXILED/Exiled.Events/Patches/Events/Map/Scp244Spawning.cs @@ -76,9 +76,6 @@ private static IEnumerable Transpiler(IEnumerable Date: Fri, 2 Aug 2024 11:14:50 +0200 Subject: [PATCH 11/12] DroppingItem light modifiication --- .../EventArgs/Player/DroppedItemEventArgs.cs | 5 +++-- .../Exiled.Events/Patches/Events/Player/DroppingItem.cs | 8 ++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/EXILED/Exiled.Events/EventArgs/Player/DroppedItemEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/DroppedItemEventArgs.cs index 14d298c84..0109a4bba 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/DroppedItemEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/DroppedItemEventArgs.cs @@ -10,6 +10,7 @@ namespace Exiled.Events.EventArgs.Player using API.Features; using Exiled.API.Features.Pickups; using Interfaces; + using InventorySystem.Items.Pickups; /// /// Contains all information after a player drops an item. @@ -28,10 +29,10 @@ public class DroppedItemEventArgs : IPlayerEvent, IPickupEvent /// /// /// - public DroppedItemEventArgs(Player player, Pickup pickup, bool wasThrown) + public DroppedItemEventArgs(Player player, ItemPickupBase pickup, bool wasThrown) { Player = player; - Pickup = pickup; + Pickup = Pickup.Get(pickup); WasThrown = wasThrown; } diff --git a/EXILED/Exiled.Events/Patches/Events/Player/DroppingItem.cs b/EXILED/Exiled.Events/Patches/Events/Player/DroppingItem.cs index 119f7d1df..26d8cb2cc 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/DroppingItem.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/DroppingItem.cs @@ -8,18 +8,15 @@ namespace Exiled.Events.Patches.Events.Player { using System.Collections.Generic; - using System.Linq; using System.Reflection.Emit; using API.Features.Pools; - using Exiled.API.Features.Pickups; using Exiled.Events.Attributes; using Exiled.Events.EventArgs.Player; using HarmonyLib; using InventorySystem; - using InventorySystem.Items.Pickups; using static HarmonyLib.AccessTools; @@ -118,15 +115,14 @@ private static IEnumerable Transpiler(IEnumerable !x.IsGenericMethod && x.Name is nameof(Item.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(InventorySystem.Items.ItemBase))), // ev.IsThrown new(OpCodes.Ldloc_S, ev.LocalIndex), new(OpCodes.Callvirt, PropertyGetter(typeof(DroppingItemEventArgs), nameof(DroppingItemEventArgs.IsThrown))), - // Player::OnDroppedItem(new DroppedItemEventArgs(ev.Player, Player::Get(ItemPickupBase), ev.IsThrown)) + // Player::OnDroppedItem(new DroppedItemEventArgs(ev.Player, ItemPickupBase, ev.IsThrown)) new(OpCodes.Newobj, GetDeclaredConstructors(typeof(DroppedItemEventArgs))[0]), new(OpCodes.Call, Method(typeof(Player), nameof(Player.OnDroppedItem))), }); From 7d62ef0b9e3b8e312d7f5965faa0c98f3f18d5ec Mon Sep 17 00:00:00 2001 From: Yamato Date: Fri, 2 Aug 2024 11:20:04 +0200 Subject: [PATCH 12/12] Moving Item.Get inside the eventargs instead of transpiller --- .../EventArgs/Item/ChangingAttachmentsEventArgs.cs | 14 +++++--------- .../Patches/Events/Item/ChangingAttachments.cs | 6 ++---- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/EXILED/Exiled.Events/EventArgs/Item/ChangingAttachmentsEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Item/ChangingAttachmentsEventArgs.cs index 0e1c8fbed..fad3ec0b5 100644 --- a/EXILED/Exiled.Events/EventArgs/Item/ChangingAttachmentsEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Item/ChangingAttachmentsEventArgs.cs @@ -38,17 +38,13 @@ public class ChangingAttachmentsEventArgs : IPlayerEvent, IDeniableEvent, IFirea /// /// /// - public ChangingAttachmentsEventArgs( - Player player, - Firearm firearm, - uint code, - bool isAllowed = true) + public ChangingAttachmentsEventArgs(Player player, InventorySystem.Items.Firearms.Firearm firearm, uint code, bool isAllowed = true) { Player = player; - Firearm = firearm; - CurrentAttachmentIdentifiers = firearm.AttachmentIdentifiers; - NewAttachmentIdentifiers = firearm.FirearmType.GetAttachmentIdentifiers(code).ToList(); - CurrentCode = firearm.Base.GetCurrentAttachmentsCode(); + Firearm = Item.Get(firearm); + CurrentAttachmentIdentifiers = Firearm.AttachmentIdentifiers; + NewAttachmentIdentifiers = Firearm.FirearmType.GetAttachmentIdentifiers(code).ToList(); + CurrentCode = firearm.GetCurrentAttachmentsCode(); NewCode = code; IsAllowed = isAllowed; } diff --git a/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs b/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs index 0cd597092..17394e444 100644 --- a/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs +++ b/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs @@ -68,10 +68,8 @@ private static IEnumerable Transpiler(IEnumerable !x.IsGenericMethod && x.Name is nameof(Item.Get) && x.GetParameters().Length is 1 && x.GetParameters()[0].ParameterType == typeof(InventorySystem.Items.ItemBase))), - new(OpCodes.Castclass, typeof(Firearm)), // AttachmentsChangeRequest::AttachmentsCode new(OpCodes.Ldarg_1), @@ -80,7 +78,7 @@ private static IEnumerable Transpiler(IEnumerable