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