Skip to content

Commit

Permalink
Fix #528
Browse files Browse the repository at this point in the history
  • Loading branch information
bcssov committed Oct 30, 2024
1 parent a6966f4 commit a18c97c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/IronyModManager.Parser.Common/Parsers/BaseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 02-17-2020
//
// Last Modified By : Mario
// Last Modified On : 10-29-2024
// Last Modified On : 10-30-2024
// ***********************************************************************
// <copyright file="BaseParser.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -476,6 +476,11 @@ protected virtual IEnumerable<IDefinition> ParseSimpleTypes(IEnumerable<IScriptE
definition.Id = TrimId(item.Key);
definition.ValueType = ValueType.Variable;
}
else if (item.Key.StartsWith(Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase))
{
// Yay, paradox
definition.ContainsInlineIdentifier = true;
}
}

if (typeAssigned)
Expand Down
106 changes: 106 additions & 0 deletions src/IronyModManager.Parser.Tests/ParametrizedParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,79 @@ public void GetObjectId_on_first_level_should_yield_results()
parserResult.Any(p => p.Id.Equals("BIO_PROPULSION_5_BATTLESHIP")).Should().BeTrue();
}

/// <summary>
/// Defines the test method GetObjectId_on_first_level_should_yield_results_with_simple_inline.
/// </summary>
[Fact]
public void GetObjectId_on_first_level_should_yield_results_with_simple_inline()
{
DISetup.SetupContainer();

var sb = new System.Text.StringBuilder(328);
sb.AppendLine(@"# ship class placeholder");
sb.AppendLine(@"entity = corvette_entity");
sb.AppendLine(@"resources = { category = starbase_stations }");
sb.AppendLine(@"potential_construction = { always = no }");
sb.AppendLine(@"possible_construction = { always = no }");
sb.AppendLine(@"is_designable = no");
sb.AppendLine(@"enable_default_design = yes");
sb.AppendLine(@"prerequisites = { }");
sb.AppendLine(@"class = shipclass_starbase");
sb.AppendLine(@"icon_frame = 1");
sb.AppendLine(@"icon = ship_size_military_station");

var sb2 = new StringBuilder(77);
sb2.AppendLine(@"rs_heavy_dreadnought = {");
sb2.AppendLine(@" inline_script = giga_placeholders/ship_sizes");
sb2.AppendLine(@"}");

var parser = new ParametrizedParser(new CodeParser(new Logger()));
var result = parser.Process(sb.ToString(), sb2.ToString());
result.Should().NotBeNullOrEmpty();
var m = DIResolver.Get<IParserManager>();
var parserResult = m.Parse(new ParserManagerArgs { File = "common\\ship_sizes\\dummy.txt", GameType = "Stellaris", IsBinary = false, Lines = result.SplitOnNewLine() });
parserResult.Count().Should().Be(1);
parserResult.Any(p => p.Id.Equals("rs_heavy_dreadnought")).Should().BeTrue();
}

/// <summary>
/// Defines the test method GetObjectId_should_yield_results_with_simple_inline.
/// </summary>
[Fact]
public void GetObjectId_should_yield_results_with_simple_inline()
{
DISetup.SetupContainer();

var sb = new System.Text.StringBuilder();
sb.AppendLine(@"rs_heavy_dreadnought = {");
sb.AppendLine(@"# ship class placeholder");
sb.AppendLine(@"entity = corvette_entity");
sb.AppendLine(@"resources = { category = starbase_stations }");
sb.AppendLine(@"potential_construction = { always = no }");
sb.AppendLine(@"possible_construction = { always = no }");
sb.AppendLine(@"is_designable = no");
sb.AppendLine(@"enable_default_design = yes");
sb.AppendLine(@"prerequisites = { }");
sb.AppendLine(@"class = shipclass_starbase");
sb.AppendLine(@"icon_frame = 1");
sb.AppendLine(@"icon = ship_size_military_station");
sb.AppendLine(@"}");


var sb2 = new StringBuilder(77);
sb2.AppendLine(@"");
sb2.AppendLine(@"inline_script = giga_placeholders/ship_sizes");
sb2.AppendLine(@"");

var parser = new ParametrizedParser(new CodeParser(new Logger()));
var result = parser.Process(sb.ToString(), sb2.ToString());
result.Should().NotBeNullOrEmpty();
var m = DIResolver.Get<IParserManager>();
var parserResult = m.Parse(new ParserManagerArgs { File = "common\\ship_sizes\\dummy.txt", GameType = "Stellaris", IsBinary = false, Lines = result.SplitOnNewLine() });
parserResult.Count().Should().Be(1);
parserResult.Any(p => p.Id.Equals("rs_heavy_dreadnought")).Should().BeTrue();
}


