Application.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <Application.h>
  9. #include <AzCore/IO/FileIO.h>
  10. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  11. #include <AzCore/StringFunc/StringFunc.h>
  12. #include <AzCore/Utils/Utils.h>
  13. #include <AzToolsFramework/Thumbnails/ThumbnailerNullComponent.h>
  14. #include <SliceConverterEditorEntityContextComponent.h>
  15. namespace AZ::SerializeContextTools
  16. {
  17. // SerializeContextTools is a full ToolsApplication that will load a project's Gem DLLs and initialize the system components.
  18. // This level of initialization is required to get all the serialization contexts and asset handlers registered, so that when
  19. // data transformations take place, none of the data is dropped due to not being recognized.
  20. // However, as a simplification, anything requiring Python or Qt is skipped during initialization:
  21. // - The gem_autoload.serializecontexttools.setreg file disables autoload for QtForPython, EditorPythonBindings, and PythonAssetBuilder
  22. // - The system component initialization below uses ThumbnailerNullComponent so that other components relying on a ThumbnailService
  23. // can still be started up, but the thumbnail service itself won't do anything. The real ThumbnailerComponent uses Qt, which is why
  24. // it isn't used.
  25. Application::Application(int argc, char** argv, AZ::IO::FileDescriptorCapturer* stdoutCapturer)
  26. : Application(argc, argv, stdoutCapturer, {})
  27. {
  28. }
  29. Application::Application(int argc, char** argv, ComponentApplicationSettings componentAppSettings)
  30. : Application(argc, argv, nullptr, AZStd::move(componentAppSettings))
  31. {
  32. }
  33. Application::Application(int argc, char** argv, AZ::IO::FileDescriptorCapturer* stdoutCapturer, ComponentApplicationSettings componentAppSettings)
  34. : AzToolsFramework::ToolsApplication(&argc, &argv, AZStd::move(componentAppSettings))
  35. , m_stdoutCapturer(stdoutCapturer)
  36. {
  37. // We need a specialized variant of EditorEntityContextComponent for the SliceConverter, so we register the descriptor here.
  38. RegisterComponentDescriptor(AzToolsFramework::SliceConverterEditorEntityContextComponent::CreateDescriptor());
  39. AZ::IO::FixedMaxPath projectPath = AZ::Utils::GetProjectPath();
  40. if (projectPath.empty())
  41. {
  42. AZ_TracePrintf("Serialize Context Tools", "Unable to determine the project path . "
  43. "Make sure project has been set or provide via the -project-path option on the command line. (See -help for more info.)");
  44. return;
  45. }
  46. size_t configSwitchCount = m_commandLine.GetNumSwitchValues("config");
  47. if (configSwitchCount > 0)
  48. {
  49. AZ::IO::FixedMaxPath absConfigFilePath = projectPath / m_commandLine.GetSwitchValue("config", configSwitchCount - 1);
  50. if (AZ::IO::SystemFile::Exists(absConfigFilePath.c_str()))
  51. {
  52. m_configFilePath = AZStd::move(absConfigFilePath);
  53. }
  54. }
  55. // Merge the build system generated setting registry file by using either "Editor" or
  56. // and "${ProjectName}_GameLauncher" as a specialization
  57. auto projectName = AZ::Utils::GetProjectName();
  58. if (projectName.empty())
  59. {
  60. AZ_Error("Serialize Context Tools", false, "Unable to query the project name from settings registry");
  61. }
  62. else
  63. {
  64. AZ::SettingsRegistryInterface::Specializations projectSpecializations{ projectName };
  65. // If a project specialization has been passed in via the command line, use it.
  66. if (size_t specializationCount = m_commandLine.GetNumSwitchValues("specializations"); specializationCount > 0)
  67. {
  68. for (size_t specializationIndex = 0; specializationIndex < specializationCount; ++specializationIndex)
  69. {
  70. projectSpecializations.Append(m_commandLine.GetSwitchValue("specializations", specializationIndex));
  71. }
  72. }
  73. else
  74. {
  75. // Otherwise, if a config file was passed in, auto-set the specialization based on the config file name.
  76. AZ::IO::PathView configFilenameStem = m_configFilePath.Stem();
  77. if (AZ::StringFunc::Equal(configFilenameStem.Native(), "Editor"))
  78. {
  79. projectSpecializations.Append("editor");
  80. }
  81. else if (AZ::StringFunc::Equal(configFilenameStem.Native(), "Game"))
  82. {
  83. projectSpecializations.Append(projectName + "_GameLauncher");
  84. }
  85. // If the "dumptypes" or "createtype" supplied attempt to load the editor gem dependencies
  86. if (m_commandLine.GetNumMiscValues() > 0 &&
  87. (m_commandLine.GetMiscValue(0) == "dumptypes" || m_commandLine.GetMiscValue(0) == "createtype"
  88. || m_commandLine.GetMiscValue(0) == "dumpsc"))
  89. {
  90. projectSpecializations.Append("editor");
  91. }
  92. }
  93. // Used the project specializations to merge the gem modules dependencies *.setreg files
  94. auto registry = AZ::SettingsRegistry::Get();
  95. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_TargetBuildDependencyRegistry(*registry,
  96. AZ_TRAIT_OS_PLATFORM_CODENAME, projectSpecializations);
  97. }
  98. }
  99. const char* Application::GetConfigFilePath() const
  100. {
  101. return m_configFilePath.c_str();
  102. }
  103. void Application::QueryApplicationType(AZ::ApplicationTypeQuery& appType) const
  104. {
  105. appType.m_maskValue = AZ::ApplicationTypeQuery::Masks::Tool;
  106. }
  107. void Application::SetSettingsRegistrySpecializations(AZ::SettingsRegistryInterface::Specializations& specializations)
  108. {
  109. AZ::ComponentApplication::SetSettingsRegistrySpecializations(specializations);
  110. specializations.Append("serializecontexttools");
  111. }
  112. AZ::ComponentTypeList Application::GetRequiredSystemComponents() const
  113. {
  114. // By default, we use all of the standard system components.
  115. AZ::ComponentTypeList components = AzToolsFramework::ToolsApplication::GetRequiredSystemComponents();
  116. // Also add in the ThumbnailerNullComponent so that components requiring a ThumbnailService can still be started up.
  117. components.emplace_back(azrtti_typeid<AzToolsFramework::Thumbnailer::ThumbnailerNullComponent>());
  118. // The Slice Converter requires a specialized variant of the EditorEntityContextComponent that exposes the ability
  119. // to disable the behavior of activating entities on creation. During conversion, the creation flow will be triggered,
  120. // but entity activation requires a significant amount of subsystem initialization that's unneeded for conversion.
  121. // So, to get around this, we swap out EditorEntityContextComponent with SliceConverterEditorEntityContextComponent.
  122. components.erase(
  123. AZStd::remove(
  124. components.begin(), components.end(), azrtti_typeid<AzToolsFramework::EditorEntityContextComponent>()),
  125. components.end());
  126. components.emplace_back(azrtti_typeid<AzToolsFramework::SliceConverterEditorEntityContextComponent>());
  127. return components;
  128. }
  129. void Application::SetStdoutCapturer(AZ::IO::FileDescriptorCapturer* stdoutCapturer)
  130. {
  131. m_stdoutCapturer = stdoutCapturer;
  132. }
  133. AZ::IO::FileDescriptorCapturer* Application::GetStdoutCapturer() const
  134. {
  135. return m_stdoutCapturer;
  136. }
  137. } // namespace AZ::SerializeContextTools