Browse Source

Moved ShouldBlockLevelLoad to an AzFramework interface so MP doesn't need to depend on CryLegacy.

Signed-off-by: Gene Walters <[email protected]>
Gene Walters 3 years ago
parent
commit
fa5c0ff790

+ 14 - 0
Code/Framework/AzFramework/AzFramework/API/ApplicationAPI.h

@@ -188,6 +188,20 @@ namespace AzFramework
         // This is useful to unload certain resources before any entities are destroyed.
         virtual void OnApplicationAboutToStop() {}
     };
+
+    class LevelSystemLifecycleRequests
+        : public AZ::EBusTraits
+    {
+    public:
+        // Bus Configuration
+        static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple;
+        static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
+
+        // Gives handlers the opportunity to block the loadlevel command.
+        // Return true to stop loading.
+        virtual bool ShouldBlockLevelLoading([[maybe_unused]]const char* levelName) { return false; }
+    };
+    using LevelSystemLifecycleRequestBus = AZ::EBus<LevelSystemLifecycleRequests>;
 } // namespace AzFramework
 
 #endif // AZFRAMEWORK_APPLICATIONAPI_H

+ 0 - 2
Code/Legacy/CryCommon/ILevelSystem.h

@@ -37,8 +37,6 @@ struct ILevelInfo
 struct ILevelSystemListener
 {
     virtual ~ILevelSystemListener() = default;
-    //! Gives listeners the opportunity to block the loadlevel command. Return true to stop loading.
-    virtual bool BlockLoading([[maybe_unused]] const char* levelName) { return false; }
     //! Called when loading a level fails due to it not being found.
     virtual void OnLevelNotFound([[maybe_unused]] const char* levelName) {}
     //! Called after ILevelSystem::PrepareNextLevel() completes.

+ 15 - 6
Code/Legacy/CrySystem/LevelSystem/SpawnableLevelSystem.cpp

@@ -236,13 +236,22 @@ namespace LegacyLevelSystem
         }
 
         // This is a valid level, find out if any systems need to stop level loading before proceeding
-        for (const auto& listener : m_listeners)
-        {
-            if (listener->BlockLoading(validLevelName.c_str()))
+        bool blockLoading = false;
+        AzFramework::LevelSystemLifecycleRequestBus::EnumerateHandlers(
+            [&blockLoading, &validLevelName](AzFramework::LevelSystemLifecycleRequests* handler)->bool
             {
-                AZ_TracePrintf("CrySystem::SpawnableLevelSystem", "LoadLevel for %s was blocked.\n", validLevelName.c_str());
-                return false;
-            }
+                if (handler->ShouldBlockLevelLoading(validLevelName.c_str()))
+                {
+                    blockLoading = true;
+                    return false; // Stop iterating handlers. This level should be blocked.
+                }
+                return true;
+            });
+
+        if (blockLoading)
+        {
+            AZ_TracePrintf("CrySystem::SpawnableLevelSystem", "LoadLevel for %s was blocked.\n", validLevelName.c_str());
+            return false;
         }
 
         // If a level is currently loaded, unload it before loading the next one.

+ 0 - 1
Gems/Multiplayer/Code/CMakeLists.txt

@@ -26,7 +26,6 @@ ly_add_target(
             AZ::AzCore
             AZ::AzFramework
             AZ::AzNetworking
-            Legacy::CryCommon
         PRIVATE
             Gem::EMotionFXStaticLib
             Gem::PhysX.Static

+ 4 - 17
Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp

@@ -38,7 +38,6 @@
 #include <AzFramework/Process/ProcessWatcher.h>
 
 #include <cmath>
-#include <ISystem.h>
 #include <AzCore/Debug/Profiler.h>
 
 AZ_DEFINE_BUDGET(MULTIPLAYER);
@@ -227,10 +226,10 @@ namespace Multiplayer
 
     void MultiplayerSystemComponent::Activate()
     {
-        CrySystemEventBus::Handler::BusConnect();
         AzFramework::RootSpawnableNotificationBus::Handler::BusConnect();
         AZ::TickBus::Handler::BusConnect();
         SessionNotificationBus::Handler::BusConnect();
+        AzFramework::LevelSystemLifecycleRequestBus::Handler::BusConnect();
         const AZ::Name interfaceName = AZ::Name(MpNetworkInterfaceName);
         m_networkInterface = AZ::Interface<INetworking>::Get()->CreateNetworkInterface(interfaceName, sv_protocol, TrustZone::ExternalClientToServer, *this);
 
@@ -256,10 +255,10 @@ namespace Multiplayer
         m_consoleCommandHandler.Disconnect();
         const AZ::Name interfaceName = AZ::Name(MpNetworkInterfaceName);
         AZ::Interface<INetworking>::Get()->DestroyNetworkInterface(interfaceName);
+        AzFramework::LevelSystemLifecycleRequestBus::Handler::BusDisconnect();
         SessionNotificationBus::Handler::BusDisconnect();
         AZ::TickBus::Handler::BusDisconnect();
         AzFramework::RootSpawnableNotificationBus::Handler::BusDisconnect();
-        CrySystemEventBus::Handler::BusDisconnect();
 
         m_networkEntityManager.Reset();
     }
@@ -1362,19 +1361,7 @@ namespace Multiplayer
         m_playersWaitingToBeSpawned.clear();
     }
 
