NetworkInputMigrationVector.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Multiplayer/NetworkInput/NetworkInputMigrationVector.h>
  9. #include <Multiplayer/IMultiplayer.h>
  10. #include <AzNetworking/Serialization/ISerializer.h>
  11. namespace Multiplayer
  12. {
  13. NetworkInputMigrationVector::NetworkInputMigrationVector()
  14. : m_owner()
  15. {
  16. ;
  17. }
  18. NetworkInputMigrationVector::NetworkInputMigrationVector(const ConstNetworkEntityHandle& entityHandle)
  19. : m_owner(entityHandle)
  20. {
  21. ;
  22. }
  23. uint32_t NetworkInputMigrationVector::GetSize() const
  24. {
  25. return aznumeric_cast<uint32_t>(m_inputs.size());
  26. }
  27. NetworkInput& NetworkInputMigrationVector::operator[](uint32_t index)
  28. {
  29. return m_inputs[index].m_networkInput;
  30. }
  31. const NetworkInput& NetworkInputMigrationVector::operator[](uint32_t index) const
  32. {
  33. return m_inputs[index].m_networkInput;
  34. }
  35. bool NetworkInputMigrationVector::PushBack(const NetworkInput& networkInput)
  36. {
  37. if (m_inputs.size() < m_inputs.capacity())
  38. {
  39. m_inputs.push_back(networkInput);
  40. return true;
  41. }
  42. return false;
  43. }
  44. bool NetworkInputMigrationVector::Serialize(AzNetworking::ISerializer& serializer)
  45. {
  46. NetEntityId ownerId = m_owner.GetNetEntityId();
  47. serializer.Serialize(ownerId, "OwnerId");
  48. uint32_t inputCount = aznumeric_cast<uint32_t>(m_inputs.size());
  49. serializer.Serialize(inputCount, "InputCount");
  50. if (serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject)
  51. {
  52. // 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
  53. m_inputs.resize(inputCount);
  54. m_owner = GetNetworkEntityManager()->GetEntity(ownerId);
  55. NetBindComponent* netBindComponent = m_owner.GetNetBindComponent();
  56. if (netBindComponent)
  57. {
  58. for (uint32_t i = 0; i < m_inputs.size(); ++i)
  59. {
  60. m_inputs[i].m_networkInput.AttachNetBindComponent(netBindComponent);
  61. }
  62. }
  63. }
  64. return serializer.Serialize(m_inputs, "Inputs");
  65. }
  66. }