AssetSeedUtil.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include "AssetSeedUtil.h"
  9. #include <AzCore/IO/Path/Path.h>
  10. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  11. namespace AssetValidation::AssetSeed
  12. {
  13. void AddPlatformSeeds(const AZStd::string& rootFolder, AZStd::vector<AZStd::string>& defaultSeedLists, AzFramework::PlatformFlags platformFlags)
  14. {
  15. AZ::IO::FileIOBase* fileIO = AZ::IO::FileIOBase::GetInstance();
  16. auto platforms = AzFramework::PlatformHelper::GetPlatformsInterpreted(platformFlags);
  17. for (auto& platform : platforms)
  18. {
  19. AZStd::string platformDirectory;
  20. AZ::StringFunc::Path::Join(rootFolder.c_str(), platform.data(), platformDirectory);
  21. if (fileIO->Exists(platformDirectory.c_str()))
  22. {
  23. bool recurse = true;
  24. AZ::Outcome<AZStd::list<AZStd::string>, AZStd::string> result = AzFramework::FileFunc::FindFileList(platformDirectory,
  25. AZStd::string::format("*.%s", SeedFileExtension).c_str(), recurse);
  26. if (result.IsSuccess())
  27. {
  28. AZStd::list<AZStd::string> seedFiles = result.TakeValue();
  29. for (AZStd::string& seedFile : seedFiles)
  30. {
  31. AZStd::string normalizedFilePath = seedFile;
  32. AZ::StringFunc::Path::Normalize(seedFile);
  33. defaultSeedLists.emplace_back(seedFile);
  34. }
  35. }
  36. }
  37. }
  38. }
  39. void AddPlatformsDirectorySeeds(const AZStd::string& rootFolder, AZStd::vector<AZStd::string>& defaultSeedLists, AzFramework::PlatformFlags platformFlags)
  40. {
  41. AZ::IO::FileIOBase* fileIO = AZ::IO::FileIOBase::GetInstance();
  42. AZ_Assert(fileIO, "AZ::IO::FileIOBase must be ready for use.\n");
  43. // Check whether platforms directory exists inside the root, if yes than add
  44. // * All seed files from the platforms directory
  45. // * All platform specific seed files based on the platform flags specified.
  46. AZStd::string platformsDirectory;
  47. AZ::StringFunc::Path::Join(rootFolder.c_str(), PlatformsDirectoryName, platformsDirectory);
  48. if (fileIO->Exists(platformsDirectory.c_str()))
  49. {
  50. fileIO->FindFiles(platformsDirectory.c_str(),
  51. AZStd::string::format("*.%s", SeedFileExtension).c_str(),
  52. [&](const char* fileName)
  53. {
  54. AZStd::string normalizedFilePath = fileName;
  55. AZ::StringFunc::Path::Normalize(normalizedFilePath);
  56. defaultSeedLists.emplace_back(normalizedFilePath);
  57. return true;
  58. });
  59. }
  60. AddPlatformSeeds(platformsDirectory, defaultSeedLists, platformFlags);
  61. }
  62. AZStd::vector<AZStd::string> GetGemSeedListFiles(const AZStd::vector<AzFramework::GemInfo>& gemInfoList, AzFramework::PlatformFlags platformFlags)
  63. {
  64. AZStd::vector<AZStd::string> gemSeedListFiles;
  65. for (const AzFramework::GemInfo& gemInfo : gemInfoList)
  66. {
  67. for (AZ::IO::Path absoluteGemAssetPath : gemInfo.m_absoluteSourcePaths)
  68. {
  69. absoluteGemAssetPath /= AzFramework::GemInfo::GetGemAssetFolder();
  70. AZ::IO::Path absoluteGemSeedFilePath = absoluteGemAssetPath / GemsSeedFileName;
  71. absoluteGemSeedFilePath.ReplaceExtension(SeedFileExtension);
  72. if (AZ::IO::FileIOBase::GetInstance()->Exists(absoluteGemSeedFilePath.c_str()))
  73. {
  74. gemSeedListFiles.emplace_back(absoluteGemSeedFilePath);
  75. }
  76. AddPlatformsDirectorySeeds(absoluteGemAssetPath.Native(), gemSeedListFiles, platformFlags);
  77. }
  78. }
  79. return gemSeedListFiles;
  80. }
  81. AZStd::vector<AZStd::string> GetDefaultSeedListFiles(const AZStd::vector<AzFramework::GemInfo>& gemInfoList, AzFramework::PlatformFlags platformFlag)
  82. {
  83. AZ::IO::FileIOBase* fileIO = AZ::IO::FileIOBase::GetInstance();
  84. AZ_Assert(fileIO, "AZ::IO::FileIOBase must be ready for use.\n");
  85. auto settingsRegistry = AZ::SettingsRegistry::Get();
  86. AZ_Assert(settingsRegistry, "Global Settings registry must be available to retrieve default seed list");
  87. AZ::IO::Path engineRoot;
  88. settingsRegistry->Get(engineRoot.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  89. // Add all seed list files of enabled gems for the given project
  90. AZStd::vector<AZStd::string> defaultSeedLists = GetGemSeedListFiles(gemInfoList, platformFlag);
  91. // Add the engine seed list file
  92. AZ::IO::Path engineSourceAssetsDirectory = engineRoot / EngineDirectoryName;
  93. AZ::IO::Path absoluteEngineSeedFilePath = engineSourceAssetsDirectory;
  94. absoluteEngineSeedFilePath /= EngineSeedFileName;
  95. absoluteEngineSeedFilePath.ReplaceExtension(SeedFileExtension);
  96. if (fileIO->Exists(absoluteEngineSeedFilePath.c_str()))
  97. {
  98. defaultSeedLists.emplace_back(AZStd::move(absoluteEngineSeedFilePath.LexicallyNormal().Native()));
  99. }
  100. AddPlatformsDirectorySeeds(engineSourceAssetsDirectory.Native(), defaultSeedLists, platformFlag);
  101. // Add the current project default seed list file
  102. AZStd::string projectName;
  103. AZ::IO::FixedMaxPathString projectPath = AZ::Utils::GetProjectPath();
  104. if (!projectPath.empty())
  105. {
  106. AZ::IO::FixedMaxPath absoluteProjectDefaultSeedFilePath{ engineRoot };
  107. absoluteProjectDefaultSeedFilePath /= projectPath;
  108. absoluteProjectDefaultSeedFilePath /= EngineSeedFileName;
  109. absoluteProjectDefaultSeedFilePath.ReplaceExtension(SeedFileExtension);
  110. if (fileIO->Exists(absoluteProjectDefaultSeedFilePath.c_str()))
  111. {
  112. defaultSeedLists.emplace_back(absoluteProjectDefaultSeedFilePath.LexicallyNormal().String());
  113. }
  114. }
  115. return defaultSeedLists;
  116. }
  117. }