Compare commits

...

11 Commits

Author SHA1 Message Date
Dawid Bepierszcz
36046fee2d Merge pull request #231 from daffyyyy/main
2.4a
2024-04-03 01:35:07 +02:00
Dawid Bepierszcz
d6de0ce6c8 Merge branch 'Nereziel:main' into main 2024-04-03 01:33:52 +02:00
Dawid Bepierszcz
d2c19d8af8 Merge branch 'main' of https://github.com/daffyyyy/cs2-WeaponPaints 2024-04-03 01:32:27 +02:00
Dawid Bepierszcz
0f6d334621 2.4a
- .NET8
- Mysql queries optimization
- CounterStrikeSharp updated
2024-04-03 01:32:24 +02:00
Dawid Bepierszcz
f99c9b2767 Merge pull request #226 from daffyyyy/main
2.3c
2024-03-27 23:20:43 +01:00
Dawid Bepierszcz
6a2d28c303 Merge branch 'Nereziel:main' into main 2024-03-27 23:19:42 +01:00
Dawid Bepierszcz
27a2ae5be9 Merge branch 'main' of https://github.com/daffyyyy/cs2-WeaponPaints 2024-03-27 23:19:26 +01:00
Dawid Bepierszcz
0af9177f07 2.3c
- Small change for mysql performance
2024-03-27 23:19:24 +01:00
Dawid Bepierszcz
cfb49e1498 Merge pull request #222 from daffyyyy/main
2.3b
2024-03-21 01:13:14 +01:00
Dawid Bepierszcz
bbdb4b82ce Merge branch 'Nereziel:main' into main 2024-03-21 01:12:08 +01:00
Dawid Bepierszcz
bfb7defcaa 2.3b
- Fixed agent team bug
- Minor changes
2024-03-21 01:09:45 +01:00
9 changed files with 194 additions and 184 deletions

View File

