Skip to content

Commit

Permalink
fix: simple help command, improvements to the operational flow
Browse files Browse the repository at this point in the history
  • Loading branch information
sargeantPig committed Jun 15, 2024
1 parent 9671c67 commit ceff940
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 106 deletions.
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
2 changes: 1 addition & 1 deletion RTWLibPlus/helpers/exArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions RTWLib_CLI/CLIHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace RTWLib_CLI;

using System;
using System.Reflection;

public static class CLIHelper
{
Expand All @@ -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;
}
}
9 changes: 7 additions & 2 deletions RTWLib_CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)
Expand All @@ -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; }
Expand Down
65 changes: 21 additions & 44 deletions RTWLib_CLI/cmd/cmdProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]> templates = [];
private static readonly Templates TemplatesManager = new();
public static Dictionary<int, string> 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);
Expand All @@ -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))
{
Expand Down Expand Up @@ -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()
Expand All @@ -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";
}
Expand Down
59 changes: 0 additions & 59 deletions RTWLib_CLI/cmd/help.cs

This file was deleted.

34 changes: 34 additions & 0 deletions RTWLib_CLI/cmd/modules/help.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
82 changes: 82 additions & 0 deletions RTWLib_CLI/cmd/screens/templates.cs
Original file line number Diff line number Diff line change
@@ -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<string, string[]> 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";
}

}

6 changes: 6 additions & 0 deletions RTWLib_CLI/input/input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ public static int GetIntInput(string message, Func<int, bool> conditional)
return id.GetIntInput(conditional);
}

public static string GetStringInput(string message, Func<string, bool> conditional)
{
InputDialog id = new InputDialog(message);
return id.GetStringInput(conditional);
}

}
Loading

0 comments on commit ceff940

Please sign in to comment.