added paradox-dlc-metadata-parser project
This commit is contained in:
parent
0c2670f55a
commit
6bdf66b326
90
paradox-dlc-metadata-parser/Program.cs
Normal file
90
paradox-dlc-metadata-parser/Program.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using DTLib.Demystifier;
|
||||
using DTLib.Console;
|
||||
using DTLib.Extensions;
|
||||
using File = DTLib.Filesystem.File;
|
||||
|
||||
namespace ParadoxDlcMetadataParser;
|
||||
|
||||
static class Program
|
||||
{
|
||||
private static string? GameName;
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
new LaunchArgumentParser(
|
||||
new LaunchArgument(
|
||||
new[] { "f", "file" },
|
||||
"Searches for dlc id and name declarations in a dlc_metadata/* file.",
|
||||
ParseFile,
|
||||
"file_name",
|
||||
priority: 99),
|
||||
new LaunchArgument(
|
||||
new[] { "g", "game" },
|
||||
"Sets game name.",
|
||||
name => GameName = name,
|
||||
"game_name")
|
||||
).ParseAndHandle(args);
|
||||
}
|
||||
catch (LaunchArgumentParser.ExitAfterHelpException)
|
||||
{
|
||||
// this exception is thrown when the program exits after showing help message
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(ex.ToStringDemystified());
|
||||
}
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
static void ParseFile(string filePath)
|
||||
{
|
||||
using var file = File.OpenRead(filePath);
|
||||
using var r = new StreamReader(file);
|
||||
List<string> names = new();
|
||||
List<int> ids = new();
|
||||
while (!r.EndOfStream)
|
||||
{
|
||||
string line = r.ReadLine()!;
|
||||
if (IsVarDeclaration(line, "name"))
|
||||
names.Add(line.AsSpan().After('=').After('"').Before('"').Trim().Trim('\t').ToString());
|
||||
else if (IsVarDeclaration(line, "id") || IsVarDeclaration(line, "steam_id"))
|
||||
{
|
||||
var s1 = line.AsSpan().After('=');
|
||||
if(s1.Contains('#'))
|
||||
s1 = s1.After("#"); // some ids are hidden in comments
|
||||
ids.Add(Convert.ToInt32(s1.Trim().Trim('\t').Trim('"').Trim(' ').ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
if (names.Count != ids.Count)
|
||||
throw new Exception("names.Count != ids.Count");
|
||||
|
||||
var ids_and_names = ids.Zip(names)
|
||||
.DistinctBy(tuple => tuple.First)
|
||||
.OrderBy(tuple => tuple.First)
|
||||
.ToList<(int id, string name)>();
|
||||
|
||||
Console.WriteLine("----------------------------------");
|
||||
foreach ((int id, string name) in ids_and_names)
|
||||
{
|
||||
Console.WriteLine($"{id} = {GameName}: {name}");
|
||||
}
|
||||
Console.WriteLine("----------------------------------");
|
||||
Console.WriteLine($"{ids_and_names.Count} DLC declarations found");
|
||||
}
|
||||
|
||||
static bool IsVarDeclaration(string line, string varName) =>
|
||||
line.StartsWith($"{varName} =") ||
|
||||
line.StartsWith($"{varName}=") ||
|
||||
line.Contains($" {varName} =") ||
|
||||
line.Contains($" {varName}=") ||
|
||||
line.Contains($"\t{varName} =") ||
|
||||
line.Contains($"\t{varName}=");
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<RootNamespace>ParadoxDlcMetadataParser</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DTLib" Version="1.6.1" />
|
||||
<PackageReference Include="DTLib.Demystifier" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Reference in New Issue
Block a user