ScriptCanvasBuilderComponent.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <AssetBuilderSDK/AssetBuilderBusses.h>
  9. #include <AzCore/std/containers/map.h>
  10. #include <AzToolsFramework/ToolsComponents/ToolsAssetCatalogBus.h>
  11. #include <Builder/ScriptCanvasBuilder.h>
  12. #include <Builder/ScriptCanvasBuilderComponent.h>
  13. #include <Builder/ScriptCanvasBuilderWorker.h>
  14. #include <ScriptCanvas/Asset/RuntimeAssetHandler.h>
  15. #include <ScriptCanvas/Asset/SubgraphInterfaceAsset.h>
  16. #include <ScriptCanvas/Asset/SubgraphInterfaceAssetHandler.h>
  17. #include <ScriptCanvas/Utils/BehaviorContextUtils.h>
  18. namespace ScriptCanvasBuilder
  19. {
  20. template<typename t_Asset, typename t_Handler>
  21. HandlerOwnership RegisterHandler(const char* extension, bool enableCatalog)
  22. {
  23. AZ::Data::AssetType assetType(azrtti_typeid<t_Asset>());
  24. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::AddAssetType, assetType);
  25. if (enableCatalog)
  26. {
  27. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::EnableCatalogForAsset, assetType);
  28. }
  29. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::AddExtension, extension);
  30. if (auto borrowedHandler = AZ::Data::AssetManager::Instance().GetHandler(assetType))
  31. {
  32. return { borrowedHandler, false };
  33. }
  34. else
  35. {
  36. t_Handler* ownedHandler = aznew t_Handler();
  37. AZ::Data::AssetManager::Instance().RegisterHandler(ownedHandler, assetType);
  38. return { ownedHandler, true };
  39. }
  40. }
  41. SharedHandlers HandleAssetTypes()
  42. {
  43. SharedHandlers handlers;
  44. handlers.m_subgraphInterfaceHandler = RegisterHandler<ScriptCanvas::SubgraphInterfaceAsset, ScriptCanvas::SubgraphInterfaceAssetHandler>("scriptcanvas_fn_compiled", true);
  45. handlers.m_runtimeAssetHandler = RegisterHandler<ScriptCanvas::RuntimeAsset, JobDependencyVerificationHandler>("scriptcanvas_compiled", true);
  46. // \todo make it so we can load script events in the builder: expose the SE handler?
  47. // const AZStd::string description = ScriptCanvas::AssetDescription::GetExtension<ScriptCanvas::RuntimeAsset>();
  48. // handlers.m_scriptEventAssetHandler = RegisterHandler<ScriptEvents::ScriptEventsAsset, ScriptEventsEditor::ScriptEventAssetHandler>(description.data());
  49. return handlers;
  50. }
  51. void PluginComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  52. {
  53. provided.push_back(AZ_CRC_CE("ScriptCanvasBuilderService"));
  54. }
  55. void PluginComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  56. {
  57. required.push_back(AZ_CRC_CE("ScriptCanvasService"));
  58. required.push_back(AZ_CRC_CE("ScriptCanvasReflectService"));
  59. }
  60. void PluginComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  61. {
  62. dependent.push_back(AZ_CRC_CE("AssetCatalogService"));
  63. }
  64. void PluginComponent::Activate()
  65. {
  66. // Register ScriptCanvas Builder
  67. {
  68. AssetBuilderSDK::AssetBuilderDesc builderDescriptor;
  69. builderDescriptor.m_name = "Script Canvas Builder";
  70. builderDescriptor.m_patterns.push_back(AssetBuilderSDK::AssetBuilderPattern("*.scriptcanvas", AssetBuilderSDK::AssetBuilderPattern::PatternType::Wildcard));
  71. builderDescriptor.m_busId = ScriptCanvasBuilder::Worker::GetUUID();
  72. builderDescriptor.m_createJobFunction = AZStd::bind(&Worker::CreateJobs, &m_scriptCanvasBuilder, AZStd::placeholders::_1, AZStd::placeholders::_2);
  73. builderDescriptor.m_processJobFunction = AZStd::bind(&Worker::ProcessJob, &m_scriptCanvasBuilder, AZStd::placeholders::_1, AZStd::placeholders::_2);
  74. // changing the version number invalidates all assets and will rebuild everything.
  75. builderDescriptor.m_version = m_scriptCanvasBuilder.GetVersionNumber();
  76. // changing the analysis fingerprint just invalidates analysis (ie, not the assets themselves)
  77. // which will cause the "CreateJobs" function to be called, for each asset, even if the
  78. // source file has not changed, but won't actually do the jobs unless the source file has changed
  79. // or the fingerprint of the individual job is different.
  80. size_t fingerprint = ScriptCanvas::BehaviorContextUtils::GenerateFingerprintForBehaviorContext();
  81. builderDescriptor.m_analysisFingerprint = AZStd::string(m_scriptCanvasBuilder.GetFingerprintString())
  82. .append("|").append(AZStd::to_string(static_cast<AZ::u64>(fingerprint)));
  83. // Include the base node version in the hash, so when it changes, script canvas jobs are reprocessed.
  84. AZStd::hash_combine(fingerprint, ScriptCanvas::Node::GetNodeVersion());
  85. builderDescriptor.AddFlags(AssetBuilderSDK::AssetBuilderDesc::BF_DeleteLastKnownGoodProductOnFailure, s_scriptCanvasProcessJobKey);
  86. builderDescriptor.m_productsToKeepOnFailure[s_scriptCanvasProcessJobKey] = { AZ_CRC_CE("SubgraphInterface") };
  87. m_scriptCanvasBuilder.BusConnect(builderDescriptor.m_busId);
  88. AssetBuilderSDK::AssetBuilderBus::Broadcast(&AssetBuilderSDK::AssetBuilderBus::Handler::RegisterBuilderInformation, builderDescriptor);
  89. }
  90. m_sharedHandlers = HandleAssetTypes();
  91. AssetHandlers workerHandlers(m_sharedHandlers);
  92. m_scriptCanvasBuilder.Activate(workerHandlers);
  93. }
  94. void PluginComponent::Deactivate()
  95. {
  96. // Finish all queued work
  97. AZ::Data::AssetBus::ExecuteQueuedEvents();
  98. m_scriptCanvasBuilder.BusDisconnect();
  99. m_sharedHandlers.DeleteOwnedHandlers();
  100. }
  101. void PluginComponent::Reflect(AZ::ReflectContext* context)
  102. {
  103. BuildVariableOverrides::Reflect(context);
  104. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  105. {
  106. serializeContext->Class<PluginComponent, AZ::Component>()
  107. ->Version(0)
  108. ->Attribute(AZ::Edit::Attributes::SystemComponentTags, AZStd::vector<AZ::Crc32>({ AssetBuilderSDK::ComponentTags::AssetBuilder }));
  109. ;
  110. }
  111. }
  112. }