-    void MultiplayerSystemComponent::OnCrySystemInitialized(ISystem& system, const SSystemInitParams&)
-    {
-        m_levelSystem = system.GetILevelSystem();
-        m_levelSystem->AddListener(this);
-    }
-
-    void MultiplayerSystemComponent::OnCrySystemShutdown([[maybe_unused]]ISystem& system)
-    {
-        m_levelSystem->RemoveListener(this);
-        m_levelSystem = nullptr;
-    }
-
-    bool MultiplayerSystemComponent::BlockLoading([[maybe_unused]]const char* levelName)
+    bool MultiplayerSystemComponent::ShouldBlockLevelLoading(const char* levelName)
     {
         switch (m_agentType)
         {
@@ -1418,7 +1405,7 @@ namespace Multiplayer
             return false;
         }
 
-        AZLOG_WARN("MultiplayerSystemComponent::BlockLoading called with unsupported agent type. Please update code to support agent type: %s.", GetEnumString(m_agentType));
+        AZLOG_WARN("MultiplayerSystemComponent::ShouldBlockLevelLoading called with unsupported agent type. Please update code to support agent type: %s.", GetEnumString(m_agentType));
         return false;
     }
 

+ 4 - 13
Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.h

@@ -21,9 +21,8 @@
 #include <AzCore/Console/IConsole.h>
 #include <AzCore/Threading/ThreadSafeDeque.h>
 #include <AzCore/std/string/string.h>
+#include <AzFramework/API/ApplicationAPI.h>
 #include <AzNetworking/ConnectionLayer/IConnectionListener.h>
-#include <CryCommon/CrySystemBus.h>
-#include <ILevelSystem.h>
 
 namespace AzFramework
 {
@@ -46,8 +45,7 @@ namespace Multiplayer
         , public AzNetworking::IConnectionListener
         , public IMultiplayer
         , AzFramework::RootSpawnableNotificationBus::Handler
-        , CrySystemEventBus::Handler
-        , ILevelSystemListener
+        , AzFramework::LevelSystemLifecycleRequestBus::Handler
     {
     public:
         AZ_COMPONENT(MultiplayerSystemComponent, "{7C99C4C1-1103-43F9-AD62-8B91CF7C1981}");
@@ -148,15 +146,9 @@ namespace Multiplayer
         void OnRootSpawnableReady(AZ::Data::Asset<AzFramework::Spawnable> rootSpawnable, uint32_t generation) override;
         //! @}
 
-        //! CrySystemEventBus::Handler overrides.
+        //! AzFramework::LevelSystemLifecycleRequestBus::Handler overrides.
         //! @{
-        void OnCrySystemInitialized(ISystem&, const SSystemInitParams&) override;
-        void OnCrySystemShutdown(ISystem&) override;
-        //! @}
-
-        //! ILevelSystemListener overrides.
-        //! @{
-        bool BlockLoading(const char* levelName) override;
+        bool ShouldBlockLevelLoading(const char* levelName) override;
         //! @}
 
     private:
@@ -219,7 +211,6 @@ namespace Multiplayer
 
         AZStd::vector<PlayerWaitingToBeSpawned> m_playersWaitingToBeSpawned;
         bool m_blockClientLoadLevel = true;
-        ILevelSystem* m_levelSystem = nullptr;
 
 #if !defined(AZ_RELEASE_BUILD)
         MultiplayerEditorConnection m_editorConnectionListener;