@@ -33,6 +33,8 @@ namespace WeaponPaints
if (weaponSync != null) if (weaponSync != null)
{ {
_ = Task.Run(async () => await weaponSync.GetPlayerData(playerInfo));
/*
if (Config.Additional.SkinEnabled) if (Config.Additional.SkinEnabled)
{ {
_ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo)); _ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo));
@@ -53,6 +55,7 @@ namespace WeaponPaints
{ {
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo)); _ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
} }
*/
RefreshGloves(player); RefreshGloves(player);
RefreshWeapons(player); RefreshWeapons(player);
@@ -471,60 +474,62 @@ namespace WeaponPaints
if (!Utility.IsPlayerValid(player) || player is null) return; if (!Utility.IsPlayerValid(player) || player is null) return;
string selectedPaintName = option.Text; string selectedPaintName = option.Text;
var selectedAgent = agentsList.FirstOrDefault(g =>
g.ContainsKey("agent_name") &&
g["agent_name"] != null && g["agent_name"]!.ToString() == selectedPaintName &&
g["team"] != null && (int)(g["team"]!) == player.TeamNum);
var selectedAgent = agentsList.FirstOrDefault(g => g.ContainsKey("agent_name") && g["agent_name"]?.ToString() == selectedPaintName); if (selectedAgent == null) return;
if (selectedAgent != null)
if (
selectedAgent != null &&
selectedAgent.ContainsKey("model")
)
{ {
if ( PlayerInfo playerInfo = new PlayerInfo
selectedAgent != null &&
selectedAgent.ContainsKey("model")
)
{ {
PlayerInfo playerInfo = new PlayerInfo UserId = player.UserId,
{ Slot = player.Slot,
UserId = player.UserId, Index = (int)player.Index,
Slot = player.Slot, SteamId = player.SteamID.ToString(),
Index = (int)player.Index, Name = player.PlayerName,
SteamId = player.SteamID.ToString(), IpAddress = player.IpAddress?.Split(":")[0]
Name = player.PlayerName,
IpAddress = player.IpAddress?.Split(":")[0]
};
if (Config.Additional.ShowSkinImage)
{
string image = selectedAgent["image"]?.ToString() ?? "";
PlayerWeaponImage[player.Slot] = image;
AddTimer(2.0f, () => PlayerWeaponImage.Remove(player.Slot), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
}
if (!string.IsNullOrEmpty(Localizer["wp_agent_menu_select"]))
{
player!.Print(Localizer["wp_agent_menu_select", selectedPaintName]);
}
if (player.TeamNum == 3)
{
g_playersAgent.AddOrUpdate(player.Slot,
key => (selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString(), null),
(key, oldValue) => (selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString(), oldValue.T));
}
else
{
g_playersAgent.AddOrUpdate(player.Slot,
key => (null, selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString()),
(key, oldValue) => (oldValue.CT, selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString())
);
}
if (weaponSync != null)
{
_ = Task.Run(async () =>
{
await weaponSync.SyncAgentToDatabase(playerInfo);
});
}
}; };
}
if (Config.Additional.ShowSkinImage)
{
string image = selectedAgent["image"]?.ToString() ?? "";
PlayerWeaponImage[player.Slot] = image;
AddTimer(2.0f, () => PlayerWeaponImage.Remove(player.Slot), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
}
if (!string.IsNullOrEmpty(Localizer["wp_agent_menu_select"]))
{
player!.Print(Localizer["wp_agent_menu_select", selectedPaintName]);
}
if (player.TeamNum == 3)
{
g_playersAgent.AddOrUpdate(player.Slot,
key => (selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString(), null),
(key, oldValue) => (selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString(), oldValue.T));
}
else
{
g_playersAgent.AddOrUpdate(player.Slot,
key => (null, selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString()),
(key, oldValue) => (oldValue.CT, selectedAgent["model"]!.ToString().Equals("null") ? null : selectedAgent["model"]!.ToString())
);
}
if (weaponSync != null)
{
_ = Task.Run(async () =>
{
await weaponSync.SyncAgentToDatabase(playerInfo);
});
}
};
}; };
// Command to open the weapon selection menu for players // Command to open the weapon selection menu for players

View File

@@ -27,6 +27,8 @@ namespace WeaponPaints
try try
{ {
_ = Task.Run(async () => await weaponSync.GetPlayerData(playerInfo));
/*
if (Config.Additional.SkinEnabled) if (Config.Additional.SkinEnabled)
{ {
_ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo)); _ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo));
@@ -47,6 +49,7 @@ namespace WeaponPaints
{ {
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo)); _ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
} }
*/
} }
catch (Exception) catch (Exception)
{ {
@@ -102,7 +105,6 @@ namespace WeaponPaints
private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon weapon) private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon weapon)
{ {
if (!Config.Additional.SkinEnabled) return; if (!Config.Additional.SkinEnabled) return;
if (player is null || weapon is null || !weapon.IsValid || !Utility.IsPlayerValid(player)) return;
if (!gPlayerWeaponsInfo.ContainsKey(player.Slot)) return; if (!gPlayerWeaponsInfo.ContainsKey(player.Slot)) return;
bool isKnife = weapon.DesignerName.Contains("knife") || weapon.DesignerName.Contains("bayonet"); bool isKnife = weapon.DesignerName.Contains("knife") || weapon.DesignerName.Contains("bayonet");
@@ -160,9 +162,9 @@ namespace WeaponPaints
return; return;
} }
if (!gPlayerWeaponsInfo[player.Slot].ContainsKey(weaponDefIndex) || gPlayerWeaponsInfo[player.Slot][weaponDefIndex].Paint == 0) return; if (!gPlayerWeaponsInfo[player.Slot].TryGetValue(weaponDefIndex, out WeaponInfo? value) || value.Paint == 0) return;
WeaponInfo weaponInfo = gPlayerWeaponsInfo[player.Slot][weaponDefIndex]; WeaponInfo weaponInfo = value;
//Log($"Apply on {weapon.DesignerName}({weapon.AttributeManager.Item.ItemDefinitionIndex}) paint {gPlayerWeaponPaints[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} seed {gPlayerWeaponSeed[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} wear {gPlayerWeaponWear[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]}"); //Log($"Apply on {weapon.DesignerName}({weapon.AttributeManager.Item.ItemDefinitionIndex}) paint {gPlayerWeaponPaints[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} seed {gPlayerWeaponSeed[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} wear {gPlayerWeaponWear[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]}");
weapon.AttributeManager.Item.ItemID = 16384; weapon.AttributeManager.Item.ItemID = 16384;
weapon.AttributeManager.Item.ItemIDLow = 16384 & 0xFFFFFFFF; weapon.AttributeManager.Item.ItemIDLow = 16384 & 0xFFFFFFFF;
@@ -214,6 +216,7 @@ namespace WeaponPaints
GivePlayerMusicKit(player); GivePlayerMusicKit(player);
GivePlayerAgent(player); GivePlayerAgent(player);
Server.NextFrame(() => Server.NextFrame(() =>
{ {
RefreshGloves(player); RefreshGloves(player);
@@ -257,7 +260,7 @@ namespace WeaponPaints
} }
*/ */
public void OnEntityCreated(CEntityInstance entity) public void OnEntitySpawned(CEntityInstance entity)
{ {
var designerName = entity.DesignerName; var designerName = entity.DesignerName;
@@ -266,7 +269,7 @@ namespace WeaponPaints
Server.NextFrame(() => Server.NextFrame(() =>
{ {
var weapon = new CBasePlayerWeapon(entity.Handle); var weapon = new CBasePlayerWeapon(entity.Handle);
if (weapon == null || !weapon.IsValid || weapon.OwnerEntity.Value == null) return; if (weapon == null || !weapon.IsValid) return;
try try
{ {
@@ -305,6 +308,8 @@ namespace WeaponPaints
private void OnTick() private void OnTick()
{ {
if (!Config.Additional.ShowSkinImage) return;
foreach (var player in Utilities.GetPlayers().Where(p => foreach (var player in Utilities.GetPlayers().Where(p =>
p is not null && p.IsValid && p.PlayerPawn != null && p.PlayerPawn.IsValid && p is not null && p.IsValid && p.PlayerPawn != null && p.PlayerPawn.IsValid &&
(LifeState_t)p.LifeState == LifeState_t.LIFE_ALIVE && p.SteamID.ToString().Length == 17 (LifeState_t)p.LifeState == LifeState_t.LIFE_ALIVE && p.SteamID.ToString().Length == 17
@@ -312,7 +317,7 @@ namespace WeaponPaints
) )
) )
{ {
if (Config.Additional.ShowSkinImage && PlayerWeaponImage.TryGetValue(player.Slot, out string? value) && !string.IsNullOrEmpty(value)) if (PlayerWeaponImage.TryGetValue(player.Slot, out string? value) && !string.IsNullOrEmpty(value))
{ {
player.PrintToCenterHtml("<img src='{PATH}'</img>".Replace("{PATH}", value)); player.PrintToCenterHtml("<img src='{PATH}'</img>".Replace("{PATH}", value));
} }
@@ -324,10 +329,13 @@ namespace WeaponPaints
RegisterListener<Listeners.OnMapStart>(OnMapStart); RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn); RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
RegisterEventHandler<EventRoundStart>(OnRoundStart, HookMode.Pre); RegisterEventHandler<EventRoundStart>(OnRoundStart);
RegisterEventHandler<EventRoundEnd>(OnRoundEnd); RegisterEventHandler<EventRoundEnd>(OnRoundEnd);
RegisterListener<Listeners.OnEntityCreated>(OnEntityCreated); RegisterListener<Listeners.OnEntitySpawned>(OnEntitySpawned);
RegisterListener<Listeners.OnTick>(OnTick);
if (Config.Additional.ShowSkinImage)
RegisterListener<Listeners.OnTick>(OnTick);
//VirtualFunctions.GiveNamedItemFunc.Hook(OnGiveNamedItemPost, HookMode.Post); //VirtualFunctions.GiveNamedItemFunc.Hook(OnGiveNamedItemPost, HookMode.Post);
} }
} }

View File

@@ -8,6 +8,7 @@ public static class PlayerExtensions
public static void Print(this CCSPlayerController controller, string message) public static void Print(this CCSPlayerController controller, string message)
{ {
if (WeaponPaints._localizer == null) return; if (WeaponPaints._localizer == null) return;
StringBuilder _message = new(WeaponPaints._localizer["wp_prefix"]); StringBuilder _message = new(WeaponPaints._localizer["wp_prefix"]);
_message.Append(message); _message.Append(message);
controller.PrintToChat(_message.ToString()); controller.PrintToChat(_message.ToString());

View File

@@ -1,11 +1,9 @@
using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils; using CounterStrikeSharp.API.Core.Translations;
using Dapper; using Dapper;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MySqlConnector;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.Reflection;
namespace WeaponPaints namespace WeaponPaints
{ {
@@ -13,22 +11,6 @@ namespace WeaponPaints
{ {
internal static WeaponPaintsConfig? Config { get; set; } internal static WeaponPaintsConfig? Config { get; set; }
internal static string BuildDatabaseConnectionString()
{
if (Config == null) return string.Empty;
var builder = new MySqlConnectionStringBuilder
{
Server = Config.DatabaseHost,
UserID = Config.DatabaseUser,
Password = Config.DatabasePassword,
Database = Config.DatabaseName,
Port = (uint)Config.DatabasePort,
Pooling = true
};
return builder.ConnectionString;
}
internal static async Task CheckDatabaseTables() internal static async Task CheckDatabaseTables()
{ {
if (WeaponPaints._database is null) return; if (WeaponPaints._database is null) return;
@@ -41,8 +23,8 @@ namespace WeaponPaints
try try
{ {
string[] createTableQueries = new[] string[] createTableQueries =
{ [
@"CREATE TABLE IF NOT EXISTS `wp_player_skins` ( @"CREATE TABLE IF NOT EXISTS `wp_player_skins` (
`steamid` varchar(18) NOT NULL, `steamid` varchar(18) NOT NULL,
`weapon_defindex` int(6) NOT NULL, `weapon_defindex` int(6) NOT NULL,
@@ -50,28 +32,28 @@ namespace WeaponPaints
`weapon_wear` float NOT NULL DEFAULT 0.000001, `weapon_wear` float NOT NULL DEFAULT 0.000001,
`weapon_seed` int(16) NOT NULL DEFAULT 0 `weapon_seed` int(16) NOT NULL DEFAULT 0
) ENGINE=InnoDB", ) ENGINE=InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_knife` ( @"CREATE TABLE IF NOT EXISTS `wp_player_knife` (
`steamid` varchar(18) NOT NULL, `steamid` varchar(18) NOT NULL,
`knife` varchar(64) NOT NULL, `knife` varchar(64) NOT NULL,
UNIQUE (`steamid`) UNIQUE (`steamid`)
) ENGINE = InnoDB", ) ENGINE = InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_gloves` ( @"CREATE TABLE IF NOT EXISTS `wp_player_gloves` (
`steamid` varchar(18) NOT NULL, `steamid` varchar(18) NOT NULL,
`weapon_defindex` int(11) NOT NULL, `weapon_defindex` int(11) NOT NULL,
UNIQUE (`steamid`) UNIQUE (`steamid`)
) ENGINE=InnoDB", ) ENGINE=InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_agents` ( @"CREATE TABLE IF NOT EXISTS `wp_player_agents` (
`steamid` varchar(18) NOT NULL, `steamid` varchar(18) NOT NULL,
`agent_ct` varchar(64) DEFAULT NULL, `agent_ct` varchar(64) DEFAULT NULL,
`agent_t` varchar(64) DEFAULT NULL, `agent_t` varchar(64) DEFAULT NULL,
UNIQUE (`steamid`) UNIQUE (`steamid`)
) ENGINE=InnoDB", ) ENGINE=InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_music` ( @"CREATE TABLE IF NOT EXISTS `wp_player_music` (
`steamid` varchar(64) NOT NULL, `steamid` varchar(64) NOT NULL,
`music_id` int(11) NOT NULL, `music_id` int(11) NOT NULL,
UNIQUE (`steamid`) UNIQUE (`steamid`)
) ENGINE=InnoDB", ) ENGINE=InnoDB",
}; ];
foreach (var query in createTableQueries) foreach (var query in createTableQueries)
{ {
@@ -99,7 +81,7 @@ namespace WeaponPaints
return (player.IsValid && !player.IsBot && !player.IsHLTV && player.UserId.HasValue); return (player.IsValid && !player.IsBot && !player.IsHLTV && player.UserId.HasValue);
} }
internal static void LoadSkinsFromFile(string filePath) internal static void LoadSkinsFromFile(string filePath, ILogger logger)
{ {
try try
{ {
@@ -109,11 +91,11 @@ namespace WeaponPaints
} }
catch (FileNotFoundException) catch (FileNotFoundException)
{ {
throw; logger?.LogError("Not found \"skins.json\" file");
} }
} }
internal static void LoadGlovesFromFile(string filePath) internal static void LoadGlovesFromFile(string filePath, ILogger logger)
{ {
try try
{ {
@@ -123,11 +105,11 @@ namespace WeaponPaints
} }
catch (FileNotFoundException) catch (FileNotFoundException)
{ {
throw; logger?.LogError("Not found \"gloves.json\" file");
} }
} }
internal static void LoadAgentsFromFile(string filePath) internal static void LoadAgentsFromFile(string filePath, ILogger logger)
{ {
try try
{ {
@@ -137,11 +119,11 @@ namespace WeaponPaints
} }
catch (FileNotFoundException) catch (FileNotFoundException)
{ {
throw; logger?.LogError("Not found \"agents.json\" file");
} }
} }
internal static void LoadMusicFromFile(string filePath) internal static void LoadMusicFromFile(string filePath, ILogger logger)
{ {
try try
{ {
@@ -151,7 +133,7 @@ namespace WeaponPaints
} }
catch (FileNotFoundException) catch (FileNotFoundException)
{ {
throw; logger?.LogError("Not found \"music.json\" file");
} }
} }
@@ -165,69 +147,50 @@ namespace WeaponPaints
internal static string ReplaceTags(string message) internal static string ReplaceTags(string message)
{ {
if (message.Contains('{')) return message.ReplaceColorTags();
{
string modifiedValue = message;
if (Config != null)
{
modifiedValue = modifiedValue.Replace("{WEBSITE}", Config.Website);
}
foreach (FieldInfo field in typeof(ChatColors).GetFields())
{
string pattern = $"{{{field.Name}}}";
if (message.Contains(pattern, StringComparison.OrdinalIgnoreCase))
{
modifiedValue = modifiedValue.Replace(pattern, field.GetValue(null)!.ToString(), StringComparison.OrdinalIgnoreCase);
}
}
return modifiedValue;
}
return message;
} }
internal static async Task CheckVersion(string version, ILogger logger) internal static async Task CheckVersion(string version, ILogger logger)
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
try
{ {
try HttpResponseMessage response = await client.GetAsync("https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/VERSION").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{ {
HttpResponseMessage response = await client.GetAsync("https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/VERSION").ConfigureAwait(false); string remoteVersion = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
remoteVersion = remoteVersion.Trim();
if (response.IsSuccessStatusCode) int comparisonResult = string.Compare(version, remoteVersion);
if (comparisonResult < 0)
{ {
string remoteVersion = await response.Content.ReadAsStringAsync().ConfigureAwait(false); logger.LogWarning("Plugin is outdated! Check https://github.com/Nereziel/cs2-WeaponPaints");
remoteVersion = remoteVersion.Trim(); }
else if (comparisonResult > 0)
int comparisonResult = string.Compare(version, remoteVersion); {
logger.LogInformation("Probably dev version detected");
if (comparisonResult < 0)
{
logger.LogWarning("Plugin is outdated! Check https://github.com/Nereziel/cs2-WeaponPaints");
}
else if (comparisonResult > 0)
{
logger.LogInformation("Probably dev version detected");
}
else
{
logger.LogInformation("Plugin is up to date");
}
} }
else else
{ {
logger.LogWarning("Failed to check version"); logger.LogInformation("Plugin is up to date");
} }
} }
catch (HttpRequestException ex) else
{ {
logger.LogError(ex, "Failed to connect to the version server."); logger.LogWarning("Failed to check version");
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while checking version.");
} }
} }
catch (HttpRequestException ex)
{
logger.LogError(ex, "Failed to connect to the version server.");
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while checking version.");
}
} }
internal static void ShowAd(string moduleVersion) internal static void ShowAd(string moduleVersion)

View File

@@ -1 +1 @@
2.3a 2.4a

View File

@@ -17,7 +17,7 @@ namespace WeaponPaints
if (PlayerHasKnife(player)) return; if (PlayerHasKnife(player)) return;
string knifeToGive = (CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife"; //string knifeToGive = (CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife";
player.GiveNamedItem(CsItem.Knife); player.GiveNamedItem(CsItem.Knife);
} }
@@ -99,12 +99,13 @@ namespace WeaponPaints
int clip1 = weapon.Value.Clip1; int clip1 = weapon.Value.Clip1;
int reservedAmmo = weapon.Value.ReserveAmmo[0]; int reservedAmmo = weapon.Value.ReserveAmmo[0];
if (!weaponsWithAmmo.ContainsKey(weaponByDefindex)) if (!weaponsWithAmmo.TryGetValue(weaponByDefindex, out List<(int, int)>? value))
{ {
weaponsWithAmmo.Add(weaponByDefindex, new List<(int, int)>()); value = new List<(int, int)>();
weaponsWithAmmo.Add(weaponByDefindex, value);
} }
weaponsWithAmmo[weaponByDefindex].Add((clip1, reservedAmmo)); value.Add((clip1, reservedAmmo));
if (gun == null || gun.VData == null) return; if (gun == null || gun.VData == null) return;

View File

@@ -10,7 +10,7 @@ using System.Collections.Concurrent;
namespace WeaponPaints; namespace WeaponPaints;
[MinimumApiVersion(191)] [MinimumApiVersion(201)]
public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig> public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{ {
internal static WeaponPaints Instance { get; private set; } = new(); internal static WeaponPaints Instance { get; private set; } = new();
@@ -75,23 +75,23 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
{ "weapon_knife_kukri", "Kukri Knife" } { "weapon_knife_kukri", "Kukri Knife" }
}; };
internal static WeaponPaintsConfig _config = new WeaponPaintsConfig(); internal static WeaponPaintsConfig _config = new();
internal static IStringLocalizer? _localizer; internal static IStringLocalizer? _localizer;
internal static Dictionary<int, int> g_knifePickupCount = new Dictionary<int, int>(); internal static Dictionary<int, int> g_knifePickupCount = new();
internal static ConcurrentDictionary<int, string> g_playersKnife = new ConcurrentDictionary<int, string>(); internal static ConcurrentDictionary<int, string> g_playersKnife = new();
internal static ConcurrentDictionary<int, ushort> g_playersGlove = new ConcurrentDictionary<int, ushort>(); internal static ConcurrentDictionary<int, ushort> g_playersGlove = new();
internal static ConcurrentDictionary<int, ushort> g_playersMusic = new ConcurrentDictionary<int, ushort>(); internal static ConcurrentDictionary<int, ushort> g_playersMusic = new();
internal static ConcurrentDictionary<int, (string? CT, string? T)> g_playersAgent = new ConcurrentDictionary<int, (string?, string?)>(); internal static ConcurrentDictionary<int, (string? CT, string? T)> g_playersAgent = new();
internal static ConcurrentDictionary<int, ConcurrentDictionary<int, WeaponInfo>> gPlayerWeaponsInfo = new ConcurrentDictionary<int, ConcurrentDictionary<int, WeaponInfo>>(); internal static ConcurrentDictionary<int, ConcurrentDictionary<int, WeaponInfo>> gPlayerWeaponsInfo = new();
internal static List<JObject> skinsList = new List<JObject>(); internal static List<JObject> skinsList = new();
internal static List<JObject> glovesList = new List<JObject>(); internal static List<JObject> glovesList = new();
internal static List<JObject> agentsList = new List<JObject>(); internal static List<JObject> agentsList = new();
internal static List<JObject> musicList = new List<JObject>(); internal static List<JObject> musicList = new();
internal static WeaponSynchronization? weaponSync; internal static WeaponSynchronization? weaponSync;
public static bool g_bCommandsAllowed = true; public static bool g_bCommandsAllowed = true;
internal Dictionary<int, string> PlayerWeaponImage = new(); internal Dictionary<int, string> PlayerWeaponImage = new();
internal static Dictionary<int, DateTime> commandsCooldown = new Dictionary<int, DateTime>(); internal static Dictionary<int, DateTime> commandsCooldown = new();
internal static Database? _database; internal static Database? _database;
internal static MemoryFunctionVoid<nint, string, float> CAttributeList_SetOrAddAttributeValueByName = new(GameData.GetSignature("CAttributeList_SetOrAddAttributeValueByName")); internal static MemoryFunctionVoid<nint, string, float> CAttributeList_SetOrAddAttributeValueByName = new(GameData.GetSignature("CAttributeList_SetOrAddAttributeValueByName"));
@@ -160,7 +160,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
public override string ModuleAuthor => "Nereziel & daffyy"; public override string ModuleAuthor => "Nereziel & daffyy";
public override string ModuleDescription => "Skin, gloves, agents and knife selector, standalone and web-based"; public override string ModuleDescription => "Skin, gloves, agents and knife selector, standalone and web-based";
public override string ModuleName => "WeaponPaints"; public override string ModuleName => "WeaponPaints";
public override string ModuleVersion => "2.3a"; public override string ModuleVersion => "2.4a";
public static WeaponPaintsConfig GetWeaponPaintsConfig() public static WeaponPaintsConfig GetWeaponPaintsConfig()
{ {
@@ -177,7 +177,10 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
foreach (var player in Utilities.GetPlayers()) foreach (var player in Utilities.GetPlayers())
{ {
if (weaponSync == null || player is null || !player.IsValid || player.SteamID.ToString().Length != 17 || player.IsBot || if (weaponSync == null)
break;
if (player is null || !player.IsValid || player.SteamID.ToString().Length != 17 || string.IsNullOrEmpty(player.IpAddress) || player.IsBot ||
player.IsHLTV || player.Connected != PlayerConnectedState.PlayerConnected) player.IsHLTV || player.Connected != PlayerConnectedState.PlayerConnected)
continue; continue;
@@ -197,6 +200,9 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
IpAddress = player?.IpAddress?.Split(":")[0] IpAddress = player?.IpAddress?.Split(":")[0]
}; };
_ = Task.Run(async () => await weaponSync.GetPlayerData(playerInfo));
/*
if (Config.Additional.SkinEnabled) if (Config.Additional.SkinEnabled)
{ {
_ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo)); _ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo));
@@ -217,13 +223,14 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
{ {
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo)); _ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
} }
*/
} }
} }
Utility.LoadSkinsFromFile(ModuleDirectory + "/skins.json"); Utility.LoadSkinsFromFile(ModuleDirectory + "/skins.json", Logger);
Utility.LoadGlovesFromFile(ModuleDirectory + "/gloves.json"); Utility.LoadGlovesFromFile(ModuleDirectory + "/gloves.json", Logger);
Utility.LoadAgentsFromFile(ModuleDirectory + "/agents.json"); Utility.LoadAgentsFromFile(ModuleDirectory + "/agents.json", Logger);
Utility.LoadMusicFromFile(ModuleDirectory + "/music.json"); Utility.LoadMusicFromFile(ModuleDirectory + "/music.json", Logger);
if (Config.Additional.KnifeEnabled) if (Config.Additional.KnifeEnabled)
SetupKnifeMenu(); SetupKnifeMenu();
@@ -255,7 +262,9 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
Password = config.DatabasePassword, Password = config.DatabasePassword,
Database = config.DatabaseName, Database = config.DatabaseName,
Port = (uint)config.DatabasePort, Port = (uint)config.DatabasePort,
Pooling = true Pooling = true,
MaximumPoolSize = 640,
ConnectionReset = false
}; };
_database = new(builder.ConnectionString); _database = new(builder.ConnectionString);

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
@@ -9,9 +9,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.193" /> <PackageReference Include="CounterStrikeSharp.API" Version="1.0.203" />
<PackageReference Include="Dapper" Version="2.1.35" /> <PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="MySqlConnector" Version="2.3.5" /> <PackageReference Include="MySqlConnector" Version="2.3.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>

View File

@@ -1,4 +1,5 @@
using Dapper; using Dapper;
using MySqlConnector;
using System.Collections.Concurrent; using System.Collections.Concurrent;
namespace WeaponPaints namespace WeaponPaints
@@ -14,16 +15,39 @@ namespace WeaponPaints
_config = config; _config = config;
} }
internal async Task GetKnifeFromDatabase(PlayerInfo player) internal async Task GetPlayerData(PlayerInfo player)
{
try
{
await using MySqlConnection connection = await _database.GetConnectionAsync();
if (_config.Additional.KnifeEnabled)
GetKnifeFromDatabase(player, connection);
if (_config.Additional.GloveEnabled)
GetGloveFromDatabase(player, connection);
if (_config.Additional.AgentEnabled)
GetAgentFromDatabase(player, connection);
if (_config.Additional.MusicEnabled)
GetMusicFromDatabase(player, connection);
if (_config.Additional.SkinEnabled)
GetWeaponPaintsFromDatabase(player, connection);
}
catch (Exception ex)
{
// Log the exception or handle it appropriately
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
internal void GetKnifeFromDatabase(PlayerInfo player, MySqlConnection connection)
{ {
try try
{ {
if (!_config.Additional.KnifeEnabled || string.IsNullOrEmpty(player?.SteamId)) if (!_config.Additional.KnifeEnabled || string.IsNullOrEmpty(player?.SteamId))
return; return;
await using var connection = await _database.GetConnectionAsync();
string query = "SELECT `knife` FROM `wp_player_knife` WHERE `steamid` = @steamid"; string query = "SELECT `knife` FROM `wp_player_knife` WHERE `steamid` = @steamid";
string? playerKnife = await connection.QueryFirstOrDefaultAsync<string>(query, new { steamid = player.SteamId }); string? playerKnife = connection.QueryFirstOrDefault<string>(query, new { steamid = player.SteamId });
if (!string.IsNullOrEmpty(playerKnife)) if (!string.IsNullOrEmpty(playerKnife))
{ {
@@ -36,16 +60,15 @@ namespace WeaponPaints
} }
} }
internal async Task GetGloveFromDatabase(PlayerInfo player) internal void GetGloveFromDatabase(PlayerInfo player, MySqlConnection connection)
{ {
try try
{ {
if (!_config.Additional.GloveEnabled || string.IsNullOrEmpty(player?.SteamId)) if (!_config.Additional.GloveEnabled || string.IsNullOrEmpty(player?.SteamId))
return; return;
await using var connection = await _database.GetConnectionAsync();
string query = "SELECT `weapon_defindex` FROM `wp_player_gloves` WHERE `steamid` = @steamid"; string query = "SELECT `weapon_defindex` FROM `wp_player_gloves` WHERE `steamid` = @steamid";
ushort? gloveData = await connection.QueryFirstOrDefaultAsync<ushort?>(query, new { steamid = player.SteamId }); ushort? gloveData = connection.QueryFirstOrDefault<ushort?>(query, new { steamid = player.SteamId });
if (gloveData != null) if (gloveData != null)
{ {
@@ -58,16 +81,15 @@ namespace WeaponPaints
} }
} }
internal async Task GetAgentFromDatabase(PlayerInfo player) internal void GetAgentFromDatabase(PlayerInfo player, MySqlConnection connection)
{ {
try try
{ {
if (!_config.Additional.AgentEnabled || string.IsNullOrEmpty(player?.SteamId)) if (!_config.Additional.AgentEnabled || string.IsNullOrEmpty(player?.SteamId))
return; return;
await using var connection = await _database.GetConnectionAsync();
string query = "SELECT `agent_ct`, `agent_t` FROM `wp_player_agents` WHERE `steamid` = @steamid"; string query = "SELECT `agent_ct`, `agent_t` FROM `wp_player_agents` WHERE `steamid` = @steamid";
var agentData = await connection.QueryFirstOrDefaultAsync<(string, string)>(query, new { steamid = player.SteamId }); var agentData = connection.QueryFirstOrDefault<(string, string)>(query, new { steamid = player.SteamId });
if (agentData != default) if (agentData != default)
{ {
@@ -85,11 +107,11 @@ namespace WeaponPaints
} }
catch (Exception ex) catch (Exception ex)
{ {
Utility.Log($"An error occurred in GetGloveFromDatabase: {ex.Message}"); Utility.Log($"An error occurred in GetAgentFromDatabase: {ex.Message}");
} }
} }
internal async Task GetWeaponPaintsFromDatabase(PlayerInfo player) internal void GetWeaponPaintsFromDatabase(PlayerInfo player, MySqlConnection connection)
{ {
try try
{ {
@@ -98,9 +120,8 @@ namespace WeaponPaints
var weaponInfos = new ConcurrentDictionary<int, WeaponInfo>(); var weaponInfos = new ConcurrentDictionary<int, WeaponInfo>();
await using var connection = await _database.GetConnectionAsync();
string query = "SELECT * FROM `wp_player_skins` WHERE `steamid` = @steamid"; string query = "SELECT * FROM `wp_player_skins` WHERE `steamid` = @steamid";
var playerSkins = await connection.QueryAsync<dynamic>(query, new { steamid = player.SteamId }); var playerSkins = connection.Query<dynamic>(query, new { steamid = player.SteamId });
if (playerSkins == null) if (playerSkins == null)
{ {
@@ -133,16 +154,15 @@ namespace WeaponPaints
} }
} }
internal async Task GetMusicFromDatabase(PlayerInfo player) internal void GetMusicFromDatabase(PlayerInfo player, MySqlConnection connection)
{ {
try try
{ {
if (!_config.Additional.MusicEnabled || string.IsNullOrEmpty(player.SteamId)) if (!_config.Additional.MusicEnabled || string.IsNullOrEmpty(player.SteamId))
return; return;
await using var connection = await _database.GetConnectionAsync();
string query = "SELECT `music_id` FROM `wp_player_music` WHERE `steamid` = @steamid"; string query = "SELECT `music_id` FROM `wp_player_music` WHERE `steamid` = @steamid";
ushort? musicData = await connection.QueryFirstOrDefaultAsync<ushort?>(query, new { steamid = player.SteamId }); ushort? musicData = connection.QueryFirstOrDefault<ushort?>(query, new { steamid = player.SteamId });
if (musicData != null) if (musicData != null)
{ {
@@ -155,6 +175,8 @@ namespace WeaponPaints
} }
} }
internal async Task SyncKnifeToDatabase(PlayerInfo player, string knife) internal async Task SyncKnifeToDatabase(PlayerInfo player, string knife)
{ {
if (!_config.Additional.KnifeEnabled || player == null || string.IsNullOrEmpty(player.SteamId) || string.IsNullOrEmpty(knife)) return; if (!_config.Additional.KnifeEnabled || player == null || string.IsNullOrEmpty(player.SteamId) || string.IsNullOrEmpty(knife)) return;
@@ -233,6 +255,7 @@ namespace WeaponPaints
string query; string query;
object parameters; object parameters;
if (existingRecordCount > 0) if (existingRecordCount > 0)
{ {
query = "UPDATE `wp_player_skins` SET `weapon_paint_id` = @paintId, `weapon_wear` = @wear, `weapon_seed` = @seed WHERE `steamid` = @steamid AND `weapon_defindex` = @weaponDefIndex"; query = "UPDATE `wp_player_skins` SET `weapon_paint_id` = @paintId, `weapon_wear` = @wear, `weapon_seed` = @seed WHERE `steamid` = @steamid AND `weapon_defindex` = @weaponDefIndex";