AssetImportRequest.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #pragma once
  9. #include <AzCore/EBus/EBus.h>
  10. #include <AzCore/Math/Uuid.h>
  11. #include <AzCore/std/smart_ptr/shared_ptr.h>
  12. #include <AzCore/std/string/string.h>
  13. #include <AzCore/std/containers/unordered_set.h>
  14. #include <SceneAPI/SceneCore/SceneCoreConfiguration.h>
  15. #include <SceneAPI/SceneCore/Events/ProcessingResult.h>
  16. #include <AzCore/Interface/Interface.h>
  17. namespace AZ
  18. {
  19. namespace SceneAPI
  20. {
  21. namespace Containers
  22. {
  23. class Scene;
  24. }
  25. namespace Events
  26. {
  27. enum class LoadingResult
  28. {
  29. Ignored,
  30. AssetLoaded,
  31. ManifestLoaded,
  32. AssetFailure,
  33. ManifestFailure
  34. };
  35. class AssetImportRequest;
  36. struct AssetImportRequestReporter
  37. {
  38. AZ_RTTI(AssetImportRequestReporter, "{3BCEDF5C-9FE6-4A16-A521-D2362E51522F}");
  39. virtual void ReportStart(const AssetImportRequest* instance) = 0;
  40. virtual void ReportFinish(const AssetImportRequest* instance) = 0;
  41. };
  42. struct AssetImportRequestEventProcessingPolicy
  43. {
  44. template<class Interface>
  45. static void ReportStart(Interface&& iface)
  46. {
  47. if (auto* reporter = AZ::Interface<AssetImportRequestReporter>::Get(); reporter)
  48. {
  49. reporter->ReportStart(static_cast<const AssetImportRequest*>(iface));
  50. }
  51. }
  52. template<class Interface>
  53. static void ReportFinish(Interface&& iface)
  54. {
  55. if (auto* reporter = AZ::Interface<AssetImportRequestReporter>::Get(); reporter)
  56. {
  57. reporter->ReportFinish(static_cast<const AssetImportRequest*>(iface));
  58. }
  59. }
  60. template<class Results, class Function, class Interface, class... InputArgs>
  61. static void CallResult(Results& results, Function&& func, Interface&& iface, InputArgs&&... args)
  62. {
  63. ReportStart(iface);
  64. results = AZStd::invoke(AZStd::forward<Function>(func), AZStd::forward<Interface>(iface), AZStd::forward<InputArgs>(args)...);
  65. ReportFinish(iface);
  66. }
  67. template<class Function, class Interface, class... InputArgs>
  68. static void Call(Function&& func, Interface&& iface, InputArgs&&... args)
  69. {
  70. ReportStart(iface);
  71. AZStd::invoke(AZStd::forward<Function>(func), AZStd::forward<Interface>(iface), AZStd::forward<InputArgs>(args)...);
  72. ReportFinish(iface);
  73. }
  74. };
  75. class SCENE_CORE_API LoadingResultCombiner
  76. {
  77. public:
  78. LoadingResultCombiner();
  79. void operator= (LoadingResult rhs);
  80. ProcessingResult GetManifestResult() const;
  81. ProcessingResult GetAssetResult() const;
  82. private:
  83. ProcessingResult m_manifestResult;
  84. ProcessingResult m_assetResult;
  85. };
  86. class SCENE_CORE_API AssetImportRequest
  87. : public AZ::EBusTraits
  88. {
  89. public:
  90. enum RequestingApplication
  91. {
  92. Generic,
  93. Editor,
  94. AssetProcessor
  95. };
  96. enum ManifestAction
  97. {
  98. Update,
  99. ConstructDefault
  100. };
  101. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple;
  102. using MutexType = AZStd::recursive_mutex;
  103. using EventProcessingPolicy = AssetImportRequestEventProcessingPolicy;
  104. static AZ::Crc32 GetAssetImportRequestComponentTag()
  105. {
  106. return AZ_CRC_CE("AssetImportRequest");
  107. }
  108. virtual ~AssetImportRequest() = 0;
  109. //! Fills the given list with all available file extensions, excluding the extension for the manifest.
  110. virtual void GetSupportedFileExtensions(AZStd::unordered_set<AZStd::string>& extensions);
  111. //! Gets the file extension for the manifest.
  112. virtual void GetManifestExtension(AZStd::string& result);
  113. //! Gets the file extension for the generated manifest.
  114. virtual void GetGeneratedManifestExtension(AZStd::string& result);
  115. //! Gets the label for a scene builder using this HandlerPolicy
  116. virtual void GetPolicyName(AZStd::string& result) const;
  117. //! Before asset loading starts this is called to allow for any required initialization.
  118. virtual ProcessingResult PrepareForAssetLoading(Containers::Scene& scene, RequestingApplication requester);
  119. //! Starts the loading of the asset at the given path in the given scene. Loading optimizations can be applied based on
  120. //! the calling application.
  121. virtual LoadingResult LoadAsset(Containers::Scene& scene, const AZStd::string& path, const Uuid& guid, RequestingApplication requester);
  122. //! FinalizeAssetLoading can be used to do any work to complete loading, such as complete asynchronous loading
  123. //! or adjust the loaded content in the SceneGraph. While manifest changes can be done here as well, it's
  124. //! recommended to wait for the UpdateManifest call.
  125. virtual void FinalizeAssetLoading(Containers::Scene& scene, RequestingApplication requester);
  126. //! After all loading has completed, this call can be used to make adjustments to the manifest. Based on the given
  127. //! action this can mean constructing a new manifest or updating an existing manifest. This call is intended
  128. //! to deal with any default behavior of the manifest.
  129. virtual ProcessingResult UpdateManifest(Containers::Scene& scene, ManifestAction action, RequestingApplication requester);
  130. // Get scene processing project setting: UseCustomNormal
  131. virtual void AreCustomNormalsUsed(bool & value);
  132. /*!
  133. Optional method for reporting source file dependencies that may exist in the scene manifest
  134. Paths is a vector of JSON Path strings, relative to the IRule object
  135. For example, the following path: /scriptFilename
  136. Would match with this manifest:
  137. {
  138. "values": [
  139. {
  140. "$type": "Test",
  141. "scriptFilename": "file.py"
  142. }
  143. ]
  144. }
  145. */
  146. virtual void GetManifestDependencyPaths(AZStd::vector<AZStd::string>& paths);
  147. //! Utility function to load an asset and manifest from file by using the EBus functions above.
  148. //! @param assetFilePath The absolute path to the source file (not the manifest).
  149. //! @param sourceGuid The guid assigned to the source file (not the manifest).
  150. //! @param requester The application making the request to load the file. This can be used to optimize the type and amount of data to load.
  151. //! @param loadingComponentUuid The UUID assigned to the loading component.
  152. //! @param watchFolder is the scan folder that it was found inside
  153. static AZStd::shared_ptr<Containers::Scene> LoadSceneFromVerifiedPath(
  154. const AZStd::string& assetFilePath,
  155. const Uuid& sourceGuid,
  156. RequestingApplication requester,
  157. const Uuid& loadingComponentUuid,
  158. const AZStd::string& watchFolder);
  159. //! Utility function to determine if a given file path points to a scene manifest file (.assetinfo).
  160. //! @param filePath A relative or absolute path to the file to check.
  161. static bool IsManifestExtension(const char* filePath);
  162. //! Utility function to determine if a given file path points to a scene file (for instance .fbx).
  163. //! @param filePath A relative or absolute path to the file to check.
  164. static bool IsSceneFileExtension(const char* filePath);
  165. };
  166. using AssetImportRequestBus = AZ::EBus<AssetImportRequest>;
  167. inline AssetImportRequest::~AssetImportRequest() = default;
  168. //! Queue EBus to resolve events during the scene import phases
  169. class SCENE_CORE_API AssetPostImportRequest
  170. : public AZ::EBusTraits
  171. {
  172. public:
  173. using MutexType = AZStd::recursive_mutex;
  174. using EventQueueMutexType = AZStd::recursive_mutex;
  175. static constexpr bool EnableEventQueue = true;
  176. virtual void CallAfterSceneExport(AZStd::function<void()> callback) = 0;
  177. };
  178. using AssetPostImportRequestBus = AZ::EBus<AssetPostImportRequest>;
  179. } // namespace Events
  180. } // namespace SceneAPI
  181. } // namespace AZ