Skip to content

Commit

Permalink
fixed linter warnings (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
sargeantPig authored Dec 30, 2023
1 parent 45fd701 commit 3b94791
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 162 deletions.
179 changes: 96 additions & 83 deletions RTWLib_CLI/cmd/cmdProcess.cs
Original file line number Diff line number Diff line change
@@ -1,128 +1,141 @@
using System;
namespace RTWLib_CLI.cmd;

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using RTWLibPlus.helpers;
using System.Linq;
using RTWLIB_CLI;
using System.IO;
using RTWLibPlus.parsers;
using RTWLib_CLI.draw;
using System.Security;

namespace RTWLib_CLI.cmd

public static class CMDProcess
{
public static class CMDProcess
public static Dictionary<string, string[]> templates = new();
public static Dictionary<int, string> configs = new();

public static ModuleRegister modules = new();

public static string ReadCMD(string cmd, Type type = null)
{
public static Dictionary<string, string[]> templates = new Dictionary<string, string[]>();
public static Dictionary<int, string> configs = new Dictionary<int, string>();
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;

public static ModuleRegister modules = new ModuleRegister();
string[] cmdSplit = cmd.Split(' ', StringSplitOptions.RemoveEmptyEntries);

public static string ReadCMD(string cmd, Type type = null)
if (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;
type = Type.GetType("RTWLib_CLI.cmd.modules." + cmdSplit[0], false, true);// Type.GetType(cmdSplit[0], true, true);
invokeInd = 1;
}
if (type == null)
{
return KW.error + ": Type not found";
}

string[] cmdSplit = cmd.Split(' ', StringSplitOptions.RemoveEmptyEntries);
object invokableObject = modules.GetModule(type.Name);

if (type == null)

foreach (MethodInfo t in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly))
{
if (t.Name.ToLower(System.Globalization.CultureInfo.CurrentCulture) != cmdSplit[invokeInd].ToLower(System.Globalization.CultureInfo.CurrentCulture))
{
type = Type.GetType("RTWLib_CLI.cmd.modules." + cmdSplit[0], false, true);// Type.GetType(cmdSplit[0], true, true);
invokeInd = 1;
continue;
}
if (type == null)
return KW.error + ": Type not found";

var invokableObject = modules.GetModule(type.Name);
string[] args = cmdSplit.GetItemsFrom(invokeInd + 1);
ParameterInfo[] par = t.GetParameters();


foreach (MethodInfo t in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly))
if (par.Length > args.Length)
{
if (t.Name.ToLower() != cmdSplit[invokeInd].ToLower()) continue;
return string.Format("{0}: Incorrect args, expected: {1}", KW.error, par.ToString(','));
}

string[] args = cmdSplit.GetItemsFrom(invokeInd + 1);
var par = t.GetParameters();
object[] newArg = new object[par.Length];

if (par.Length > args.Length)
for (int i = 0; i < par.Length; i++)
{
type = par[i].ParameterType;

if (type == typeof(int))
{
return string.Format("{0}: Incorrect args, expected: {1}", KW.error, par.ToString(','));
newArg[i] = Convert.ToInt32(args[i]);
}

object[] newArg = new object[par.Length];

for (int i = 0; i < par.Length; i++)
else if (type == typeof(string[]))
{
type = par[i].ParameterType;

if (type == typeof(Int32))
newArg[i] = Convert.ToInt32(args[i]);
else if (type == typeof(string[]))
{
newArg[i] = args.GetItemsFrom(i);
}
else newArg[i] = args[i];

newArg[i] = args.GetItemsFrom(i);
}
else
{
newArg[i] = args[i];
}
}


return (string)t.Invoke(invokableObject, newArg);
}
return KW.error + ": Command not found, are the arguments correct?";
return (string)t.Invoke(invokableObject, newArg);
}
return KW.error + ": Command not found, are the arguments correct?";
}

public static string ProcessTemplate(string template)
public static string ProcessTemplate(string template)
{
string[] cmds = templates[template];
Progress p = new(1f / cmds.Length, "Running: " + template);
foreach (string cmd in cmds)
{
var cmds = templates[template];
Progress p = new Progress(1f / (cmds.Count()), "Running: " + template);
foreach (var cmd in cmds)
{
p.Message("Doing: " + cmd);
ReadCMD(cmd);
p.Update("Complete");
}
return "template finished processing";
p.Message("Doing: " + cmd);
ReadCMD(cmd);
p.Update("Complete");
}
return "template finished processing";
}

