Skip to content

Commit a18c97c

Browse files
committed
Fix #528
1 parent a6966f4 commit a18c97c

File tree

5 files changed

+166
-12
lines changed

5 files changed

+166
-12
lines changed

src/IronyModManager.Parser.Common/Parsers/BaseParser.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Created : 02-17-2020
55
//
66
// Last Modified By : Mario
7-
// Last Modified On : 10-29-2024
7+
// Last Modified On : 10-30-2024
88
// ***********************************************************************
99
// <copyright file="BaseParser.cs" company="Mario">
1010
// Mario
@@ -476,6 +476,11 @@ protected virtual IEnumerable<IDefinition> ParseSimpleTypes(IEnumerable<IScriptE
476476
definition.Id = TrimId(item.Key);
477477
definition.ValueType = ValueType.Variable;
478478
}
479+
else if (item.Key.StartsWith(Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase))
480+
{
481+
// Yay, paradox
482+
definition.ContainsInlineIdentifier = true;
483+
}
479484
}
480485

481486
if (typeAssigned)

src/IronyModManager.Parser.Tests/ParametrizedParserTests.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,79 @@ public void GetObjectId_on_first_level_should_yield_results()
10131013
parserResult.Any(p => p.Id.Equals("BIO_PROPULSION_5_BATTLESHIP")).Should().BeTrue();
10141014
}
10151015

1016+
/// <summary>
1017+
/// Defines the test method GetObjectId_on_first_level_should_yield_results_with_simple_inline.
1018+
/// </summary>
1019+
[Fact]
1020+
public void GetObjectId_on_first_level_should_yield_results_with_simple_inline()
1021+
{
1022+
DISetup.SetupContainer();
1023+
1024+
var sb = new System.Text.StringBuilder(328);
1025+
sb.AppendLine(@"# ship class placeholder");
1026+
sb.AppendLine(@"entity = corvette_entity");
1027+
sb.AppendLine(@"resources = { category = starbase_stations }");
1028+
sb.AppendLine(@"potential_construction = { always = no }");
1029+
sb.AppendLine(@"possible_construction = { always = no }");
1030+
sb.AppendLine(@"is_designable = no");
1031+
sb.AppendLine(@"enable_default_design = yes");
1032+
sb.AppendLine(@"prerequisites = { }");
1033+
sb.AppendLine(@"class = shipclass_starbase");
1034+
sb.AppendLine(@"icon_frame = 1");
1035+
sb.AppendLine(@"icon = ship_size_military_station");
1036+
1037+
var sb2 = new StringBuilder(77);
1038+
sb2.AppendLine(@"rs_heavy_dreadnought = {");
1039+
sb2.AppendLine(@" inline_script = giga_placeholders/ship_sizes");
1040+
sb2.AppendLine(@"}");
1041+
1042+
var parser = new ParametrizedParser(new CodeParser(new Logger()));
1043+
var result = parser.Process(sb.ToString(), sb2.ToString());
1044+
result.Should().NotBeNullOrEmpty();
1045+
var m = DIResolver.Get<IParserManager>();
1046+
var parserResult = m.Parse(new ParserManagerArgs { File = "common\\ship_sizes\\dummy.txt", GameType = "Stellaris", IsBinary = false, Lines = result.SplitOnNewLine() });
1047+
parserResult.Count().Should().Be(1);
1048+
parserResult.Any(p => p.Id.Equals("rs_heavy_dreadnought")).Should().BeTrue();
1049+
}
1050+
1051+
/// <summary>
1052+
/// Defines the test method GetObjectId_should_yield_results_with_simple_inline.
1053+
/// </summary>
1054+
[Fact]
1055+
public void GetObjectId_should_yield_results_with_simple_inline()
1056+
{
1057+
DISetup.SetupContainer();
1058+
1059+
var sb = new System.Text.StringBuilder();
1060+
sb.AppendLine(@"rs_heavy_dreadnought = {");
1061+
sb.AppendLine(@"# ship class placeholder");
1062+
sb.AppendLine(@"entity = corvette_entity");
1063+
sb.AppendLine(@"resources = { category = starbase_stations }");
1064+
sb.AppendLine(@"potential_construction = { always = no }");
1065+
sb.AppendLine(@"possible_construction = { always = no }");
1066+
sb.AppendLine(@"is_designable = no");
1067+
sb.AppendLine(@"enable_default_design = yes");
1068+
sb.AppendLine(@"prerequisites = { }");
1069+
sb.AppendLine(@"class = shipclass_starbase");
1070+
sb.AppendLine(@"icon_frame = 1");
1071+
sb.AppendLine(@"icon = ship_size_military_station");
1072+
sb.AppendLine(@"}");
1073+
1074+
1075+
var sb2 = new StringBuilder(77);
1076+
sb2.AppendLine(@"");
1077+
sb2.AppendLine(@"inline_script = giga_placeholders/ship_sizes");
1078+
sb2.AppendLine(@"");
1079+
1080+
var parser = new ParametrizedParser(new CodeParser(new Logger()));
1081+
var result = parser.Process(sb.ToString(), sb2.ToString());
1082+
result.Should().NotBeNullOrEmpty();
1083+
var m = DIResolver.Get<IParserManager>();
1084+
var parserResult = m.Parse(new ParserManagerArgs { File = "common\\ship_sizes\\dummy.txt", GameType = "Stellaris", IsBinary = false, Lines = result.SplitOnNewLine() });
1085+
parserResult.Count().Should().Be(1);
1086+
parserResult.Any(p => p.Id.Equals("rs_heavy_dreadnought")).Should().BeTrue();
1087+
}
1088+
10161089

