PythonBuilderWorker.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <PythonBuilderWorker.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. #include <AzToolsFramework/API/EditorPythonConsoleBus.h>
  12. #include <Source/PythonAssetBuilderSystemComponent.h>
  13. #include <PythonAssetBuilder/PythonBuilderNotificationBus.h>
  14. namespace PythonAssetBuilder
  15. {
  16. void PythonBuilderWorker::Reflect(AZ::ReflectContext* context)
  17. {
  18. if (auto&& serialize = azrtti_cast<AZ::SerializeContext*>(context))
  19. {
  20. serialize->Class<PythonBuilderWorker>()->Version(0);
  21. }
  22. if (auto&& behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  23. {
  24. behaviorContext->Class<PythonBuilderWorker>("PythonBuilderWorker")
  25. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  26. ->Attribute(AZ::Script::Attributes::Module, "asset.builder")
  27. ->Constructor()
  28. ;
  29. }
  30. }
  31. bool PythonBuilderWorker::ConfigureBuilderInformation(const AssetBuilderSDK::AssetBuilderDesc& assetBuilderDesc)
  32. {
  33. using namespace AssetBuilderSDK;
  34. if (m_assetBuilderDesc)
  35. {
  36. AZ_Error(PythonBuilder, false, "Asset Builder (%s) already configured!", assetBuilderDesc.m_name.c_str());
  37. return false;
  38. }
  39. // register the new PythonBuilderWorker instance with the Asset Builder
  40. m_assetBuilderDesc = AZStd::make_shared<AssetBuilderSDK::AssetBuilderDesc>(assetBuilderDesc);
  41. // prepare delegate handler for CreateJobs function to be resolved in a Python script
  42. m_assetBuilderDesc->m_createJobFunction = [this](auto&& request, auto&& response)
  43. {
  44. this->CreateJobs(request, response);
  45. };
  46. // prepare delegate handler for ProcessJob function to be resolved in a Python script
  47. m_assetBuilderDesc->m_processJobFunction = [this](auto&& request, auto&& response)
  48. {
  49. this->ProcessJob(request, response);
  50. };
  51. // connect to the shutdown signal handler
  52. AssetBuilderCommandBus::Handler::BusConnect(m_assetBuilderDesc->m_busId);
  53. // register with the Asset Builder
  54. AssetBuilderBus::Broadcast(&AssetBuilderBus::Events::RegisterBuilderInformation, *m_assetBuilderDesc);
  55. return true;
  56. }
  57. void PythonBuilderWorker::ShutDown()
  58. {
  59. // Note - Shutdown will be called on a different thread than your process job thread
  60. if (!m_isShuttingDown)
  61. {
  62. m_isShuttingDown = true;
  63. PythonBuilderNotificationBus::Event(m_busId, &PythonBuilderNotificationBus::Events::OnShutdown);
  64. AssetBuilderSDK::AssetBuilderCommandBus::Handler::BusDisconnect();
  65. }
  66. }
  67. void PythonBuilderWorker::CreateJobs(const AssetBuilderSDK::CreateJobsRequest& request, AssetBuilderSDK::CreateJobsResponse& response)
  68. {
  69. if (m_isShuttingDown)
  70. {
  71. response.m_result = AssetBuilderSDK::CreateJobsResultCode::ShuttingDown;
  72. return;
  73. }
  74. // assume failure
  75. response.m_result = AssetBuilderSDK::CreateJobsResultCode::Failed;
  76. PythonBuilderNotificationBus::EventResult(
  77. response,
  78. request.m_builderid,
  79. &PythonBuilderNotificationBus::Events::OnCreateJobsRequest,
  80. request);
  81. }
  82. void PythonBuilderWorker::ProcessJob(const AssetBuilderSDK::ProcessJobRequest& request, AssetBuilderSDK::ProcessJobResponse& response)
  83. {
  84. AssetBuilderSDK::JobCommandBus::Handler::BusConnect(request.m_jobId);
  85. // assume failure
  86. response.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  87. PythonBuilderNotificationBus::EventResult(
  88. response,
  89. request.m_builderGuid,
  90. &PythonBuilderNotificationBus::Events::OnProcessJobRequest,
  91. request);
  92. AssetBuilderSDK::JobCommandBus::Handler::BusDisconnect(request.m_jobId);
  93. }
  94. void PythonBuilderWorker::Cancel()
  95. {
  96. PythonBuilderNotificationBus::Event(m_busId, &PythonBuilderNotificationBus::Events::OnCancel);
  97. }
  98. }