MPSGameLiftModuleInterface.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <AzCore/Memory/SystemAllocator.h>
  9. #include <AzCore/Module/Module.h>
  10. #include <Unified/MPSGameLiftSystemComponent.h>
  11. #if AZ_TRAIT_CLIENT
  12. #include <MatchmakingSystemComponent.h>
  13. #include <MPSGameLiftClientSystemComponent.h>
  14. #include <Components/UI/UiGameLiftConnectWithPlayerSessionData.h>
  15. #include <Components/UI/UiGameLiftFlexMatchConnect.h>
  16. #include <RegionalLatencySystemComponent.h>
  17. #endif
  18. // We only want this logic to execute in dedicated server builds, not in the Editor or Unified builds.
  19. #define AZ_DEDICATED_SERVER_ONLY (AZ_TRAIT_SERVER && !AZ_TRAIT_CLIENT)
  20. #if AZ_DEDICATED_SERVER_ONLY
  21. #include <MPSGameLiftServerSystemComponent.h>
  22. #include <AzCore/Console/IConsole.h>
  23. #include <AzCore/Date/DateFormat.h>
  24. #include <AzCore/Settings/SettingsRegistry.h>
  25. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  26. #include <AzCore/Platform.h>
  27. #endif
  28. namespace MPSGameLift
  29. {
  30. class MPSGameLiftModuleInterface
  31. : public AZ::Module
  32. {
  33. public:
  34. AZ_RTTI(MPSGameLiftModuleInterface, "{2CB13C68-FA4D-4B94-8CFB-6AC71B8630B6}", AZ::Module);
  35. AZ_CLASS_ALLOCATOR(MPSGameLiftModuleInterface, AZ::SystemAllocator, 0);
  36. MPSGameLiftModuleInterface()
  37. {
  38. #if AZ_DEDICATED_SERVER_ONLY
  39. // Change game server logs to go into a folder based on timestamp + server process id.
  40. // Normally, all the server.log files would go to the same folder named "log".
  41. // This way GameLift can archive the logs for a specific server.
  42. // Timestamp
  43. AZ::Date::Iso8601TimestampString utcTimestampString;
  44. AZ::Date::GetFilenameCompatibleFormatNow(utcTimestampString);
  45. // Process Id
  46. AZStd::fixed_string<32> processIdString;
  47. AZStd::to_string(processIdString, AZ::Platform::GetCurrentProcessId());
  48. // Create a log subfolder using Timestamp + Process Id
  49. AZ::IO::FixedMaxPath projectLogPath;
  50. AZ::SettingsRegistry::Get()->Get(projectLogPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_ProjectLogPath);
  51. projectLogPath = projectLogPath / AZ::IO::FixedMaxPathString::format("%s_%s", utcTimestampString.c_str(), processIdString.c_str());
  52. AZ::SettingsRegistry::Get()->Set(AZ::SettingsRegistryMergeUtils::FilePathKey_ProjectLogPath, projectLogPath.Native());
  53. #endif
  54. m_descriptors.insert(m_descriptors.end(), {
  55. MPSGameLiftSystemComponent::CreateDescriptor(),
  56. #if AZ_TRAIT_CLIENT
  57. MatchmakingSystemComponent::CreateDescriptor(),
  58. RegionalLatencySystemComponent::CreateDescriptor(),
  59. MPSGameLiftClientSystemComponent::CreateDescriptor(),
  60. UiGameLiftConnectWithPlayerSessionData::CreateDescriptor(),
  61. UiGameLiftFlexMatchConnect::CreateDescriptor(),
  62. #endif
  63. #if AZ_DEDICATED_SERVER_ONLY
  64. MPSGameLiftServerSystemComponent::CreateDescriptor(),
  65. #endif
  66. });
  67. }
  68. /**
  69. * Add required SystemComponents to the SystemEntity.
  70. */
  71. AZ::ComponentTypeList GetRequiredSystemComponents() const override
  72. {
  73. AZ::ComponentTypeList requiredSystemComponents{
  74. azrtti_typeid<MPSGameLiftSystemComponent>()
  75. };
  76. #if AZ_TRAIT_CLIENT
  77. requiredSystemComponents.push_back(azrtti_typeid<MatchmakingSystemComponent>());
  78. requiredSystemComponents.push_back(azrtti_typeid<RegionalLatencySystemComponent>());
  79. requiredSystemComponents.push_back(azrtti_typeid<MPSGameLiftClientSystemComponent>());
  80. #endif
  81. // Only activate the MultiplayerSample GameLift server system component if this a dedicated server running on GameLift.
  82. #if AZ_DEDICATED_SERVER_ONLY
  83. const auto console = AZ::Interface<AZ::IConsole>::Get();
  84. if (console == nullptr)
  85. {
  86. AZ_Assert(false, "MultiplayerSample expecting to check AZ::Console, but it's not available. Please update code to properly check if this server is running on GameLift.");
  87. return requiredSystemComponents;
  88. }
  89. bool sv_gameLiftEnabled = false;
  90. if (console->GetCvarValue("sv_gameLiftEnabled", sv_gameLiftEnabled) != AZ::GetValueResult::Success)
  91. {
  92. AZ_Assert(false, "MultiplayerSample expecting to access an invalid sv_gameLiftEnabled. Please update code to properly check if GameLift is enabled in order to enable it's custom GameLift server system component.")
  93. return requiredSystemComponents;
  94. }
  95. if (sv_gameLiftEnabled)
  96. {
  97. requiredSystemComponents.push_back(azrtti_typeid<MPSGameLiftServerSystemComponent>());
  98. }
  99. #endif
  100. return requiredSystemComponents;
  101. }
  102. };
  103. }// namespace MPSGameLift