MPSGameLiftServerSystemComponent.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include "MPSGameLiftServerSystemComponent.h"
  8. #include <AzCore/Console/IConsole.h>
  9. #include <AzCore/Interface/Interface.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <Request/AWSGameLiftServerRequestBus.h>
  12. AZ_CVAR(
  13. float,
  14. sv_gameSessionNoPlayerShutdownTimeoutSeconds,
  15. 3600.0f,
  16. nullptr,
  17. AZ::ConsoleFunctorFlags::DontReplicate,
  18. "The amount of seconds to wait before shutting down a game session if no players join."
  19. );
  20. namespace MPSGameLift
  21. {
  22. void MPSGameLiftServerSystemComponent::Reflect(AZ::ReflectContext* context)
  23. {
  24. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  25. {
  26. serialize->Class<MPSGameLiftServerSystemComponent, AZ::Component>()
  27. ->Version(0)
  28. ;
  29. }
  30. }
  31. void MPSGameLiftServerSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  32. {
  33. provided.push_back(AZ_CRC_CE("MPSGameLiftServerService"));
  34. }
  35. void MPSGameLiftServerSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  36. {
  37. incompatible.push_back(AZ_CRC_CE("MPSGameLiftServerService"));
  38. }
  39. void MPSGameLiftServerSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  40. {
  41. required.push_back(AZ_CRC_CE("MultiplayerService"));
  42. required.push_back(AZ_CRC_CE("AWSGameLiftServerService"));
  43. }
  44. void MPSGameLiftServerSystemComponent::Init()
  45. {
  46. }
  47. void MPSGameLiftServerSystemComponent::Activate()
  48. {
  49. Multiplayer::SessionNotificationBus::Handler::BusConnect();
  50. AzFramework::LevelLoadBlockerBus::Handler::BusConnect();
  51. Multiplayer::GetMultiplayer()->AddConnectionAcquiredHandler(m_connectionAquiredEventHandler);
  52. AWSGameLift::AWSGameLiftServerRequestBus::Broadcast(
  53. &AWSGameLift::AWSGameLiftServerRequestBus::Events::NotifyGameLiftProcessReady);
  54. }
  55. void MPSGameLiftServerSystemComponent::Deactivate()
  56. {
  57. m_connectionAquiredEventHandler.Disconnect();
  58. Multiplayer::SessionNotificationBus::Handler::BusDisconnect();
  59. AzFramework::LevelLoadBlockerBus::Handler::BusDisconnect();
  60. }
  61. bool MPSGameLiftServerSystemComponent::OnSessionHealthCheck()
  62. {
  63. // Add here: additional checks against game stats or other conditions, if needed, to determine session health.
  64. // For now, sufficient to return true so Amazon GameLift knows server process is responsive.
  65. return true;
  66. }
  67. void MPSGameLiftServerSystemComponent::OnCreateSessionEnd()
  68. {
  69. AzFramework::LevelLoadBlockerBus::Handler::BusDisconnect();
  70. if (!m_loadedLevelName.empty())
  71. {
  72. AZ_Info("MPSGameLiftServerSystemComponent", "Session requested by Amazon GameLift. Attempting to load level: '%s'", m_loadedLevelName.c_str());
  73. auto loadLevelCommand = AZStd::string::format("LoadLevel %s", m_loadedLevelName.c_str());
  74. AZ::Interface<AZ::IConsole>::Get()->PerformCommand(loadLevelCommand.c_str());
  75. }
  76. else
  77. {
  78. AZ_Info(
  79. "MPSGameLiftServerSystemComponent",
  80. "Session requested by Amazon GameLift. Make sure to load into a multiplayer level before players join.");
  81. }
  82. // Start a timer to shutdown this server if no players join.
  83. // This scheduled event will be stopped if m_connectionAquiredEventHandler is triggered.
  84. m_gameSessionNoPlayerShutdown.Enqueue(AZ::SecondsToTimeMs(sv_gameSessionNoPlayerShutdownTimeoutSeconds));
  85. }
  86. bool MPSGameLiftServerSystemComponent::ShouldBlockLevelLoading(const char* levelName)
  87. {
  88. m_loadedLevelName = levelName;
  89. if (levelName)
  90. {
  91. AZ_Info("MPSGameLiftServerSystemComponent", "Interrupted load of level: '%s'", levelName);
  92. }
  93. else
  94. {
  95. AZ_Info("MPSGameLiftServerSystemComponent", "Interrupted level load, but no level provided!");
  96. }
  97. return true;
  98. }
  99. }