10171090
/// <summary>
10181091
/// Defines the test method GetScriptPath_should_yield_results.
@@ -1090,5 +1163,38 @@ public void GetScriptPath_as_sub_element_should_yield_results()
10901163
var result = parser.GetScriptPath(sb.ToString());
10911164
result.Should().Be("grand_archive\\mutations\\core_components\\component_thrusters_bio");
10921165
}
1166+
1167+
/// <summary>
1168+
/// Defines the test method GetScriptPath_as_sub_element_should_handle_simple_inline_scripts.
1169+
/// </summary>
1170+
[Fact]
1171+
public void GetScriptPath_as_sub_element_should_handle_simple_inline_scripts()
1172+
{
1173+
DISetup.SetupContainer();
1174+
1175+
var sb = new StringBuilder(77);
1176+
sb.AppendLine(@"rs_heavy_dreadnought = {");
1177+
sb.AppendLine(@" inline_script = giga_placeholders/ship_sizes");
1178+
sb.AppendLine(@"}");
1179+
1180+
var parser = new ParametrizedParser(new CodeParser(new Logger()));
1181+
var result = parser.GetScriptPath(sb.ToString());
1182+
result.Should().Be("giga_placeholders\\ship_sizes");
1183+
}
1184+
1185+
[Fact]
1186+
public void GetScriptPath_as_should_handle_simple_inline_scripts()
1187+
{
1188+
DISetup.SetupContainer();
1189+
1190+
var sb = new StringBuilder(77);
1191+
sb.AppendLine(@"");
1192+
sb.AppendLine(@"inline_script = giga_placeholders/ship_sizes");
1193+
sb.AppendLine(@"");
1194+
1195+
var parser = new ParametrizedParser(new CodeParser(new Logger()));
1196+
var result = parser.GetScriptPath(sb.ToString());
1197+
result.Should().Be("giga_placeholders\\ship_sizes");
1198+
}
10931199
}
10941200
}

src/IronyModManager.Parser/ParametrizedParser.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public string GetScriptPath(string parameters)
7878
{
7979
if (elParams.Values.Count(p => p.Key.Equals(Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase)) == 1)
8080
{
81+
var simpleResult = elParams.Values.FirstOrDefault(p => !string.IsNullOrWhiteSpace(p.Value));
82+
if (simpleResult != null)
83+
{
84+
return simpleResult.Value.StandardizeDirectorySeparator();
85+
}
8186
var elObj = elParams.Values.FirstOrDefault(p => p.Values != null);
8287
var match = elObj?.Values.FirstOrDefault(p => p.Key.Equals(Script, StringComparison.OrdinalIgnoreCase));
8388
if (match != null)
@@ -88,10 +93,18 @@ public string GetScriptPath(string parameters)
8893
else if (elParams.Values.Count() == 1 && elParams.Values.FirstOrDefault()!.Values.Count(p => p.Key.Equals(Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase)) == 1)
8994
{
9095
var elObj = elParams.Values.FirstOrDefault(p => p.Values != null)?.Values.FirstOrDefault();
91-
var match = elObj?.Values.FirstOrDefault(p => p.Key.Equals(Script, StringComparison.OrdinalIgnoreCase));
92-
if (match != null)
96+
if (elObj != null)
9397
{
94-
return (match.Value ?? string.Empty).Trim(Quotes).StandardizeDirectorySeparator();
98+
if (!string.IsNullOrWhiteSpace(elObj.Value))
99+
{
100+
return elObj.Value.StandardizeDirectorySeparator();
101+
}
102+
103+
var match = elObj.Values.FirstOrDefault(p => p.Key.Equals(Script, StringComparison.OrdinalIgnoreCase));
104+
if (match != null)
105+
{
106+
return (match.Value ?? string.Empty).Trim(Quotes).StandardizeDirectorySeparator();
107+
}
95108
}
96109
}
97110
}
@@ -133,8 +146,8 @@ public string Process(string code, string parameters)
133146
else if (elParams.Values.Count() == 1 && elParams.Values.FirstOrDefault()!.Values.Count(p => p.Key.Equals(Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase)) == 1)
134147
{
135148
var processed = code;
136-
var elObj = elParams.Values.FirstOrDefault(p => p.Values != null)?.Values.FirstOrDefault();
137-
if (elObj != null)
149+
var elObj = elParams.Values.FirstOrDefault(p => p.Values != null)?.Values?.FirstOrDefault();
150+
if (elObj is { Values: not null })
138151
{
139152
foreach (var value in elObj.Values)
140153
{
@@ -159,6 +172,20 @@ public string Process(string code, string parameters)
159172
}
160173
}
161174
}
175+
else if (!string.IsNullOrWhiteSpace(elObj?.Value))
176+
{
177+
var replacementCode = codeParser.ParseScriptWithoutValidation(processed.SplitOnNewLine(), string.Empty);
178+
if (replacementCode is { Values: not null, Error: null })
179+
{
180+
var newCode = elParams.Values.FirstOrDefault(p => p.Values != null);
181+
if (newCode != null)
182+
{
183+
newCode.Values = replacementCode.Values;
184+
processed = codeParser.FormatCode(newCode);
185+
return processed;
186+
}
187+
}
188+
}
162189
}
163190
}
164191

