split CLI modes to separate files
This commit is contained in:
parent
eeb8f43d3d
commit
3c1d195849
@ -12,51 +12,11 @@ internal enum Mode
|
|||||||
Unset, Search, Interactive
|
Unset, Search, Interactive
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static class Modes
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void Interactive()
|
internal static void Interactive()
|
||||||
{
|
{
|
||||||
Console.Clear();
|
ColoredConsole.Clear();
|
||||||
Console.ResetColor();
|
|
||||||
ColoredConsole.WriteTitle("interactive mode", fg: ConsoleColor.Cyan);
|
ColoredConsole.WriteTitle("interactive mode", fg: ConsoleColor.Cyan);
|
||||||
ColoredConsole.WriteLine($"working directory: '{Environment.CurrentDirectory}'", ConsoleColor.Gray);
|
ColoredConsole.WriteLine($"working directory: '{Environment.CurrentDirectory}'", ConsoleColor.Gray);
|
||||||
IOPath? inputPath = null;
|
IOPath? inputPath = null;
|
||||||
@ -67,8 +27,7 @@ internal static class Modes
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
ColoredConsole.Write("> ", ConsoleColor.Blue);
|
ColoredConsole.Write("> ", ConsoleColor.Blue);
|
||||||
Console.ForegroundColor = ConsoleColor.Gray;
|
string input = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||||
string? input = Console.ReadLine();
|
|
||||||
if (string.IsNullOrEmpty(input))
|
if (string.IsNullOrEmpty(input))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -108,13 +67,13 @@ internal static class Modes
|
|||||||
|
|
||||||
case "h":
|
case "h":
|
||||||
case "help":
|
case "help":
|
||||||
ColoredConsole.WriteLine(helpMessage, fg: ConsoleColor.White);
|
ColoredConsole.WriteLine(helpMessage, ConsoleColor.White);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "i":
|
case "i":
|
||||||
case "input":
|
case "input":
|
||||||
input = ColoredConsole.ReadLine("Input file path",
|
ColoredConsole.Write("Input file path: ", ConsoleColor.Blue);
|
||||||
ConsoleColor.Blue);
|
input = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||||
if (string.IsNullOrEmpty(input))
|
if (string.IsNullOrEmpty(input))
|
||||||
throw new NullReferenceException();
|
throw new NullReferenceException();
|
||||||
inputPath = input;
|
inputPath = input;
|
||||||
@ -122,8 +81,8 @@ internal static class Modes
|
|||||||
|
|
||||||
case "o":
|
case "o":
|
||||||
case "output":
|
case "output":
|
||||||
input = ColoredConsole.ReadLine("Output file path [default=stdout]",
|
ColoredConsole.Write("Output file path [default=stdout]: ", ConsoleColor.Blue);
|
||||||
ConsoleColor.Blue);
|
input = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||||
if (string.IsNullOrEmpty(input))
|
if (string.IsNullOrEmpty(input))
|
||||||
throw new ArgumentException("Input file path is required");
|
throw new ArgumentException("Input file path is required");
|
||||||
inputPath = input;
|
inputPath = input;
|
||||||
@ -134,8 +93,8 @@ internal static class Modes
|
|||||||
if (inputPath is null)
|
if (inputPath is null)
|
||||||
throw new ArgumentException("Input file path is required");
|
throw new ArgumentException("Input file path is required");
|
||||||
|
|
||||||
var searchQuery = ColoredConsole.ReadLine("search expression",
|
ColoredConsole.Write("search expression: ", ConsoleColor.Blue);
|
||||||
ConsoleColor.Blue);
|
var searchQuery = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||||
if (string.IsNullOrEmpty(searchQuery))
|
if (string.IsNullOrEmpty(searchQuery))
|
||||||
throw new ArgumentException("Search expression is required");
|
throw new ArgumentException("Search expression is required");
|
||||||
|
|
||||||
@ -150,8 +109,8 @@ internal static class Modes
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "cd":
|
case "cd":
|
||||||
input = ColoredConsole.ReadLine("Change working directory to",
|
ColoredConsole.Write("Change working directory to: ", ConsoleColor.Blue);
|
||||||
ConsoleColor.Blue);
|
input = ColoredConsole.ReadLine(ConsoleColor.Gray);
|
||||||
if (!string.IsNullOrEmpty(input))
|
if (!string.IsNullOrEmpty(input))
|
||||||
{
|
{
|
||||||
Environment.CurrentDirectory = new IOPath(input).Str;
|
Environment.CurrentDirectory = new IOPath(input).Str;
|
||||||
49
ParadoxSaveParser.CLI/Modes/Search.cs
Normal file
49
ParadoxSaveParser.CLI/Modes/Search.cs
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,6 +14,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DTLib" Version="1.6.5"/>
|
<PackageReference Include="DTLib" Version="1.7.0"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -0,0 +1,2 @@
|
|||||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=modes/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||||
@ -14,26 +14,26 @@ try
|
|||||||
string? searchQuery = null;
|
string? searchQuery = null;
|
||||||
|
|
||||||
new LaunchArgumentParser(
|
new LaunchArgumentParser(
|
||||||
new LaunchArgument(["-i", "--input"],
|
new LaunchArgument(["-i", "--input"],
|
||||||
"Set input file path",
|
"Set input file path",
|
||||||
s => inputPath = s,
|
s => inputPath = s,
|
||||||
"gamestate or zip file"),
|
"gamestate or zip file"),
|
||||||
|
|
||||||
new LaunchArgument(["-o", "--output"],
|
new LaunchArgument(["-o", "--output"],
|
||||||
"Set output file path",
|
"Set output file path",
|
||||||
s => outputPath = s,
|
s => outputPath = s,
|
||||||
"json file [default=stdout]"),
|
"json file [default=stdout]"),
|
||||||
|
|
||||||
new LaunchArgument(["-s", "--search"],
|
new LaunchArgument(["-s", "--search"],
|
||||||
"Search in input file",
|
"Search in input file",
|
||||||
s =>
|
s =>
|
||||||
{
|
{
|
||||||
searchQuery = s;
|
searchQuery = s;
|
||||||
mode = Mode.Search;
|
mode = Mode.Search;
|
||||||
},
|
},
|
||||||
"search expression")
|
"search expression")
|
||||||
)
|
)
|
||||||
.WithNoExit()
|
.AllowNoArguments()
|
||||||
.ParseAndHandle(args);
|
.ParseAndHandle(args);
|
||||||
|
|
||||||
if (args.Length == 0)
|
if (args.Length == 0)
|
||||||
@ -57,6 +57,10 @@ try
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (LaunchArgumentParser.ExitAfterHelpException)
|
||||||
|
{
|
||||||
|
// this exception is throwed after -h argument to close the program
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
ColoredConsole.WriteLine(ex.ToStringDemystified(), ConsoleColor.Red);
|
ColoredConsole.WriteLine(ex.ToStringDemystified(), ConsoleColor.Red);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user