Skip to content

Commit

Permalink
feat: clean up windows specific logic (roflmuffin/CounterStrikeSharp#377
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ianlucas committed Jul 24, 2024
1 parent 2cf5d48 commit 8424c01
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 53 deletions.
16 changes: 0 additions & 16 deletions source/InventorySimulator/InventorySimulator.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 0 additions & 23 deletions source/InventorySimulator/InventorySimulator.Give.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
}
}
11 changes: 5 additions & 6 deletions source/InventorySimulator/InventorySimulator.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ public partial class InventorySimulator
public readonly FakeConVar<string> invsim_hostname = new("invsim_hostname", "Inventory Simulator API's hostname.", "inventory.cstrike.app");
public readonly FakeConVar<string> invsim_protocol = new("invsim_protocol", "Inventory Simulator API's protocol.", "https");

public readonly HashSet<ulong> FetchingPlayerInventory = new();
public readonly HashSet<ulong> LoadedPlayerInventory = new();
public readonly HashSet<ulong> FetchingPlayerInventory = [];
public readonly HashSet<ulong> LoadedPlayerInventory = [];

public readonly Dictionary<ulong, long> PlayerCooldownManager = new();
public readonly Dictionary<ulong, (CCSPlayerController?, PlayerInventory)> PlayerOnTickInventoryManager = new();
public readonly Dictionary<ulong, PlayerInventory> PlayerInventoryManager = new();
public readonly Dictionary<ulong, long> PlayerCooldownManager = [];
public readonly Dictionary<ulong, (CCSPlayerController?, PlayerInventory)> PlayerOnTickInventoryManager = [];
public readonly Dictionary<ulong, PlayerInventory> 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;

Expand Down
9 changes: 1 addition & 8 deletions source/InventorySimulator/InventorySimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Listeners.OnTick>(OnTick);
RegisterListener<Listeners.OnEntityCreated>(OnEntityCreated);
RegisterEventHandler<EventPlayerConnect>(OnPlayerConnect);
RegisterEventHandler<EventPlayerConnectFull>(OnPlayerConnectFull);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
RegisterEventHandler<EventItemPickup>(OnItemPickup);
VirtualFunctions.GiveNamedItemFunc.Hook(OnGiveNamedItemPost, HookMode.Post);
RegisterEventHandler<EventPlayerDeath>(OnPlayerDeathPre, HookMode.Pre);
RegisterEventHandler<EventRoundMvp>(OnRoundMvpPre, HookMode.Pre);
RegisterEventHandler<EventPlayerDisconnect>(OnPlayerDisconnect);
Expand Down

0 comments on commit 8424c01

Please sign in to comment.