mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-02-17 18:39:07 +00:00
2.4a
- .NET8 - Mysql queries optimization - CounterStrikeSharp updated
This commit is contained in:
@@ -33,6 +33,8 @@ namespace WeaponPaints
|
||||
|
||||
if (weaponSync != null)
|
||||
{
|
||||
_ = Task.Run(async () => await weaponSync.GetPlayerData(playerInfo));
|
||||
/*
|
||||
if (Config.Additional.SkinEnabled)
|
||||
{
|
||||
_ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo));
|
||||
@@ -53,6 +55,7 @@ namespace WeaponPaints
|
||||
{
|
||||
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
|
||||
}
|
||||
*/
|
||||
|
||||
RefreshGloves(player);
|
||||
RefreshWeapons(player);
|
||||
|
||||
12
Events.cs
12
Events.cs
@@ -27,6 +27,8 @@ namespace WeaponPaints
|
||||
|
||||
try
|
||||
{
|
||||
_ = Task.Run(async () => await weaponSync.GetPlayerData(playerInfo));
|
||||
/*
|
||||
if (Config.Additional.SkinEnabled)
|
||||
{
|
||||
_ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo));
|
||||
@@ -47,6 +49,7 @@ namespace WeaponPaints
|
||||
{
|
||||
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
|
||||
}
|
||||
*/
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -102,7 +105,6 @@ namespace WeaponPaints
|
||||
private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon weapon)
|
||||
{
|
||||
if (!Config.Additional.SkinEnabled) return;
|
||||
if (player is null || weapon is null || !weapon.IsValid || !Utility.IsPlayerValid(player)) return;
|
||||
if (!gPlayerWeaponsInfo.ContainsKey(player.Slot)) return;
|
||||
|
||||
bool isKnife = weapon.DesignerName.Contains("knife") || weapon.DesignerName.Contains("bayonet");
|
||||
@@ -214,6 +216,7 @@ namespace WeaponPaints
|
||||
|
||||
GivePlayerMusicKit(player);
|
||||
GivePlayerAgent(player);
|
||||
|
||||
Server.NextFrame(() =>
|
||||
{
|
||||
RefreshGloves(player);
|
||||
@@ -266,7 +269,7 @@ namespace WeaponPaints
|
||||
Server.NextFrame(() =>
|
||||
{
|
||||
var weapon = new CBasePlayerWeapon(entity.Handle);
|
||||
if (weapon == null || !weapon.IsValid || weapon.OwnerEntity.Value == null) return;
|
||||
if (weapon == null || !weapon.IsValid) return;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -326,10 +329,13 @@ namespace WeaponPaints
|
||||
RegisterListener<Listeners.OnMapStart>(OnMapStart);
|
||||
|
||||
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
|
||||
RegisterEventHandler<EventRoundStart>(OnRoundStart, HookMode.Pre);
|
||||
RegisterEventHandler<EventRoundStart>(OnRoundStart);
|
||||
RegisterEventHandler<EventRoundEnd>(OnRoundEnd);
|
||||
RegisterListener<Listeners.OnEntitySpawned>(OnEntitySpawned);
|
||||
|
||||
if (Config.Additional.ShowSkinImage)
|
||||
RegisterListener<Listeners.OnTick>(OnTick);
|
||||
|
||||
//VirtualFunctions.GiveNamedItemFunc.Hook(OnGiveNamedItemPost, HookMode.Post);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ public static class PlayerExtensions
|
||||
public static void Print(this CCSPlayerController controller, string message)
|
||||
{
|
||||
if (WeaponPaints._localizer == null) return;
|
||||
|
||||
StringBuilder _message = new(WeaponPaints._localizer["wp_prefix"]);
|
||||
_message.Append(message);
|
||||
controller.PrintToChat(_message.ToString());
|
||||
|
||||
67
Utility.cs
67
Utility.cs
@@ -1,11 +1,9 @@
|
||||
using CounterStrikeSharp.API.Core;
|
||||
using CounterStrikeSharp.API.Modules.Utils;
|
||||
using CounterStrikeSharp.API.Core.Translations;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MySqlConnector;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace WeaponPaints
|
||||
{
|
||||
@@ -13,22 +11,6 @@ namespace WeaponPaints
|
||||
{
|
||||
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()
|
||||
{
|
||||
if (WeaponPaints._database is null) return;
|
||||
@@ -41,8 +23,8 @@ namespace WeaponPaints
|
||||
|
||||
try
|
||||
{
|
||||
string[] createTableQueries = new[]
|
||||
{
|
||||
string[] createTableQueries =
|
||||
[
|
||||
@"CREATE TABLE IF NOT EXISTS `wp_player_skins` (
|
||||
`steamid` varchar(18) NOT NULL,
|
||||
`weapon_defindex` int(6) NOT NULL,
|
||||
@@ -71,7 +53,7 @@ namespace WeaponPaints
|
||||
`music_id` int(11) NOT NULL,
|
||||
UNIQUE (`steamid`)
|
||||
) ENGINE=InnoDB",
|
||||
};
|
||||
];
|
||||
|
||||
foreach (var query in createTableQueries)
|
||||
{
|
||||
@@ -99,7 +81,7 @@ namespace WeaponPaints
|
||||
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
|
||||
{
|
||||
@@ -109,11 +91,11 @@ namespace WeaponPaints
|
||||
}
|
||||
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
|
||||
{
|
||||
@@ -123,11 +105,11 @@ namespace WeaponPaints
|
||||
}
|
||||
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
|
||||
{
|
||||
@@ -137,11 +119,11 @@ namespace WeaponPaints
|
||||
}
|
||||
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
|
||||
{
|
||||
@@ -151,7 +133,7 @@ namespace WeaponPaints
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
throw;
|
||||
logger?.LogError("Not found \"music.json\" file");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,31 +147,13 @@ namespace WeaponPaints
|
||||
|
||||
internal static string ReplaceTags(string message)
|
||||
{
|
||||
if (message.Contains('{'))
|
||||
{
|
||||
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;
|
||||
return message.ReplaceColorTags();
|
||||
}
|
||||
|
||||
internal static async Task CheckVersion(string version, ILogger logger)
|
||||
{
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
using HttpClient client = new();
|
||||
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await client.GetAsync("https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/VERSION").ConfigureAwait(false);
|
||||
@@ -228,7 +192,6 @@ namespace WeaponPaints
|
||||
logger.LogError(ex, "An error occurred while checking version.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static void ShowAd(string moduleVersion)
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace WeaponPaints
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ using System.Collections.Concurrent;
|
||||
|
||||
namespace WeaponPaints;
|
||||
|
||||
[MinimumApiVersion(195)]
|
||||
[MinimumApiVersion(201)]
|
||||
public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
|
||||
{
|
||||
internal static WeaponPaints Instance { get; private set; } = new();
|
||||
@@ -160,7 +160,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
|
||||
public override string ModuleAuthor => "Nereziel & daffyy";
|
||||
public override string ModuleDescription => "Skin, gloves, agents and knife selector, standalone and web-based";
|
||||
public override string ModuleName => "WeaponPaints";
|
||||
public override string ModuleVersion => "2.3c";
|
||||
public override string ModuleVersion => "2.4a";
|
||||
|
||||
public static WeaponPaintsConfig GetWeaponPaintsConfig()
|
||||
{
|
||||
@@ -200,6 +200,9 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
|
||||
IpAddress = player?.IpAddress?.Split(":")[0]
|
||||
};
|
||||
|
||||
_ = Task.Run(async () => await weaponSync.GetPlayerData(playerInfo));
|
||||
|
||||
/*
|
||||
if (Config.Additional.SkinEnabled)
|
||||
{
|
||||
_ = Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo));
|
||||
@@ -220,13 +223,14 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
|
||||
{
|
||||
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
Utility.LoadSkinsFromFile(ModuleDirectory + "/skins.json");
|
||||
Utility.LoadGlovesFromFile(ModuleDirectory + "/gloves.json");
|
||||
Utility.LoadAgentsFromFile(ModuleDirectory + "/agents.json");
|
||||
Utility.LoadMusicFromFile(ModuleDirectory + "/music.json");
|
||||
Utility.LoadSkinsFromFile(ModuleDirectory + "/skins.json", Logger);
|
||||
Utility.LoadGlovesFromFile(ModuleDirectory + "/gloves.json", Logger);
|
||||
Utility.LoadAgentsFromFile(ModuleDirectory + "/agents.json", Logger);
|
||||
Utility.LoadMusicFromFile(ModuleDirectory + "/music.json", Logger);
|
||||
|
||||
if (Config.Additional.KnifeEnabled)
|
||||
SetupKnifeMenu();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||
@@ -9,7 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<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="MySqlConnector" Version="2.3.6" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using MySqlConnector;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace WeaponPaints
|
||||
@@ -14,16 +15,39 @@ namespace WeaponPaints
|
||||
_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
|
||||
{
|
||||
if (!_config.Additional.KnifeEnabled || string.IsNullOrEmpty(player?.SteamId))
|
||||
return;
|
||||
|
||||
await using var connection = await _database.GetConnectionAsync();
|
||||
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))
|
||||
{
|
||||
@@ -36,16 +60,15 @@ namespace WeaponPaints
|
||||
}
|
||||
}
|
||||
|
||||
internal async Task GetGloveFromDatabase(PlayerInfo player)
|
||||
internal void GetGloveFromDatabase(PlayerInfo player, MySqlConnection connection)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_config.Additional.GloveEnabled || string.IsNullOrEmpty(player?.SteamId))
|
||||
return;
|
||||
|
||||
await using var connection = await _database.GetConnectionAsync();
|
||||
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)
|
||||
{
|
||||
@@ -58,16 +81,15 @@ namespace WeaponPaints
|
||||
}
|
||||
}
|
||||
|
||||
internal async Task GetAgentFromDatabase(PlayerInfo player)
|
||||
internal void GetAgentFromDatabase(PlayerInfo player, MySqlConnection connection)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_config.Additional.AgentEnabled || string.IsNullOrEmpty(player?.SteamId))
|
||||
return;
|
||||
|
||||
await using var connection = await _database.GetConnectionAsync();
|
||||
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)
|
||||
{
|
||||
@@ -85,11 +107,11 @@ namespace WeaponPaints
|
||||
}
|
||||
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
|
||||
{
|
||||
@@ -98,9 +120,8 @@ namespace WeaponPaints
|
||||
|
||||
var weaponInfos = new ConcurrentDictionary<int, WeaponInfo>();
|
||||
|
||||
await using var connection = await _database.GetConnectionAsync();
|
||||
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)
|
||||
{
|
||||
@@ -133,16 +154,15 @@ namespace WeaponPaints
|
||||
}
|
||||
}
|
||||
|
||||
internal async Task GetMusicFromDatabase(PlayerInfo player)
|
||||
internal void GetMusicFromDatabase(PlayerInfo player, MySqlConnection connection)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_config.Additional.MusicEnabled || string.IsNullOrEmpty(player.SteamId))
|
||||
return;
|
||||
|
||||
await using var connection = await _database.GetConnectionAsync();
|
||||
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)
|
||||
{
|
||||
@@ -155,6 +175,8 @@ namespace WeaponPaints
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal async Task SyncKnifeToDatabase(PlayerInfo player, string knife)
|
||||
{
|
||||
if (!_config.Additional.KnifeEnabled || player == null || string.IsNullOrEmpty(player.SteamId) || string.IsNullOrEmpty(knife)) return;
|
||||
|
||||
Reference in New Issue
Block a user