AWSGameLiftServerSystemComponent.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <AWSGameLiftServerSystemComponent.h>
  13. #include <AWSGameLiftServerManager.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Serialization/EditContext.h>
  16. #include <AzCore/Serialization/EditContextConstants.inl>
  17. #include <AzCore/Console/IConsole.h>
  18. #include <AzCore/Interface/Interface.h>
  19. #include <AzCore/IO/FileIO.h>
  20. #include <AzCore/IO/SystemFile.h>
  21. namespace AWSGameLift
  22. {
  23. AWSGameLiftServerSystemComponent::AWSGameLiftServerSystemComponent()
  24. : m_gameLiftServerManager(AZStd::make_unique<AWSGameLiftServerManager>())
  25. {
  26. }
  27. AWSGameLiftServerSystemComponent::~AWSGameLiftServerSystemComponent()
  28. {
  29. m_gameLiftServerManager.reset();
  30. }
  31. void AWSGameLiftServerSystemComponent::Reflect(AZ::ReflectContext* context)
  32. {
  33. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  34. {
  35. serialize->Class<AWSGameLiftServerSystemComponent, AZ::Component>()
  36. ->Version(0)
  37. ;
  38. if (AZ::EditContext* ec = serialize->GetEditContext())
  39. {
  40. ec->Class<AWSGameLiftServerSystemComponent>("AWSGameLiftServer", "Create the GameLift server manager which manages the server process for hosting a game session via GameLiftServerSDK.")
  41. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  42. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("System"))
  43. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  44. ;
  45. }
  46. }
  47. }
  48. void AWSGameLiftServerSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  49. {
  50. provided.push_back(AZ_CRC_CE("AWSGameLiftServerService"));
  51. }
  52. void AWSGameLiftServerSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  53. {
  54. incompatible.push_back(AZ_CRC_CE("AWSGameLiftServerService"));
  55. }
  56. void AWSGameLiftServerSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  57. {
  58. AZ_UNUSED(required);
  59. }
  60. void AWSGameLiftServerSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  61. {
  62. AZ_UNUSED(dependent);
  63. }
  64. void AWSGameLiftServerSystemComponent::Init()
  65. {
  66. }
  67. void AWSGameLiftServerSystemComponent::Activate()
  68. {
  69. if (m_gameLiftServerManager->InitializeGameLiftServerSDK())
  70. {
  71. GameLiftServerProcessDesc serverProcessDesc;
  72. UpdateGameLiftServerProcessDesc(serverProcessDesc);
  73. m_gameLiftServerManager->NotifyGameLiftProcessReady(serverProcessDesc);
  74. }
  75. }
  76. void AWSGameLiftServerSystemComponent::Deactivate()
  77. {
  78. m_gameLiftServerManager->HandleDestroySession();
  79. }
  80. void AWSGameLiftServerSystemComponent::UpdateGameLiftServerProcessDesc(GameLiftServerProcessDesc& serverProcessDesc)
  81. {
  82. AZ::IO::FileIOBase* fileIO = AZ::IO::FileIOBase::GetDirectInstance();
  83. if (fileIO)
  84. {
  85. const char pathToLogFolder[] = "@log@/";
  86. char resolvedPath[AZ_MAX_PATH_LEN];
  87. if (fileIO->ResolvePath(pathToLogFolder, resolvedPath, AZ_ARRAY_SIZE(resolvedPath)))
  88. {
  89. serverProcessDesc.m_logPaths.push_back(resolvedPath);
  90. }
  91. else
  92. {
  93. AZ_Error("AWSGameLift", false, "Failed to resolve the path to the log folder.");
  94. }
  95. }
  96. else
  97. {
  98. AZ_Error("AWSGameLift", false, "Failed to get File IO.");
  99. }
  100. if (auto console = AZ::Interface<AZ::IConsole>::Get(); console != nullptr)
  101. {
  102. AZ::GetValueResult getCvarResult = console->GetCvarValue("sv_port", serverProcessDesc.m_port);
  103. AZ_Error(
  104. "AWSGameLift", getCvarResult == AZ::GetValueResult::Success, "Lookup of 'sv_port' console variable failed with error %s",
  105. AZ::GetEnumString(getCvarResult));
  106. }
  107. }
  108. void AWSGameLiftServerSystemComponent::SetGameLiftServerManager(AZStd::unique_ptr<AWSGameLiftServerManager> gameLiftServerManager)
  109. {
  110. m_gameLiftServerManager.reset();
  111. m_gameLiftServerManager = AZStd::move(gameLiftServerManager);
  112. }
  113. } // namespace AWSGameLift