AssetUtilEBusHelper.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/std/parallel/mutex.h>
  11. #include <AzCore/std/string/string.h>
  12. #include <AzCore/std/containers/vector.h>
  13. #include <AzFramework/Asset/AssetProcessorMessages.h>
  14. #include <AssetBuilderSDK/AssetBuilderBusses.h>
  15. #include <native/assetprocessor.h>
  16. #include <QByteArray>
  17. namespace AssetProcessor
  18. {
  19. struct BuilderParams;
  20. }
  21. namespace AssetUtilities
  22. {
  23. class QuitListener;
  24. class JobLogTraceListener;
  25. }
  26. //This EBUS broadcasts the platform of the connection the AssetProcessor connected or disconnected with
  27. class AssetProcessorPlaformBusTraits
  28. : public AZ::EBusTraits
  29. {
  30. public:
  31. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple; // multi listener
  32. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; //single bus
  33. virtual ~AssetProcessorPlaformBusTraits() {}
  34. //Informs that the AP got a connection for this platform.
  35. virtual void AssetProcessorPlatformConnected(const AZStd::string platform) {}
  36. //Informs that a connection got disconnected for this platform.
  37. virtual void AssetProcessorPlatformDisconnected(const AZStd::string platform) {}
  38. };
  39. using AssetProcessorPlatformBus = AZ::EBus<AssetProcessorPlaformBusTraits>;
  40. class ApplicationServerBusTraits
  41. : public AZ::EBusTraits
  42. {
  43. public:
  44. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  45. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
  46. typedef AZStd::recursive_mutex MutexType;
  47. virtual ~ApplicationServerBusTraits() {};
  48. //! Returns the port the server is set to listen on
  49. virtual int GetServerListeningPort() const = 0;
  50. };
  51. using ApplicationServerBus = AZ::EBus<ApplicationServerBusTraits>;
  52. namespace AzFramework
  53. {
  54. namespace AssetSystem
  55. {
  56. class BaseAssetProcessorMessage;
  57. }
  58. }
  59. namespace AssetProcessor
  60. {
  61. // This bus sends messages to connected clients/proxies identified by their connection ID. The bus
  62. // is addressed by the connection ID as assigned by the ConnectionManager.
  63. class ConnectionBusTraits
  64. : public AZ::EBusTraits
  65. {
  66. public:
  67. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById;
  68. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  69. typedef unsigned int BusIdType;
  70. typedef AZStd::recursive_mutex MutexType;
  71. virtual ~ConnectionBusTraits() {}
  72. // Sends an unsolicited message to the connection
  73. virtual size_t Send(unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message) = 0;
  74. // Sends a raw buffer to the connection
  75. virtual size_t SendRaw(unsigned int type, unsigned int serial, const QByteArray& data) = 0;
  76. // Sends a message to the connection if the platform match
  77. virtual size_t SendPerPlatform(unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message, const QString& platform) = 0;
  78. // Sends a raw buffer to the connection if the platform match
  79. virtual size_t SendRawPerPlatform(unsigned int type, unsigned int serial, const QByteArray& data, const QString& platform) = 0;
  80. using ResponseCallback = AZStd::function<void(AZ::u32, QByteArray)>;
  81. // Sends a message to the connection which expects a response.
  82. virtual unsigned int SendRequest(const AzFramework::AssetSystem::BaseAssetProcessorMessage& message, const ResponseCallback& callback) = 0;
  83. // Sends a response to the connection
  84. virtual size_t SendResponse(unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message) = 0;
  85. // Removes a response handler that is no longer needed
  86. virtual void RemoveResponseHandler(unsigned int serial) = 0;
  87. };
  88. using ConnectionBus = AZ::EBus<ConnectionBusTraits>;
  89. class MessageInfoBusTraits
  90. : public AZ::EBusTraits
  91. {
  92. public:
  93. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple; // multi listener
  94. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; //single bus
  95. typedef AZStd::recursive_mutex MutexType;
  96. virtual ~MessageInfoBusTraits() {}
  97. //Show a message window to the user
  98. virtual void NegotiationFailed() {}
  99. // Notifies listeners of a given Asset failing to process
  100. virtual void OnAssetFailed(const AZStd::string& /*sourceFileName*/) {}
  101. // Notifies listener about a general error
  102. virtual void OnErrorMessage([[maybe_unused]] const char* error) {}
  103. // Notifies listener about a builder registration failure
  104. virtual void OnBuilderRegistrationFailure() {};
  105. };
  106. using MessageInfoBus = AZ::EBus<MessageInfoBusTraits>;
  107. typedef AZStd::vector <AssetBuilderSDK::AssetBuilderDesc> BuilderInfoList;
  108. // This EBUS is used to retrieve asset builder Information
  109. class AssetBuilderInfoBusTraits
  110. : public AZ::EBusTraits
  111. {
  112. public:
  113. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single; // single listener
  114. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; //single bus
  115. using MutexType = AZStd::recursive_mutex;
  116. virtual ~AssetBuilderInfoBusTraits() {}
  117. // For a given asset returns a list of all asset builder that are interested in it.
  118. virtual void GetMatchingBuildersInfo(const AZStd::string& assetPath, AssetProcessor::BuilderInfoList& /*builderInfoList*/) = 0;
  119. virtual void GetAllBuildersInfo(AssetProcessor::BuilderInfoList& /*builderInfoList*/) = 0;
  120. };
  121. using AssetBuilderInfoBus = AZ::EBus<AssetBuilderInfoBusTraits>;
  122. // This EBUS is used to broadcast information about the currently processing job
  123. class ProcessingJobInfoBusTraits
  124. : public AZ::EBusTraits
  125. {
  126. public:
  127. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single; // single listener
  128. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; //single bus
  129. typedef AZStd::recursive_mutex MutexType;
  130. virtual ~ProcessingJobInfoBusTraits() {}
  131. // Will notify other systems a product is about to be updated in the cache. This can mean that
  132. // it will be created, overwritten with new data or deleted. BeginCacheFileUpdate is pared with
  133. // EndCacheFileUpdate.
  134. virtual void BeginCacheFileUpdate(const char* /*productPath*/) {};
  135. // Will notify other systems that a file in the cache has been updated along with status of whether it
  136. // succeeded or failed. EndCacheFileUpdate is paired with BeginCacheFileUpdate.
  137. virtual void EndCacheFileUpdate(const char* /*productPath*/, bool /*queueAgainForDeletion*/) {};
  138. virtual AZ::u32 GetJobFingerprint(const AssetProcessor::JobIndentifier& /*jobIndentifier*/) { return 0; };
  139. };
  140. using ProcessingJobInfoBus = AZ::EBus<ProcessingJobInfoBusTraits>;
  141. // This EBUS is used to issue requests to the AssetCatalog
  142. class AssetRegistryRequests
  143. : public AZ::EBusTraits
  144. {
  145. public:
  146. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single; // single listener
  147. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; //single bus
  148. typedef AZStd::recursive_mutex MutexType;
  149. // This function will either return the registry version of the next registry save or of the current one, if it is in progress
  150. // It will not put another save registry event in the event pump if we are currently in the process of saving the registry
  151. virtual int SaveRegistry() = 0;
  152. // This method checks for cyclic preload dependency for all the currently processed assets.
  153. virtual void ValidatePreLoadDependency() = 0;
  154. };
  155. typedef AZ::EBus<AssetRegistryRequests> AssetRegistryRequestBus;
  156. // This EBUS issues notifications when the catalog begins and finishes saving the asset registry
  157. class AssetRegistryNotifications
  158. : public AZ::EBusTraits
  159. {
  160. public:
  161. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple; // single listener
  162. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; //single bus
  163. typedef AZStd::recursive_mutex MutexType;
  164. // The asset catalog has finished saving the registry
  165. virtual void OnRegistrySaveComplete(int /*assetCatalogVersion*/, bool /*allCatalogsSaved*/) {}
  166. };
  167. using AssetRegistryNotificationBus = AZ::EBus<AssetRegistryNotifications>;
  168. // This EBUS is used to check if there is sufficient disk space
  169. class IDiskSpaceInfo
  170. {
  171. public:
  172. AZ_RTTI(IDiskSpaceInfo, "{0A324878-9871-49DA-8F34-AE3B1D43B7C0}");
  173. // Returns true if there is at least `requiredSpace` bytes plus 256kb free disk space in the project cache folder
  174. // If shutdownIfInsufficient is true, an error will be displayed and the application will be shutdown
  175. virtual bool CheckSufficientDiskSpace(qint64 /*requiredSpace*/, bool /*shutdownIfInsufficient*/) { return true; }
  176. };
  177. //! Defines the modes the Asset Cache Server mode setting for the Asset Processor (AP).
  178. enum class AssetServerMode
  179. {
  180. Inactive, //! This mode means the AP is offline; only processing the assets locally
  181. Server, //! This mode means the AP is writing out the asset products to a remote location
  182. Client //! This mode means the AP is attempting to retrieve asset products from a remote location
  183. };
  184. // This EBUS is used to perform Asset Server related tasks.
  185. class AssetServerBusTraits
  186. : public AZ::EBusTraits
  187. {
  188. public:
  189. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  190. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
  191. typedef AZStd::recursive_mutex MutexType;
  192. static const bool LocklessDispatch = true;
  193. //! This will return true if we were able to verify the server address as being valid, otherwise return false.
  194. virtual bool IsServerAddressValid() = 0;
  195. //! StoreJobResult should store all the files in the temp folder provided by the builderParams to the server
  196. //! As well as any outputProducts which are outside the temp folder intended to be copied directly to the
  197. //! Cache without going through the temp folder
  198. //! It should associate those files with the server key provided by the builderParams because
  199. //! it will be send the same server key to retrieve these files by the client.
  200. //! This will return true if it was able to save all the relevant job data to the server, otherwise return false.
  201. virtual bool StoreJobResult(const AssetProcessor::BuilderParams& builderParams, AZStd::vector<AZStd::string>& sourceFileList) = 0;
  202. //! RetrieveJobResult should retrieve all the files associated with the server key provided in the builderParams
  203. //! and put them in the temporary directory provided by the builderParam.
  204. //! This will return true if it was able to retrieve all the relevant job data from the server, otherwise return false.
  205. virtual bool RetrieveJobResult(const AssetProcessor::BuilderParams& builderParams) = 0;
  206. //! Retrieve the current mode for shared caching
  207. virtual AssetServerMode GetRemoteCachingMode() const = 0;
  208. //! Store the shared caching mode
  209. virtual void SetRemoteCachingMode(AssetServerMode mode) = 0;
  210. //! Retrieve the remote folder location for the shared cache
  211. virtual const AZStd::string& GetServerAddress() const = 0;
  212. //! Store the remote folder location for the shared cache
  213. virtual bool SetServerAddress(const AZStd::string& address) = 0;
  214. };
  215. using AssetServerBus = AZ::EBus<AssetServerBusTraits>;
  216. // This EBUS has notify listeners when Asset Server state(s) changes.
  217. class AssetServerNotifications
  218. : public AZ::EBusTraits
  219. {
  220. public:
  221. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple; // multi listener
  222. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; //single bus
  223. typedef AZStd::recursive_mutex MutexType;
  224. //! This emits when the mode of the Asset Server Cache mode has changed.
  225. virtual void OnRemoteCachingModeChanged([[maybe_unused]] AssetServerMode mode) {}
  226. };
  227. using AssetServerNotificationBus = AZ::EBus<AssetServerNotifications>;
  228. // This EBUS is used to retrieve asset server information
  229. class AssetServerInfoBusTraits
  230. : public AZ::EBusTraits
  231. {
  232. public:
  233. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single; // single listener
  234. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; // single bus
  235. using MutexType = AZStd::recursive_mutex;
  236. virtual ~AssetServerInfoBusTraits() = default;
  237. virtual const AZStd::string& ComputeArchiveFilePath(const AssetProcessor::BuilderParams& builderParams) = 0;
  238. };
  239. using AssetServerInfoBus = AZ::EBus<AssetServerInfoBusTraits>;
  240. } // namespace AssetProcessor