mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-02-18 10:43:22 +00:00
Fix: Factory patten when opening connection mysql
This commit is contained in:
27
Database.cs
27
Database.cs
@@ -2,19 +2,20 @@
|
|||||||
|
|
||||||
namespace WeaponPaints
|
namespace WeaponPaints
|
||||||
{
|
{
|
||||||
public class Database
|
public class Database
|
||||||
{
|
{
|
||||||
private readonly IDatabaseConnection _connection;
|
private readonly Func<IDatabaseConnection> _connectionFactory;
|
||||||
|
|
||||||
public Database(IDatabaseConnection connection)
|
public Database(Func<IDatabaseConnection> connectionFactory)
|
||||||
{
|
{
|
||||||
_connection = connection;
|
_connectionFactory = connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IDatabaseConnection> GetConnectionAsync()
|
public async Task<IDatabaseConnection> GetConnectionAsync()
|
||||||
{
|
{
|
||||||
await _connection.OpenAsync();
|
var connection = _connectionFactory();
|
||||||
return _connection;
|
await connection.OpenAsync();
|
||||||
}
|
return connection;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
308
WeaponPaints.cs
308
WeaponPaints.cs
@@ -13,173 +13,173 @@ namespace WeaponPaints;
|
|||||||
[MinimumApiVersion(338)]
|
[MinimumApiVersion(338)]
|
||||||
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();
|
||||||
|
|
||||||
public WeaponPaintsConfig Config { get; set; } = new();
|
public WeaponPaintsConfig Config { get; set; } = new();
|
||||||
private static WeaponPaintsConfig _config { get; set; } = new();
|
private static WeaponPaintsConfig _config { get; set; } = new();
|
||||||
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 => "3.2a";
|
public override string ModuleVersion => "3.2a";
|
||||||
|
|
||||||
public override void Load(bool hotReload)
|
public override void Load(bool hotReload)
|
||||||
{
|
{
|
||||||
// Hardcoded hotfix needs to be changed later (Not needed 17.09.2025)
|
// Hardcoded hotfix needs to be changed later (Not needed 17.09.2025)
|
||||||
//if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
//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");
|
// 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
|
//else
|
||||||
// Patch.PerformPatch("74 ? 48 8D 0D ? ? ? ? FF 15 ? ? ? ? EB ? BA", "EB");
|
// Patch.PerformPatch("74 ? 48 8D 0D ? ? ? ? FF 15 ? ? ? ? EB ? BA", "EB");
|
||||||
|
|
||||||
Instance = this;
|
|
||||||
|
|
||||||
if (hotReload)
|
Instance = this;
|
||||||
{
|
|
||||||
OnMapStart(string.Empty);
|
|
||||||
|
|
||||||
GPlayerWeaponsInfo.Clear();
|
|
||||||
GPlayersKnife.Clear();
|
|
||||||
GPlayersGlove.Clear();
|
|
||||||
GPlayersAgent.Clear();
|
|
||||||
GPlayersPin.Clear();
|
|
||||||
GPlayersMusic.Clear();
|
|
||||||
|
|
||||||
foreach (var player in Enumerable
|
if (hotReload)
|
||||||
.OfType<CCSPlayerController>(Utilities.GetPlayers().TakeWhile(_ => WeaponSync != null))
|
{
|
||||||
.Where(player => player.IsValid &&
|
OnMapStart(string.Empty);
|
||||||
!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]
|
|
||||||
};
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
GPlayerWeaponsInfo.Clear();
|
||||||
{
|
GPlayersKnife.Clear();
|
||||||
if (WeaponSync != null) await WeaponSync.GetPlayerData(playerInfo);
|
GPlayersGlove.Clear();
|
||||||
});
|
GPlayersAgent.Clear();
|
||||||
}
|
GPlayersPin.Clear();
|
||||||
}
|
GPlayersMusic.Clear();
|
||||||
|
|
||||||
Utility.LoadSkinsFromFile(ModuleDirectory + $"/data/skins_{_config.SkinsLanguage}.json", Logger);
|
foreach (var player in Enumerable
|
||||||
Utility.LoadGlovesFromFile(ModuleDirectory + $"/data/gloves_{_config.SkinsLanguage}.json", Logger);
|
.OfType<CCSPlayerController>(Utilities.GetPlayers().TakeWhile(_ => WeaponSync != null))
|
||||||
Utility.LoadAgentsFromFile(ModuleDirectory + $"/data/agents_{_config.SkinsLanguage}.json", Logger);
|
.Where(player => player.IsValid &&
|
||||||
Utility.LoadMusicFromFile(ModuleDirectory + $"/data/music_{_config.SkinsLanguage}.json", Logger);
|
!string.IsNullOrEmpty(player.IpAddress) && player is
|
||||||
Utility.LoadPinsFromFile(ModuleDirectory + $"/data/collectibles_{_config.SkinsLanguage}.json", Logger);
|
{ 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)
|
Utility.LoadSkinsFromFile(ModuleDirectory + $"/data/skins_{_config.SkinsLanguage}.json", Logger);
|
||||||
{
|
Utility.LoadGlovesFromFile(ModuleDirectory + $"/data/gloves_{_config.SkinsLanguage}.json", Logger);
|
||||||
Config = config;
|
Utility.LoadAgentsFromFile(ModuleDirectory + $"/data/agents_{_config.SkinsLanguage}.json", Logger);
|
||||||
_config = config;
|
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
|
RegisterListeners();
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!File.Exists(Path.GetDirectoryName(Path.GetDirectoryName(ModuleDirectory)) + "/gamedata/weaponpaints.json"))
|
public void OnConfigParsed(WeaponPaintsConfig config)
|
||||||
{
|
{
|
||||||
Logger.LogError("You need to upload \"weaponpaints.json\" to \"gamedata directory\"!");
|
Config = config;
|
||||||
Unload(false);
|
_config = config;
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
if (!File.Exists(Path.GetDirectoryName(Path.GetDirectoryName(ModuleDirectory)) + "/gamedata/weaponpaints.json"))
|
||||||
Task.Run(async () => await Utility.CheckDatabaseTables());
|
{
|
||||||
_localizer = Localizer;
|
Logger.LogError("You need to upload \"weaponpaints.json\" to \"gamedata directory\"!");
|
||||||
Utility.ShowAd(ModuleVersion);
|
Unload(false);
|
||||||
Task.Run(async () => await Utility.CheckVersion(ModuleVersion, Logger));
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnAllPluginsLoaded(bool hotReload)
|
// Criar factory de conexão baseada no tipo de banco
|
||||||
{
|
Func<IDatabaseConnection> connectionFactory;
|
||||||
try
|
if (config.DatabaseType.ToLower() == "mysql")
|
||||||
{
|
{
|
||||||
MenuApi = MenuCapability.Get();
|
var builder = new MySqlConnectionStringBuilder
|
||||||
|
{
|
||||||
if (Config.Additional.KnifeEnabled)
|
Server = config.DatabaseHost,
|
||||||
SetupKnifeMenu();
|
UserID = config.DatabaseUser,
|
||||||
if (Config.Additional.SkinEnabled)
|
Password = config.DatabasePassword,
|
||||||
SetupSkinsMenu();
|
Database = config.DatabaseName,
|
||||||
if (Config.Additional.GloveEnabled)
|
Port = (uint)config.DatabasePort,
|
||||||
SetupGlovesMenu();
|
Pooling = true,
|
||||||
if (Config.Additional.AgentEnabled)
|
MaximumPoolSize = 640,
|
||||||
SetupAgentsMenu();
|
};
|
||||||
if (Config.Additional.MusicEnabled)
|
connectionFactory = () => new MySQLConnection(builder.ConnectionString, Logger);
|
||||||
SetupMusicMenu();
|
}
|
||||||
if (Config.Additional.PinsEnabled)
|
else // SQLite
|
||||||
SetupPinsMenu();
|
{
|
||||||
|
// Garantir que o diretório existe
|
||||||
RegisterCommands();
|
var dbPath = Path.GetFullPath(config.DatabasePath);
|
||||||
}
|
var dbDirectory = Path.GetDirectoryName(dbPath);
|
||||||
catch (Exception)
|
if (!string.IsNullOrEmpty(dbDirectory) && !Directory.Exists(dbDirectory))
|
||||||
{
|
{
|
||||||
MenuApi = null;
|
Directory.CreateDirectory(dbDirectory);
|
||||||
Logger.LogError("Error while loading required plugins");
|
}
|
||||||
throw;
|
|
||||||
}
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user