2018-03-17 23:45:45 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <windows.h>
|
2018-04-25 20:38:10 +00:00
|
|
|
#include <ShlObj.h>
|
2018-03-17 23:45:45 +00:00
|
|
|
#include <shellapi.h>
|
2019-03-03 12:56:54 +00:00
|
|
|
|
2019-02-25 22:08:03 +00:00
|
|
|
#include <string>
|
2018-03-17 23:45:45 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-03-03 12:56:54 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2018-03-17 23:45:45 +00:00
|
|
|
|
2019-02-25 22:08:03 +00:00
|
|
|
#include "UpdaterCommon/UI.h"
|
|
|
|
#include "UpdaterCommon/UpdaterCommon.h"
|
2018-03-27 22:28:40 +00:00
|
|
|
|
2018-03-17 23:45:45 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::vector<std::string> CommandLineToUtf8Argv(PCWSTR command_line)
|
|
|
|
{
|
|
|
|
int nargs;
|
|
|
|
LPWSTR* tokenized = CommandLineToArgvW(command_line, &nargs);
|
|
|
|
if (!tokenized)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<std::string> argv(nargs);
|
|
|
|
for (int i = 0; i < nargs; ++i)
|
|
|
|
{
|
|
|
|
argv[i] = UTF16ToUTF8(tokenized[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalFree(tokenized);
|
|
|
|
return argv;
|
|
|
|
}
|
2019-02-25 22:08:03 +00:00
|
|
|
}; // namespace
|
2018-03-17 23:45:45 +00:00
|
|
|
|
|
|
|
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
|
|
|
|
{
|
2018-05-05 21:07:39 +00:00
|
|
|
if (lstrlenW(pCmdLine) == 0)
|
|
|
|
{
|
|
|
|
MessageBox(nullptr,
|
|
|
|
L"This updater is not meant to be launched directly. Configure Auto-Update in "
|
|
|
|
"Dolphin's settings instead.",
|
|
|
|
L"Error", MB_ICONERROR);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-03 12:56:54 +00:00
|
|
|
// Test for write permissions
|
2018-04-25 20:38:10 +00:00
|
|
|
bool need_admin = false;
|
|
|
|
|
2019-03-03 12:56:54 +00:00
|
|
|
FILE* test_fh = fopen("Updater.log", "w");
|
2018-03-17 23:45:45 +00:00
|
|
|
|
2019-03-03 12:56:54 +00:00
|
|
|
if (test_fh == nullptr)
|
|
|
|
need_admin = true;
|
|
|
|
else
|
|
|
|
fclose(test_fh);
|
2018-03-17 23:45:45 +00:00
|
|
|
|
2018-04-25 20:38:10 +00:00
|
|
|
if (need_admin)
|
|
|
|
{
|
|
|
|
if (IsUserAnAdmin())
|
|
|
|
{
|
2019-03-03 12:56:54 +00:00
|
|
|
MessageBox(nullptr, L"Failed to write to directory despite administrator priviliges.",
|
|
|
|
L"Error", MB_ICONERROR);
|
2018-04-25 20:38:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t path[MAX_PATH];
|
|
|
|
if (GetModuleFileName(hInstance, path, sizeof(path)) == 0)
|
|
|
|
{
|
2019-03-03 12:56:54 +00:00
|
|
|
MessageBox(nullptr, L"Failed to get updater filename.", L"Error", MB_ICONERROR);
|
2018-04-25 20:38:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relaunch the updater as administrator
|
|
|
|
ShellExecuteW(nullptr, L"runas", path, pCmdLine, NULL, SW_SHOW);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-27 22:28:40 +00:00
|
|
|
|
2019-03-03 12:56:54 +00:00
|
|
|
std::vector<std::string> args = CommandLineToUtf8Argv(pCmdLine);
|
2018-03-27 22:28:40 +00:00
|
|
|
|
2019-03-03 12:56:54 +00:00
|
|
|
return RunUpdater(args) ? 0 : 1;
|
2018-03-17 23:45:45 +00:00
|
|
|
}
|