ScriptEventsSystemComponent.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "ScriptEventsSystemComponent.h"
  9. #include <AzCore/Component/ComponentApplicationBus.h>
  10. #include <AzCore/Component/EntityId.h>
  11. #include <AzCore/EBus/EBus.h>
  12. #include <AzCore/Math/Color.h>
  13. #include <AzCore/RTTI/TypeInfo.h>
  14. #include <AzCore/RTTI/BehaviorContext.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzCore/Serialization/EditContext.h>
  17. #include <AzCore/std/smart_ptr/make_shared.h>
  18. #include <AzCore/std/string/string.h>
  19. #include <AzCore/std/sort.h>
  20. #include <AzFramework/Asset/GenericAssetHandler.h>
  21. #include <ScriptEvents/ScriptEventsAssetRef.h>
  22. #include <ScriptEvents/ScriptEventDefinition.h>
  23. #include <ScriptEvents/ScriptEventFundamentalTypes.h>
  24. AZ_DEFINE_BUDGET(ScriptCanvas);
  25. namespace ScriptEvents
  26. {
  27. void ScriptEventsSystemComponent::Reflect(AZ::ReflectContext* context)
  28. {
  29. using namespace ScriptEvents;
  30. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  31. {
  32. serialize->Class<ScriptEventsSystemComponent, AZ::Component>()
  33. ->Version(1)
  34. // ScriptEvents avoids a use dependency on the AssetBuilderSDK. Therefore the Crc is used directly to register this component with the Gem builder
  35. ->Attribute(AZ::Edit::Attributes::SystemComponentTags, AZStd::vector<AZ::Crc32>({ AZ_CRC_CE("AssetBuilder") }));
  36. ;
  37. }
  38. ScriptEventData::VersionedProperty::Reflect(context);
  39. Parameter::Reflect(context);
  40. Method::Reflect(context);
  41. ScriptEvent::Reflect(context);
  42. ScriptEvents::ScriptEventsAsset::Reflect(context);
  43. ScriptEvents::ScriptEventsAssetRef::Reflect(context);
  44. ScriptEvents::ScriptEventsAssetPtr::Reflect(context);
  45. }
  46. void ScriptEventsSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  47. {
  48. provided.push_back(AZ_CRC_CE("ScriptEventsService"));
  49. }
  50. void ScriptEventsSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  51. {
  52. incompatible.push_back(AZ_CRC_CE("ScriptEventsService"));
  53. }
  54. void ScriptEventsSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  55. {
  56. required.push_back(AZ_CRC_CE("AssetDatabaseService"));
  57. }
  58. void ScriptEventsSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  59. {
  60. AZ_UNUSED(dependent);
  61. }
  62. void ScriptEventsSystemComponent::Init()
  63. {
  64. }
  65. void ScriptEventsSystemComponent::Activate()
  66. {
  67. ScriptEventsSystemComponentImpl* moduleConfiguration = nullptr;
  68. ScriptEventModuleConfigurationRequestBus::BroadcastResult(moduleConfiguration, &ScriptEventModuleConfigurationRequests::GetSystemComponentImpl);
  69. if (moduleConfiguration)
  70. {
  71. moduleConfiguration->RegisterAssetHandler();
  72. }
  73. }
  74. void ScriptEventsSystemComponent::Deactivate()
  75. {
  76. for (auto& asset : m_scriptEvents)
  77. {
  78. asset.second.reset();
  79. }
  80. m_scriptEvents.clear();
  81. ScriptEventsSystemComponentImpl* moduleConfiguration = nullptr;
  82. ScriptEventModuleConfigurationRequestBus::BroadcastResult(moduleConfiguration, &ScriptEventModuleConfigurationRequests::GetSystemComponentImpl);
  83. if (moduleConfiguration)
  84. {
  85. moduleConfiguration->UnregisterAssetHandler();
  86. }
  87. }
  88. void ScriptEventsSystemComponentRuntimeImpl::RegisterAssetHandler()
  89. {
  90. AZ::Data::AssetType assetType(azrtti_typeid<ScriptEvents::ScriptEventsAsset>());
  91. if (AZ::Data::AssetManager::Instance().GetHandler(assetType))
  92. {
  93. return; // Asset Type already handled
  94. }
  95. m_assetHandler = AZStd::make_unique<ScriptEventAssetRuntimeHandler>(ScriptEvents::ScriptEventsAsset::GetDisplayName(),
  96. ScriptEvents::ScriptEventsAsset::GetGroup(), ScriptEvents::ScriptEventsAsset::GetFileFilter(),
  97. AZ::AzTypeInfo<ScriptEvents::ScriptEventsSystemComponent>::Uuid());
  98. AZ::Data::AssetManager::Instance().RegisterHandler(m_assetHandler.get(), assetType);
  99. // Use AssetCatalog service to register ScriptCanvas asset type and extension
  100. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::AddAssetType, assetType);
  101. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::EnableCatalogForAsset, assetType);
  102. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::AddExtension,
  103. ScriptEvents::ScriptEventsAsset::GetFileFilter());
  104. }
  105. void ScriptEventsSystemComponentRuntimeImpl::UnregisterAssetHandler()
  106. {
  107. AZ::Data::AssetManager::Instance().UnregisterHandler(m_assetHandler.get());
  108. m_assetHandler.reset();
  109. }
  110. }