Skip to content

Commit

Permalink
Disable the ERE restoration decision if the ERE has no holder in hist…
Browse files Browse the repository at this point in the history
…ory (#2364) #minor

closes #2359
  • Loading branch information
IhateTrains authored Dec 15, 2024
1 parent 2c5c62b commit 3e20ab2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
5 changes: 2 additions & 3 deletions ImperatorToCK3/Outputter/CulturesOutputter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -139,7 +138,7 @@ private static void OutputCCULanguageParameters(string outputModPath, ModFilesys
.Where(s => s is not null)
.Cast<Types.Statement>()
.ToList();
var fsharpList = ListModule.OfSeq(tooutput);
var fsharpList = ListModule.OfSeq(toOutput);

var outputFilePath = Path.Join(outputModPath, relativePath);
// Output the file with UTF8-BOM encoding.
Expand Down
77 changes: 77 additions & 0 deletions ImperatorToCK3/Outputter/DecisionsOutputter.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 10 additions & 4 deletions ImperatorToCK3/Outputter/WorldOutputter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down

0 comments on commit 3e20ab2

Please sign in to comment.