PresenceSystemComponent.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <PresenceSystemComponent.h>
  9. #include <Presence/PresenceNotificationBus.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. namespace Presence
  14. {
  15. class PresenceNotificationBusBehaviorHandler
  16. : public PresenceNotificationBus::Handler
  17. , public AZ::BehaviorEBusHandler
  18. {
  19. public:
  20. ////////////////////////////////////////////////////////////////////////////////////////////
  21. AZ_EBUS_BEHAVIOR_BINDER(PresenceNotificationBusBehaviorHandler, "{6ECFBA30-CBAA-498F-BE71-01C78B0215EA}", AZ::SystemAllocator
  22. , OnPresenceSet
  23. , OnPresenceQueried
  24. );
  25. ////////////////////////////////////////////////////////////////////////////////////////////
  26. void OnPresenceSet(const AzFramework::LocalUserId& localUserId) override
  27. {
  28. Call(FN_OnPresenceSet, localUserId);
  29. }
  30. ////////////////////////////////////////////////////////////////////////////////////////////
  31. void OnPresenceQueried(const PresenceDetails& details) override
  32. {
  33. Call(FN_OnPresenceQueried, details);
  34. }
  35. };
  36. ////////////////////////////////////////////////////////////////////////////////////////////
  37. void PresenceDetails::Reflect(AZ::ReflectContext* context)
  38. {
  39. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  40. {
  41. serialize->Class<PresenceDetails>()
  42. ->Version(0);
  43. if (AZ::EditContext* ec = serialize->GetEditContext())
  44. {
  45. ec->Class<PresenceDetails>("PresenceDetails", "Struct to hold platform agnostic presence details for query results")
  46. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  47. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  48. }
  49. }
  50. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  51. {
  52. behaviorContext->Class<PresenceDetails>()
  53. ->Constructor<const PresenceDetails&>()
  54. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  55. ->Property("localUserId", BehaviorValueProperty(&PresenceDetails::localUserId))
  56. ->Property("titleId", BehaviorValueProperty(&PresenceDetails::titleId))
  57. ->Property("title", BehaviorValueProperty(&PresenceDetails::title))
  58. ->Property("presence", BehaviorValueProperty(&PresenceDetails::presence));
  59. }
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////////////////
  62. PresenceDetails::PresenceDetails() :
  63. localUserId(0),
  64. titleId(0),
  65. title(""),
  66. presence("")
  67. {
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////////////////
  70. void PresenceSystemComponent::Reflect(AZ::ReflectContext* context)
  71. {
  72. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  73. {
  74. serialize->Class<PresenceSystemComponent, AZ::Component>()
  75. ->Version(0);
  76. if (AZ::EditContext* ec = serialize->GetEditContext())
  77. {
  78. ec->Class<PresenceSystemComponent>("Presence", "Platform agnostic interface for Presence API requests")
  79. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  80. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  81. ;
  82. }
  83. }
  84. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  85. {
  86. using SetPresenceParams = PresenceRequests::SetPresenceParams;
  87. behaviorContext->Class<SetPresenceParams>()
  88. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  89. ->Property("localUserId", BehaviorValueProperty(&SetPresenceParams::localUserId))
  90. ->Property("presenceToken", BehaviorValueProperty(&SetPresenceParams::presenceToken))
  91. ->Property("presenceString", BehaviorValueProperty(&SetPresenceParams::presenceString))
  92. ->Property("languageCode", BehaviorValueProperty(&SetPresenceParams::languageCode));
  93. using QueryPresenceParams = PresenceRequests::QueryPresenceParams;
  94. behaviorContext->Class<QueryPresenceParams>()
  95. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  96. ->Property("presence", BehaviorValueProperty(&QueryPresenceParams::presence))
  97. ->Property("localUserId", BehaviorValueProperty(&QueryPresenceParams::localUserId));
  98. behaviorContext->EBus<PresenceNotificationBus>("PresenceNotificationBus")
  99. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  100. ->Handler<PresenceNotificationBusBehaviorHandler>();
  101. behaviorContext->EBus<PresenceRequestBus>("PresenceRequestBus")
  102. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  103. ->Attribute(AZ::Script::Attributes::Category, "Presence")
  104. ->Event("SetPresence", &PresenceRequestBus::Events::SetPresence)
  105. ->Event("QueryPresence", &PresenceRequestBus::Events::QueryPresence);
  106. }
  107. PresenceDetails::Reflect(context);
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////////////////
  110. void PresenceSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  111. {
  112. provided.push_back(AZ_CRC_CE("PresenceService"));
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////////////////
  115. void PresenceSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  116. {
  117. incompatible.push_back(AZ_CRC_CE("PresenceService"));
  118. }
  119. ////////////////////////////////////////////////////////////////////////////////////////////
  120. void PresenceSystemComponent::Activate()
  121. {
  122. m_pimpl.reset(Implementation::Create(*this));
  123. PresenceRequestBus::Handler::BusConnect();
  124. }
  125. ////////////////////////////////////////////////////////////////////////////////////////////
  126. void PresenceSystemComponent::Deactivate()
  127. {
  128. PresenceRequestBus::Handler::BusDisconnect();
  129. m_pimpl.reset();
  130. }
  131. ////////////////////////////////////////////////////////////////////////////////////////////
  132. void PresenceSystemComponent::SetPresence(const SetPresenceParams& params)
  133. {
  134. #if defined(DEBUG_PRESENCE)
  135. AZ_Printf("Presence", "Setting presence for localuserId %u", params.localUserId);
  136. #endif
  137. if (m_pimpl)
  138. {
  139. m_pimpl->SetPresence(params);
  140. }
  141. }
  142. ////////////////////////////////////////////////////////////////////////////////////////////
  143. void PresenceSystemComponent::QueryPresence(const QueryPresenceParams& params)
  144. {
  145. #if defined(DEBUG_PRESENCE)
  146. AZ_Printf("Presence", "Querying presence info for localuserId %u", params.localUserId);
  147. #endif
  148. if (m_pimpl)
  149. {
  150. m_pimpl->QueryPresence(params);
  151. }
  152. }
  153. ////////////////////////////////////////////////////////////////////////////////////////////
  154. PresenceSystemComponent::Implementation::Implementation(PresenceSystemComponent& presenceSystemComponent)
  155. : m_presenceSystemComponent(presenceSystemComponent)
  156. {
  157. }
  158. ////////////////////////////////////////////////////////////////////////////////////////////
  159. PresenceSystemComponent::Implementation::~Implementation()
  160. {
  161. }
  162. ////////////////////////////////////////////////////////////////////////////////////////////
  163. void PresenceSystemComponent::Implementation::OnPresenceSetComplete(const SetPresenceParams& params)
  164. {
  165. AZ::TickBus::QueueFunction([params]()
  166. {
  167. if (params.OnPresenceSetCallback)
  168. {
  169. params.OnPresenceSetCallback(params.localUserId);
  170. }
  171. PresenceNotificationBus::Broadcast(&PresenceNotifications::OnPresenceSet, params.localUserId);
  172. });
  173. }
  174. ////////////////////////////////////////////////////////////////////////////////////////////
  175. void PresenceSystemComponent::Implementation::OnPresenceQueriedComplete(const QueryPresenceParams& params, const PresenceDetails& details)
  176. {
  177. AZ::TickBus::QueueFunction([params, details ]()
  178. {
  179. if (params.OnQueryPresenceCallback)
  180. {
  181. params.OnQueryPresenceCallback(details);
  182. }
  183. PresenceNotificationBus::Broadcast(&PresenceNotifications::OnPresenceQueried, details);
  184. });
  185. }
  186. }