created project ParadoxSaveParser.CLI
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<PublishAot>true</PublishAot>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ParadoxSaveParser.Lib\ParadoxSaveParser.Lib.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DTLib" Version="1.6.5"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,108 @@
|
||||
global using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using DTLib.Console;
|
||||
using DTLib.Demystifier;
|
||||
using DTLib.Filesystem;
|
||||
using ParadoxSaveParser.Lib;
|
||||
using File = DTLib.Filesystem.File;
|
||||
using Directory = DTLib.Filesystem.Directory;
|
||||
|
||||
namespace ParadoxSaveParser.CLI;
|
||||
|
||||
[JsonSourceGenerationOptions(MaxDepth = 1024, WriteIndented = true)]
|
||||
[JsonSerializable(typeof(Dictionary<string, List<object>>))]
|
||||
[JsonSerializable(typeof(List<object>))]
|
||||
[JsonSerializable(typeof(string))]
|
||||
[JsonSerializable(typeof(long))]
|
||||
[JsonSerializable(typeof(double))]
|
||||
public partial class ParsedValueJsonContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
private static IOPath? _inputPath;
|
||||
private static IOPath? _outputPath;
|
||||
private static string? _searchQuery;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
new LaunchArgumentParser(
|
||||
new LaunchArgument(["-i", "--input"],
|
||||
"Set input file path",
|
||||
s => _inputPath = s,
|
||||
"gamestate or zip file"),
|
||||
|
||||
new LaunchArgument(["-o", "--output"],
|
||||
"Set output file path",
|
||||
s => _outputPath = s,
|
||||
"json file [default=stdout]"),
|
||||
|
||||
new LaunchArgument(["-s", "--search"],
|
||||
"Search expression",
|
||||
Search),
|
||||
|
||||
new LaunchArgument(["-e", "--search-expression"],
|
||||
"Set search expression",
|
||||
s => _searchQuery = s,
|
||||
"expression in my custom format"
|
||||
)
|
||||
)
|
||||
.WithNoExit()
|
||||
.ParseAndHandle(args);
|
||||
|
||||
Console.Clear();
|
||||
Console.ResetColor();
|
||||
ColoredConsole.WriteTitle("interactive mode", fg: ConsoleColor.Cyan);
|
||||
ColoredConsole.WriteLine($"working directory: '{Environment.CurrentDirectory}'", ConsoleColor.Gray);
|
||||
|
||||
string? input = null;
|
||||
for (int i = 0; i < 10 && string.IsNullOrEmpty(input); i++)
|
||||
input = ColoredConsole.ReadLine("input file", ConsoleColor.Blue);
|
||||
_inputPath = input ?? throw new NullReferenceException();
|
||||
|
||||
input = ColoredConsole.ReadLine("output file [default=stdout]", ConsoleColor.Blue);
|
||||
if (!string.IsNullOrEmpty(input))
|
||||
_outputPath = input;
|
||||
|
||||
_searchQuery = ColoredConsole.ReadLine("search expression", ConsoleColor.Blue);
|
||||
|
||||
ColoredConsole.WriteHLine('-', ConsoleColor.Cyan);
|
||||
Console.ResetColor();
|
||||
|
||||
Search();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ColoredConsole.WriteLine(ex.ToStringDemystified(), ConsoleColor.Red);
|
||||
Console.ResetColor();
|
||||
Environment.Exit(1);
|
||||
}
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
private static void Search()
|
||||
{
|
||||
if(_inputPath == null)
|
||||
throw new ArgumentException("Input file path is required");
|
||||
using var inputStream = File.OpenRead(_inputPath.Value);
|
||||
|
||||
using var outputStream = _outputPath is null
|
||||
? Console.OpenStandardOutput()
|
||||
: File.OpenWrite(_outputPath.Value);
|
||||
|
||||
var searchExpression = string.IsNullOrEmpty(_searchQuery)
|
||||
? null
|
||||
: SearchExpressionCompiler.Compile(_searchQuery);
|
||||
var parser = new SaveParserEU4(inputStream, searchExpression);
|
||||
var parsedValue = parser.Parse();
|
||||
JsonSerializer.Serialize(outputStream, parsedValue, ParsedValueJsonContext.Default.DictionaryStringListObject);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user