From ed594134b4d7e99b4468681cddfab5f56522515a Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Fri, 30 Jun 2017 16:13:15 -0400 Subject: [PATCH] Chat command refactor (#91) * Generalize Commands * Generalization * Compiling * Finishing touched --- TGCommandLine/BYONDCommands.cs | 18 +- TGCommandLine/ChatCommands.cs | 176 ++++++------ TGCommandLine/ConfigCommands.cs | 36 +-- TGCommandLine/DDCommands.cs | 70 ++--- TGCommandLine/DMCommands.cs | 64 ++--- TGCommandLine/Program.cs | 46 +--- TGCommandLine/RepoCommands.cs | 108 ++++---- TGCommandLine/RootCommands.cs | 81 +----- TGServerService/Chat.cs | 76 +----- TGServerService/ChatCommands.cs | 265 +++++++++++++++++++ TGServerService/InterfaceBase.cs | 1 + TGServerService/Interop.cs | 18 +- TGServerService/TGServerService.csproj | 1 + TGServiceInterface/Command.cs | 122 +++++++++ TGServiceInterface/TGServiceInterface.csproj | 1 + 15 files changed, 662 insertions(+), 421 deletions(-) create mode 100644 TGServerService/ChatCommands.cs create mode 100644 TGServiceInterface/Command.cs diff --git a/TGCommandLine/BYONDCommands.cs b/TGCommandLine/BYONDCommands.cs index d99b8e55e9..e30350be19 100644 --- a/TGCommandLine/BYONDCommands.cs +++ b/TGCommandLine/BYONDCommands.cs @@ -12,7 +12,7 @@ namespace TGCommandLine Keyword = "byond"; Children = new Command[] { new BYONDUpdateCommand(), new BYONDVersionCommand(), new BYONDStatusCommand() }; } - protected override string GetHelpText() + public override string GetHelpText() { return "Manage BYOND installation"; } @@ -24,7 +24,7 @@ namespace TGCommandLine { Keyword = "version"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var type = TGByondVersion.Installed; if (parameters.Count > 0) @@ -35,12 +35,12 @@ namespace TGCommandLine Console.WriteLine(Server.GetComponent().GetVersion(type) ?? "Unistalled"); return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return "[--staged|--latest]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Print the currently installed BYOND version"; } @@ -53,7 +53,7 @@ namespace TGCommandLine { Keyword = "status"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { switch (Server.GetComponent().CurrentStatus()) { @@ -81,7 +81,7 @@ namespace TGCommandLine } return ExitCode.Normal; } - protected override string GetHelpText() + public override string GetHelpText() { return "Print the current status of the BYOND updater"; } @@ -94,7 +94,7 @@ namespace TGCommandLine Keyword = "update"; RequiredParameters = 2; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { int Major = 0, Minor = 0; try @@ -126,11 +126,11 @@ namespace TGCommandLine Console.WriteLine(res ?? (stat == TGByondStatus.Staged ? "Update staged and will apply next DD reboot" : "Update finished")); return res == null ? ExitCode.Normal : ExitCode.ServerError; } - protected override string GetArgumentString() + public override string GetArgumentString() { return " "; } - protected override string GetHelpText() + public override string GetHelpText() { return "Updates the BYOND installation to the specified version"; } diff --git a/TGCommandLine/ChatCommands.cs b/TGCommandLine/ChatCommands.cs index ed130ef4c4..0b262ef96b 100644 --- a/TGCommandLine/ChatCommands.cs +++ b/TGCommandLine/ChatCommands.cs @@ -11,7 +11,7 @@ namespace TGCommandLine Keyword = "irc"; Children = new Command[] { new IRCNickCommand(), new IRCAuthCommand(), new IRCDisableAuthCommand(), new IRCServerCommand(), new ChatJoinCommand(TGChatProvider.IRC), new ChatPartCommand(TGChatProvider.IRC), new ChatListAdminsCommand(TGChatProvider.IRC), new ChatReconnectCommand(TGChatProvider.IRC), new ChatAddminCommand(TGChatProvider.IRC), new ChatDeadminCommand(TGChatProvider.IRC), new ChatEnableCommand(TGChatProvider.IRC), new ChatDisableCommand(TGChatProvider.IRC), new ChatStatusCommand(TGChatProvider.IRC), new IRCAuthModeCommand(), new IRCAuthLevelCommand() }; } - protected override string GetHelpText() + public override string GetHelpText() { return "Manages the IRC bot"; } @@ -23,7 +23,7 @@ namespace TGCommandLine Keyword = "discord"; Children = new Command[] { new DiscordSetTokenCommand(), new ChatJoinCommand(TGChatProvider.Discord), new ChatPartCommand(TGChatProvider.Discord), new ChatListAdminsCommand(TGChatProvider.Discord), new ChatReconnectCommand(TGChatProvider.Discord), new ChatAddminCommand(TGChatProvider.Discord), new ChatDeadminCommand(TGChatProvider.Discord), new ChatEnableCommand(TGChatProvider.Discord), new ChatDisableCommand(TGChatProvider.Discord), new ChatStatusCommand(TGChatProvider.Discord) , new DiscordAuthModeCommand() }; } - protected override string GetHelpText() + public override string GetHelpText() { return "Manages the Discord bot"; } @@ -36,16 +36,16 @@ namespace TGCommandLine RequiredParameters = 1; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Sets the IRC nickname"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var Chat = Server.GetComponent(); Chat.SetProviderInfo(new TGIRCSetupInfo(Chat.ProviderInfos()[(int)TGChatProvider.IRC]) @@ -66,16 +66,16 @@ namespace TGCommandLine providerIndex = (int)pI; } - protected override string GetArgumentString() + public override string GetArgumentString() { return " "; } - protected override string GetHelpText() + public override string GetHelpText() { return "Joins a channel for listening and broadcasting of the specified message type (Developer, Watchdog, Game, Admin)"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); var info = IRC.ProviderInfos()[providerIndex]; @@ -96,7 +96,7 @@ namespace TGCommandLine channels = info.AdminChannels; break; default: - Console.WriteLine("Invalid parameter: " + parameters[1]); + OutputProc("Invalid parameter: " + parameters[1]); return ExitCode.BadCommand; } @@ -105,7 +105,7 @@ namespace TGCommandLine { if (I.ToLower() == lowerParam) { - Console.WriteLine("Already in this channel!"); + OutputProc("Already in this channel!"); return ExitCode.BadCommand; } } @@ -128,7 +128,7 @@ namespace TGCommandLine var res = IRC.SetProviderInfo(info); if(res != null) { - Console.WriteLine(res); + OutputProc(res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -145,15 +145,15 @@ namespace TGCommandLine providerIndex = (int)pI; } - protected override string GetArgumentString() + public override string GetArgumentString() { return " "; } - protected override string GetHelpText() + public override string GetHelpText() { return "Stops listening and broadcasting on a channel for the specified message type (Developer, Watchdog, Game, Admin)"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); var info = IRC.ProviderInfos()[providerIndex]; @@ -174,7 +174,7 @@ namespace TGCommandLine channels = info.AdminChannels; break; default: - Console.WriteLine("Invalid parameter: " + parameters[1]); + OutputProc("Invalid parameter: " + parameters[1]); return ExitCode.BadCommand; } var lowerParam = parameters[0].ToLower(); @@ -199,7 +199,7 @@ namespace TGCommandLine var res = IRC.SetProviderInfo(info); if (res != null) { - Console.WriteLine(res); + OutputProc(res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -214,12 +214,12 @@ namespace TGCommandLine providerIndex = (int)pI; } - protected override string GetHelpText() + public override string GetHelpText() { return "List users which can use restricted commands in the admin channel"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var info = Server.GetComponent().ProviderInfos()[providerIndex]; string authType; @@ -238,29 +238,29 @@ namespace TGCommandLine authType = "User IDs:"; break; default: - Console.WriteLine(String.Format("Invalid provider: {0}!", providerIndex)); + OutputProc(String.Format("Invalid provider: {0}!", providerIndex)); return ExitCode.ServerError; } - Console.WriteLine("Authorized " + authType); + OutputProc("Authorized " + authType); if (info.AdminsAreSpecial && (TGChatProvider)providerIndex == TGChatProvider.IRC) switch(new TGIRCSetupInfo(info).AuthLevel) { case IRCMode.Voice: - Console.WriteLine("+"); + OutputProc("+"); break; case IRCMode.Halfop: - Console.WriteLine("%"); + OutputProc("%"); break; case IRCMode.Op: - Console.WriteLine("@"); + OutputProc("@"); break; case IRCMode.Owner: - Console.WriteLine("~"); + OutputProc("~"); break; } else foreach (var I in info.AdminList) - Console.WriteLine(I); + OutputProc(I); return ExitCode.Normal; } } @@ -273,17 +273,17 @@ namespace TGCommandLine providerIndex = pI; } - protected override string GetHelpText() + public override string GetHelpText() { return "Restablish the chat connection"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var res = Server.GetComponent().Reconnect(providerIndex); if (res != null) { - Console.WriteLine("Error: " + res); + OutputProc("Error: " + res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -299,15 +299,15 @@ namespace TGCommandLine providerIndex = (int)pI; } - protected override string GetArgumentString() + public override string GetArgumentString() { return "[nick]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Add a user which can use restricted commands in the admin channels"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); var info = IRC.ProviderInfos()[providerIndex]; @@ -315,13 +315,13 @@ namespace TGCommandLine if (info.AdminsAreSpecial && (TGChatProvider)providerIndex == TGChatProvider.IRC) { - Console.WriteLine("Invalid auth mode for this command!"); + OutputProc("Invalid auth mode for this command!"); return ExitCode.BadCommand; } if (info.AdminList.Contains(newmin)) { - Console.WriteLine(parameters[0] + " is already an admin!"); + OutputProc(parameters[0] + " is already an admin!"); return ExitCode.BadCommand; } var al = info.AdminList; @@ -330,7 +330,7 @@ namespace TGCommandLine var res = IRC.SetProviderInfo(info); if (res != null) { - Console.WriteLine(res); + OutputProc(res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -344,15 +344,15 @@ namespace TGCommandLine RequiredParameters = 1; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Switch between admin command authorization via user channel mode or nicknames"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); var info = IRC.ProviderInfos()[(int)TGChatProvider.IRC]; @@ -363,13 +363,13 @@ namespace TGCommandLine info.AdminsAreSpecial = false; else { - Console.WriteLine("Invalid parameter: " + parameters[0]); + OutputProc("Invalid parameter: " + parameters[0]); return ExitCode.BadCommand; } var res = IRC.SetProviderInfo(info); if (res != null) { - Console.WriteLine(res); + OutputProc(res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -383,15 +383,15 @@ namespace TGCommandLine RequiredParameters = 1; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Switch between admin command authorization via user roles or individual users"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); var info = IRC.ProviderInfos()[(int)TGChatProvider.Discord]; @@ -402,13 +402,13 @@ namespace TGCommandLine info.AdminsAreSpecial = false; else { - Console.WriteLine("Invalid parameter: " + parameters[0]); + OutputProc("Invalid parameter: " + parameters[0]); return ExitCode.BadCommand; } var res = IRC.SetProviderInfo(info); if (res != null) { - Console.WriteLine(res); + OutputProc(res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -422,15 +422,15 @@ namespace TGCommandLine RequiredParameters = 1; } - protected override string GetArgumentString() + public override string GetArgumentString() { return "<+|%|@|~>"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Set the required channel mode for users to use admin commands"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); var info = new TGIRCSetupInfo(IRC.ProviderInfos()[(int)TGChatProvider.IRC]); @@ -449,14 +449,14 @@ namespace TGCommandLine info.AuthLevel = IRCMode.Owner; break; default: - Console.WriteLine("Invalid parameter: " + parameters[0]); + OutputProc("Invalid parameter: " + parameters[0]); return ExitCode.BadCommand; } var res = IRC.SetProviderInfo(info); if (res != null) { - Console.WriteLine(res); + OutputProc(res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -471,15 +471,15 @@ namespace TGCommandLine RequiredParameters = 1; providerIndex = (int)pI; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Remove a user which can use restricted commands in the admin channels"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); var info = IRC.ProviderInfos()[providerIndex]; @@ -487,13 +487,13 @@ namespace TGCommandLine if (info.AdminsAreSpecial && (TGChatProvider)providerIndex == TGChatProvider.IRC) { - Console.WriteLine("Invalid auth mode for this command!"); + OutputProc("Invalid auth mode for this command!"); return ExitCode.BadCommand; } if (!info.AdminList.Contains(newmin)) { - Console.WriteLine(parameters[0] + " is not an admin!"); + OutputProc(parameters[0] + " is not an admin!"); return ExitCode.BadCommand; } @@ -503,7 +503,7 @@ namespace TGCommandLine var res = IRC.SetProviderInfo(info); if (res != null) { - Console.WriteLine(res); + OutputProc(res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -518,15 +518,15 @@ namespace TGCommandLine RequiredParameters = 2; } - protected override string GetArgumentString() + public override string GetArgumentString() { return " "; } - protected override string GetHelpText() + public override string GetHelpText() { return "Set the authentication message to send to target for identification. e.g. NickServ \"identify hunter2\""; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); IRC.SetProviderInfo(new TGIRCSetupInfo(IRC.ProviderInfos()[(int)TGChatProvider.IRC]) @@ -544,11 +544,11 @@ namespace TGCommandLine { Keyword = "disable-auth"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Turns off IRC authentication"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); IRC.SetProviderInfo(new TGIRCSetupInfo(IRC.ProviderInfos()[(int)TGChatProvider.IRC]) @@ -568,28 +568,28 @@ namespace TGCommandLine Keyword = "status"; providerIndex = (int)pI; } - protected override string GetHelpText() + public override string GetHelpText() { return "Lists channels and connections status"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var IRC = Server.GetComponent(); var info = IRC.ProviderInfos()[providerIndex]; - Console.WriteLine("Currently configured channels:"); - Console.WriteLine("Admin:"); + OutputProc("Currently configured channels:"); + OutputProc("Admin:"); foreach (var I in info.AdminChannels) - Console.WriteLine("\t" + I); - Console.WriteLine("Watchdog:"); + OutputProc("\t" + I); + OutputProc("Watchdog:"); foreach (var I in info.WatchdogChannels) - Console.WriteLine("\t" + I); - Console.WriteLine("Game:"); + OutputProc("\t" + I); + OutputProc("Game:"); foreach (var I in info.GameChannels) - Console.WriteLine("\t" + I); - Console.WriteLine("Developer:"); + OutputProc("\t" + I); + OutputProc("Developer:"); foreach (var I in info.DevChannels) - Console.WriteLine("\t" + I); - Console.WriteLine("Chat bot is: " + (!info.Enabled ? "Disabled" : IRC.Connected(info.Provider) ? "Connected" : "Disconnected")); + OutputProc("\t" + I); + OutputProc("Chat bot is: " + (!info.Enabled ? "Disabled" : IRC.Connected(info.Provider) ? "Connected" : "Disconnected")); return ExitCode.Normal; } } @@ -602,12 +602,12 @@ namespace TGCommandLine providerIndex = (int)pI; } - protected override string GetHelpText() + public override string GetHelpText() { return "Enables the chat bot"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var Chat = Server.GetComponent(); var info = Chat.ProviderInfos()[providerIndex]; @@ -615,7 +615,7 @@ namespace TGCommandLine var res = Chat.SetProviderInfo(info); if (res != null) { - Console.WriteLine(res); + OutputProc(res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -630,12 +630,12 @@ namespace TGCommandLine providerIndex = (int)pI; } - protected override string GetHelpText() + public override string GetHelpText() { return "Disables the chat bot"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var Chat = Server.GetComponent(); var info = Chat.ProviderInfos()[providerIndex]; @@ -643,7 +643,7 @@ namespace TGCommandLine var res = Chat.SetProviderInfo(info); if (res != null) { - Console.WriteLine(res); + OutputProc(res); return ExitCode.ServerError; } return ExitCode.Normal; @@ -657,21 +657,21 @@ namespace TGCommandLine Keyword = "set-server"; RequiredParameters = 1; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ":"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Sets the IRC server"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var splits = parameters[0].Split(':'); if(splits.Length < 2) { - Console.WriteLine("Invalid parameter!"); + OutputProc("Invalid parameter!"); return ExitCode.BadCommand; } var Chat = Server.GetComponent(); @@ -685,12 +685,12 @@ namespace TGCommandLine } catch { - Console.WriteLine("Invalid port number!"); + OutputProc("Invalid port number!"); return ExitCode.BadCommand; } var res = Chat.SetProviderInfo(PI); - Console.WriteLine(res ?? "Success"); + OutputProc(res ?? "Success"); return res == null ? ExitCode.Normal : ExitCode.ServerError; } } @@ -702,19 +702,19 @@ namespace TGCommandLine Keyword = "set-token"; RequiredParameters = 1; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Sets the discord API bot token"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var Chat = Server.GetComponent(); var res = Chat.SetProviderInfo(new TGDiscordSetupInfo(Chat.ProviderInfos()[(int)TGChatProvider.Discord]) { BotToken = parameters[0] }); - Console.WriteLine(res ?? "Success"); + OutputProc(res ?? "Success"); return res == null ? ExitCode.Normal : ExitCode.ServerError; } } diff --git a/TGCommandLine/ConfigCommands.cs b/TGCommandLine/ConfigCommands.cs index 796afdd8b0..1039bb98d7 100644 --- a/TGCommandLine/ConfigCommands.cs +++ b/TGCommandLine/ConfigCommands.cs @@ -12,7 +12,7 @@ namespace TGCommandLine Keyword = "config"; Children = new Command[] { new ConfigMoveServerCommand(), new ConfigServerDirectoryCommand(), new ConfigDownloadCommand(), new ConfigUploadCommand() }; } - protected override string GetHelpText() + public override string GetHelpText() { return "Manage settings"; } @@ -26,19 +26,19 @@ namespace TGCommandLine RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var res = Server.GetComponent().MoveServer(parameters[0]); - Console.WriteLine(res ?? "Success"); + OutputProc(res ?? "Success"); return res == null ? ExitCode.Normal : ExitCode.ServerError; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Move the server installation (BYOND, Repo, Game) to a new location. Nothing else may be running for this task to complete"; } @@ -51,13 +51,13 @@ namespace TGCommandLine Keyword = "server-dir"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { - Console.WriteLine(Server.GetComponent().ServerDirectory()); + OutputProc(Server.GetComponent().ServerDirectory()); return ExitCode.Normal; } - protected override string GetHelpText() + public override string GetHelpText() { return "Print the directory the server is installed in"; } @@ -71,12 +71,12 @@ namespace TGCommandLine RequiredParameters = 2; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var bytes = Server.GetComponent().ReadRaw(parameters[0], parameters.Count > 2 && parameters[2].ToLower() == "--repo", out string error); if(bytes == null) { - Console.WriteLine("Error: " + error); + OutputProc("Error: " + error); return ExitCode.ServerError; } @@ -86,16 +86,16 @@ namespace TGCommandLine } catch (Exception e) { - Console.WriteLine("Error: " + e.ToString()); + OutputProc("Error: " + e.ToString()); } return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return " [--repo]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Downloads the specified file from the config tree and writes it to out file. --repo will fetch it from the repository instead of the game config"; } @@ -109,30 +109,30 @@ namespace TGCommandLine RequiredParameters = 2; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { try { var res = Server.GetComponent().WriteRaw(parameters[0], File.ReadAllText(parameters[1])); if (res != null) { - Console.WriteLine("Error: " + res); + OutputProc("Error: " + res); return ExitCode.ServerError; } } catch (Exception e) { - Console.WriteLine("Error: " + e.ToString()); + OutputProc("Error: " + e.ToString()); } return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return " [--repo]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Uploads the specified file to the config tree from source file"; } diff --git a/TGCommandLine/DDCommands.cs b/TGCommandLine/DDCommands.cs index e358a3af1e..63170b1e3d 100644 --- a/TGCommandLine/DDCommands.cs +++ b/TGCommandLine/DDCommands.cs @@ -11,7 +11,7 @@ namespace TGCommandLine Keyword = "dd"; Children = new Command[] { new DDStartCommand(), new DDStopCommand(), new DDRestartCommand(), new DDStatusCommand(), new DDAutostartCommand(), new DDPortCommand(), new DDVisibilityCommand(), new DDSecurityCommand() }; } - protected override string GetHelpText() + public override string GetHelpText() { return "Manage DreamDaemon"; } @@ -24,15 +24,15 @@ namespace TGCommandLine Keyword = "start"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Starts the server and watchdog"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var res = Server.GetComponent().Start(); - Console.WriteLine(res ?? "Success!"); + OutputProc(res ?? "Success!"); return res == null ? ExitCode.Normal : ExitCode.ServerError; } } @@ -43,31 +43,31 @@ namespace TGCommandLine { Keyword = "stop"; } - protected override string GetArgumentString() + public override string GetArgumentString() { return "[--graceful]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Stops the server and watchdog optionally waiting for the current round to end"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var DD = Server.GetComponent(); if (parameters.Count > 0 && parameters[0].ToLower() == "--graceful") { if (DD.DaemonStatus() != TGDreamDaemonStatus.Online) { - Console.WriteLine("Error: The game is not currently running!"); + OutputProc("Error: The game is not currently running!"); return ExitCode.ServerError; } DD.RequestStop(); return ExitCode.Normal; } var res = DD.Stop(); - Console.WriteLine(res ?? "Success!"); + OutputProc(res ?? "Success!"); return res == null ? ExitCode.Normal : ExitCode.ServerError; } } @@ -78,29 +78,29 @@ namespace TGCommandLine Keyword = "restart"; } - protected override string GetArgumentString() + public override string GetArgumentString() { return "[--graceful]"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var DD = Server.GetComponent(); if (parameters.Count > 0 && parameters[0].ToLower() == "--graceful") { if (DD.DaemonStatus() != TGDreamDaemonStatus.Online) { - Console.WriteLine("Error: The game is not currently running!"); + OutputProc("Error: The game is not currently running!"); return ExitCode.ServerError; } DD.RequestRestart(); return ExitCode.Normal; } var res = DD.Restart(); - Console.WriteLine(res ?? "Success!"); + OutputProc(res ?? "Success!"); return res == null ? ExitCode.Normal : ExitCode.ServerError; } - protected override string GetHelpText() + public override string GetHelpText() { return "Restarts the server and watchdog optionally waiting for the current round to end"; } @@ -112,17 +112,17 @@ namespace TGCommandLine Keyword = "status"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Gets the current status of the watchdog and server"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var DD = Server.GetComponent(); - Console.WriteLine(DD.StatusString(true)); + OutputProc(DD.StatusString(true)); if (DD.ShutdownInProgress()) - Console.WriteLine("The server will shutdown once the current round completes."); + OutputProc("The server will shutdown once the current round completes."); return ExitCode.Normal; } } @@ -135,7 +135,7 @@ namespace TGCommandLine RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var DD = Server.GetComponent(); switch (parameters[0].ToLower()) @@ -147,20 +147,20 @@ namespace TGCommandLine DD.SetAutostart(false); break; case "check": - Console.WriteLine("Autostart is: " + (DD.Autostart() ? "On" : "Off")); + OutputProc("Autostart is: " + (DD.Autostart() ? "On" : "Off")); break; default: - Console.WriteLine("Invalid parameter: " + parameters[0]); + OutputProc("Invalid parameter: " + parameters[0]); return ExitCode.BadCommand; } return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Change or check autostarting of the game server with the service"; } @@ -173,7 +173,7 @@ namespace TGCommandLine RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { ushort port; try @@ -182,7 +182,7 @@ namespace TGCommandLine } catch { - Console.WriteLine("Invalid port number!"); + OutputProc("Invalid port number!"); return ExitCode.BadCommand; } @@ -190,12 +190,12 @@ namespace TGCommandLine return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Sets the port DreamDaemon will open the server on. Requires a server restart to apply and queues a graceful one up"; } @@ -209,7 +209,7 @@ namespace TGCommandLine RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { TGDreamDaemonVisibility vis; switch (parameters[0].ToLower()) @@ -227,19 +227,19 @@ namespace TGCommandLine vis = TGDreamDaemonVisibility.Public; break; default: - Console.WriteLine("Invalid visiblity word!"); + OutputProc("Invalid visiblity word!"); return ExitCode.BadCommand; } Server.GetComponent().SetVisibility(vis); return ExitCode.Normal; } - protected override string GetHelpText() + public override string GetHelpText() { return "Sets the visibility option for the DreamDaemon world. Requires a server restart to apply and queues a graceful one up"; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } @@ -253,7 +253,7 @@ namespace TGCommandLine RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { TGDreamDaemonSecurity sec; switch (parameters[0].ToLower()) @@ -270,19 +270,19 @@ namespace TGCommandLine sec = TGDreamDaemonSecurity.Trusted; break; default: - Console.WriteLine("Invalid security word!"); + OutputProc("Invalid security word!"); return ExitCode.BadCommand; } Server.GetComponent().SetSecurityLevel(sec); return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Sets the visibility option for the DreamDaemon world"; } diff --git a/TGCommandLine/DMCommands.cs b/TGCommandLine/DMCommands.cs index 56a0e39152..f734e731f4 100644 --- a/TGCommandLine/DMCommands.cs +++ b/TGCommandLine/DMCommands.cs @@ -12,7 +12,7 @@ namespace TGCommandLine Keyword = "dm"; Children = new Command[] { new DMCompileCommand(), new DMInitializeCommand(), new DMStatusCommand(), new DMSetProjectNameCommand(), new DMCancelCommand() }; } - protected override string GetHelpText() + public override string GetHelpText() { return "Manage compiling the server"; } @@ -25,13 +25,13 @@ namespace TGCommandLine Keyword = "compile"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var DM = Server.GetComponent(); var stat = DM.GetStatus(); if (stat != TGCompilerStatus.Initialized) { - Console.WriteLine("Error: Compiler is " + ((stat == TGCompilerStatus.Uninitialized) ? "unintialized!" : "busy with another task!")); + OutputProc("Error: Compiler is " + ((stat == TGCompilerStatus.Uninitialized) ? "unintialized!" : "busy with another task!")); return ExitCode.ServerError; } @@ -43,13 +43,13 @@ namespace TGCommandLine if (!DM.Compile()) { - Console.WriteLine("Error: Unable to start compilation!"); + OutputProc("Error: Unable to start compilation!"); var err = DM.CompileError(); if (err != null) - Console.WriteLine(err); + OutputProc(err); return ExitCode.ServerError; } - Console.WriteLine("Compile job started"); + OutputProc("Compile job started"); if (parameters.Count > 0 && parameters[0] == "--wait") { do @@ -57,19 +57,19 @@ namespace TGCommandLine Thread.Sleep(1000); } while (DM.GetStatus() == TGCompilerStatus.Compiling); var res = DM.CompileError(); - Console.WriteLine(res ?? "Compilation successful"); + OutputProc(res ?? "Compilation successful"); if (res != null) return ExitCode.ServerError; } return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return "[--wait]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Starts a compile/update job optionally waiting for completion"; } @@ -85,38 +85,38 @@ namespace TGCommandLine { var error = Server.GetComponent().CompileError(); if (error != null) - Console.WriteLine("Last error: " + error); + OutputProc("Last error: " + error); } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var DM = Server.GetComponent(); - Console.WriteLine(String.Format("Target Project: /{0}.dme", DM.ProjectName())); + OutputProc(String.Format("Target Project: /{0}.dme", DM.ProjectName())); Console.Write("Compilier is currently: "); switch (DM.GetStatus()) { case TGCompilerStatus.Compiling: - Console.WriteLine("Compiling..."); + OutputProc("Compiling..."); break; case TGCompilerStatus.Initialized: - Console.WriteLine("Idle"); + OutputProc("Idle"); ShowError(); break; case TGCompilerStatus.Initializing: - Console.WriteLine("Setting up..."); + OutputProc("Setting up..."); break; case TGCompilerStatus.Uninitialized: - Console.WriteLine("Uninitialized"); + OutputProc("Uninitialized"); ShowError(); break; default: - Console.WriteLine("Seizing the means of production (This is an error)."); + OutputProc("Seizing the means of production (This is an error)."); return ExitCode.ServerError; } return ExitCode.Normal; } - protected override string GetHelpText() + public override string GetHelpText() { return "Get the current status of the compiler"; } @@ -129,17 +129,17 @@ namespace TGCommandLine Keyword = "project-name"; RequiredParameters = 1; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Set the relative path of the .dme/.dmb to compile/run"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { Server.GetComponent().SetProjectName(parameters[0]); return ExitCode.Normal; @@ -153,34 +153,34 @@ namespace TGCommandLine Keyword = "initialize"; } - protected override string GetArgumentString() + public override string GetArgumentString() { return "[--wait]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Starts an initialization job optionally waiting for completion"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var DM = Server.GetComponent(); var stat = DM.GetStatus(); if (stat == TGCompilerStatus.Compiling || stat == TGCompilerStatus.Initializing) { - Console.WriteLine("Error: Compiler is " + ((stat == TGCompilerStatus.Initializing) ? "already initialized!" : " already running!")); + OutputProc("Error: Compiler is " + ((stat == TGCompilerStatus.Initializing) ? "already initialized!" : " already running!")); return ExitCode.ServerError; } if (!DM.Initialize()) { - Console.WriteLine("Error: Unable to start initialization!"); + OutputProc("Error: Unable to start initialization!"); var err = DM.CompileError(); if (err != null) - Console.WriteLine(err); + OutputProc(err); return ExitCode.ServerError; } - Console.WriteLine("Initialize job started"); + OutputProc("Initialize job started"); if (parameters.Count > 0 && parameters[0] == "--wait") { do @@ -188,7 +188,7 @@ namespace TGCommandLine Thread.Sleep(1000); } while (DM.GetStatus() == TGCompilerStatus.Initializing); var res = DM.CompileError(); - Console.WriteLine(res ?? "Initialization successful"); + OutputProc(res ?? "Initialization successful"); if (res != null) return ExitCode.ServerError; } @@ -203,14 +203,14 @@ namespace TGCommandLine Keyword = "cancel"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var res = Server.GetComponent().Cancel(); - Console.WriteLine(res ?? "Success!"); + OutputProc(res ?? "Success!"); return ExitCode.Normal; //because failing cancellation implys it's already cancelled } - protected override string GetHelpText() + public override string GetHelpText() { return "Cancels the current compilation job"; } diff --git a/TGCommandLine/Program.cs b/TGCommandLine/Program.cs index b136a58a3c..dc44483132 100644 --- a/TGCommandLine/Program.cs +++ b/TGCommandLine/Program.cs @@ -5,49 +5,6 @@ using TGServiceInterface; namespace TGCommandLine { - enum ExitCode - { - Normal = 0, - ConnectionError = 1, - BadCommand = 2, - ServerError = 3, - } - - abstract class Command - { - public string Keyword { get; protected set; } - public Command[] Children { get; protected set; } = { }; - public int RequiredParameters { get; protected set; } - public abstract ExitCode Run(IList parameters); - public virtual void PrintHelp() - { - var Prefixes = new List(); - var Postfixes = new List(); - int MaxPrefixLen = 0; - foreach (var c in Children) - { - var ns = c.Keyword + " " + c.GetArgumentString(); - MaxPrefixLen = Math.Max(MaxPrefixLen, ns.Length); - Prefixes.Add(ns); - Postfixes.Add(c.GetHelpText()); - } - - var Final = new List(); - for(var I = 0; I < Prefixes.Count; ++I) - { - var lp = Prefixes[I]; - for (; lp.Length < MaxPrefixLen + 1; lp += " ") ; - Final.Add(lp + "- " + Postfixes[I]); - } - Final.Sort(); - Final.ForEach(Console.WriteLine); - } - protected virtual string GetArgumentString() - { - return ""; - } - protected abstract string GetHelpText(); - } class Program { @@ -61,7 +18,7 @@ namespace TGCommandLine } try { - return new RootCommand().Run(argsAsList); + return new CLICommand().DoRun(argsAsList); } catch (Exception e) { @@ -98,6 +55,7 @@ namespace TGCommandLine static int Main(string[] args) { + Command.OutputProcVar.Value = Console.WriteLine; if (args.Length != 0) return (int)RunCommandLine(new List(args)); diff --git a/TGCommandLine/RepoCommands.cs b/TGCommandLine/RepoCommands.cs index 5184126fbb..32c8cf5825 100644 --- a/TGCommandLine/RepoCommands.cs +++ b/TGCommandLine/RepoCommands.cs @@ -11,7 +11,7 @@ namespace TGCommandLine Keyword = "repo"; Children = new Command[] { new RepoSetupCommand(), new RepoUpdateCommand(), new RepoChangelogCommand(), new RepoPythonPathCommand(), new RepoSetEmailCommand(), new RepoSetNameCommand(), new RepoMergePRCommand(), new RepoListPRsCommand(), new RepoStatusCommand(), new RepoListBackupsCommand(), new RepoCheckoutCommand(), new RepoResetCommand() }; } - protected override string GetHelpText() + public override string GetHelpText() { return "Manage the git repository"; } @@ -24,22 +24,22 @@ namespace TGCommandLine Keyword = "setup"; RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var res = Server.GetComponent().Setup(parameters[0], parameters.Count > 1 ? parameters[1] : "master"); if (res != null) { - Console.WriteLine("Error: " + res); + OutputProc("Error: " + res); return ExitCode.ServerError; } - Console.WriteLine("Setting up repo. This will take a while..."); + OutputProc("Setting up repo. This will take a while..."); return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return " [branchname]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Clean up everything and clones the repo at git-url with optional branch name"; } @@ -51,13 +51,13 @@ namespace TGCommandLine { Keyword = "status"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var Repo = Server.GetComponent(); var busy = Repo.OperationInProgress(); if (!busy) { - Console.WriteLine("Repo: Idle"); + OutputProc("Repo: Idle"); var head = Repo.GetHead(out string error); if (head == null) head = "Error: " + error; @@ -67,13 +67,13 @@ namespace TGCommandLine var remote = Repo.GetRemote(out error); if (remote == null) remote = "Error: " + error; - Console.WriteLine("Remote: " + remote); - Console.WriteLine("Branch: " + branch); - Console.WriteLine("HEAD: " + head); + OutputProc("Remote: " + remote); + OutputProc("Branch: " + branch); + OutputProc("HEAD: " + head); } else { - Console.WriteLine("Repo: Busy"); + OutputProc("Repo: Busy"); var progress = Repo.CheckoutProgress(); if (progress != -1) { @@ -83,12 +83,12 @@ namespace TGCommandLine var dshs = ""; for (var I = 0; I < 10 - (progress / 10); ++I) eqs += "-"; - Console.WriteLine(String.Format("Progress: [{0}{1}] {2}%", eqs, dshs, progress)); + OutputProc(String.Format("Progress: [{0}{1}] {2}%", eqs, dshs, progress)); } } return ExitCode.Normal; } - protected override string GetHelpText() + public override string GetHelpText() { return "Shows the busy status of the repo, remote, branch, and HEAD information"; } @@ -100,17 +100,17 @@ namespace TGCommandLine { Keyword = "reset"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var result = Server.GetComponent().Reset(parameters.Count > 0 && parameters[0].ToLower() == "--origin"); - Console.WriteLine(result ?? "Success!"); + OutputProc(result ?? "Success!"); return result == null ? ExitCode.Normal : ExitCode.ServerError; } - protected override string GetArgumentString() + public override string GetArgumentString() { return "[--origin]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Hard resets the repo. If a target is specified, the current branch is reset to that branch"; } @@ -123,7 +123,7 @@ namespace TGCommandLine Keyword = "update"; RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { bool hard; switch (parameters[0].ToLower()) @@ -135,18 +135,18 @@ namespace TGCommandLine hard = false; break; default: - Console.WriteLine("Invalid parameter: " + parameters[0]); + OutputProc("Invalid parameter: " + parameters[0]); return ExitCode.BadCommand; } var res = Server.GetComponent().Update(hard); - Console.WriteLine(res ?? "Success"); + OutputProc(res ?? "Success"); return res == null ? ExitCode.Normal : ExitCode.ServerError; } - protected override string GetHelpText() + public override string GetHelpText() { return "Updates the current branch the repo is on either via a merge or hard reset"; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } @@ -157,16 +157,16 @@ namespace TGCommandLine { Keyword = "gen-changelog"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var result = Server.GetComponent().GenerateChangelog(out string error); - Console.WriteLine(error ?? "Success!"); + OutputProc(error ?? "Success!"); if (result != null) - Console.WriteLine(result); + OutputProc(result); return error == null ? ExitCode.Normal : ExitCode.ServerError; } - protected override string GetHelpText() + public override string GetHelpText() { return "Compiles the html changelog"; } @@ -178,17 +178,17 @@ namespace TGCommandLine Keyword = "set-email"; RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { Server.GetComponent().SetCommitterEmail(parameters[0]); return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Set the e-mail used for commits"; } @@ -200,16 +200,16 @@ namespace TGCommandLine Keyword = "set-name"; RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { Server.GetComponent().SetCommitterName(parameters[0]); return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Set the name used for commits"; } @@ -221,16 +221,16 @@ namespace TGCommandLine Keyword = "python-path"; RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { Server.GetComponent().SetPythonPath(parameters[0]); return ExitCode.Normal; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Set the path to the folder containing the python 2.7 installation"; } @@ -244,7 +244,7 @@ namespace TGCommandLine RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { ushort PR; try @@ -253,19 +253,19 @@ namespace TGCommandLine } catch { - Console.WriteLine("Invalid PR Number!"); + OutputProc("Invalid PR Number!"); return ExitCode.BadCommand; } var res = Server.GetComponent().MergePullRequest(PR); - Console.WriteLine(res ?? "Success"); + OutputProc(res ?? "Success"); return res == null ? ExitCode.Normal : ExitCode.ServerError; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Merge the given pull request from the origin repository into the current branch. Only supported with github remotes"; } @@ -277,23 +277,23 @@ namespace TGCommandLine { Keyword = "list-prs"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Lists currently merge pull requests"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var data = Server.GetComponent().MergedPullRequests(out string error); if (data == null) { - Console.WriteLine(error); + OutputProc(error); return ExitCode.ServerError; } if (data.Count == 0) - Console.WriteLine("None!"); + OutputProc("None!"); else foreach (var I in data) - Console.WriteLine(String.Format("#{0}: {2} by {3} at commit {1}", I.Number, I.Sha, I.Title, I.Author)); + OutputProc(String.Format("#{0}: {2} by {3} at commit {1}", I.Number, I.Sha, I.Title, I.Author)); return ExitCode.Normal; } } @@ -304,23 +304,23 @@ namespace TGCommandLine { Keyword = "list-backups"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Lists backup tags created by compilation"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var data = Server.GetComponent().ListBackups(out string error); if (data == null) { - Console.WriteLine(error); + OutputProc(error); return ExitCode.ServerError; } if (data.Count == 0) - Console.WriteLine("None!"); + OutputProc("None!"); else foreach (var I in data) - Console.WriteLine(String.Format("{0} at commit {1}", I.Key, I.Value)); + OutputProc(String.Format("{0} at commit {1}", I.Key, I.Value)); return ExitCode.Normal; } } @@ -331,14 +331,14 @@ namespace TGCommandLine Keyword = "checkout"; RequiredParameters = 1; } - protected override string GetHelpText() + public override string GetHelpText() { return "Checks out the targeted object"; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var res = Server.GetComponent().Checkout(parameters[0]); - Console.WriteLine(res ?? "Success"); + OutputProc(res ?? "Success"); return res == null ? ExitCode.Normal : ExitCode.ServerError; } } diff --git a/TGCommandLine/RootCommands.cs b/TGCommandLine/RootCommands.cs index 38271c96c9..4a4c6d1e0a 100644 --- a/TGCommandLine/RootCommands.cs +++ b/TGCommandLine/RootCommands.cs @@ -1,74 +1,22 @@ using System; using System.Collections.Generic; -using System.Threading; using TGServiceInterface; namespace TGCommandLine { - - class RootCommand : Command + class CLICommand : RootCommand { - bool IsRealRoot() + public CLICommand() { - return !GetType().IsSubclassOf(typeof(RootCommand)); - } - public RootCommand() - { - if (IsRealRoot()) //stack overflows - Children = new Command[] { new UpdateCommand(), new TestmergeCommand(), new RepoCommand(), new BYONDCommand(), new DMCommand(), new DDCommand(), new ConfigCommand(), new IRCCommand(), new DiscordCommand() }; - } - public override ExitCode Run(IList parameters) - { - if (parameters.Count > 0) - { - var LocalKeyword = parameters[0].Trim().ToLower(); - parameters.RemoveAt(0); - - switch (LocalKeyword) - { - case "help": - case "?": - if (!IsRealRoot()) - { - Console.WriteLine(Keyword + " commands:"); - Console.WriteLine(); - } - PrintHelp(); - return ExitCode.Normal; - default: - foreach (var c in Children) - if (c.Keyword == LocalKeyword) - { - if (parameters.Count < c.RequiredParameters) - { - Console.WriteLine("Not enough parameters!"); - return ExitCode.BadCommand; - } - return c.Run(parameters); - } - parameters.Insert(0, LocalKeyword); - break; - } - } - Console.WriteLine(String.Format("Invalid command: {0} {1}", Keyword, String.Join(" ", parameters))); - Console.WriteLine(String.Format("Type '{0}?' or '{0}help' for available commands.", Keyword != null ? Keyword + " " : "")); - return ExitCode.BadCommand; + Children = new Command[] { new UpdateCommand(), new TestmergeCommand(), new RepoCommand(), new BYONDCommand(), new DMCommand(), new DDCommand(), new ConfigCommand(), new IRCCommand(), new DiscordCommand() }; } public override void PrintHelp() { - Console.WriteLine("/tg/station 13 Server Command Line"); - Console.WriteLine("Available commands (type '?' or 'help' after command for more info):"); - Console.WriteLine(); + OutputProc("/tg/station 13 Server Command Line"); base.PrintHelp(); } - - protected override string GetHelpText() - { - throw new NotImplementedException(); - } } - class UpdateCommand : Command { public UpdateCommand() @@ -76,7 +24,7 @@ namespace TGCommandLine Keyword = "update"; RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { var gen_cl = parameters.Count > 1 && parameters[1].ToLower() == "--cl"; TGRepoUpdateMethod method; @@ -89,24 +37,23 @@ namespace TGCommandLine method = TGRepoUpdateMethod.Merge; break; default: - Console.WriteLine("Please specify hard or merge"); + OutputProc("Please specify hard or merge"); return ExitCode.BadCommand; } var result = Server.GetComponent().UpdateServer(method, gen_cl); - Console.WriteLine(result ?? "Compilation started!"); + OutputProc(result ?? "Compilation started!"); return result == null ? ExitCode.Normal : ExitCode.ServerError; } - protected override string GetArgumentString() + public override string GetArgumentString() { return " [--cl]"; } - protected override string GetHelpText() + public override string GetHelpText() { return "Updates the server fully, optionally generating and pushing a changelog. Runs asynchronously once compilation starts"; } - } class TestmergeCommand : Command @@ -116,7 +63,7 @@ namespace TGCommandLine Keyword = "testmerge"; RequiredParameters = 1; } - public override ExitCode Run(IList parameters) + protected override ExitCode Run(IList parameters) { ushort tm; try @@ -127,19 +74,19 @@ namespace TGCommandLine } catch { - Console.WriteLine("Invalid tesmerge #: " + parameters[0]); + OutputProc("Invalid tesmerge #: " + parameters[0]); return ExitCode.BadCommand; } var result = Server.GetComponent().UpdateServer(TGRepoUpdateMethod.None, false, tm); - Console.WriteLine(result ?? "Compilation started!"); + OutputProc(result ?? "Compilation started!"); return result == null ? ExitCode.Normal : ExitCode.ServerError; } - protected override string GetArgumentString() + public override string GetArgumentString() { return ""; } - protected override string GetHelpText() + public override string GetHelpText() { return "Merges the specified pull request and updates the server"; } diff --git a/TGServerService/Chat.cs b/TGServerService/Chat.cs index 43133511c1..7d3ab39b8b 100644 --- a/TGServerService/Chat.cs +++ b/TGServerService/Chat.cs @@ -129,7 +129,7 @@ namespace TGServerService private void ChatProvider_OnChatMessage(ITGChatProvider ChatProvider, string speaker, string channel, string message, bool isAdmin, bool isAdminChannel) { var splits = message.Trim().Split(' '); - + if (splits.Length == 1 && splits[0] == "") { ChatProvider.SendMessageDirect("Hi!", channel); @@ -137,21 +137,19 @@ namespace TGServerService } var asList = new List(splits); - var command = asList[0].ToLower(); - asList.RemoveAt(0); - ChatProvider.SendMessageDirect(ChatCommand(command, speaker, channel, asList, isAdmin, isAdminChannel), channel); + Command.OutputProcVar.Value = (m) => ChatProvider.SendMessageDirect(m, channel); + ChatCommand.CommandInfo.Value = new CommandInfo() + { + IsAdmin = isAdmin, + IsAdminChannel = isAdminChannel, + Speaker = speaker, + Server = this, + }; + TGServerService.WriteInfo(String.Format("Chat Command from {0} ({2}): {1}", speaker, String.Join(" ", asList), channel), TGServerService.EventID.ChatCommand); + new RootChatCommand().DoRun(asList); } - string HasChatAdmin(bool isAdmin, bool isAdminChannel) - { - if (!isAdmin) - return "You are not authorized to use that command!"; - if (!isAdminChannel) - return "Use this command in an admin channel!"; - return null; - } - //cleanup and save void DisposeChat() { @@ -173,58 +171,6 @@ namespace TGServerService Config.ChatProviderEntropy = entrp; } - //Do stuff with words that were spoken to us - string ChatCommand(string command, string speaker, string channel, IList parameters, bool isAdmin, bool isAdminChannel) - { - TGServerService.WriteInfo(String.Format("Chat Command from {0}: {1} {2}", speaker, command, String.Join(" ", parameters)), TGServerService.EventID.ChatCommand); - var adminmessage = HasChatAdmin(isAdmin, isAdminChannel); - switch (command) - { - case "check": - return StatusString(adminmessage == null); - case "byond": - if (parameters.Count > 0) - if (parameters[0].ToLower() == "--staged") - return GetVersion(TGByondVersion.Staged) ?? "None"; - else if (parameters[0].ToLower() == "--latest") - return GetVersion(TGByondVersion.Latest) ?? "Unknown"; - return GetVersion(TGByondVersion.Installed) ?? "Uninstalled"; - case "status": - return adminmessage ?? SendCommand(SCIRCStatus); - case "adminwho": - return adminmessage ?? SendCommand(SCAdminWho); - case "ahelp": - if (adminmessage != null) - return adminmessage; - if (parameters.Count < 2) - return "Usage: ahelp "; - var ckey = parameters[0]; - parameters.RemoveAt(0); - return SendPM(ckey, speaker, String.Join(" ", parameters)); - case "namecheck": - if (adminmessage != null) - return adminmessage; - if (parameters.Count < 1) - return "Usage: namecheck "; - return NameCheck(parameters[0], speaker); - case "prs": - var PRs = MergedPullRequests(out string res); - if (PRs == null) - return res; - if (PRs.Count == 0) - return "None!"; - res = ""; - foreach(var I in PRs) - res += I.Number + " "; - return res; - case "version": - return TGServerService.Version; - case "kek": - return "kek"; - } - return "Unknown command: " + command; - } - public IList ProviderInfos() { var infosList = new List(); diff --git a/TGServerService/ChatCommands.cs b/TGServerService/ChatCommands.cs new file mode 100644 index 0000000000..7ef47569b5 --- /dev/null +++ b/TGServerService/ChatCommands.cs @@ -0,0 +1,265 @@ +using TGServiceInterface; +using System.Threading; +using System.Collections.Generic; +using System; + +namespace TGServerService +{ + class CommandInfo + { + public bool IsAdmin { get; set; } + public bool IsAdminChannel { get; set; } + public string Speaker { get; set; } + public TGStationServer Server { get; set; } + } + abstract class ChatCommand : Command + { + public bool RequiresAdmin { get; protected set; } + public static ThreadLocal CommandInfo = new ThreadLocal(); + protected TGStationServer Instance { get { return CommandInfo.Value.Server; } } + public override ExitCode DoRun(IList parameters) + { + if (RequiresAdmin) + { + var Info = CommandInfo.Value; + if (!Info.IsAdmin) + { + OutputProc("You are not authorized to use that command!"); + return ExitCode.BadCommand; + } + if (!Info.IsAdminChannel) + { + OutputProc("Use this command in an admin channel!"); + return ExitCode.BadCommand; + } + } + return base.DoRun(parameters); + } + } + class RootChatCommand : RootCommand + { + public RootChatCommand() + { + PrintHelpList = true; + Children = new Command[] { new CheckCommand(), new StatusCommand(), new PRsCommand(), new VersionCommand(), new AHelpCommand(), new NameCheckCommand(), new RevisionCommand(), new AdminWhoCommand(), new ByondCommand(), new KekCommand() }; + } + } + class RevisionCommand : ChatCommand + { + public RevisionCommand() + { + Keyword = "revision"; + } + protected override ExitCode Run(IList parameters) + { + OutputProc(Instance.GetHead(out string error) ?? error); + return error == null ? ExitCode.Normal : ExitCode.ServerError; + } + + public override string GetHelpText() + { + return "Returns mob info of the specified target"; + } + } + class NameCheckCommand : ChatCommand + { + public NameCheckCommand() + { + Keyword = "namecheck"; + RequiredParameters = 1; + RequiresAdmin = true; + } + protected override ExitCode Run(IList parameters) + { + OutputProc(Instance.NameCheck(parameters[0], CommandInfo.Value.Speaker)); + return ExitCode.Normal; + } + + public override string GetHelpText() + { + return "Returns mob info of the specified target"; + } + public override string GetArgumentString() + { + return ""; + } + } + class AdminWhoCommand : ChatCommand + { + public AdminWhoCommand() + { + Keyword = "adminwho"; + RequiresAdmin = true; + } + protected override ExitCode Run(IList parameters) + { + OutputProc(Instance.SendCommand(TGStationServer.SCAdminWho)); + return ExitCode.Normal; + } + + public override string GetHelpText() + { + return "Returns mob info of the specified target"; + } + public override string GetArgumentString() + { + return ""; + } + } + + class ByondCommand : ChatCommand + { + public ByondCommand() + { + Keyword = "byond"; + } + protected override ExitCode Run(IList parameters) + { + if (parameters.Count > 0) + if (parameters[0].ToLower() == "--staged") + OutputProc(Instance.GetVersion(TGByondVersion.Staged) ?? "None"); + else if (parameters[0].ToLower() == "--latest") + OutputProc(Instance.GetVersion(TGByondVersion.Latest) ?? "Unknown"); + else + OutputProc(Instance.GetVersion(TGByondVersion.Installed) ?? "Uninstalled"); + return ExitCode.Normal; + } + + public override string GetHelpText() + { + return "Gets the specified BYOND version"; + } + public override string GetArgumentString() + { + return "[--staged|--latest]"; + } + } + class CheckCommand : ChatCommand + { + public CheckCommand() + { + Keyword = "check"; + } + protected override ExitCode Run(IList parameters) + { + OutputProc(Instance.StatusString(CommandInfo.Value.IsAdmin && CommandInfo.Value.IsAdminChannel)); + return ExitCode.Normal; + } + + public override string GetHelpText() + { + return "Gets the playercount, gamemode, and address of the server"; + } + } + + class StatusCommand : ChatCommand + { + public StatusCommand() + { + Keyword = "status"; + RequiresAdmin = true; + } + protected override ExitCode Run(IList parameters) + { + OutputProc(Instance.SendCommand(TGStationServer.SCIRCStatus)); + return ExitCode.Normal; + } + + public override string GetHelpText() + { + return "Gets the admincount, playercount, gamemode, and true game mode of the server"; + } + } + class VersionCommand : ChatCommand + { + public VersionCommand() + { + Keyword = "version"; + } + protected override ExitCode Run(IList parameters) + { + OutputProc(Instance.Version()); + return ExitCode.Normal; + } + + public override string GetHelpText() + { + return "Gets the running service version"; + } + } + class KekCommand : ChatCommand + { + public KekCommand() + { + Keyword = "kek"; + } + protected override ExitCode Run(IList parameters) + { + OutputProc("kek"); + return ExitCode.Normal; + } + + public override string GetHelpText() + { + return "kek"; + } + } + class PRsCommand : ChatCommand + { + public PRsCommand() + { + Keyword = "prs"; + } + protected override ExitCode Run(IList parameters) + { + var PRs = Instance.MergedPullRequests(out string res); + if (PRs == null) + { + OutputProc(res); + return ExitCode.ServerError; + } + if (PRs.Count == 0) + OutputProc("None!"); + else + { + res = ""; + foreach (var I in PRs) + res += I.Number + " "; + OutputProc(res); + } + return ExitCode.Normal; + } + + public override string GetHelpText() + { + return "Gets the currently merged pull requests in the repository"; + } + } + + class AHelpCommand : ChatCommand + { + public AHelpCommand() + { + Keyword = "ahelp"; + RequiresAdmin = true; + RequiredParameters = 2; + } + protected override ExitCode Run(IList parameters) + { + var ckey = parameters[0]; + parameters.RemoveAt(0); + OutputProc(Instance.SendPM(ckey, CommandInfo.Value.Speaker, String.Join(" ", parameters))); + return ExitCode.Normal; + } + + public override string GetHelpText() + { + return "Respond to a relayed admin help request"; + } + + public override string GetArgumentString() + { + return " |list>>"; + } + } +} diff --git a/TGServerService/InterfaceBase.cs b/TGServerService/InterfaceBase.cs index 93c7969894..9b800f2c38 100644 --- a/TGServerService/InterfaceBase.cs +++ b/TGServerService/InterfaceBase.cs @@ -97,6 +97,7 @@ namespace TGServerService public void PrepareForUpdate() { Properties.Settings.Default.ReattachToDD = true; + SendMessage("SERVICE: Update started...", ChatMessageType.DeveloperInfo); } //mostly generated code with a call to RunDisposals() diff --git a/TGServerService/Interop.cs b/TGServerService/Interop.cs index ee606c7a27..7a62289d34 100644 --- a/TGServerService/Interop.cs +++ b/TGServerService/Interop.cs @@ -24,11 +24,11 @@ namespace TGServerService const string SCHardReboot = "hard_reboot"; //requests that dreamdaemon restarts when the round ends const string SCGracefulShutdown = "graceful_shutdown"; //requests that dreamdaemon stops when the round ends const string SCWorldAnnounce = "world_announce"; //sends param 'message' to the world - const string SCIRCCheck = "irc_check"; //returns game stats - const string SCIRCStatus = "irc_status"; //returns admin stats - const string SCNameCheck = "namecheck"; //returns keywords lookup - const string SCAdminPM = "adminmsg"; //pms a target ckey - const string SCAdminWho = "adminwho"; //lists admins + public const string SCIRCCheck = "irc_check"; //returns game stats + public const string SCIRCStatus = "irc_status"; //returns admin stats + public const string SCNameCheck = "namecheck"; //returns keywords lookup + const string SCAdminPM = "adminmsg"; //pms a target ckey + public const string SCAdminWho = "adminwho"; //lists admins const string SRKillProcess = "killme"; const string SRIRCBroadcast = "irc"; @@ -66,8 +66,8 @@ namespace TGServerService break; } } - - string SendCommand(string cmd) + + public string SendCommand(string cmd) { lock (watchdogLock) { @@ -82,12 +82,12 @@ namespace TGServerService return SendCommand(SCWorldAnnounce + ";message=" + message) == "SUCCESS" ; } - string SendPM(string targetCkey, string sender, string message) + public string SendPM(string targetCkey, string sender, string message) { return SendCommand(String.Format("{3};target={0};sender={1};message={2}", targetCkey, sender, message, SCAdminPM)); } - string NameCheck(string targetCkey, string sender) + public string NameCheck(string targetCkey, string sender) { return SendCommand(String.Format("{2};target={0};sender={1}", targetCkey, sender, SCNameCheck)); } diff --git a/TGServerService/TGServerService.csproj b/TGServerService/TGServerService.csproj index 55d8dbed62..64f164c9a6 100644 --- a/TGServerService/TGServerService.csproj +++ b/TGServerService/TGServerService.csproj @@ -85,6 +85,7 @@ + diff --git a/TGServiceInterface/Command.cs b/TGServiceInterface/Command.cs new file mode 100644 index 0000000000..55c09830d7 --- /dev/null +++ b/TGServiceInterface/Command.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace TGServiceInterface +{ + public enum ExitCode + { + Normal = 0, + ConnectionError = 1, + BadCommand = 2, + ServerError = 3, + } + public abstract class Command + { + public static ThreadLocal> OutputProcVar = new ThreadLocal>(); + protected static void OutputProc(string message) + { + OutputProcVar.Value(message); + } + public string Keyword { get; protected set; } + public Command[] Children { get; protected set; } = { }; + public int RequiredParameters { get; protected set; } + public virtual ExitCode DoRun(IList parameters) + { + return Run(parameters); + } + protected abstract ExitCode Run(IList parameters); + public virtual void PrintHelp() + { + var argstr = GetArgumentString(); + OutputProc(String.Format("{0} {1}- {2}", Keyword, argstr.Length > 0 ? argstr + " " : "", GetHelpText())); + } + public virtual string GetArgumentString() + { + return ""; + } + public abstract string GetHelpText(); + } + + public class RootCommand : Command + { + public static bool PrintHelpList = false; + protected override ExitCode Run(IList parameters) + { + if (parameters.Count > 0) + { + var LocalKeyword = parameters[0].Trim().ToLower(); + parameters.RemoveAt(0); + switch (LocalKeyword) + { + case "help": + case "?": + PrintHelp(); + return ExitCode.Normal; + default: + foreach (var c in Children) + if (c.Keyword == LocalKeyword) + { + if(parameters.Count> 0) + { + var possibleHelp = parameters[0].ToLower(); + if (possibleHelp == "help" || possibleHelp == "?") + { + c.PrintHelp(); + return ExitCode.Normal; + } + } + if (parameters.Count < c.RequiredParameters) + { + OutputProc("Not enough parameters!"); + return ExitCode.BadCommand; + } + return c.DoRun(parameters); + } + parameters.Insert(0, LocalKeyword); + break; + } + } + OutputProc(String.Format("Invalid command: {0} {1}", Keyword, String.Join(" ", parameters))); + OutputProc(String.Format("Type '{0}?' or '{0}help' for available commands.", Keyword != null ? Keyword + " " : "")); + return ExitCode.BadCommand; + } + public override void PrintHelp() + { + var Final = new List(); + if (PrintHelpList) + { + foreach (var c in Children) + Final.Add(c.Keyword); + OutputProc("Available commands (type '?' or 'help' after command for more info): " + String.Join(", ", Final)); + } + else + { + var Prefixes = new List(); + var Postfixes = new List(); + int MaxPrefixLen = 0; + foreach (var c in Children) + { + var ns = c.Keyword + " " + c.GetArgumentString(); + MaxPrefixLen = Math.Max(MaxPrefixLen, ns.Length); + Prefixes.Add(ns); + Postfixes.Add(c.GetHelpText()); + } + + for (var I = 0; I < Prefixes.Count; ++I) + { + var lp = Prefixes[I]; + for (; lp.Length < MaxPrefixLen + 1; lp += " ") ; + Final.Add(lp + "- " + Postfixes[I]); + } + Final.Sort(); + Final.ForEach(OutputProc); + } + } + + public override string GetHelpText() + { + throw new NotImplementedException(); + } + } +} diff --git a/TGServiceInterface/TGServiceInterface.csproj b/TGServiceInterface/TGServiceInterface.csproj index 3f501150f9..7d247f5d09 100644 --- a/TGServiceInterface/TGServiceInterface.csproj +++ b/TGServiceInterface/TGServiceInterface.csproj @@ -40,6 +40,7 @@ +