3
0

AssetStatusReporterSystem.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/AssetStatusReporterSystem.h>
  9. #include <AtomToolsFramework/Window/AtomToolsMainWindowRequestBus.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/std/smart_ptr/make_shared.h>
  12. namespace AtomToolsFramework
  13. {
  14. AssetStatusReporterSystem::AssetStatusReporterSystem(const AZ::Crc32& toolId)
  15. : m_toolId(toolId)
  16. {
  17. AssetStatusReporterSystemRequestBus::Handler::BusConnect(m_toolId);
  18. // Create a thread that continuously processes a queue of incoming asset status requests.
  19. m_threadRunning = true;
  20. m_threadDesc.m_name = "AssetStatusReporterSystem";
  21. m_thread = AZStd::thread(
  22. m_threadDesc,
  23. [this]()
  24. {
  25. while (m_threadRunning)
  26. {
  27. Update();
  28. // Sleep briefly to give AP time to update and other possible threads time to make AssetSystemJobRequestBus requests
  29. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(10));
  30. }
  31. });
  32. }
  33. AssetStatusReporterSystem::~AssetStatusReporterSystem()
  34. {
  35. StopReportingAll();
  36. m_threadRunning = false;
  37. m_thread.join();
  38. AssetStatusReporterSystemRequestBus::Handler::BusDisconnect();
  39. }
  40. void AssetStatusReporterSystem::StartReporting(const AZ::Uuid& requestId, const AZStd::vector<AZStd::string>& sourcePaths)
  41. {
  42. StopReporting(requestId);
  43. AZStd::scoped_lock lock(m_requestMutex);
  44. m_activeReporterTable.emplace_back(requestId, AZStd::make_shared<AssetStatusReporter>(sourcePaths));
  45. }
  46. void AssetStatusReporterSystem::StopReporting(const AZ::Uuid& requestId)
  47. {
  48. AZStd::scoped_lock lock(m_requestMutex);
  49. AZStd::erase_if(m_activeReporterTable, [&requestId](const auto& reporterPair) {
  50. return reporterPair.first == requestId;
  51. });
  52. AZStd::erase_if(m_inactiveReporterTable, [&requestId](const auto& reporterPair) {
  53. return reporterPair.first == requestId;
  54. });
  55. }
  56. void AssetStatusReporterSystem::StopReportingAll()
  57. {
  58. AZStd::scoped_lock lock(m_requestMutex);
  59. m_activeReporterTable.clear();
  60. m_inactiveReporterTable.clear();
  61. }
  62. AssetStatusReporterState AssetStatusReporterSystem::GetStatus(const AZ::Uuid& requestId) const
  63. {
  64. AZStd::scoped_lock lock(m_requestMutex);
  65. if (auto reporterIt = AZStd::find_if(
  66. m_activeReporterTable.begin(),
  67. m_activeReporterTable.end(),
  68. [&requestId](const auto& reporterPair)
  69. {
  70. return reporterPair.first == requestId;
  71. });
  72. reporterIt != m_activeReporterTable.end())
  73. {
  74. return reporterIt->second->GetCurrentState();
  75. }
  76. if (auto reporterIt = AZStd::find_if(
  77. m_inactiveReporterTable.begin(),
  78. m_inactiveReporterTable.end(),
  79. [&requestId](const auto& reporterPair)
  80. {
  81. return reporterPair.first == requestId;
  82. });
  83. reporterIt != m_inactiveReporterTable.end())
  84. {
  85. return reporterIt->second->GetCurrentState();
  86. }
  87. return AssetStatusReporterState::Invalid;
  88. }
  89. void AssetStatusReporterSystem::Update()
  90. {
  91. AZStd::scoped_lock lock(m_requestMutex);
  92. if (!m_activeReporterTable.empty())
  93. {
  94. // Retrieve and update the status for the current active request.
  95. auto reporterIt = m_activeReporterTable.begin();
  96. reporterIt->second->Update();
  97. // Create a string message from the current status.
  98. const AZStd::string statusMessage = reporterIt->second->GetCurrentStatusMessage();
  99. // If the message has not changed since the last update then send it to the main windows status bar.
  100. if (m_lastStatusMessage != statusMessage)
  101. {
  102. m_lastStatusMessage = statusMessage;
  103. // Queuing the notification on the system take bus so that it triggers on the main thread.
  104. AZ::SystemTickBus::QueueFunction([toolId = m_toolId, statusMessage]() {
  105. // This should be generalized with a status reporter notification bus so the message can be handled by other systems
  106. // or UI than the status bar.
  107. AtomToolsMainWindowRequestBus::Event(
  108. toolId, &AtomToolsMainWindowRequestBus::Events::SetStatusMessage, statusMessage);
  109. });
  110. }
  111. // Any complete or canceled requests will get moved to the inactive list.
  112. if (reporterIt->second->GetCurrentState() != AssetStatusReporterState::Processing)
  113. {
  114. m_inactiveReporterTable.emplace_back(AZStd::move(*reporterIt));
  115. m_activeReporterTable.erase(reporterIt);
  116. m_lastStatusMessage.clear();
  117. }
  118. }
  119. }
  120. } // namespace AtomToolsFramework