2018-03-17 17:45:45 -06:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-03-17 17:45:45 -06:00
|
|
|
|
|
|
|
#include <windows.h>
|
2018-04-25 14:38:10 -06:00
|
|
|
#include <ShlObj.h>
|
2018-03-17 17:45:45 -06:00
|
|
|
#include <shellapi.h>
|
2019-03-03 05:56:54 -07:00
|
|
|
|
2019-07-04 13:33:11 -06:00
|
|
|
#include <optional>
|
2019-02-25 15:08:03 -07:00
|
|
|
#include <string>
|
2018-03-17 17:45:45 -06:00
|
|
|
#include <vector>
|
|
|
|
|
2019-10-06 14:17:00 -06:00
|
|
|
#include "Common/CommonFuncs.h"
|
2019-03-03 05:56:54 -07:00
|
|
|
#include "Common/StringUtil.h"
|
2018-03-17 17:45:45 -06:00
|
|
|
|
2019-02-25 15:08:03 -07:00
|
|
|
#include "UpdaterCommon/UI.h"
|
|
|
|
#include "UpdaterCommon/UpdaterCommon.h"
|
2018-03-27 16:28:40 -06:00
|
|
|
|
2021-03-12 18:10:53 -07:00
|
|
|
// Refer to docs/autoupdate_overview.md for a detailed overview of the autoupdate process
|
|
|
|
|
2018-03-17 17:45:45 -06:00
|
|
|
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
|
|
|
|
{
|
2018-05-05 15:07:39 -06:00
|
|
|
if (lstrlenW(pCmdLine) == 0)
|
|
|
|
{
|
2022-09-11 00:21:12 -06:00
|
|
|
MessageBoxW(nullptr,
|
|
|
|
L"This updater is not meant to be launched directly. Configure Auto-Update in "
|
|
|
|
"Dolphin's settings instead.",
|
|
|
|
L"Error", MB_ICONERROR);
|
2018-05-05 15:07:39 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-03 05:56:54 -07:00
|
|
|
// Test for write permissions
|
2018-04-25 14:38:10 -06:00
|
|
|
bool need_admin = false;
|
|
|
|
|
2022-11-15 18:41:25 -07:00
|
|
|
auto path = GetModuleName(hInstance);
|
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
UI::Error("Failed to get updater filename.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:04 -07:00
|
|
|
const auto test_fh = ::CreateFileW(
|
|
|
|
(std::filesystem::path(*path).parent_path() / "directory_writable_check.tmp").c_str(),
|
|
|
|
GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr);
|
2018-03-17 17:45:45 -06:00
|
|
|
|
2022-11-21 17:36:04 -07:00
|
|
|
if (test_fh == INVALID_HANDLE_VALUE)
|
2019-03-03 05:56:54 -07:00
|
|
|
need_admin = true;
|
|
|
|
else
|
2022-11-21 17:36:04 -07:00
|
|
|
CloseHandle(test_fh);
|
2018-03-17 17:45:45 -06:00
|
|
|
|
2018-04-25 14:38:10 -06:00
|
|
|
if (need_admin)
|
|
|
|
{
|
|
|
|
if (IsUserAnAdmin())
|
|
|
|
{
|
2019-03-03 05:56:54 -07:00
|
|
|
MessageBox(nullptr, L"Failed to write to directory despite administrator priviliges.",
|
|
|
|
L"Error", MB_ICONERROR);
|
2018-04-25 14:38:10 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relaunch the updater as administrator
|
2019-07-04 13:33:11 -06:00
|
|
|
ShellExecuteW(nullptr, L"runas", path->c_str(), pCmdLine, NULL, SW_SHOW);
|
2018-04-25 14:38:10 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2018-03-27 16:28:40 -06:00
|
|
|
|
2019-03-03 05:56:54 -07:00
|
|
|
std::vector<std::string> args = CommandLineToUtf8Argv(pCmdLine);
|
2018-03-27 16:28:40 -06:00
|
|
|
|
2019-03-03 05:56:54 -07:00
|
|
|
return RunUpdater(args) ? 0 : 1;
|
2018-03-17 17:45:45 -06:00
|
|
|
}
|