AssetCatalog.h 12 KB

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