diff --git a/Database.cs b/Database.cs index 21dbe970..1bd46514 100644 --- a/Database.cs +++ b/Database.cs @@ -2,19 +2,20 @@ namespace WeaponPaints { - public class Database - { - private readonly IDatabaseConnection _connection; + public class Database + { + private readonly Func _connectionFactory; - public Database(IDatabaseConnection connection) - { - _connection = connection; - } + public Database(Func connectionFactory) + { + _connectionFactory = connectionFactory; + } - public async Task GetConnectionAsync() - { - await _connection.OpenAsync(); - return _connection; - } - } + public async Task GetConnectionAsync() + { + var connection = _connectionFactory(); + await connection.OpenAsync(); + return connection; + } + } } \ No newline at end of file diff --git a/WeaponPaints.cs b/WeaponPaints.cs index ec180710..7329f611 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -13,173 +13,173 @@ namespace WeaponPaints; [MinimumApiVersion(338)] public partial class WeaponPaints : BasePlugin, IPluginConfig { - internal static WeaponPaints Instance { get; private set; } = new(); + internal static WeaponPaints Instance { get; private set; } = new(); - public WeaponPaintsConfig Config { get; set; } = new(); + public WeaponPaintsConfig Config { get; set; } = new(); private static WeaponPaintsConfig _config { get; set; } = new(); 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 => "3.2a"; + public override string ModuleDescription => "Skin, gloves, agents and knife selector, standalone and web-based"; + public override string ModuleName => "WeaponPaints"; + public override string ModuleVersion => "3.2a"; - public override void Load(bool hotReload) - { - // Hardcoded hotfix needs to be changed later (Not needed 17.09.2025) - //if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - // Patch.PerformPatch("0F 85 ? ? ? ? 31 C0 B9 ? ? ? ? BA ? ? ? ? 66 0F EF C0 31 F6 31 FF 48 C7 45 ? ? ? ? ? 48 C7 45 ? ? ? ? ? 48 C7 45 ? ? ? ? ? 48 C7 45 ? ? ? ? ? 0F 29 45 ? 48 C7 45 ? ? ? ? ? C7 45 ? ? ? ? ? 66 89 45 ? E8 ? ? ? ? 41 89 C5 85 C0 0F 8E", "90 90 90 90 90 90"); - //else - // Patch.PerformPatch("74 ? 48 8D 0D ? ? ? ? FF 15 ? ? ? ? EB ? BA", "EB"); - - Instance = this; + public override void Load(bool hotReload) + { + // Hardcoded hotfix needs to be changed later (Not needed 17.09.2025) + //if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + // Patch.PerformPatch("0F 85 ? ? ? ? 31 C0 B9 ? ? ? ? BA ? ? ? ? 66 0F EF C0 31 F6 31 FF 48 C7 45 ? ? ? ? ? 48 C7 45 ? ? ? ? ? 48 C7 45 ? ? ? ? ? 48 C7 45 ? ? ? ? ? 0F 29 45 ? 48 C7 45 ? ? ? ? ? C7 45 ? ? ? ? ? 66 89 45 ? E8 ? ? ? ? 41 89 C5 85 C0 0F 8E", "90 90 90 90 90 90"); + //else + // Patch.PerformPatch("74 ? 48 8D 0D ? ? ? ? FF 15 ? ? ? ? EB ? BA", "EB"); - if (hotReload) - { - OnMapStart(string.Empty); - - GPlayerWeaponsInfo.Clear(); - GPlayersKnife.Clear(); - GPlayersGlove.Clear(); - GPlayersAgent.Clear(); - GPlayersPin.Clear(); - GPlayersMusic.Clear(); + Instance = this; - foreach (var player in Enumerable - .OfType(Utilities.GetPlayers().TakeWhile(_ => WeaponSync != null)) - .Where(player => player.IsValid && - !string.IsNullOrEmpty(player.IpAddress) && player is - { IsBot: false, Connected: PlayerConnectedState.PlayerConnected })) - { - var playerInfo = new PlayerInfo - { - UserId = player.UserId, - Slot = player.Slot, - Index = (int)player.Index, - SteamId = player?.SteamID.ToString(), - Name = player?.PlayerName, - IpAddress = player?.IpAddress?.Split(":")[0] - }; + if (hotReload) + { + OnMapStart(string.Empty); - _ = Task.Run(async () => - { - if (WeaponSync != null) await WeaponSync.GetPlayerData(playerInfo); - }); - } - } + GPlayerWeaponsInfo.Clear(); + GPlayersKnife.Clear(); + GPlayersGlove.Clear(); + GPlayersAgent.Clear(); + GPlayersPin.Clear(); + GPlayersMusic.Clear(); - Utility.LoadSkinsFromFile(ModuleDirectory + $"/data/skins_{_config.SkinsLanguage}.json", Logger); - Utility.LoadGlovesFromFile(ModuleDirectory + $"/data/gloves_{_config.SkinsLanguage}.json", Logger); - Utility.LoadAgentsFromFile(ModuleDirectory + $"/data/agents_{_config.SkinsLanguage}.json", Logger); - Utility.LoadMusicFromFile(ModuleDirectory + $"/data/music_{_config.SkinsLanguage}.json", Logger); - Utility.LoadPinsFromFile(ModuleDirectory + $"/data/collectibles_{_config.SkinsLanguage}.json", Logger); + foreach (var player in Enumerable + .OfType(Utilities.GetPlayers().TakeWhile(_ => WeaponSync != null)) + .Where(player => player.IsValid && + !string.IsNullOrEmpty(player.IpAddress) && player is + { IsBot: false, Connected: PlayerConnectedState.PlayerConnected })) + { + var playerInfo = new PlayerInfo + { + UserId = player.UserId, + Slot = player.Slot, + Index = (int)player.Index, + SteamId = player?.SteamID.ToString(), + Name = player?.PlayerName, + IpAddress = player?.IpAddress?.Split(":")[0] + }; - RegisterListeners(); - } + _ = Task.Run(async () => + { + if (WeaponSync != null) await WeaponSync.GetPlayerData(playerInfo); + }); + } + } - public void OnConfigParsed(WeaponPaintsConfig config) - { - Config = config; - _config = config; + Utility.LoadSkinsFromFile(ModuleDirectory + $"/data/skins_{_config.SkinsLanguage}.json", Logger); + Utility.LoadGlovesFromFile(ModuleDirectory + $"/data/gloves_{_config.SkinsLanguage}.json", Logger); + Utility.LoadAgentsFromFile(ModuleDirectory + $"/data/agents_{_config.SkinsLanguage}.json", Logger); + Utility.LoadMusicFromFile(ModuleDirectory + $"/data/music_{_config.SkinsLanguage}.json", Logger); + Utility.LoadPinsFromFile(ModuleDirectory + $"/data/collectibles_{_config.SkinsLanguage}.json", Logger); - // Validar configurações de banco de dados - if (config.DatabaseType.ToLower() == "mysql") - { - if (config.DatabaseHost.Length < 1 || config.DatabaseName.Length < 1 || config.DatabaseUser.Length < 1) - { - Logger.LogError("You need to setup MySQL Database credentials in \"configs/plugins/WeaponPaints/WeaponPaints.json\"!"); - Unload(false); - return; - } - } - else if (config.DatabaseType.ToLower() == "sqlite") - { - if (string.IsNullOrEmpty(config.DatabasePath)) - { - Logger.LogError("You need to setup SQLite Database path in \"configs/plugins/WeaponPaints/WeaponPaints.json\"!"); - Unload(false); - return; - } - } - else - { - Logger.LogError("Invalid DatabaseType. Use 'mysql' or 'sqlite'."); - Unload(false); - return; - } + RegisterListeners(); + } - if (!File.Exists(Path.GetDirectoryName(Path.GetDirectoryName(ModuleDirectory)) + "/gamedata/weaponpaints.json")) - { - Logger.LogError("You need to upload \"weaponpaints.json\" to \"gamedata directory\"!"); - Unload(false); - return; - } - - // Criar conexão baseada no tipo de banco - IDatabaseConnection connection; - if (config.DatabaseType.ToLower() == "mysql") - { - var builder = new MySqlConnectionStringBuilder - { - Server = config.DatabaseHost, - UserID = config.DatabaseUser, - Password = config.DatabasePassword, - Database = config.DatabaseName, - Port = (uint)config.DatabasePort, - Pooling = true, - MaximumPoolSize = 640, - }; - connection = new MySQLConnection(builder.ConnectionString, Logger); - } - else // SQLite - { - // Garantir que o diretório existe - var dbPath = Path.GetFullPath(config.DatabasePath); - var dbDirectory = Path.GetDirectoryName(dbPath); - if (!string.IsNullOrEmpty(dbDirectory) && !Directory.Exists(dbDirectory)) - { - Directory.CreateDirectory(dbDirectory); - } - - var connectionString = $"Data Source={dbPath}"; - Logger.LogInformation($"[WeaponPaints] SQLite database path: {dbPath}"); - - connection = new SQLiteConnection(connectionString, Logger); - } + public void OnConfigParsed(WeaponPaintsConfig config) + { + Config = config; + _config = config; - Database = new Database(connection); + // Validar configurações de banco de dados + if (config.DatabaseType.ToLower() == "mysql") + { + if (config.DatabaseHost.Length < 1 || config.DatabaseName.Length < 1 || config.DatabaseUser.Length < 1) + { + Logger.LogError("You need to setup MySQL Database credentials in \"configs/plugins/WeaponPaints/WeaponPaints.json\"!"); + Unload(false); + return; + } + } + else if (config.DatabaseType.ToLower() == "sqlite") + { + if (string.IsNullOrEmpty(config.DatabasePath)) + { + Logger.LogError("You need to setup SQLite Database path in \"configs/plugins/WeaponPaints/WeaponPaints.json\"!"); + Unload(false); + return; + } + } + else + { + Logger.LogError("Invalid DatabaseType. Use 'mysql' or 'sqlite'."); + Unload(false); + return; + } - Utility.Config = config; - Task.Run(async () => await Utility.CheckDatabaseTables()); - _localizer = Localizer; - Utility.ShowAd(ModuleVersion); - Task.Run(async () => await Utility.CheckVersion(ModuleVersion, Logger)); - } + if (!File.Exists(Path.GetDirectoryName(Path.GetDirectoryName(ModuleDirectory)) + "/gamedata/weaponpaints.json")) + { + Logger.LogError("You need to upload \"weaponpaints.json\" to \"gamedata directory\"!"); + Unload(false); + return; + } - public override void OnAllPluginsLoaded(bool hotReload) - { - try - { - MenuApi = MenuCapability.Get(); - - if (Config.Additional.KnifeEnabled) - SetupKnifeMenu(); - if (Config.Additional.SkinEnabled) - SetupSkinsMenu(); - if (Config.Additional.GloveEnabled) - SetupGlovesMenu(); - if (Config.Additional.AgentEnabled) - SetupAgentsMenu(); - if (Config.Additional.MusicEnabled) - SetupMusicMenu(); - if (Config.Additional.PinsEnabled) - SetupPinsMenu(); - - RegisterCommands(); - } - catch (Exception) - { - MenuApi = null; - Logger.LogError("Error while loading required plugins"); - throw; - } - } + // Criar factory de conexão baseada no tipo de banco + Func connectionFactory; + if (config.DatabaseType.ToLower() == "mysql") + { + var builder = new MySqlConnectionStringBuilder + { + Server = config.DatabaseHost, + UserID = config.DatabaseUser, + Password = config.DatabasePassword, + Database = config.DatabaseName, + Port = (uint)config.DatabasePort, + Pooling = true, + MaximumPoolSize = 640, + }; + connectionFactory = () => new MySQLConnection(builder.ConnectionString, Logger); + } + else // SQLite + { + // Garantir que o diretório existe + var dbPath = Path.GetFullPath(config.DatabasePath); + var dbDirectory = Path.GetDirectoryName(dbPath); + if (!string.IsNullOrEmpty(dbDirectory) && !Directory.Exists(dbDirectory)) + { + Directory.CreateDirectory(dbDirectory); + } + + var connectionString = $"Data Source={dbPath}"; + Logger.LogInformation($"[WeaponPaints] SQLite database path: {dbPath}"); + + connectionFactory = () => new SQLiteConnection(connectionString, Logger); + } + + Database = new Database(connectionFactory); + + Utility.Config = config; + Task.Run(async () => await Utility.CheckDatabaseTables()); + _localizer = Localizer; + Utility.ShowAd(ModuleVersion); + Task.Run(async () => await Utility.CheckVersion(ModuleVersion, Logger)); + } + + public override void OnAllPluginsLoaded(bool hotReload) + { + try + { + MenuApi = MenuCapability.Get(); + + if (Config.Additional.KnifeEnabled) + SetupKnifeMenu(); + if (Config.Additional.SkinEnabled) + SetupSkinsMenu(); + if (Config.Additional.GloveEnabled) + SetupGlovesMenu(); + if (Config.Additional.AgentEnabled) + SetupAgentsMenu(); + if (Config.Additional.MusicEnabled) + SetupMusicMenu(); + if (Config.Additional.PinsEnabled) + SetupPinsMenu(); + + RegisterCommands(); + } + catch (Exception) + { + MenuApi = null; + Logger.LogError("Error while loading required plugins"); + throw; + } + } }