created project ParadoxSaveParser.CLI

This commit is contained in:
Timerix 2025-04-06 00:38:13 +05:00
parent a8c4361512
commit b0c8841250
7 changed files with 164 additions and 19 deletions

View File

@ -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>

View File

@ -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);
}
}

View File

@ -1,21 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="DTLib" Version="1.6.5"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<ProjectReference Include="..\ParadoxSaveParser.Lib\ParadoxSaveParser.Lib.csproj"/>
</ItemGroup>
<ItemGroup>
@ -23,7 +19,17 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ParadoxSaveParser.Lib\ParadoxSaveParser.Lib.csproj"/>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="DTLib" Version="1.6.5"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
</ItemGroup>
</Project>

View File

@ -1,3 +1,4 @@
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using DTLib.Extensions;

View File

@ -1,11 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.3" />
</ItemGroup>
</Project>

View File

@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFolder", "SolutionF
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParadoxSaveParser.Lib.Tests", "ParadoxSaveParser.Lib.Tests\ParadoxSaveParser.Lib.Tests.csproj", "{23F4BE1B-3043-4821-9F65-74FF5F57FA59}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParadoxSaveParser.CLI", "ParadoxSaveParser.CLI\ParadoxSaveParser.CLI.csproj", "{2D4448A6-390D-47F3-9BB7-6266669719DE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -30,5 +32,9 @@ Global
{23F4BE1B-3043-4821-9F65-74FF5F57FA59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23F4BE1B-3043-4821-9F65-74FF5F57FA59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23F4BE1B-3043-4821-9F65-74FF5F57FA59}.Release|Any CPU.Build.0 = Release|Any CPU
{2D4448A6-390D-47F3-9BB7-6266669719DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D4448A6-390D-47F3-9BB7-6266669719DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D4448A6-390D-47F3-9BB7-6266669719DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D4448A6-390D-47F3-9BB7-6266669719DE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -1,11 +1,14 @@
Parser.CLI:
Temp files management system
Main:
Add temporary files deletion
Temp files management system
Database???
Add query to get parsed data
ParseSaveHandler:
Make this method run as background task instead of POST query
Add debug log
Save parsed in protobuf
Save parsed data in protobuf
Re-parse if saved data was parsed with another query
Parser:
Optimize it (5 sec per query isn't good)