From a5fdbddedaba651f097982dfa0facfacd404b9be Mon Sep 17 00:00:00 2001 From: pereslav Date: Thu, 15 Apr 2021 20:24:50 +0100 Subject: [PATCH 1/8] Merged MultiplayerPipeline from CodeCommit --- .../Serialization/ISerializer.inl | 18 ++ Gems/Multiplayer/Code/CMakeLists.txt | 21 ++ .../Code/Source/MultiplayerGem.cpp | 4 + .../Code/Source/MultiplayerToolsModule.cpp | 65 +++++++ .../Code/Source/MultiplayerToolsModule.h | 33 ++++ .../Code/Source/MultiplayerTypes.h | 47 ++++- .../EntityReplicationManager.cpp | 2 +- .../NetworkEntity/INetworkEntityManager.h | 6 +- .../NetworkEntity/NetworkEntityManager.cpp | 110 ++++++++++- .../NetworkEntity/NetworkEntityManager.h | 31 ++- .../NetworkEntity/NetworkSpawnableLibrary.cpp | 81 ++++++++ .../NetworkEntity/NetworkSpawnableLibrary.h | 43 ++++ .../Pipeline/NetBindMarkerComponent.cpp | 35 ++++ .../Source/Pipeline/NetBindMarkerComponent.h | 39 ++++ .../Pipeline/NetworkPrefabProcessor.cpp | 184 ++++++++++++++++++ .../Source/Pipeline/NetworkPrefabProcessor.h | 43 ++++ .../NetworkSpawnableHolderComponent.cpp | 42 ++++ .../NetworkSpawnableHolderComponent.h | 44 +++++ Gems/Multiplayer/Code/multiplayer_files.cmake | 6 + .../Code/multiplayer_tools_files.cmake | 19 ++ Gems/Multiplayer/Registry/prefab.tools.setreg | 26 +++ 21 files changed, 890 insertions(+), 9 deletions(-) create mode 100644 Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp create mode 100644 Gems/Multiplayer/Code/Source/MultiplayerToolsModule.h create mode 100644 Gems/Multiplayer/Code/Source/NetworkEntity/NetworkSpawnableLibrary.cpp create mode 100644 Gems/Multiplayer/Code/Source/NetworkEntity/NetworkSpawnableLibrary.h create mode 100644 Gems/Multiplayer/Code/Source/Pipeline/NetBindMarkerComponent.cpp create mode 100644 Gems/Multiplayer/Code/Source/Pipeline/NetBindMarkerComponent.h create mode 100644 Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.cpp create mode 100644 Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.h create mode 100644 Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp create mode 100644 Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.h create mode 100644 Gems/Multiplayer/Code/multiplayer_tools_files.cmake create mode 100644 Gems/Multiplayer/Registry/prefab.tools.setreg diff --git a/Code/Framework/AzNetworking/AzNetworking/Serialization/ISerializer.inl b/Code/Framework/AzNetworking/AzNetworking/Serialization/ISerializer.inl index 2d983c2061..2720f09f4b 100644 --- a/Code/Framework/AzNetworking/AzNetworking/Serialization/ISerializer.inl +++ b/Code/Framework/AzNetworking/AzNetworking/Serialization/ISerializer.inl @@ -18,6 +18,8 @@ #include #include #include +#include "AzCore/Name/Name.h" +#include "AzCore/Name/NameDictionary.h" namespace AzNetworking { @@ -173,6 +175,22 @@ namespace AzNetworking return true; } }; + + template<> + struct SerializeObjectHelper + { + static bool SerializeObject(ISerializer& serializer, AZ::Name& value) + { + AZ::Name::Hash nameHash = value.GetHash(); + bool result = serializer.Serialize(nameHash, "NameHash"); + + if (result && serializer.GetSerializerMode() == SerializerMode::WriteToObject) + { + value = AZ::NameDictionary::Instance().FindName(nameHash); + } + return result; + } + }; } #include diff --git a/Gems/Multiplayer/Code/CMakeLists.txt b/Gems/Multiplayer/Code/CMakeLists.txt index dde5e387f6..d395938e8c 100644 --- a/Gems/Multiplayer/Code/CMakeLists.txt +++ b/Gems/Multiplayer/Code/CMakeLists.txt @@ -56,6 +56,27 @@ ly_add_target( Gem::CertificateManager ) + +if (PAL_TRAIT_BUILD_HOST_TOOLS) + ly_add_target( + NAME Multiplayer.Tools MODULE + NAMESPACE Gem + OUTPUT_NAME Gem.Multiplayer.Tools + FILES_CMAKE + multiplayer_tools_files.cmake + INCLUDE_DIRECTORIES + PRIVATE + Source + . + PUBLIC + Include + BUILD_DEPENDENCIES + PRIVATE + AZ::AzToolsFramework + Gem::Multiplayer.Static + ) +endif() + ################################################################################ # Tests ################################################################################ diff --git a/Gems/Multiplayer/Code/Source/MultiplayerGem.cpp b/Gems/Multiplayer/Code/Source/MultiplayerGem.cpp index 9fa1413be5..15d4e2f6f0 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerGem.cpp +++ b/Gems/Multiplayer/Code/Source/MultiplayerGem.cpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include namespace Multiplayer { @@ -26,6 +28,8 @@ namespace Multiplayer AzNetworking::NetworkingSystemComponent::CreateDescriptor(), MultiplayerSystemComponent::CreateDescriptor(), NetBindComponent::CreateDescriptor(), + NetBindMarkerComponent::CreateDescriptor(), + NetworkSpawnableHolderComponent::CreateDescriptor(), }); CreateComponentDescriptors(m_descriptors); diff --git a/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp b/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp new file mode 100644 index 0000000000..16f13f9ee8 --- /dev/null +++ b/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp @@ -0,0 +1,65 @@ +/* +* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution (the "License"). All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file. Do not +* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#include +#include +#include "Pipeline/NetworkPrefabProcessor.h" +#include "AzCore/Serialization/Json/RegistrationContext.h" +#include "Prefab/Instance/InstanceSerializer.h" + +namespace Multiplayer +{ + //! Multiplayer system component wraps the bridging logic between the game and transport layer. + class MultiplayerToolsSystemComponent final + : public AZ::Component + { + public: + AZ_COMPONENT(MultiplayerToolsSystemComponent, "{65AF5342-0ECE-423B-B646-AF55A122F72B}"); + + static void Reflect(AZ::ReflectContext* context) + { + NetworkPrefabProcessor::Reflect(context); + } + + MultiplayerToolsSystemComponent() = default; + ~MultiplayerToolsSystemComponent() override = default; + + /// AZ::Component overrides. + void Activate() override + { + + } + + void Deactivate() override + { + + } + }; + + MultiplayerToolsModule::MultiplayerToolsModule() + : AZ::Module() + { + m_descriptors.insert(m_descriptors.end(), { + MultiplayerToolsSystemComponent::CreateDescriptor(), + }); + } + + AZ::ComponentTypeList MultiplayerToolsModule::GetRequiredSystemComponents() const + { + return AZ::ComponentTypeList + { + azrtti_typeid(), + }; + } +} // namespace Multiplayer + +AZ_DECLARE_MODULE_CLASS(Gem_Multiplayer2_Tools, Multiplayer::MultiplayerToolsModule); diff --git a/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.h b/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.h new file mode 100644 index 0000000000..823bd63a1d --- /dev/null +++ b/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.h @@ -0,0 +1,33 @@ +/* +* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution (the "License"). All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file. Do not +* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#pragma once + +#include + +namespace Multiplayer +{ + class MultiplayerToolsModule + : public AZ::Module + { + public: + + AZ_RTTI(MultiplayerToolsModule, "{3F726172-21FC-48FA-8CFA-7D87EBA07E55}", AZ::Module); + AZ_CLASS_ALLOCATOR(MultiplayerToolsModule, AZ::SystemAllocator, 0); + + MultiplayerToolsModule(); + ~MultiplayerToolsModule() override = default; + + AZ::ComponentTypeList GetRequiredSystemComponents() const override; + }; +} // namespace Multiplayer + diff --git a/Gems/Multiplayer/Code/Source/MultiplayerTypes.h b/Gems/Multiplayer/Code/Source/MultiplayerTypes.h index b387602843..ad491f4016 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerTypes.h +++ b/Gems/Multiplayer/Code/Source/MultiplayerTypes.h @@ -17,6 +17,7 @@ #include #include #include +#include namespace Multiplayer { @@ -69,14 +70,54 @@ namespace Multiplayer True }; + template + bool Serialize(TYPE& value, const char* name); + + inline NetEntityId MakeEntityId(uint8_t a_ServerId, int32_t a_NextId) + { + constexpr int32_t MAX_ENTITYID = 0x00FFFFFF; + + AZ_Assert((a_NextId < MAX_ENTITYID) && (a_NextId > 0), "Requested Id out of range"); + + NetEntityId ret = NetEntityId(((static_cast(a_ServerId) << 24) & 0xFF000000) | (a_NextId & MAX_ENTITYID)); + return ret; + } + // This is just a placeholder // The level/prefab cooking will devise the actual solution for identifying a dynamically spawnable entity within a prefab struct PrefabEntityId { AZ_TYPE_INFO(PrefabEntityId, "{EFD37465-CCAC-4E87-A825-41B4010A2C75}"); - bool operator==(const PrefabEntityId&) const { return true; } - bool operator!=(const PrefabEntityId& rhs) const { return !(*this == rhs); } - bool Serialize(AzNetworking::ISerializer&) { return true; } + + static constexpr uint32_t AllIndices = AZStd::numeric_limits::max(); + + AZ::Name m_prefabName; + uint32_t m_entityOffset = AllIndices; + + PrefabEntityId() = default; + + explicit PrefabEntityId(AZ::Name name, uint32_t entityOffset = AllIndices) + : m_prefabName(name) + , m_entityOffset(entityOffset) + { + } + + bool operator==(const PrefabEntityId& rhs) const + { + return m_prefabName == rhs.m_prefabName && m_entityOffset == rhs.m_entityOffset; + } + + bool operator!=(const PrefabEntityId& rhs) const + { + return !(*this == rhs); + } + + bool Serialize(AzNetworking::ISerializer& serializer) + { + serializer.Serialize(m_prefabName, "prefabName"); + serializer.Serialize(m_entityOffset, "entityOffset"); + return serializer.IsValid(); + } }; } diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp index e58b7fde9d..e19bc7685b 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp @@ -544,7 +544,7 @@ namespace Multiplayer if (createEntity) { //replicatorEntity = GetNetworkEntityManager()->CreateSingleEntityImmediateInternal(prefabEntityId, EntitySpawnType::Replicate, AutoActivate::DoNotActivate, netEntityId, localNetworkRole, AZ::Transform::Identity()); - AZ_Assert(replicatorEntity != nullptr, "Failed to create entity from prefab");// %s", prefabEntityId.GetString()); + AZ_Assert(replicatorEntity != nullptr, "Failed to create entity from prefab %s", prefabEntityId.m_prefabName.GetCStr()); if (replicatorEntity == nullptr) { return false; diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/INetworkEntityManager.h b/Gems/Multiplayer/Code/Source/NetworkEntity/INetworkEntityManager.h index 2262cccd19..be4f32752d 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/INetworkEntityManager.h +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/INetworkEntityManager.h @@ -16,6 +16,7 @@ #include #include #include +#include namespace Multiplayer { @@ -35,6 +36,7 @@ namespace Multiplayer AZ_RTTI(INetworkEntityManager, "{109759DE-9492-439C-A0B1-AE46E6FD029C}"); using OwnedEntitySet = AZStd::unordered_set; + using EntityList = AZStd::vector; virtual ~INetworkEntityManager() = default; @@ -50,7 +52,9 @@ namespace Multiplayer //! @return the HostId for this INetworkEntityManager instance virtual HostId GetHostId() const = 0; - // TODO: Spawn methods for entities within slices/prefabs/levels + //! Creates new entities of the given archetype + //! @param prefabEntryId the name of the spawnable to spawn + virtual void CreateEntitiesImmediate(const PrefabEntityId& prefabEntryId) = 0; //! Returns an ConstEntityPtr for the provided entityId. //! @param netEntityId the netEntityId to get an ConstEntityPtr for diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp index 46d1617760..d92becfb97 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp @@ -32,12 +32,15 @@ namespace Multiplayer , m_updateEntityDomainEvent([this] { UpdateEntityDomain(); }, AZ::Name("NetworkEntityManager update entity domain event")) , m_entityAddedEventHandler([this](AZ::Entity* entity) { OnEntityAdded(entity); }) , m_entityRemovedEventHandler([this](AZ::Entity* entity) { OnEntityRemoved(entity); }) + , m_rootSpawnableMonitor(*this) { AZ::Interface::Register(this); + AzFramework::RootSpawnableNotificationBus::Handler::BusConnect(); } NetworkEntityManager::~NetworkEntityManager() { + AzFramework::RootSpawnableNotificationBus::Handler::BusDisconnect(); AZ::Interface::Unregister(this); } @@ -147,7 +150,6 @@ namespace Multiplayer //{ // rootSlice->RemoveEntity(entity); //} - m_nonNetworkedEntities.clear(); m_networkEntityTracker.clear(); } @@ -282,7 +284,7 @@ namespace Multiplayer NetBindComponent* netBindComponent = entity->FindComponent(); if (netBindComponent != nullptr) { - const NetEntityId netEntityId = m_nextEntityId++; + const NetEntityId netEntityId = NextId(); netBindComponent->PreInit(entity, PrefabEntityId(), netEntityId, NetEntityRole::Authority); } } @@ -334,4 +336,108 @@ namespace Multiplayer m_networkEntityTracker.erase(entityId); } } + + INetworkEntityManager::EntityList NetworkEntityManager::CreateEntitiesImmediate(const AzFramework::Spawnable& spawnable) + { + INetworkEntityManager::EntityList returnList; + + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext); + + const AzFramework::Spawnable::EntityList& entities = spawnable.GetEntities(); + size_t entitiesSize = entities.size(); + + for (size_t i = 0; i < entitiesSize; ++i) + { + AZ::Entity* clone = serializeContext->CloneObject(entities[i].get()); + AZ_Assert(clone != nullptr, "Failed to clone spawnable entity."); + clone->SetId(AZ::Entity::MakeId()); + + NetBindComponent* netBindComponent = clone->FindComponent(); + if (netBindComponent != nullptr) + { + PrefabEntityId prefabEntityId; + prefabEntityId.m_prefabName = m_networkPrefabLibrary.GetPrefabNameFromAssetId(spawnable.GetId()); + prefabEntityId.m_entityOffset = aznumeric_cast(i); + + const NetEntityId netEntityId = NextId(); + netBindComponent->PreInit(clone, prefabEntityId, netEntityId, NetEntityRole::Authority); + + AzFramework::GameEntityContextRequestBus::Broadcast( + &AzFramework::GameEntityContextRequestBus::Events::AddGameEntity, clone); + + returnList.push_back(netBindComponent->GetEntityHandle()); + + } + else + { + delete clone; + } + } + + return returnList; + } + + void NetworkEntityManager::CreateEntitiesImmediate([[maybe_unused]] const PrefabEntityId& a_SliceEntryId) + { + } + + Multiplayer::NetEntityId NetworkEntityManager::NextId() + { + const NetEntityId netEntityId = m_nextEntityId++; + return netEntityId; + } + + void NetworkEntityManager::OnRootSpawnableAssigned( + [[maybe_unused]] AZ::Data::Asset rootSpawnable, [[maybe_unused]] uint32_t generation) + { + AZStd::string hint = rootSpawnable.GetHint(); + + size_t extensionPos = hint.find(".spawnable"); + if (extensionPos == AZStd::string::npos) + { + AZ_Error("NetworkEntityManager", false, "OnRootSpawnableAssigned: Root spawnable hint doesn't have .spawnable extension"); + return; + } + + AZStd::string newhint = hint.replace(extensionPos, 0, ".network"); + auto rootSpawnableAssetId = m_networkPrefabLibrary.GetAssetIdByName(AZ::Name(newhint)); + if (!rootSpawnableAssetId.IsValid()) + { + AZ_Error("NetworkEntityManager", false, "OnRootSpawnableAssigned: Network spawnable asset ID is invalid"); + return; + } + + m_rootSpawnableAsset = AZ::Data::Asset( + rootSpawnableAssetId, azrtti_typeid(), newhint); + if (m_rootSpawnableAsset.QueueLoad()) + { + m_rootSpawnableMonitor.Connect(rootSpawnableAssetId); + } + else + { + AZ_Error("NetworkEntityManager", false, "OnRootSpawnableAssigned: Unable to queue networked root spawnable '%s' for loading.", + m_rootSpawnableAsset.GetHint().c_str()); + } + } + + void NetworkEntityManager::OnRootSpawnableReleased([[maybe_unused]] uint32_t generation) + { + m_rootSpawnableMonitor.Disconnect(); + } + + + NetworkEntityManager::NetworkSpawnableMonitor::NetworkSpawnableMonitor( + NetworkEntityManager& entityManager) + : m_entityManager(entityManager) + { + } + + void NetworkEntityManager::NetworkSpawnableMonitor::OnAssetReady(AZ::Data::Asset asset) + { + AzFramework::Spawnable* spawnable = asset.GetAs(); + AZ_Assert(spawnable, "NetworkSpawnableMonitor: Loaded asset data didn't contain a Spawanble."); + + m_entityManager.CreateEntitiesImmediate(*spawnable); + } } diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h index b154983f4c..20c67a74fd 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h @@ -14,11 +14,15 @@ #include #include +#include +#include #include #include #include #include #include +#include + namespace Multiplayer { @@ -26,6 +30,7 @@ namespace Multiplayer //! This class creates and manages all networked entities. class NetworkEntityManager final : public INetworkEntityManager + , public AzFramework::RootSpawnableNotificationBus::Handler { public: NetworkEntityManager(); @@ -40,6 +45,11 @@ namespace Multiplayer NetworkEntityAuthorityTracker* GetNetworkEntityAuthorityTracker() override; HostId GetHostId() const override; ConstNetworkEntityHandle GetEntity(NetEntityId netEntityId) const override; + + EntityList CreateEntitiesImmediate(const AzFramework::Spawnable& spawnable); + + void CreateEntitiesImmediate(const PrefabEntityId& a_SliceEntryId) override; + uint32_t GetEntityCount() const override; NetworkEntityHandle AddEntityToEntityMap(NetEntityId netEntityId, AZ::Entity* entity) override; void MarkForRemoval(const ConstNetworkEntityHandle& entityHandle) override; @@ -61,19 +71,32 @@ namespace Multiplayer void DispatchLocalDeferredRpcMessages(); void UpdateEntityDomain(); void OnEntityExitDomain(NetEntityId entityId); + //! RootSpawnableNotificationBus + //! @{ + void OnRootSpawnableAssigned(AZ::Data::Asset rootSpawnable, uint32_t generation) override; + void OnRootSpawnableReleased(uint32_t generation) override; + //! @} private: + class NetworkSpawnableMonitor final : public AzFramework::SpawnableMonitor + { + public: + explicit NetworkSpawnableMonitor(NetworkEntityManager& entityManager); + void OnAssetReady(AZ::Data::Asset asset) override; + + NetworkEntityManager& m_entityManager; + }; void OnEntityAdded(AZ::Entity* entity); void OnEntityRemoved(AZ::Entity* entity); void RemoveEntities(); + NetEntityId NextId(); + NetworkEntityTracker m_networkEntityTracker; NetworkEntityAuthorityTracker m_networkEntityAuthorityTracker; AZ::ScheduledEvent m_removeEntitiesEvent; AZStd::vector m_removeList; - AZStd::vector m_nonNetworkedEntities; // Contains entities that we've instantiated, but are not networked entities - AZStd::unique_ptr m_entityDomain; AZ::ScheduledEvent m_updateEntityDomainEvent; @@ -95,5 +118,9 @@ namespace Multiplayer // This is done to prevent local and network sent RPC's from having different dispatch behaviours typedef AZStd::deque DeferredRpcMessages; DeferredRpcMessages m_localDeferredRpcMessages; + + NetworkSpawnableLibrary m_networkPrefabLibrary; + NetworkSpawnableMonitor m_rootSpawnableMonitor; + AZ::Data::Asset m_rootSpawnableAsset; }; } diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkSpawnableLibrary.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkSpawnableLibrary.cpp new file mode 100644 index 0000000000..ad2e18e222 --- /dev/null +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkSpawnableLibrary.cpp @@ -0,0 +1,81 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#include +#include +#include +#include + +namespace Multiplayer +{ + NetworkSpawnableLibrary::NetworkSpawnableLibrary() + { + AzFramework::AssetCatalogEventBus::Handler::BusConnect(); + } + + NetworkSpawnableLibrary::~NetworkSpawnableLibrary() + { + AzFramework::AssetCatalogEventBus::Handler::BusDisconnect(); + } + + void NetworkSpawnableLibrary::BuildPrefabsList() + { + auto enumerateCallback = [this](const AZ::Data::AssetId id, const AZ::Data::AssetInfo& info) + { + if (info.m_assetType == AZ::AzTypeInfo::Uuid()) + { + ProcessSpawnableAsset(info.m_relativePath, id); + } + }; + + AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::EnumerateAssets, nullptr, + enumerateCallback, nullptr); + } + + void NetworkSpawnableLibrary::ProcessSpawnableAsset(const AZStd::string& relativePath, const AZ::Data::AssetId id) + { + const AZ::Name name = AZ::Name(relativePath); + m_spawnables[name] = id; + m_spawnablesReverseLookup[id] = name; + + } + + void NetworkSpawnableLibrary::OnCatalogLoaded([[maybe_unused]] const char* catalogFile) + { + BuildPrefabsList(); + } + + AZ::Name NetworkSpawnableLibrary::GetPrefabNameFromAssetId(AZ::Data::AssetId assetId) + { + if (assetId.IsValid()) + { + auto it = m_spawnablesReverseLookup.find(assetId); + if (it != m_spawnablesReverseLookup.end()) + { + return it->second; + } + } + + return {}; + } + + AZ::Data::AssetId NetworkSpawnableLibrary::GetAssetIdByName(AZ::Name name) + { + auto it = m_spawnables.find(name); + if (it != m_spawnables.end()) + { + return it->second; + } + + return {}; + } +} diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkSpawnableLibrary.h b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkSpawnableLibrary.h new file mode 100644 index 0000000000..a2c3d4ae56 --- /dev/null +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkSpawnableLibrary.h @@ -0,0 +1,43 @@ +/* +* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution (the "License"). All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file. Do not +* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#pragma once + +#include +#include +#include +#include + +namespace Multiplayer +{ + /// Implementation of the network prefab library interface. + class NetworkSpawnableLibrary final + : private AzFramework::AssetCatalogEventBus::Handler + { + public: + NetworkSpawnableLibrary(); + ~NetworkSpawnableLibrary(); + + void BuildPrefabsList(); + void ProcessSpawnableAsset(const AZStd::string& relativePath, AZ::Data::AssetId id); + + /// AssetCatalogEventBus overrides. + void OnCatalogLoaded(const char* catalogFile) override; + + AZ::Name GetPrefabNameFromAssetId(AZ::Data::AssetId assetId); + AZ::Data::AssetId GetAssetIdByName(AZ::Name name); + + private: + AZStd::unordered_map m_spawnables; + AZStd::unordered_map m_spawnablesReverseLookup; + }; +} diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetBindMarkerComponent.cpp b/Gems/Multiplayer/Code/Source/Pipeline/NetBindMarkerComponent.cpp new file mode 100644 index 0000000000..84983b700e --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetBindMarkerComponent.cpp @@ -0,0 +1,35 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#include +#include + +namespace Multiplayer +{ + void NetBindMarkerComponent::Reflect(AZ::ReflectContext* context) + { + AZ::SerializeContext* serializeContext = azrtti_cast(context); + if (serializeContext) + { + serializeContext->Class() + ->Version(1); + } + } + + void NetBindMarkerComponent::Activate() + { + } + + void NetBindMarkerComponent::Deactivate() + { + } +} diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetBindMarkerComponent.h b/Gems/Multiplayer/Code/Source/Pipeline/NetBindMarkerComponent.h new file mode 100644 index 0000000000..ebafead73c --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetBindMarkerComponent.h @@ -0,0 +1,39 @@ +/* +* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution (the "License"). All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file. Do not +* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#pragma once + +#include + +namespace Multiplayer +{ + //! @class NetBindMarkerComponent + //! @brief Component for tracking net entities in the original non-networked spawnable. + class NetBindMarkerComponent final : public AZ::Component + { + public: + AZ_COMPONENT(NetBindMarkerComponent, "{40612C1B-427D-45C6-A2F0-04E16DF5B718}"); + + static void Reflect(AZ::ReflectContext* context); + + NetBindMarkerComponent() = default; + ~NetBindMarkerComponent() override = default; + + //! AZ::Component overrides. + //! @{ + void Activate() override; + void Deactivate() override; + //! @} + + private: + }; +} // namespace Multiplayer diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.cpp b/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.cpp new file mode 100644 index 0000000000..0995ec29d8 --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.cpp @@ -0,0 +1,184 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Multiplayer +{ + using AzToolsFramework::Prefab::PrefabConversionUtils::PrefabProcessor; + using AzToolsFramework::Prefab::PrefabConversionUtils::ProcessedObjectStore; + + void NetworkPrefabProcessor::Process(PrefabProcessorContext& context) + { + context.ListPrefabs([&context](AZStd::string_view prefabName, PrefabDom& prefab) { + ProcessPrefab(context, prefabName, prefab); + }); + } + + void NetworkPrefabProcessor::Reflect(AZ::ReflectContext* context) + { + if (auto* serializeContext = azrtti_cast(context); serializeContext != nullptr) + { + serializeContext->Class()->Version(1); + } + } + + static AZStd::vector GetEntitiesFromInstance(AZStd::unique_ptr& instance) + { + AZStd::vector result; + + instance->GetNestedEntities([&result](const AZStd::unique_ptr& entity) { + result.emplace_back(entity.get()); + return true; + }); + + if (instance->HasContainerEntity()) + { + auto containerEntityReference = instance->GetContainerEntity(); + result.emplace_back(&containerEntityReference->get()); + } + + return result; + } + void NetworkPrefabProcessor::ProcessPrefab(PrefabProcessorContext& context, AZStd::string_view prefabName, PrefabDom& prefab) + { + using namespace AzToolsFramework::Prefab; + + // convert Prefab DOM into Prefab Instance. + AZStd::unique_ptr sourceInstance(aznew Instance()); + if (!PrefabDomUtils::LoadInstanceFromPrefabDom(*sourceInstance, prefab, + PrefabDomUtils::LoadInstanceFlags::AssignRandomEntityId)) + { + PrefabDomValueReference sourceReference = PrefabDomUtils::FindPrefabDomValue(prefab, PrefabDomUtils::SourceName); + + AZStd::string errorMessage("NetworkPrefabProcessor: Failed to Load Prefab Instance from given Prefab Dom."); + if (sourceReference.has_value() && sourceReference->get().IsString() && sourceReference->get().GetStringLength() != 0) + { + AZStd::string_view source(sourceReference->get().GetString(), sourceReference->get().GetStringLength()); + errorMessage += AZStd::string::format("Prefab Source: %.*s", AZ_STRING_ARG(source)); + } + AZ_Error("NetworkPrefabProcessor", false, errorMessage.c_str()); + return; + } + + AZStd::string uniqueName = prefabName; + uniqueName += ".network.spawnable"; + + auto serializer = [](AZStd::vector& output, const ProcessedObjectStore& object) -> bool { + AZ::IO::ByteContainerStream stream(&output); + auto& asset = object.GetAsset(); + return AZ::Utils::SaveObjectToStream(stream, AZ::DataStream::ST_JSON, &asset, asset.GetType()); + }; + + auto&& [object, networkSpawnable] = + ProcessedObjectStore::Create(uniqueName, context.GetSourceUuid(), AZStd::move(serializer)); + + // grab all nested entities from the Instance as source entities. + AZStd::vector sourceEntities = GetEntitiesFromInstance(sourceInstance); + AZStd::vector networkedEntityIds; + networkedEntityIds.reserve(sourceEntities.size()); + + for (auto* sourceEntity : sourceEntities) + { + if (sourceEntity->FindComponent()) + { + networkedEntityIds.push_back(sourceEntity->GetId()); + } + } + if (!PrefabDomUtils::StoreInstanceInPrefabDom(*sourceInstance, prefab)) + { + AZ_Error("NetworkPrefabProcessor", false, "Saving exported Prefab Instance within a Prefab Dom failed."); + return; + } + + AZStd::unique_ptr networkInstance(aznew Instance()); + + for (auto entityId : networkedEntityIds) + { + AZ::Entity* netEntity = sourceInstance->DetachEntity(entityId).release(); + + networkInstance->AddEntity(*netEntity); + + AZ::Entity* breadcrumbEntity = aznew AZ::Entity(netEntity->GetName()); + breadcrumbEntity->SetRuntimeActiveByDefault(netEntity->IsRuntimeActiveByDefault()); + breadcrumbEntity->CreateComponent(); + AzFramework::TransformComponent* transformComponent = netEntity->FindComponent(); + breadcrumbEntity->CreateComponent(*transformComponent); + + // TODO: Add NetBindMarkerComponent here referring to the net entity + sourceInstance->AddEntity(*breadcrumbEntity); + } + + // Add net spawnable asset holder + { + AZ::Data::AssetId assetId = networkSpawnable->GetId(); + AZ::Data::Asset networkSpawnableAsset; + networkSpawnableAsset.Create(assetId); + + EntityOptionalReference containerEntityRef = sourceInstance->GetContainerEntity(); + if (containerEntityRef.has_value()) + { + auto* networkSpawnableHolderComponent = containerEntityRef.value().get().CreateComponent(); + networkSpawnableHolderComponent->SetNetworkSpawnableAsset(networkSpawnableAsset); + } + else + { + AZ::Entity* networkSpawnableHolderEntity = aznew AZ::Entity(uniqueName); + auto* networkSpawnableHolderComponent = networkSpawnableHolderEntity->CreateComponent(); + networkSpawnableHolderComponent->SetNetworkSpawnableAsset(networkSpawnableAsset); + sourceInstance->AddEntity(*networkSpawnableHolderEntity); + } + } + + // save the final result in the target Prefab DOM. + PrefabDom networkPrefab; + if (!PrefabDomUtils::StoreInstanceInPrefabDom(*networkInstance, networkPrefab)) + { + AZ_Error("NetworkPrefabProcessor", false, "Saving exported Prefab Instance within a Prefab Dom failed."); + return; + } + + if (!PrefabDomUtils::StoreInstanceInPrefabDom(*sourceInstance, prefab)) + { + AZ_Error("NetworkPrefabProcessor", false, "Saving exported Prefab Instance within a Prefab Dom failed."); + return; + } + + + bool result = SpawnableUtils::CreateSpawnable(*networkSpawnable, networkPrefab); + if (result) + { + AzFramework::Spawnable::EntityList& entities = networkSpawnable->GetEntities(); + for (auto it = entities.begin(); it != entities.end(); ++it) + { + (*it)->InvalidateDependencies(); + (*it)->EvaluateDependencies(); + } + context.GetProcessedObjects().push_back(AZStd::move(object)); + } + else + { + AZ_Error("Prefabs", false, "Failed to convert prefab '%.*s' to a spawnable.", AZ_STRING_ARG(prefabName)); + context.ErrorEncountered(); + } + } +} diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.h b/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.h new file mode 100644 index 0000000000..ea927a1453 --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.h @@ -0,0 +1,43 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#pragma once + +#include + +namespace AzToolsFramework::Prefab::PrefabConversionUtils +{ + class PrefabProcessorContext; +} + +namespace Multiplayer +{ + using AzToolsFramework::Prefab::PrefabConversionUtils::PrefabProcessor; + using AzToolsFramework::Prefab::PrefabConversionUtils::PrefabProcessorContext; + using AzToolsFramework::Prefab::PrefabDom; + + class NetworkPrefabProcessor : public PrefabProcessor + { + public: + AZ_CLASS_ALLOCATOR(NetworkPrefabProcessor, AZ::SystemAllocator, 0); + AZ_RTTI(NetworkPrefabProcessor, "{AF6C36DA-CBB9-4DF4-AE2D-7BC6CCE65176}", PrefabProcessor); + + ~NetworkPrefabProcessor() override = default; + + void Process(PrefabProcessorContext& context) override; + + static void Reflect(AZ::ReflectContext* context); + + protected: + static void ProcessPrefab(PrefabProcessorContext& context, AZStd::string_view prefabName, PrefabDom& prefab); + }; +} diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp new file mode 100644 index 0000000000..26592fb935 --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp @@ -0,0 +1,42 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#include +#include + +namespace Multiplayer +{ + void NetworkSpawnableHolderComponent::Reflect(AZ::ReflectContext* context) + { + AZ::SerializeContext* serializeContext = azrtti_cast(context); + if (serializeContext) + { + serializeContext->Class() + ->Version(1) + ->Field("AssetRef", &NetworkSpawnableHolderComponent::m_networkSpawnableAsset); + } + } + + void NetworkSpawnableHolderComponent::Activate() + { + } + + void NetworkSpawnableHolderComponent::Deactivate() + { + } + + void NetworkSpawnableHolderComponent::SetNetworkSpawnableAsset(AZ::Data::Asset networkSpawnableAsset) + { + m_networkSpawnableAsset = networkSpawnableAsset; + } + +} diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.h b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.h new file mode 100644 index 0000000000..81f0959c74 --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.h @@ -0,0 +1,44 @@ +/* +* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution (the "License"). All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file. Do not +* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#pragma once + +#include +#include +#include + +namespace Multiplayer +{ + //! @class NetworkSpawnableHolderComponent + //! @brief Component for holding a reference to the network spawnable to make sure it is loaded with the original one. + class NetworkSpawnableHolderComponent final : public AZ::Component + { + public: + AZ_COMPONENT(NetworkSpawnableHolderComponent, "{B0E3ADEE-FCB4-4A32-8D4F-6920F1CB08E4}"); + + static void Reflect(AZ::ReflectContext* context); + + NetworkSpawnableHolderComponent() = default; + ~NetworkSpawnableHolderComponent() override = default; + + //! AZ::Component overrides. + //! @{ + void Activate() override; + void Deactivate() override; + //! @} + + void SetNetworkSpawnableAsset(AZ::Data::Asset networkSpawnableAsset); + + private: + AZ::Data::Asset m_networkSpawnableAsset{ AZ::Data::AssetLoadBehavior::PreLoad }; + }; +} // namespace Multiplayer diff --git a/Gems/Multiplayer/Code/multiplayer_files.cmake b/Gems/Multiplayer/Code/multiplayer_files.cmake index eea9379df9..9a7dd52cb8 100644 --- a/Gems/Multiplayer/Code/multiplayer_files.cmake +++ b/Gems/Multiplayer/Code/multiplayer_files.cmake @@ -60,6 +60,8 @@ set(FILES Source/NetworkEntity/NetworkEntityHandle.inl Source/NetworkEntity/NetworkEntityManager.cpp Source/NetworkEntity/NetworkEntityManager.h + Source/NetworkEntity/NetworkSpawnableLibrary.cpp + Source/NetworkEntity/NetworkSpawnableLibrary.h Source/NetworkEntity/NetworkEntityRpcMessage.cpp Source/NetworkEntity/NetworkEntityRpcMessage.h Source/NetworkEntity/NetworkEntityTracker.cpp @@ -81,6 +83,10 @@ set(FILES Source/NetworkTime/NetworkTime.h Source/NetworkTime/RewindableObject.h Source/NetworkTime/RewindableObject.inl + Source/Pipeline/NetBindMarkerComponent.cpp + Source/Pipeline/NetBindMarkerComponent.h + Source/Pipeline/NetworkSpawnableHolderComponent.cpp + Source/Pipeline/NetworkSpawnableHolderComponent.h Source/ReplicationWindows/IReplicationWindow.h Source/ReplicationWindows/ServerToClientReplicationWindow.cpp Source/ReplicationWindows/ServerToClientReplicationWindow.h diff --git a/Gems/Multiplayer/Code/multiplayer_tools_files.cmake b/Gems/Multiplayer/Code/multiplayer_tools_files.cmake new file mode 100644 index 0000000000..1be02fd999 --- /dev/null +++ b/Gems/Multiplayer/Code/multiplayer_tools_files.cmake @@ -0,0 +1,19 @@ +# +# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +# its licensors. +# +# For complete copyright and license terms please see the LICENSE at the root of this +# distribution (the "License"). All use of this software is governed by the License, +# or, if provided, by the license below or the license accompanying this file. Do not +# remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# + +set(FILES + Source/Multiplayer_precompiled.cpp + Source/Multiplayer_precompiled.h + Source/Pipeline/NetworkPrefabProcessor.cpp + Source/Pipeline/NetworkPrefabProcessor.h + Source/MultiplayerToolsModule.h + Source/MultiplayerToolsModule.cpp +) diff --git a/Gems/Multiplayer/Registry/prefab.tools.setreg b/Gems/Multiplayer/Registry/prefab.tools.setreg new file mode 100644 index 0000000000..4f20f88df9 --- /dev/null +++ b/Gems/Multiplayer/Registry/prefab.tools.setreg @@ -0,0 +1,26 @@ +{ + "Amazon": + { + "Tools": + { + "Prefab": + { + "Processing": + { + "Stack": + { + "GameObjectCreation": + [ + { "$type": "AzToolsFramework::Prefab::PrefabConversionUtils::EditorInfoRemover" }, + { "$type": "{AF6C36DA-CBB9-4DF4-AE2D-7BC6CCE65176}" }, + { + "$type": "AzToolsFramework::Prefab::PrefabConversionUtils::PrefabCatchmentProcessor", + "SerializationFormat": "Text" // Options are "Binary" (default) or "Text". Prefer "Binary" for performance. + } + ] + } + } + } + } + } +} \ No newline at end of file From 4962218d2932517b23a72beff1e4e2bce8e2f0cc Mon Sep 17 00:00:00 2001 From: pereslav Date: Fri, 16 Apr 2021 00:19:43 +0100 Subject: [PATCH 2/8] Refactored root spawnable instantiation, added selective instantiation of root spawnable entities --- .../Serialization/ISerializer.inl | 4 +- .../Code/Source/MultiplayerGem.cpp | 2 +- .../Code/Source/MultiplayerToolsModule.cpp | 8 +- .../Code/Source/MultiplayerTypes.h | 15 +- .../EntityReplicationManager.cpp | 12 +- .../NetworkEntity/INetworkEntityManager.h | 3 +- .../NetworkEntity/NetworkEntityManager.cpp | 137 +++++++++++++----- .../NetworkEntity/NetworkEntityManager.h | 17 +-- .../Pipeline/NetworkPrefabProcessor.cpp | 3 +- .../NetworkSpawnableHolderComponent.cpp | 9 ++ .../NetworkSpawnableHolderComponent.h | 3 +- 11 files changed, 136 insertions(+), 77 deletions(-) diff --git a/Code/Framework/AzNetworking/AzNetworking/Serialization/ISerializer.inl b/Code/Framework/AzNetworking/AzNetworking/Serialization/ISerializer.inl index 2720f09f4b..df3b08f798 100644 --- a/Code/Framework/AzNetworking/AzNetworking/Serialization/ISerializer.inl +++ b/Code/Framework/AzNetworking/AzNetworking/Serialization/ISerializer.inl @@ -18,8 +18,8 @@ #include #include #include -#include "AzCore/Name/Name.h" -#include "AzCore/Name/NameDictionary.h" +#include +#include namespace AzNetworking { diff --git a/Gems/Multiplayer/Code/Source/MultiplayerGem.cpp b/Gems/Multiplayer/Code/Source/MultiplayerGem.cpp index 15d4e2f6f0..596a1b40fa 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerGem.cpp +++ b/Gems/Multiplayer/Code/Source/MultiplayerGem.cpp @@ -15,9 +15,9 @@ #include #include #include -#include #include #include +#include namespace Multiplayer { diff --git a/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp b/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp index 16f13f9ee8..71b04585ad 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp +++ b/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp @@ -12,13 +12,13 @@ #include #include -#include "Pipeline/NetworkPrefabProcessor.h" -#include "AzCore/Serialization/Json/RegistrationContext.h" -#include "Prefab/Instance/InstanceSerializer.h" +#include +#include +#include namespace Multiplayer { - //! Multiplayer system component wraps the bridging logic between the game and transport layer. + //! Multiplayer Tools system component provides serialize context reflection for tools-only systems. class MultiplayerToolsSystemComponent final : public AZ::Component { diff --git a/Gems/Multiplayer/Code/Source/MultiplayerTypes.h b/Gems/Multiplayer/Code/Source/MultiplayerTypes.h index ad491f4016..7ffaa8a56e 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerTypes.h +++ b/Gems/Multiplayer/Code/Source/MultiplayerTypes.h @@ -13,11 +13,11 @@ #pragma once #include +#include #include #include #include #include -#include namespace Multiplayer { @@ -70,19 +70,6 @@ namespace Multiplayer True }; - template - bool Serialize(TYPE& value, const char* name); - - inline NetEntityId MakeEntityId(uint8_t a_ServerId, int32_t a_NextId) - { - constexpr int32_t MAX_ENTITYID = 0x00FFFFFF; - - AZ_Assert((a_NextId < MAX_ENTITYID) && (a_NextId > 0), "Requested Id out of range"); - - NetEntityId ret = NetEntityId(((static_cast(a_ServerId) << 24) & 0xFF000000) | (a_NextId & MAX_ENTITYID)); - return ret; - } - // This is just a placeholder // The level/prefab cooking will devise the actual solution for identifying a dynamically spawnable entity within a prefab struct PrefabEntityId diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp index e19bc7685b..3295f9e45e 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace Multiplayer { @@ -532,7 +533,7 @@ namespace Multiplayer NetEntityId netEntityId, NetEntityRole localNetworkRole, AzNetworking::ISerializer& serializer, - [[maybe_unused]] const PrefabEntityId& prefabEntityId + const PrefabEntityId& prefabEntityId ) { ConstNetworkEntityHandle replicatorEntity = GetNetworkEntityManager()->GetEntity(netEntityId); @@ -544,6 +545,15 @@ namespace Multiplayer if (createEntity) { //replicatorEntity = GetNetworkEntityManager()->CreateSingleEntityImmediateInternal(prefabEntityId, EntitySpawnType::Replicate, AutoActivate::DoNotActivate, netEntityId, localNetworkRole, AZ::Transform::Identity()); + INetworkEntityManager::EntityList entityList = GetNetworkEntityManager()->CreateEntitiesImmediate( + prefabEntityId, netEntityId, localNetworkRole, + AZ::Transform::Identity()); + + if (entityList.size() == 1) + { + replicatorEntity = entityList[0]; + } + AZ_Assert(replicatorEntity != nullptr, "Failed to create entity from prefab %s", prefabEntityId.m_prefabName.GetCStr()); if (replicatorEntity == nullptr) { diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/INetworkEntityManager.h b/Gems/Multiplayer/Code/Source/NetworkEntity/INetworkEntityManager.h index be4f32752d..557a912a31 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/INetworkEntityManager.h +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/INetworkEntityManager.h @@ -54,7 +54,8 @@ namespace Multiplayer //! Creates new entities of the given archetype //! @param prefabEntryId the name of the spawnable to spawn - virtual void CreateEntitiesImmediate(const PrefabEntityId& prefabEntryId) = 0; + virtual EntityList CreateEntitiesImmediate( + const PrefabEntityId& prefabEntryId, NetEntityId netEntityId, NetEntityRole netEntityRole, const AZ::Transform& transform) = 0; //! Returns an ConstEntityPtr for the provided entityId. //! @param netEntityId the netEntityId to get an ConstEntityPtr for diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp index d92becfb97..80ad654cae 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp @@ -20,6 +20,9 @@ #include #include #include +#include +#include +#include namespace Multiplayer { @@ -32,7 +35,6 @@ namespace Multiplayer , m_updateEntityDomainEvent([this] { UpdateEntityDomain(); }, AZ::Name("NetworkEntityManager update entity domain event")) , m_entityAddedEventHandler([this](AZ::Entity* entity) { OnEntityAdded(entity); }) , m_entityRemovedEventHandler([this](AZ::Entity* entity) { OnEntityRemoved(entity); }) - , m_rootSpawnableMonitor(*this) { AZ::Interface::Register(this); AzFramework::RootSpawnableNotificationBus::Handler::BusConnect(); @@ -284,8 +286,8 @@ namespace Multiplayer NetBindComponent* netBindComponent = entity->FindComponent(); if (netBindComponent != nullptr) { - const NetEntityId netEntityId = NextId(); - netBindComponent->PreInit(entity, PrefabEntityId(), netEntityId, NetEntityRole::Authority); + //const NetEntityId netEntityId = NextId(); + //netBindComponent->PreInit(entity, PrefabEntityId(), netEntityId, NetEntityRole::Authority); } } @@ -337,7 +339,8 @@ namespace Multiplayer } } - INetworkEntityManager::EntityList NetworkEntityManager::CreateEntitiesImmediate(const AzFramework::Spawnable& spawnable) + INetworkEntityManager::EntityList NetworkEntityManager::CreateEntitiesImmediate( + const AzFramework::Spawnable& spawnable, NetEntityRole netEntityRole) { INetworkEntityManager::EntityList returnList; @@ -361,7 +364,7 @@ namespace Multiplayer prefabEntityId.m_entityOffset = aznumeric_cast(i); const NetEntityId netEntityId = NextId(); - netBindComponent->PreInit(clone, prefabEntityId, netEntityId, NetEntityRole::Authority); + netBindComponent->PreInit(clone, prefabEntityId, netEntityId, netEntityRole); AzFramework::GameEntityContextRequestBus::Broadcast( &AzFramework::GameEntityContextRequestBus::Events::AddGameEntity, clone); @@ -378,8 +381,62 @@ namespace Multiplayer return returnList; } - void NetworkEntityManager::CreateEntitiesImmediate([[maybe_unused]] const PrefabEntityId& a_SliceEntryId) + INetworkEntityManager::EntityList NetworkEntityManager::CreateEntitiesImmediate( + const PrefabEntityId& prefabEntryId, NetEntityId netEntityId, NetEntityRole netEntityRole, + const AZ::Transform& transform) { + INetworkEntityManager::EntityList returnList; + + // TODO: Implement for non-root spawnables + auto spawnableAssetId = m_networkPrefabLibrary.GetAssetIdByName(prefabEntryId.m_prefabName); + if (spawnableAssetId == m_rootSpawnableAsset.GetId()) + { + AzFramework::Spawnable* netSpawnable = m_rootSpawnableAsset.GetAs(); + if (!netSpawnable) + { + return returnList; + } + + const uint32_t entityIndex = prefabEntryId.m_entityOffset; + + if (entityIndex == PrefabEntityId::AllIndices) + { + return CreateEntitiesImmediate(*netSpawnable, netEntityRole); + } + + const AzFramework::Spawnable::EntityList& entities = netSpawnable->GetEntities(); + size_t entitiesSize = entities.size(); + if (entityIndex >= entitiesSize) + { + return returnList; + } + + AZ::SerializeContext* serializeContext = nullptr; + AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext); + + AZ::Entity* clone = serializeContext->CloneObject(entities[entityIndex].get()); + AZ_Assert(clone != nullptr, "Failed to clone spawnable entity."); + clone->SetId(AZ::Entity::MakeId()); + + NetBindComponent* netBindComponent = clone->FindComponent(); + if (netBindComponent) + { + netBindComponent->PreInit(clone, prefabEntryId, netEntityId, netEntityRole); + + auto* transformComponent = clone->FindComponent(); + if (transformComponent) + { + transformComponent->SetWorldTM(transform); + } + + AzFramework::GameEntityContextRequestBus::Broadcast( + &AzFramework::GameEntityContextRequestBus::Events::AddGameEntity, clone); + + returnList.push_back(netBindComponent->GetEntityHandle()); + } + } + + return returnList; } Multiplayer::NetEntityId NetworkEntityManager::NextId() @@ -389,55 +446,57 @@ namespace Multiplayer } void NetworkEntityManager::OnRootSpawnableAssigned( - [[maybe_unused]] AZ::Data::Asset rootSpawnable, [[maybe_unused]] uint32_t generation) + AZ::Data::Asset rootSpawnable, [[maybe_unused]] uint32_t generation) { - AZStd::string hint = rootSpawnable.GetHint(); - - size_t extensionPos = hint.find(".spawnable"); - if (extensionPos == AZStd::string::npos) + AzFramework::Spawnable* rootSpawnableData = rootSpawnable.GetAs(); + const auto& entityList = rootSpawnableData->GetEntities(); + if (entityList.size() == 0) { - AZ_Error("NetworkEntityManager", false, "OnRootSpawnableAssigned: Root spawnable hint doesn't have .spawnable extension"); + AZ_Error("NetworkEntityManager", false, "OnRootSpawnableAssigned: Root spawnable doesn't have any entities."); return; } - AZStd::string newhint = hint.replace(extensionPos, 0, ".network"); - auto rootSpawnableAssetId = m_networkPrefabLibrary.GetAssetIdByName(AZ::Name(newhint)); - if (!rootSpawnableAssetId.IsValid()) + const auto& rootEntity = entityList[0]; + auto* spawnableHolder = rootEntity->FindComponent(); + if (!spawnableHolder) { - AZ_Error("NetworkEntityManager", false, "OnRootSpawnableAssigned: Network spawnable asset ID is invalid"); + AZ_Error("NetworkEntityManager", false, "OnRootSpawnableAssigned: Root entity doesn't have NetworkSpawnableHolderComponent."); return; } - m_rootSpawnableAsset = AZ::Data::Asset( - rootSpawnableAssetId, azrtti_typeid(), newhint); - if (m_rootSpawnableAsset.QueueLoad()) + AZ::Data::Asset netSpawnableAsset = spawnableHolder->GetNetworkSpawnableAsset(); + AzFramework::Spawnable* netSpawnable = netSpawnableAsset.GetAs(); + if (!netSpawnable) { - m_rootSpawnableMonitor.Connect(rootSpawnableAssetId); + // TODO: Temp sync load until JsonSerialization of loadBehavior is fixed. + netSpawnableAsset = AZ::Data::AssetManager::Instance().GetAsset( + netSpawnableAsset.GetId(), AZ::Data::AssetLoadBehavior::PreLoad); + AZ::Data::AssetManager::Instance().BlockUntilLoadComplete(netSpawnableAsset); + + netSpawnable = netSpawnableAsset.GetAs(); } - else + + if (!netSpawnable) { - AZ_Error("NetworkEntityManager", false, "OnRootSpawnableAssigned: Unable to queue networked root spawnable '%s' for loading.", - m_rootSpawnableAsset.GetHint().c_str()); + AZ_Error("NetworkEntityManager", false, "OnRootSpawnableAssigned: Net spawnable doesn't have any data."); + return; + } + + m_rootSpawnableAsset = netSpawnableAsset; + + const auto agentType = AZ::Interface::Get()->GetAgentType(); + const bool spawnImmediately = + (agentType == MultiplayerAgentType::ClientServer || agentType == MultiplayerAgentType::DedicatedServer); + + if (spawnImmediately) + { + CreateEntitiesImmediate(*netSpawnable, NetEntityRole::Authority); } } void NetworkEntityManager::OnRootSpawnableReleased([[maybe_unused]] uint32_t generation) { - m_rootSpawnableMonitor.Disconnect(); - } - - - NetworkEntityManager::NetworkSpawnableMonitor::NetworkSpawnableMonitor( - NetworkEntityManager& entityManager) - : m_entityManager(entityManager) - { - } - - void NetworkEntityManager::NetworkSpawnableMonitor::OnAssetReady(AZ::Data::Asset asset) - { - AzFramework::Spawnable* spawnable = asset.GetAs(); - AZ_Assert(spawnable, "NetworkSpawnableMonitor: Loaded asset data didn't contain a Spawanble."); - - m_entityManager.CreateEntitiesImmediate(*spawnable); + // TODO: Do we need to clear all entities here? + m_rootSpawnableAsset.Release(); } } diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h index 20c67a74fd..d9d21d6b7b 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -46,9 +45,11 @@ namespace Multiplayer HostId GetHostId() const override; ConstNetworkEntityHandle GetEntity(NetEntityId netEntityId) const override; - EntityList CreateEntitiesImmediate(const AzFramework::Spawnable& spawnable); + EntityList CreateEntitiesImmediate(const AzFramework::Spawnable& spawnable, NetEntityRole netEntityRole); - void CreateEntitiesImmediate(const PrefabEntityId& a_SliceEntryId) override; + EntityList CreateEntitiesImmediate( + const PrefabEntityId& prefabEntryId, NetEntityId netEntityId, NetEntityRole netEntityRole, + const AZ::Transform& transform) override; uint32_t GetEntityCount() const override; NetworkEntityHandle AddEntityToEntityMap(NetEntityId netEntityId, AZ::Entity* entity) override; @@ -78,15 +79,6 @@ namespace Multiplayer //! @} private: - class NetworkSpawnableMonitor final : public AzFramework::SpawnableMonitor - { - public: - explicit NetworkSpawnableMonitor(NetworkEntityManager& entityManager); - void OnAssetReady(AZ::Data::Asset asset) override; - - NetworkEntityManager& m_entityManager; - }; - void OnEntityAdded(AZ::Entity* entity); void OnEntityRemoved(AZ::Entity* entity); void RemoveEntities(); @@ -120,7 +112,6 @@ namespace Multiplayer DeferredRpcMessages m_localDeferredRpcMessages; NetworkSpawnableLibrary m_networkPrefabLibrary; - NetworkSpawnableMonitor m_rootSpawnableMonitor; AZ::Data::Asset m_rootSpawnableAsset; }; } diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.cpp b/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.cpp index 0995ec29d8..2006272135 100644 --- a/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.cpp +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetworkPrefabProcessor.cpp @@ -124,7 +124,7 @@ namespace Multiplayer AzFramework::TransformComponent* transformComponent = netEntity->FindComponent(); breadcrumbEntity->CreateComponent(*transformComponent); - // TODO: Add NetBindMarkerComponent here referring to the net entity + // TODO: Configure NetBindMarkerComponent to refer to the net entity sourceInstance->AddEntity(*breadcrumbEntity); } @@ -133,6 +133,7 @@ namespace Multiplayer AZ::Data::AssetId assetId = networkSpawnable->GetId(); AZ::Data::Asset networkSpawnableAsset; networkSpawnableAsset.Create(assetId); + networkSpawnableAsset.SetAutoLoadBehavior(AZ::Data::AssetLoadBehavior::PreLoad); EntityOptionalReference containerEntityRef = sourceInstance->GetContainerEntity(); if (containerEntityRef.has_value()) diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp index 26592fb935..3c3d1f079d 100644 --- a/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp @@ -39,4 +39,13 @@ namespace Multiplayer m_networkSpawnableAsset = networkSpawnableAsset; } + AZ::Data::Asset NetworkSpawnableHolderComponent::GetNetworkSpawnableAsset() + { + return m_networkSpawnableAsset; + } + + NetworkSpawnableHolderComponent::NetworkSpawnableHolderComponent() + { + } + } diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.h b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.h index 81f0959c74..54a9a4e42f 100644 --- a/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.h +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.h @@ -27,7 +27,7 @@ namespace Multiplayer static void Reflect(AZ::ReflectContext* context); - NetworkSpawnableHolderComponent() = default; + NetworkSpawnableHolderComponent();; ~NetworkSpawnableHolderComponent() override = default; //! AZ::Component overrides. @@ -37,6 +37,7 @@ namespace Multiplayer //! @} void SetNetworkSpawnableAsset(AZ::Data::Asset networkSpawnableAsset); + AZ::Data::Asset GetNetworkSpawnableAsset(); private: AZ::Data::Asset m_networkSpawnableAsset{ AZ::Data::AssetLoadBehavior::PreLoad }; From 4e75a099b8e4a1658f04109d03df2068feeeaf5d Mon Sep 17 00:00:00 2001 From: karlberg Date: Sat, 17 Apr 2021 19:06:28 -0700 Subject: [PATCH 3/8] Initial Imgui debug display for stats, some hookup between entity replication and the spawnable code to make testing possible --- Gems/Multiplayer/Code/CMakeLists.txt | 38 ++++-- .../Source/AutoGen/AutoComponent_Source.jinja | 6 + .../Source/Imgui/MultiplayerImguiModule.cpp | 36 +++++ .../Source/Imgui/MultiplayerImguiModule.h | 31 +++++ .../Imgui/MultiplayerImguiSystemComponent.cpp | 123 ++++++++++++++++++ .../Imgui/MultiplayerImguiSystemComponent.h | 56 ++++++++ .../Source/MultiplayerSystemComponent.cpp | 18 ++- .../Code/Source/MultiplayerToolsModule.cpp | 2 +- .../EntityReplicationManager.cpp | 4 +- .../NetworkEntity/NetworkEntityManager.cpp | 12 +- .../NetworkSpawnableHolderComponent.cpp | 9 +- .../Code/multiplayer_imgui_files.cmake | 19 +++ 12 files changed, 328 insertions(+), 26 deletions(-) create mode 100644 Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.cpp create mode 100644 Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.h create mode 100644 Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.cpp create mode 100644 Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.h create mode 100644 Gems/Multiplayer/Code/multiplayer_imgui_files.cmake diff --git a/Gems/Multiplayer/Code/CMakeLists.txt b/Gems/Multiplayer/Code/CMakeLists.txt index d395938e8c..8cde5e01e2 100644 --- a/Gems/Multiplayer/Code/CMakeLists.txt +++ b/Gems/Multiplayer/Code/CMakeLists.txt @@ -18,16 +18,16 @@ ly_add_target( INCLUDE_DIRECTORIES PRIVATE ${pal_source_dir} - Source AZ::AzNetworking + Source . + PUBLIC + Include BUILD_DEPENDENCIES PUBLIC AZ::AzCore AZ::AzFramework AZ::AzNetworking - Gem::CertificateManager - 3rdParty::AWSNativeSDK::Core AUTOGEN_RULES *.AutoPackets.xml,AutoPackets_Header.jinja,$path/$fileprefix.AutoPackets.h *.AutoPackets.xml,AutoPackets_Inline.jinja,$path/$fileprefix.AutoPackets.inl @@ -49,6 +49,8 @@ ly_add_target( PRIVATE Source . + PUBLIC + Include BUILD_DEPENDENCIES PRIVATE Gem::Multiplayer.Static @@ -56,7 +58,6 @@ ly_add_target( Gem::CertificateManager ) - if (PAL_TRAIT_BUILD_HOST_TOOLS) ly_add_target( NAME Multiplayer.Tools MODULE @@ -77,10 +78,7 @@ if (PAL_TRAIT_BUILD_HOST_TOOLS) ) endif() -################################################################################ -# Tests -################################################################################ -if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) +if (PAL_TRAIT_BUILD_TESTS_SUPPORTED) ly_add_target( NAME Multiplayer.Tests ${PAL_TRAIT_TEST_TARGET_TYPE} NAMESPACE Gem @@ -92,6 +90,8 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) ${pal_source_dir} Source . + PUBLIC + Include BUILD_DEPENDENCIES PRIVATE AZ::AzTest @@ -101,3 +101,25 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) NAME Gem::Multiplayer.Tests ) endif() + +ly_add_target( + NAME Multiplayer.Imgui ${PAL_TRAIT_MONOLITHIC_DRIVEN_MODULE_TYPE} + NAMESPACE Gem + FILES_CMAKE + multiplayer_imgui_files.cmake + INCLUDE_DIRECTORIES + PRIVATE + Source + . + PUBLIC + Include + BUILD_DEPENDENCIES + PRIVATE + AZ::AzCore + AZ::AtomCore + AZ::AzFramework + AZ::AzNetworking + Gem::Atom_Feature_Common.Static + Gem::Multiplayer.Static + Gem::ImGui.Static +) diff --git a/Gems/Multiplayer/Code/Source/AutoGen/AutoComponent_Source.jinja b/Gems/Multiplayer/Code/Source/AutoGen/AutoComponent_Source.jinja index aee15bc190..d6907876e1 100644 --- a/Gems/Multiplayer/Code/Source/AutoGen/AutoComponent_Source.jinja +++ b/Gems/Multiplayer/Code/Source/AutoGen/AutoComponent_Source.jinja @@ -1168,6 +1168,12 @@ namespace {{ Component.attrib['Namespace'] }} void {{ ComponentBaseName }}::Init() { + if (m_netBindComponent == nullptr) + { + AZLOG_ERROR("NetBindComponent is null, ensure NetworkAttach is called prior to activating a networked entity"); + return; + } + {{ DefineComponentServiceProxyGrabs(Component, ClassType, ComponentName)|indent(8) }} {% if ComponentDerived %} OnInit(); diff --git a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.cpp b/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.cpp new file mode 100644 index 0000000000..1b59a704dc --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.cpp @@ -0,0 +1,36 @@ +/* +* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution (the "License"). All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file. Do not +* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#include +#include +#include + +namespace Multiplayer +{ + MultiplayerImguiModule::MultiplayerImguiModule() + : AZ::Module() + { + m_descriptors.insert(m_descriptors.end(), { + MultiplayerImguiSystemComponent::CreateDescriptor(), + }); + } + + AZ::ComponentTypeList MultiplayerImguiModule::GetRequiredSystemComponents() const + { + return AZ::ComponentTypeList + { + azrtti_typeid(), + }; + } +} + +AZ_DECLARE_MODULE_CLASS(Gem_Multiplayer_Imgui, Multiplayer::MultiplayerImguiModule); diff --git a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.h b/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.h new file mode 100644 index 0000000000..ce0ed244be --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.h @@ -0,0 +1,31 @@ +/* +* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution (the "License"). All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file. Do not +* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#pragma once + +#include + +namespace Multiplayer +{ + class MultiplayerImguiModule + : public AZ::Module + { + public: + AZ_RTTI(MultiplayerImguiModule, "{9E1460FA-4513-4B5E-86B4-9DD8ADEFA714}", AZ::Module); + AZ_CLASS_ALLOCATOR(MultiplayerImguiModule, AZ::SystemAllocator, 0); + + MultiplayerImguiModule(); + ~MultiplayerImguiModule() override = default; + + AZ::ComponentTypeList GetRequiredSystemComponents() const override; + }; +} diff --git a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.cpp b/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.cpp new file mode 100644 index 0000000000..a53dd2800f --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.cpp @@ -0,0 +1,123 @@ +/* +* All or portions of this file Copyright(c) Amazon.com, Inc.or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution(the "License").All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file.Do not +* remove or modify any license notices.This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#include +#include +#include +#include + +namespace Multiplayer +{ + void MultiplayerImguiSystemComponent::Reflect(AZ::ReflectContext* context) + { + if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) + { + serializeContext->Class() + ->Version(1); + } + } + + void MultiplayerImguiSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) + { + provided.push_back(AZ_CRC_CE("MultiplayerImguiSystemComponent")); + } + + void MultiplayerImguiSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required) + { + ; + } + + void MultiplayerImguiSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatbile) + { + incompatbile.push_back(AZ_CRC_CE("MultiplayerImguiSystemComponent")); + } + + void MultiplayerImguiSystemComponent::Activate() + { +#ifdef IMGUI_ENABLED + ImGui::ImGuiUpdateListenerBus::Handler::BusConnect(); +#endif + } + + void MultiplayerImguiSystemComponent::Deactivate() + { +#ifdef IMGUI_ENABLED + ImGui::ImGuiUpdateListenerBus::Handler::BusDisconnect(); +#endif + } + +#ifdef IMGUI_ENABLED + void MultiplayerImguiSystemComponent::OnImGuiMainMenuUpdate() + { + if (ImGui::BeginMenu("Multiplayer")) + { + //{ + // static int lossPercent{ 0 }; + // lossPercent = static_cast(net_UdpDebugLossPercent); + // if (ImGui::SliderInt("UDP Loss Percent", &lossPercent, 0, 100)) + // { + // net_UdpDebugLossPercent = lossPercent; + // m_ClientAgent.UpdateConnectionCvars(net_UdpDebugLossPercent); + // } + //} + // + //{ + // static int latency{ 0 }; + // latency = static_cast(net_UdpDebugLatencyMs); + // if (ImGui::SliderInt("UDP Latency Ms", &latency, 0, 3000)) + // { + // net_UdpDebugLatencyMs = latency; + // m_ClientAgent.UpdateConnectionCvars(net_UdpDebugLatencyMs); + // } + //} + // + //{ + // static int variance{ 0 }; + // variance = static_cast(net_UdpDebugVarianceMs); + // if (ImGui::SliderInt("UDP Variance Ms", &variance, 0, 1000)) + // { + // net_UdpDebugVarianceMs = variance; + // m_ClientAgent.UpdateConnectionCvars(net_UdpDebugVarianceMs); + // } + //} + + ImGui::Checkbox("Multiplayer Stats", &m_displayStats); + ImGui::EndMenu(); + } + } + + void MultiplayerImguiSystemComponent::OnImGuiUpdate() + { + if (m_displayStats) + { + if (ImGui::Begin("Multiplayer Stats", &m_displayStats, ImGuiWindowFlags_HorizontalScrollbar)) + { + IMultiplayer* multiplayer = AZ::Interface::Get(); + Multiplayer::MultiplayerStats& stats = multiplayer->GetStats(); + ImGui::Text("Multiplayer operating in %s mode", GetEnumString(multiplayer->GetAgentType())); + ImGui::Text("Total networked entities: %llu", aznumeric_cast(stats.m_entityCount)); + ImGui::Text("Total client connections: %llu", aznumeric_cast(stats.m_clientConnectionCount)); + ImGui::Text("Total server connections: %llu", aznumeric_cast(stats.m_serverConnectionCount)); + ImGui::Text("Total property updates sent: %llu", aznumeric_cast(stats.m_propertyUpdatesSent)); + ImGui::Text("Total property updates sent bytes: %llu", aznumeric_cast(stats.m_propertyUpdatesSentBytes)); + ImGui::Text("Total property updates received: %llu", aznumeric_cast(stats.m_propertyUpdatesRecv)); + ImGui::Text("Total property updates received bytes: %llu", aznumeric_cast(stats.m_propertyUpdatesRecvBytes)); + ImGui::Text("Total RPCs sent: %llu", aznumeric_cast(stats.m_rpcsSent)); + ImGui::Text("Total RPCs sent bytes: %llu", aznumeric_cast(stats.m_rpcsSentBytes)); + ImGui::Text("Total RPCs received: %llu", aznumeric_cast(stats.m_rpcsRecv)); + ImGui::Text("Total RPCs received bytes: %llu", aznumeric_cast(stats.m_rpcsRecvBytes)); + } + ImGui::End(); + } + } +#endif +} diff --git a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.h b/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.h new file mode 100644 index 0000000000..1650d62264 --- /dev/null +++ b/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.h @@ -0,0 +1,56 @@ +/* +* All or portions of this file Copyright(c) Amazon.com, Inc.or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution(the "License").All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file.Do not +* remove or modify any license notices.This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#pragma once + +#include + +#ifdef IMGUI_ENABLED +# include +# include +#endif + +namespace Multiplayer +{ + class MultiplayerImguiSystemComponent final + : public AZ::Component +#ifdef IMGUI_ENABLED + , public ImGui::ImGuiUpdateListenerBus::Handler +#endif + { + public: + AZ_COMPONENT(MultiplayerImguiSystemComponent, "{060BF3F1-0BFE-4FCE-9C3C-EE991F0DA581}"); + + static void Reflect(AZ::ReflectContext* context); + static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided); + static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required); + static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatbile); + + ~MultiplayerImguiSystemComponent() override = default; + + //! AZ::Component overrides + //! @{ + void Activate() override; + void Deactivate() override; + //! @} + +#ifdef IMGUI_ENABLED + //! ImGui::ImGuiUpdateListenerBus overrides + //! @{ + void OnImGuiMainMenuUpdate() override; + void OnImGuiUpdate() override; + //! @} +#endif + private: + bool m_displayStats = false; + }; +} diff --git a/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp b/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp index 70323d7de3..e40e9e9d66 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp +++ b/Gems/Multiplayer/Code/Source/MultiplayerSystemComponent.cpp @@ -133,23 +133,33 @@ namespace Multiplayer // Let the network system know the frame is done and we can collect dirty bits m_networkEntityManager.NotifyEntitiesDirtied(); + MultiplayerStats& stats = GetStats(); + stats.m_entityCount = GetNetworkEntityManager()->GetEntityCount(); + stats.m_serverConnectionCount = 0; + stats.m_clientConnectionCount = 0; + // Send out the game state update to all connections { - auto sendNetworkUpdates = [serverGameTimeMs](IConnection& connection) + auto sendNetworkUpdates = [serverGameTimeMs, &stats](IConnection& connection) { if (connection.GetUserData() != nullptr) { IConnectionData* connectionData = reinterpret_cast(connection.GetUserData()); connectionData->Update(serverGameTimeMs); + if (connectionData->GetConnectionDataType() == ConnectionDataType::ServerToClient) + { + stats.m_clientConnectionCount++; + } + else + { + stats.m_serverConnectionCount++; + } } }; m_networkInterface->GetConnectionSet().VisitConnections(sendNetworkUpdates); } - MultiplayerStats& stats = GetStats(); - stats.m_entityCount = GetNetworkEntityManager()->GetEntityCount(); - MultiplayerPackets::SyncConsole packet; AZ::ThreadSafeDeque::DequeType cvarUpdates; m_cvarCommands.Swap(cvarUpdates); diff --git a/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp b/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp index 71b04585ad..4c57924eb4 100644 --- a/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp +++ b/Gems/Multiplayer/Code/Source/MultiplayerToolsModule.cpp @@ -62,4 +62,4 @@ namespace Multiplayer } } // namespace Multiplayer -AZ_DECLARE_MODULE_CLASS(Gem_Multiplayer2_Tools, Multiplayer::MultiplayerToolsModule); +AZ_DECLARE_MODULE_CLASS(Gem_Multiplayer_Tools, Multiplayer::MultiplayerToolsModule); diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp index a3bb2029d2..2d2c668497 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp @@ -9,7 +9,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ - +#pragma optimize("", off) #include #include #include @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -30,7 +31,6 @@ #include #include #include -#include namespace Multiplayer { diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp index c06d7bc8f9..257173b347 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp @@ -39,12 +39,6 @@ namespace Multiplayer { AZ::Interface::Register(this); AzFramework::RootSpawnableNotificationBus::Handler::BusConnect(); - if (AZ::Interface::Get() != nullptr) - { - // Null guard needed for unit tests - AZ::Interface::Get()->RegisterEntityAddedEventHandler(m_entityAddedEventHandler); - AZ::Interface::Get()->RegisterEntityRemovedEventHandler(m_entityRemovedEventHandler); - } } NetworkEntityManager::~NetworkEntityManager() @@ -58,6 +52,12 @@ namespace Multiplayer m_hostId = hostId; m_entityDomain = AZStd::move(entityDomain); m_updateEntityDomainEvent.Enqueue(net_EntityDomainUpdateMs, true); + if (AZ::Interface::Get() != nullptr) + { + // Null guard needed for unit tests + AZ::Interface::Get()->RegisterEntityAddedEventHandler(m_entityAddedEventHandler); + AZ::Interface::Get()->RegisterEntityRemovedEventHandler(m_entityRemovedEventHandler); + } } NetworkEntityTracker* NetworkEntityManager::GetNetworkEntityTracker() diff --git a/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp index 3c3d1f079d..6087376b30 100644 --- a/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp +++ b/Gems/Multiplayer/Code/Source/Pipeline/NetworkSpawnableHolderComponent.cpp @@ -26,6 +26,10 @@ namespace Multiplayer } } + NetworkSpawnableHolderComponent::NetworkSpawnableHolderComponent() + { + } + void NetworkSpawnableHolderComponent::Activate() { } @@ -43,9 +47,4 @@ namespace Multiplayer { return m_networkSpawnableAsset; } - - NetworkSpawnableHolderComponent::NetworkSpawnableHolderComponent() - { - } - } diff --git a/Gems/Multiplayer/Code/multiplayer_imgui_files.cmake b/Gems/Multiplayer/Code/multiplayer_imgui_files.cmake new file mode 100644 index 0000000000..57623772d2 --- /dev/null +++ b/Gems/Multiplayer/Code/multiplayer_imgui_files.cmake @@ -0,0 +1,19 @@ +# +# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +# its licensors. +# +# For complete copyright and license terms please see the LICENSE at the root of this +# distribution (the "License"). All use of this software is governed by the License, +# or, if provided, by the license below or the license accompanying this file. Do not +# remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# + +set(FILES + Source/Multiplayer_precompiled.cpp + Source/Multiplayer_precompiled.h + Source/Imgui/MultiplayerImguiModule.cpp + Source/Imgui/MultiplayerImguiModule.h + Source/Imgui/MultiplayerImguiSystemComponent.cpp + Source/Imgui/MultiplayerImguiSystemComponent.h +) From 2b6b4f5d170d85b22cb3eb6c752625de5c8b4660 Mon Sep 17 00:00:00 2001 From: pereslav Date: Tue, 20 Apr 2021 17:49:17 +0100 Subject: [PATCH 4/8] Removed OnEntityAdded/OnEntityRemoved from NetworkEntityManager --- .../NetworkEntity/NetworkEntityManager.cpp | 32 ------------------- .../NetworkEntity/NetworkEntityManager.h | 4 --- 2 files changed, 36 deletions(-) diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp index 257173b347..43302efdaa 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp @@ -34,8 +34,6 @@ namespace Multiplayer : m_networkEntityAuthorityTracker(*this) , m_removeEntitiesEvent([this] { RemoveEntities(); }, AZ::Name("NetworkEntityManager remove entities event")) , m_updateEntityDomainEvent([this] { UpdateEntityDomain(); }, AZ::Name("NetworkEntityManager update entity domain event")) - , m_entityAddedEventHandler([this](AZ::Entity* entity) { OnEntityAdded(entity); }) - , m_entityRemovedEventHandler([this](AZ::Entity* entity) { OnEntityRemoved(entity); }) { AZ::Interface::Register(this); AzFramework::RootSpawnableNotificationBus::Handler::BusConnect(); @@ -52,12 +50,6 @@ namespace Multiplayer m_hostId = hostId; m_entityDomain = AZStd::move(entityDomain); m_updateEntityDomainEvent.Enqueue(net_EntityDomainUpdateMs, true); - if (AZ::Interface::Get() != nullptr) - { - // Null guard needed for unit tests - AZ::Interface::Get()->RegisterEntityAddedEventHandler(m_entityAddedEventHandler); - AZ::Interface::Get()->RegisterEntityRemovedEventHandler(m_entityRemovedEventHandler); - } } NetworkEntityTracker* NetworkEntityManager::GetNetworkEntityTracker() @@ -281,30 +273,6 @@ namespace Multiplayer } } - void NetworkEntityManager::OnEntityAdded(AZ::Entity* entity) - { - NetBindComponent* netBindComponent = entity->FindComponent(); - if (netBindComponent != nullptr) - { - // @pereslav - // Note that this is a total hack.. we should not be listening to this event on a client - // Entities should instead be spawned by the prefabEntityId inside EntityReplicationManager::HandlePropertyChangeMessage() - const bool isClient = AZ::Interface::Get()->GetAgentType() == MultiplayerAgentType::Client; - const NetEntityRole netEntityRole = isClient ? NetEntityRole::Client: NetEntityRole::Authority; - const NetEntityId netEntityId = m_nextEntityId++; - netBindComponent->PreInit(entity, PrefabEntityId(), netEntityId, netEntityRole); - } - } - - void NetworkEntityManager::OnEntityRemoved(AZ::Entity* entity) - { - NetBindComponent* netBindComponent = entity->FindComponent(); - if (netBindComponent != nullptr) - { - MarkForRemoval(netBindComponent->GetEntityHandle()); - } - } - void NetworkEntityManager::RemoveEntities() { //RewindableObjectState::ClearRewoundEntities(); diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h index d9d21d6b7b..148645c638 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h @@ -79,8 +79,6 @@ namespace Multiplayer //! @} private: - void OnEntityAdded(AZ::Entity* entity); - void OnEntityRemoved(AZ::Entity* entity); void RemoveEntities(); NetEntityId NextId(); @@ -100,8 +98,6 @@ namespace Multiplayer AZ::Event<> m_onEntityNotifyChanges; ControllersActivatedEvent m_controllersActivatedEvent; ControllersDeactivatedEvent m_controllersDeactivatedEvent; - AZ::EntityAddedEvent::Handler m_entityAddedEventHandler; - AZ::EntityRemovedEvent::Handler m_entityRemovedEventHandler; HostId m_hostId = InvalidHostId; NetEntityId m_nextEntityId = NetEntityId{ 0 }; From a2094e730813ffa943f0e0628e2bb2bdcbefb2d3 Mon Sep 17 00:00:00 2001 From: karlberg Date: Tue, 20 Apr 2021 17:20:37 -0700 Subject: [PATCH 5/8] Removing debug pragma --- .../EntityReplication/EntityReplicationManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp index 2d2c668497..8db582f6e5 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/EntityReplication/EntityReplicationManager.cpp @@ -9,7 +9,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ -#pragma optimize("", off) + #include #include #include From 55304c8b9cd592ceeb75847084c8acc07d94afc6 Mon Sep 17 00:00:00 2001 From: karlberg Date: Tue, 20 Apr 2021 18:00:39 -0700 Subject: [PATCH 6/8] Renamed Multiplayer.Imgui to Multiplayer.Debug --- .../MultiplayerDebugModule.cpp} | 14 +++++------ .../MultiplayerDebugModule.h} | 10 ++++---- .../MultiplayerDebugSystemComponent.cpp} | 24 +++++++++---------- .../MultiplayerDebugSystemComponent.h} | 6 ++--- ...es.cmake => multiplayer_debug_files.cmake} | 8 +++---- 5 files changed, 31 insertions(+), 31 deletions(-) rename Gems/Multiplayer/Code/Source/{Imgui/MultiplayerImguiModule.cpp => Debug/MultiplayerDebugModule.cpp} (69%) rename Gems/Multiplayer/Code/Source/{Imgui/MultiplayerImguiModule.h => Debug/MultiplayerDebugModule.h} (75%) rename Gems/Multiplayer/Code/Source/{Imgui/MultiplayerImguiSystemComponent.cpp => Debug/MultiplayerDebugSystemComponent.cpp} (86%) rename Gems/Multiplayer/Code/Source/{Imgui/MultiplayerImguiSystemComponent.h => Debug/MultiplayerDebugSystemComponent.h} (90%) rename Gems/Multiplayer/Code/{multiplayer_imgui_files.cmake => multiplayer_debug_files.cmake} (76%) diff --git a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.cpp b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.cpp similarity index 69% rename from Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.cpp rename to Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.cpp index 1b59a704dc..6ecb8e2ad6 100644 --- a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.cpp +++ b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.cpp @@ -11,26 +11,26 @@ */ #include -#include -#include +#include +#include namespace Multiplayer { - MultiplayerImguiModule::MultiplayerImguiModule() + MultiplayerDebugModule::MultiplayerDebugModule() : AZ::Module() { m_descriptors.insert(m_descriptors.end(), { - MultiplayerImguiSystemComponent::CreateDescriptor(), + MultiplayerDebugSystemComponent::CreateDescriptor(), }); } - AZ::ComponentTypeList MultiplayerImguiModule::GetRequiredSystemComponents() const + AZ::ComponentTypeList MultiplayerDebugModule::GetRequiredSystemComponents() const { return AZ::ComponentTypeList { - azrtti_typeid(), + azrtti_typeid(), }; } } -AZ_DECLARE_MODULE_CLASS(Gem_Multiplayer_Imgui, Multiplayer::MultiplayerImguiModule); +AZ_DECLARE_MODULE_CLASS(Gem_Multiplayer_Imgui, Multiplayer::MultiplayerDebugModule); diff --git a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.h b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.h similarity index 75% rename from Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.h rename to Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.h index ce0ed244be..94e96edf95 100644 --- a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiModule.h +++ b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.h @@ -16,15 +16,15 @@ namespace Multiplayer { - class MultiplayerImguiModule + class MultiplayerDebugModule : public AZ::Module { public: - AZ_RTTI(MultiplayerImguiModule, "{9E1460FA-4513-4B5E-86B4-9DD8ADEFA714}", AZ::Module); - AZ_CLASS_ALLOCATOR(MultiplayerImguiModule, AZ::SystemAllocator, 0); + AZ_RTTI(MultiplayerDebugModule, "{9E1460FA-4513-4B5E-86B4-9DD8ADEFA714}", AZ::Module); + AZ_CLASS_ALLOCATOR(MultiplayerDebugModule, AZ::SystemAllocator, 0); - MultiplayerImguiModule(); - ~MultiplayerImguiModule() override = default; + MultiplayerDebugModule(); + ~MultiplayerDebugModule() override = default; AZ::ComponentTypeList GetRequiredSystemComponents() const override; }; diff --git a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.cpp b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp similarity index 86% rename from Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.cpp rename to Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp index a53dd2800f..106a8b1d03 100644 --- a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.cpp +++ b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp @@ -10,45 +10,45 @@ * */ -#include +#include #include #include #include namespace Multiplayer { - void MultiplayerImguiSystemComponent::Reflect(AZ::ReflectContext* context) + void MultiplayerDebugSystemComponent::Reflect(AZ::ReflectContext* context) { if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) { - serializeContext->Class() + serializeContext->Class() ->Version(1); } } - void MultiplayerImguiSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) + void MultiplayerDebugSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) { - provided.push_back(AZ_CRC_CE("MultiplayerImguiSystemComponent")); + provided.push_back(AZ_CRC_CE("MultiplayerDebugSystemComponent")); } - void MultiplayerImguiSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required) + void MultiplayerDebugSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required) { ; } - void MultiplayerImguiSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatbile) + void MultiplayerDebugSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatbile) { - incompatbile.push_back(AZ_CRC_CE("MultiplayerImguiSystemComponent")); + incompatbile.push_back(AZ_CRC_CE("MultiplayerDebugSystemComponent")); } - void MultiplayerImguiSystemComponent::Activate() + void MultiplayerDebugSystemComponent::Activate() { #ifdef IMGUI_ENABLED ImGui::ImGuiUpdateListenerBus::Handler::BusConnect(); #endif } - void MultiplayerImguiSystemComponent::Deactivate() + void MultiplayerDebugSystemComponent::Deactivate() { #ifdef IMGUI_ENABLED ImGui::ImGuiUpdateListenerBus::Handler::BusDisconnect(); @@ -56,7 +56,7 @@ namespace Multiplayer } #ifdef IMGUI_ENABLED - void MultiplayerImguiSystemComponent::OnImGuiMainMenuUpdate() + void MultiplayerDebugSystemComponent::OnImGuiMainMenuUpdate() { if (ImGui::BeginMenu("Multiplayer")) { @@ -95,7 +95,7 @@ namespace Multiplayer } } - void MultiplayerImguiSystemComponent::OnImGuiUpdate() + void MultiplayerDebugSystemComponent::OnImGuiUpdate() { if (m_displayStats) { diff --git a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.h b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.h similarity index 90% rename from Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.h rename to Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.h index 1650d62264..81940423d7 100644 --- a/Gems/Multiplayer/Code/Source/Imgui/MultiplayerImguiSystemComponent.h +++ b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.h @@ -21,21 +21,21 @@ namespace Multiplayer { - class MultiplayerImguiSystemComponent final + class MultiplayerDebugSystemComponent final : public AZ::Component #ifdef IMGUI_ENABLED , public ImGui::ImGuiUpdateListenerBus::Handler #endif { public: - AZ_COMPONENT(MultiplayerImguiSystemComponent, "{060BF3F1-0BFE-4FCE-9C3C-EE991F0DA581}"); + AZ_COMPONENT(MultiplayerDebugSystemComponent, "{060BF3F1-0BFE-4FCE-9C3C-EE991F0DA581}"); static void Reflect(AZ::ReflectContext* context); static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided); static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required); static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatbile); - ~MultiplayerImguiSystemComponent() override = default; + ~MultiplayerDebugSystemComponent() override = default; //! AZ::Component overrides //! @{ diff --git a/Gems/Multiplayer/Code/multiplayer_imgui_files.cmake b/Gems/Multiplayer/Code/multiplayer_debug_files.cmake similarity index 76% rename from Gems/Multiplayer/Code/multiplayer_imgui_files.cmake rename to Gems/Multiplayer/Code/multiplayer_debug_files.cmake index 57623772d2..8d0b121735 100644 --- a/Gems/Multiplayer/Code/multiplayer_imgui_files.cmake +++ b/Gems/Multiplayer/Code/multiplayer_debug_files.cmake @@ -12,8 +12,8 @@ set(FILES Source/Multiplayer_precompiled.cpp Source/Multiplayer_precompiled.h - Source/Imgui/MultiplayerImguiModule.cpp - Source/Imgui/MultiplayerImguiModule.h - Source/Imgui/MultiplayerImguiSystemComponent.cpp - Source/Imgui/MultiplayerImguiSystemComponent.h + Source/Debug/MultiplayerDebugModule.cpp + Source/Debug/MultiplayerDebugModule.h + Source/Debug/MultiplayerDebugSystemComponent.cpp + Source/Debug/MultiplayerDebugSystemComponent.h ) From 0da6d5ad613a2168d7ac367eae8dacfb9da915fd Mon Sep 17 00:00:00 2001 From: karlberg Date: Tue, 20 Apr 2021 18:23:17 -0700 Subject: [PATCH 7/8] Missed the associated cmake changes --- Gems/Multiplayer/Code/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gems/Multiplayer/Code/CMakeLists.txt b/Gems/Multiplayer/Code/CMakeLists.txt index 8cde5e01e2..cd0cfca40d 100644 --- a/Gems/Multiplayer/Code/CMakeLists.txt +++ b/Gems/Multiplayer/Code/CMakeLists.txt @@ -103,10 +103,10 @@ if (PAL_TRAIT_BUILD_TESTS_SUPPORTED) endif() ly_add_target( - NAME Multiplayer.Imgui ${PAL_TRAIT_MONOLITHIC_DRIVEN_MODULE_TYPE} + NAME Multiplayer.Debug ${PAL_TRAIT_MONOLITHIC_DRIVEN_MODULE_TYPE} NAMESPACE Gem FILES_CMAKE - multiplayer_imgui_files.cmake + multiplayer_debug_files.cmake INCLUDE_DIRECTORIES PRIVATE Source From 7adbdb2889b6c3def78ec48f71c2f6dd7f94a9b6 Mon Sep 17 00:00:00 2001 From: karlberg Date: Tue, 20 Apr 2021 18:37:53 -0700 Subject: [PATCH 8/8] Fix some include paths after rename/refactor --- Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.cpp | 4 ++-- .../Code/Source/Debug/MultiplayerDebugSystemComponent.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.cpp b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.cpp index 6ecb8e2ad6..ec148d09b1 100644 --- a/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.cpp +++ b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugModule.cpp @@ -11,8 +11,8 @@ */ #include -#include -#include +#include +#include namespace Multiplayer { diff --git a/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp index 106a8b1d03..67dd678c54 100644 --- a/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp +++ b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp @@ -10,7 +10,7 @@ * */ -#include +#include #include #include #include