From c71ae11cc580e96336a14f3d0e0e5c85888531e2 Mon Sep 17 00:00:00 2001 From: onepiecefreak3 Date: Thu, 22 Jul 2021 00:32:53 +0200 Subject: [PATCH] Add automatic script detection #3; --- XtractQuery/Program.cs | 47 +++++++++++++++++----- XtractQuery/Properties/launchSettings.json | 2 +- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/XtractQuery/Program.cs b/XtractQuery/Program.cs index b81bd89..cd4ace9 100644 --- a/XtractQuery/Program.cs +++ b/XtractQuery/Program.cs @@ -2,6 +2,7 @@ using System.IO; using System.Linq; using System.Reflection; +using Komponent.IO; using XtractQuery.Interfaces; using XtractQuery.Options; using XtractQuery.Parsers; @@ -26,13 +27,15 @@ static void Main(string[] args) return; } + #region Check arguments existing + if (!arguments.Operation.Exists) { Console.WriteLine("No operation mode was given. Specify an operation mode by using the -o argument."); return; } - if (!arguments.QueryType.Exists) + if (arguments.Operation.Values.First() == "c" && !arguments.QueryType.Exists) { Console.WriteLine("No query type was given. Specify a query type by using the -t argument."); return; @@ -44,25 +47,26 @@ static void Main(string[] args) return; } - operation = arguments.Operation.Values.First(); - type = arguments.QueryType.Values.First(); - file = arguments.QueryFile.Values.First(); + #endregion - if (operation != "e" && operation != "c") + file = arguments.QueryFile.Values.First(); + if (!File.Exists(file)) { - Console.WriteLine($"The operation mode '{operation}' is not valid. Use -h to see a list of valid operation modes."); + Console.WriteLine($"File '{Path.GetFullPath(file)}' was not found."); return; } - if (type != "xq32" && type != "xseq") + operation = arguments.Operation.Values.First(); + if (operation != "e" && operation != "c") { - Console.WriteLine($"The query type '{type}' is not valid. Use -h to see a list of valid query types."); + Console.WriteLine($"The operation mode '{operation}' is not valid. Use -h to see a list of valid operation modes."); return; } - if (!File.Exists(file)) + type = arguments.QueryType.Values.FirstOrDefault(); + if (operation == "c" && type != "xq32" && type != "xseq") { - Console.WriteLine($"File '{Path.GetFullPath(file)}' was not found."); + Console.WriteLine($"The query type '{type}' is not valid. Use -h to see a list of valid query types."); return; } @@ -71,6 +75,7 @@ static void Main(string[] args) switch (operation) { case "e": + type = DetermineType(file); ExtractFile(type, file); break; @@ -80,6 +85,24 @@ static void Main(string[] args) } } + private static string DetermineType(string file) + { + using var fileStream = File.OpenRead(file); + using var br = new BinaryReaderX(fileStream); + + switch (br.ReadString(4)) + { + case "XQ32": + return "xq32"; + + case "XSEQ": + return "xseq"; + + default: + return string.Empty; + } + } + private static void ExtractFile(string type, string file) { IParser parser; @@ -142,11 +165,13 @@ private static void PrintHelp() Console.WriteLine(" Valid operations are: e for extraction, c for creation"); Console.WriteLine(" -t, --type\t\tThe type of file given"); Console.WriteLine(" Valid types are: xq32, xseq"); + Console.WriteLine(" The type is automatically detected when extracting; This argument will not have any effect on operation 'e'"); Console.WriteLine(" -f, --file\t\tThe file to process"); Console.WriteLine(); Console.WriteLine("Example usage:"); - Console.WriteLine($"\tExtract a xq32 query to human readable text: {Assembly.GetExecutingAssembly().Location} -o e -t xq32 -f [file]"); + Console.WriteLine($"\tExtract any query to human readable text: {Assembly.GetExecutingAssembly().Location} -o e -f [file]"); Console.WriteLine($"\tCreate a xq32 query from human readable text: {Assembly.GetExecutingAssembly().Location} -o c -t xq32 -f [file]"); + Console.WriteLine($"\tCreate a xseq query from human readable text: {Assembly.GetExecutingAssembly().Location} -o c -t xseq -f [file]"); } } } diff --git a/XtractQuery/Properties/launchSettings.json b/XtractQuery/Properties/launchSettings.json index c7b4de3..05eaec1 100644 --- a/XtractQuery/Properties/launchSettings.json +++ b/XtractQuery/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "XtractQuery": { "commandName": "Project", - "commandLineArgs": "-o e -t xq32 -f D:\\Users\\Kirito\\Desktop\\strinput.xq.txt.xq" + "commandLineArgs": "-o c -f D:\\Users\\Kirito\\Desktop\\xq_sample\\02_020025.xq" } } } \ No newline at end of file