UuidManager.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/IO/Path/Path.h>
  10. #include <AzCore/Interface/Interface.h>
  11. #include <AzCore/std/containers/unordered_set.h>
  12. #include <AzCore/std/string/string.h>
  13. #include <AzToolsFramework/Metadata/UuidUtils.h>
  14. #include <native/AssetManager/SourceAssetReference.h>
  15. #include <AzToolsFramework/Metadata/MetaUuidEntry.h>
  16. namespace AzToolsFramework
  17. {
  18. struct IMetadataRequests;
  19. }
  20. namespace AssetProcessor
  21. {
  22. struct IUuidRequests
  23. {
  24. AZ_RTTI(IUuidRequests, "{4EA7E0F6-CB4E-4F9C-ADBC-807676D51772}");
  25. virtual ~IUuidRequests() = default;
  26. //! Gets the canonical UUID for a given source asset. A new metadata file may be created with a randomly generated UUID if
  27. //! generation is enabled for the file type. Otherwise the UUID returned will follow the legacy system based on the file path.
  28. virtual AZ::Outcome<AZ::Uuid, AZStd::string> GetUuid(const SourceAssetReference& sourceAsset) = 0;
  29. //! Gets the set of legacy UUIDs for the given source asset. These are all the UUIDs that may have been used to reference this asset based on previous generation methods.
  30. virtual AZ::Outcome<AZStd::unordered_set<AZ::Uuid>, AZStd::string> GetLegacyUuids(const SourceAssetReference& sourceAsset) = 0;
  31. //! Returns the full set of UUID entry details for the given asset.
  32. virtual AZ::Outcome<AzToolsFramework::MetaUuidEntry, AZStd::string> GetUuidDetails(const SourceAssetReference& sourceAsset) = 0;
  33. //! Returns the file(s) matching the provided UUID. If the UUID provided is a legacy UUID there may be multiple matches.
  34. //! Note this API relies on the internal cache and expects all files to have had Get(Legacy)Uuid(s) called at least once already.
  35. //! This is expected to happened already as part of normal AP operations.
  36. virtual AZStd::vector<AZ::IO::Path> FindFilesByUuid(AZ::Uuid uuid) = 0;
  37. //! Returns the highest priority file matching the provided UUID.
  38. //! In the case of a legacy UUID provided which matches multiple files, the oldest file in the highest priority scanfolder will be returned.
  39. //! Note this API relies on the internal cache and expects all files to have had Get(Legacy)Uuid(s) called at least once already.
  40. //! This is expected to happened already as part of normal AP operations.
  41. virtual AZStd::optional<AZ::IO::Path> FindHighestPriorityFileByUuid(AZ::Uuid uuid) = 0;
  42. //! Upgrades a potentially legacy UUID to the canonical UUID associated with the asset.
  43. //! Note this API relies on the internal cache and expects all files to have had Get(Legacy)Uuid(s) called at least once already.
  44. //! This is expected to happened already as part of normal AP operations.
  45. virtual AZStd::optional<AZ::Uuid> GetCanonicalUuid(AZ::Uuid legacyUuid) = 0;
  46. //! Notifies the manager a metadata file has changed so the cache can be cleared.
  47. //! @param file Absolute path to the metadata file that changed.
  48. virtual void FileChanged(AZ::IO::PathView file) = 0;
  49. //! Notifies the manager a metadata file has been removed so the cache can be cleared.
  50. //! @param file Absolute path to the metadata file that was removed.
  51. virtual void FileRemoved(AZ::IO::PathView file) = 0;
  52. //! Sets the file types (based on file extension) which the manager will generate random UUIDs and store in a metadata file. Types
  53. //! which are not enabled will use legacy path-based UUIDs.
  54. //! @param types Set of file extensions to enable generation for.
  55. virtual void EnableGenerationForTypes(AZStd::unordered_set<AZStd::string> types) = 0;
  56. //! Returns true if UUID generation is enabled for the type (based on file extension), false otherwise.
  57. virtual bool IsGenerationEnabledForFile(AZ::IO::PathView file) = 0;
  58. //! Returns the list of file types that UUID generation is enabled for. This is useful over checking the registry setting
  59. //! directly, because other types may be enabled via code.
  60. virtual AZStd::unordered_set<AZStd::string> GetEnabledTypes() = 0;
  61. };
  62. //! Serialized settings type for storing the user preferences for the UUID manager
  63. struct UuidSettings
  64. {
  65. AZ_TYPE_INFO(UuidSettings, "{0E4FD61F-1BB3-4FFF-90DA-E583D75BF948}");
  66. static void Reflect(AZ::ReflectContext* context);
  67. AZ::u32 m_metaCreationDelayMs{};
  68. AZStd::unordered_set<AZStd::string> m_enabledTypes;
  69. };
  70. //! Handles all UUID lookup (and generation) requests for the AssetProcessor
  71. class UuidManager : public AZ::Interface<IUuidRequests>::Registrar
  72. {
  73. public:
  74. AZ_RTTI(UuidManager, "{49FA0129-7272-4256-A5C6-D789C156E6BA}", IUuidRequests);
  75. static void Reflect(AZ::ReflectContext* context);
  76. AZ::Outcome<AZ::Uuid, AZStd::string> GetUuid(const SourceAssetReference& sourceAsset) override;
  77. AZ::Outcome<AZStd::unordered_set<AZ::Uuid>, AZStd::string> GetLegacyUuids(const SourceAssetReference& sourceAsset) override;
  78. AZ::Outcome<AzToolsFramework::MetaUuidEntry, AZStd::string> GetUuidDetails(const SourceAssetReference& sourceAsset) override;
  79. AZStd::vector<AZ::IO::Path> FindFilesByUuid(AZ::Uuid uuid) override;
  80. AZStd::optional<AZ::IO::Path> FindHighestPriorityFileByUuid(AZ::Uuid uuid) override;
  81. AZStd::optional<AZ::Uuid> GetCanonicalUuid(AZ::Uuid legacyUuid) override;
  82. void FileChanged(AZ::IO::PathView file) override;
  83. void FileRemoved(AZ::IO::PathView file) override;
  84. void EnableGenerationForTypes(AZStd::unordered_set<AZStd::string> types) override;
  85. bool IsGenerationEnabledForFile(AZ::IO::PathView file) override;
  86. AZStd::unordered_set<AZStd::string> GetEnabledTypes() override;
  87. private:
  88. AZ::IO::Path GetCanonicalPath(AZ::IO::PathView file);
  89. AZ::Outcome<AzToolsFramework::MetaUuidEntry, AZStd::string> GetOrCreateUuidEntry(const SourceAssetReference& sourceAsset);
  90. AzToolsFramework::IMetadataRequests* GetMetadataManager();
  91. AzToolsFramework::MetaUuidEntry CreateUuidEntry(const SourceAssetReference& sourceAsset, bool enabledType);
  92. AZ::Outcome<void, AZStd::string> CacheUuidEntry(AZ::IO::PathView normalizedPath, AzToolsFramework::MetaUuidEntry entry, bool enabledType);
  93. AZ::Uuid CreateUuid();
  94. AZStd::unordered_set<AZ::Uuid> CreateLegacyUuids(const AZStd::string& file);
  95. void InvalidateCacheEntry(AZ::IO::FixedMaxPath file);
  96. AZStd::recursive_mutex m_uuidMutex;
  97. // Cache of uuids. AbsPath -> UUIDEntry
  98. AZStd::unordered_map<AZ::IO::Path, AzToolsFramework::MetaUuidEntry> m_uuids;
  99. // Types which should use randomly generated UUIDs
  100. AZStd::unordered_set<AZStd::string> m_enabledTypes;
  101. // Map of already existing UUIDs -> File path
  102. AZStd::unordered_map<AZ::Uuid, AZ::IO::Path> m_existingUuids;
  103. // Map of legacy UUID -> File path. There may be multiple files with the same legacy UUID
  104. AZStd::unordered_multimap<AZ::Uuid, AZ::IO::Path> m_existingLegacyUuids;
  105. AzToolsFramework::IMetadataRequests* m_metadataManager{};
  106. };
  107. } // namespace AssetProcessor