From 3b94791819a8d5a724f52989a43c0a914465cbf0 Mon Sep 17 00:00:00 2001 From: Aaron T Date: Sat, 30 Dec 2023 12:52:23 +0000 Subject: [PATCH] fixed linter warnings (#47) --- RTWLib_CLI/cmd/cmdProcess.cs | 179 ++++++++++++++------------- RTWLib_CLI/cmd/modules/randomiser.cs | 66 +++++----- RTWLib_CLI/cmd/modules/searchCLI.cs | 99 ++++++++------- 3 files changed, 182 insertions(+), 162 deletions(-) diff --git a/RTWLib_CLI/cmd/cmdProcess.cs b/RTWLib_CLI/cmd/cmdProcess.cs index 432a0e4..2632cc9 100644 --- a/RTWLib_CLI/cmd/cmdProcess.cs +++ b/RTWLib_CLI/cmd/cmdProcess.cs @@ -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 templates = new(); + public static Dictionary configs = new(); + + public static ModuleRegister modules = new(); + + public static string ReadCMD(string cmd, Type type = null) { - public static Dictionary templates = new Dictionary(); - public static Dictionary configs = new Dictionary(); + 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"; } } diff --git a/RTWLib_CLI/cmd/modules/randomiser.cs b/RTWLib_CLI/cmd/modules/randomiser.cs index 14a5592..470c13a 100644 --- a/RTWLib_CLI/cmd/modules/randomiser.cs +++ b/RTWLib_CLI/cmd/modules/randomiser.cs @@ -26,48 +26,56 @@ 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; } @@ -75,8 +83,8 @@ public string SetSeed(string seed) public string InitialSetup() { - List list = new List() { edu, edb, ds, dr, smf, mr, bm }; - Progress p = new Progress(1f / (list.Count + 1), "Setting up"); + List 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++) { @@ -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"); @@ -97,23 +105,23 @@ public string Output() { string path = string.Empty; - List list = new List() { edu, ds }; - Progress p = new Progress(0.50f, "Writing Files"); + List 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"; } diff --git a/RTWLib_CLI/cmd/modules/searchCLI.cs b/RTWLib_CLI/cmd/modules/searchCLI.cs index 63fffff..be66ebe 100644 --- a/RTWLib_CLI/cmd/modules/searchCLI.cs +++ b/RTWLib_CLI/cmd/modules/searchCLI.cs @@ -1,74 +1,73 @@ -using RTWLibPlus.data; +namespace RTWLib_CLI.cmd.modules; + +using RTWLibPlus.data; using RTWLibPlus.dataWrappers; using RTWLibPlus.interfaces; -using System; +using RTWLibPlus.parsers.objects; using System.Collections.Generic; -using System.Security.Cryptography.X509Certificates; -using System.Text; -namespace RTWLib_CLI.cmd.modules +public class Search { - public class Search + TWConfig config; + + public Search(TWConfig twConfig) => this.config = twConfig; + /// + /// -id file acronym, -m -v (mod or vanilla), -w find what (can be multiple values separate by comma), + /// + /// + public void Find(params string[] args) { - TWConfig config; + IWrapper wrapper = null; - public Search(TWConfig twConfig) + switch (args[0]) { - config = twConfig; + case "edu": + wrapper = new EDU(this.SetFilePath(args[0], args[1]), this.SetFilePath(args[0], args[1])); + break; + case "edb": + break; + case "ds": + break; + case "smf": + break; + case "dr": + break; + default: + break; } - /// - /// -id file acronym, -m -v (mod or vanilla), -w find what (can be multiple values separate by comma), - /// - /// - public void Find(params string[] args) - { - IWrapper wrapper = null; - switch (args[0]) - { - case "edu": - wrapper = new EDU(SetFilePath(args[0], args[1]), SetFilePath(args[0], args[1])); - break; - case "edb": - break; - case "ds": - break; - case "smf": - break; - case "dr": - break; - } + wrapper.Parse(); - wrapper.Parse(); + List> results = new(); - List> results = new List>(); - - for (int i = 3; i < args.Length; i++) - { - ((BaseWrapper)wrapper).GetItemsByIdent(args[i]); - } + for (int i = 3; i < args.Length; i++) + { + ((BaseWrapper)wrapper).GetItemsByIdent(args[i]); + } - foreach (string arg in args) - { + foreach (string arg in args) + { - } } + } - private string SetFilePath(string file, string vanOrMod) - { - Operation operation = Operation.Load; - - if (vanOrMod == "-m") - operation = Operation.Save; - else if (vanOrMod == "-v") - operation = Operation.Load; - + private string SetFilePath(string file, string vanOrMod) + { + Operation operation = Operation.Load; - return config.GetPath(operation, file); + if (vanOrMod == "-m") + { + operation = Operation.Save; + } + else if (vanOrMod == "-v") + { + operation = Operation.Load; } + return this.config.GetPath(operation, file); } + }