Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using CounterStrikeSharp.API.Modules.Entities;

namespace CounterStrikeSharp.API.Modules.Extensions;

public static class CBasePlayerWeaponExtensions
{
/// <summary>
/// Gets the weapon's designer name (e.g., "weapon_ak47").
/// </summary>
/// <param name="weapon">The <see cref="CBasePlayerWeapon"/> instance.</param>
/// <returns>The designer name of the weapon as a string, or <c>null</c> if it cannot be retrieved.</returns>
public static string? GetWeaponName(this CBasePlayerWeapon weapon)
{
return weapon.GetVData<CCSWeaponBaseVData>()?.Name;
}

/// <summary>
/// Gets the owner of a weapon entity
/// </summary>
/// <param name="weapon">The weapon entity.</param>
/// <returns>The <see cref="CCSPlayerController"/> instance for the player, or <c>null</c> if it doesn't exist.</returns>
public static CCSPlayerController? GetOwner(this CBasePlayerWeapon weapon)
{
SteamID? steamId = null;

if (weapon.OriginalOwnerXuidLow > 0)
steamId = new(weapon.OriginalOwnerXuidLow);

CCSPlayerController? player;

if (steamId != null && steamId.IsValid())
{
player = Utilities.GetPlayerFromSteamId(steamId.SteamId64);

if (player == null)
player = Utilities.GetPlayerFromSteamId(weapon.OriginalOwnerXuidLow);
}
else
{
player = Utilities.GetPlayerFromIndex((int)weapon.OwnerEntity.Index);

if (player == null)
player = Utilities.GetPlayerFromIndex((int)weapon.As<CCSWeaponBaseGun>().OwnerEntity.Value!.Index);
}

return !string.IsNullOrEmpty(player?.PlayerName) ? player : null;
}
}
3 changes: 2 additions & 1 deletion managed/CounterStrikeSharp.API/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ public static void PrintToChatAll(string message)

public static string GameDirectory => NativeAPI.GetGameDirectory();

public static int MaxPlayers => NativeAPI.GetMaxClients();
private static readonly int _maxPlayers = NativeAPI.GetMaxClients();
public static int MaxPlayers => _maxPlayers;

public static bool IsMapValid(string mapName) => NativeAPI.IsMapValid(mapName);

Expand Down
72 changes: 39 additions & 33 deletions managed/CounterStrikeSharp.API/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using CounterStrikeSharp.API.Core.Logging;
using CounterStrikeSharp.API.Modules.Commands.Targeting;
using CounterStrikeSharp.API.Modules.Entities;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -60,13 +57,19 @@

public static CCSPlayerController? GetPlayerFromIndex(int index)
{
var player = GetEntityFromIndex<CCSPlayerController>(index);
if (player == null || player.DesignerName != "cs_player_controller")
var entityPtr = EntitySystem.GetEntityByIndex((uint)index);
if (entityPtr is null || entityPtr == IntPtr.Zero)
{
return null;
}

var controller = new CCSPlayerController(entityPtr.Value);
if (!controller.IsValid || controller.Connected != PlayerConnectedState.PlayerConnected)
{
return null;
}

return player;
return controller;
}

public static CCSPlayerController? GetPlayerFromSlot(int slot)
Expand All @@ -81,47 +84,50 @@

public static CCSPlayerController? GetPlayerFromSteamId(ulong steamId)
{
return Utilities.GetPlayers().FirstOrDefault(player => player.AuthorizedSteamID == (SteamID)steamId);
return GetPlayers().FirstOrDefault(player => player.AuthorizedSteamID == (SteamID)steamId);
}

public static TargetResult ProcessTargetString(string pattern, CCSPlayerController player)
public static CCSPlayerController? GetPlayerFromSteamId64(ulong steamId)
{
return new Target(pattern).GetTarget(player);
return GetPlayers().FirstOrDefault(player => player.SteamID == steamId);
}

public static bool RemoveItemByDesignerName(this CCSPlayerController player, string designerName)
public static TargetResult ProcessTargetString(string pattern, CCSPlayerController player)
{
return RemoveItemByDesignerName(player, designerName, false);
return new Target(pattern).GetTarget(player);
}

public static bool RemoveItemByDesignerName(this CCSPlayerController player, string designerName, bool shouldRemoveEntity)
public static bool RemoveItemByDesignerName(this CCSPlayerController player, string designerName)
{
CHandle<CBasePlayerWeapon>? item = null;
if (player.PlayerPawn.Value == null || player.PlayerPawn.Value.WeaponServices == null) return false;
var weapon = player.PlayerPawn.Value?.WeaponServices?.MyWeapons
.Select(w => w.Value)
.Where(w => w?.IsValid is true && w.DesignerName == designerName)
.FirstOrDefault();

foreach (var weapon in player.PlayerPawn.Value.WeaponServices.MyWeapons)
{
if (weapon is not { IsValid: true, Value.IsValid: true })
continue;
if (weapon.Value.DesignerName != designerName)
continue;
if (weapon == null)
return false;

item = weapon;
}
weapon.AddEntityIOEvent("Kill", weapon, delay: 0.1f);
return true;
}

if (item != null && item.Value != null)
{
player.PlayerPawn.Value.RemovePlayerItem(item.Value);
public static bool RemoveItemByDesignerName(this CCSPlayerController player, string designerName, bool _)
{
return RemoveItemByDesignerName(player, designerName);
}

if (shouldRemoveEntity)
{
item.Value.Remove();
}
public static bool RemoveItemBySlot(this CCSPlayerController player, gear_slot_t slot)
{
var weapon = player.PlayerPawn.Value?.WeaponServices?.MyWeapons
.Select(w => w.Value)
.Where(w => w?.As<CCSWeaponBase>().VData?.GearSlot == slot)
.FirstOrDefault();

return true;
}
if (weapon == null)
return false;

return false;
weapon.AddEntityIOEvent("Kill", weapon, delay: 0.1f);
return true;
}

public static IEnumerable<T> FindAllEntitiesByDesignerName<T>(string designerName) where T : CEntityInstance
Expand Down Expand Up @@ -154,7 +160,7 @@
{
var controller = GetPlayerFromSlot(i);

if (controller == null || !controller.IsValid || controller.Connected != PlayerConnectedState.PlayerConnected)
if (controller == null)
continue;

players.Add(controller);
Expand Down Expand Up @@ -185,7 +191,7 @@

if (nativeUtf8 == IntPtr.Zero)
{
return null;

Check warning on line 194 in managed/CounterStrikeSharp.API/Utilities.cs

View workflow job for this annotation

GitHub Actions / build_managed

Possible null reference return.
}

var len = 0;
Expand Down
Loading