GraphCompiler.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <AtomToolsFramework/Graph/AssetStatusReporterSystemRequestBus.h>
  9. #include <AtomToolsFramework/Graph/GraphCompiler.h>
  10. #include <AtomToolsFramework/Util/Util.h>
  11. #include <AtomToolsFramework/Window/AtomToolsMainWindowRequestBus.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <AzCore/Jobs/JobFunction.h>
  14. #include <AzCore/RTTI/RTTI.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  17. namespace AtomToolsFramework
  18. {
  19. void GraphCompiler::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (auto serialize = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serialize->Class<GraphCompiler>()
  24. ->Version(0)
  25. ;
  26. }
  27. }
  28. GraphCompiler::GraphCompiler(const AZ::Crc32& toolId)
  29. : m_toolId(toolId)
  30. {
  31. }
  32. GraphCompiler::~GraphCompiler()
  33. {
  34. // Stop monitoring assets from prior requests since the graph compiler is being destroyed.
  35. AssetStatusReporterSystemRequestBus::Event(
  36. m_toolId, &AssetStatusReporterSystemRequestBus::Events::StopReporting, m_assetReportRequestId);
  37. }
  38. bool GraphCompiler::IsCompileLoggingEnabled()
  39. {
  40. return GetSettingsValue("/O3DE/AtomToolsFramework/GraphCompiler/EnableLogging", false);
  41. }
  42. bool GraphCompiler::Reset()
  43. {
  44. if (CanCompileGraph())
  45. {
  46. return true;
  47. }
  48. SetState(State::Canceled);
  49. return false;
  50. }
  51. void GraphCompiler::SetStateChangeHandler(StateChangeHandler handler)
  52. {
  53. m_stateChangeHandler = handler;
  54. }
  55. void GraphCompiler::SetState(GraphCompiler::State state)
  56. {
  57. m_state = state;
  58. switch (m_state)
  59. {
  60. case State::Idle:
  61. ReportStatus(AZStd::string::format("%s (Idle)", GetGraphPath().c_str()));
  62. break;
  63. case State::Compiling:
  64. ReportStatus(AZStd::string::format("%s (Compiling)", GetGraphPath().c_str()));
  65. break;
  66. case State::Processing:
  67. ReportStatus(AZStd::string::format("%s (Processing)", GetGraphPath().c_str()));
  68. break;
  69. case State::Complete:
  70. ReportStatus(AZStd::string::format("%s (Complete)", GetGraphPath().c_str()));
  71. break;
  72. case State::Failed:
  73. ReportStatus(AZStd::string::format("%s (Failed)", GetGraphPath().c_str()));
  74. break;
  75. case State::Canceled:
  76. ReportStatus(AZStd::string::format("%s (Cancelled)", GetGraphPath().c_str()));
  77. break;
  78. }
  79. AssetStatusReporterSystemRequestBus::Event(
  80. m_toolId, &AssetStatusReporterSystemRequestBus::Events::StopReporting, m_assetReportRequestId);
  81. // Invoke the optional state change handler function if provided
  82. if (m_stateChangeHandler)
  83. {
  84. m_stateChangeHandler(this);
  85. }
  86. }
  87. GraphCompiler::State GraphCompiler::GetState() const
  88. {
  89. return m_state;
  90. }
  91. AZStd::string GraphCompiler::GetGraphPath() const
  92. {
  93. return m_graphPath;
  94. }
  95. const AZStd::vector<AZStd::string>& GraphCompiler::GetGeneratedFilePaths() const
  96. {
  97. return m_generatedFiles;
  98. }
  99. bool GraphCompiler::CanCompileGraph() const
  100. {
  101. switch (m_state)
  102. {
  103. case State::Idle:
  104. case State::Failed:
  105. case State::Complete:
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool GraphCompiler::CompileGraph(GraphModel::GraphPtr graph, const AZStd::string& graphName, const AZStd::string& graphPath)
  111. {
  112. if (!CanCompileGraph())
  113. {
  114. return false;
  115. }
  116. m_graph = graph;
  117. m_graphName = graphName;
  118. m_graphPath = graphPath;
  119. m_generatedFiles.clear();
  120. // Skip compilation if there is no graph or this is a template.
  121. if (!m_graph || m_graphName.empty() || GetGraphPath().empty())
  122. {
  123. SetState(State::Failed);
  124. return false;
  125. }
  126. SetState(State::Compiling);
  127. return true;
  128. }
  129. bool GraphCompiler::ReportGeneratedFileStatus()
  130. {
  131. SetState(State::Processing);
  132. // Start monitoring and reporting AP status for any files generated during this compile.
  133. if (!m_generatedFiles.empty())
  134. {
  135. // Begin requesting status from the asset reporting system, which manages a queue of requests from multiple graphs.
  136. AssetStatusReporterSystemRequestBus::Event(
  137. m_toolId, &AssetStatusReporterSystemRequestBus::Events::StartReporting, m_assetReportRequestId, m_generatedFiles);
  138. while (m_state == State::Processing)
  139. {
  140. AssetStatusReporterState status = AssetStatusReporterState::Failed;
  141. AssetStatusReporterSystemRequestBus::EventResult(
  142. status, m_toolId, &AssetStatusReporterSystemRequestBus::Events::GetStatus, m_assetReportRequestId);
  143. if (status != AssetStatusReporterState::Processing)
  144. {
  145. AssetStatusReporterSystemRequestBus::Event(
  146. m_toolId, &AssetStatusReporterSystemRequestBus::Events::StopReporting, m_assetReportRequestId);
  147. return status == AssetStatusReporterState::Succeeded;
  148. }
  149. // Sleep to give other possible threats time to make AssetStatusReporterSystemRequestBus requests
  150. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(10));
  151. }
  152. AssetStatusReporterSystemRequestBus::Event(
  153. m_toolId, &AssetStatusReporterSystemRequestBus::Events::StopReporting, m_assetReportRequestId);
  154. }
  155. return true;
  156. }
  157. void GraphCompiler::ReportStatus(const AZStd::string& statusMessage)
  158. {
  159. AZStd::scoped_lock lock(m_lastStatusMessageMutex);
  160. if (m_lastStatusMessage != statusMessage)
  161. {
  162. m_lastStatusMessage = statusMessage;
  163. AZ::SystemTickBus::QueueFunction([toolId = m_toolId, statusMessage]() {
  164. AtomToolsMainWindowRequestBus::Event(toolId, &AtomToolsMainWindowRequestBus::Events::SetStatusMessage, statusMessage);
  165. });
  166. }
  167. }
  168. } // namespace AtomToolsFramework