Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ScriptValueCollection not loading values using values defined below #524

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 22 additions & 10 deletions commonItems/ScriptValueCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@ 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);
}

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
Loading