Skip to content

Commit

Permalink
[Workspaces] Fix restart launcher when elevated (#35064)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeraphimaZykova authored Sep 25, 2024
1 parent cf5adda commit 4240a7c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 64 deletions.
72 changes: 27 additions & 45 deletions src/modules/Workspaces/WorkspacesLauncher/main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#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>
#include <common/utils/process_path.h>
#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"";

Expand All @@ -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");
Expand All @@ -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;
}

Expand All @@ -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())
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/modules/Workspaces/WorkspacesLib/LaunchingStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ bool LaunchingStatus::AllLaunchedAndMoved() noexcept
{
if (data.state != LaunchingState::Failed && data.state != LaunchingState::LaunchedAndMoved)
{
Logger::debug(data.state);
return false;
}
}
Expand Down
40 changes: 37 additions & 3 deletions src/modules/Workspaces/WorkspacesLib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
20 changes: 5 additions & 15 deletions src/modules/Workspaces/WorkspacesWindowArranger/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 4240a7c

Please sign in to comment.