From 5f9ab59a8fd9ba4863572f9cf2d2d102bdb78dd8 Mon Sep 17 00:00:00 2001 From: IhateTrains Date: Tue, 15 Oct 2024 17:31:11 +0100 Subject: [PATCH] Fix ScriptValueCollection not loading values using values defined below (#524) * Fix ScriptValueCollection not loading values using values defined below * Update ScriptValueCollection.cs --- .../ScriptValueCollectionTests.cs | 2 ++ .../common/script_values/script_values.txt | 2 ++ commonItems/ScriptValueCollection.cs | 32 +++++++++++++------ commonItems/commonItems.csproj | 2 +- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/commonItems.UnitTests/ScriptValueCollectionTests.cs b/commonItems.UnitTests/ScriptValueCollectionTests.cs index fc0c1c55..1f02b719 100644 --- a/commonItems.UnitTests/ScriptValueCollectionTests.cs +++ b/commonItems.UnitTests/ScriptValueCollectionTests.cs @@ -17,6 +17,7 @@ public void SimpleScriptValuesAreReadFromGameAndMods() { scriptValueCollection.Keys.Should() .BeEquivalentTo( + "value_using_value_defined_below", "value1", "value2", "value3", @@ -31,6 +32,7 @@ public void SimpleScriptValuesAreReadFromGameAndMods() { "cheap_building_tier_1_cost" ); + Assert.Equal(-0.4d, scriptValueCollection["value_using_value_defined_below"]); // same as value2 Assert.Equal(0.4d, scriptValueCollection["value1"]); Assert.Equal(-0.4d, scriptValueCollection["value2"]); Assert.Equal(1d, scriptValueCollection["value3"]); diff --git a/commonItems.UnitTests/TestFiles/CK3/game/common/script_values/script_values.txt b/commonItems.UnitTests/TestFiles/CK3/game/common/script_values/script_values.txt index 2a055ba4..85361fd6 100644 --- a/commonItems.UnitTests/TestFiles/CK3/game/common/script_values/script_values.txt +++ b/commonItems.UnitTests/TestFiles/CK3/game/common/script_values/script_values.txt @@ -1,6 +1,8 @@ @variable1 = 420 @variable2 = 69 +value_using_value_defined_below = value2 # simple value using another simple value defined below + value1 = 0.4 value2 = -0.4 value3 = 1 diff --git a/commonItems/ScriptValueCollection.cs b/commonItems/ScriptValueCollection.cs index 5cbe25f1..b6fb0416 100644 --- a/commonItems/ScriptValueCollection.cs +++ b/commonItems/ScriptValueCollection.cs @@ -16,16 +16,28 @@ public class ScriptValueCollection : IReadOnlyDictionary { /// mod file system to load script values from public void LoadScriptValues(ModFilesystem modFilesystem) { Logger.Info("Reading script values..."); - - var parser = new Parser(); - parser.RegisterRegex(CommonRegexes.String, (reader, name) => { - var value = ParseValue(reader); - if (value is not null) { - dict[name] = (double)value; - } - }); - parser.RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreAndLogItem); - parser.ParseGameFolder("common/script_values", modFilesystem, "txt", recursive: true); + + // Some values can be used in other values before they are defined, for example: + // scheme_agent_general_bonuses_contribution_score_bonus_max_value = agent_max_skill_value + // agent_max_skill_value = 10 + // To handle this, we read the script values multiple times until no new values are added. + int addedValuesCount; + do { + addedValuesCount = 0; + + var parser = new Parser(); + parser.RegisterRegex(CommonRegexes.String, (reader, name) => { + var value = ParseValue(reader); + if (value is not null) { + if (!dict.ContainsKey(name)) { + ++addedValuesCount; + } + dict[name] = (double)value; + } + }); + parser.RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreAndLogItem); + parser.ParseGameFolder("common/script_values", modFilesystem, "txt", recursive: true); + } while (addedValuesCount > 0); } private double? ParseValue(BufferedReader reader) { diff --git a/commonItems/commonItems.csproj b/commonItems/commonItems.csproj index 5ce3a159..75addb4a 100644 --- a/commonItems/commonItems.csproj +++ b/commonItems/commonItems.csproj @@ -6,7 +6,7 @@ False PGCG.$(AssemblyName) - 14.1.1 + 14.1.2 PGCG https://github.com/ParadoxGameConverters/commonItems.NET https://github.com/ParadoxGameConverters/commonItems.NET