using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.IO; using System.Threading.Tasks; using SharedLibrary; using SharedLibrary.Interfaces; using SharedLibrary.Commands; using SharedLibrary.Helpers; using SharedLibrary.Exceptions; namespace IW4MAdmin { class ApplicationManager : IManager { private List _servers; public List Servers => _servers.OrderByDescending(s => s.ClientNum).ToList(); public ILogger Logger { get; private set; } public bool Running { get; private set; } static ApplicationManager Instance; List TaskStatuses; Database ClientDatabase; Database AliasesDatabase; IPenaltyList ClientPenalties; List Commands; List MessageTokens; Kayak.IScheduler webServiceTask; Thread WebThread; List PrivilegedClients; #if FTP_LOG const int UPDATE_FREQUENCY = 15000; #else const int UPDATE_FREQUENCY = 300; #endif private ApplicationManager() { Logger = new Logger("Logs/IW4MAdmin.log"); _servers = new List(); Commands = new List(); TaskStatuses = new List(); MessageTokens = new List(); ClientDatabase = new ClientsDB("Database/clients.rm"); AliasesDatabase = new AliasesDB("Database/aliases.rm"); ClientPenalties = new PenaltyList(); } public IList GetServers() { return Servers; } public IList GetCommands() { return Commands; } public static ApplicationManager GetInstance() { return Instance ?? (Instance = new ApplicationManager()); } public void Init() { #region WEBSERVICE SharedLibrary.WebService.Init(); webServiceTask = WebService.GetScheduler(); WebThread = new Thread(webServiceTask.Start) { Name = "Web Thread" }; WebThread.Start(); #endregion #region PLUGINS SharedLibrary.Plugins.PluginImporter.Load(this); foreach (var Plugin in SharedLibrary.Plugins.PluginImporter.ActivePlugins) { try { Plugin.OnLoadAsync(this); } catch (Exception e) { Logger.WriteError($"An error occured loading plugin {Plugin.Name}"); Logger.WriteDebug($"Exception: {e.Message}"); Logger.WriteDebug($"Stack Trace: {e.StackTrace}"); } } #endregion #region CONFIG var Configs = Directory.EnumerateFiles("config/servers").Where(x => x.Contains(".cfg")); if (Configs.Count() == 0) ServerConfigurationGenerator.Generate(); foreach (var file in Configs) { var Conf = ServerConfiguration.Read(file); Task.Run(async () => { try { var ServerInstance = new IW4MServer(this, Conf); await ServerInstance.Initialize(); lock (_servers) { _servers.Add(ServerInstance); } Logger.WriteVerbose($"Now monitoring {ServerInstance.Hostname}"); // this way we can keep track of execution time and see if problems arise. var Status = new AsyncStatus(ServerInstance, UPDATE_FREQUENCY); lock (TaskStatuses) { TaskStatuses.Add(Status); } } catch (ServerException e) { Logger.WriteError($"Not monitoring server {Conf.IP}:{Conf.Port} due to uncorrectable errors"); if (e.GetType() == typeof(DvarException)) Logger.WriteDebug($"Could not get the dvar value for {(e as DvarException).Data["dvar_name"]} (ensure the server has a map loaded)"); else if (e.GetType() == typeof(NetworkException)) { Logger.WriteDebug(e.Message); Logger.WriteDebug($"Internal Exception: {e.Data["internal_exception"]}"); } } }); } #endregion #region COMMANDS if ((ClientDatabase as ClientsDB).GetOwner() == null) Commands.Add(new COwner("owner", "claim ownership of the server", "o", Player.Permission.User, 0, false)); Commands.Add(new CQuit("quit", "quit IW4MAdmin", "q", Player.Permission.Owner, 0, false)); Commands.Add(new CKick("kick", "kick a player by name. syntax: !kick .", "k", Player.Permission.Trusted, 2, true)); Commands.Add(new CSay("say", "broadcast message to all players. syntax: !say .", "s", Player.Permission.Moderator, 1, false)); Commands.Add(new CTempBan("tempban", "temporarily ban a player for for specified time (defaults to 1 hour). syntax: !tempban