summaryrefslogtreecommitdiff
path: root/Source/Core/WinUpdater/Main.cpp
diff options
context:
space:
mode:
authorspycrab <spycrab@users.noreply.github.com>2019-04-10 10:46:49 +0200
committerspycrab <spycrab@users.noreply.github.com>2019-04-10 10:46:49 +0200
commitd73987e466b734a837253b50c5d650322ec02662 (patch)
tree41e1681a11a311e59d2e033ac81ac55c7f6af734 /Source/Core/WinUpdater/Main.cpp
parent1f6c67a6fb26c873a72c5d33367d9be14f7c028e (diff)
Move Updater to WinUpdater
Diffstat (limited to 'Source/Core/WinUpdater/Main.cpp')
-rw-r--r--Source/Core/WinUpdater/Main.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/Source/Core/WinUpdater/Main.cpp b/Source/Core/WinUpdater/Main.cpp
new file mode 100644
index 0000000000..0cb09cb966
--- /dev/null
+++ b/Source/Core/WinUpdater/Main.cpp
@@ -0,0 +1,82 @@
+// Copyright 2018 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include <windows.h>
+#include <ShlObj.h>
+#include <shellapi.h>
+
+#include <string>
+#include <vector>
+
+#include "Common/StringUtil.h"
+
+#include "UpdaterCommon/UI.h"
+#include "UpdaterCommon/UpdaterCommon.h"
+
+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;
+}
+}; // namespace
+
+int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
+{
+ 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;
+ }
+
+ // Test for write permissions
+ bool need_admin = false;
+
+ FILE* test_fh = fopen("Updater.log", "w");
+
+ if (test_fh == nullptr)
+ need_admin = true;
+ else
+ fclose(test_fh);
+
+ if (need_admin)
+ {
+ if (IsUserAnAdmin())
+ {
+ MessageBox(nullptr, L"Failed to write to directory despite administrator priviliges.",
+ L"Error", MB_ICONERROR);
+ return 1;
+ }
+
+ wchar_t path[MAX_PATH];
+ if (GetModuleFileName(hInstance, path, sizeof(path)) == 0)
+ {
+ MessageBox(nullptr, L"Failed to get updater filename.", L"Error", MB_ICONERROR);
+ return 1;
+ }
+
+ // Relaunch the updater as administrator
+ ShellExecuteW(nullptr, L"runas", path, pCmdLine, NULL, SW_SHOW);
+ return 0;
+ }
+
+ std::vector<std::string> args = CommandLineToUtf8Argv(pCmdLine);
+
+ return RunUpdater(args) ? 0 : 1;
+}