AWSGameLiftClientSystemComponent.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <AzCore/Serialization/SerializeContext.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Serialization/EditContextConstants.inl>
  15. #include <AzFramework/Session/SessionConfig.h>
  16. #include <AWSGameLiftClientManager.h>
  17. #include <AWSGameLiftClientSystemComponent.h>
  18. #include <Request/AWSGameLiftCreateSessionOnQueueRequest.h>
  19. #include <Request/AWSGameLiftCreateSessionRequest.h>
  20. #include <Request/AWSGameLiftJoinSessionRequest.h>
  21. #include <Request/AWSGameLiftSearchSessionsRequest.h>
  22. #include <aws/gamelift/GameLiftClient.h>
  23. namespace AWSGameLift
  24. {
  25. AWSGameLiftClientSystemComponent::AWSGameLiftClientSystemComponent()
  26. {
  27. m_gameliftClientManager = AZStd::make_unique<AWSGameLiftClientManager>();
  28. }
  29. void AWSGameLiftClientSystemComponent::Reflect(AZ::ReflectContext* context)
  30. {
  31. ReflectCreateSessionRequest(context);
  32. AWSGameLiftCreateSessionOnQueueRequest::Reflect(context);
  33. AWSGameLiftCreateSessionRequest::Reflect(context);
  34. AWSGameLiftJoinSessionRequest::Reflect(context);
  35. AWSGameLiftSearchSessionsRequest::Reflect(context);
  36. ReflectSearchSessionsResponse(context);
  37. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  38. {
  39. serialize->Class<AWSGameLiftClientSystemComponent, AZ::Component>()
  40. ->Version(0)
  41. ;
  42. if (AZ::EditContext* editContext = serialize->GetEditContext())
  43. {
  44. editContext
  45. ->Class<AWSGameLiftClientSystemComponent>(
  46. "AWSGameLiftClient",
  47. "Create the GameLift client manager that handles communication between game clients and the GameLift service.")
  48. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  49. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
  50. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  51. ;
  52. }
  53. }
  54. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  55. {
  56. behaviorContext->EBus<AWSGameLiftRequestBus>("AWSGameLiftRequestBus")
  57. ->Attribute(AZ::Script::Attributes::Category, "AWSGameLift")
  58. ->Event("ConfigureGameLiftClient", &AWSGameLiftRequestBus::Events::ConfigureGameLiftClient,
  59. {{{"Region", ""}}})
  60. ->Event("CreatePlayerId", &AWSGameLiftRequestBus::Events::CreatePlayerId,
  61. {{{"IncludeBrackets", ""},
  62. {"IncludeDashes", ""}}})
  63. ;
  64. behaviorContext->EBus<AWSGameLiftSessionAsyncRequestBus>("AWSGameLiftSessionAsyncRequestBus")
  65. ->Attribute(AZ::Script::Attributes::Category, "AWSGameLift")
  66. ->Event("CreateSessionAsync", &AWSGameLiftSessionAsyncRequestBus::Events::CreateSessionAsync,
  67. {{{"CreateSessionRequest", ""}}})
  68. ->Event("JoinSessionAsync", &AWSGameLiftSessionAsyncRequestBus::Events::JoinSessionAsync,
  69. {{{"JoinSessionRequest", ""}}})
  70. ->Event("SearchSessionsAsync", &AWSGameLiftSessionAsyncRequestBus::Events::SearchSessionsAsync,
  71. {{{"SearchSessionsRequest", ""}}})
  72. ->Event("LeaveSessionAsync", &AWSGameLiftSessionAsyncRequestBus::Events::LeaveSessionAsync)
  73. ;
  74. behaviorContext
  75. ->EBus<AzFramework::SessionAsyncRequestNotificationBus>("AWSGameLiftSessionAsyncRequestNotificationBus")
  76. ->Attribute(AZ::Script::Attributes::Category, "AWSGameLift")
  77. ->Handler<AWSGameLiftSessionAsyncRequestNotificationBusHandler>()
  78. ;
  79. behaviorContext->EBus<AWSGameLiftSessionRequestBus>("AWSGameLiftSessionRequestBus")
  80. ->Attribute(AZ::Script::Attributes::Category, "AWSGameLift")
  81. ->Event("CreateSession", &AWSGameLiftSessionRequestBus::Events::CreateSession,
  82. {{{"CreateSessionRequest", ""}}})
  83. ->Event("JoinSession", &AWSGameLiftSessionRequestBus::Events::JoinSession,
  84. {{{"JoinSessionRequest", ""}}})
  85. ->Event("SearchSessions", &AWSGameLiftSessionRequestBus::Events::SearchSessions,
  86. {{{"SearchSessionsRequest", ""}}})
  87. ->Event("LeaveSession", &AWSGameLiftSessionRequestBus::Events::LeaveSession)
  88. ;
  89. }
  90. }
  91. void AWSGameLiftClientSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  92. {
  93. provided.push_back(AZ_CRC_CE("AWSGameLiftClientService"));
  94. }
  95. void AWSGameLiftClientSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  96. {
  97. incompatible.push_back(AZ_CRC_CE("AWSGameLiftClientService"));
  98. }
  99. void AWSGameLiftClientSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  100. {
  101. required.push_back(AZ_CRC_CE("AWSCoreService"));
  102. }
  103. void AWSGameLiftClientSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  104. {
  105. AZ_UNUSED(dependent);
  106. }
  107. void AWSGameLiftClientSystemComponent::Init()
  108. {
  109. }
  110. void AWSGameLiftClientSystemComponent::Activate()
  111. {
  112. m_gameliftClientManager->ActivateManager();
  113. }
  114. void AWSGameLiftClientSystemComponent::Deactivate()
  115. {
  116. m_gameliftClientManager->DeactivateManager();
  117. }
  118. void AWSGameLiftClientSystemComponent::ReflectCreateSessionRequest(AZ::ReflectContext* context)
  119. {
  120. AzFramework::CreateSessionRequest::Reflect(context);
  121. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  122. {
  123. behaviorContext->Class<AzFramework::CreateSessionRequest>("CreateSessionRequest")
  124. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  125. // Expose base type to BehaviorContext, but hide it to be used directly
  126. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  127. ;
  128. }
  129. }
  130. void AWSGameLiftClientSystemComponent::ReflectSearchSessionsResponse(AZ::ReflectContext* context)
  131. {
  132. // As it is a common response type, reflection could be moved to AzFramework to avoid duplication
  133. AzFramework::SessionConfig::Reflect(context);
  134. AzFramework::SearchSessionsResponse::Reflect(context);
  135. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  136. {
  137. behaviorContext->Class<AzFramework::SessionConfig>("SessionConfig")
  138. ->Attribute(AZ::Script::Attributes::Category, "Session")
  139. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  140. ->Property("CreationTime", BehaviorValueProperty(&AzFramework::SessionConfig::m_creationTime))
  141. ->Property("CreatorId", BehaviorValueProperty(&AzFramework::SessionConfig::m_creatorId))
  142. ->Property("CurrentPlayer", BehaviorValueProperty(&AzFramework::SessionConfig::m_currentPlayer))
  143. ->Property("DnsName", BehaviorValueProperty(&AzFramework::SessionConfig::m_dnsName))
  144. ->Property("IpAddress", BehaviorValueProperty(&AzFramework::SessionConfig::m_ipAddress))
  145. ->Property("MaxPlayer", BehaviorValueProperty(&AzFramework::SessionConfig::m_maxPlayer))
  146. ->Property("Port", BehaviorValueProperty(&AzFramework::SessionConfig::m_port))
  147. ->Property("SessionId", BehaviorValueProperty(&AzFramework::SessionConfig::m_sessionId))
  148. ->Property("SessionName", BehaviorValueProperty(&AzFramework::SessionConfig::m_sessionName))
  149. ->Property("SessionProperties", BehaviorValueProperty(&AzFramework::SessionConfig::m_sessionProperties))
  150. ->Property("Status", BehaviorValueProperty(&AzFramework::SessionConfig::m_status))
  151. ->Property("StatusReason", BehaviorValueProperty(&AzFramework::SessionConfig::m_statusReason))
  152. ->Property("TerminationTime", BehaviorValueProperty(&AzFramework::SessionConfig::m_terminationTime))
  153. ;
  154. behaviorContext->Class<AzFramework::SearchSessionsResponse>("SearchSessionsResponse")
  155. ->Attribute(AZ::Script::Attributes::Category, "Session")
  156. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  157. ->Property("NextToken", BehaviorValueProperty(&AzFramework::SearchSessionsResponse::m_nextToken))
  158. ->Property("SessionConfigs", BehaviorValueProperty(&AzFramework::SearchSessionsResponse::m_sessionConfigs))
  159. ;
  160. }
  161. }
  162. void AWSGameLiftClientSystemComponent::SetGameLiftClientManager(AZStd::unique_ptr<AWSGameLiftClientManager> gameliftClientManager)
  163. {
  164. m_gameliftClientManager.reset();
  165. m_gameliftClientManager = AZStd::move(gameliftClientManager);
  166. }
  167. } // namespace AWSGameLift