Skip to content

Commit

Permalink
WIP ERE restoration decision tweaking
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Dec 15, 2024
1 parent 4107f2d commit 6ffb0c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
34 changes: 21 additions & 13 deletions ImperatorToCK3/Outputter/DecisionsOutputter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImperatorToCK3.Outputter;

internal static class DecisionsOutputter {
internal static void TweakERERestorationDecision(Title.LandedTitles titles, ModFilesystem ck3ModFS, string outputModPath) {
internal static async Task TweakERERestorationDecision(Title.LandedTitles titles, ModFilesystem ck3ModFS,
string outputModPath) {
if (!titles.ContainsKey("e_byzantium")) {
return;
}

Logger.Info("Tweaking ERE restoration decision...");
string relativeDecisionsFilePath = "common/decisions/dlc_decisions/ep3_decisions.txt";
const string relativeDecisionsFilePath = "common/decisions/dlc_decisions/ep3_decisions.txt";
string? 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 statements = CKParser.parseFile(decisionsFilePath).GetResult();
var rootNode = Parsers.ProcessStatements(fileName, decisionsFilePath, statements);
Expand All @@ -38,26 +40,32 @@ internal static void TweakERERestorationDecision(Title.LandedTitles titles, ModF
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;
}

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);
allChildren.Add(Child.NewNodeC(rootNodeForStatements.Nodes.First()));
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;

// Output the modified file with UTF8-BOM encoding.
var output = rootNode.ToRaw;
var fsharpListToOutput = ListModule.OfSeq([output]);

var kvl = ListModule.OfSeq([rootNode.ToRaw]);
var outputFilePath = Path.Join(outputModPath, relativeDecisionsFilePath);
File.WriteAllText(outputFilePath, CKPrinter.printTopLevelKeyValueList(fsharpListToOutput), Encoding.UTF8);// TODO: check how this is outputted

await File.WriteAllTextAsync(outputFilePath, CKPrinter.printTopLevelKeyValueList(kvl), Encoding.UTF8); // TODO: check how this is outputted
}
}
4 changes: 3 additions & 1 deletion ImperatorToCK3/Outputter/WorldOutputter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void OutputWorld(World ck3World, Imperator.World imperatorWorld, C

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

DecisionsOutputter.TweakERERestorationDecision(ck3World.LandedTitles, ck3World.ModFS, outputPath),
DecisionsOutputter.TweakERERestorationDecision(ck3World.LandedTitles, ck3World.ModFS, outputPath)
);


Expand Down Expand Up @@ -206,6 +206,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"));
Expand Down

0 comments on commit 6ffb0c1

Please sign in to comment.