[Workspaces] Fix restart launcher when elevated (#35064)

This commit is contained in:
Seraphima Zykova 2024-09-25 18:56:31 +03:00 committed by GitHub
parent cf5addab28
commit 4240a7cee0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 69 additions and 64 deletions

View File

@ -1,12 +1,5 @@
#include "pch.h"
#include <WorkspacesLib/JsonUtils.h>
#include <WorkspacesLib/utils.h>
#include <Launcher.h>
#include <Generated Files/resource.h>
#include <common/utils/elevation.h>
#include <common/utils/gpo.h>
#include <common/utils/logger_helper.h>
@ -14,6 +7,13 @@
#include <common/utils/UnhandledExceptionHandler.h>
#include <common/utils/resources.h>
#include <WorkspacesLib/JsonUtils.h>
#include <WorkspacesLib/utils.h>
#include <Launcher.h>
#include <Generated Files/resource.h>
const std::wstring moduleName = L"Workspaces\\WorkspacesLauncher";
const std::wstring internalPath = L"";
@ -28,6 +28,15 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
return 0;
}
std::wstring cmdLineStr{ GetCommandLineW() };
auto cmdArgs = split(cmdLineStr, L" ");
if (cmdArgs.workspaceId.empty())
{
Logger::warn("Incorrect command line arguments: no workspace id");
MessageBox(NULL, GET_RESOURCE_STRING(IDS_INCORRECT_ARGS).c_str(), GET_RESOURCE_STRING(IDS_WORKSPACES).c_str(), MB_ICONERROR | MB_OK);
return 1;
}
if (is_process_elevated())
{
Logger::warn("Workspaces Launcher is elevated, restart");
@ -41,7 +50,9 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
std::string cmdLineStr(cmdline);
std::wstring cmdLineWStr(cmdLineStr.begin(), cmdLineStr.end());
run_non_elevated(exe_path.get(), cmdLineWStr, nullptr, modulePath.c_str());
std::wstring cmd = cmdArgs.workspaceId + L" " + std::to_wstring(cmdArgs.invokePoint);
RunNonElevatedEx(exe_path.get(), cmd, modulePath);
return 1;
}
@ -54,50 +65,21 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
std::wstring cmdLineStr{ GetCommandLineW() };
auto cmdArgs = split(cmdLineStr, L" ");
if (cmdArgs.size() < 2)
{
Logger::warn("Incorrect command line arguments");
MessageBox(NULL, GET_RESOURCE_STRING(IDS_INCORRECT_ARGS).c_str(), GET_RESOURCE_STRING(IDS_WORKSPACES).c_str(), MB_ICONERROR | MB_OK);
return 1;
}
std::wstring id(cmdArgs[1].begin(), cmdArgs[1].end());
if (id.empty())
{
Logger::warn("Incorrect command line arguments: no workspace id");
MessageBox(NULL, GET_RESOURCE_STRING(IDS_INCORRECT_ARGS).c_str(), GET_RESOURCE_STRING(IDS_WORKSPACES).c_str(), MB_ICONERROR | MB_OK);
return 1;
}
InvokePoint invokePoint = InvokePoint::EditorButton;
if (cmdArgs.size() > 2)
{
try
{
invokePoint = static_cast<InvokePoint>(std::stoi(cmdArgs[2]));
}
catch (std::exception)
{
}
}
Logger::trace(L"Invoke point: {}", invokePoint);
Logger::trace(L"Invoke point: {}", cmdArgs.invokePoint);
// read workspaces
std::vector<WorkspacesData::WorkspacesProject> workspaces;
WorkspacesData::WorkspacesProject projectToLaunch{};
if (invokePoint == InvokePoint::LaunchAndEdit)
if (cmdArgs.invokePoint == InvokePoint::LaunchAndEdit)
{
// check the temp file in case the project is just created and not saved to the workspaces.json yet
auto file = WorkspacesData::TempWorkspacesFile();
auto res = JsonUtils::ReadSingleWorkspace(file);
if (res.isOk() && projectToLaunch.id == id)
if (res.isOk() && projectToLaunch.id == cmdArgs.workspaceId)
{
projectToLaunch = res.getValue();
}
else
else if (res.isError())
{
std::wstring formattedMessage{};
switch (res.error())
@ -150,7 +132,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
for (const auto& proj : workspaces)
{
if (proj.id == id)
if (proj.id == cmdArgs.workspaceId)
{
projectToLaunch = proj;
break;
@ -160,13 +142,13 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
if (projectToLaunch.id.empty())
{
Logger::critical(L"Workspace {} not found", id);
std::wstring formattedMessage = fmt::format(GET_RESOURCE_STRING(IDS_PROJECT_NOT_FOUND), id);
Logger::critical(L"Workspace {} not found", cmdArgs.workspaceId);
std::wstring formattedMessage = fmt::format(GET_RESOURCE_STRING(IDS_PROJECT_NOT_FOUND), cmdArgs.workspaceId);
MessageBox(NULL, formattedMessage.c_str(), GET_RESOURCE_STRING(IDS_WORKSPACES).c_str(), MB_ICONERROR | MB_OK);
return 1;
}
Launcher launcher(projectToLaunch, workspaces, invokePoint);
Launcher launcher(projectToLaunch, workspaces, cmdArgs.invokePoint);
Logger::trace("Finished");
CoUninitialize();

View File

@ -26,7 +26,6 @@ bool LaunchingStatus::AllLaunchedAndMoved() noexcept
{
if (data.state != LaunchingState::Failed && data.state != LaunchingState::LaunchedAndMoved)
{
Logger::debug(data.state);
return false;
}
}

View File

@ -3,11 +3,22 @@
#include <vector>
#include <string>
std::vector<std::wstring> split(std::wstring s, const std::wstring& delimiter)
#include <workspaces-common/GuidUtils.h>
#include <workspaces-common/InvokePoint.h>
struct CommandLineArgs
{
std::vector<std::wstring> tokens;
std::wstring workspaceId;
InvokePoint invokePoint;
};
CommandLineArgs split(std::wstring s, const std::wstring& delimiter)
{
CommandLineArgs cmdArgs{};
size_t pos = 0;
std::wstring token;
std::vector<std::wstring> tokens;
while ((pos = s.find(delimiter)) != std::wstring::npos)
{
token = s.substr(0, pos);
@ -16,5 +27,28 @@ std::vector<std::wstring> split(std::wstring s, const std::wstring& delimiter)
}
tokens.push_back(s);
return tokens;
for (const auto& token : tokens)
{
if (!cmdArgs.workspaceId.empty())
{
try
{
auto invokePoint = static_cast<InvokePoint>(std::stoi(token));
cmdArgs.invokePoint = invokePoint;
}
catch (std::exception)
{
}
}
else
{
auto guid = GuidFromString(token);
if (guid.has_value())
{
cmdArgs.workspaceId = token;
}
}
}
return cmdArgs;
}

View File

@ -35,17 +35,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
}
auto args = split(commandLine, L" ");
std::wstring id{};
if (args.size() == 1)
{
id = args[0];
}
else if (args.size() == 2)
{
id = args[1];
}
if (id.empty())
if (args.workspaceId.empty())
{
Logger::warn("Incorrect command line arguments: no workspace id");
return 1;
@ -60,11 +50,11 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
{
auto file = WorkspacesData::TempWorkspacesFile();
auto res = JsonUtils::ReadSingleWorkspace(file);
if (res.isOk() && res.value().id == id)
if (res.isOk() && res.value().id == args.workspaceId)
{
projectToLaunch = res.getValue();
}
else
else if (res.isError())
{
Logger::error(L"Error reading temp file");
return 1;
@ -86,7 +76,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
for (const auto& proj : workspaces)
{
if (proj.id == id)
if (proj.id == args.workspaceId)
{
projectToLaunch = proj;
break;
@ -96,7 +86,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cm
if (projectToLaunch.id.empty())
{
Logger::critical(L"Workspace {} not found", id);
Logger::critical(L"Workspace {} not found", args.workspaceId);
return 1;
}