// Which server script an object runs: as the client picks its script, a non-empty custom script in the level config // always wins over the ScriptComponent row's; an empty or missing one leaves the row's. #include "GameDependencies.h" #include "GameMessages.h" #include "CDClientManager.h" #include "CDScriptComponentTable.h" #include "Entity.h" #include "ScriptComponent.h" #include namespace { const std::string cannonScript = "scripts\\ai\\MINIGAME\\SG_GF\\SERVER\\SG_CANNON.lua"; const std::string targetScript = "scripts/ai/ACT/SG_TARGET.lua"; const std::string emptyScript = "scripts\\empty.lua"; } class ScriptComponentTests : public GameDependenciesTest { protected: std::unique_ptr entity; void SetUp() override { SetUpDependencies(); auto& scripts = CDClientManager::GetEntriesMutable(); scripts[90001] = { 90001, cannonScript, "" }; scripts[90002] = { 90002, emptyScript, "" }; scripts[90003] = { 90003, "", "" }; entity = std::make_unique(1, info); } void TearDown() override { entity.reset(); auto& scripts = CDClientManager::GetEntriesMutable(); scripts.erase(90001); scripts.erase(90002); scripts.erase(90003); TearDownDependencies(); } CppScripts::Script* Load(const int32_t componentID) { ScriptComponent component(entity.get(), componentID); component.LoadDataFromTemplate(); component.LoadConfigData(); return component.GetScript(); } }; TEST(ScriptComponentChoice, CustomScriptWinsOverTheRowScript) { EXPECT_EQ(ScriptComponent::ChooseServerScript(cannonScript, targetScript), targetScript); EXPECT_EQ(ScriptComponent::ChooseServerScript(emptyScript, targetScript), targetScript); EXPECT_EQ(ScriptComponent::ChooseServerScript("", targetScript), targetScript); } TEST(ScriptComponentChoice, EmptyCustomScriptKeepsTheRowScript) { EXPECT_EQ(ScriptComponent::ChooseServerScript(cannonScript, ""), cannonScript); EXPECT_EQ(ScriptComponent::ChooseServerScript("", ""), ""); } TEST(ScriptComponentChoice, UnportedReplacementIsReportedOncePerLot) { EXPECT_TRUE(ScriptComponent::FirstUnportedReplacement(999901)); EXPECT_FALSE(ScriptComponent::FirstUnportedReplacement(999901)); EXPECT_TRUE(ScriptComponent::FirstUnportedReplacement(999902)); } TEST(ScriptComponentChoice, PortedScripts) { EXPECT_TRUE(CppScripts::IsPorted(cannonScript)); EXPECT_TRUE(CppScripts::IsPorted(targetScript)); EXPECT_TRUE(CppScripts::IsPorted(emptyScript)); // left out on purpose EXPECT_FALSE(CppScripts::IsPorted("scripts\\no\\such\\script.lua")); } TEST_F(ScriptComponentTests, RowScriptWithoutCustomScript) { EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), cannonScript)); } TEST_F(ScriptComponentTests, EmptyCustomScriptKeepsTheRowScript) { entity->SetVar(u"custom_script_server", ""); EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), cannonScript)); } TEST_F(ScriptComponentTests, CustomScriptWinsOverAPortedRowScript) { entity->SetVar(u"custom_script_server", targetScript); EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), targetScript)); } TEST_F(ScriptComponentTests, CustomScriptWinsOverTheEmptyScript) { entity->SetVar(u"custom_script_server", targetScript); EXPECT_EQ(Load(90002), CppScripts::GetScript(entity.get(), targetScript)); } TEST_F(ScriptComponentTests, CustomScriptWithoutARowScript) { entity->SetVar(u"custom_script_server", targetScript); EXPECT_EQ(Load(90003), CppScripts::GetScript(entity.get(), targetScript)); } // Not ported: the object runs no script (the row's is lost until the custom one is ported) TEST_F(ScriptComponentTests, UnportedCustomScriptStillWins) { entity->SetVar(u"custom_script_server", std::string("scripts\\no\\such\\script.lua")); EXPECT_EQ(Load(90001), CppScripts::GetInvalidScript()); }