paradox-mod-merger/paradox-dlc-metadata-parser/Program.cs

90 lines
3.0 KiB
C#

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}=");
}