From ceff940905962ae28b0fad92124eb593d3c3eac7 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 15 Jun 2024 18:49:14 +0100 Subject: [PATCH] fix: simple help command, improvements to the operational flow --- .vscode/tasks.json | 12 +++++ RTWLibPlus/helpers/exArray.cs | 2 +- RTWLib_CLI/CLIHelper.cs | 24 +++++++++ RTWLib_CLI/Program.cs | 9 +++- RTWLib_CLI/cmd/cmdProcess.cs | 65 ++++++++--------------- RTWLib_CLI/cmd/help.cs | 59 --------------------- RTWLib_CLI/cmd/modules/help.cs | 34 ++++++++++++ RTWLib_CLI/cmd/screens/templates.cs | 82 +++++++++++++++++++++++++++++ RTWLib_CLI/input/input.cs | 6 +++ RTWLib_CLI/input/inputDialog.cs | 18 +++++++ RTWLib_CLI/keywords.cs | 1 + 11 files changed, 206 insertions(+), 106 deletions(-) create mode 100644 .vscode/tasks.json delete mode 100644 RTWLib_CLI/cmd/help.cs create mode 100644 RTWLib_CLI/cmd/modules/help.cs create mode 100644 RTWLib_CLI/cmd/screens/templates.cs diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..ed30564 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,12 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "shell", + "command": "dotnet build /Users/aaron/Repositories/RTWLibTools/RTWLib_CLI/RTWLib_CLI.csproj --output \"/Users/aaron/Library/Application Support/Feral Interactive/Total War ROME REMASTERED/\" /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary /p:Configuration=Debug /p:Platform=\"AnyCPU\"", + "group": "build", + "problemMatcher": [], + "label": "dotnet: build" + } + ] +} diff --git a/RTWLibPlus/helpers/exArray.cs b/RTWLibPlus/helpers/exArray.cs index 9490488..698c2c8 100644 --- a/RTWLibPlus/helpers/exArray.cs +++ b/RTWLibPlus/helpers/exArray.cs @@ -375,7 +375,7 @@ public static string ArrayToString(this string[] array, bool idx = false, bool i { for (int nl = 0; nl < newLineCount; nl++) { - value += "\r\n"; + value += "\n"; } } else if (insertSeperator) diff --git a/RTWLib_CLI/CLIHelper.cs b/RTWLib_CLI/CLIHelper.cs index bdd82e4..6d7465a 100644 --- a/RTWLib_CLI/CLIHelper.cs +++ b/RTWLib_CLI/CLIHelper.cs @@ -1,6 +1,7 @@ namespace RTWLib_CLI; using System; +using System.Reflection; public static class CLIHelper { @@ -10,4 +11,27 @@ public static void ScreenChange(string title) Console.WriteLine(title); Console.WriteLine(); } + + public static string ScreenChangeRTN(string title) => title + "\n"; + + + public static string GetMethodList(Type type) + { + string list = string.Empty; + MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); + int i = -1; + foreach (MethodInfo method in methods) + { + i++; + if (i == 0) + { + continue; + } + + string methodName = method.ToString().Split(' ')[1]; + + list += string.Format("{0}: {1}{2}", i.ToString(), methodName, "\n"); + } + return list; + } } diff --git a/RTWLib_CLI/Program.cs b/RTWLib_CLI/Program.cs index 1c9f49a..25af418 100644 --- a/RTWLib_CLI/Program.cs +++ b/RTWLib_CLI/Program.cs @@ -9,6 +9,7 @@ using System; using System.IO; + internal class Program { private static readonly string Title = "Welcome to the RTWLib CLI\n By Sargeant Pig\n---\ntype 'help' for commands and usage"; @@ -17,10 +18,10 @@ internal class Program private static void Main(string[] args) { + Console.WindowWidth = 100; string wdir = AppDomain.CurrentDomain.BaseDirectory; Directory.SetCurrentDirectory(wdir); - Console.WriteLine(CMDProcess.LoadTemplates()); Console.WriteLine(CMDProcess.LoadConfigs()); if (CMDProcess.configs.Count == 0) @@ -32,12 +33,16 @@ private static void Main(string[] args) int input = Input.GetIntInput(ConfigTitle, x => x >= 0 && x < CMDProcess.configs.Count); TWConfig config = TWConfig.LoadConfig(CMDProcess.configs[input]); RandCMD rand = new(config); + Help help = new(); Console.WriteLine("Config Loaded".ApplyBorder('#', 1, 1)); CMDProcess.modules.RegisterModule(rand); + CMDProcess.modules.RegisterModule(help); + + Console.WriteLine(CMDProcess.CMDScreener("templates")); //Rand.InitialSetup(); while (true) { - string ret = CMDProcess.ReadCMD(Console.ReadLine()); + string ret = CMDProcess.CMDScreener(Console.ReadLine()); if (ret != KW.back) { Console.WriteLine(ret.ApplyBorder('=', 1, 1)); continue; } diff --git a/RTWLib_CLI/cmd/cmdProcess.cs b/RTWLib_CLI/cmd/cmdProcess.cs index 4d1ca08..bf2e2b1 100644 --- a/RTWLib_CLI/cmd/cmdProcess.cs +++ b/RTWLib_CLI/cmd/cmdProcess.cs @@ -6,26 +6,19 @@ using RTWLibPlus.helpers; using System.IO; using RTWLibPlus.parsers; - +using RTWLib_CLI.cmd.screens; +using RTWLib_CLI.draw; +using System.Data; public static class CMDProcess { - public static Dictionary templates = []; + private static readonly Templates TemplatesManager = new(); public static Dictionary configs = []; public static ModuleRegister modules = new(); - public static string ReadCMD(string cmd, Type type = null) + private static string ReadCMD(string cmd, Type type = null) { - if (cmd == KW.back) - { return KW.back; } - if (cmd == KW.help) - { return Help.help(); } - if (cmd == string.Empty) - { return "no command"; } - if (templates.ContainsKey(cmd)) - { return ProcessTemplate(cmd); } - int invokeInd = 0; string[] cmdSplit = cmd.Split(' ', StringSplitOptions.RemoveEmptyEntries); @@ -42,6 +35,10 @@ public static string ReadCMD(string cmd, Type type = null) object invokableObject = modules.GetModule(type.Name); + if (cmdSplit.Length < 2) + { + return KW.error + ": No command specified"; + } foreach (MethodInfo t in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)) { @@ -84,39 +81,19 @@ public static string ReadCMD(string cmd, Type type = null) return KW.error + ": Command not found, are the arguments correct?"; } - public static string ProcessTemplate(string template) - { - string[] cmds = templates[template]; - Console.WriteLine("Running: " + template); - //Progress p = new(1f / cmds.Length, "Running: " + template); - foreach (string cmd in cmds) - { - Console.WriteLine("Doing: " + cmd); - //p.Message("Doing: " + cmd); - ReadCMD(cmd); - //p.Update("Complete"); - } - return "template finished processing"; - } - - public static string LoadTemplates() + public static string CMDScreener(string cmd, Type type = null) { - if (!Directory.Exists("randomiser_templates")) - { - return "Template folder does not exist. Skipping template loading"; - } - - string[] files = Directory.GetFiles("randomiser_templates"); + if (cmd == KW.back) + { return KW.back; } + if (cmd == string.Empty) + { return "no command"; } + if (cmd == KW.templates) + { return TemplatesManager.View_Templates(); } + if (cmd.Split(" ")[0] == "run") + { return TemplatesManager.Action(cmd); } - DepthParse dp = new(); + return ReadCMD(cmd, type); - foreach (string file in files) - { - string name = Path.GetFileName(file); - string[] parse = dp.ReadFile(file); - templates.Add(name, parse); - } - return "Templates Loaded"; } public static string LoadConfigs() @@ -134,8 +111,8 @@ public static string LoadConfigs() { string file = files[i]; string name = Path.GetFileName(file); - string parse = file; - configs.Add(i, parse); + string parse = name; + configs.Add(i, file); } return "Configs Loaded"; } diff --git a/RTWLib_CLI/cmd/help.cs b/RTWLib_CLI/cmd/help.cs deleted file mode 100644 index 94ccb58..0000000 --- a/RTWLib_CLI/cmd/help.cs +++ /dev/null @@ -1,59 +0,0 @@ -namespace RTWLib_CLI.cmd; - -using RTWLib_CLI.draw; -using RTWLibPlus.helpers; -using System; -using System.Reflection; - -public static class Help -{ - private static bool open; - private static string title; - public static string help() - { - string methods = GetMethodList(); - title = string.Format("RTWLib CLI\nHelp Screen\n---\nTry the following commands in\nformat [name] [arg1] [arg2] etc\n---\n{0}", methods).ApplyBorder('#', 2); - - if (open) - { - return "Already on Help screen"; - } - open = true; - - CLIHelper.ScreenChange(title); - while (true) - { - string ret = CMDProcess.ReadCMD(Console.ReadLine(), typeof(Help)); - if (ret == KW.back) - { open = false; return KW.back; } - Console.WriteLine(ret.ApplyBorder('~', 1, 1)); - } - } - - public static string Test(params string[] args) - { - string argsStr = args.ToString(','); - return "response: " + argsStr; - } - - private static string GetMethodList() - { - string list = string.Empty; - Type type = typeof(Help); - MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); - int i = -1; - foreach (MethodInfo method in methods) - { - i++; - if (i == 0) - { - continue; - } - - string methodName = method.ToString().Split(' ')[1]; - - list += string.Format("{0}: {1}{2}", i.ToString(), methodName, "\n"); - } - return list; - } -} diff --git a/RTWLib_CLI/cmd/modules/help.cs b/RTWLib_CLI/cmd/modules/help.cs new file mode 100644 index 0000000..653c61c --- /dev/null +++ b/RTWLib_CLI/cmd/modules/help.cs @@ -0,0 +1,34 @@ +namespace RTWLib_CLI.cmd.modules; + +using RTWLib_CLI; +using RTWLib_CLI.draw; +using RTWLibPlus.helpers; + +public class Help +{ + string title; + + public string help() + { + string methods = CLIHelper.GetMethodList(typeof(Help)); + title = string.Format("RTWLib CLI\nHelp\n---\nTry the following commands in\nformat [name] [arg1] [arg2] etc\n---\n{0}", methods).ApplyBorder('#', 2); + + return CLIHelper.ScreenChangeRTN(title); + + + } + + public string Templates() => "You can view available templates by typing 'templates'\nYou can run a tempalte by typing 'run template_name.txt' where template_name is the name of the template you want to run"; + + public string Randomiser() => + "The Randomiser tool can be navigated by typing commands, excluding the apostrophes. the following are some of the commands and descriptors.\n\n" + + "'templates': Access the template menu to start a randomisation.\n" + + "'help': The help menu, you're here right now.\n" + + "'back': Go back a menu"; + public string Test(params string[] args) + { + string argsStr = args.ToString(','); + return "response: " + argsStr; + } + +} diff --git a/RTWLib_CLI/cmd/screens/templates.cs b/RTWLib_CLI/cmd/screens/templates.cs new file mode 100644 index 0000000..4a2ec3f --- /dev/null +++ b/RTWLib_CLI/cmd/screens/templates.cs @@ -0,0 +1,82 @@ +namespace RTWLib_CLI.cmd.screens; + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using RTWLib_CLI.draw; +using RTWLibPlus.helpers; +using RTWLibPlus.parsers; + +public class Templates +{ + private static readonly CompositeFormat TemplatesTitle = CompositeFormat.Parse("Please select a template from below by typing the following 'run template_name.txt': {0}{1}"); + + private readonly Dictionary templates = []; + + private readonly string title; + public Templates() + { + this.LoadTemplates(); + this.title = string.Format(null, TemplatesTitle, "\n", this.templates.Keys.ToArray().ArrayToString(false, true, true, 1, false, false)); + } + + public string Action(string cmd) + { + string[] cmdSplit = cmd.Split(" "); + if (cmdSplit.Length == 1) + { + return "run command invalid"; + } + + if (this.templates.ContainsKey(cmdSplit[1])) + { + return this.ProcessTemplate(cmdSplit[1]); + } + + else + { + return string.Format("Template not found: {0}", cmdSplit[1]); + } + } + + public string View_Templates() => CLIHelper.ScreenChangeRTN(this.title.ApplyBorder('=', 1, 1)); + + private string ProcessTemplate(string template) + { + string[] cmds = this.templates[template]; + Console.WriteLine("Running: " + template); + //Progress p = new(1f / cmds.Length, "Running: " + template); + foreach (string cmd in cmds) + { + Console.WriteLine("Doing: " + cmd); + //p.Message("Doing: " + cmd); + CMDProcess.CMDScreener(cmd); + //p.Update("Complete"); + } + return "template finished processing"; + } + + private string LoadTemplates() + { + if (!Directory.Exists("randomiser_templates")) + { + return "Template folder does not exist. Skipping template loading"; + } + + string[] files = Directory.GetFiles("randomiser_templates"); + + DepthParse dp = new(); + + foreach (string file in files) + { + string name = Path.GetFileName(file); + string[] parse = dp.ReadFile(file); + this.templates.Add(name, parse); + } + return "Templates Loaded"; + } + +} + diff --git a/RTWLib_CLI/input/input.cs b/RTWLib_CLI/input/input.cs index ee32d4f..cb32d63 100644 --- a/RTWLib_CLI/input/input.cs +++ b/RTWLib_CLI/input/input.cs @@ -11,4 +11,10 @@ public static int GetIntInput(string message, Func conditional) return id.GetIntInput(conditional); } + public static string GetStringInput(string message, Func conditional) + { + InputDialog id = new InputDialog(message); + return id.GetStringInput(conditional); + } + } diff --git a/RTWLib_CLI/input/inputDialog.cs b/RTWLib_CLI/input/inputDialog.cs index 5106e81..299918b 100644 --- a/RTWLib_CLI/input/inputDialog.cs +++ b/RTWLib_CLI/input/inputDialog.cs @@ -39,5 +39,23 @@ public int GetIntInput(Func conditional) return num; } + public string GetStringInput(Func conditional) + { + this.Display(); + string input; + string num = ""; + + while (!conditional(num)) + { + input = Console.ReadLine(); + num = input.Trim(); + if (!conditional(num)) + { + Console.WriteLine("invalid input"); + } + } + return num; + } + } diff --git a/RTWLib_CLI/keywords.cs b/RTWLib_CLI/keywords.cs index 2d413bb..1610d3a 100644 --- a/RTWLib_CLI/keywords.cs +++ b/RTWLib_CLI/keywords.cs @@ -5,5 +5,6 @@ public static class KW public static string back = "back"; public static string help = "help"; public static string error = "error"; + public static string templates = "templates"; }