AssetCatalog.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #if !defined(Q_MOC_RUN)
  10. #include <AzCore/std/smart_ptr/unique_ptr.h>
  11. #include <AzCore/std/smart_ptr/shared_ptr.h>
  12. #include <QObject>
  13. #include <QString>
  14. #include <QTimer>
  15. #include <QStringList>
  16. #include <QHash>
  17. #include <QDir>
  18. #include "native/AssetDatabase/AssetDatabase.h"
  19. #include "native/assetprocessor.h"
  20. #include "native/utilities/AssetUtilEBusHelper.h"
  21. #include "native/utilities/PlatformConfiguration.h"
  22. #include <AzFramework/Asset/AssetRegistry.h>
  23. #include <QMutex>
  24. #include <QMultiMap>
  25. #include <AzCore/IO/SystemFile.h>
  26. #include <AzToolsFramework/ToolsComponents/ToolsAssetCatalogBus.h>
  27. #endif
  28. #include "AssetRequestHandler.h"
  29. namespace AzFramework
  30. {
  31. class AssetRegistry;
  32. namespace AssetSystem
  33. {
  34. class AssetNotificationMessage;
  35. }
  36. }
  37. namespace AssetProcessor
  38. {
  39. class AssetDatabaseConnection;
  40. class AssetCatalog
  41. : public QObject
  42. , private AssetRegistryRequestBus::Handler
  43. , private AzToolsFramework::AssetSystemRequestBus::Handler
  44. , private AzToolsFramework::ToolsAssetSystemBus::Handler
  45. , private AZ::Data::AssetCatalogRequestBus::Handler
  46. {
  47. using NetworkRequestID = AssetProcessor::NetworkRequestID;
  48. using BaseAssetProcessorMessage = AzFramework::AssetSystem::BaseAssetProcessorMessage;
  49. Q_OBJECT;
  50. public:
  51. AssetCatalog(QObject* parent, AssetProcessor::PlatformConfiguration* platformConfiguration);
  52. virtual ~AssetCatalog();
  53. Q_SIGNALS:
  54. // outgoing message to the network
  55. void SendAssetMessage(AzFramework::AssetSystem::AssetNotificationMessage message);
  56. void AsyncAssetCatalogStatusResponse(AssetCatalogStatus status);
  57. public Q_SLOTS:
  58. // incoming message from the AP
  59. void OnAssetMessage(AzFramework::AssetSystem::AssetNotificationMessage message);
  60. void OnDependencyResolved(const AZ::Data::AssetId& assetId, const AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntry& entry);
  61. void SaveRegistry_Impl();
  62. virtual AzFramework::AssetSystem::GetUnresolvedDependencyCountsResponse HandleGetUnresolvedDependencyCountsRequest(MessageData<AzFramework::AssetSystem::GetUnresolvedDependencyCountsRequest> messageData);
  63. virtual void HandleSaveAssetCatalogRequest(MessageData<AzFramework::AssetSystem::SaveAssetCatalogRequest> messageData);
  64. void BuildRegistry();
  65. void OnSourceQueued(AZ::Uuid sourceUuid, AZ::Uuid legacyUuid, QString rootPath, QString relativeFilePath);
  66. void OnSourceFinished(AZ::Uuid sourceUuid, AZ::Uuid legacyUuid);
  67. void AsyncAssetCatalogStatusRequest();
  68. protected:
  69. //////////////////////////////////////////////////////////////////////////
  70. // AssetRegistryRequestBus::Handler overrides
  71. int SaveRegistry() override;
  72. void ValidatePreLoadDependency() override;
  73. //////////////////////////////////////////////////////////////////////////
  74. void RegistrySaveComplete(int assetCatalogVersion, bool allCatalogsSaved);
  75. //////////////////////////////////////////////////////////////////////////
  76. // AzToolsFramework::AssetSystem::AssetSystemRequestBus::Handler overrides
  77. bool GetRelativeProductPathFromFullSourceOrProductPath(const AZStd::string& fullPath, AZStd::string& relativeProductPath) override;
  78. //! Given a partial or full source file path, respond with its relative path and the watch folder it is relative to.
  79. //! The input source path does not need to exist, so this can be used for new files that haven't been saved yet.
  80. bool GenerateRelativeSourcePath(
  81. const AZStd::string& sourcePath, AZStd::string& relativePath, AZStd::string& watchFolder) override;
  82. bool GetFullSourcePathFromRelativeProductPath(const AZStd::string& relPath, AZStd::string& fullSourcePath) override;
  83. bool GetAssetInfoById(const AZ::Data::AssetId& assetId, const AZ::Data::AssetType& assetType, const AZStd::string& platformName, AZ::Data::AssetInfo& assetInfo, AZStd::string& rootFilePath) override;
  84. bool GetSourceInfoBySourcePath(const char* sourcePath, AZ::Data::AssetInfo& assetInfo, AZStd::string& watchFolder) override;
  85. bool GetSourceInfoBySourceUUID(const AZ::Uuid& sourceUuid, AZ::Data::AssetInfo& assetInfo, AZStd::string& watchFolder) override;
  86. bool GetScanFolders(AZStd::vector<AZStd::string>& scanFolders) override;
  87. bool GetAssetSafeFolders(AZStd::vector<AZStd::string>& assetSafeFolders) override;
  88. bool IsAssetPlatformEnabled(const char* platform) override;
  89. int GetPendingAssetsForPlatform(const char* platform) override;
  90. bool GetAssetsProducedBySourceUUID(const AZ::Uuid& sourceUuid, AZStd::vector<AZ::Data::AssetInfo>& productsAssetInfo) override;
  91. ////////////////////////////////////////////////////////////////////////////////
  92. ///////////////////////////////////////////////////////////////////////////
  93. // AssetCatalogRequestBus overrides
  94. AZStd::string GetAssetPathById(const AZ::Data::AssetId& id) override;
  95. AZ::Data::AssetId GetAssetIdByPath(const char* path, const AZ::Data::AssetType& typeToRegister, bool autoRegisterIfNotFound) override;
  96. AZ::Data::AssetInfo GetAssetInfoById(const AZ::Data::AssetId& id) override;
  97. AZ::Outcome<AZStd::vector<AZ::Data::ProductDependency>, AZStd::string> GetDirectProductDependencies(const AZ::Data::AssetId& id) override;
  98. AZ::Outcome<AZStd::vector<AZ::Data::ProductDependency>, AZStd::string> GetAllProductDependencies(const AZ::Data::AssetId& id) override;
  99. AZ::Outcome<AZStd::vector<AZ::Data::ProductDependency>, AZStd::string> GetLoadBehaviorProductDependencies(
  100. const AZ::Data::AssetId& id, AZStd::unordered_set<AZ::Data::AssetId>& noloadSet,
  101. AZ::Data::PreloadAssetListType& preloadAssetList) override;
  102. ////////////////////////////////////////////////////////////////////////////////
  103. //////////////////////////////////////////////////////////////////////////
  104. // AzToolsFramework::ToolsAssetSystemBus::Handler
  105. void RegisterSourceAssetType(const AZ::Data::AssetType& assetType, const char* assetFileFilter) override;
  106. void UnregisterSourceAssetType(const AZ::Data::AssetType& assetType) override;
  107. //////////////////////////////////////////////////////////////////////////
  108. //! given some absolute path, please respond with its relative product path. For now, this will be a
  109. //! string like 'textures/blah.tif' (we don't care about extensions), but eventually, this will
  110. //! be an actual asset UUID.
  111. void ProcessGetRelativeProductPathFromFullSourceOrProductPathRequest(const AZStd::string& fullPath, AZStd::string& relativeProductPath);
  112. //! This function helps in determining the full product path of an relative product path.
  113. //! In the future we will be sending an asset UUID to this function to request for full path.
  114. void ProcessGetFullSourcePathFromRelativeProductPathRequest(const AZStd::string& relPath, AZStd::string& fullSourcePath);
  115. //! Gets the source file info for an Asset by checking the DB first and the APM queue second
  116. bool GetSourceFileInfoFromAssetId(const AZ::Data::AssetId &assetId, AZStd::string& watchFolder, AZStd::string& relativePath);
  117. //! Gets the product AssetInfo based on a platform and assetId. If you specify a null or empty platform the current or first available will be used.
  118. AZ::Data::AssetInfo GetProductAssetInfo(const char* platformName, const AZ::Data::AssetId& id);
  119. //! GetAssetInfo that tries to figure out if the asset is a product or source so it can return info about the product or source respectively
  120. bool GetAssetInfoByIdOnly(const AZ::Data::AssetId& id, const AZStd::string& platformName, AZ::Data::AssetInfo& assetInfo, AZStd::string& rootFilePath);
  121. //! Checks in the currently-in-queue assets list for info on an asset (by source Id)
  122. bool GetQueuedAssetInfoById(const AZ::Uuid& guid, AZStd::string& watchFolder, AZStd::string& relativePath);
  123. //! Checks in the currently-in-queue assets list for info on an asset (by source name)
  124. bool GetQueuedAssetInfoByRelativeSourceName(const char* sourceName, AZ::Data::AssetInfo& assetInfo, AZStd::string& watchFolder);
  125. //! Gets the source info for a source that is not in the DB or APM queue
  126. bool GetUncachedSourceInfoFromDatabaseNameAndWatchFolder(const char* sourceDatabasePath, const char* watchFolder, AZ::Data::AssetInfo& assetInfo);
  127. bool ConnectToDatabase();
  128. bool CheckValidatedAssets(AZ::Data::AssetId assetId, const QString& platform);
  129. //! For lookups that don't provide a specific platform, provide a default platform to use.
  130. QString GetDefaultAssetPlatform();
  131. AZ::Outcome<AZStd::vector<AZ::Data::ProductDependency>, AZStd::string> GetAllProductDependenciesFilter(
  132. const AZ::Data::AssetId& id,
  133. const AZStd::unordered_set<AZ::Data::AssetId>& exclusionList,
  134. const AZStd::vector<AZStd::string>& wildcardPatternExclusionList) override;
  135. bool DoesAssetIdMatchWildcardPattern(const AZ::Data::AssetId& assetId, const AZStd::string& wildcardPattern) override;
  136. void AddAssetDependencies(
  137. const AZ::Data::AssetId& searchAssetId,
  138. AZStd::unordered_set<AZ::Data::AssetId>& assetSet,
  139. AZStd::vector<AZ::Data::ProductDependency>& dependencyList,
  140. const AZStd::unordered_set<AZ::Data::AssetId>& exclusionList,
  141. const AZStd::vector<AZStd::string>& wildcardPatternExclusionList,
  142. AZ::Data::PreloadAssetListType& preloadAssetList);
  143. //! List of AssetTypes that should return info for the source instead of the product
  144. AZStd::unordered_set<AZ::Data::AssetType> m_sourceAssetTypes;
  145. AZStd::unordered_map<AZStd::string, AZ::Data::AssetType> m_sourceAssetTypeFilters;
  146. AZStd::mutex m_sourceAssetTypesMutex;
  147. //! Used to protect access to the database connection, only one thread can use it at a time
  148. AZStd::mutex m_databaseMutex;
  149. struct SourceInfo
  150. {
  151. QString m_watchFolder;
  152. QString m_sourceName;
  153. };
  154. AZStd::mutex m_sourceUUIDToSourceNameMapMutex;
  155. using SourceUUIDToSourceNameMap = AZStd::unordered_map<AZ::Uuid, SourceInfo>;
  156. using SourceNameToSourceUuidMap = AZStd::unordered_map<AZStd::string, AZ::Uuid>;
  157. SourceUUIDToSourceNameMap m_sourceUUIDToSourceNameMap; // map of uuids to source file names for assets that are currently in the processing queue
  158. SourceNameToSourceUuidMap m_sourceNameToSourceUUIDMap;
  159. QMutex m_registriesMutex;
  160. QHash<QString, AzFramework::AssetRegistry> m_registries; // per platform.
  161. AssetProcessor::PlatformConfiguration* m_platformConfig;
  162. QStringList m_platforms;
  163. AZStd::unique_ptr<AssetDatabaseConnection> m_db;
  164. QDir m_cacheRoot;
  165. bool m_registryBuiltOnce;
  166. bool m_catalogIsDirty = true;
  167. bool m_currentlySavingCatalog = false;
  168. bool m_currentlyValidatingPreloadDependency = false;
  169. int m_currentRegistrySaveVersion = 0;
  170. QMutex m_savingRegistryMutex;
  171. QMultiMap<int, AssetProcessor::NetworkRequestID> m_queuedSaveCatalogRequest;
  172. AZStd::vector<AZStd::pair<AZ::Data::AssetId, QString>> m_preloadAssetList;
  173. AZStd::unordered_multimap<AZ::Data::AssetId, QString> m_cachedNoPreloadDependenyAssetList;
  174. AZStd::vector<char> m_saveBuffer; // so that we don't realloc all the time
  175. QDir m_cacheRootDir;
  176. };
  177. }