Integrating latest 47acbe8

This commit is contained in:
alexpete
2021-03-25 13:57:57 -07:00
parent 448c549698
commit 75dc720198
10312 changed files with 2711566 additions and 671451 deletions
@@ -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.
*
*/
#pragma once
#include <Source/MultiplayerTypes.h>
#include <AzNetworking/DataStructures/FixedSizeBitset.h>
#include <AzCore/std/containers/vector.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
namespace AzNetworking
{
class ISerializer;
}
namespace Multiplayer
{
class IMultiplayerComponentInput
{
public:
virtual ~IMultiplayerComponentInput() = default;
virtual NetComponentId GetComponentId() const = 0;
virtual bool Serialize(AzNetworking::ISerializer& serializer) = 0;
};
using MultiplayerComponentInputVector = AZStd::vector<AZStd::unique_ptr<IMultiplayerComponentInput>>;
}
@@ -0,0 +1,150 @@
/*
* 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 <Source/NetworkInput/NetworkInput.h>
#include <Source/Components/NetBindComponent.h>
#include <AzCore/Console/IConsole.h>
#include <AzCore/Console/ILogger.h>
namespace Multiplayer
{
NetworkInput::NetworkInput(const NetworkInput& rhs)
: m_inputId(rhs.m_inputId)
, m_owner(rhs.m_owner)
{
CopyInternal(rhs);
}
NetworkInput& NetworkInput::operator= (const NetworkInput& rhs)
{
if (&rhs != this)
{
CopyInternal(rhs);
}
return *this;
}
void NetworkInput::SetNetworkInputId(NetworkInputId inputId)
{
m_inputId = inputId;
}
NetworkInputId NetworkInput::GetNetworkInputId() const
{
return m_inputId;
}
NetworkInputId& NetworkInput::ModifyNetworkInputId()
{
return m_inputId;
}
void NetworkInput::AttachNetBindComponent(NetBindComponent* netBindComponent)
{
m_wasAttached = true;
m_componentInputs.clear();
if (netBindComponent)
{
m_owner = netBindComponent->GetEntityHandle();
m_componentInputs = netBindComponent->AllocateComponentInputs();
}
}
bool NetworkInput::Serialize(AzNetworking::ISerializer& serializer)
{
//static_assert(UINT8_MAX >= Multiplayer::ComponentTypes::c_Count, "Expected fewer than 255 components, this code needs to be updated");
if (!serializer.Serialize(m_inputId, "InputId"))
{
return false;
}
uint8_t componentInputCount = static_cast<uint8_t>(m_componentInputs.size());
serializer.Serialize(componentInputCount, "ComponentInputCount");
m_componentInputs.resize(componentInputCount);
if (serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject)
{
for (uint8_t i = 0; i < componentInputCount; ++i)
{
// We need to do a little extra work here, the delta serializer won't actually write out values if they were the same as the parent.
// We need to make sure we don't lose state that is intrinsic to the underlying type
// The default InvalidNetComponentId is a placeholder, we expect it to be overwritten by the serializer
// This happens when deserializing a non-delta'd input command
// However in the delta serializer case, we use the previous input as our initial value
// which will have the NetworkInputs setup and therefore won't write out the componentId
NetComponentId componentId = m_componentInputs[i] ? m_componentInputs[i]->GetComponentId() : InvalidNetComponentId;
serializer.Serialize(componentId, "ComponentType");
// Create a new input if we don't have one or the types do not match
if ((m_componentInputs[i] == nullptr) || (componentId != m_componentInputs[i]->GetComponentId()))
{
// TODO: ComponentInput factory, needs multiplayer component architecture and autogen
m_componentInputs[i] = nullptr; // ComponentInputFactory(componentId);
}
if (!m_componentInputs[i])
{
// If the client tells us a component type that does not have a NetworkInput, they are likely hacking
AZLOG_ERROR("Unexpected MultiplayerComponent type, unable to deserialize, dropping message");
return false;
}
serializer.Serialize(*m_componentInputs[i], "ComponentInput");
}
m_wasAttached = true;
}
else
{
AZ_Assert(m_wasAttached, "AttachNetSystemComponent was never called for NetworkInput");
// We assume that the order of the network inputs is fixed between the server and client
for (auto& componentInput : m_componentInputs)
{
NetComponentId componentId = componentInput->GetComponentId();
serializer.Serialize(componentId, "ComponentId");
serializer.Serialize(*componentInput, "ComponentInput");
}
}
return true;
}
const IMultiplayerComponentInput* NetworkInput::FindComponentInput(NetComponentId componentId) const
{
// linear search since we expect to have very few components
for (auto& componentInput : m_componentInputs)
{
if (componentInput->GetComponentId() == componentId)
{
return componentInput.get();
}
}
return nullptr;
}
IMultiplayerComponentInput* NetworkInput::FindComponentInput(NetComponentId componentId)
{
const NetworkInput* tmp = this;
return const_cast<IMultiplayerComponentInput*>(tmp->FindComponentInput(componentId));
}
void NetworkInput::CopyInternal(const NetworkInput& rhs)
{
m_inputId = rhs.m_inputId;
m_componentInputs.resize(rhs.m_componentInputs.size());
for (int i = 0; i < rhs.m_componentInputs.size(); ++i)
{
if (m_componentInputs[i] == nullptr || m_componentInputs[i]->GetComponentId() != rhs.m_componentInputs[i]->GetComponentId())
{
// TODO: ComponentInput factory, needs multiplayer component architecture and autogen
m_componentInputs[i] = nullptr; // ComponentInputFactory(rhs.m_componentInputs[i]->GetComponentId());
}
*m_componentInputs[i] = *rhs.m_componentInputs[i];
}
m_wasAttached = rhs.m_wasAttached;
}
}
@@ -0,0 +1,76 @@
/*
* 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 <Source/NetworkInput/IMultiplayerComponentInput.h>
#include <Source/NetworkEntity/NetworkEntityHandle.h>
#include <AzCore/RTTI/TypeSafeIntegral.h>
namespace Multiplayer
{
// Forwards
class NetBindComponent;
AZ_TYPE_SAFE_INTEGRAL(NetworkInputId, uint16_t);
//! @class NetworkInput
//! @brief A single networked client input command.
class NetworkInput final
{
public:
//! Intentionally restrict instancing of these objects to associated containers classes only
//! This is a mechanism used to restrict calling autonomous client predicted setter functions to the ProcessInput call chain only
friend class NetworkInputVector;
friend class MigrateNetworkInputVector;
friend class NetworkInputHistory;
friend class NetworkInputChild;
NetworkInput(const NetworkInput&);
NetworkInput& operator= (const NetworkInput&);
void SetNetworkInputId(NetworkInputId inputId);
NetworkInputId GetNetworkInputId() const;
NetworkInputId& ModifyNetworkInputId();
void AttachNetBindComponent(NetBindComponent* netBindComponent);
bool Serialize(AzNetworking::ISerializer& serializer);
const IMultiplayerComponentInput* FindComponentInput(NetComponentId componentId) const;
IMultiplayerComponentInput* FindComponentInput(NetComponentId componentId);
template <class InputType>
const InputType* FindInput() const
{
return static_cast<const InputType*>(FindInput(InputType::s_Type));
}
template <typename InputType>
InputType* FindInput()
{
return static_cast<InputType*>(FindInput(InputType::s_Type));
}
private:
//! Only associated containers can instance; see above comments.
NetworkInput() = default;
void CopyInternal(const NetworkInput& rhs);
MultiplayerComponentInputVector m_componentInputs;
NetworkInputId m_inputId = NetworkInputId{ 0 };
ConstNetworkEntityHandle m_owner;
bool m_wasAttached = false;
};
}
AZ_TYPE_SAFE_INTEGRAL_SERIALIZEBINDING(Multiplayer::NetworkInputId);
@@ -0,0 +1,68 @@
/*
* 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 <Source/NetworkInput/NetworkInputChild.h>
#include <Source/NetworkEntity/INetworkEntityManager.h>
#include <AzNetworking/Serialization/ISerializer.h>
namespace Multiplayer
{
NetworkInputChild::NetworkInputChild(const ConstNetworkEntityHandle& entityHandle)
: m_owner(entityHandle)
{
Attach(m_owner);
}
NetworkInputChild& NetworkInputChild::operator= (const NetworkInputChild& rhs)
{
m_owner = rhs.m_owner;
m_networkInput = rhs.m_networkInput;
return *this;
}
void NetworkInputChild::Attach(const ConstNetworkEntityHandle& entityHandle)
{
m_owner = entityHandle;
NetBindComponent* netBindComponent = entityHandle.GetNetBindComponent();
if (netBindComponent)
{
m_networkInput.AttachNetBindComponent(netBindComponent);
}
}
const ConstNetworkEntityHandle& NetworkInputChild::GetOwner() const
{
return m_owner;
}
const NetworkInput& NetworkInputChild::GetNetworkInput() const
{
return m_networkInput;
}
NetworkInput& NetworkInputChild::GetNetworkInput()
{
return m_networkInput;
}
bool NetworkInputChild::Serialize(AzNetworking::ISerializer& serializer)
{
NetEntityId ownerId = m_owner.GetNetEntityId();
serializer.Serialize(ownerId, "OwnerId");
if (serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject)
{
m_owner = GetNetworkEntityManager()->GetEntity(ownerId);
}
serializer.Serialize(m_networkInput, "NetworkInput");
return serializer.IsValid();
}
}
@@ -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 <Source/NetworkInput/NetworkInput.h>
namespace Multiplayer
{
//! Max number of entities that can be children of our netbound player entity.
static constexpr uint32_t MaxEntityHierarchyChildren = 16;
//! Used by the EntityHierarchyComponent. This component allows the gameplay programmer to specify inputs for dependent entities.
//! Since it is possible to for the Client/Server to disagree about the state of related entities,
//! this network input encodes the entity that is associated with it.
class NetworkInputChild
{
public:
NetworkInputChild() = default;
NetworkInputChild(const NetworkInputChild& rhs) = default;
NetworkInputChild(const ConstNetworkEntityHandle& entityHandle);
NetworkInputChild& operator= (const NetworkInputChild& rhs);
void Attach(const ConstNetworkEntityHandle& entityHandle);
const ConstNetworkEntityHandle& GetOwner() const;
const NetworkInput& GetNetworkInput() const;
NetworkInput& GetNetworkInput();
bool Serialize(AzNetworking::ISerializer& a_Serializer);
private:
ConstNetworkEntityHandle m_owner;
NetworkInput m_networkInput;
};
}
@@ -0,0 +1,46 @@
/*
* 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 <Source/NetworkInput/NetworkInputHistory.h>
namespace Multiplayer
{
AZStd::size_t NetworkInputHistory::Size() const
{
return m_history.size();
}
const NetworkInput& NetworkInputHistory::operator[](AZStd::size_t index) const
{
return m_history[index].m_networkInput;
}
NetworkInput& NetworkInputHistory::operator[](AZStd::size_t index)
{
return m_history[index].m_networkInput;
}
void NetworkInputHistory::PushBack(NetworkInput& networkInput)
{
m_history.push_back(Wrapper(networkInput));
}
void NetworkInputHistory::PopFront()
{
m_history.pop_front();
}
const NetworkInput& NetworkInputHistory::Front() const
{
return m_history.front().m_networkInput;
}
}
@@ -0,0 +1,45 @@
/*
* 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 <Source/NetworkInput/NetworkInput.h>
#include <AzCore/std/containers/deque.h>
namespace Multiplayer
{
//! @class NetworkInputHistory
//! @brief A list of input commands, used for bookkeeping on the client.
class NetworkInputHistory final
{
public:
AZStd::size_t Size() const;
const NetworkInput& operator[](AZStd::size_t index) const;
NetworkInput& operator[](AZStd::size_t index);
void PushBack(NetworkInput& networkInput);
void PopFront();
const NetworkInput& Front() const;
private:
struct Wrapper // Strictly a workaround to deal with the private constructor of NetworkInput
{
Wrapper() : m_networkInput() {}
Wrapper(const NetworkInput& networkInput) : m_networkInput(networkInput) {}
NetworkInput m_networkInput;
};
AZStd::deque<Wrapper> m_history;
};
}
@@ -0,0 +1,171 @@
/*
* 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 <Source/NetworkInput/NetworkInputVector.h>
#include <Source/NetworkEntity/INetworkEntityManager.h>
#include <AzNetworking/Serialization/ISerializer.h>
#include <AzNetworking/Serialization/DeltaSerializer.h>
namespace Multiplayer
{
NetworkInputVector::NetworkInputVector()
: m_owner()
, m_inputs()
{
;
}
NetworkInputVector::NetworkInputVector(const ConstNetworkEntityHandle& entityHandle)
: m_owner(entityHandle)
, m_inputs()
{
NetBindComponent* netBindComponent = entityHandle.GetNetBindComponent();
if (netBindComponent)
{
for (AZStd::size_t i = 0; i < m_inputs.size(); ++i)
{
m_inputs[i].m_networkInput.AttachNetBindComponent(netBindComponent);
}
}
}
NetworkInput& NetworkInputVector::operator[](uint32_t index)
{
return m_inputs[index].m_networkInput;
}
const NetworkInput& NetworkInputVector::operator[](uint32_t index) const
{
return m_inputs[index].m_networkInput;
}
void NetworkInputVector::SetPreviousInputId(NetworkInputId previousInputId)
{
m_previousInputId = previousInputId;
}
NetworkInputId NetworkInputVector::GetPreviousInputId() const
{
return m_previousInputId;
}
bool NetworkInputVector::Serialize(AzNetworking::ISerializer& serializer)
{
// Always serialize the full first element
if (!m_inputs[0].m_networkInput.Serialize(serializer))
{
return false;
}
// For each subsequent element
for (uint32_t i = 1; i < m_inputs.size(); ++i)
{
if (serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject)
{
AzNetworking::SerializerDelta deltaSerializer;
// Read out the delta
if (!deltaSerializer.Serialize(serializer))
{
return false;
}
// Start with previous value
m_inputs[i].m_networkInput = m_inputs[i - 1].m_networkInput;
// Then apply delta
AzNetworking::DeltaSerializerApply applySerializer(deltaSerializer);
if (!applySerializer.ApplyDelta(m_inputs[i].m_networkInput))
{
return false;
}
}
else
{
AzNetworking::SerializerDelta deltaSerializer;
// Create the delta
AzNetworking::DeltaSerializerCreate createSerializer(deltaSerializer);
if (!createSerializer.CreateDelta(m_inputs[i - 1].m_networkInput, m_inputs[i].m_networkInput))
{
return false;
}
// Then write out the delta
if (!deltaSerializer.Serialize(serializer))
{
return false;
}
}
}
serializer.Serialize(m_previousInputId, "PreviousInputId");
return true;
}
MigrateNetworkInputVector::MigrateNetworkInputVector()
: m_owner()
{
;
}
MigrateNetworkInputVector::MigrateNetworkInputVector(const ConstNetworkEntityHandle& entityHandle)
: m_owner(entityHandle)
{
;
}
uint32_t MigrateNetworkInputVector::GetSize() const
{
return aznumeric_cast<uint32_t>(m_inputs.size());
}
NetworkInput& MigrateNetworkInputVector::operator[](uint32_t index)
{
return m_inputs[index].m_networkInput;
}
const NetworkInput& MigrateNetworkInputVector::operator[](uint32_t index) const
{
return m_inputs[index].m_networkInput;
}
bool MigrateNetworkInputVector::PushBack(const NetworkInput& networkInput)
{
if (m_inputs.size() < m_inputs.capacity())
{
m_inputs.push_back(networkInput);
return true;
}
return false;
}
bool MigrateNetworkInputVector::Serialize(AzNetworking::ISerializer& serializer)
{
NetEntityId ownerId = m_owner.GetNetEntityId();
serializer.Serialize(ownerId, "OwnerId");
uint32_t inputCount = aznumeric_cast<uint32_t>(m_inputs.size());
serializer.Serialize(inputCount, "InputCount");
if (serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject)
{
// make sure all the possible NetworkInputs get attached prior to serialization, this double sends the size, but this message is only sent on server migration
m_inputs.resize(inputCount);
m_owner = GetNetworkEntityManager()->GetEntity(ownerId);
NetBindComponent* netBindComponent = m_owner.GetNetBindComponent();
if (netBindComponent)
{
for (uint32_t i = 0; i < m_inputs.size(); ++i)
{
m_inputs[i].m_networkInput.AttachNetBindComponent(netBindComponent);
}
}
}
return serializer.Serialize(m_inputs, "Inputs");
}
}
@@ -0,0 +1,85 @@
/*
* 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 <Source/NetworkInput/NetworkInput.h>
#include <Source/NetworkEntity/NetworkEntityHandle.h>
#include <AzCore/std/containers/fixed_vector.h>
namespace Multiplayer
{
//! @class InputCommandArray
//! @brief An array of network inputs. Used to mitigate loss of input packets on the server. Compresses subsequent elements.
class NetworkInputVector final
{
public:
static constexpr uint32_t MaxElements = 8; // Never try to replicate a list larger than this amount
NetworkInputVector();
NetworkInputVector(const ConstNetworkEntityHandle& entityHandle);
~NetworkInputVector() = default;
NetworkInput& operator[](uint32_t index);
const NetworkInput& operator[](uint32_t index) const;
void SetPreviousInputId(NetworkInputId previousInputId);
NetworkInputId GetPreviousInputId() const;
bool Serialize(AzNetworking::ISerializer& serializer);
private:
struct Wrapper // Strictly a workaround to deal with the private constructor of NetworkInput
{
Wrapper() : m_networkInput() {}
Wrapper(const NetworkInput& networkInput) : m_networkInput(networkInput) {}
NetworkInput m_networkInput;
};
ConstNetworkEntityHandle m_owner;
AZStd::fixed_vector<Wrapper, MaxElements> m_inputs;
NetworkInputId m_previousInputId;
};
//! @class MigrateNetworkInputVector
//! @brief A variable sized array of input commands, used specifically when migrate a clients inputs.
class MigrateNetworkInputVector final
{
public:
static constexpr uint32_t MaxElements = 90; // Never try to migrate a list larger than this amount, bumped up to handle DTLS connection time
MigrateNetworkInputVector();
MigrateNetworkInputVector(const ConstNetworkEntityHandle& entityHandle);
virtual ~MigrateNetworkInputVector() = default;
uint32_t GetSize() const;
NetworkInput& operator[](uint32_t index);
const NetworkInput& operator[](uint32_t index) const;
bool PushBack(const NetworkInput& networkInput);
bool Serialize(AzNetworking::ISerializer& serializer);
private:
struct Wrapper // Strictly a workaround to deal with the private constructor of NetworkInput
{
Wrapper() : m_networkInput() {}
Wrapper(const NetworkInput& networkInput) : m_networkInput(networkInput) {}
bool Serialize(AzNetworking::ISerializer& serializer) { return m_networkInput.Serialize(serializer); }
NetworkInput m_networkInput;
};
ConstNetworkEntityHandle m_owner;
AZStd::fixed_vector<Wrapper, MaxElements> m_inputs;
};
}