/// <summary>
/// Defines the test method GetScriptPath_should_yield_results.
Expand Down Expand Up @@ -1090,5 +1163,38 @@ public void GetScriptPath_as_sub_element_should_yield_results()
var result = parser.GetScriptPath(sb.ToString());
result.Should().Be("grand_archive\\mutations\\core_components\\component_thrusters_bio");
}

/// <summary>
/// Defines the test method GetScriptPath_as_sub_element_should_handle_simple_inline_scripts.
/// </summary>
[Fact]
public void GetScriptPath_as_sub_element_should_handle_simple_inline_scripts()
{
DISetup.SetupContainer();

var sb = new StringBuilder(77);
sb.AppendLine(@"rs_heavy_dreadnought = {");
sb.AppendLine(@" inline_script = giga_placeholders/ship_sizes");
sb.AppendLine(@"}");

var parser = new ParametrizedParser(new CodeParser(new Logger()));
var result = parser.GetScriptPath(sb.ToString());
result.Should().Be("giga_placeholders\\ship_sizes");
}

[Fact]
public void GetScriptPath_as_should_handle_simple_inline_scripts()
{
DISetup.SetupContainer();

var sb = new StringBuilder(77);
sb.AppendLine(@"");
sb.AppendLine(@"inline_script = giga_placeholders/ship_sizes");
sb.AppendLine(@"");

var parser = new ParametrizedParser(new CodeParser(new Logger()));
var result = parser.GetScriptPath(sb.ToString());
result.Should().Be("giga_placeholders\\ship_sizes");
}
}
}
37 changes: 32 additions & 5 deletions src/IronyModManager.Parser/ParametrizedParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public string GetScriptPath(string parameters)
{
if (elParams.Values.Count(p => p.Key.Equals(Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase)) == 1)
{
var simpleResult = elParams.Values.FirstOrDefault(p => !string.IsNullOrWhiteSpace(p.Value));
if (simpleResult != null)
{
return simpleResult.Value.StandardizeDirectorySeparator();
}
var elObj = elParams.Values.FirstOrDefault(p => p.Values != null);
var match = elObj?.Values.FirstOrDefault(p => p.Key.Equals(Script, StringComparison.OrdinalIgnoreCase));
if (match != null)
Expand All @@ -88,10 +93,18 @@ public string GetScriptPath(string parameters)
else if (elParams.Values.Count() == 1 && elParams.Values.FirstOrDefault()!.Values.Count(p => p.Key.Equals(Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase)) == 1)
{
var elObj = elParams.Values.FirstOrDefault(p => p.Values != null)?.Values.FirstOrDefault();
var match = elObj?.Values.FirstOrDefault(p => p.Key.Equals(Script, StringComparison.OrdinalIgnoreCase));
if (match != null)
if (elObj != null)
{
return (match.Value ?? string.Empty).Trim(Quotes).StandardizeDirectorySeparator();
if (!string.IsNullOrWhiteSpace(elObj.Value))
{
return elObj.Value.StandardizeDirectorySeparator();
}

var match = elObj.Values.FirstOrDefault(p => p.Key.Equals(Script, StringComparison.OrdinalIgnoreCase));
if (match != null)
{
return (match.Value ?? string.Empty).Trim(Quotes).StandardizeDirectorySeparator();
}
}
}
}
Expand Down Expand Up @@ -133,8 +146,8 @@ public string Process(string code, string parameters)
else if (elParams.Values.Count() == 1 && elParams.Values.FirstOrDefault()!.Values.Count(p => p.Key.Equals(Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase)) == 1)
{
var processed = code;
var elObj = elParams.Values.FirstOrDefault(p => p.Values != null)?.Values.FirstOrDefault();
if (elObj != null)
var elObj = elParams.Values.FirstOrDefault(p => p.Values != null)?.Values?.FirstOrDefault();
if (elObj is { Values: not null })
{
foreach (var value in elObj.Values)
{
Expand All @@ -159,6 +172,20 @@ public string Process(string code, string parameters)
}
}
}
else if (!string.IsNullOrWhiteSpace(elObj?.Value))
{
var replacementCode = codeParser.ParseScriptWithoutValidation(processed.SplitOnNewLine(), string.Empty);
if (replacementCode is { Values: not null, Error: null })
{
var newCode = elParams.Values.FirstOrDefault(p => p.Values != null);
if (newCode != null)
{
newCode.Values = replacementCode.Values;
processed = codeParser.FormatCode(newCode);
return processed;
}
}
}
}
}

