split CLI modes to separate files
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using ParadoxSaveParser.Lib;
|
||||
using Path = DTLib.Filesystem.Path;
|
||||
|
||||
namespace ParadoxSaveParser.CLI;
|
||||
|
||||
internal enum Mode
|
||||
{
|
||||
Unset, Search, Interactive
|
||||
}
|
||||
|
||||
internal static partial class Modes
|
||||
{
|
||||
internal static void Interactive()
|
||||
{
|
||||
ColoredConsole.Clear();
|
||||
ColoredConsole.WriteTitle("interactive mode", fg: ConsoleColor.Cyan);
|
||||
ColoredConsole.WriteLine($"working directory: '{Environment.CurrentDirectory}'", ConsoleColor.Gray);
|
||||
IOPath? inputPath = null;
|
||||
IOPath? outputPath = null;
|
||||
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
ColoredConsole.Write("> ", ConsoleColor.Blue);
|
||||
string input = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||
if (string.IsNullOrEmpty(input))
|
||||
continue;
|
||||
|
||||
const string helpMessage =
|
||||
"""
|
||||
Commands dont have arguments. Just write command name ant it will ask you for more information.
|
||||
Avaliable commands:
|
||||
h, help - show this message
|
||||
q, quit, exit - close the program
|
||||
pwd - show working directory
|
||||
cd - change working directory
|
||||
ls - show list of files in working directory
|
||||
i, input - set input file path
|
||||
o, output - set output file path
|
||||
s, search - perform search in input file using expression
|
||||
""";
|
||||
|
||||
if (input.Contains(' '))
|
||||
{
|
||||
ColoredConsole.WriteLine("Commands dont have arguments."
|
||||
+ " Just write command name ant it will ask you for more information.",
|
||||
ConsoleColor.Red);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (input)
|
||||
{
|
||||
default:
|
||||
ColoredConsole.WriteLine("Unknown command, use 'help' for a list of commands.",
|
||||
ConsoleColor.Red);
|
||||
continue;
|
||||
|
||||
case "q":
|
||||
case "quit":
|
||||
case "exit":
|
||||
return;
|
||||
|
||||
case "h":
|
||||
case "help":
|
||||
ColoredConsole.WriteLine(helpMessage, ConsoleColor.White);
|
||||
break;
|
||||
|
||||
case "i":
|
||||
case "input":
|
||||
ColoredConsole.Write("Input file path: ", ConsoleColor.Blue);
|
||||
input = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||
if (string.IsNullOrEmpty(input))
|
||||
throw new NullReferenceException();
|
||||
inputPath = input;
|
||||
break;
|
||||
|
||||
case "o":
|
||||
case "output":
|
||||
ColoredConsole.Write("Output file path [default=stdout]: ", ConsoleColor.Blue);
|
||||
input = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||
if (string.IsNullOrEmpty(input))
|
||||
throw new ArgumentException("Input file path is required");
|
||||
inputPath = input;
|
||||
break;
|
||||
|
||||
case "s":
|
||||
case "search":
|
||||
if (inputPath is null)
|
||||
throw new ArgumentException("Input file path is required");
|
||||
|
||||
ColoredConsole.Write("search expression: ", ConsoleColor.Blue);
|
||||
var searchQuery = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||
if (string.IsNullOrEmpty(searchQuery))
|
||||
throw new ArgumentException("Search expression is required");
|
||||
|
||||
ColoredConsole.WriteHLine('-', ConsoleColor.Cyan);
|
||||
Console.ResetColor();
|
||||
Search(searchQuery, inputPath.Value, outputPath);
|
||||
ColoredConsole.WriteHLine('-', ConsoleColor.Green);
|
||||
break;
|
||||
|
||||
case "pwd":
|
||||
ColoredConsole.WriteLine(Environment.CurrentDirectory, ConsoleColor.White);
|
||||
break;
|
||||
|
||||
case "cd":
|
||||
ColoredConsole.Write("Change working directory to: ", ConsoleColor.Blue);
|
||||
input = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||
if (!string.IsNullOrEmpty(input))
|
||||
{
|
||||
Environment.CurrentDirectory = new IOPath(input).Str;
|
||||
ColoredConsole.WriteLine(Environment.CurrentDirectory, ConsoleColor.Green);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "ls":
|
||||
IOPath curdir = Directory.GetCurrent();
|
||||
foreach (var dir in Directory.GetDirectories(curdir))
|
||||
{
|
||||
ColoredConsole.WriteLine(dir.RemoveBase(curdir).Str + Path.Sep, ConsoleColor.Cyan);
|
||||
}
|
||||
|
||||
foreach (var file in Directory.GetFiles(curdir))
|
||||
{
|
||||
ColoredConsole.WriteLine(file.RemoveBase(curdir).Str, ConsoleColor.White);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ColoredConsole.WriteLine(ex.ToStringDemystified(), ConsoleColor.Red);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using ParadoxSaveParser.Lib;
|
||||
|
||||
namespace ParadoxSaveParser.CLI;
|
||||
|
||||
internal static partial class Modes
|
||||
{
|
||||
internal static void Search(string searchQuery, IOPath inputPath, IOPath? outputPath)
|
||||
{
|
||||
Stream inputStream = File.OpenRead(inputPath);
|
||||
try
|
||||
{
|
||||
byte[] head4 = new byte[4];
|
||||
inputStream.ReadExactly(head4);
|
||||
inputStream.Seek(0, SeekOrigin.Begin);
|
||||
if (head4.SequenceEqual<byte>([(byte)'P', (byte)'K', 3, 4]))
|
||||
{
|
||||
var zipArchive = new ZipArchive(inputStream, ZipArchiveMode.Read);
|
||||
var zipEntry = zipArchive.Entries.FirstOrDefault(e => e.Name == "gamestate");
|
||||
if (zipEntry is null)
|
||||
throw new Exception("'gamestate' file not found in zip archive");
|
||||
var unzipped = new MemoryStream((int)zipEntry.Length);
|
||||
zipEntry.Open().CopyTo(unzipped);
|
||||
zipArchive.Dispose(); // closes inputStream
|
||||
unzipped.Seek(0, SeekOrigin.Begin);
|
||||
inputStream = unzipped;
|
||||
}
|
||||
|
||||
using var outputStream = outputPath is null
|
||||
? Console.OpenStandardOutput()
|
||||
: File.OpenWrite(outputPath.Value);
|
||||
|
||||
var searchExpression = SearchExpressionCompiler.Compile(searchQuery);
|
||||
|
||||
var parser = new SaveParserEU4(inputStream, searchExpression);
|
||||
var parsedValue = parser.Parse();
|
||||
JsonSerializer.Serialize(outputStream, parsedValue,
|
||||
ParsedValueJsonContext.Default.DictionaryStringListObject);
|
||||
outputStream.WriteByte((byte)'\n');
|
||||
}
|
||||
finally
|
||||
{
|
||||
inputStream.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user