3
0

ScriptEventRegistration.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #pragma once
  9. #include <AzCore/Component/TickBus.h>
  10. #include <AzCore/Asset/AssetManager.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <ScriptEvents/Internal/BehaviorContextBinding/ScriptEventBinding.h>
  13. #include "ScriptEventsAsset.h"
  14. namespace ScriptEvents
  15. {
  16. namespace Internal
  17. {
  18. //! This is the internal object that represents a ScriptEvent.
  19. //! It provides the binding between the BehaviorContext and the messaging functionality.
  20. //! It is ref counted so that it remains alive as long as anything is referencing it, this can happen
  21. //! when multiple scripts or script canvas graphs are sending or receiving events defined in a given script event.
  22. class ScriptEventRegistration
  23. : public AZ::Data::AssetBus::Handler
  24. , public AZ::SystemTickBus::Handler
  25. {
  26. public:
  27. AZ_RTTI(ScriptEventRegistration, "{B8801400-65CD-49D5-B797-58E56D705A0A}");
  28. AZ_CLASS_ALLOCATOR(ScriptEventRegistration, AZ::SystemAllocator);
  29. AZ::AttributeArray* m_currentAttributes;
  30. ScriptEventRegistration() = default;
  31. virtual ~ScriptEventRegistration();
  32. ScriptEventRegistration(AZ::Data::AssetId scriptEventAssetId)
  33. {
  34. Init(scriptEventAssetId);
  35. }
  36. void Init(AZ::Data::AssetId scriptEventAssetId);
  37. bool GetMethod(AZStd::string_view eventName, AZ::BehaviorMethod*& outMethod);
  38. AZ::BehaviorEBus* GetBehaviorBus(AZ::u32 version = (std::numeric_limits<AZ::u32>::max)())
  39. {
  40. if (version == (std::numeric_limits<AZ::u32>::max)())
  41. {
  42. return m_behaviorEBus[m_maxVersion];
  43. }
  44. return m_behaviorEBus[version];
  45. }
  46. void CompleteRegistration(AZ::Data::Asset<AZ::Data::AssetData> asset);
  47. AZStd::string GetBusName() const
  48. {
  49. return m_busName;
  50. }
  51. bool IsReady() const { return m_isReady; }
  52. protected:
  53. void OnSystemTick() override;
  54. private:
  55. AZ::u32 m_maxVersion = 0;
  56. AZ::Data::AssetId m_assetId;
  57. AZStd::string m_busName;
  58. AZStd::unordered_map<AZ::u32, AZ::BehaviorEBus*> m_behaviorEBus; // version, ebus
  59. AZ::Data::Asset<ScriptEvents::ScriptEventsAsset> m_asset;
  60. AZStd::unordered_map<AZ::Data::AssetId, AZStd::unique_ptr<ScriptEventBinding>> m_scriptEventBindings;
  61. bool m_isReady = false;
  62. // AZ::Data::AssetBus::Handler
  63. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  64. void OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  65. //
  66. // Reference count for intrusive_ptr
  67. template<class T>
  68. friend struct AZStd::IntrusivePtrCountPolicy;
  69. mutable unsigned int m_refCount = 0;
  70. AZ_FORCE_INLINE void add_ref() { ++m_refCount; }
  71. AZ_FORCE_INLINE void release()
  72. {
  73. AZ_Assert(m_refCount > 0, "Reference count logic error, trying to release reference when there are none left.");
  74. if (--m_refCount == 0)
  75. {
  76. AZ::Data::AssetBus::Handler::BusDisconnect();
  77. delete this;
  78. }
  79. }
  80. AZStd::string m_previousName;
  81. int m_previousVersion;
  82. };
  83. class OnScopeEnd
  84. {
  85. public:
  86. using ScopeEndFunctor = std::function<void()>;
  87. private:
  88. ScopeEndFunctor m_functor;
  89. public:
  90. OnScopeEnd(ScopeEndFunctor&& functor)
  91. : m_functor(AZStd::move(functor))
  92. {}
  93. OnScopeEnd(const ScopeEndFunctor& functor)
  94. : m_functor(functor)
  95. {}
  96. ~OnScopeEnd()
  97. {
  98. m_functor();
  99. }
  100. };
  101. }
  102. }