StandaloneToolsApplication.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "StandaloneToolsApplication.h"
  9. #include <AzCore/IO/Streamer/StreamerComponent.h>
  10. #include <AzCore/Jobs/JobManagerComponent.h>
  11. #include <AzCore/UserSettings/UserSettingsComponent.h>
  12. #include <AzCore/std/containers/array.h>
  13. #include <AzFramework/API/ApplicationAPI.h>
  14. #include <AzFramework/Asset/AssetCatalogComponent.h>
  15. #include <AzFramework/StringFunc/StringFunc.h>
  16. #include <AzNetworking/Framework/INetworkInterface.h>
  17. #include <AzNetworking/Framework/INetworking.h>
  18. #include <AzNetworking/Framework/NetworkingSystemComponent.h>
  19. #include <AzToolsFramework/UI/LegacyFramework/Core/IPCComponent.h>
  20. namespace StandaloneTools
  21. {
  22. BaseApplication::BaseApplication(int argc, char** argv)
  23. : LegacyFramework::Application(argc, argv)
  24. {
  25. AZ::UserSettingsFileLocatorBus::Handler::BusConnect();
  26. }
  27. BaseApplication::~BaseApplication()
  28. {
  29. AZ::UserSettingsFileLocatorBus::Handler::BusDisconnect();
  30. }
  31. void BaseApplication::RegisterCoreComponents()
  32. {
  33. LegacyFramework::Application::RegisterCoreComponents();
  34. RegisterComponentDescriptor(LegacyFramework::IPCComponent::CreateDescriptor());
  35. RegisterComponentDescriptor(AzNetworking::NetworkingSystemComponent::CreateDescriptor());
  36. RegisterComponentDescriptor(AZ::UserSettingsComponent::CreateDescriptor());
  37. RegisterComponentDescriptor(AZ::JobManagerComponent::CreateDescriptor());
  38. RegisterComponentDescriptor(AZ::StreamerComponent::CreateDescriptor());
  39. }
  40. void BaseApplication::CreateSystemComponents()
  41. {
  42. LegacyFramework::Application::CreateSystemComponents();
  43. // AssetCatalogComponent was moved to the Application Entity to fulfil service requirements.
  44. EnsureComponentRemoved(AzFramework::AssetCatalogComponent::RTTI_Type());
  45. }
  46. void BaseApplication::CreateApplicationComponents()
  47. {
  48. EnsureComponentCreated(AZ::StreamerComponent::RTTI_Type());
  49. EnsureComponentCreated(AZ::JobManagerComponent::RTTI_Type());
  50. EnsureComponentCreated(AzNetworking::NetworkingSystemComponent::RTTI_Type());
  51. //EnsureComponentCreated(AzFramework::TargetManagementComponent::RTTI_Type());
  52. EnsureComponentCreated(LegacyFramework::IPCComponent::RTTI_Type());
  53. // Check for user settings components already added (added by the app descriptor
  54. AZStd::array<bool, AZ::UserSettings::CT_MAX> userSettingsAdded;
  55. userSettingsAdded.fill(false);
  56. for (const auto& component : m_applicationEntity->GetComponents())
  57. {
  58. if (const auto userSettings = azrtti_cast<AZ::UserSettingsComponent*>(component))
  59. {
  60. userSettingsAdded[userSettings->GetProviderId()] = true;
  61. }
  62. }
  63. /*
  64. AZ::ModuleManagerRequestBus::Broadcast(
  65. &AZ::ModuleManagerRequestBus::Events::EnumerateModules,
  66. [](const AZ::ModuleData& moduleData)
  67. {
  68. AZ::Entity* moduleEntity = moduleData.GetEntity();
  69. for (const auto& component : moduleEntity->GetComponents())
  70. {
  71. if (auto targetManagement = azrtti_cast<AzFramework::TargetManagementComponent*>(component))
  72. {
  73. targetManagement->SetTargetAsHost(true);
  74. }
  75. }
  76. return true;
  77. });
  78. */
  79. // For each provider not already added, add it.
  80. for (AZ::u32 providerId = 0; providerId < userSettingsAdded.size(); ++providerId)
  81. {
  82. if (!userSettingsAdded[providerId])
  83. {
  84. // Don't need to add one for global, that's added by someone else
  85. m_applicationEntity->AddComponent(aznew AZ::UserSettingsComponent(providerId));
  86. }
  87. }
  88. }
  89. bool BaseApplication::StartDebugService()
  90. {
  91. AzNetworking::INetworkInterface* networkInterface =
  92. AZ::Interface<AzNetworking::INetworking>::Get()->RetrieveNetworkInterface(AZ::Name("LuaRemoteTools"));
  93. if (networkInterface)
  94. {
  95. const auto console = AZ::Interface<AZ::IConsole>::Get();
  96. uint16_t target_port = 0;//AzFramework::DefaultTargetPort;
  97. if (console->GetCvarValue("target_port", target_port) != AZ::GetValueResult::Success)
  98. {
  99. AZ_Assert(false, "TargetManagement port could not be found");
  100. }
  101. networkInterface->Listen(target_port);
  102. return true;
  103. }
  104. return false;
  105. }
  106. void BaseApplication::OnApplicationEntityActivated()
  107. {
  108. [[maybe_unused]] bool launched = StartDebugService();
  109. AZ_Warning("EditorApplication", launched, "Could not start hosting; Only replay is available.");
  110. }
  111. void BaseApplication::SetSettingsRegistrySpecializations(AZ::SettingsRegistryInterface::Specializations& specializations)
  112. {
  113. LegacyFramework::Application::SetSettingsRegistrySpecializations(specializations);
  114. specializations.Append("standalone_tools");
  115. }
  116. AZStd::string BaseApplication::GetStoragePath() const
  117. {
  118. AZStd::string storagePath;
  119. FrameworkApplicationMessages::Bus::BroadcastResult(storagePath, &FrameworkApplicationMessages::GetApplicationGlobalStoragePath);
  120. if (storagePath.empty())
  121. {
  122. FrameworkApplicationMessages::Bus::BroadcastResult(storagePath, &FrameworkApplicationMessages::GetApplicationDirectory);
  123. }
  124. return storagePath;
  125. }
  126. AZStd::string BaseApplication::ResolveFilePath(AZ::u32 providerId)
  127. {
  128. AZStd::string appName;
  129. FrameworkApplicationMessages::Bus::BroadcastResult(appName, &FrameworkApplicationMessages::GetApplicationName);
  130. AZStd::string userStoragePath = GetStoragePath();
  131. AzFramework::StringFunc::Path::Join(userStoragePath.c_str(), appName.c_str(), userStoragePath);
  132. AZ::IO::SystemFile::CreateDir(userStoragePath.c_str());
  133. AZStd::string fileName;
  134. switch (providerId)
  135. {
  136. case AZ::UserSettings::CT_LOCAL:
  137. fileName = AZStd::string::format("%s_UserSettings.xml", appName.c_str());
  138. break;
  139. case AZ::UserSettings::CT_GLOBAL:
  140. fileName = "GlobalUserSettings.xml";
  141. break;
  142. }
  143. AzFramework::StringFunc::Path::Join(userStoragePath.c_str(), fileName.c_str(), userStoragePath);
  144. return userStoragePath;
  145. }
  146. } // namespace StandaloneTools