PythonBuilderNotificationHandler.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <PythonBuilderNotificationHandler.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/EditContextConstants.inl>
  12. #include <AzToolsFramework/API/EditorPythonConsoleBus.h>
  13. #include <AssetBuilderSDK/AssetBuilderSDK.h>
  14. #include <EditorPythonBindings/EditorPythonBindingsSymbols.h>
  15. #include <Source/PythonAssetBuilderSystemComponent.h>
  16. namespace PythonAssetBuilder
  17. {
  18. void PythonBuilderNotificationHandler::Reflect(AZ::ReflectContext * context)
  19. {
  20. if (auto&& behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  21. {
  22. behaviorContext->EBus<PythonBuilderNotificationBus>("PythonBuilderNotificationBus")
  23. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  24. ->Attribute(AZ::Script::Attributes::Module, "asset.builder")
  25. ->Handler<PythonBuilderNotificationHandler>()
  26. ->Event("OnCreateJobsRequest", &PythonBuilderNotificationBus::Events::OnCreateJobsRequest)
  27. ->Event("OnProcessJobRequest", &PythonBuilderNotificationBus::Events::OnProcessJobRequest)
  28. ->Event("OnShutdown", &PythonBuilderNotificationBus::Events::OnShutdown)
  29. ->Event("OnCancel", &PythonBuilderNotificationBus::Events::OnCancel)
  30. ;
  31. }
  32. }
  33. AssetBuilderSDK::CreateJobsResponse PythonBuilderNotificationHandler::OnCreateJobsRequest(const AssetBuilderSDK::CreateJobsRequest& request)
  34. {
  35. AssetBuilderSDK::CreateJobsResponse response;
  36. try
  37. {
  38. auto editorPythonEventsInterface = AZ::Interface<AzToolsFramework::EditorPythonEventsInterface>::Get();
  39. if (editorPythonEventsInterface)
  40. {
  41. editorPythonEventsInterface->ExecuteWithLock([&response, &request, this]()
  42. {
  43. this->CallResult(response, FN_OnCreateJobsRequest, request);
  44. });
  45. }
  46. else
  47. {
  48. response.m_result = AssetBuilderSDK::CreateJobsResultCode::Failed;
  49. }
  50. }
  51. catch ([[maybe_unused]] const std::exception& e)
  52. {
  53. AZ_Error(PythonBuilder, false, "OnCreateJobsRequest exception %s", e.what());
  54. response.m_result = AssetBuilderSDK::CreateJobsResultCode::Failed;
  55. }
  56. return response;
  57. }
  58. AssetBuilderSDK::ProcessJobResponse PythonBuilderNotificationHandler::OnProcessJobRequest(const AssetBuilderSDK::ProcessJobRequest& request)
  59. {
  60. AssetBuilderSDK::ProcessJobResponse response;
  61. try
  62. {
  63. auto editorPythonEventsInterface = AZ::Interface<AzToolsFramework::EditorPythonEventsInterface>::Get();
  64. if (editorPythonEventsInterface)
  65. {
  66. editorPythonEventsInterface->ExecuteWithLock([&response, &request, this]()
  67. {
  68. this->CallResult(response, FN_OnProcessJobRequest, request);
  69. });
  70. }
  71. else
  72. {
  73. response.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  74. }
  75. }
  76. catch ([[maybe_unused]] const std::exception& e)
  77. {
  78. AZ_Error(PythonBuilder, false, "OnProcessJobRequest exception %s", e.what());
  79. response.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  80. }
  81. return response;
  82. }
  83. void PythonBuilderNotificationHandler::OnShutdown()
  84. {
  85. try
  86. {
  87. auto editorPythonEventsInterface = AZ::Interface<AzToolsFramework::EditorPythonEventsInterface>::Get();
  88. if (editorPythonEventsInterface)
  89. {
  90. editorPythonEventsInterface->ExecuteWithLock([this]()
  91. {
  92. this->Call(FN_OnShutdown);
  93. });
  94. }
  95. }
  96. catch ([[maybe_unused]] const std::exception& e)
  97. {
  98. AZ_Warning(PythonBuilder, false, "OnShutdown exception %s", e.what());
  99. }
  100. }
  101. void PythonBuilderNotificationHandler::OnCancel()
  102. {
  103. try
  104. {
  105. auto editorPythonEventsInterface = AZ::Interface<AzToolsFramework::EditorPythonEventsInterface>::Get();
  106. if (editorPythonEventsInterface)
  107. {
  108. editorPythonEventsInterface->ExecuteWithLock([this]()
  109. {
  110. this->Call(FN_OnCancel);
  111. });
  112. }
  113. }
  114. catch ([[maybe_unused]] const std::exception& e)
  115. {
  116. AZ_Error(PythonBuilder, false, "OnCancel exception %s", e.what());
  117. }
  118. }
  119. }