public static string LoadTemplates()
public static string LoadTemplates()
{
if (!Directory.Exists("randomiser_templates"))
{
if (!Directory.Exists("randomiser_templates"))
return "Template folder does not exist. Skipping template loading";
return "Template folder does not exist. Skipping template loading";
}

var files = Directory.GetFiles("randomiser_templates");
string[] files = Directory.GetFiles("randomiser_templates");

DepthParse dp = new DepthParse();
DepthParse dp = new();

foreach (var file in files)
{
string name = Path.GetFileName(file);
var parse = dp.ReadFile(file);
templates.Add(name, parse);
}
return "Templates Loaded";
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()
public static string LoadConfigs()
{
if (!Directory.Exists("randomiser_config"))
{
if (!Directory.Exists("randomiser_config"))
return "Config folder does not exist.\nERROR config required. Exiting Program";
return "Config folder does not exist.\nERROR config required. Exiting Program";
}

var files = Directory.GetFiles("randomiser_config");
string[] files = Directory.GetFiles("randomiser_config");

DepthParse dp = new DepthParse();
DepthParse dp = new();

for (int i = 0; i < files.Length; i++)
{
string file = files[i];
string name = Path.GetFileName(file);
var parse = file;
configs.Add(i, parse);
}
return "Configs Loaded";
for (int i = 0; i < files.Length; i++)
{
string file = files[i];
string name = Path.GetFileName(file);
string parse = file;
configs.Add(i, parse);
}
return "Configs Loaded";
}
}
66 changes: 37 additions & 29 deletions RTWLib_CLI/cmd/modules/randomiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,65 @@ public class RandCMD
public RandCMD(TWConfig config)
{
this.config = config;
edb = new EDB(config.GetPath(Operation.Save, "edb"), config.GetPath(Operation.Load, "edb"));
edu = new EDU(config.GetPath(Operation.Save, "edu"), config.GetPath(Operation.Load, "edu"));
ds = new DS(config.GetPath(Operation.Save, "ds"), config.GetPath(Operation.Load, "ds"));
dr = new DR(config.GetPath(Operation.Save, "dr"), config.GetPath(Operation.Load, "dr"));
smf = new SMF(config.GetPath(Operation.Save, "smf"), config.GetPath(Operation.Load, "smf"));
mr = new TGA(config.GetPath(Operation.Load, "mr"), "");
bm = new TGA(config.GetPath(Operation.Load, "bm"), "");
rnd = new RandWrap("0");
this.edb = new EDB(config.GetPath(Operation.Save, "edb"), config.GetPath(Operation.Load, "edb"));
this.edu = new EDU(config.GetPath(Operation.Save, "edu"), config.GetPath(Operation.Load, "edu"));
this.ds = new DS(config.GetPath(Operation.Save, "ds"), config.GetPath(Operation.Load, "ds"));
this.dr = new DR(config.GetPath(Operation.Save, "dr"), config.GetPath(Operation.Load, "dr"));
this.smf = new SMF(config.GetPath(Operation.Save, "smf"), config.GetPath(Operation.Load, "smf"));
this.mr = new TGA(config.GetPath(Operation.Load, "mr"), "");
this.bm = new TGA(config.GetPath(Operation.Load, "bm"), "");
this.rnd = new RandWrap("0");
}

public string Ownership(int maxPerUnit = 3, int minimumPerUnit = 1)
{
if (edu == null)
if (this.edu == null)
{
return "EDU not loaded - run 'rand initialsetup'";
}

return RandEDU.RandomiseOwnership(edu, rnd, smf, maxPerUnit, minimumPerUnit);
return RandEDU.RandomiseOwnership(this.edu, this.rnd, this.smf, maxPerUnit, minimumPerUnit);
}

public string CitiesBasic()
{
if (ds == null)
if (this.ds == null)
{
return "DS not loaded - run 'rand initialsetup'";
return RandDS.RandCitiesBasic(smf, rnd, ds, cm);
}

return RandDS.RandCitiesBasic(this.smf, this.rnd, this.ds, this.cm);
}

public string CitiesVoronoi()
{
if (ds == null)
if (this.ds == null)
{
return "DS not loaded - run 'rand initialsetup'";
return RandDS.RandCitiesVoronoi(smf, rnd, ds, cm);
}

return RandDS.RandCitiesVoronoi(this.smf, this.rnd, this.ds, this.cm);
}

public string PaintFactionMap()
{
FactionMap factionMap = new FactionMap();
factionMap.PaintRegionMap(mr, bm, ds, dr, smf, config.GetPath(Operation.Save, "dir_campaign"));
FactionMap factionMap = new();
factionMap.PaintRegionMap(this.mr, this.bm, this.ds, this.dr, this.smf, this.config.GetPath(Operation.Save, "dir_campaign"));
return "Maps Painted";
}

public string SetSeed(string seed)
{
rnd.RefreshRndSeed(seed);
this.rnd.RefreshRndSeed(seed);
return "Seed set to: " + seed;
}


public string InitialSetup()
{

List<IWrapper> list = new List<IWrapper>() { edu, edb, ds, dr, smf, mr, bm };
Progress p = new Progress(1f / (list.Count + 1), "Setting up");
List<IWrapper> list = new() { this.edu, this.edb, this.ds, this.dr, this.smf, this.mr, this.bm };
Progress p = new(1f / (list.Count + 1), "Setting up");
for (int i = 0; i < list.Count; i++)
{

Expand All @@ -85,8 +93,8 @@ public string InitialSetup()
list[i].Parse();
p.Update("Complete");
}
edu.PrepareEDU();
cm = new CityMap(mr, dr);
this.edu.PrepareEDU();
this.cm = new CityMap(this.mr, this.dr);
p.Message("Forming: City Map");
p.Update("Complete");

Expand All @@ -97,23 +105,23 @@ public string Output()
{
string path = string.Empty;

List<IWrapper> list = new List<IWrapper>() { edu, ds };
Progress p = new Progress(0.50f, "Writing Files");
List<IWrapper> list = new() { this.edu, this.ds };
Progress p = new(0.50f, "Writing Files");
for (int i = 0; i < list.Count; i++)
{
list[i].Output();
p.Update();
}

if (edu != null)
if (this.edu != null)
{
path = config.GetPath(Operation.Save, "edu");
RFH.Write(path, edu.Output());
path = this.config.GetPath(Operation.Save, "edu");
RFH.Write(path, this.edu.Output());
}
if (ds != null)
if (this.ds != null)
{
path = config.GetPath(Operation.Save, "ds");
RFH.Write(path, ds.Output());
path = this.config.GetPath(Operation.Save, "ds");
RFH.Write(path, this.ds.Output());
}
return "output complete";
}
Expand Down
Loading

0 comments on commit 3b94791

Please sign in to comment.