AssetBuilderApplication.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. : AssetBuilderApplication(argc, argv, {})
  87. {
  88. }
  89. AssetBuilderApplication::AssetBuilderApplication(int* argc, char*** argv, AZ::ComponentApplicationSettings componentAppSettings)
  90. : AzToolsFramework::ToolsApplication(argc, argv, AZStd::move(componentAppSettings))
  91. , m_qtApplication(*argc, *argv)
  92. {
  93. // The settings registry has been created at this point
  94. auto settingsRegistry = AZ::SettingsRegistry::Get();
  95. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddBuildSystemTargetSpecialization(
  96. *settingsRegistry, AssetBuilder::GetBuildTargetName());
  97. AZ::Interface<IBuilderApplication>::Register(this);
  98. }
  99. AssetBuilderApplication::~AssetBuilderApplication()
  100. {
  101. AZ::Interface<IBuilderApplication>::Unregister(this);
  102. }
  103. void AssetBuilderApplication::RegisterCoreComponents()
  104. {
  105. AzToolsFramework::ToolsApplication::RegisterCoreComponents();
  106. RegisterComponentDescriptor(AssetBuilderComponent::CreateDescriptor());
  107. RegisterComponentDescriptor(AssetProcessor::ToolsAssetCatalogComponent::CreateDescriptor());
  108. }
  109. void AssetBuilderApplication::StartCommon(AZ::Entity* systemEntity)
  110. {
  111. InstallCtrlHandler();
  112. // Merge in the SettingsRegistry for the game being processed. This does not
  113. // necessarily correspond to the project name in the bootstrap.cfg since it
  114. // the AssetBuilder supports overriding the project-path on the command line
  115. AZ::SettingsRegistryInterface& registry = *AZ::SettingsRegistry::Get();
  116. // Retrieve specializations from the Settings Registry and ComponentApplication derived classes
  117. AZ::SettingsRegistryInterface::Specializations specializations;
  118. SetSettingsRegistrySpecializations(specializations);
  119. // Merge the SettingsRegistry file again using gameName as an additional specialization
  120. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_TargetBuildDependencyRegistry(registry,
  121. AZ_TRAIT_OS_PLATFORM_CODENAME, specializations);
  122. AzToolsFramework::ToolsApplication::StartCommon(systemEntity);
  123. #if defined(AZ_PLATFORM_MAC)
  124. // The asset builder needs to start astcenc as a child process to compress textures.
  125. // astcenc is started by the PVRTexLib dynamic library. In order for it to be able to find
  126. // the executable, we need to set the PATH environment variable.
  127. AZStd::string exeFolder;
  128. AZ::ComponentApplicationBus::BroadcastResult(exeFolder, &AZ::ComponentApplicationBus::Events::GetExecutableFolder);
  129. setenv("PATH", exeFolder.c_str(), 1);
  130. #endif // AZ_PLATFORM_MAC
  131. // Make sure a project path was set to settings registry and error/warn if not.
  132. auto projectPath = AZ::Utils::GetProjectPath();
  133. if (projectPath.empty())
  134. {
  135. if (IsInDebugMode())
  136. {
  137. AZ_Error("AssetBuilder", false, "Unable to determine the project path automatically. "
  138. "Make sure a default project path has been set or provide a --project-path option on the command line. "
  139. "(See -help for more info.)");
  140. return;
  141. }
  142. else
  143. {
  144. AZ_Printf(AssetBuilderSDK::InfoWindow, "project-path not specified on the command line, assuming current directory.\n");
  145. AZ_Printf(AssetBuilderSDK::InfoWindow, "project-path is best specified as the full path to the project's folder.");
  146. }
  147. }
  148. // Loads dynamic modules and registers any component descriptors populated into the AZ::Module m_descriptor list
  149. // for each instantiated module class
  150. LoadDynamicModules();
  151. AssetBuilderSDK::InitializeSerializationContext();
  152. AssetBuilderSDK::InitializeBehaviorContext();
  153. AssetBuilder::InitializeSerializationContext();
  154. // the asset builder app never writes source files, only assets, so there is no need to do any kind of asset upgrading
  155. AZ::Data::AssetManager::Instance().SetAssetInfoUpgradingEnabled(false);
  156. // Disable parallel dependency loads since the builders can't count on all other assets and their info being ready.
  157. // Specifically, asset builders can trigger asset loads during the building process. The ToolsAssetCatalog doesn't
  158. // implement the dependency APIs, so the asset loads will fail to load any dependent assets.
  159. //
  160. // NOTE: The ToolsAssetCatalog could *potentially* implement the dependency APIs by querying the live Asset Processor instance,
  161. // but this will return incomplete dependency information based on the subset of assets that have already processed.
  162. // In theory, if the Asset Builder dependencies are set up correctly, the needed subset should always be processed first,
  163. // but the one edge case that can't be handled is the case where the Asset Builder intends to filter out the dependent load,
  164. // but needs to query enough information about the asset (specifically asset type) to know that it can filter it out. Since
  165. // the assets are being filtered out, they aren't dependencies, might not be built yet, and so might not have asset type available.
  166. AZ::Data::AssetManager::Instance().SetParallelDependentLoadingEnabled(false);
  167. }
  168. bool AssetBuilderApplication::IsInDebugMode() const
  169. {
  170. return AssetBuilderComponent::IsInDebugMode(m_commandLine);
  171. }
  172. void AssetBuilderApplication::InitializeBuilderComponents()
  173. {
  174. CreateAndAddEntityFromComponentTags(AZStd::vector<AZ::Crc32>({ AssetBuilderSDK::ComponentTags::AssetBuilder }), "AssetBuilders Entity");
  175. }