2
0

ModuleGraphManager.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // AZ
  9. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  10. #include <AzFramework/StringFunc/StringFunc.h>
  11. #include <AzCore/IO/FileIO.h>
  12. #include <AzCore/Serialization/Utils.h>
  13. #include <AzCore/std/smart_ptr/make_shared.h>
  14. #include <AzCore/Component/ComponentApplicationBus.h>
  15. // Graph Model
  16. #include <GraphModel/Model/Graph.h>
  17. #include <GraphModel/Model/GraphContext.h>
  18. #include <GraphModel/Model/Module/ModuleGraphManager.h>
  19. namespace GraphModel
  20. {
  21. ModuleGraphManager::ModuleGraphManager(GraphContextPtr graphContext, AZ::SerializeContext* serializeContext)
  22. : m_graphContext(graphContext)
  23. , m_moduleFileExtension(graphContext->GetModuleFileExtension())
  24. , m_serializeContext(serializeContext)
  25. {
  26. if (m_serializeContext == nullptr)
  27. {
  28. // use the default app serialize context
  29. AZ::ComponentApplicationBus::BroadcastResult(m_serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext);
  30. if (!m_serializeContext)
  31. {
  32. AZ_Error(graphContext->GetSystemName(), false, "ModuleGraphManager: No serialize context provided! We will not be able to load module files.");
  33. }
  34. }
  35. AzToolsFramework::AssetSystemBus::Handler::BusConnect();
  36. }
  37. ModuleGraphManager::~ModuleGraphManager()
  38. {
  39. AzToolsFramework::AssetSystemBus::Handler::BusDisconnect();
  40. }
  41. void ModuleGraphManager::SourceFileChanged(AZStd::string relativePath, [[maybe_unused]] AZStd::string scanFolder, AZ::Uuid sourceUUID)
  42. {
  43. AZStd::string extension;
  44. if (AzFramework::StringFunc::Path::GetExtension(relativePath.data(), extension) && extension == m_moduleFileExtension)
  45. {
  46. // Force the manager to reload the graph next time GetModuleGraph() is called.
  47. m_graphs.erase(sourceUUID);
  48. }
  49. }
  50. ConstGraphPtr ModuleGraphManager::LoadGraph(AZ::IO::FileIOStream& stream)
  51. {
  52. GraphPtr graph = AZStd::make_shared<Graph>();
  53. bool loadSuccess = AZ::Utils::LoadObjectFromStreamInPlace(stream, *graph, m_serializeContext);
  54. if (loadSuccess)
  55. {
  56. graph->PostLoadSetup(m_graphContext.lock());
  57. return graph;
  58. }
  59. else
  60. {
  61. return nullptr;
  62. }
  63. }
  64. AZ::Outcome<ConstGraphPtr, AZStd::string> ModuleGraphManager::LoadGraph(AZ::Uuid sourceFileId)
  65. {
  66. bool gotSourceInfo = false;
  67. AZ::Data::AssetInfo assetInfo;
  68. AZStd::string watchFolder;
  69. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(gotSourceInfo, &AzToolsFramework::AssetSystem::AssetSystemRequest::GetSourceInfoBySourceUUID, sourceFileId, assetInfo, watchFolder);
  70. if (!gotSourceInfo)
  71. {
  72. return AZ::Failure<AZStd::string>("Could not get source file info for [" + sourceFileId.ToString<AZStd::string>() + "]");
  73. }
  74. AZStd::string extension;
  75. if (!AzFramework::StringFunc::Path::GetExtension(assetInfo.m_relativePath.data(), extension) || extension != m_moduleFileExtension)
  76. {
  77. return AZ::Failure<AZStd::string>("Incorrect extension [" + assetInfo.m_relativePath + "]. Must be [" + m_moduleFileExtension + "]");
  78. }
  79. AZStd::string fullAssetPath;
  80. AzFramework::StringFunc::Path::Join(watchFolder.data(), assetInfo.m_relativePath.data(), fullAssetPath);
  81. AZ::IO::FileIOStream stream;
  82. stream.Open(fullAssetPath.data(), AZ::IO::OpenMode::ModeRead);
  83. if (!stream.IsOpen())
  84. {
  85. return AZ::Failure<AZStd::string>("Could not open [" + fullAssetPath + "]");
  86. }
  87. ConstGraphPtr graph = LoadGraph(stream);
  88. if (!graph)
  89. {
  90. return AZ::Failure<AZStd::string>("Could not load [" + fullAssetPath + "]");
  91. }
  92. return AZ::Success(graph);
  93. }
  94. AZ::Outcome<ConstGraphPtr, AZStd::string> ModuleGraphManager::GetModuleGraph(AZ::Uuid sourceFileId)
  95. {
  96. auto iter = m_graphs.find(sourceFileId);
  97. // If the soure file has never been loaded, go ahead and load it now
  98. if (iter == m_graphs.end())
  99. {
  100. AZ::Outcome<ConstGraphPtr, AZStd::string> graphOutcome = LoadGraph(sourceFileId);
  101. if (!graphOutcome.IsSuccess())
  102. {
  103. return graphOutcome;
  104. }
  105. m_graphs[sourceFileId] = graphOutcome.GetValue();
  106. return graphOutcome;
  107. }
  108. else
  109. {
  110. // If the Graph has been loaded and is still in memory, we can just return it
  111. ConstGraphPtr graph = iter->second.lock();
  112. if (graph)
  113. {
  114. return AZ::Success(graph);
  115. }
  116. // The Graph has been released at some point and needs to be loaded again
  117. else
  118. {
  119. AZ::Outcome<ConstGraphPtr, AZStd::string> graphOutcome = LoadGraph(sourceFileId);
  120. if (!graphOutcome.IsSuccess())
  121. {
  122. m_graphs.erase(iter);
  123. return graphOutcome;
  124. }
  125. m_graphs[sourceFileId] = graphOutcome.GetValue();
  126. return graphOutcome;
  127. }
  128. }
  129. }
  130. } // namespace GraphModel