From 695ce382ec6dbdddcad239176f03ba23e7f635d6 Mon Sep 17 00:00:00 2001 From: Freagarach Date: Thu, 9 Dec 2021 16:22:52 +0000 Subject: [PATCH] Fix Trainer/Researcher without entities/techs. It was explicitly allowed in 0c4f59d0a7, but not all references to `this.template` were properly checked. Noticed by: @nwtour Differential revision: https://code.wildfiregames.com/D4357 Tested by: @nwtour Comments by: @Stan This was SVN commit r26043. --- .../data/mods/public/simulation/components/Researcher.js | 8 ++------ .../data/mods/public/simulation/components/Trainer.js | 8 +++----- .../simulation/components/tests/test_Researcher.js | 8 ++++++++ .../public/simulation/components/tests/test_Trainer.js | 9 +++++++++ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/binaries/data/mods/public/simulation/components/Researcher.js b/binaries/data/mods/public/simulation/components/Researcher.js index 94b6ed0131..ea5de6d2ab 100644 --- a/binaries/data/mods/public/simulation/components/Researcher.js +++ b/binaries/data/mods/public/simulation/components/Researcher.js @@ -218,11 +218,7 @@ Researcher.prototype.Deserialize = function(data) */ Researcher.prototype.GetTechnologiesList = function() { - if (!this.template.Technologies) - return []; - - const string = ApplyValueModificationsToEntity("Researcher/Technologies/_string", this.template.Technologies._string, this.entity); - + const string = ApplyValueModificationsToEntity("Researcher/Technologies/_string", this.template?.Technologies?._string || "", this.entity); if (!string) return []; @@ -309,7 +305,7 @@ Researcher.prototype.GetTechnologiesList = function() Researcher.prototype.GetTechCostMultiplier = function() { const techCostMultiplier = {}; - for (const res in this.template.TechCostMultiplier) + for (const res in this.template?.TechCostMultiplier) techCostMultiplier[res] = ApplyValueModificationsToEntity( "Researcher/TechCostMultiplier/" + res, +this.template.TechCostMultiplier[res], diff --git a/binaries/data/mods/public/simulation/components/Trainer.js b/binaries/data/mods/public/simulation/components/Trainer.js index 4d7ad215a2..879b680b66 100644 --- a/binaries/data/mods/public/simulation/components/Trainer.js +++ b/binaries/data/mods/public/simulation/components/Trainer.js @@ -461,10 +461,8 @@ Trainer.prototype.CalculateEntitiesMap = function() // Don't reset the map, it's used below to update entities. if (!this.entitiesMap) this.entitiesMap = new Map(); - if (!this.template.Entities) - return; - const string = this.template.Entities._string; + const string = this.template?.Entities?._string || ""; // Tokens can be added -> process an empty list to get them. let addedTokens = ApplyValueModificationsToEntity("Trainer/Entities/_string", "", this.entity); if (!addedTokens && !string) @@ -577,7 +575,7 @@ Trainer.prototype.GetUpgradedTemplate = function(templateName) Trainer.prototype.GetTrainCostMultiplier = function() { const trainCostMultiplier = {}; - for (const res in this.template.TrainCostMultiplier) + for (const res in this.template?.TrainCostMultiplier) trainCostMultiplier[res] = ApplyValueModificationsToEntity( "Trainer/TrainCostMultiplier/" + res, +this.template.TrainCostMultiplier[res], @@ -594,7 +592,7 @@ Trainer.prototype.GetBatchTime = function(batchSize) // TODO: work out what equation we should use here. return Math.pow(batchSize, ApplyValueModificationsToEntity( "Trainer/BatchTimeModifier", - +(this.template.BatchTimeModifier || 1), + +(this.template?.BatchTimeModifier || 1), this.entity)); }; diff --git a/binaries/data/mods/public/simulation/components/tests/test_Researcher.js b/binaries/data/mods/public/simulation/components/tests/test_Researcher.js index 74e4b58a0d..3f1bfa3370 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_Researcher.js +++ b/binaries/data/mods/public/simulation/components/tests/test_Researcher.js @@ -151,3 +151,11 @@ spyTechManager = new Spy(techManager, "ResearchTechnology"); TS_ASSERT_EQUALS(cmpResearcher.Progress(id, 1000), 500); TS_ASSERT_EQUALS(spyTechManager._called, 1); TS_ASSERT_EQUALS(cmpResearcher.queue.size, 0); + + +// Test that we can affect an empty researcher. +Engine.RegisterGlobal("ApplyValueModificationsToEntity", (_, value) => value + "some_test"); +TS_ASSERT_UNEVAL_EQUALS( + ConstructComponent(entityID, "Researcher", null).GetTechnologiesList(), + ["some_test"] +); diff --git a/binaries/data/mods/public/simulation/components/tests/test_Trainer.js b/binaries/data/mods/public/simulation/components/tests/test_Trainer.js index ccd69cb417..e29b83bc81 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_Trainer.js +++ b/binaries/data/mods/public/simulation/components/tests/test_Trainer.js @@ -299,3 +299,12 @@ TS_ASSERT_UNEVAL_EQUALS( TS_ASSERT_EQUALS(cmpTrainer.queue.size, 1); TS_ASSERT_EQUALS(cmpTrainer.GetBatch(id1), undefined); TS_ASSERT_EQUALS(cmpTrainer.GetBatch(id2).unitTemplate, "units/iber/c"); + + +// Test that we can affect an empty trainer. +const emptyTrainer = ConstructComponent(entityID, "Trainer", null); +emptyTrainer.OnValueModification({ "component": "Trainer", "entities": [entityID], "valueNames": ["Trainer/Entities/"] }); +TS_ASSERT_UNEVAL_EQUALS( + emptyTrainer.GetEntitiesList(), + ["units/iber/d"] +);