PlatformConfiguration.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #ifndef PLATFORMCONFIGURATION_H
  9. #define PLATFORMCONFIGURATION_H
  10. #if !defined(Q_MOC_RUN)
  11. #include <QList>
  12. #include <QString>
  13. #include <QObject>
  14. #include <QHash>
  15. #include <QRegExp>
  16. #include <QPair>
  17. #include <QVector>
  18. #include <QSet>
  19. #include <AzCore/Settings/SettingsRegistry.h>
  20. #include <AzCore/Settings/SettingsRegistryVisitorUtils.h>
  21. #include <AzCore/std/string/string.h>
  22. #include <native/utilities/assetUtils.h>
  23. #include <native/AssetManager/assetScanFolderInfo.h>
  24. #include <AssetBuilderSDK/AssetBuilderSDK.h>
  25. #include <AzToolsFramework/Asset/AssetUtils.h>
  26. #endif
  27. #include "IPathConversion.h"
  28. namespace AZ
  29. {
  30. class SettingsRegistryInterface;
  31. }
  32. namespace AssetProcessor
  33. {
  34. inline constexpr const char* AssetProcessorServerKey{ "/O3DE/AssetProcessor/Settings/Server" };
  35. class PlatformConfiguration;
  36. class ScanFolderInfo;
  37. extern const char AssetConfigPlatformDir[];
  38. extern const char AssetProcessorPlatformConfigFileName[];
  39. //! Information for a given recognizer, on a specific platform
  40. //! essentially a plain data holder, but with helper funcs
  41. enum class AssetInternalSpec
  42. {
  43. Copy,
  44. Skip
  45. };
  46. //! The data about a particular recognizer, including all platform specs.
  47. //! essentially a plain data holder, but with helper funcs
  48. struct AssetRecognizer
  49. {
  50. AZ_CLASS_ALLOCATOR(AssetRecognizer, AZ::SystemAllocator);
  51. AZ_TYPE_INFO(AssetRecognizer, "{29B7A73A-4D7F-4C19-AEAC-6D6750FB1156}");
  52. AssetRecognizer() = default;
  53. AssetRecognizer(
  54. const AZStd::string& name,
  55. bool testLockSource,
  56. int priority,
  57. bool critical,
  58. bool supportsCreateJobs,
  59. AssetBuilderSDK::FilePatternMatcher patternMatcher,
  60. const AZStd::string& version,
  61. const AZ::Data::AssetType& productAssetType,
  62. bool outputProductDependencies,
  63. bool checkServer = false)
  64. : m_name(name)
  65. , m_testLockSource(testLockSource)
  66. , m_priority(priority)
  67. , m_isCritical(critical)
  68. , m_supportsCreateJobs(supportsCreateJobs)
  69. , m_patternMatcher(patternMatcher)
  70. , m_version(version)
  71. , m_productAssetType(productAssetType) // if specified, it allows you to assign a UUID for the type of products directly.
  72. , m_outputProductDependencies(outputProductDependencies)
  73. , m_checkServer(checkServer)
  74. {}
  75. AZStd::string m_name;
  76. AssetBuilderSDK::FilePatternMatcher m_patternMatcher;
  77. AZStd::string m_version = {};
  78. // the QString is the Platform Identifier ("pc")
  79. // the AssetInternalSpec specifies the type of internal job to process
  80. AZStd::unordered_map<AZStd::string, AssetInternalSpec> m_platformSpecs;
  81. // an optional parameter which is a UUID of types to assign to the output asset(s)
  82. // if you don't specify one, then a heuristic will be used
  83. AZ::Uuid m_productAssetType = AZ::Uuid::CreateNull();
  84. int m_priority = 0; // used in order to sort these jobs vs other jobs when no other priority is applied (such as platform connected)
  85. bool m_testLockSource = false;
  86. bool m_isCritical = false;
  87. bool m_checkServer = false;
  88. bool m_supportsCreateJobs = false; // used to indicate a recognizer that can respond to a createJobs request
  89. bool m_outputProductDependencies = false;
  90. };
  91. //! Dictionary of Asset Recognizers based on name
  92. typedef AZStd::unordered_map<AZStd::string, AssetRecognizer> RecognizerContainer;
  93. typedef QList<const AssetRecognizer*> RecognizerPointerContainer;
  94. //! The structure holds information about a particular exclude recognizer
  95. struct ExcludeAssetRecognizer
  96. {
  97. QString m_name;
  98. AssetBuilderSDK::FilePatternMatcher m_patternMatcher;
  99. };
  100. typedef QHash<QString, ExcludeAssetRecognizer> ExcludeRecognizerContainer;
  101. //! Interface to get constant references to asset and exclude recognizers
  102. struct RecognizerConfiguration
  103. {
  104. AZ_RTTI(RecognizerConfiguration, "{2E4DD73E-8D1E-42BC-A3E3-1A671D636DAC}");
  105. virtual const RecognizerContainer& GetAssetRecognizerContainer() const = 0;
  106. virtual const RecognizerContainer& GetAssetCacheRecognizerContainer() const = 0;
  107. virtual const ExcludeRecognizerContainer& GetExcludeAssetRecognizerContainer() const = 0;
  108. virtual bool AddAssetCacheRecognizerContainer(const RecognizerContainer& recognizerContainer) = 0;
  109. };
  110. /** Reads the platform ini configuration file to determine
  111. * platforms for which assets needs to be build
  112. */
  113. class PlatformConfiguration
  114. : public QObject
  115. , public RecognizerConfiguration
  116. , public AZ::Interface<IPathConversion>::Registrar
  117. {
  118. Q_OBJECT
  119. public:
  120. AZ_RTTI(PlatformConfiguration, "{9F0C465D-A3A6-417E-B69C-62CBD22FD950}", RecognizerConfiguration, IPathConversion);
  121. typedef QPair<QRegExp, QString> RCSpec;
  122. typedef QVector<RCSpec> RCSpecList;
  123. public:
  124. explicit PlatformConfiguration(QObject* pParent = nullptr);
  125. virtual ~PlatformConfiguration() = default;
  126. static void Reflect(AZ::ReflectContext* context);
  127. /** Use this function to parse the set of config files and the gem file to set up the platform config.
  128. * This should be about the only function that is required to be called in order to end up with
  129. * a full configuration.
  130. * Note that order of the config files is relevant - later files override settings in
  131. * files that are earlier.
  132. **/
  133. bool InitializeFromConfigFiles(const QString& absoluteSystemRoot, const QString& absoluteAssetRoot, const QString& projectPath, bool addPlatformConfigs = true, bool addGemsConfigs = true);
  134. //! Merge an AssetProcessor*Config.ini path to the Settings Registry
  135. //! The settings are anchored underneath the AssetProcessor::AssetProcessorSettingsKey JSON pointer
  136. static bool MergeConfigFileToSettingsRegistry(AZ::SettingsRegistryInterface& settingsRegistry, const AZ::IO::PathView& filePathView);
  137. const AZStd::vector<AssetBuilderSDK::PlatformInfo>& GetEnabledPlatforms() const;
  138. const AssetBuilderSDK::PlatformInfo* const GetPlatformByIdentifier(const char* identifier) const;
  139. //! Add AssetProcessor config files from platform specific folders
  140. bool AddPlatformConfigFilePaths(AZStd::vector<AZ::IO::Path>& configList);
  141. int MetaDataFileTypesCount() const { return m_metaDataFileTypes.count(); }
  142. // Metadata file types are (meta file extension, original file extension - or blank if its tacked on the end instead of replacing).
  143. // so for example if its
  144. // blah.tif + blah.tif.metadata, then its ("metadata", "")
  145. // but if its blah.tif + blah.metadata (replacing tif, data is lost) then its ("metadata", "tif")
  146. QPair<QString, QString> GetMetaDataFileTypeAt(int pos) const;
  147. // Metadata extensions can also be a real file, to create a dependency on file types if a specific file changes
  148. // so for example, a Metadata file type pair ("Animations/SkeletonList.xml", "i_caf")
  149. // would cause all i_caf files to be re-evaluated when Animations/SkeletonList.xml is modified.
  150. bool IsMetaDataTypeRealFile(QString relativeName) const;
  151. void EnablePlatform(const AssetBuilderSDK::PlatformInfo& platform, bool enable = true);
  152. void EnableCommonPlatform();
  153. void AddIntermediateScanFolder();
  154. //! Return how many scan folders there are
  155. int GetScanFolderCount() const;
  156. //! Return the gems info list
  157. AZStd::vector<AzFramework::GemInfo> GetGemsInformation() const;
  158. //! Retrieve the scan folder at a given index.
  159. AssetProcessor::ScanFolderInfo& GetScanFolderAt(int index);
  160. //! Retrieve the scan folder at a given index.
  161. const AssetProcessor::ScanFolderInfo& GetScanFolderAt(int index) const;
  162. //! Retrieve the scan folder found by a boolean predicate function, when the predicate returns true, the current scan folder info is returned.
  163. const AssetProcessor::ScanFolderInfo* FindScanFolder(AZStd::function<bool(const AssetProcessor::ScanFolderInfo&)> predicate) const;
  164. const AssetProcessor::ScanFolderInfo* GetScanFolderById(AZ::s64 id) const override;
  165. const AZ::s64 GetIntermediateAssetScanFolderId() const;
  166. //! Manually add a scan folder. Also used for testing.
  167. void AddScanFolder(const AssetProcessor::ScanFolderInfo& source, bool isUnitTesting = false);
  168. //! Manually add a recognizer. Used for testing.
  169. void AddRecognizer(const AssetRecognizer& source);
  170. //! Manually remove a recognizer. Used for testing.
  171. void RemoveRecognizer(QString name);
  172. //! Manually add an exclude recognizer. Used for testing.
  173. void AddExcludeRecognizer(const ExcludeAssetRecognizer& recogniser);
  174. //! Manually remove an exclude recognizer. Used for testing.
  175. void RemoveExcludeRecognizer(QString name);
  176. //! Manually add a metadata type. Used for testing.
  177. //! The originalextension, if specified, means this metafile type REPLACES the given extension
  178. //! If not specified (blank) it means that the metafile extension is added onto the end instead and does
  179. //! not remove the original file extension
  180. void AddMetaDataType(const QString& type, const QString& originalExtension);
  181. // ------------------- utility functions --------------------
  182. //! Checks to see whether the input file is an excluded file, assumes input is absolute path.
  183. bool IsFileExcluded(QString fileName) const;
  184. //! If you already have a relative path, this is a cheaper function to call:
  185. bool IsFileExcludedRelPath(QString relPath) const;
  186. //! Given a file name, return a container that contains all matching recognizers
  187. //!
  188. //! Returns false if there were no matches, otherwise returns true
  189. bool GetMatchingRecognizers(QString fileName, RecognizerPointerContainer& output) const;
  190. //! given a fileName (as a relative and which scan folder it was found in)
  191. //! Return either an empty string, or the canonical path to a file which overrides it
  192. //! because of folder priority.
  193. //! Note that scanFolderName is only used to exit quickly
  194. //! If its found in any scan folder before it arrives at scanFolderName it will be considered a hit
  195. QString GetOverridingFile(QString relativeName, QString scanFolderName) const;
  196. //! given a relative name, loop over folders and resolve it to a full path with the first existing match.
  197. QString FindFirstMatchingFile(QString relativeName, bool skipIntermediateScanFolder = false, const AssetProcessor::ScanFolderInfo** scanFolderInfo = nullptr) const;
  198. //! given a relative name with wildcard characters (* allowed) find a set of matching files or optionally folders
  199. QStringList FindWildcardMatches(const QString& sourceFolder, QString relativeName, bool includeFolders = false,
  200. bool recursiveSearch = true) const;
  201. //! given a relative name with wildcard characters (* allowed) find a set of matching files or optionally folders
  202. QStringList FindWildcardMatches(
  203. const QString& sourceFolder,
  204. QString relativeName,
  205. const AZStd::unordered_set<AZStd::string>& excludedFolders,
  206. bool includeFolders = false,
  207. bool recursiveSearch = true) const;
  208. //! given a fileName (as a full path), return the database source name which includes the output prefix.
  209. //!
  210. //! for example
  211. //! c:/dev/mygame/textures/texture1.tga
  212. //! ----> [textures/texture1.tga] found under [c:/dev/mygame]
  213. //! c:/dev/engine/models/box01.mdl
  214. //! ----> [models/box01.mdl] found under[c:/dev/engine]
  215. //! note that this does return a database source path by default
  216. bool ConvertToRelativePath(QString fullFileName, QString& databaseSourceName, QString& scanFolderName) const override;
  217. static bool ConvertToRelativePath(const QString& fullFileName, const ScanFolderInfo* scanFolderInfo, QString& databaseSourceName);
  218. //! given a full file name (assumed already fed through the normalization funciton), return the first matching scan folder
  219. const AssetProcessor::ScanFolderInfo* GetScanFolderForFile(const QString& fullFileName) const override;
  220. //! Given a scan folder path, get its complete info
  221. const AssetProcessor::ScanFolderInfo* GetScanFolderByPath(const QString& scanFolderPath) const;
  222. const RecognizerContainer& GetAssetRecognizerContainer() const override;
  223. const RecognizerContainer& GetAssetCacheRecognizerContainer() const override;
  224. const ExcludeRecognizerContainer& GetExcludeAssetRecognizerContainer() const override;
  225. bool AddAssetCacheRecognizerContainer(const RecognizerContainer& recognizerContainer) override;
  226. static bool ConvertToJson(const RecognizerContainer& recognizerContainer, AZStd::string& jsonText);
  227. static bool ConvertFromJson(const AZStd::string& jsonText, RecognizerContainer& recognizerContainer);
  228. /** returns true if the config is valid.
  229. * configs are considered invalid if critical information is missing.
  230. * for example, if no recognizers are given, or no platforms are enabled.
  231. * They can also be considered invalid if a critical parse error occurred during load.
  232. */
  233. bool IsValid() const;
  234. /** If IsValid is false, this will contain the full error string to show to the user.
  235. * Note that IsValid will automatically write this error string to stderror as part of checking
  236. * So this function is there for those wishing to use a GUI.
  237. */
  238. const AZStd::string& GetError() const;
  239. void PopulatePlatformsForScanFolder(AZStd::vector<AssetBuilderSDK::PlatformInfo>& platformsList, QStringList includeTagsList = QStringList(), QStringList excludeTagsList = QStringList());
  240. // uses const + mutability since its a cache.
  241. void CacheIntermediateAssetsScanFolderId() const;
  242. AZStd::optional<AZ::s64> GetIntermediateAssetsScanFolderId() const;
  243. void ReadMetaDataFromSettingsRegistry();
  244. protected:
  245. // call this first, to populate the list of platform informations
  246. void ReadPlatformInfosFromSettingsRegistry();
  247. // call this next, in order to find out what platforms are enabled
  248. void PopulateEnabledPlatforms();
  249. // finally, call this, in order to delete the platforminfos for non-enabled platforms
  250. void FinalizeEnabledPlatforms();
  251. // iterate over all the gems and add their folders to the "scan folders" list as appropriate.
  252. void AddGemScanFolders(const AZStd::vector<AzFramework::GemInfo>& gemInfoList);
  253. void ReadEnabledPlatformsFromSettingsRegistry();
  254. bool ReadRecognizersFromSettingsRegistry(const QString& assetRoot, bool skipScanFolders = false, QStringList scanFolderPatterns = QStringList() );
  255. int GetProjectScanFolderOrder() const;
  256. private:
  257. AZStd::vector<AssetBuilderSDK::PlatformInfo> m_enabledPlatforms;
  258. RecognizerContainer m_assetRecognizers;
  259. RecognizerContainer m_assetCacheServerRecognizers;
  260. ExcludeRecognizerContainer m_excludeAssetRecognizers;
  261. AZStd::vector<AssetProcessor::ScanFolderInfo> m_scanFolders;
  262. QList<QPair<QString, QString> > m_metaDataFileTypes;
  263. QSet<QString> m_metaDataRealFiles;
  264. AZStd::vector<AzFramework::GemInfo> m_gemInfoList;
  265. mutable AZ::s64 m_intermediateAssetScanFolderId = -1; // Cached ID for intermediate scanfolder, for quick lookups
  266. // used only during file read, keeps the total running list of all the enabled platforms from all config files and command lines
  267. AZStd::vector<AZStd::string> m_tempEnabledPlatforms;
  268. ///! if non-empty, fatalError contains the error that occurred during read.
  269. ///! it will be printed out to the log when
  270. mutable AZStd::string m_fatalError;
  271. };
  272. } // end namespace AssetProcessor
  273. #endif // PLATFORMCONFIGURATION_H