AssetImportRequest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <API/EditorAssetSystemAPI.h>
  9. #include <AzCore/IO/SystemFile.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. #include <AzFramework/StringFunc/StringFunc.h>
  12. #include <SceneAPI/SceneCore/Components/LoadingComponent.h>
  13. #include <SceneAPI/SceneCore/Components/SceneSystemComponent.h>
  14. #include <SceneAPI/SceneCore/Components/Utilities/EntityConstructor.h>
  15. #include <SceneAPI/SceneCore/Containers/Scene.h>
  16. #include <SceneAPI/SceneCore/Events/AssetImportRequest.h>
  17. #include <SceneAPI/SceneCore/Events/SceneSerializationBus.h>
  18. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  19. #include <AzCore/Serialization/Json/JsonUtils.h>
  20. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  21. #include <AzToolsFramework/Debug/TraceContext.h>
  22. #include <AzCore/JSON/writer.h>
  23. #include <AzCore/IO/TextStreamWriters.h>
  24. namespace AZ
  25. {
  26. namespace SceneAPI
  27. {
  28. namespace Events
  29. {
  30. struct UpdateManifestScope final
  31. : public AssetImportRequestReporter
  32. {
  33. AZ_RTTI(UpdateManifestScope, "{C9DD605E-2A9D-4C0D-A22E-A71B44528420}", AssetImportRequestReporter);
  34. UpdateManifestScope(AZStd::shared_ptr<Containers::Scene> scene)
  35. : m_scene(scene)
  36. {
  37. m_registered = false;
  38. if (AZ::SettingsRegistry::Get())
  39. {
  40. AZ::SettingsRegistry::Get()->Get(m_registered, AZ::SceneAPI::Utilities::Key_AssetProcessorInDebugOutput);
  41. }
  42. if (m_registered)
  43. {
  44. AZ::Interface<AssetImportRequestReporter>::Register(this);
  45. }
  46. }
  47. ~UpdateManifestScope()
  48. {
  49. if (m_registered)
  50. {
  51. AZ::Interface<AssetImportRequestReporter>::Unregister(this);
  52. m_registered = false;
  53. }
  54. m_previousManifest = {};
  55. }
  56. void ReportStart([[maybe_unused]] const AssetImportRequest* instance) override
  57. {
  58. ++m_reportNumber;
  59. if (m_previousManifest.IsEmpty() && !m_scene->GetManifest().IsEmpty())
  60. {
  61. m_previousManifest = m_scene->GetManifest();
  62. }
  63. }
  64. void ReportFinish(const AssetImportRequest* instance) override
  65. {
  66. AZStd::string policy;
  67. instance->GetPolicyName(policy);
  68. AZ_TraceContext("PolicyName", policy.c_str());
  69. if (ManifestInstancesAreEqual(m_previousManifest, m_scene->GetManifest()))
  70. {
  71. AZ_TracePrintf(AZ::SceneAPI::Utilities::LogWindow, "UpdateManifest(%d): No Changes \n", m_reportNumber);
  72. }
  73. else
  74. {
  75. AZ_TraceContext("Old manifest rule count", aznumeric_cast<int32_t>(m_previousManifest.GetEntryCount()));
  76. AZ_TraceContext("New manifest rule count", aznumeric_cast<int32_t>(m_scene->GetManifest().GetEntryCount()));
  77. AZ_TracePrintf(AZ::SceneAPI::Utilities::LogWindow, "UpdateManifest(%d): Updated \n", m_reportNumber);
  78. m_previousManifest = m_scene->GetManifest();
  79. }
  80. }
  81. bool ManifestInstancesAreEqual(const Containers::SceneManifest& lhs, const Containers::SceneManifest& rhs) const
  82. {
  83. if (lhs.IsEmpty() && rhs.IsEmpty())
  84. {
  85. return true;
  86. }
  87. if (lhs.GetEntryCount() == rhs.GetEntryCount())
  88. {
  89. auto rhsValueStorage = rhs.GetValueStorage();
  90. for (auto index = 0; index < lhs.GetEntryCount(); ++index)
  91. {
  92. const auto lhsObject = lhs.GetValue(index);
  93. auto found = AZStd::find_if(rhsValueStorage.begin(), rhsValueStorage.end(), [lhsObject](const auto& rhs)
  94. {
  95. return lhsObject.get()->RTTI_Type() == rhs.get()->RTTI_Type();
  96. });
  97. if (found == rhsValueStorage.end())
  98. {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. return false;
  105. }
  106. AZStd::shared_ptr<Containers::Scene> m_scene;
  107. AZ::s32 m_reportNumber = 0;
  108. bool m_registered = false;
  109. Containers::SceneManifest m_previousManifest;
  110. };
  111. // an internal scope object that is active during a scene import
  112. struct ImportScope final
  113. : public AZ::SceneAPI::Events::AssetPostImportRequestBus::Handler
  114. {
  115. ImportScope()
  116. {
  117. AZ::SceneAPI::Events::AssetPostImportRequestBus::Handler::BusConnect();
  118. }
  119. ~ImportScope()
  120. {
  121. AZ::SceneAPI::Events::AssetPostImportRequestBus::ExecuteQueuedEvents();
  122. AZ::SceneAPI::Events::AssetPostImportRequestBus::Handler::BusDisconnect();
  123. }
  124. void CallAfterSceneExport(AZStd::function<void()> callback) override
  125. {
  126. callback();
  127. }
  128. };
  129. //
  130. // Loading Result Combiner
  131. //
  132. LoadingResultCombiner::LoadingResultCombiner()
  133. : m_manifestResult(ProcessingResult::Ignored)
  134. , m_assetResult(ProcessingResult::Ignored)
  135. {
  136. }
  137. void LoadingResultCombiner::operator=(LoadingResult rhs)
  138. {
  139. switch (rhs)
  140. {
  141. case LoadingResult::Ignored:
  142. return;
  143. case LoadingResult::AssetLoaded:
  144. m_assetResult = m_assetResult != ProcessingResult::Failure ? ProcessingResult::Success : ProcessingResult::Failure;
  145. return;
  146. case LoadingResult::ManifestLoaded:
  147. m_manifestResult = m_manifestResult != ProcessingResult::Failure ? ProcessingResult::Success : ProcessingResult::Failure;
  148. return;
  149. case LoadingResult::AssetFailure:
  150. m_assetResult = ProcessingResult::Failure;
  151. return;
  152. case LoadingResult::ManifestFailure:
  153. m_manifestResult = ProcessingResult::Failure;
  154. return;
  155. }
  156. }
  157. ProcessingResult LoadingResultCombiner::GetManifestResult() const
  158. {
  159. return m_manifestResult;
  160. }
  161. ProcessingResult LoadingResultCombiner::GetAssetResult() const
  162. {
  163. return m_assetResult;
  164. }
  165. //
  166. // Asset Importer Request
  167. //
  168. void AssetImportRequest::GetManifestExtension(AZStd::string& /*result*/)
  169. {
  170. }
  171. void AssetImportRequest::GetGeneratedManifestExtension(AZStd::string& /*result*/)
  172. {
  173. }
  174. void AssetImportRequest::GetPolicyName(AZStd::string& result) const
  175. {
  176. result = "AssetImportRequest Base";
  177. }
  178. void AssetImportRequest::GetSupportedFileExtensions(AZStd::unordered_set<AZStd::string>& /*extensions*/)
  179. {
  180. }
  181. ProcessingResult AssetImportRequest::PrepareForAssetLoading(Containers::Scene& /*scene*/, RequestingApplication /*requester*/)
  182. {
  183. return ProcessingResult::Ignored;
  184. }
  185. LoadingResult AssetImportRequest::LoadAsset(Containers::Scene& /*scene*/, const AZStd::string& /*path*/, const Uuid& /*guid*/,
  186. RequestingApplication /*requester*/)
  187. {
  188. return LoadingResult::Ignored;
  189. }
  190. void AssetImportRequest::FinalizeAssetLoading(Containers::Scene& /*scene*/, RequestingApplication /*requester*/)
  191. {
  192. }
  193. ProcessingResult AssetImportRequest::UpdateManifest(Containers::Scene& /*scene*/, ManifestAction /*action*/, RequestingApplication /*requester*/)
  194. {
  195. return ProcessingResult::Ignored;
  196. }
  197. void AssetImportRequest::AreCustomNormalsUsed(bool &value)
  198. {
  199. // Leave the SceneProcessingConfigSystemComponent do the job
  200. AZ_UNUSED(value);
  201. }
  202. void AssetImportRequest::GetManifestDependencyPaths(AZStd::vector<AZStd::string>&)
  203. {
  204. }
  205. AZStd::shared_ptr<Containers::Scene> AssetImportRequest::LoadSceneFromVerifiedPath(
  206. const AZStd::string& assetFilePath,
  207. const Uuid& sourceGuid,
  208. RequestingApplication requester,
  209. const Uuid& loadingComponentUuid,
  210. const AZStd::string& watchFolder)
  211. {
  212. ImportScope importScope;
  213. AZStd::string sceneName;
  214. AzFramework::StringFunc::Path::GetFileName(assetFilePath.c_str(), sceneName);
  215. AZStd::shared_ptr<Containers::Scene> scene = AZStd::make_shared<Containers::Scene>(AZStd::move(sceneName));
  216. AZ_Assert(scene, "Unable to create new scene for asset importing.");
  217. Data::AssetInfo assetInfo;
  218. AZStd::string watchFolderFromDatabase;
  219. bool result = false;
  220. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(result, &AzToolsFramework::AssetSystemRequestBus::Events::GetSourceInfoBySourceUUID, sourceGuid, assetInfo, watchFolderFromDatabase);
  221. if (result)
  222. {
  223. scene->SetWatchFolder(watchFolderFromDatabase);
  224. }
  225. else
  226. {
  227. scene->SetWatchFolder(watchFolder);
  228. AZ_Warning(
  229. "AssetImportRequest", false, "Failed to get watch folder for source %s",
  230. sourceGuid.ToString<AZStd::string>().c_str());
  231. }
  232. // Unique pointer, will deactivate and clean up once going out of scope.
  233. SceneCore::EntityConstructor::EntityPointer loaders =
  234. SceneCore::EntityConstructor::BuildEntity("Scene Loading", loadingComponentUuid);
  235. ProcessingResultCombiner areAllPrepared;
  236. AssetImportRequestBus::BroadcastResult(areAllPrepared, &AssetImportRequestBus::Events::PrepareForAssetLoading, *scene, requester);
  237. if (areAllPrepared.GetResult() == ProcessingResult::Failure)
  238. {
  239. AZ_TracePrintf(Utilities::ErrorWindow, "Not all asset loaders could initialize.\n");
  240. return nullptr;
  241. }
  242. LoadingResultCombiner filesLoaded;
  243. AssetImportRequestBus::BroadcastResult(filesLoaded, &AssetImportRequestBus::Events::LoadAsset, *scene, assetFilePath, sourceGuid, requester);
  244. AssetImportRequestBus::Broadcast(&AssetImportRequestBus::Events::FinalizeAssetLoading, *scene, requester);
  245. if (filesLoaded.GetAssetResult() != ProcessingResult::Success)
  246. {
  247. AZ_TracePrintf(Utilities::ErrorWindow, "Failed to load requested scene file.\n");
  248. return nullptr;
  249. }
  250. ManifestAction action = ManifestAction::Update;
  251. // If the result for manifest is ignored it means no manifest was found or it was empty.
  252. if (filesLoaded.GetManifestResult() == ProcessingResult::Failure ||
  253. filesLoaded.GetManifestResult() == ProcessingResult::Ignored ||
  254. scene->GetManifest().IsEmpty())
  255. {
  256. scene->GetManifest().Clear();
  257. action = ManifestAction::ConstructDefault;
  258. }
  259. UpdateManifestScope updateManifestScope(scene);
  260. ProcessingResultCombiner manifestUpdate;
  261. AssetImportRequestBus::BroadcastResult(manifestUpdate, &AssetImportRequestBus::Events::UpdateManifest, *scene, action, requester);
  262. if (manifestUpdate.GetResult() == ProcessingResult::Failure)
  263. {
  264. AZ_TracePrintf(Utilities::ErrorWindow, "Unable to %s manifest.\n", action == ManifestAction::ConstructDefault ? "create new" : "update");
  265. return nullptr;
  266. }
  267. return scene;
  268. }
  269. bool AssetImportRequest::IsManifestExtension(const char* filePath)
  270. {
  271. AZStd::string manifestExtension;
  272. AssetImportRequestBus::Broadcast(&AssetImportRequestBus::Events::GetManifestExtension, manifestExtension);
  273. AZ_Assert(!manifestExtension.empty(), "Manifest extension was not declared.");
  274. return AzFramework::StringFunc::Path::IsExtension(filePath, manifestExtension.c_str());
  275. }
  276. bool AssetImportRequest::IsSceneFileExtension(const char* filePath)
  277. {
  278. AZStd::unordered_set<AZStd::string> extensions;
  279. AssetImportRequestBus::Broadcast(&AssetImportRequestBus::Events::GetSupportedFileExtensions, extensions);
  280. AZ_Assert(!extensions.empty(), "No extensions found for source files.");
  281. for (const AZStd::string& extension : extensions)
  282. {
  283. if (AzFramework::StringFunc::Path::IsExtension(filePath, extension.c_str()))
  284. {
  285. return true;
  286. }
  287. }
  288. return false;
  289. }
  290. } // namespace Events
  291. } // namespace SceneAPI
  292. } // namespace AZ