mirror of
https://github.com/daffyyyy/CS2-SimpleAdmin.git
synced 2026-02-17 18:39:07 +00:00
176 lines
4.9 KiB
C#
176 lines
4.9 KiB
C#
using CounterStrikeSharp.API.Core;
|
|
using CounterStrikeSharp.API.Core.Attributes;
|
|
using CounterStrikeSharp.API.Modules.Commands;
|
|
using CounterStrikeSharp.API.Modules.Commands.Targeting;
|
|
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
|
|
using Discord.Webhook;
|
|
using Microsoft.Extensions.Localization;
|
|
using Microsoft.Extensions.Logging;
|
|
using MySqlConnector;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace CS2_SimpleAdmin;
|
|
|
|
[MinimumApiVersion(215)]
|
|
public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdminConfig>
|
|
{
|
|
public static CS2_SimpleAdmin Instance { get; private set; } = new();
|
|
|
|
public static IStringLocalizer? _localizer;
|
|
public static Dictionary<string, int> voteAnswers = [];
|
|
private static ConcurrentBag<int> godPlayers = [];
|
|
private static ConcurrentBag<int> silentPlayers = [];
|
|
private static ConcurrentBag<string> bannedPlayers = [];
|
|
private static bool TagsDetected;
|
|
public static bool voteInProgress = false;
|
|
public static int? ServerId = null;
|
|
|
|
public static DiscordWebhookClient? _discordWebhookClientLog;
|
|
public static DiscordWebhookClient? _discordWebhookClientPenalty;
|
|
|
|
internal string dbConnectionString = string.Empty;
|
|
internal static Database? _database;
|
|
|
|
internal static ILogger? _logger;
|
|
|
|
public static MemoryFunctionVoid<CBasePlayerController, CCSPlayerPawn, bool, bool>? CBasePlayerControllerSetPawnFunc;
|
|
public override string ModuleName => "CS2-SimpleAdmin" + (Helper.IsDebugBuild ? " (DEBUG)" : " (RELEASE)");
|
|
public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)";
|
|
public override string ModuleAuthor => "daffyy & Dliix66";
|
|
public override string ModuleVersion => "1.4.2b";
|
|
|
|
public CS2_SimpleAdminConfig Config { get; set; } = new();
|
|
|
|
public override void Load(bool hotReload)
|
|
{
|
|
Instance = this;
|
|
|
|
RegisterEvents();
|
|
|
|
if (hotReload)
|
|
{
|
|
OnMapStart(string.Empty);
|
|
}
|
|
|
|
CBasePlayerControllerSetPawnFunc = new MemoryFunctionVoid<CBasePlayerController, CCSPlayerPawn, bool, bool>(GameData.GetSignature("CBasePlayerController_SetPawn"));
|
|
}
|
|
|
|
public void OnConfigParsed(CS2_SimpleAdminConfig config)
|
|
{
|
|
if (config.DatabaseHost.Length < 1 || config.DatabaseName.Length < 1 || config.DatabaseUser.Length < 1)
|
|
{
|
|
throw new Exception("[CS2-SimpleAdmin] You need to setup Database credentials in config!");
|
|
}
|
|
|
|
Instance = this;
|
|
_logger = Logger;
|
|
|
|
MySqlConnectionStringBuilder builder = new()
|
|
{
|
|
Server = config.DatabaseHost,
|
|
Database = config.DatabaseName,
|
|
UserID = config.DatabaseUser,
|
|
Password = config.DatabasePassword,
|
|
Port = (uint)config.DatabasePort,
|
|
Pooling = true,
|
|
MinimumPoolSize = 0,
|
|
MaximumPoolSize = 640,
|
|
};
|
|
|
|
dbConnectionString = builder.ConnectionString;
|
|
_database = new(dbConnectionString);
|
|
|
|
if (!_database.CheckDatabaseConnection())
|
|
{
|
|
Logger.LogError("Unable connect to database!");
|
|
Unload(false);
|
|
return;
|
|
}
|
|
|
|
Task.Run(() => _database.DatabaseMigration());
|
|
|
|
/*
|
|
Task.Run(async () =>
|
|
{
|
|
try
|
|
{
|
|
using MySqlConnection connection = await _database.GetConnectionAsync();
|
|
using MySqlTransaction transaction = await connection.BeginTransactionAsync();
|
|
|
|
try
|
|
{
|
|
string sqlFilePath = ModuleDirectory + "/Database/database_setup.sql";
|
|
string sql = await File.ReadAllTextAsync(sqlFilePath);
|
|
|
|
await connection.QueryAsync(sql, transaction: transaction);
|
|
await transaction.CommitAsync();
|
|
|
|
Console.WriteLine("[CS2-SimpleAdmin] Connected to database!");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
await transaction.RollbackAsync();
|
|
throw;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError($"Unable to connect to the database: {ex.Message}");
|
|
throw;
|
|
}
|
|
});
|
|
*/
|
|
|
|
Config = config;
|
|
Helper.UpdateConfig(config);
|
|
|
|
if (!Directory.Exists(ModuleDirectory + "/data"))
|
|
{
|
|
Directory.CreateDirectory(ModuleDirectory + "/data");
|
|
}
|
|
|
|
_localizer = Localizer;
|
|
|
|
if (!string.IsNullOrEmpty(Config.Discord.DiscordLogWebhook))
|
|
_discordWebhookClientLog = new DiscordWebhookClient(Config.Discord.DiscordLogWebhook);
|
|
if (!string.IsNullOrEmpty(Config.Discord.DiscordPenaltyWebhook))
|
|
_discordWebhookClientPenalty = new DiscordWebhookClient(Config.Discord.DiscordPenaltyWebhook);
|
|
}
|
|
|
|
private static TargetResult? GetTarget(CommandInfo command)
|
|
{
|
|
var matches = command.GetArgTargetResult(1);
|
|
|
|
if (!matches.Any())
|
|
{
|
|
command.ReplyToCommand($"Target {command.GetArg(1)} not found.");
|
|
return null;
|
|
}
|
|
|
|
if (command.GetArg(1).StartsWith('@'))
|
|
return matches;
|
|
|
|
if (matches.Count() == 1)
|
|
return matches;
|
|
|
|
command.ReplyToCommand($"Multiple targets found for \"{command.GetArg(1)}\".");
|
|
return null;
|
|
}
|
|
|
|
public static void RemoveFromConcurrentBag(ConcurrentBag<int> bag, int playerSlot)
|
|
{
|
|
List<int> tempList = [];
|
|
while (!bag.IsEmpty)
|
|
{
|
|
if (bag.TryTake(out var item) && item != playerSlot)
|
|
{
|
|
tempList.Add(item);
|
|
}
|
|
}
|
|
|
|
foreach (var item in tempList)
|
|
{
|
|
bag.Add(item);
|
|
}
|
|
}
|
|
} |