summaryrefslogtreecommitdiff
path: root/Source/Core/WinUpdater/Main.cpp
blob: bcf814c78df1d70d4587b52ead2a9ae12619dc37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <windows.h>
#include <ShlObj.h>
#include <shellapi.h>

#include <optional>
#include <string>
#include <vector>

#include "Common/CommonFuncs.h"
#include "Common/StringUtil.h"

#include "UpdaterCommon/UI.h"
#include "UpdaterCommon/UpdaterCommon.h"

// Refer to docs/autoupdate_overview.md for a detailed overview of the autoupdate process

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
  if (lstrlenW(pCmdLine) == 0)
  {
    MessageBoxW(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;

  auto path = Common::GetModuleName(hInstance);
  if (!path)
  {
    UI::Error("Failed to get updater filename.");
    return 1;
  }

  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);

  if (test_fh == INVALID_HANDLE_VALUE)
    need_admin = true;
  else
    CloseHandle(test_fh);

  if (need_admin)
  {
    if (IsUserAnAdmin())
    {
      MessageBox(nullptr, L"Failed to write to directory despite administrator priviliges.",
                 L"Error", MB_ICONERROR);
      return 1;
    }

    // Relaunch the updater as administrator
    ShellExecuteW(nullptr, L"runas", path->c_str(), pCmdLine, nullptr, SW_SHOW);
    return 0;
  }

  std::vector<std::string> args = Common::CommandLineToUtf8Argv(pCmdLine);

  return RunUpdater(args) ? 0 : 1;
}