Expand Down
22 changes: 19 additions & 3 deletions src/IronyModManager.Services/ModPatchCollectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public virtual async Task<IConflictResult> FindConflictsAsync(IIndexedDefinition
foreach (var item in allDefs)
{
var addDefault = true;
if (item.Id.Equals(Parser.Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase) || item.ContainsInlineIdentifier)
if ((item.Id.Equals(Parser.Common.Constants.Stellaris.InlineScriptId, StringComparison.OrdinalIgnoreCase) || item.ContainsInlineIdentifier) && item.File.StartsWith(provider.InlineScriptsPath))
{
addDefault = false;
var path = Path.Combine(Parser.Common.Constants.Stellaris.InlineScripts, parametrizedParser.GetScriptPath(item.Code));
Expand Down Expand Up @@ -568,6 +568,11 @@ public virtual async Task<IConflictResult> FindConflictsAsync(IIndexedDefinition
prunedInlineDefinitions = null;
GCRunner.RunGC(GCCollectionMode.Optimized, false);

// Redeclare stuff
fileKeys = await indexedDefinitions.GetAllFileKeysAsync();
typeAndIdKeys = await indexedDefinitions.GetAllTypeAndIdKeysAsync();
overwritten = (await indexedDefinitions.GetByValueTypeAsync(ValueType.OverwrittenObject)).Concat(await indexedDefinitions.GetByValueTypeAsync(ValueType.OverwrittenObjectSingleFile));
empty = await indexedDefinitions.GetByValueTypeAsync(ValueType.EmptyFile);

var stopWatch = new Stopwatch();
stopWatch.Start();
Expand Down Expand Up @@ -3074,6 +3079,16 @@ protected virtual IEnumerable<IDefinition> ParseModFiles(IGame game, IEnumerable
}
}

var validationType = ValidationType.Full;
if (definitionInfoProvider.SupportsInlineScripts)
{
if (fileInfo.FileName != null && fileInfo.FileName.StartsWith(definitionInfoProvider.InlineScriptsPath))
{
// Skip inline validation
validationType = ValidationType.SkipAll;
}
}

var fileDefs = parserManager.Parse(new ParserManagerArgs
{
ContentSHA = fileInfo.ContentSHA,
Expand All @@ -3083,7 +3098,8 @@ protected virtual IEnumerable<IDefinition> ParseModFiles(IGame game, IEnumerable
ModDependencies = modObject.Dependencies,
ModName = modObject.Name,
FileLastModified = fileInfo.LastModified,
IsBinary = fileInfo.IsBinary
IsBinary = fileInfo.IsBinary,
ValidationType = validationType
});
if (fileDefs.Any())
{
Expand Down Expand Up @@ -3285,7 +3301,7 @@ async Task handleDefinition(IDefinition item)
var others = parsed.Where(p => p.ValueType != ValueType.Variable && p.ValueType != ValueType.Namespace);
foreach (var other in others)
{
var variables = parsed.Where(p => p.ValueType == ValueType.Variable || p.ValueType == ValueType.Namespace);
var variables = parsed.Where(p => p.ValueType is ValueType.Variable or ValueType.Namespace);
other.Variables = variables;
var exportCopy = CopyDefinition(other);
var allType = (await conflictResult.AllConflicts.GetByTypeAndIdAsync(definition!.TypeAndId)).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 02-12-2020
//
// Last Modified By : Mario
// Last Modified On : 10-29-2024
// Last Modified On : 10-30-2024
// ***********************************************************************
// <copyright file="GameRegistration.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -39,12 +39,12 @@ public class GameRegistration : PostStartup
/// <summary>
/// The hoi4 cache version
/// </summary>
private const int HOI4CacheVersion = 16;
private const int HOI4CacheVersion = 17;

/// <summary>
/// The stellaris cache version
/// </summary>
private const int StellarisCacheVersion = 26;
private const int StellarisCacheVersion = 27;

/// <summary>
/// The path resolver
Expand Down

0 comments on commit a18c97c

Please sign in to comment.