Skip to content

Commit

Permalink
Fix crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
NockyCZ committed Jun 7, 2024
1 parent 6151dd7 commit d3ff352
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 147 deletions.
7 changes: 4 additions & 3 deletions source/Deathmatch/API.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CounterStrikeSharp.API.Modules.Utils;
using DeathmatchAPI;
using DeathmatchAPI.Events;
using DeathmatchAPI.Helpers;
Expand Down Expand Up @@ -41,22 +42,22 @@ public void ChangeCheckDistance(int distance)
CheckedEnemiesDistance = distance;
}

public void SetupCustomSpawns(string team, Dictionary<string, string> spawns)
public void SetupCustomSpawns(string team, Dictionary<Vector, QAngle> spawns)
{
if (team.Equals("ct"))
{
spawnPositionsCT.Clear();
foreach (var spawn in spawns)
{
spawnPositionsCT.Add(ParseVector(spawn.Key), ParseQAngle(spawn.Value));
spawnPositionsCT.Add(spawn.Key, spawn.Value);
}
}
else if (team.Equals("t"))
{
spawnPositionsT.Clear();
foreach (var spawn in spawns)
{
spawnPositionsT.Add(ParseVector(spawn.Key), ParseQAngle(spawn.Value));
spawnPositionsT.Add(spawn.Key, spawn.Value);
}
}
else
Expand Down
10 changes: 6 additions & 4 deletions source/Deathmatch/Deathmatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class Deathmatch : BasePlugin, IPluginConfig<DeathmatchConfig>
{
public override string ModuleName => "Deathmatch Core";
public override string ModuleAuthor => "Nocky";
public override string ModuleVersion => "1.1.4";
public override string ModuleVersion => "1.1.5";

public void OnConfigParsed(DeathmatchConfig config)
{
Expand All @@ -31,16 +31,16 @@ public override void Load(bool hotReload)
{
var API = new Deathmatch();
Capabilities.RegisterPluginCapability(DeathmatchAPI, () => API);

IsLinuxServer = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

if (IsLinuxServer)
{
GetCSWeaponDataFromKeyFunc = new(GameData.GetSignature("GetCSWeaponDataFromKey"));
CCSPlayer_CanAcquireFunc = new(GameData.GetSignature("CCSPlayer_CanAcquire"));
CCSPlayer_CanAcquireFunc.Hook(OnWeaponCanAcquire, HookMode.Pre);
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnTakeDamage, HookMode.Pre);
}

VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnTakeDamage, HookMode.Pre);
LoadCustomModes();
LoadWeaponsRestrict();

Expand Down Expand Up @@ -171,10 +171,12 @@ public override void Load(bool hotReload)

public override void Unload(bool hotReload)
{
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(OnTakeDamage, HookMode.Pre);

if (IsLinuxServer)
{
CCSPlayer_CanAcquireFunc?.Unhook(OnWeaponCanAcquire, HookMode.Pre);
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(OnTakeDamage, HookMode.Pre);
}
//else
// VirtualFunctions.CCSPlayer_WeaponServices_CanUseFunc.Unhook(OnWeaponCanUse, HookMode.Pre);
}
Expand Down
2 changes: 1 addition & 1 deletion source/Deathmatch/Deathmatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.228" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.239" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<ProjectReference Include="..\DeathmatchAPI\DeathmatchAPI.csproj" />
</ItemGroup>
Expand Down
55 changes: 39 additions & 16 deletions source/Deathmatch/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Admin;
using System.Runtime.InteropServices;
using CounterStrikeSharp.API.Modules.Utils;

namespace Deathmatch
{
public partial class Deathmatch
{
[GameEventHandler]
[GameEventHandler(HookMode.Post)]
public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info)
{
var player = @event.Userid;
if (IsPlayerValid(player!) && !playerData.ContainsPlayer(player))
if (player != null && player.IsValid && !player.IsBot && !player.IsHLTV && player.SteamID.ToString().Length == 17 && !playerData.ContainsPlayer(player))
{
bool IsVIP = AdminManager.PlayerHasPermissions(player, Config.PlayersSettings.VIPFlag);
DeathmatchPlayerData setupPlayerData = new DeathmatchPlayerData
Expand All @@ -31,17 +32,17 @@ public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventIn
HudMessages = Config.PlayersPreferences.HudMessages.Enabled ? ((Config.PlayersPreferences.HudMessages.OnlyVIP && IsVIP ? Config.PlayersPreferences.HudMessages.DefaultValue : false) || (!Config.PlayersPreferences.HudMessages.OnlyVIP ? Config.PlayersPreferences.HudMessages.DefaultValue : false)) : false,
SpawnProtection = false,
OpenedMenu = 0,
LastSpawn = null!
LastSpawn = new Vector()
};
playerData[player!] = setupPlayerData;
playerData[player] = setupPlayerData;
}
return HookResult.Continue;
}
[GameEventHandler(HookMode.Pre)]
public HookResult OnPlayerConnectDisconnect(EventPlayerDisconnect @event, GameEventInfo info)
{
var player = @event.Userid;
if (playerData.ContainsPlayer(player))
if (player != null && player.IsValid && playerData.ContainsPlayer(player))
playerData.RemovePlayer(player);

return HookResult.Continue;
Expand All @@ -59,16 +60,17 @@ public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
[GameEventHandler(HookMode.Pre)]
public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
{
if (@event.Userid == null || !@event.Userid.IsValid)
return HookResult.Continue;

var attacker = @event.Attacker;
var player = @event.Userid;

if (player == null || !player.IsValid)
return HookResult.Continue;

if (ActiveMode != null && attacker != null && playerData.ContainsPlayer(attacker))
{
if (ActiveMode.OnlyHS)
{
if (@event.Hitgroup == 1 && (!@event.Weapon.Contains("knife") || !@event.Weapon.Contains("bayonet")) && playerData[attacker].HitSound)
if (@event.Hitgroup == 1 && playerData[attacker].HitSound && (!@event.Weapon.Contains("knife") || !@event.Weapon.Contains("bayonet")))
attacker.ExecuteClientCommand("play " + Config.PlayersPreferences.HitSound.Path);
}
else
Expand All @@ -84,9 +86,26 @@ public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
attacker.ExecuteClientCommand("play " + Config.PlayersPreferences.HitSound.Path);
}
}

if (!IsLinuxServer)
{
if (playerData.ContainsPlayer(player) && playerData[player].SpawnProtection)
{
player!.PlayerPawn.Value!.Health = player.PlayerPawn.Value.Health >= 100 ? 100 : player.PlayerPawn.Value.Health + @event.DmgHealth;
player.PlayerPawn.Value.ArmorValue = player.PlayerPawn.Value.ArmorValue >= 100 ? 100 : player.PlayerPawn.Value.ArmorValue + @event.DmgArmor;
return HookResult.Continue;
}
if (!ActiveMode.KnifeDamage && (@event.Weapon.Contains("knife") || @event.Weapon.Contains("bayonet")))
{
attacker.PrintToCenter(Localizer["Hud.KnifeDamageIsDisabled"]);
player!.PlayerPawn.Value!.Health = player.PlayerPawn.Value.Health >= 100 ? 100 : player.PlayerPawn.Value.Health + @event.DmgHealth;
player.PlayerPawn.Value.ArmorValue = player.PlayerPawn.Value.ArmorValue >= 100 ? 100 : player.PlayerPawn.Value.ArmorValue + @event.DmgArmor;
}
}
}
return HookResult.Continue;
}

[GameEventHandler(HookMode.Pre)]
public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
{
Expand Down Expand Up @@ -221,13 +240,14 @@ public HookResult OnItemPurcharsed(EventItemPurchase @event, GameEventInfo info)
player.PrintToChat($"{Localizer["Chat.Prefix"]} {Localizer["Chat.WeaponIsDisabled", replacedweaponName]}");
}
player.RemoveWeapons();
GivePlayerWeapons(player, false, false);
GivePlayerWeapons(player, false, false, true);
return HookResult.Continue;
}

