Skip to content

Commit

Permalink
Fix ScriptValueCollection not loading values using values defined below
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Oct 15, 2024
1 parent 7bd3cb5 commit 2a28283
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions commonItems.UnitTests/ScriptValueCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void SimpleScriptValuesAreReadFromGameAndMods() {

scriptValueCollection.Keys.Should()
.BeEquivalentTo(
"value_using_value_defined_below",
"value1",
"value2",
"value3",
Expand All @@ -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"]);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 23 additions & 10 deletions commonItems/ScriptValueCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,29 @@ public class ScriptValueCollection : IReadOnlyDictionary<string, double> {
/// <param name="modFilesystem">mod file system to load script values from</param>
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);

}

Check notice on line 42 in commonItems/ScriptValueCollection.cs

View check run for this annotation

codefactor.io / CodeFactor

commonItems/ScriptValueCollection.cs#L42

A closing brace should not be preceded by a blank line. (SA1508)

private double? ParseValue(BufferedReader reader) {
Expand Down
2 changes: 1 addition & 1 deletion commonItems/commonItems.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<PackageId>PGCG.$(AssemblyName)</PackageId>
<Version>14.1.1</Version>
<Version>14.1.2</Version>
<Authors>PGCG</Authors>
<PackageProjectUrl>https://github.com/ParadoxGameConverters/commonItems.NET</PackageProjectUrl>
<RepositoryUrl>https://github.com/ParadoxGameConverters/commonItems.NET</RepositoryUrl>
Expand Down

0 comments on commit 2a28283

Please sign in to comment.