MultiplayerEditorSystemComponent.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <Multiplayer/IMultiplayer.h>
  10. #include <Multiplayer/MultiplayerEditorServerBus.h>
  11. #include <Multiplayer/Editor/MultiplayerPythonEditorEventsBus.h>
  12. #include <IEditor.h>
  13. #include <AzCore/Component/Component.h>
  14. #include <AzCore/Component/TickBus.h>
  15. #include <AzCore/IO/ByteContainerStream.h>
  16. #include <AzFramework/Entity/GameEntityContextBus.h>
  17. #include <AzFramework/Process/ProcessWatcher.h>
  18. #include <AzFramework/Process/ProcessCommunicatorTracePrinter.h>
  19. #include <AzFramework/Viewport/ScreenGeometry.h>
  20. #include <AzToolsFramework/ActionManager/ActionManagerRegistrationNotificationBus.h>
  21. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  22. #include <AzToolsFramework/Prefab/Spawnable/PrefabToInMemorySpawnableNotificationBus.h>
  23. namespace AzNetworking
  24. {
  25. class INetworkInterface;
  26. }
  27. namespace Multiplayer
  28. {
  29. //! A component to reflect scriptable commands for the Editor
  30. class PythonEditorFuncs : public AZ::Component
  31. {
  32. public:
  33. AZ_COMPONENT(PythonEditorFuncs, "{22AEEA59-94E6-4033-B67D-7C8FBB84DF0D}")
  34. static void Reflect(AZ::ReflectContext* context);
  35. // AZ::Component ...
  36. void Activate() override {}
  37. void Deactivate() override {}
  38. };
  39. //! Multiplayer system component wraps the bridging logic between the game and transport layer.
  40. class MultiplayerEditorSystemComponent final
  41. : public AZ::Component
  42. , public MultiplayerEditorLayerPythonRequestBus::Handler
  43. , private AzToolsFramework::EditorEvents::Bus::Handler
  44. , private IEditorNotifyListener
  45. , private MultiplayerEditorServerRequestBus::Handler
  46. , private AZ::TickBus::Handler
  47. , private AzToolsFramework::Prefab::PrefabToInMemorySpawnableNotificationBus::Handler
  48. , private AzToolsFramework::EditorEntityContextNotificationBus::Handler
  49. , private AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler
  50. {
  51. public:
  52. AZ_COMPONENT(MultiplayerEditorSystemComponent, "{9F335CC0-5574-4AD3-A2D8-2FAEF356946C}");
  53. static void Reflect(AZ::ReflectContext* context);
  54. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  55. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  56. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  57. MultiplayerEditorSystemComponent();
  58. ~MultiplayerEditorSystemComponent() override = default;
  59. //! Called once the editor receives the server's accept packet
  60. void OnServerAcceptanceReceived();
  61. //! AZ::Component overrides.
  62. //! @{
  63. void Activate() override;
  64. void Deactivate() override;
  65. //! @}
  66. //! EditorEvents::Bus::Handler overrides.
  67. //! @{
  68. void NotifyRegisterViews() override;
  69. //! @}
  70. //! MultiplayerEditorLayerPythonRequestBus::Handler overrides.
  71. //! @{
  72. void EnterGameMode() override;
  73. bool IsInGameMode() override;
  74. //! @}
  75. private:
  76. bool LaunchEditorServer();
  77. bool FindServerLauncher(AZ::IO::FixedMaxPath& serverPath);
  78. void Connect();
  79. //! AzToolsFramework::EditorEntityContextNotificationBus::Handler overrides
  80. //! @{
  81. void OnStartPlayInEditorBegin() override;
  82. void OnStartPlayInEditor() override;
  83. void OnStopPlayInEditorBegin() override;
  84. //! @}
  85. // AzToolsFramework::ActionManagerRegistrationNotificationBus overrides ...
  86. void OnActionRegistrationHook() override;
  87. void OnMenuBindingHook() override;
  88. //! AzToolsFramework::Prefab::PrefabToInMemorySpawnableNotificationBus::Handler overrides
  89. //! @{
  90. void OnPreparingInMemorySpawnableFromPrefab(const AzFramework::Spawnable& spawnable, const AZStd::string& assetHint) override;
  91. //! @}
  92. //! EditorEvents::Handler overrides
  93. //! @{
  94. void OnEditorNotifyEvent(EEditorNotifyEvent event) override;
  95. //! @}
  96. //! MultiplayerEditorServerRequestBus::Handler
  97. //! @{
  98. void SendEditorServerLevelDataPacket(AzNetworking::IConnection* connection) override;
  99. //! @}
  100. //! AZ::TickBus::Handler
  101. //! @{
  102. void OnTick(float, AZ::ScriptTimePoint) override;
  103. //! @}
  104. //! Context menu handler
  105. void ContextMenu_NewMultiplayerEntity(AZ::EntityId parentEntityId, const AZ::Vector3& worldPosition);
  106. void ResetLevelSendData();
  107. void SendLevelDataToServer();
  108. IEditor* m_editor = nullptr;
  109. AZStd::unique_ptr<AzFramework::ProcessWatcher> m_serverProcessWatcher = nullptr;
  110. AZStd::unique_ptr<ProcessCommunicatorTracePrinter> m_serverProcessTracePrinter = nullptr;
  111. AzNetworking::ConnectionId m_editorConnId;
  112. ServerAcceptanceReceivedEvent::Handler m_serverAcceptanceReceivedHandler;
  113. AZ::ScheduledEvent m_connectionEvent = AZ::ScheduledEvent([this]{this->Connect();}, AZ::Name("MultiplayerEditorConnect"));
  114. uint16_t m_connectionAttempts = 0;
  115. struct PreAliasedSpawnableData
  116. {
  117. AZStd::unique_ptr<AzFramework::Spawnable> spawnable;
  118. AZStd::string assetHint;
  119. AZ::Data::AssetId assetId;
  120. };
  121. AZStd::vector<PreAliasedSpawnableData> m_preAliasedSpawnablesForServer;
  122. // Structure that encapsulates the data we need for sending the level data to the server when entering game mode.
  123. struct LevelSendData
  124. {
  125. AZStd::vector<uint8_t> m_sendBuffer;
  126. AZStd::unique_ptr<AZ::IO::ByteContainerStream<AZStd::vector<uint8_t>>> m_byteStream;
  127. AzNetworking::IConnection* m_sendConnection = nullptr;
  128. };
  129. LevelSendData m_levelSendData;
  130. };
  131. }