string localizerWeaponName = Localizer[weaponName];
bool IsVIP = AdminManager.PlayerHasPermissions(player, Config.PlayersSettings.VIPFlag);
if (CheckIsWeaponRestricted(weaponName, IsVIP, player.Team))
bool IsPrimary = AllowedPrimaryWeaponsList.Contains(weaponName);
if (CheckIsWeaponRestricted(weaponName, IsVIP, player.Team, IsPrimary))
{
if (!string.IsNullOrEmpty(Config.SoundSettings.CantEquipSound))
player.ExecuteClientCommand("play " + Config.SoundSettings.CantEquipSound);
Expand All @@ -238,7 +258,6 @@ public HookResult OnItemPurcharsed(EventItemPurchase @event, GameEventInfo info)
return HookResult.Continue;
}

bool IsPrimary = AllowedPrimaryWeaponsList.Contains(weaponName);
if (IsPrimary)
{
if (weaponName == playerData[player].PrimaryWeapon)
Expand All @@ -249,7 +268,7 @@ public HookResult OnItemPurcharsed(EventItemPurchase @event, GameEventInfo info)
if (!Config.Gameplay.SwitchWeapons)
{
player.RemoveWeapons();
GivePlayerWeapons(player, false, false);
GivePlayerWeapons(player, false, false, true);
return HookResult.Continue;
}
playerData[player].PrimaryWeapon = weaponName;
Expand All @@ -265,7 +284,7 @@ public HookResult OnItemPurcharsed(EventItemPurchase @event, GameEventInfo info)
if (!Config.Gameplay.SwitchWeapons)
{
player.RemoveWeapons();
GivePlayerWeapons(player, false, false);
GivePlayerWeapons(player, false, false, true);
return HookResult.Continue;
}
playerData[player].SecondaryWeapon = weaponName;
Expand All @@ -290,6 +309,9 @@ private HookResult OnRandomWeapons(CCSPlayerController? player, CommandInfo info

private HookResult OnTakeDamage(DynamicHook hook)
{
//if (!IsLinuxServer)
// return HookResult.Continue;

var damageInfo = hook.GetParam<CTakeDamageInfo>(1);

var playerPawn = hook.GetParam<CCSPlayerPawn>(0);
Expand Down Expand Up @@ -380,7 +402,9 @@ private HookResult OnWeaponCanAcquire(DynamicHook hook)
{
string localizerWeaponName = Localizer[vdata.Name];
bool IsVIP = AdminManager.PlayerHasPermissions(player, Config.PlayersSettings.VIPFlag);
if (CheckIsWeaponRestricted(vdata.Name, IsVIP, player.Team))

bool IsPrimary = AllowedPrimaryWeaponsList.Contains(vdata.Name);
if (CheckIsWeaponRestricted(vdata.Name, IsVIP, player.Team, IsPrimary))
{
if (!string.IsNullOrEmpty(Config.SoundSettings.CantEquipSound))
player.ExecuteClientCommand("play " + Config.SoundSettings.CantEquipSound);
Expand All @@ -391,7 +415,6 @@ private HookResult OnWeaponCanAcquire(DynamicHook hook)
return HookResult.Stop;
}

bool IsPrimary = AllowedPrimaryWeaponsList.Contains(vdata.Name);
if (IsPrimary)
{
if (vdata.Name == playerData[player].PrimaryWeapon)
Expand Down
4 changes: 2 additions & 2 deletions source/Deathmatch/Functions/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void OnAddSpawnCT_CMD(CCSPlayerController player, CommandInfo info)
}
var position = player.PlayerPawn.Value!.AbsOrigin;
var angle = player.PlayerPawn.Value.AbsRotation;
AddNewSpawnPoint(ModuleDirectory + $"/spawns/{Server.MapName}.json", $"{position}", $"{angle}", "ct");
AddNewSpawnPoint(ModuleDirectory + $"/spawns/{Server.MapName}.json", position!, angle!, "ct");
info.ReplyToCommand($"{Localizer["Chat.Prefix"]} Spawn for the CT team has been added. (Total: {ChatColors.Green}{spawnPositionsCT.Count}{ChatColors.Default})");
}

Expand All @@ -203,7 +203,7 @@ public void OnAddSpawnT_CMD(CCSPlayerController player, CommandInfo info)
}
var position = player.PlayerPawn.Value!.AbsOrigin;
var angle = player.PlayerPawn.Value.AbsRotation;
AddNewSpawnPoint(ModuleDirectory + $"/spawns/{Server.MapName}.json", $"{position}", $"{angle}", "t");
AddNewSpawnPoint(ModuleDirectory + $"/spawns/{Server.MapName}.json", position!, angle!, "t");
info.ReplyToCommand($"{Localizer["Chat.Prefix"]} Spawn for the T team has been added. (Total: {ChatColors.Green}{spawnPositionsT.Count}{ChatColors.Default})");
}

Expand Down
Loading

0 comments on commit d3ff352

Please sign in to comment.