utils.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/std/string/string.h>
  10. #include <AzCore/Debug/TraceMessageBus.h>
  11. #include <AzCore/IO/Path/Path.h>
  12. #include <AzCore/Outcome/Outcome.h>
  13. #include <AzFramework/Platform/PlatformDefaults.h>
  14. #include <AzToolsFramework/Asset/AssetBundler.h>
  15. #include <AzToolsFramework/Asset/AssetUtils.h>
  16. #include <QDir>
  17. #include <QStringList>
  18. #include <QJsonObject>
  19. namespace AssetBundler
  20. {
  21. enum CommandType
  22. {
  23. Invalid,
  24. Seeds,
  25. AssetLists,
  26. ComparisonRules,
  27. Compare,
  28. BundleSettings,
  29. Bundles,
  30. BundleSeed
  31. };
  32. ////////////////////////////////////////////////////////////////////////////////////////////
  33. // General
  34. extern const char* AppWindowName;
  35. extern const char* AppWindowNameVerbose;
  36. extern const char* HelpFlag;
  37. extern const char* HelpFlagAlias;
  38. extern const char* VerboseFlag;
  39. extern const char* SaveFlag;
  40. extern const char* PlatformArg;
  41. extern const char* PrintFlag;
  42. extern const char* AssetCatalogFileArg;
  43. extern const char* AllowOverwritesFlag;
  44. extern const char* IgnoreFileCaseFlag;
  45. extern const char* ProjectArg;
  46. ////////////////////////////////////////////////////////////////////////////////////////////
  47. ////////////////////////////////////////////////////////////////////////////////////////////
  48. // Seeds
  49. extern const char* SeedsCommand;
  50. extern const char* SeedListFileArg;
  51. extern const char* AddSeedArg;
  52. extern const char* RemoveSeedArg;
  53. extern const char* AddPlatformToAllSeedsFlag;
  54. extern const char* RemovePlatformFromAllSeedsFlag;
  55. extern const char* UpdateSeedPathArg;
  56. extern const char* RemoveSeedPathArg;
  57. ////////////////////////////////////////////////////////////////////////////////////////////
  58. ////////////////////////////////////////////////////////////////////////////////////////////
  59. // Asset Lists
  60. extern const char* AssetListsCommand;
  61. extern const char* AssetListFileArg;
  62. extern const char* AddDefaultSeedListFilesFlag;
  63. extern const char* DryRunFlag;
  64. extern const char* GenerateDebugFileFlag;
  65. extern const char* SkipArg;
  66. ////////////////////////////////////////////////////////////////////////////////////////////
  67. ////////////////////////////////////////////////////////////////////////////////////////////
  68. // Comparison Rules
  69. extern const char* ComparisonRulesCommand;
  70. extern const char* ComparisonRulesFileArg;
  71. extern const char* ComparisonTypeArg;
  72. extern const char* ComparisonFilePatternArg;
  73. extern const char* ComparisonFilePatternTypeArg;
  74. extern const char* ComparisonTokenNameArg;
  75. extern const char* ComparisonFirstInputArg;
  76. extern const char* ComparisonSecondInputArg;
  77. extern const char* AddComparisonStepArg;
  78. extern const char* RemoveComparisonStepArg;
  79. extern const char* MoveComparisonStepArg;
  80. extern const char* EditComparisonStepArg;
  81. ////////////////////////////////////////////////////////////////////////////////////////////
  82. ////////////////////////////////////////////////////////////////////////////////////////////
  83. // Compare
  84. extern const char* CompareCommand;
  85. extern const char* CompareFirstFileArg;
  86. extern const char* CompareSecondFileArg;
  87. extern const char* CompareOutputFileArg;
  88. extern const char* ComparePrintArg;
  89. extern const char* IntersectionCountArg;
  90. ////////////////////////////////////////////////////////////////////////////////////////////
  91. ////////////////////////////////////////////////////////////////////////////////////////////
  92. // Bundle Settings
  93. extern const char* BundleSettingsCommand;
  94. extern const char* BundleSettingsFileArg;
  95. extern const char* OutputBundlePathArg;
  96. extern const char* BundleVersionArg;
  97. extern const char* MaxBundleSizeArg;
  98. ////////////////////////////////////////////////////////////////////////////////////////////
  99. ////////////////////////////////////////////////////////////////////////////////////////////
  100. // Bundles
  101. extern const char* BundlesCommand;
  102. ////////////////////////////////////////////////////////////////////////////////////////////
  103. ////////////////////////////////////////////////////////////////////////////////////////////
  104. // Bundle Seed
  105. extern const char* BundleSeedCommand;
  106. ////////////////////////////////////////////////////////////////////////////////////////////
  107. extern const char* AssetCatalogFilename;
  108. static const size_t MaxErrorMessageLength = 4096;
  109. // The Warning Absorber is used to absorb warnings
  110. // One case that this is being used is during loading of the asset catalog.
  111. // During loading the asset catalog tries to communicate to the AP which is not required for this application.
  112. class WarningAbsorber
  113. : public AZ::Debug::TraceMessageBus::Handler
  114. {
  115. public:
  116. WarningAbsorber();
  117. ~WarningAbsorber();
  118. bool OnWarning(const char* window, const char* message) override;
  119. bool OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) override;
  120. };
  121. /**
  122. * Determines the name of the currently enabled game project
  123. * @return Current Project name on success, error message on failure
  124. */
  125. AZ::Outcome<AZStd::string, AZStd::string> GetCurrentProjectName();
  126. /**
  127. * Retrieve the project path from the Settings Registry
  128. * @return Absolute path of the Project Folder on success, error message on failure
  129. */
  130. AZ::Outcome<AZ::IO::Path, AZStd::string> GetProjectFolderPath();
  131. /**
  132. * Retrieve the project path from the Settings Registry
  133. * @return Absolute path of the project-specific cache folder on success, error message on failure
  134. */
  135. AZ::Outcome<AZ::IO::Path, AZStd::string> GetProjectCacheFolderPath();
  136. /**
  137. * Computes the absolute path to the Asset Catalog file for a specified project and platform.
  138. * With platform set as "pc" and project as "ProjectName", the path will resemble: C:/ProjectPath/Cache/pc/assetcatalog.xml
  139. *
  140. * @param pathToCacheFolder The absolute path to the Cache folder. ex: C:/ProjectPath/Cache
  141. * @param platformIdentifier The platform identifier of the desired Asset Catalog. Valid inputs can be found by reading the folder names
  142. * found inside ProjectPath/Cache
  143. * @param projectName The name of the project you want to search
  144. * @return Absolute Path to the Asset Catalog file on success, error message on failure
  145. */
  146. AZ::Outcome<AZ::IO::Path, AZStd::string> GetAssetCatalogFilePath();
  147. /**
  148. * Computes the absolute path to the platform-specific Cache folder where product assets are stored.
  149. * With platform set as "pc" the path will resemble: C:/ProjectPath/Cache/pc/projectname/
  150. *
  151. * @param projectSpecificCacheFolderAbsolutePath The absolute path to the Cache folder. Example: C:/ProjectPath/Cache
  152. * @param platform the platform of the desired cache location
  153. * @return Absolute path to the platform-specific Cache folder where product assets are stored
  154. */
  155. AZ::IO::Path GetPlatformSpecificCacheFolderPath();
  156. AZStd::string GenerateKeyFromAbsolutePath(const AZStd::string& absoluteFilePath);
  157. void ConvertToRelativePath(AZStd::string_view parentFolderPath, AZStd::string& absoluteFilePath);
  158. AZ::Outcome<void, AZStd::string> MakePath(const AZStd::string& path);
  159. //! Add the specified platform identifier to the filename
  160. void AddPlatformIdentifier(AZStd::string& filePath, const AZStd::string& platformIdentifier);
  161. //! Returns the list of platforms that exist on-disk for the input file path.
  162. AzFramework::PlatformFlags GetPlatformsOnDiskForPlatformSpecificFile(const AZStd::string& platformIndependentAbsolutePath);
  163. //! Returns a map of <absolute file path, source folder display name> of all default Seed List files for the current game project.
  164. AZStd::unordered_map<AZStd::string, AZStd::string> GetDefaultSeedListFiles(
  165. AZStd::string_view enginePath,
  166. AZStd::string_view projectPath,
  167. const AZStd::vector<AzFramework::GemInfo>& gemInfoList,
  168. AzFramework::PlatformFlags platformFlags);
  169. //! Returns a vector of relative paths to Assets that should be included as default Seeds, but are not already in a Seed List file.
  170. AZStd::vector<AZStd::string> GetDefaultSeeds(AZStd::string_view projectPath, AZStd::string_view projectName);
  171. //! Returns the absolute path of {ProjectName}_Dependencies.xml
  172. AZ::IO::Path GetProjectDependenciesFile(AZStd::string_view productPath, AZStd::string_view projectName);
  173. //! Creates the ProjectName_Dependencies.xml file if it does not exist, and adds returns the relative path to the asset in the Cache.
  174. AZ::IO::Path GetProjectDependenciesAssetPath(AZStd::string_view projectPath, AZStd::string_view projectName);
  175. //! Returns the map from gem seed list file path to gem name
  176. AZStd::unordered_map<AZStd::string, AZStd::string> GetGemSeedListFilePathToGemNameMap(
  177. const AZStd::vector<AzFramework::GemInfo>& gemInfoList,
  178. AzFramework::PlatformFlags platformFlags);
  179. //! Given an absolute gem seed file path determines whether the file is valid for the current game project.
  180. //! This method is for validating gem seed list files only.
  181. bool IsGemSeedFilePathValid(
  182. AZStd::string_view enginePath,
  183. AZStd::string seedAbsoluteFilePath,
  184. const AZStd::vector<AzFramework::GemInfo>& gemInfoList,
  185. AzFramework::PlatformFlags platformFlags);
  186. //! Returns platformFlags of all enabled platforms by parsing all the asset processor config files.
  187. //! Please note that the game project could be in a different location to the engine therefore we need the assetRoot param.
  188. AzFramework::PlatformFlags GetEnabledPlatformFlags(
  189. AZStd::string_view enginePath,
  190. AZStd::string_view projectPath);
  191. QJsonObject ReadJson(const AZStd::string& filePath);
  192. void SaveJson(const AZStd::string& filePath, const QJsonObject& jsonObject);
  193. //! Filepath is a helper class that is used to find the absolute path of a file
  194. //! if the inputted file path is an absolute path than it does nothing
  195. //! if the inputted file path is a relative path than based on whether the user
  196. //! also inputted a root directory it computes the absolute path,
  197. //! if root directory is provided it uses that otherwise it uses the engine root as the default root folder.
  198. class FilePath
  199. {
  200. public:
  201. AZ_CLASS_ALLOCATOR(FilePath, AZ::SystemAllocator);
  202. explicit FilePath(
  203. const AZStd::string& filePath,
  204. AZStd::string platformIdentifier = AZStd::string(),
  205. bool checkFileCase = false,
  206. bool ignoreFileCase = false);
  207. explicit FilePath(const AZStd::string& filePath, bool checkFileCase, bool ignoreFileCase);
  208. FilePath() = default;
  209. const AZStd::string& AbsolutePath() const;
  210. const AZStd::string& OriginalPath() const;
  211. AZStd::string ErrorString() const;
  212. bool IsValid() const;
  213. private:
  214. void ComputeAbsolutePath(const AZStd::string& platformIdentifier, bool checkFileCase, bool ignoreFileCase);
  215. AZ::IO::Path m_absolutePath;
  216. AZ::IO::Path m_originalPath;
  217. AZStd::string m_errorString;
  218. bool m_validPath = false;
  219. };
  220. void ValidateOutputFilePath(FilePath filePath, const char* format, ...);
  221. //! ScopedTraceHandler can be used to handle and report errors
  222. class ScopedTraceHandler : public AZ::Debug::TraceMessageBus::Handler
  223. {
  224. public:
  225. ScopedTraceHandler();
  226. ~ScopedTraceHandler();
  227. //! TraceMessageBus Interface
  228. bool OnError(const char* /*window*/, const char* /*message*/) override;
  229. //////////////////////////////////////////////////////////
  230. //! Returns the error count
  231. int GetErrorCount() const;
  232. //! Report all the errors
  233. void ReportErrors();
  234. //! Clear all the errors
  235. void ClearErrors();
  236. private:
  237. AZStd::vector<AZStd::string> m_errors;
  238. bool m_reportingError = false;
  239. };
  240. AZ::Outcome<AzToolsFramework::AssetFileInfoListComparison::ComparisonType, AZStd::string> ParseComparisonType(
  241. const AZStd::string& comparisonType);
  242. AZ::Outcome<AzToolsFramework::AssetFileInfoListComparison::FilePatternType, AZStd::string> ParseFilePatternType(
  243. const AZStd::string& filePatternType);
  244. bool LooksLikePath(const AZStd::string& inputString);
  245. bool LooksLikeWildcardPattern(const AZStd::string& inputPattern);
  246. }