src/IronyModManager.Services/ModPatchCollectionService.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ public virtual async Task<IConflictResult> FindConflictsAsync(IIndexedDefinition
466466
foreach (var item in allDefs)
467467
{
468468
var addDefault = true;
469-
if (item.Id.Equals(Parser.Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase) || item.ContainsInlineIdentifier)
469+
if ((item.Id.Equals(Parser.Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase) || item.ContainsInlineIdentifier) && item.File.StartsWith(provider.InlineScriptsPath))
470470
{
471471
addDefault = false;
472472
var path = Path.Combine(Parser.Common.Constants.Stellaris.InlineScripts, parametrizedParser.GetScriptPath(item.Code));
@@ -568,6 +568,11 @@ public virtual async Task<IConflictResult> FindConflictsAsync(IIndexedDefinition
568568
prunedInlineDefinitions = null;
569569
GCRunner.RunGC(GCCollectionMode.Optimized, false);
570570

571+
// Redeclare stuff
572+
fileKeys = await indexedDefinitions.GetAllFileKeysAsync();
573+
typeAndIdKeys = await indexedDefinitions.GetAllTypeAndIdKeysAsync();
574+
overwritten = (await indexedDefinitions.GetByValueTypeAsync(ValueType.OverwrittenObject)).Concat(await indexedDefinitions.GetByValueTypeAsync(ValueType.OverwrittenObjectSingleFile));
575+
empty = await indexedDefinitions.GetByValueTypeAsync(ValueType.EmptyFile);
571576

572577
var stopWatch = new Stopwatch();
573578
stopWatch.Start();
@@ -3074,6 +3079,16 @@ protected virtual IEnumerable<IDefinition> ParseModFiles(IGame game, IEnumerable
30743079
}
30753080
}
30763081

3082+
var validationType = ValidationType.Full;
3083+
if (definitionInfoProvider.SupportsInlineScripts)
3084+
{
3085+
if (fileInfo.FileName != null && fileInfo.FileName.StartsWith(definitionInfoProvider.InlineScriptsPath))
3086+
{
3087+
// Skip inline validation
3088+
validationType = ValidationType.SkipAll;
3089+
}
3090+
}
3091+
30773092
var fileDefs = parserManager.Parse(new ParserManagerArgs
30783093
{
30793094
ContentSHA = fileInfo.ContentSHA,
@@ -3083,7 +3098,8 @@ protected virtual IEnumerable<IDefinition> ParseModFiles(IGame game, IEnumerable
30833098
ModDependencies = modObject.Dependencies,
30843099
ModName = modObject.Name,
30853100
FileLastModified = fileInfo.LastModified,
3086-
IsBinary = fileInfo.IsBinary
3101+
IsBinary = fileInfo.IsBinary,
3102+
ValidationType = validationType
30873103
});
30883104
if (fileDefs.Any())
30893105
{
@@ -3285,7 +3301,7 @@ async Task handleDefinition(IDefinition item)
32853301
var others = parsed.Where(p => p.ValueType != ValueType.Variable && p.ValueType != ValueType.Namespace);
32863302
foreach (var other in others)
32873303
{
3288-
var variables = parsed.Where(p => p.ValueType == ValueType.Variable || p.ValueType == ValueType.Namespace);
3304+
var variables = parsed.Where(p => p.ValueType is ValueType.Variable or ValueType.Namespace);
32893305
other.Variables = variables;
32903306
var exportCopy = CopyDefinition(other);
32913307
var allType = (await conflictResult.AllConflicts.GetByTypeAndIdAsync(definition!.TypeAndId)).ToList();

src/IronyModManager.Services/Registrations/GameRegistration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Created : 02-12-2020
55
//
66
// Last Modified By : Mario
7-
// Last Modified On : 10-29-2024
7+
// Last Modified On : 10-30-2024
88
// ***********************************************************************
99
// <copyright file="GameRegistration.cs" company="Mario">
1010
// Mario
@@ -39,12 +39,12 @@ public class GameRegistration : PostStartup
3939
/// <summary>
4040
/// The hoi4 cache version
4141
/// </summary>
42-
private const int HOI4CacheVersion = 16;
42+
private const int HOI4CacheVersion = 17;
4343

4444
/// <summary>
4545
/// The stellaris cache version
4646
/// </summary>
47-
private const int StellarisCacheVersion = 26;
47+
private const int StellarisCacheVersion = 27;
4848

4949
/// <summary>
5050
/// The path resolver

0 commit comments

Comments
 (0)