1
+ using commonItems ;
2
+ using commonItems . Mods ;
3
+ using CWTools . CSharp ;
4
+ using CWTools . Parser ;
5
+ using CWTools . Process ;
6
+ using ImperatorToCK3 . CK3 . Titles ;
7
+ using System . IO ;
8
+ using System . Linq ;
9
+ using System . Text ;
10
+ using System . Threading . Tasks ;
11
+
12
+ namespace ImperatorToCK3 . Outputter ;
13
+
14
+ internal static class DecisionsOutputter {
15
+ internal static async Task TweakERERestorationDecision ( Title . LandedTitles titles , ModFilesystem ck3ModFS ,
16
+ string outputModPath ) {
17
+ if ( ! titles . ContainsKey ( "e_byzantium" ) ) {
18
+ return ;
19
+ }
20
+
21
+ Logger . Info ( "Tweaking ERE restoration decision..." ) ;
22
+ const string relativeDecisionsFilePath = "common/decisions/dlc_decisions/ep3_decisions.txt" ;
23
+
24
+ // The file may already be in the output mod.
25
+ string ? decisionsFilePath ;
26
+ string fileInOutputPath = Path . Join ( outputModPath , relativeDecisionsFilePath ) ;
27
+ if ( File . Exists ( fileInOutputPath ) ) {
28
+ decisionsFilePath = fileInOutputPath ;
29
+ } else {
30
+ decisionsFilePath = ck3ModFS . GetActualFileLocation ( relativeDecisionsFilePath ) ;
31
+ }
32
+
33
+ if ( decisionsFilePath is null ) {
34
+ Logger . Warn ( $ "Can't find { relativeDecisionsFilePath } !") ;
35
+ return ;
36
+ }
37
+
38
+ Encoding . RegisterProvider ( CodePagesEncodingProvider . Instance ) ;
39
+
40
+ var fileName = Path . GetFileName ( decisionsFilePath ) ;
41
+
42
+ var text = await File . ReadAllTextAsync ( decisionsFilePath ) ;
43
+ var parsed = Parsers . ParseScriptFile ( fileName , text ) ;
44
+ var decisionsFile = parsed . GetResult ( ) ;
45
+
46
+ var processed = Parsers . ProcessStatements ( fileName , decisionsFilePath , decisionsFile ) ;
47
+
48
+ const string decisionName = "recreate_byzantine_empire_decision" ;
49
+ var decisionNode = processed . Nodes . FirstOrDefault ( n => n . Key == decisionName ) ;
50
+ if ( decisionNode is null ) {
51
+ Logger . Warn ( $ "Decision { decisionName } not found!") ;
52
+ return ;
53
+ }
54
+
55
+ var isShownNode = decisionNode . Nodes . FirstOrDefault ( n => n . Key == "is_shown" ) ;
56
+ if ( isShownNode is null ) {
57
+ Logger . Warn ( $ "is_shown node not found in decision { decisionName } !") ;
58
+ return ;
59
+ }
60
+
61
+ const string additionalCondition = "\t \t exists = title:e_byzantium.previous_holder" ;
62
+ var additionalStatements = CKParser . parseString ( additionalCondition , fileName ) . GetResult ( ) ;
63
+ var rootNodeForStatements = Parsers . ProcessStatements ( fileName , decisionsFilePath , additionalStatements ) ;
64
+
65
+ var newChild = Child . NewLeafC ( rootNodeForStatements . Leaves . First ( ) ) ;
66
+ isShownNode . SetTag ( newChild . leaf . Key , newChild ) ;
67
+
68
+ StringBuilder sb = new ( ) ;
69
+ foreach ( var child in processed . Children ) {
70
+ sb . AppendLine ( CKPrinter . api . prettyPrintStatement . Invoke ( child . ToRaw ) ) ;
71
+ }
72
+
73
+ // Output the modified file with UTF8-BOM encoding.
74
+ var outputFilePath = Path . Join ( outputModPath , relativeDecisionsFilePath ) ;
75
+ await File . WriteAllTextAsync ( outputFilePath , sb . ToString ( ) , Encoding . UTF8 ) ;
76
+ }
77
+ }
0 commit comments