ATOM-15439 Implement basic local socket and server for IPC in material editor and other tools

This replaces grid hub usage in the material editor. It allows material editor and other tools to intercommunicate on the local host.  This will allow enforcing that there is only one instance of the material editor running.  Opening a second instance will forward command line options to the first instance running a local server.

https://jira.agscollab.com/browse/ATOM-15439
https://jira.agscollab.com/browse/ATOM-13742
This commit is contained in:
guthadam
2021-05-02 17:08:21 -05:00
parent 781415755d
commit fae33e9235
18 changed files with 406 additions and 135 deletions
@@ -13,7 +13,6 @@
#include <Atom/Document/MaterialDocumentModule.h>
#include <Document/MaterialDocumentSystemComponent.h>
#include <AzFramework/TargetManagement/TargetManagementComponent.h>
#include <AzToolsFramework/UI/PropertyEditor/PropertyManagerComponent.h>
#include <AzToolsFramework/Asset/AssetSystemComponent.h>
@@ -32,7 +31,6 @@ namespace MaterialEditor
return AZ::ComponentTypeList{
azrtti_typeid<AzToolsFramework::AssetSystem::AssetSystemComponent>(),
azrtti_typeid<MaterialDocumentSystemComponent>(),
azrtti_typeid<AzFramework::TargetManagementComponent>(),
};
}
}
@@ -109,7 +109,6 @@ namespace MaterialEditor
void MaterialDocumentSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
{
required.push_back(AZ_CRC("TargetManagerService", 0x6d5708bc));
required.push_back(AZ_CRC("AssetProcessorToolsConnection", 0x734669bc));
required.push_back(AZ_CRC("AssetDatabaseService", 0x3abf5601));
required.push_back(AZ_CRC("PropertyManagerService", 0x63a3d7ad));
@@ -135,31 +134,16 @@ namespace MaterialEditor
m_documentMap.clear();
MaterialDocumentSystemRequestBus::Handler::BusConnect();
MaterialDocumentNotificationBus::Handler::BusConnect();
AzFramework::TmMsgBus::Handler::BusConnect(AZ_CRC("OpenInMaterialEditor", 0x9f92aac8));
}
void MaterialDocumentSystemComponent::Deactivate()
{
AZ::TickBus::Handler::BusDisconnect();
AzFramework::TmMsgBus::Handler::BusDisconnect();
MaterialDocumentNotificationBus::Handler::BusDisconnect();
MaterialDocumentSystemRequestBus::Handler::BusDisconnect();
m_documentMap.clear();
}
void MaterialDocumentSystemComponent::OnReceivedMsg(AzFramework::TmMsgPtr msg)
{
if (msg->GetId() == AZ_CRC("OpenInMaterialEditor", 0x9f92aac8))
{
const char* documentPath = reinterpret_cast<const char*>(msg->GetCustomBlob());
MaterialDocumentSystemRequestBus::Broadcast(&MaterialDocumentSystemRequestBus::Events::OpenDocument, documentPath);
}
else
{
AZ_Assert(false, "We received a message of an unrecognized class type!");
}
}
AZ::Uuid MaterialDocumentSystemComponent::CreateDocument()
{
auto document = AZStd::make_unique<MaterialDocument>();
@@ -16,7 +16,6 @@
#include <AzCore/Component/TickBus.h>
#include <AzCore/std/smart_ptr/shared_ptr.h>
#include <AzCore/Asset/AssetCommon.h>
#include <AzFramework/TargetManagement/TargetManagementAPI.h>
#include <Atom/Document/MaterialDocumentNotificationBus.h>
#include <Atom/Document/MaterialDocumentSystemRequestBus.h>
@@ -35,7 +34,6 @@ namespace MaterialEditor
class MaterialDocumentSystemComponent
: public AZ::Component
, private AZ::TickBus::Handler
, private AzFramework::TmMsgBus::Handler
, private MaterialDocumentNotificationBus::Handler
, private MaterialDocumentSystemRequestBus::Handler
{
@@ -72,11 +70,6 @@ namespace MaterialEditor
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// AzFramework::TmMsgBus::Handler overrides...
void OnReceivedMsg(AzFramework::TmMsgPtr msg) override;
//////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// MaterialDocumentSystemRequestBus::Handler overrides...
AZ::Uuid CreateDocument() override;
@@ -90,6 +90,14 @@ namespace MaterialEditor
});
}
MaterialEditorApplication::~MaterialEditorApplication()
{
AzToolsFramework::EditorPythonConsoleNotificationBus::Handler::BusDisconnect();
AzToolsFramework::AssetDatabase::AssetDatabaseRequestsBus::Handler::BusDisconnect();
MaterialEditorWindowNotificationBus::Handler::BusDisconnect();
AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
}
void MaterialEditorApplication::CreateReflectionManager()
{
Application::CreateReflectionManager();
@@ -299,12 +307,12 @@ namespace MaterialEditor
return false;
}
void MaterialEditorApplication::ProcessCommandLine()
void MaterialEditorApplication::ProcessCommandLine(const AZ::CommandLine& commandLine)
{
const AZStd::string timeoputSwitchName = "timeout";
if (m_commandLine.HasSwitch(timeoputSwitchName))
if (commandLine.HasSwitch(timeoputSwitchName))
{
const AZStd::string& timeoutValue = m_commandLine.GetSwitchValue(timeoputSwitchName, 0);
const AZStd::string& timeoutValue = commandLine.GetSwitchValue(timeoputSwitchName, 0);
const uint32_t timeoutInMs = atoi(timeoutValue.c_str());
AZ_Printf("MaterialEditor", "Timeout scheduled, shutting down in %u ms", timeoutInMs);
QTimer::singleShot(timeoutInMs, [this] {
@@ -315,10 +323,10 @@ namespace MaterialEditor
// Process command line options for running one or more python scripts on startup
const AZStd::string runPythonScriptSwitchName = "runpython";
size_t runPythonScriptCount = m_commandLine.GetNumSwitchValues(runPythonScriptSwitchName);
size_t runPythonScriptCount = commandLine.GetNumSwitchValues(runPythonScriptSwitchName);
for (size_t runPythonScriptIndex = 0; runPythonScriptIndex < runPythonScriptCount; ++runPythonScriptIndex)
{
const AZStd::string runPythonScriptPath = m_commandLine.GetSwitchValue(runPythonScriptSwitchName, runPythonScriptIndex);
const AZStd::string runPythonScriptPath = commandLine.GetSwitchValue(runPythonScriptSwitchName, runPythonScriptIndex);
AZStd::vector<AZStd::string_view> runPythonArgs;
AZ_Printf("MaterialEditor", "Launching script: %s", runPythonScriptPath.c_str());
@@ -329,17 +337,17 @@ namespace MaterialEditor
}
// Process command line options for opening one or more material documents on startup
size_t openDocumentCount = m_commandLine.GetNumMiscValues();
size_t openDocumentCount = commandLine.GetNumMiscValues();
for (size_t openDocumentIndex = 0; openDocumentIndex < openDocumentCount; ++openDocumentIndex)
{
const AZStd::string openDocumentPath = m_commandLine.GetMiscValue(openDocumentIndex);
const AZStd::string openDocumentPath = commandLine.GetMiscValue(openDocumentIndex);
AZ_Printf("MaterialEditor", "Opening document: %s", openDocumentPath.c_str());
MaterialDocumentSystemRequestBus::Broadcast(&MaterialDocumentSystemRequestBus::Events::OpenDocument, openDocumentPath);
}
const AZStd::string exitAfterCommandsSwitchName = "exitaftercommands";
if (m_commandLine.HasSwitch(exitAfterCommandsSwitchName))
if (commandLine.HasSwitch(exitAfterCommandsSwitchName))
{
ExitMainLoop();
}
@@ -409,9 +417,50 @@ namespace MaterialEditor
bool MaterialEditorApplication::LaunchDiscoveryService()
{
const QStringList arguments = { "-fail_silently" };
// Determine if this is the first launch of the tool by attempting to connect to a running server
if (m_socket.Connect(QApplication::applicationName()))
{
// If the server was located, the application is already running.
// Forward commandline options to other application instance.
QByteArray buffer;
buffer.append("ProcessCommandLine:");
for (int argi = 1; argi < m_argC; ++argi)
{
buffer.append(QString(m_argV[argi]).append("\n").toUtf8());
}
m_socket.Send(buffer);
m_socket.Disconnect();
return false;
}
return AtomToolsFramework::LaunchTool("GridHub", AZ_TRAIT_MATERIALEDITOR_EXT, arguments);
// Setup server to handle basic commands
m_server.SetReadHandler([this](const QByteArray& buffer) {
// Handle commmand line params from connected socket
if (buffer.startsWith("ProcessCommandLine:"))
{
// Remove header and parse commands
AZStd::string params(buffer.data(), buffer.size());
params = params.substr(strlen("ProcessCommandLine:"));
AZStd::vector<AZStd::string> tokens;
AZ::StringFunc::Tokenize(params, tokens, "\n");
if (!tokens.empty())
{
AZ::CommandLine commandLine;
commandLine.Parse(tokens);
ProcessCommandLine(commandLine);
}
}
});
// Launch local server
if (!m_server.Connect(QApplication::applicationName()))
{
return false;
}
return true;
}
void MaterialEditorApplication::StartInternal()
@@ -421,10 +470,14 @@ namespace MaterialEditor
return;
}
//[GFX TODO][ATOM-415] Try to factor out some of this stuff with AtomSampleViewerApplication
WriteStartupLog();
if (!LaunchDiscoveryService())
{
ExitMainLoop();
return;
}
AzToolsFramework::AssetDatabase::AssetDatabaseRequestsBus::Handler::BusConnect();
AzToolsFramework::AssetBrowser::AssetDatabaseLocationNotificationBus::Broadcast(&AzToolsFramework::AssetBrowser::AssetDatabaseLocationNotifications::OnDatabaseInitialized);
@@ -434,8 +487,6 @@ namespace MaterialEditor
LoadSettings();
LaunchDiscoveryService();
MaterialEditorWindowNotificationBus::Handler::BusConnect();
MaterialEditor::MaterialEditorWindowFactoryRequestBus::Broadcast(
@@ -450,7 +501,7 @@ namespace MaterialEditor
}
// Delay execution of commands and scripts post initialization
QTimer::singleShot(0, [this]() { ProcessCommandLine(); });
QTimer::singleShot(0, [this]() { ProcessCommandLine(m_commandLine); });
}
bool MaterialEditorApplication::GetAssetDatabaseLocation(AZStd::string& result)
@@ -12,22 +12,20 @@
#pragma once
#include <Atom/Document/MaterialDocumentSystemRequestBus.h>
#include <Atom/Window/MaterialEditorWindowNotificationBus.h>
#include <AtomToolsFramework/Communication/LocalServer.h>
#include <AtomToolsFramework/Communication/LocalSocket.h>
#include <AzCore/Component/Entity.h>
#include <AzCore/Component/TickBus.h>
#include <AzCore/UserSettings/UserSettingsProvider.h>
#include <AzCore/Debug/TraceMessageBus.h>
#include <AzCore/UserSettings/UserSettingsProvider.h>
#include <AzFramework/Application/Application.h>
#include <AzFramework/Asset/AssetSystemBus.h>
#include <AzFramework/Logging/LogFile.h>
#include <AzToolsFramework/API/AssetDatabaseBus.h>
#include <AzToolsFramework/API/EditorPythonConsoleBus.h>
#include <Atom/Document/MaterialDocumentSystemRequestBus.h>
#include <Atom/Window/MaterialEditorWindowNotificationBus.h>
#include <QApplication>
#include <QTimer>
@@ -51,7 +49,7 @@ namespace MaterialEditor
using Base = AzFramework::Application;
MaterialEditorApplication(int* argc, char*** argv);
virtual ~MaterialEditorApplication() = default;
virtual ~MaterialEditorApplication();
//////////////////////////////////////////////////////////////////////////
// AzFramework::Application
@@ -110,7 +108,7 @@ namespace MaterialEditor
void CompileCriticalAssets();
void ProcessCommandLine();
void ProcessCommandLine(const AZ::CommandLine& commandLine);
void WriteStartupLog();
void LoadSettings();
@@ -138,5 +136,8 @@ namespace MaterialEditor
bool m_activatedLocalUserSettings = false;
QTimer m_timer;
AtomToolsFramework::LocalSocket m_socket;
AtomToolsFramework::LocalServer m_server;
};
} // namespace MaterialEditor
@@ -1,30 +1,31 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#if !defined(Q_MOC_RUN)
#include <MaterialEditorApplication.h>
#include <AzCore/IO/Path/Path.h>
#include <AzCore/Settings/SettingsRegistryMergeUtils.h>
#include <AzQtComponents/Components/StyleManager.h>
#include <AzQtComponents/Utilities/QtPluginPaths.h>
#include <AzQtComponents/Components/GlobalEventFilter.h>
#include <AzQtComponents/Components/StyledDockWidget.h>
#include <AzQtComponents/Components/O3DEStylesheet.h>
#include <AzQtComponents/Utilities/QtPluginPaths.h>
#include <AzQtComponents/Utilities/HandleDpiAwareness.h>
#include <AzQtComponents/Components/StyleManager.h>
#include <AzQtComponents/Components/StyledDockWidget.h>
#include <AzQtComponents/Components/WindowDecorationWrapper.h>
#include <AzQtComponents/Utilities/HandleDpiAwareness.h>
#include <AzQtComponents/Utilities/QtPluginPaths.h>
#include <QtWidgets/QApplication>
#include <QtGui/private/qhighdpiscaling_p.h>
#include <Source/MaterialEditorApplication.h>
#include <QtWidgets/QApplication>
#endif
int main(int argc, char** argv)
{
@@ -45,15 +46,16 @@ int main(int argc, char** argv)
AzQtComponents::Utilities::HandleDpiAwareness(AzQtComponents::Utilities::SystemDpiAware);
MaterialEditor::MaterialEditorApplication app(&argc, &argv);
auto globalEventFilter = new AzQtComponents::GlobalEventFilter(&app);
app.installEventFilter(globalEventFilter);
AZ::IO::FixedMaxPath engineRootPath;
if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
{
settingsRegistry->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
}
auto globalEventFilter = new AzQtComponents::GlobalEventFilter(&app);
app.installEventFilter(globalEventFilter);
AzQtComponents::StyleManager styleManager(&app);
styleManager.initialize(&app, engineRootPath);
@@ -62,3 +64,5 @@ int main(int argc, char** argv)
app.Stop();
return 0;
}
#include "main.moc"