3
0

AssetBuilderApplication.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <AzCore/Debug/Trace.h>
  9. #include <AzCore/IO/FileIO.h>
  10. #include <AzCore/Asset/AssetManager.h>
  11. #include <AzCore/Serialization/Utils.h>
  12. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  13. #include <AzCore/UserSettings/UserSettingsComponent.h>
  14. #include <AzCore/Slice/SliceSystemComponent.h>
  15. #include <AzCore/Interface/Interface.h>
  16. #include <AzCore/Utils/Utils.h>
  17. #include <AzFramework/Asset/AssetCatalogComponent.h>
  18. #include <AzFramework/Asset/AssetSystemComponent.h>
  19. #include <AzFramework/Input/System/InputSystemComponent.h>
  20. #include <AzFramework/StringFunc/StringFunc.h>
  21. #include <AzToolsFramework/Asset/AssetSystemComponent.h>
  22. #include <AzToolsFramework/Component/EditorComponentAPIComponent.h>
  23. #include <AzToolsFramework/Entity/EditorEntityActionComponent.h>
  24. #include <AzToolsFramework/Entity/EditorEntityContextComponent.h>
  25. #include <AzToolsFramework/Entity/EditorEntityModelComponent.h>
  26. #include <AzToolsFramework/Entity/EditorEntitySearchComponent.h>
  27. #include <AzToolsFramework/Prefab/PrefabSystemComponent.h>
  28. #include <AzToolsFramework/Slice/SliceMetadataEntityContextComponent.h>
  29. #include <AzToolsFramework/ToolsComponents/ToolsAssetCatalogComponent.h>
  30. #include <AzToolsFramework/Metadata/MetadataManager.h>
  31. #include <AzToolsFramework/Metadata/UuidUtils.h>
  32. #include <AssetBuilderSDK/AssetBuilderSDK.h>
  33. #include <AssetBuilderApplication.h>
  34. #include <AssetBuilderComponent.h>
  35. #include <AssetBuilderInfo.h>
  36. #include <AzCore/Interface/Interface.h>
  37. #include <Entity/EntityUtilityComponent.h>
  38. #include <AssetBuilderStatic.h>
  39. namespace AssetBuilder
  40. {
  41. //! This function returns the build system target name
  42. AZStd::string_view GetBuildTargetName()
  43. {
  44. #if !defined (LY_CMAKE_TARGET)
  45. #error "LY_CMAKE_TARGET must be defined in order to add this source file to a CMake executable target"
  46. #endif
  47. return AZStd::string_view{ LY_CMAKE_TARGET };
  48. }
  49. }
  50. AZ::ComponentTypeList AssetBuilderApplication::GetRequiredSystemComponents() const
  51. {
  52. AZ::ComponentTypeList components = AzFramework::Application::GetRequiredSystemComponents();
  53. for (auto iter = components.begin(); iter != components.end();)
  54. {
  55. if (*iter == azrtti_typeid<AZ::UserSettingsComponent>()
  56. || *iter == azrtti_typeid<AzFramework::InputSystemComponent>()
  57. || *iter == azrtti_typeid<AzFramework::AssetCatalogComponent>()
  58. )
  59. {
  60. iter = components.erase(iter);
  61. }
  62. else
  63. {
  64. ++iter;
  65. }
  66. }
  67. components.insert(components.end(), {
  68. azrtti_typeid<AZ::SliceSystemComponent>(),
  69. azrtti_typeid<AzToolsFramework::SliceMetadataEntityContextComponent>(),
  70. azrtti_typeid<AssetBuilderComponent>(),
  71. azrtti_typeid<AssetProcessor::ToolsAssetCatalogComponent>(),
  72. azrtti_typeid<AzToolsFramework::AssetSystem::AssetSystemComponent>(),
  73. azrtti_typeid<AzToolsFramework::Components::EditorComponentAPIComponent>(),
  74. azrtti_typeid<AzToolsFramework::Components::EditorEntityActionComponent>(),
  75. azrtti_typeid<AzToolsFramework::Components::EditorEntitySearchComponent>(),
  76. azrtti_typeid<AzToolsFramework::Components::EditorEntityModelComponent>(),
  77. azrtti_typeid<AzToolsFramework::EditorEntityContextComponent>(),
  78. azrtti_typeid<AzToolsFramework::Prefab::PrefabSystemComponent>(),
  79. azrtti_typeid<AzToolsFramework::EntityUtilityComponent>(),
  80. azrtti_typeid<AzToolsFramework::MetadataManager>(),
  81. azrtti_typeid<AzToolsFramework::UuidUtilComponent>(),
  82. });
  83. return components;
  84. }
  85. AssetBuilderApplication::AssetBuilderApplication(int* argc, char*** argv)
  86. : AzToolsFramework::ToolsApplication(argc, argv)
  87. , m_qtApplication(*argc, *argv)
  88. {
  89. // The settings registry has been created at this point
  90. auto settingsRegistry = AZ::SettingsRegistry::Get();
  91. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddBuildSystemTargetSpecialization(
  92. *settingsRegistry, AssetBuilder::GetBuildTargetName());
  93. AZ::Interface<IBuilderApplication>::Register(this);
  94. }
  95. AssetBuilderApplication::~AssetBuilderApplication()
  96. {
  97. AZ::Interface<IBuilderApplication>::Unregister(this);
  98. }
  99. void AssetBuilderApplication::RegisterCoreComponents()
  100. {
  101. AzToolsFramework::ToolsApplication::RegisterCoreComponents();
  102. RegisterComponentDescriptor(AssetBuilderComponent::CreateDescriptor());
  103. RegisterComponentDescriptor(AssetProcessor::ToolsAssetCatalogComponent::CreateDescriptor());
  104. }
  105. void AssetBuilderApplication::StartCommon(AZ::Entity* systemEntity)
  106. {
  107. InstallCtrlHandler();
  108. // Merge in the SettingsRegistry for the game being processed. This does not
  109. // necessarily correspond to the project name in the bootstrap.cfg since it
  110. // the AssetBuilder supports overriding the project-path on the command line
  111. AZ::SettingsRegistryInterface& registry = *AZ::SettingsRegistry::Get();
  112. // Retrieve specializations from the Settings Registry and ComponentApplication derived classes
  113. AZ::SettingsRegistryInterface::Specializations specializations;
  114. SetSettingsRegistrySpecializations(specializations);
  115. // Merge the SettingsRegistry file again using gameName as an additional specialization
  116. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_TargetBuildDependencyRegistry(registry,
  117. AZ_TRAIT_OS_PLATFORM_CODENAME, specializations);
  118. AzToolsFramework::ToolsApplication::StartCommon(systemEntity);
  119. #if defined(AZ_PLATFORM_MAC)
  120. // The asset builder needs to start astcenc as a child process to compress textures.
  121. // astcenc is started by the PVRTexLib dynamic library. In order for it to be able to find
  122. // the executable, we need to set the PATH environment variable.
  123. AZStd::string exeFolder;
  124. AZ::ComponentApplicationBus::BroadcastResult(exeFolder, &AZ::ComponentApplicationBus::Events::GetExecutableFolder);
  125. setenv("PATH", exeFolder.c_str(), 1);
  126. #endif // AZ_PLATFORM_MAC
  127. // Make sure a project path was set to settings registry and error/warn if not.
  128. auto projectPath = AZ::Utils::GetProjectPath();
  129. if (projectPath.empty())
  130. {
  131. if (IsInDebugMode())
  132. {
  133. AZ_Error("AssetBuilder", false, "Unable to determine the project path automatically. "
  134. "Make sure a default project path has been set or provide a --project-path option on the command line. "
  135. "(See -help for more info.)");
  136. return;
  137. }
  138. else
  139. {
  140. AZ_Printf(AssetBuilderSDK::InfoWindow, "project-path not specified on the command line, assuming current directory.\n");
  141. AZ_Printf(AssetBuilderSDK::InfoWindow, "project-path is best specified as the full path to the project's folder.");
  142. }
  143. }
  144. // Loads dynamic modules and registers any component descriptors populated into the AZ::Module m_descriptor list
  145. // for each instantiated module class
  146. LoadDynamicModules();
  147. AssetBuilderSDK::InitializeSerializationContext();
  148. AssetBuilderSDK::InitializeBehaviorContext();
  149. AssetBuilder::InitializeSerializationContext();
  150. // the asset builder app never writes source files, only assets, so there is no need to do any kind of asset upgrading
  151. AZ::Data::AssetManager::Instance().SetAssetInfoUpgradingEnabled(false);
  152. // Disable parallel dependency loads since the builders can't count on all other assets and their info being ready.
  153. // Specifically, asset builders can trigger asset loads during the building process. The ToolsAssetCatalog doesn't
  154. // implement the dependency APIs, so the asset loads will fail to load any dependent assets.
  155. //
  156. // NOTE: The ToolsAssetCatalog could *potentially* implement the dependency APIs by querying the live Asset Processor instance,
  157. // but this will return incomplete dependency information based on the subset of assets that have already processed.
  158. // In theory, if the Asset Builder dependencies are set up correctly, the needed subset should always be processed first,
  159. // but the one edge case that can't be handled is the case where the Asset Builder intends to filter out the dependent load,
  160. // but needs to query enough information about the asset (specifically asset type) to know that it can filter it out. Since
  161. // the assets are being filtered out, they aren't dependencies, might not be built yet, and so might not have asset type available.
  162. AZ::Data::AssetManager::Instance().SetParallelDependentLoadingEnabled(false);
  163. }
  164. bool AssetBuilderApplication::IsInDebugMode() const
  165. {
  166. return AssetBuilderComponent::IsInDebugMode(m_commandLine);
  167. }
  168. void AssetBuilderApplication::InitializeBuilderComponents()
  169. {
  170. CreateAndAddEntityFromComponentTags(AZStd::vector<AZ::Crc32>({ AssetBuilderSDK::ComponentTags::AssetBuilder }), "AssetBuilders Entity");
  171. }