Model.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <AzCore/Asset/AssetManagerBus.h>
  9. #include <CryCommon/CrySystemBus.h>
  10. #include <Editor/View/Windows/Tools/UpgradeTool/Model.h>
  11. #include <IConsole.h>
  12. #include <ISystem.h>
  13. namespace ModifierCpp
  14. {
  15. }
  16. namespace ScriptCanvasEditor
  17. {
  18. namespace VersionExplorer
  19. {
  20. EditorKeepAlive::EditorKeepAlive()
  21. {
  22. ISystem* system = nullptr;
  23. CrySystemRequestBus::BroadcastResult(system, &CrySystemRequestBus::Events::GetCrySystem);
  24. if (system)
  25. {
  26. m_edKeepEditorActive = system->GetIConsole()->GetCVar("ed_KeepEditorActive");
  27. if (m_edKeepEditorActive)
  28. {
  29. m_keepEditorActive = m_edKeepEditorActive->GetIVal();
  30. m_edKeepEditorActive->Set(1);
  31. }
  32. }
  33. }
  34. EditorKeepAlive::~EditorKeepAlive()
  35. {
  36. if (m_edKeepEditorActive)
  37. {
  38. m_edKeepEditorActive->Set(m_keepEditorActive);
  39. }
  40. }
  41. Model::Model()
  42. {
  43. ModelRequestsBus::Handler::BusConnect();
  44. }
  45. Model::~Model()
  46. {
  47. ModelRequestsBus::Handler::BusDisconnect();
  48. }
  49. void Model::CacheSettings()
  50. {
  51. m_settingsCache = AZStd::make_unique<ScriptCanvas::Grammar::SettingsCache>();
  52. ScriptCanvas::Grammar::g_saveRawTranslationOuputToFile = false;
  53. ScriptCanvas::Grammar::g_printAbstractCodeModel = false;
  54. ScriptCanvas::Grammar::g_saveRawTranslationOuputToFile = false;
  55. }
  56. const ModificationResults* Model::GetResults()
  57. {
  58. return !IsWorking() ? &m_modResults : nullptr;
  59. }
  60. void Model::Idle()
  61. {
  62. m_state = State::Idle;
  63. m_keepEditorAlive.reset();
  64. m_log.Deactivate();
  65. }
  66. bool Model::IsReadyToModify() const
  67. {
  68. if (IsWorking())
  69. {
  70. return false;
  71. }
  72. if (!m_scanner)
  73. {
  74. return false;
  75. }
  76. return !m_scanner->GetResult().m_unfiltered.empty();
  77. }
  78. bool Model::IsWorking() const
  79. {
  80. return m_state != State::Idle;
  81. }
  82. void Model::Modify(const ModifyConfiguration& modification)
  83. {
  84. if (!IsReadyToModify())
  85. {
  86. AZ_Warning(ScriptCanvas::k_VersionExplorerWindow.data(), false, "Explorer is not ready to modify graphs.");
  87. return;
  88. }
  89. if (!modification.modifySingleAsset.RelativePath().empty())
  90. {
  91. const auto& results = m_scanner->GetResult();
  92. auto iter = AZStd::find_if
  93. ( results.m_unfiltered.begin()
  94. , results.m_unfiltered.end()
  95. , [&modification](const auto& candidate)
  96. {
  97. return candidate.AnyEquals(modification.modifySingleAsset);
  98. });
  99. if (iter == results.m_unfiltered.end())
  100. {
  101. AZ_Warning(ScriptCanvas::k_VersionExplorerWindow.data(), false, "Requested upgrade graph not found in scanned list.");
  102. return;
  103. }
  104. m_state = State::ModifySingle;
  105. m_modifier = AZStd::make_unique<Modifier>(modification, AZStd::vector<SourceHandle>{ *iter }, [this]() { OnModificationComplete(); });
  106. }
  107. else
  108. {
  109. auto results = m_scanner->TakeResult();
  110. m_state = State::ModifyAll;
  111. m_modifier = AZStd::make_unique<Modifier>(modification, AZStd::move(results.m_unfiltered), [this]() { OnModificationComplete(); });
  112. }
  113. m_modResults = {};
  114. m_log.Activate();
  115. m_keepEditorAlive = AZStd::make_unique<EditorKeepAlive>();
  116. }
  117. void Model::OnModificationComplete()
  118. {
  119. ModelNotificationsBus::Broadcast(&ModelNotificationsTraits::OnUpgradeComplete, m_modifier->GetResult());
  120. m_modifier.reset();
  121. if (m_state == State::ModifyAll)
  122. {
  123. m_scanner.reset();
  124. }
  125. Idle();
  126. RestoreSettings();
  127. }
  128. void Model::OnScanComplete()
  129. {
  130. ModelNotificationsBus::Broadcast(&ModelNotificationsTraits::OnScanComplete, m_scanner->GetResult());
  131. Idle();
  132. }
  133. void Model::Scan(const ScanConfiguration& config)
  134. {
  135. if (IsWorking())
  136. {
  137. AZ_Warning(ScriptCanvas::k_VersionExplorerWindow.data(), false, "Explorer is already working");
  138. return;
  139. }
  140. CacheSettings();
  141. m_state = State::Scanning;
  142. m_log.Activate();
  143. m_keepEditorAlive = AZStd::make_unique<EditorKeepAlive>();
  144. m_scanner = AZStd::make_unique<Scanner>(config, [this](){ OnScanComplete(); });
  145. }
  146. void Model::RestoreSettings()
  147. {
  148. m_settingsCache.reset();
  149. }
  150. } // namespace VersionExplorer
  151. } // namespace ScriptCanvasEditor