From 8424c010cc3d2931e3bf069c073a87a0f112bf4b Mon Sep 17 00:00:00 2001 From: Ian Lucas Date: Wed, 24 Jul 2024 13:25:02 -0300 Subject: [PATCH] feat: clean up windows specific logic (roflmuffin/CounterStrikeSharp#377) --- .../InventorySimulator.Events.cs | 16 ------------- .../InventorySimulator.Give.cs | 23 ------------------- .../InventorySimulator.State.cs | 11 ++++----- .../InventorySimulator/InventorySimulator.cs | 9 +------- 4 files changed, 6 insertions(+), 53 deletions(-) diff --git a/source/InventorySimulator/InventorySimulator.Events.cs b/source/InventorySimulator/InventorySimulator.Events.cs index aec89fb..512a825 100644 --- a/source/InventorySimulator/InventorySimulator.Events.cs +++ b/source/InventorySimulator/InventorySimulator.Events.cs @@ -48,22 +48,6 @@ public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo _) return HookResult.Continue; } - public HookResult OnItemPickup(EventItemPickup @event, GameEventInfo _) - { - if (IsWindows) - { - var player = @event.Userid; - if (player != null && - IsPlayerHumanAndValid(player) && - IsPlayerPawnValid(player)) - { - GiveOnItemPickup(player); - } - } - - return HookResult.Continue; - } - public HookResult OnPlayerDeathPre(EventPlayerDeath @event, GameEventInfo _) { var attacker = @event.Attacker; diff --git a/source/InventorySimulator/InventorySimulator.Give.cs b/source/InventorySimulator/InventorySimulator.Give.cs index 8cd32cf..b1ddf13 100644 --- a/source/InventorySimulator/InventorySimulator.Give.cs +++ b/source/InventorySimulator/InventorySimulator.Give.cs @@ -244,27 +244,4 @@ public void GiveOnRefreshPlayerInventory(CCSPlayerController player) var inventory = GetPlayerInventory(player); GivePlayerPin(player, inventory); } - - // Nuke this when roflmuffin/CounterStrikeSharp#377 is resolved. This workaround makes sure the - // subclass of a knife will be changed as soon as the player receives it. Only needed on Windows - // because on Linux we hook GiveNamedItem that happens a bit early than this event. - public void GiveOnItemPickup(CCSPlayerController player) - { - var pawn = player.PlayerPawn.Value; - if (pawn != null) - { - var myWeapons = pawn.WeaponServices?.MyWeapons; - if (myWeapons != null) - { - foreach (var handle in myWeapons) - { - var weapon = handle.Value; - if (weapon != null && IsKnifeClassName(weapon.DesignerName)) - { - GivePlayerWeaponSkin(player, weapon); - } - } - } - } - } } diff --git a/source/InventorySimulator/InventorySimulator.State.cs b/source/InventorySimulator/InventorySimulator.State.cs index fd202e7..07f492d 100644 --- a/source/InventorySimulator/InventorySimulator.State.cs +++ b/source/InventorySimulator/InventorySimulator.State.cs @@ -21,16 +21,15 @@ public partial class InventorySimulator public readonly FakeConVar invsim_hostname = new("invsim_hostname", "Inventory Simulator API's hostname.", "inventory.cstrike.app"); public readonly FakeConVar invsim_protocol = new("invsim_protocol", "Inventory Simulator API's protocol.", "https"); - public readonly HashSet FetchingPlayerInventory = new(); - public readonly HashSet LoadedPlayerInventory = new(); + public readonly HashSet FetchingPlayerInventory = []; + public readonly HashSet LoadedPlayerInventory = []; - public readonly Dictionary PlayerCooldownManager = new(); - public readonly Dictionary PlayerOnTickInventoryManager = new(); - public readonly Dictionary PlayerInventoryManager = new(); + public readonly Dictionary PlayerCooldownManager = []; + public readonly Dictionary PlayerOnTickInventoryManager = []; + public readonly Dictionary PlayerInventoryManager = []; public readonly PlayerInventory EmptyInventory = new(); - public static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); public static readonly string InventoryFilePath = "csgo/addons/counterstrikesharp/configs/plugins/InventorySimulator/inventories.json"; public static readonly ulong MinimumCustomItemID = 68719476736; diff --git a/source/InventorySimulator/InventorySimulator.cs b/source/InventorySimulator/InventorySimulator.cs index 65deb91..8a120d3 100644 --- a/source/InventorySimulator/InventorySimulator.cs +++ b/source/InventorySimulator/InventorySimulator.cs @@ -21,19 +21,12 @@ public override void Load(bool hotReload) { LoadPlayerInventories(); - if (!IsWindows) - { - // GiveNamedItemFunc hooking is not working on Windows due an issue with CounterStrikeSharp's - // DynamicHooks. See: https://github.com/roflmuffin/CounterStrikeSharp/issues/377 - VirtualFunctions.GiveNamedItemFunc.Hook(OnGiveNamedItemPost, HookMode.Post); - } - RegisterListener(OnTick); RegisterListener(OnEntityCreated); RegisterEventHandler(OnPlayerConnect); RegisterEventHandler(OnPlayerConnectFull); RegisterEventHandler(OnPlayerSpawn); - RegisterEventHandler(OnItemPickup); + VirtualFunctions.GiveNamedItemFunc.Hook(OnGiveNamedItemPost, HookMode.Post); RegisterEventHandler(OnPlayerDeathPre, HookMode.Pre); RegisterEventHandler(OnRoundMvpPre, HookMode.Pre); RegisterEventHandler(OnPlayerDisconnect);