Application.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. : AzToolsFramework::ToolsApplication(&argc, &argv)
  27. , m_stdoutCapturer(stdoutCapturer)
  28. {
  29. // We need a specialized variant of EditorEntityContextComponent for the SliceConverter, so we register the descriptor here.
  30. RegisterComponentDescriptor(AzToolsFramework::SliceConverterEditorEntityContextComponent::CreateDescriptor());
  31. AZ::IO::FixedMaxPath projectPath = AZ::Utils::GetProjectPath();
  32. if (projectPath.empty())
  33. {
  34. AZ_TracePrintf("Serialize Context Tools", "Unable to determine the project path . "
  35. "Make sure project has been set or provide via the -project-path option on the command line. (See -help for more info.)");
  36. return;
  37. }
  38. size_t configSwitchCount = m_commandLine.GetNumSwitchValues("config");
  39. if (configSwitchCount > 0)
  40. {
  41. AZ::IO::FixedMaxPath absConfigFilePath = projectPath / m_commandLine.GetSwitchValue("config", configSwitchCount - 1);
  42. if (AZ::IO::SystemFile::Exists(absConfigFilePath.c_str()))
  43. {
  44. m_configFilePath = AZStd::move(absConfigFilePath);
  45. }
  46. }
  47. // Merge the build system generated setting registry file by using either "Editor" or
  48. // and "${ProjectName}_GameLauncher" as a specialization
  49. auto projectName = AZ::Utils::GetProjectName();
  50. if (projectName.empty())
  51. {
  52. AZ_Error("Serialize Context Tools", false, "Unable to query the project name from settings registry");
  53. }
  54. else
  55. {
  56. AZ::SettingsRegistryInterface::Specializations projectSpecializations{ projectName };
  57. // If a project specialization has been passed in via the command line, use it.
  58. if (size_t specializationCount = m_commandLine.GetNumSwitchValues("specializations"); specializationCount > 0)
  59. {
  60. for (size_t specializationIndex = 0; specializationIndex < specializationCount; ++specializationIndex)
  61. {
  62. projectSpecializations.Append(m_commandLine.GetSwitchValue("specializations", specializationIndex));
  63. }
  64. }
  65. else
  66. {
  67. // Otherwise, if a config file was passed in, auto-set the specialization based on the config file name.
  68. AZ::IO::PathView configFilenameStem = m_configFilePath.Stem();
  69. if (AZ::StringFunc::Equal(configFilenameStem.Native(), "Editor"))
  70. {
  71. projectSpecializations.Append("editor");
  72. }
  73. else if (AZ::StringFunc::Equal(configFilenameStem.Native(), "Game"))
  74. {
  75. projectSpecializations.Append(projectName + "_GameLauncher");
  76. }
  77. // If the "dumptypes" or "createtype" supplied attempt to load the editor gem dependencies
  78. if (m_commandLine.GetNumMiscValues() > 0 &&
  79. (m_commandLine.GetMiscValue(0) == "dumptypes" || m_commandLine.GetMiscValue(0) == "createtype"
  80. || m_commandLine.GetMiscValue(0) == "dumpsc"))
  81. {
  82. projectSpecializations.Append("editor");
  83. }
  84. }
  85. // Used the project specializations to merge the gem modules dependencies *.setreg files
  86. auto registry = AZ::SettingsRegistry::Get();
  87. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_TargetBuildDependencyRegistry(*registry,
  88. AZ_TRAIT_OS_PLATFORM_CODENAME, projectSpecializations);
  89. }
  90. }
  91. const char* Application::GetConfigFilePath() const
  92. {
  93. return m_configFilePath.c_str();
  94. }
  95. void Application::QueryApplicationType(AZ::ApplicationTypeQuery& appType) const
  96. {
  97. appType.m_maskValue = AZ::ApplicationTypeQuery::Masks::Tool;
  98. }
  99. void Application::SetSettingsRegistrySpecializations(AZ::SettingsRegistryInterface::Specializations& specializations)
  100. {
  101. AZ::ComponentApplication::SetSettingsRegistrySpecializations(specializations);
  102. specializations.Append("serializecontexttools");
  103. }
  104. AZ::ComponentTypeList Application::GetRequiredSystemComponents() const
  105. {
  106. // By default, we use all of the standard system components.
  107. AZ::ComponentTypeList components = AzToolsFramework::ToolsApplication::GetRequiredSystemComponents();
  108. // Also add in the ThumbnailerNullComponent so that components requiring a ThumbnailService can still be started up.
  109. components.emplace_back(azrtti_typeid<AzToolsFramework::Thumbnailer::ThumbnailerNullComponent>());
  110. // The Slice Converter requires a specialized variant of the EditorEntityContextComponent that exposes the ability
  111. // to disable the behavior of activating entities on creation. During conversion, the creation flow will be triggered,
  112. // but entity activation requires a significant amount of subsystem initialization that's unneeded for conversion.
  113. // So, to get around this, we swap out EditorEntityContextComponent with SliceConverterEditorEntityContextComponent.
  114. components.erase(
  115. AZStd::remove(
  116. components.begin(), components.end(), azrtti_typeid<AzToolsFramework::EditorEntityContextComponent>()),
  117. components.end());
  118. components.emplace_back(azrtti_typeid<AzToolsFramework::SliceConverterEditorEntityContextComponent>());
  119. return components;
  120. }
  121. void Application::SetStdoutCapturer(AZ::IO::FileDescriptorCapturer* stdoutCapturer)
  122. {
  123. m_stdoutCapturer = stdoutCapturer;
  124. }
  125. AZ::IO::FileDescriptorCapturer* Application::GetStdoutCapturer() const
  126. {
  127. return m_stdoutCapturer;
  128. }
  129. } // namespace AZ::SerializeContextTools