Skip to content

Commit

Permalink
Polish the decision tweaking function
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Dec 15, 2024
1 parent 6ffb0c1 commit b78b48c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
46 changes: 26 additions & 20 deletions ImperatorToCK3/Outputter/DecisionsOutputter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using CWTools.Parser;
using CWTools.Process;
using ImperatorToCK3.CK3.Titles;
using Microsoft.FSharp.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -22,7 +20,16 @@ internal static async Task TweakERERestorationDecision(Title.LandedTitles titles

Logger.Info("Tweaking ERE restoration decision...");
const string relativeDecisionsFilePath = "common/decisions/dlc_decisions/ep3_decisions.txt";
string? decisionsFilePath = ck3ModFS.GetActualFileLocation(relativeDecisionsFilePath);

// 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;
Expand All @@ -31,12 +38,15 @@ internal static async Task TweakERERestorationDecision(Title.LandedTitles titles
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

var fileName = Path.GetFileName(decisionsFilePath);
var statements = CKParser.parseFile(decisionsFilePath).GetResult();
var rootNode = Parsers.ProcessStatements(fileName, decisionsFilePath, statements);
var nodes = rootNode.Nodes.ToArray();

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 = nodes.FirstOrDefault(n => n.Key == decisionName);
var decisionNode = processed.Nodes.FirstOrDefault(n => n.Key == decisionName);
if (decisionNode is null) {
Logger.Warn($"Decision {decisionName} not found!");
return;
Expand All @@ -48,24 +58,20 @@ internal static async Task TweakERERestorationDecision(Title.LandedTitles titles
return;
}

List<Child> allChildren = isShownNode.AllChildren;
const string additionalCondition = "\t\texists = title:e_byzantium.previous_holder";
var additionalStatements = CKParser.parseString(additionalCondition, fileName).GetResult();
foreach (var statement in additionalStatements) {
Logger.Notice($"Adding statement: {statement}");
}
var rootNodeForStatements = Parsers.ProcessStatements(fileName, decisionsFilePath, additionalStatements);
Logger.Notice($"Root node for additional statements: {rootNodeForStatements.ToRaw}");

allChildren.Add(Child.NewLeafC(rootNodeForStatements.Leaves.First()));

// allChildren.Add(Child.NewNodeC(rootNodeForStatements.Nodes.First()));
isShownNode.AllChildren = allChildren;

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 kvl = ListModule.OfSeq([rootNode.ToRaw]);
var outputFilePath = Path.Join(outputModPath, relativeDecisionsFilePath);

await File.WriteAllTextAsync(outputFilePath, CKPrinter.printTopLevelKeyValueList(kvl), Encoding.UTF8); // TODO: check how this is outputted
await File.WriteAllTextAsync(outputFilePath, sb.ToString(), Encoding.UTF8);
}
}
16 changes: 9 additions & 7 deletions ImperatorToCK3/Outputter/WorldOutputter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ public static void OutputWorld(World ck3World, Imperator.World imperatorWorld, C
CoatOfArmsOutputter.OutputCoas(outputPath, ck3World.LandedTitles, ck3World.Dynasties, ck3World.CK3CoaMapper),
Task.Run(() => CoatOfArmsOutputter.CopyCoaPatterns(imperatorWorld.ModFS, outputPath)),

BookmarkOutputter.OutputBookmark(ck3World, config, ck3World.LocDB),

DecisionsOutputter.TweakERERestorationDecision(ck3World.LandedTitles, ck3World.ModFS, outputPath)
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);
Expand Down

0 comments on commit b78b48c

Please sign in to comment.