mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-02-17 18:39:07 +00:00
211 lines
6.7 KiB
C#
211 lines
6.7 KiB
C#
using CounterStrikeSharp.API.Core;
|
|
using CounterStrikeSharp.API.Core.Translations;
|
|
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace WeaponPaints
|
|
{
|
|
internal static class Utility
|
|
{
|
|
internal static WeaponPaintsConfig? Config { get; set; }
|
|
|
|
internal static async Task CheckDatabaseTables()
|
|
{
|
|
if (WeaponPaints._database is null) return;
|
|
|
|
try
|
|
{
|
|
await using var connection = await WeaponPaints._database.GetConnectionAsync();
|
|
|
|
await using var transaction = await connection.BeginTransactionAsync();
|
|
|
|
try
|
|
{
|
|
string[] createTableQueries =
|
|
[
|
|
@"CREATE TABLE IF NOT EXISTS `wp_player_skins` (
|
|
`steamid` varchar(18) NOT NULL,
|
|
`weapon_defindex` int(6) NOT NULL,
|
|
`weapon_paint_id` int(6) NOT NULL,
|
|
`weapon_wear` float NOT NULL DEFAULT 0.000001,
|
|
`weapon_seed` int(16) NOT NULL DEFAULT 0
|
|
) ENGINE=InnoDB",
|
|
@"CREATE TABLE IF NOT EXISTS `wp_player_knife` (
|
|
`steamid` varchar(18) NOT NULL,
|
|
`knife` varchar(64) NOT NULL,
|
|
UNIQUE (`steamid`)
|
|
) ENGINE = InnoDB",
|
|
@"CREATE TABLE IF NOT EXISTS `wp_player_gloves` (
|
|
`steamid` varchar(18) NOT NULL,
|
|
`weapon_defindex` int(11) NOT NULL,
|
|
UNIQUE (`steamid`)
|
|
) ENGINE=InnoDB",
|
|
@"CREATE TABLE IF NOT EXISTS `wp_player_agents` (
|
|
`steamid` varchar(18) NOT NULL,
|
|
`agent_ct` varchar(64) DEFAULT NULL,
|
|
`agent_t` varchar(64) DEFAULT NULL,
|
|
UNIQUE (`steamid`)
|
|
) ENGINE=InnoDB",
|
|
@"CREATE TABLE IF NOT EXISTS `wp_player_music` (
|
|
`steamid` varchar(64) NOT NULL,
|
|
`music_id` int(11) NOT NULL,
|
|
UNIQUE (`steamid`)
|
|
) ENGINE=InnoDB",
|
|
];
|
|
|
|
foreach (var query in createTableQueries)
|
|
{
|
|
await connection.ExecuteAsync(query, transaction: transaction);
|
|
}
|
|
|
|
await transaction.CommitAsync();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
await transaction.RollbackAsync();
|
|
throw new Exception("[WeaponPaints] Unable to create tables!");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("[WeaponPaints] Unknown MySQL exception! " + ex.Message);
|
|
}
|
|
}
|
|
|
|
internal static bool IsPlayerValid(CCSPlayerController? player)
|
|
{
|
|
if (player is null || WeaponPaints.weaponSync is null) return false;
|
|
|
|
return (player.IsValid && !player.IsBot && !player.IsHLTV && player.UserId.HasValue);
|
|
}
|
|
|
|
internal static void LoadSkinsFromFile(string filePath, ILogger logger)
|
|
{
|
|
try
|
|
{
|
|
string json = File.ReadAllText(filePath);
|
|
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
|
|
WeaponPaints.skinsList = deserializedSkins ?? new List<JObject>();
|
|
}
|
|
catch (FileNotFoundException)
|
|
{
|
|
logger?.LogError("Not found \"skins.json\" file");
|
|
}
|
|
}
|
|
|
|
internal static void LoadGlovesFromFile(string filePath, ILogger logger)
|
|
{
|
|
try
|
|
{
|
|
string json = File.ReadAllText(filePath);
|
|
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
|
|
WeaponPaints.glovesList = deserializedSkins ?? new List<JObject>();
|
|
}
|
|
catch (FileNotFoundException)
|
|
{
|
|
logger?.LogError("Not found \"gloves.json\" file");
|
|
}
|
|
}
|
|
|
|
internal static void LoadAgentsFromFile(string filePath, ILogger logger)
|
|
{
|
|
try
|
|
{
|
|
string json = File.ReadAllText(filePath);
|
|
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
|
|
WeaponPaints.agentsList = deserializedSkins ?? new List<JObject>();
|
|
}
|
|
catch (FileNotFoundException)
|
|
{
|
|
logger?.LogError("Not found \"agents.json\" file");
|
|
}
|
|
}
|
|
|
|
internal static void LoadMusicFromFile(string filePath, ILogger logger)
|
|
{
|
|
try
|
|
{
|
|
string json = File.ReadAllText(filePath);
|
|
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
|
|
WeaponPaints.musicList = deserializedSkins ?? new List<JObject>();
|
|
}
|
|
catch (FileNotFoundException)
|
|
{
|
|
logger?.LogError("Not found \"music.json\" file");
|
|
}
|
|
}
|
|
|
|
internal static void Log(string message)
|
|
{
|
|
Console.BackgroundColor = ConsoleColor.DarkGray;
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
Console.WriteLine("[WeaponPaints] " + message);
|
|
Console.ResetColor();
|
|
}
|
|
|
|
internal static string ReplaceTags(string message)
|
|
{
|
|
return message.ReplaceColorTags();
|
|
}
|
|
|
|
internal static async Task CheckVersion(string version, ILogger logger)
|
|
{
|
|
using HttpClient client = new();
|
|
|
|
try
|
|
{
|
|
HttpResponseMessage response = await client.GetAsync("https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/VERSION").ConfigureAwait(false);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string remoteVersion = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
|
remoteVersion = remoteVersion.Trim();
|
|
|
|
int comparisonResult = string.Compare(version, remoteVersion);
|
|
|
|
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
|
|
{
|
|
logger.LogWarning("Failed to check 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)
|
|
{
|
|
Console.WriteLine(" ");
|
|
Console.WriteLine(" _ _ _______ _______ _______ _______ __ _ _______ _______ ___ __ _ _______ _______ ");
|
|
Console.WriteLine("| | _ | || || _ || || || | | || || _ || | | | | || || |");
|
|
Console.WriteLine("| || || || ___|| |_| || _ || _ || |_| || _ || |_| || | | |_| ||_ _|| _____|");
|
|
Console.WriteLine("| || |___ | || |_| || | | || || |_| || || | | | | | | |_____ ");
|
|
Console.WriteLine("| || ___|| || ___|| |_| || _ || ___|| || | | _ | | | |_____ |");
|
|
Console.WriteLine("| _ || |___ | _ || | | || | | || | | _ || | | | | | | | _____| |");
|
|
Console.WriteLine("|__| |__||_______||__| |__||___| |_______||_| |__||___| |__| |__||___| |_| |__| |___| |_______|");
|
|
Console.WriteLine(" >> Version: " + moduleVersion);
|
|
Console.WriteLine(" >> GitHub: https://github.com/Nereziel/cs2-WeaponPaints");
|
|
Console.WriteLine(" ");
|
|
}
|
|
}
|
|
} |