PythonAssetBuilderSystemComponent.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <PythonAssetBuilderSystemComponent.h>
  9. #include <PythonAssetBuilder/PythonAssetBuilderBus.h>
  10. #include <AzCore/IO/Path/Path.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/EditContextConstants.inl>
  14. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  15. #include <AzCore/std/smart_ptr/make_shared.h>
  16. #include <AzCore/StringFunc/StringFunc.h>
  17. #include <AzFramework/API/ApplicationAPI.h>
  18. #include <AzFramework/IO/LocalFileIO.h>
  19. #include <AssetBuilderSDK/AssetBuilderSDK.h>
  20. #include <AzToolsFramework/API/EditorPythonConsoleBus.h>
  21. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  22. #include <AzToolsFramework/Slice/SliceTransaction.h>
  23. #include <AzToolsFramework/Slice/SliceUtilities.h>
  24. #include <EditorPythonBindings/EditorPythonBindingsSymbols.h>
  25. #include <Source/PythonBuilderWorker.h>
  26. #include <Source/PythonBuilderMessageSink.h>
  27. #include <Source/PythonBuilderNotificationHandler.h>
  28. namespace PythonAssetBuilder
  29. {
  30. void PythonAssetBuilderSystemComponent::Reflect(AZ::ReflectContext* context)
  31. {
  32. PythonBuilderNotificationHandler::Reflect(context);
  33. PythonBuilderWorker::Reflect(context);
  34. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  35. {
  36. const AZStd::vector<AZ::Crc32> systemTags({ AssetBuilderSDK::ComponentTags::AssetBuilder });
  37. serialize->Class<PythonAssetBuilderSystemComponent, AZ::Component>()
  38. ->Version(0)
  39. ->Attribute(AZ::Edit::Attributes::SystemComponentTags, systemTags)
  40. ;
  41. }
  42. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  43. {
  44. behaviorContext->EBus<PythonAssetBuilderRequestBus>("PythonAssetBuilderRequestBus")
  45. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  46. ->Attribute(AZ::Script::Attributes::Module, "asset.builder")
  47. ->Event("RegisterAssetBuilder", &PythonAssetBuilderRequestBus::Events::RegisterAssetBuilder)
  48. ->Event("GetExecutableFolder", &PythonAssetBuilderRequestBus::Events::GetExecutableFolder)
  49. ;
  50. }
  51. }
  52. void PythonAssetBuilderSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  53. {
  54. provided.push_back(AZ_CRC_CE("PythonAssetBuilderService"));
  55. }
  56. void PythonAssetBuilderSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  57. {
  58. incompatible.push_back(AZ_CRC_CE("PythonAssetBuilderService"));
  59. }
  60. void PythonAssetBuilderSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  61. {
  62. dependent.push_back(EditorPythonBindings::PythonMarshalingService);
  63. dependent.push_back(EditorPythonBindings::PythonReflectionService);
  64. dependent.push_back(EditorPythonBindings::PythonEmbeddedService);
  65. }
  66. void PythonAssetBuilderSystemComponent::Init()
  67. {
  68. m_messageSink = AZStd::make_shared<PythonBuilderMessageSink>();
  69. }
  70. void PythonAssetBuilderSystemComponent::Activate()
  71. {
  72. PythonAssetBuilderRequestBus::Handler::BusConnect();
  73. if (auto&& pythonInterface = AZ::Interface<AzToolsFramework::EditorPythonEventsInterface>::Get())
  74. {
  75. pythonInterface->StartPython(true);
  76. }
  77. }
  78. void PythonAssetBuilderSystemComponent::Deactivate()
  79. {
  80. m_messageSink.reset();
  81. if (PythonAssetBuilderRequestBus::HasHandlers())
  82. {
  83. PythonAssetBuilderRequestBus::Handler::BusDisconnect();
  84. if (auto&& pythonInterface = AZ::Interface<AzToolsFramework::EditorPythonEventsInterface>::Get())
  85. {
  86. pythonInterface->StopPython(true);
  87. }
  88. }
  89. }
  90. AZ::Outcome<bool, AZStd::string> PythonAssetBuilderSystemComponent::RegisterAssetBuilder(const AssetBuilderSDK::AssetBuilderDesc& desc)
  91. {
  92. const AZ::Uuid busId = desc.m_busId;
  93. if (m_pythonBuilderWorkerMap.find(busId) != m_pythonBuilderWorkerMap.end())
  94. {
  95. AZStd::string busIdString(busId.ToString<AZStd::string>());
  96. AZStd::string failMessage = AZStd::string::format("Asset Builder of JobId:%s has already been created.", busIdString.c_str());
  97. AZ_Warning(PythonBuilder, false, failMessage.c_str());
  98. return AZ::Failure(failMessage);
  99. }
  100. // create a PythonBuilderWorker instance
  101. auto worker = AZStd::make_shared<PythonBuilderWorker>();
  102. if (worker->ConfigureBuilderInformation(desc) == false)
  103. {
  104. return AZ::Failure(AZStd::string::format("Failed to configure builderId:%s", busId.ToString<AZStd::string>().c_str()));
  105. }
  106. m_pythonBuilderWorkerMap[busId] = worker;
  107. return AZ::Success(true);
  108. }
  109. AZ::Outcome<AZStd::string, AZStd::string> PythonAssetBuilderSystemComponent::GetExecutableFolder() const
  110. {
  111. const char* exeFolderName = nullptr;
  112. AZ::ComponentApplicationBus::BroadcastResult(exeFolderName, &AZ::ComponentApplicationRequests::GetExecutableFolder);
  113. if (exeFolderName)
  114. {
  115. return AZ::Success(AZStd::string(exeFolderName));
  116. }
  117. return AZ::Failure(AZStd::string("GetExecutableFolder access is missing."));
  118. }
  119. }