diff --git a/ImperatorToCK3/Outputter/CulturesOutputter.cs b/ImperatorToCK3/Outputter/CulturesOutputter.cs index de638163f..b524994a5 100644 --- a/ImperatorToCK3/Outputter/CulturesOutputter.cs +++ b/ImperatorToCK3/Outputter/CulturesOutputter.cs @@ -8,7 +8,6 @@ using ImperatorToCK3.CK3.Cultures; using ImperatorToCK3.CommonUtils; using Microsoft.FSharp.Collections; -using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; @@ -126,7 +125,7 @@ private static void OutputCCULanguageParameters(string outputModPath, ModFilesys branchEffectNode.AllChildren = allChildren; // Output the modified file. - var tooutput = rootNode.AllChildren + var toOutput = rootNode.AllChildren .Select(c => { if (c.IsLeafC) { return c.leaf.ToRaw; @@ -139,7 +138,7 @@ private static void OutputCCULanguageParameters(string outputModPath, ModFilesys .Where(s => s is not null) .Cast() .ToList(); - var fsharpList = ListModule.OfSeq(tooutput); + var fsharpList = ListModule.OfSeq(toOutput); var outputFilePath = Path.Join(outputModPath, relativePath); // Output the file with UTF8-BOM encoding. diff --git a/ImperatorToCK3/Outputter/DecisionsOutputter.cs b/ImperatorToCK3/Outputter/DecisionsOutputter.cs new file mode 100644 index 000000000..d82f4dc54 --- /dev/null +++ b/ImperatorToCK3/Outputter/DecisionsOutputter.cs @@ -0,0 +1,77 @@ +using commonItems; +using commonItems.Mods; +using CWTools.CSharp; +using CWTools.Parser; +using CWTools.Process; +using ImperatorToCK3.CK3.Titles; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ImperatorToCK3.Outputter; + +internal static class DecisionsOutputter { + internal static async Task TweakERERestorationDecision(Title.LandedTitles titles, ModFilesystem ck3ModFS, + string outputModPath) { + if (!titles.ContainsKey("e_byzantium")) { + return; + } + + Logger.Info("Tweaking ERE restoration decision..."); + const string relativeDecisionsFilePath = "common/decisions/dlc_decisions/ep3_decisions.txt"; + + // The file may already be in the output mod. + string? decisionsFilePath; + string fileInOutputPath = Path.Join(outputModPath, relativeDecisionsFilePath); + if (File.Exists(fileInOutputPath)) { + decisionsFilePath = fileInOutputPath; + } else { + decisionsFilePath = ck3ModFS.GetActualFileLocation(relativeDecisionsFilePath); + } + + if (decisionsFilePath is null) { + Logger.Warn($"Can't find {relativeDecisionsFilePath}!"); + return; + } + + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + var fileName = Path.GetFileName(decisionsFilePath); + + var text = await File.ReadAllTextAsync(decisionsFilePath); + var parsed = Parsers.ParseScriptFile(fileName, text); + var decisionsFile = parsed.GetResult(); + + var processed = Parsers.ProcessStatements(fileName, decisionsFilePath, decisionsFile); + + const string decisionName = "recreate_byzantine_empire_decision"; + var decisionNode = processed.Nodes.FirstOrDefault(n => n.Key == decisionName); + if (decisionNode is null) { + Logger.Warn($"Decision {decisionName} not found!"); + return; + } + + var isShownNode = decisionNode.Nodes.FirstOrDefault(n => n.Key == "is_shown"); + if (isShownNode is null) { + Logger.Warn($"is_shown node not found in decision {decisionName}!"); + return; + } + + const string additionalCondition = "\t\texists = title:e_byzantium.previous_holder"; + var additionalStatements = CKParser.parseString(additionalCondition, fileName).GetResult(); + var rootNodeForStatements = Parsers.ProcessStatements(fileName, decisionsFilePath, additionalStatements); + + var newChild = Child.NewLeafC(rootNodeForStatements.Leaves.First()); + isShownNode.SetTag(newChild.leaf.Key, newChild); + + StringBuilder sb = new(); + foreach (var child in processed.Children) { + sb.AppendLine(CKPrinter.api.prettyPrintStatement.Invoke(child.ToRaw)); + } + + // Output the modified file with UTF8-BOM encoding. + var outputFilePath = Path.Join(outputModPath, relativeDecisionsFilePath); + await File.WriteAllTextAsync(outputFilePath, sb.ToString(), Encoding.UTF8); + } +} \ No newline at end of file diff --git a/ImperatorToCK3/Outputter/WorldOutputter.cs b/ImperatorToCK3/Outputter/WorldOutputter.cs index 1d2e10e60..0b96f93f3 100644 --- a/ImperatorToCK3/Outputter/WorldOutputter.cs +++ b/ImperatorToCK3/Outputter/WorldOutputter.cs @@ -60,11 +60,15 @@ public static void OutputWorld(World ck3World, Imperator.World imperatorWorld, C BookmarkOutputter.OutputBookmark(ck3World, config, ck3World.LocDB) ); - + Task.WaitAll( + DecisionsOutputter.TweakERERestorationDecision(ck3World.LandedTitles, ck3World.ModFS, outputPath), - if (config.LegionConversion == LegionConversion.MenAtArms) { - MenAtArmsOutputter.OutputMenAtArms(outputName, ck3World.ModFS, ck3World.Characters, ck3World.MenAtArmsTypes); - } + Task.Run(() => { + if (config.LegionConversion == LegionConversion.MenAtArms) { + MenAtArmsOutputter.OutputMenAtArms(outputName, ck3World.ModFS, ck3World.Characters, ck3World.MenAtArmsTypes); + } + }) + ); // Localization should be output last, as it uses data written by other outputters. LocalizationOutputter.OutputLocalization(outputPath, ck3World); @@ -204,6 +208,8 @@ private static void CreateFolders(string outputPath) { SystemUtils.TryCreateFolder(Path.Combine(outputPath, "common", "coat_of_arms", "coat_of_arms")); SystemUtils.TryCreateFolder(Path.Combine(outputPath, "common", "culture", "cultures")); SystemUtils.TryCreateFolder(Path.Combine(outputPath, "common", "culture", "pillars")); + SystemUtils.TryCreateFolder(Path.Combine(outputPath, "common", "decisions")); + SystemUtils.TryCreateFolder(Path.Combine(outputPath, "common", "decisions", "dlc_decisions")); SystemUtils.TryCreateFolder(Path.Combine(outputPath, "common", "dna_data")); SystemUtils.TryCreateFolder(Path.Combine(outputPath, "common", "dynasties")); SystemUtils.TryCreateFolder(Path.Combine(outputPath, "common", "dynasty_houses"));