AssetBuilderApplication.cpp 8.5 KB

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