From 883786796d759532d8604a385518419d0285bf70 Mon Sep 17 00:00:00 2001 From: Aaron Ruiz Mora Date: Fri, 14 May 2021 19:10:58 +0100 Subject: [PATCH] [NvCloth] Replace legacy IsDedicated() call with new AZ_CVAR sv_isDedicated --- Gems/NvCloth/Code/CMakeLists.txt | 6 +- .../Code/Source/Components/ClothComponent.cpp | 13 ++-- Gems/NvCloth/Code/Source/Module.cpp | 31 -------- .../Tests/Components/ClothComponentTest.cpp | 70 ++++++++----------- 4 files changed, 37 insertions(+), 83 deletions(-) diff --git a/Gems/NvCloth/Code/CMakeLists.txt b/Gems/NvCloth/Code/CMakeLists.txt index d83190a05c..0f019a985f 100644 --- a/Gems/NvCloth/Code/CMakeLists.txt +++ b/Gems/NvCloth/Code/CMakeLists.txt @@ -30,11 +30,7 @@ ly_add_target( BUILD_DEPENDENCIES PUBLIC 3rdParty::NvCloth - # CryCommon required for 'gEnv->IsDedicated()'. - # Because of this the module will need CrySystemEventBus to initialize gEnv - # and tests targets will need to fake gEnv. To be removed when there is - # an AZ replacement for asking if the game is running on a server or not. - Legacy::CryCommon + AZ::AzFramework Gem::AtomLyIntegration_CommonFeatures.Public PRIVATE Gem::EMotionFXStaticLib diff --git a/Gems/NvCloth/Code/Source/Components/ClothComponent.cpp b/Gems/NvCloth/Code/Source/Components/ClothComponent.cpp index 912255c798..0170fd65bd 100644 --- a/Gems/NvCloth/Code/Source/Components/ClothComponent.cpp +++ b/Gems/NvCloth/Code/Source/Components/ClothComponent.cpp @@ -10,9 +10,8 @@ * */ -#include - #include +#include #include @@ -55,10 +54,14 @@ namespace NvCloth void ClothComponent::Activate() { // Cloth components do not run on dedicated servers. - AZ_Assert(gEnv, "Environment not ready"); - if (gEnv->IsDedicated()) + if (auto* console = AZ::Interface::Get()) { - return; + bool isDedicated = false; + if (const auto result = console->GetCvarValue("sv_isDedicated", isDedicated); + result == AZ::GetValueResult::Success && isDedicated) + { + return; + } } AZ::Render::MeshComponentNotificationBus::Handler::BusConnect(GetEntityId()); diff --git a/Gems/NvCloth/Code/Source/Module.cpp b/Gems/NvCloth/Code/Source/Module.cpp index 08d673fd07..66390dda86 100644 --- a/Gems/NvCloth/Code/Source/Module.cpp +++ b/Gems/NvCloth/Code/Source/Module.cpp @@ -10,9 +10,6 @@ * */ -#include -#include - #include #include @@ -31,7 +28,6 @@ namespace NvCloth { class Module : public AZ::Module - , protected CrySystemEventBus::Handler { public: AZ_RTTI(Module, "{34C529D4-688F-4B51-BF60-75425754A7E6}", AZ::Module); @@ -47,8 +43,6 @@ namespace NvCloth m_fabricCooker = AZStd::make_unique(); m_tangentSpaceHelper = AZStd::make_unique(); - CrySystemEventBus::Handler::BusConnect(); - // Push results of [MyComponent]::CreateDescriptor() into m_descriptors here. m_descriptors.insert(m_descriptors.end(), { SystemComponent::CreateDescriptor(), @@ -64,8 +58,6 @@ namespace NvCloth ~Module() { - CrySystemEventBus::Handler::BusDisconnect(); - m_tangentSpaceHelper.reset(); m_fabricCooker.reset(); @@ -85,33 +77,10 @@ namespace NvCloth }; } - protected: - // CrySystemEventBus ... - void OnCrySystemPreInitialize(ISystem& system, const SSystemInitParams& systemInitParams) override; - void OnCrySystemPostShutdown() override; - private: AZStd::unique_ptr m_fabricCooker; AZStd::unique_ptr m_tangentSpaceHelper; }; - - void Module::OnCrySystemPreInitialize( - [[maybe_unused]] ISystem& system, - [[maybe_unused]] const SSystemInitParams& systemInitParams) - { -#if !defined(AZ_MONOLITHIC_BUILD) - // When module is linked dynamically, we must set our gEnv pointer. - // When module is linked statically, we'll share the application's gEnv pointer. - gEnv = system.GetGlobalEnvironment(); -#endif - } - - void Module::OnCrySystemPostShutdown() - { -#if !defined(AZ_MONOLITHIC_BUILD) - gEnv = nullptr; -#endif - } } // namespace NvCloth // DO NOT MODIFY THIS LINE UNLESS YOU RENAME THE GEM diff --git a/Gems/NvCloth/Code/Tests/Components/ClothComponentTest.cpp b/Gems/NvCloth/Code/Tests/Components/ClothComponentTest.cpp index 379cf6988d..ad37c8f079 100644 --- a/Gems/NvCloth/Code/Tests/Components/ClothComponentTest.cpp +++ b/Gems/NvCloth/Code/Tests/Components/ClothComponentTest.cpp @@ -12,8 +12,6 @@ #include -#include - #include #include #include @@ -25,48 +23,14 @@ namespace UnitTest { - //! Sets up a mock global environment to - //! change between server and client. class NvClothComponent : public ::testing::Test { - public: - static void SetUpTestCase(); - static void TearDownTestCase(); - protected: AZStd::unique_ptr CreateClothActorEntity(const NvCloth::ClothConfiguration& clothConfiguration); bool IsConnectedToMeshComponentNotificationBus(NvCloth::ClothComponent* clothComponent) const; - - private: - static AZStd::unique_ptr s_mockGEnv; - static SSystemGlobalEnvironment* s_previousGEnv; }; - AZStd::unique_ptr NvClothComponent::s_mockGEnv; - SSystemGlobalEnvironment* NvClothComponent::s_previousGEnv = nullptr; - - void NvClothComponent::SetUpTestCase() - { - // override global environment - s_previousGEnv = gEnv; - s_mockGEnv = AZStd::make_unique(); - gEnv = s_mockGEnv.get(); - -#if !defined(CONSOLE) - // Set environment to not be a server by default. - gEnv->SetIsDedicated(false); -#endif - } - - void NvClothComponent::TearDownTestCase() - { - // restore global environment - gEnv = s_previousGEnv; - s_mockGEnv.reset(); - s_previousGEnv = nullptr; - } - AZStd::unique_ptr NvClothComponent::CreateClothActorEntity(const NvCloth::ClothConfiguration& clothConfiguration) { AZStd::unique_ptr entity = AZStd::make_unique(); @@ -101,10 +65,35 @@ namespace UnitTest EXPECT_TRUE(sortOutcome.IsSuccess()); } -#if !defined(CONSOLE) - TEST_F(NvClothComponent, ClothComponent_OnServer_DoesNotConnectToMeshComponentNotificationBusOnActivation) + TEST_F(NvClothComponent, ClothComponent_WithoutMultiplayerGem_ConnectsToMeshComponentNotificationBusOnActivation) { - gEnv->SetIsDedicated(true); + AZStd::unique_ptr entity = CreateClothActorEntity({}); + entity->Activate(); + + auto* clothComponent = entity->FindComponent(); + + EXPECT_TRUE(IsConnectedToMeshComponentNotificationBus(clothComponent)); + } + + TEST_F(NvClothComponent, ClothComponent_WithMultiplayerGem_Game_ConnectsToMeshComponentNotificationBusOnActivation) + { + // Fake that multiplayer gem is enabled by creating a local sv_isDedicated AZ_CVAR + AZ::ConsoleDataWrapper> sv_isDedicated(false, + nullptr, "sv_isDedicated", "", AZ::ConsoleFunctorFlags::DontReplicate); + + AZStd::unique_ptr entity = CreateClothActorEntity({}); + entity->Activate(); + + auto* clothComponent = entity->FindComponent(); + + EXPECT_TRUE(IsConnectedToMeshComponentNotificationBus(clothComponent)); + } + + TEST_F(NvClothComponent, ClothComponent_WithMultiplayerGem_Server_DoesNotConnectToMeshComponentNotificationBusOnActivation) + { + // Fake that multiplayer gem is enabled by creating a local sv_isDedicated AZ_CVAR + AZ::ConsoleDataWrapper> sv_isDedicated(true, + nullptr, "sv_isDedicated", "", AZ::ConsoleFunctorFlags::DontReplicate); AZStd::unique_ptr entity = CreateClothActorEntity({}); entity->Activate(); @@ -112,10 +101,7 @@ namespace UnitTest auto* clothComponent = entity->FindComponent(); EXPECT_FALSE(IsConnectedToMeshComponentNotificationBus(clothComponent)); - - gEnv->SetIsDedicated(false); } -#endif TEST_F(NvClothComponent, ClothComponent_OneEntityWithTwoClothComponents_BothConnectToMeshComponentNotificationBusOnActivation) {