Asks Garage for the newest release of a project, downloads it with resume, checks the sha256, installs it beside the old one and restarts into it. Splash on garage_ui, a native stub and a first time installer for windows, bundle swap on mac, the system installer on android. No-op on web. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KHotisA4hV2xmuZsHTksFX
1108 lines
40 KiB
C++
1108 lines
40 KiB
C++
// the first-time installer. one exe, nothing beside it: the stub rides along
|
|
// as a resource and everything else comes from garage.
|
|
//
|
|
// asks who its for (if the developer left that on), asks garage what the
|
|
// newest windows build is, downloads it, checks the sha256, puts the stub in
|
|
// <root> and has the stub unpack the zip, so theres one bit of code that knows
|
|
// how a version folder is made. then a start menu shortcut, an entry in
|
|
// "installed apps", and the app is started.
|
|
//
|
|
// who its for decides where it goes:
|
|
// just me %LOCALAPPDATA%\Programs\<AppName>, nothing needs admin, ever
|
|
// everyone %ProgramFiles%\<AppName>, and we start ourselves again elevated
|
|
//
|
|
// the window is a task dialog. windows lays it out and draws it, theres no
|
|
// layout code in here to get wrong on somebody elses dpi.
|
|
|
|
#include "garage_common.h"
|
|
#include "installer_config.h"
|
|
|
|
#include <winhttp.h>
|
|
#include <bcrypt.h>
|
|
#include <commctrl.h>
|
|
#include <shellapi.h>
|
|
#include <shlobj.h>
|
|
#include <objbase.h>
|
|
|
|
using namespace garage;
|
|
|
|
enum { kExitOk = 0, kExitFailed = 1, kExitCancelled = 2 };
|
|
|
|
enum { ID_INSTALL = 1001 };
|
|
enum { RADIO_ME = 2001, RADIO_EVERYONE = 2002 };
|
|
enum Page { PAGE_CHOICE, PAGE_PROGRESS, PAGE_ERROR };
|
|
|
|
static const wchar_t* kAppName = GARAGE_APP_NAME;
|
|
static const wchar_t* kPublisher = GARAGE_PUBLISHER;
|
|
static const wchar_t* kUserAgent = L"garage_updates-installer/1";
|
|
|
|
static bool g_everyone = GARAGE_DEFAULT_EVERYONE != 0;
|
|
static bool g_showChoice = GARAGE_SHOW_CHOICE != 0;
|
|
static bool g_desktopShortcut = GARAGE_DESKTOP_SHORTCUT != 0;
|
|
// started by ourselves through the uac prompt. the one that asked is still
|
|
// there waiting, and its the one that starts the app afterwards
|
|
static bool g_elevatedChild = false;
|
|
|
|
static Page g_page = PAGE_CHOICE;
|
|
static int g_exit = kExitCancelled;
|
|
static HANDLE g_child = nullptr;
|
|
static HICON g_icon = nullptr;
|
|
|
|
static HANDLE g_worker = nullptr;
|
|
static volatile LONG g_cancel = 0;
|
|
static bool g_marquee = false;
|
|
static std::wstring g_shown;
|
|
|
|
// what the worker thread has to say to the window. the window reads it on a timer
|
|
static CRITICAL_SECTION g_lock;
|
|
static struct {
|
|
std::wstring status;
|
|
int permille = -1; // -1 is "no idea how long", the bar just slides
|
|
bool done = false;
|
|
bool ok = false;
|
|
std::wstring error;
|
|
} g_progress;
|
|
|
|
static std::wstring g_logDir;
|
|
|
|
|
|
// ---- small things ----
|
|
|
|
static void log_line(const std::wstring& msg) {
|
|
OutputDebugStringW((L"garage_setup: " + msg + L"\n").c_str());
|
|
if (g_logDir.empty()) return;
|
|
|
|
SYSTEMTIME t;
|
|
GetLocalTime(&t);
|
|
wchar_t stamp[48];
|
|
wsprintfW(stamp, L"%04d-%02d-%02d %02d:%02d:%02d ", t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond);
|
|
std::string bytes = narrow(stamp + msg) + "\r\n";
|
|
|
|
HANDLE h = CreateFileW(lp(join(g_logDir, L"setup.log")).c_str(), FILE_APPEND_DATA,
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
OutputDebugStringW(L"garage_setup: and the log couldnt be opened\n");
|
|
return;
|
|
}
|
|
DWORD wrote = 0;
|
|
WriteFile(h, bytes.data(), (DWORD)bytes.size(), &wrote, nullptr);
|
|
CloseHandle(h);
|
|
}
|
|
|
|
static void log_fail(const std::wstring& what, DWORD err) {
|
|
log_line(what + L" (win32 " + win_error(err) + L")");
|
|
}
|
|
|
|
static bool cancelled() {
|
|
return InterlockedCompareExchange(&g_cancel, 0, 0) != 0;
|
|
}
|
|
|
|
static void set_status(const std::wstring& status, int permille) {
|
|
EnterCriticalSection(&g_lock);
|
|
g_progress.status = status;
|
|
g_progress.permille = permille;
|
|
LeaveCriticalSection(&g_lock);
|
|
}
|
|
|
|
static std::wstring shell_folder(int csidl) {
|
|
wchar_t buf[MAX_PATH];
|
|
HRESULT hr = SHGetFolderPathW(nullptr, csidl | CSIDL_FLAG_CREATE, nullptr, SHGFP_TYPE_CURRENT, buf);
|
|
if (FAILED(hr)) {
|
|
log_fail(L"couldnt find shell folder " + num(csidl), (DWORD)hr);
|
|
return L"";
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
// the app name is a folder name and a shortcut name as well
|
|
static std::wstring file_safe(const std::wstring& name) {
|
|
std::wstring out;
|
|
for (size_t i = 0; i < name.size(); i++) {
|
|
wchar_t c = name[i];
|
|
if (c < 32 || wcschr(L"\\/:*?\"<>|", c)) continue;
|
|
out += c;
|
|
}
|
|
while (!out.empty() && (out[out.size() - 1] == L' ' || out[out.size() - 1] == L'.')) out.resize(out.size() - 1);
|
|
return out.empty() ? std::wstring(L"App") : out;
|
|
}
|
|
|
|
static std::wstring install_root(bool everyone) {
|
|
std::wstring base = everyone ? shell_folder(CSIDL_PROGRAM_FILES)
|
|
: join(shell_folder(CSIDL_LOCAL_APPDATA), L"Programs");
|
|
return join(base, file_safe(kAppName));
|
|
}
|
|
|
|
static bool is_admin() {
|
|
HANDLE token = nullptr;
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) return false;
|
|
TOKEN_ELEVATION info = {};
|
|
DWORD size = 0;
|
|
BOOL ok = GetTokenInformation(token, TokenElevation, &info, sizeof(info), &size);
|
|
CloseHandle(token);
|
|
return ok && info.TokenIsElevated != 0;
|
|
}
|
|
|
|
// the machine, not us. an x64 build of this runs fine on an arm64 pc and
|
|
// should still ask for the arm64 app
|
|
static const wchar_t* native_arch() {
|
|
typedef BOOL (WINAPI *Wow64Fn)(HANDLE, USHORT*, USHORT*);
|
|
HMODULE k32 = GetModuleHandleW(L"kernel32.dll");
|
|
Wow64Fn fn = k32 ? (Wow64Fn)(void*)GetProcAddress(k32, "IsWow64Process2") : nullptr;
|
|
USHORT process = 0, machine = 0;
|
|
if (fn && fn(GetCurrentProcess(), &process, &machine)) {
|
|
return machine == 0xAA64 ? L"arm64" : L"x64";
|
|
}
|
|
#if defined(_M_ARM64) || defined(__aarch64__)
|
|
return L"arm64";
|
|
#else
|
|
return L"x64";
|
|
#endif
|
|
}
|
|
|
|
|
|
// ---- http ----
|
|
|
|
struct Http {
|
|
HINTERNET session = nullptr;
|
|
HINTERNET conn = nullptr;
|
|
HINTERNET req = nullptr;
|
|
DWORD status = 0;
|
|
};
|
|
|
|
static void http_end(Http& h) {
|
|
DWORD keep = GetLastError();
|
|
if (h.req) WinHttpCloseHandle(h.req);
|
|
if (h.conn) WinHttpCloseHandle(h.conn);
|
|
if (h.session) WinHttpCloseHandle(h.session);
|
|
h = Http();
|
|
SetLastError(keep);
|
|
}
|
|
|
|
// sends a GET and reads as far as the status. redirects are followed by
|
|
// winhttp, and it carries the Range header along to wherever they lead
|
|
static bool http_begin(Http& h, const std::wstring& url, long long from) {
|
|
URL_COMPONENTS u = {};
|
|
u.dwStructSize = sizeof(u);
|
|
u.dwSchemeLength = (DWORD)-1;
|
|
u.dwHostNameLength = (DWORD)-1;
|
|
u.dwUrlPathLength = (DWORD)-1;
|
|
u.dwExtraInfoLength = (DWORD)-1;
|
|
if (!WinHttpCrackUrl(url.c_str(), 0, 0, &u)) return false;
|
|
|
|
std::wstring host(u.lpszHostName, u.dwHostNameLength);
|
|
std::wstring path(u.lpszUrlPath, u.dwUrlPathLength);
|
|
if (u.lpszExtraInfo) path.append(u.lpszExtraInfo, u.dwExtraInfoLength);
|
|
if (path.empty()) path = L"/";
|
|
bool secure = u.nScheme == INTERNET_SCHEME_HTTPS;
|
|
|
|
h.session = WinHttpOpen(kUserAgent, WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME,
|
|
WINHTTP_NO_PROXY_BYPASS, 0);
|
|
if (!h.session) {
|
|
// windows before 8.1 doesnt know that one
|
|
h.session = WinHttpOpen(kUserAgent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME,
|
|
WINHTTP_NO_PROXY_BYPASS, 0);
|
|
}
|
|
if (!h.session) return false;
|
|
|
|
DWORD tls = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2;
|
|
#ifdef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
|
|
DWORD newer = tls | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3;
|
|
if (!WinHttpSetOption(h.session, WINHTTP_OPTION_SECURE_PROTOCOLS, &newer, sizeof(newer)))
|
|
#endif
|
|
{
|
|
if (!WinHttpSetOption(h.session, WINHTTP_OPTION_SECURE_PROTOCOLS, &tls, sizeof(tls))) {
|
|
log_fail(L"couldnt pick the tls versions, using what windows defaults to", GetLastError());
|
|
}
|
|
}
|
|
WinHttpSetTimeouts(h.session, 15000, 15000, 30000, 30000);
|
|
|
|
h.conn = WinHttpConnect(h.session, host.c_str(), u.nPort, 0);
|
|
if (!h.conn) { http_end(h); return false; }
|
|
|
|
h.req = WinHttpOpenRequest(h.conn, L"GET", path.c_str(), nullptr, WINHTTP_NO_REFERER,
|
|
WINHTTP_DEFAULT_ACCEPT_TYPES, secure ? WINHTTP_FLAG_SECURE : 0);
|
|
if (!h.req) { http_end(h); return false; }
|
|
|
|
if (from > 0) {
|
|
std::wstring range = L"Range: bytes=" + num(from) + L"-";
|
|
if (!WinHttpAddRequestHeaders(h.req, range.c_str(), (DWORD)-1, WINHTTP_ADDREQ_FLAG_ADD)) {
|
|
http_end(h);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!WinHttpSendRequest(h.req, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0) ||
|
|
!WinHttpReceiveResponse(h.req, nullptr)) {
|
|
http_end(h);
|
|
return false;
|
|
}
|
|
|
|
DWORD size = sizeof(h.status);
|
|
if (!WinHttpQueryHeaders(h.req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
|
|
WINHTTP_HEADER_NAME_BY_INDEX, &h.status, &size, WINHTTP_NO_HEADER_INDEX)) {
|
|
http_end(h);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static std::wstring http_header(Http& h, DWORD which) {
|
|
wchar_t buf[256];
|
|
DWORD size = sizeof(buf);
|
|
if (!WinHttpQueryHeaders(h.req, which, WINHTTP_HEADER_NAME_BY_INDEX, buf, &size, WINHTTP_NO_HEADER_INDEX)) {
|
|
return L"";
|
|
}
|
|
return std::wstring(buf, size / sizeof(wchar_t));
|
|
}
|
|
|
|
static bool http_text(const std::wstring& url, DWORD& status, std::string& body) {
|
|
Http h;
|
|
if (!http_begin(h, url, 0)) return false;
|
|
status = h.status;
|
|
body.clear();
|
|
char chunk[8192];
|
|
for (;;) {
|
|
DWORD got = 0;
|
|
if (!WinHttpReadData(h.req, chunk, sizeof(chunk), &got)) {
|
|
http_end(h);
|
|
return false;
|
|
}
|
|
if (got == 0) break;
|
|
body.append(chunk, got);
|
|
// its a few hundred bytes of json. anything like this isnt it
|
|
if (body.size() > 1024 * 1024) break;
|
|
}
|
|
http_end(h);
|
|
return true;
|
|
}
|
|
|
|
|
|
// ---- the release ----
|
|
|
|
struct Release {
|
|
long long build = 0;
|
|
std::wstring label;
|
|
std::wstring url;
|
|
std::wstring sha256;
|
|
long long size = 0;
|
|
};
|
|
|
|
static bool ask_garage(const wchar_t* arch, Release& out, bool& none, std::wstring& error) {
|
|
none = false;
|
|
std::wstring url = std::wstring(GARAGE_API_BASE) + L"/v1/updates/" + GARAGE_PROJECT_ID +
|
|
L"?platform=windows&arch=" + arch;
|
|
DWORD status = 0;
|
|
std::string body;
|
|
if (!http_text(url, status, body)) {
|
|
DWORD err = GetLastError();
|
|
log_fail(L"couldnt ask " + url, err);
|
|
error = L"Could not reach the download server. Check your internet connection.\n\n(" + win_error(err) + L")";
|
|
return false;
|
|
}
|
|
|
|
JVal json;
|
|
bool parsed = json_parse(body, json) && json.kind == JVal::Obj;
|
|
if (status != 200 || !parsed) {
|
|
std::wstring said = parsed ? widen(json.str("message")) : L"";
|
|
log_line(url + L" answered " + num(status) + L": " + widen(body.substr(0, 400)));
|
|
error = L"The download server gave an answer that makes no sense (" + num(status) + L").";
|
|
if (!said.empty()) error += L"\n\n" + said;
|
|
return false;
|
|
}
|
|
|
|
const JVal* latest = json.get("latest");
|
|
const JVal* asset = latest ? latest->get("asset") : nullptr;
|
|
if (!latest || latest->kind != JVal::Obj || !asset || asset->kind != JVal::Obj) {
|
|
none = true;
|
|
return false;
|
|
}
|
|
|
|
out.build = latest->number("build", 0);
|
|
out.label = widen(latest->str("label"));
|
|
out.url = widen(asset->str("url"));
|
|
out.sha256 = widen(asset->str("sha256"));
|
|
out.size = asset->number("size", 0);
|
|
std::string format = asset->str("format");
|
|
|
|
if (format != "zip") {
|
|
log_line(L"the windows asset of build " + num(out.build) + L" is a " + widen(format) + L", it has to be a zip");
|
|
error = L"The newest release is not in a form this installer can use.";
|
|
return false;
|
|
}
|
|
bool http = out.url.compare(0, 8, L"https://") == 0 || out.url.compare(0, 7, L"http://") == 0;
|
|
if (out.build <= 0 || !http || out.sha256.size() != 64) {
|
|
log_line(L"the release that came back is missing things: " + widen(body.substr(0, 400)));
|
|
error = L"The download server gave an answer that makes no sense.";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// one go at it. true is "the whole file is there". false with `again` set is
|
|
// "ask once more, itll carry on from where this got to"
|
|
static bool download_once(const Release& rel, const std::wstring& part, bool& again, std::wstring& error) {
|
|
again = false;
|
|
long long have = file_size(part);
|
|
if (have < 0) have = 0;
|
|
if (rel.size > 0 && have == rel.size) return true;
|
|
if (rel.size > 0 && have > rel.size) {
|
|
// not ours, or a different build that got the same name
|
|
DeleteFileW(lp(part).c_str());
|
|
have = 0;
|
|
}
|
|
|
|
Http h;
|
|
if (!http_begin(h, rel.url, have)) {
|
|
DWORD err = GetLastError();
|
|
log_fail(L"couldnt start the download", err);
|
|
error = L"The download could not be started. Check your internet connection.\n\n(" + win_error(err) + L")";
|
|
again = true;
|
|
return false;
|
|
}
|
|
|
|
bool append = false;
|
|
if (h.status == 206 && have > 0) {
|
|
// "bytes 1000-1999/2000". if it isnt carrying on from where we are, start over
|
|
std::wstring range = http_header(h, WINHTTP_QUERY_CONTENT_RANGE);
|
|
std::wstring want = L"bytes " + num(have) + L"-";
|
|
append = range.compare(0, want.size(), want) == 0;
|
|
if (!append) {
|
|
log_line(L"asked to resume at " + num(have) + L" and got \"" + range + L"\", starting again");
|
|
http_end(h);
|
|
DeleteFileW(lp(part).c_str());
|
|
again = true;
|
|
return false;
|
|
}
|
|
} else if (h.status == 416) {
|
|
// what we have is longer than the file is
|
|
http_end(h);
|
|
DeleteFileW(lp(part).c_str());
|
|
again = true;
|
|
return false;
|
|
} else if (h.status != 200) {
|
|
log_line(L"the download answered " + num(h.status));
|
|
error = L"The download server refused the download (" + num(h.status) + L").";
|
|
again = h.status >= 500;
|
|
http_end(h);
|
|
return false;
|
|
} else {
|
|
// a plain 200 is the whole file from the top, whatever we asked for
|
|
have = 0;
|
|
}
|
|
|
|
HANDLE file = CreateFileW(lp(part).c_str(), GENERIC_WRITE, FILE_SHARE_READ, nullptr,
|
|
append ? OPEN_ALWAYS : CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
if (file == INVALID_HANDLE_VALUE) {
|
|
DWORD err = GetLastError();
|
|
log_fail(L"couldnt open " + part, err);
|
|
error = L"The download could not be saved.\n\n(" + win_error(err) + L")";
|
|
http_end(h);
|
|
return false;
|
|
}
|
|
LARGE_INTEGER zero = {};
|
|
SetFilePointerEx(file, zero, nullptr, FILE_END);
|
|
|
|
long long total = rel.size;
|
|
std::vector<BYTE> buf(64 * 1024);
|
|
bool ok = true;
|
|
for (;;) {
|
|
if (cancelled()) { ok = false; break; }
|
|
|
|
DWORD got = 0;
|
|
if (!WinHttpReadData(h.req, &buf[0], (DWORD)buf.size(), &got)) {
|
|
DWORD err = GetLastError();
|
|
log_fail(L"the download broke off at " + num(have), err);
|
|
error = L"The download was interrupted.\n\n(" + win_error(err) + L")";
|
|
again = true;
|
|
ok = false;
|
|
break;
|
|
}
|
|
if (got == 0) break;
|
|
|
|
DWORD wrote = 0;
|
|
if (!WriteFile(file, &buf[0], got, &wrote, nullptr) || wrote != got) {
|
|
DWORD err = GetLastError();
|
|
log_fail(L"couldnt write to " + part, err);
|
|
error = L"The download could not be saved. The disk may be full.\n\n(" + win_error(err) + L")";
|
|
ok = false;
|
|
break;
|
|
}
|
|
have += got;
|
|
if (total > 0) {
|
|
int permille = (int)((have * 1000) / total);
|
|
set_status(L"Downloading\x2026 " + num(have / (1024 * 1024)) + L" of " + num(total / (1024 * 1024)) + L" MB",
|
|
permille > 1000 ? 1000 : permille);
|
|
} else {
|
|
set_status(L"Downloading\x2026 " + num(have / (1024 * 1024)) + L" MB", -1);
|
|
}
|
|
}
|
|
CloseHandle(file);
|
|
http_end(h);
|
|
if (!ok) return false;
|
|
|
|
if (total > 0 && have < total) {
|
|
// the server hung up early without it being an error
|
|
log_line(L"the download stopped at " + num(have) + L" of " + num(total));
|
|
error = L"The download was interrupted.";
|
|
again = true;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool download(const Release& rel, const std::wstring& part, std::wstring& error) {
|
|
for (int attempt = 0; attempt < 5; attempt++) {
|
|
bool again = false;
|
|
if (download_once(rel, part, again, error)) return true;
|
|
if (cancelled() || !again) return false;
|
|
// a breath, then carry on from where it stopped. the Retry button is
|
|
// for when this runs out
|
|
Sleep(1500);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static bool sha256_file(const std::wstring& path, std::wstring& hex) {
|
|
hex.clear();
|
|
HANDLE file = CreateFileW(lp(path).c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
|
|
FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
|
|
if (file == INVALID_HANDLE_VALUE) {
|
|
log_fail(L"couldnt open " + path + L" to check it", GetLastError());
|
|
return false;
|
|
}
|
|
|
|
BCRYPT_ALG_HANDLE alg = nullptr;
|
|
BCRYPT_HASH_HANDLE hash = nullptr;
|
|
BYTE digest[32];
|
|
bool ok = false;
|
|
LONG rc = BCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA256_ALGORITHM, nullptr, 0);
|
|
if (rc >= 0) rc = BCryptCreateHash(alg, &hash, nullptr, 0, nullptr, 0, 0);
|
|
if (rc >= 0) {
|
|
std::vector<BYTE> buf(256 * 1024);
|
|
long long total = file_size(path), done = 0;
|
|
for (;;) {
|
|
DWORD got = 0;
|
|
if (!ReadFile(file, &buf[0], (DWORD)buf.size(), &got, nullptr)) {
|
|
log_fail(L"couldnt read " + path + L" to check it", GetLastError());
|
|
rc = -1;
|
|
break;
|
|
}
|
|
if (got == 0) break;
|
|
rc = BCryptHashData(hash, &buf[0], got, 0);
|
|
if (rc < 0) break;
|
|
done += got;
|
|
if (total > 0) set_status(L"Checking the download\x2026", (int)((done * 1000) / total));
|
|
}
|
|
if (rc >= 0) rc = BCryptFinishHash(hash, digest, sizeof(digest), 0);
|
|
ok = rc >= 0;
|
|
}
|
|
if (!ok) log_line(L"sha256 didnt work, ntstatus " + num((long long)rc));
|
|
if (hash) BCryptDestroyHash(hash);
|
|
if (alg) BCryptCloseAlgorithmProvider(alg, 0);
|
|
CloseHandle(file);
|
|
if (!ok) return false;
|
|
|
|
static const wchar_t digits[] = L"0123456789abcdef";
|
|
for (size_t i = 0; i < sizeof(digest); i++) {
|
|
hex += digits[digest[i] >> 4];
|
|
hex += digits[digest[i] & 15];
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
// ---- laying it out ----
|
|
|
|
// the stub comes out of our own resources and goes in under the apps exe name
|
|
static bool write_stub(const std::wstring& to) {
|
|
HMODULE me = GetModuleHandleW(nullptr);
|
|
HRSRC res = FindResourceW(me, L"STUB", RT_RCDATA);
|
|
HGLOBAL mem = res ? LoadResource(me, res) : nullptr;
|
|
const void* bytes = mem ? LockResource(mem) : nullptr;
|
|
DWORD size = res ? SizeofResource(me, res) : 0;
|
|
if (!bytes || size == 0) {
|
|
log_fail(L"the stub isnt inside this installer, it was built wrong", GetLastError());
|
|
return false;
|
|
}
|
|
|
|
std::wstring fresh = to + L".new";
|
|
HANDLE h = CreateFileW(lp(fresh).c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
log_fail(L"couldnt make " + fresh, GetLastError());
|
|
return false;
|
|
}
|
|
DWORD wrote = 0;
|
|
bool ok = WriteFile(h, bytes, size, &wrote, nullptr) && wrote == size;
|
|
DWORD err = GetLastError();
|
|
CloseHandle(h);
|
|
if (!ok) {
|
|
log_fail(L"couldnt write " + fresh, err);
|
|
DeleteFileW(lp(fresh).c_str());
|
|
return false;
|
|
}
|
|
|
|
// installing over the top of one thats there. the old stub is only ever
|
|
// running for the moment it takes to start the app
|
|
for (int i = 0; i < 15; i++) {
|
|
if (MoveFileExW(lp(fresh).c_str(), lp(to).c_str(), MOVEFILE_REPLACE_EXISTING)) return true;
|
|
err = GetLastError();
|
|
Sleep(300);
|
|
}
|
|
log_fail(L"couldnt put the stub at " + to, err);
|
|
DeleteFileW(lp(fresh).c_str());
|
|
return false;
|
|
}
|
|
|
|
static bool make_shortcut(const std::wstring& lnk, const std::wstring& target, const std::wstring& dir) {
|
|
IShellLinkW* link = nullptr;
|
|
HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (void**)&link);
|
|
if (FAILED(hr) || !link) {
|
|
log_fail(L"couldnt make a shortcut object for " + lnk, (DWORD)hr);
|
|
return false;
|
|
}
|
|
link->SetPath(target.c_str());
|
|
link->SetWorkingDirectory(dir.c_str());
|
|
link->SetDescription(kAppName);
|
|
link->SetIconLocation(target.c_str(), 0);
|
|
|
|
IPersistFile* file = nullptr;
|
|
hr = link->QueryInterface(IID_IPersistFile, (void**)&file);
|
|
if (SUCCEEDED(hr) && file) {
|
|
hr = file->Save(lnk.c_str(), TRUE);
|
|
file->Release();
|
|
}
|
|
link->Release();
|
|
if (FAILED(hr)) {
|
|
log_fail(L"couldnt save " + lnk, (DWORD)hr);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static long long folder_size(const std::wstring& dir) {
|
|
long long total = 0;
|
|
WIN32_FIND_DATAW fd;
|
|
HANDLE find = FindFirstFileW(lp(join(dir, L"*")).c_str(), &fd);
|
|
if (find == INVALID_HANDLE_VALUE) return 0;
|
|
do {
|
|
std::wstring name = fd.cFileName;
|
|
if (name == L"." || name == L"..") continue;
|
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) total += folder_size(join(dir, name));
|
|
} else {
|
|
total += ((long long)fd.nFileSizeHigh << 32) | (long long)fd.nFileSizeLow;
|
|
}
|
|
} while (FindNextFileW(find, &fd));
|
|
FindClose(find);
|
|
return total;
|
|
}
|
|
|
|
static bool reg_text(HKEY key, const wchar_t* name, const std::wstring& value) {
|
|
LSTATUS rc = RegSetValueExW(key, name, 0, REG_SZ, (const BYTE*)value.c_str(),
|
|
(DWORD)((value.size() + 1) * sizeof(wchar_t)));
|
|
if (rc != ERROR_SUCCESS) log_fail(std::wstring(L"couldnt set ") + name + L" on the uninstall entry", (DWORD)rc);
|
|
return rc == ERROR_SUCCESS;
|
|
}
|
|
|
|
static bool reg_number(HKEY key, const wchar_t* name, DWORD value) {
|
|
LSTATUS rc = RegSetValueExW(key, name, 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
|
|
if (rc != ERROR_SUCCESS) log_fail(std::wstring(L"couldnt set ") + name + L" on the uninstall entry", (DWORD)rc);
|
|
return rc == ERROR_SUCCESS;
|
|
}
|
|
|
|
// the line in "installed apps". its also where the stub finds out what to
|
|
// take away again: it looks for the entry whose InstallLocation is its own
|
|
// folder and deletes the shortcuts listed in GarageShortcuts
|
|
static bool write_uninstall_entry(const std::wstring& root, const std::wstring& stub, const Release& rel,
|
|
const std::vector<std::wstring>& shortcuts) {
|
|
std::wstring path = std::wstring(L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\") + GARAGE_APP_ID;
|
|
HKEY key = nullptr;
|
|
LSTATUS rc = RegCreateKeyExW(g_everyone ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, path.c_str(), 0, nullptr, 0,
|
|
KEY_WRITE | KEY_WOW64_64KEY, nullptr, &key, nullptr);
|
|
if (rc != ERROR_SUCCESS) {
|
|
log_fail(L"couldnt make the uninstall entry", (DWORD)rc);
|
|
return false;
|
|
}
|
|
|
|
SYSTEMTIME t;
|
|
GetLocalTime(&t);
|
|
wchar_t date[16];
|
|
wsprintfW(date, L"%04d%02d%02d", t.wYear, t.wMonth, t.wDay);
|
|
|
|
std::wstring multi;
|
|
for (size_t i = 0; i < shortcuts.size(); i++) {
|
|
multi += shortcuts[i];
|
|
multi += L'\0';
|
|
}
|
|
multi += L'\0';
|
|
|
|
bool ok = true;
|
|
ok = reg_text(key, L"DisplayName", kAppName) && ok;
|
|
ok = reg_text(key, L"DisplayVersion", rel.label.empty() ? num(rel.build) : rel.label) && ok;
|
|
ok = reg_text(key, L"DisplayIcon", stub) && ok;
|
|
ok = reg_text(key, L"InstallLocation", root) && ok;
|
|
ok = reg_text(key, L"InstallDate", date) && ok;
|
|
ok = reg_text(key, L"UninstallString", quote_arg(stub) + L" --garage-uninstall") && ok;
|
|
ok = reg_text(key, L"QuietUninstallString", quote_arg(stub) + L" --garage-uninstall --quiet") && ok;
|
|
if (kPublisher[0]) ok = reg_text(key, L"Publisher", kPublisher) && ok;
|
|
ok = reg_number(key, L"NoModify", 1) && ok;
|
|
ok = reg_number(key, L"NoRepair", 1) && ok;
|
|
ok = reg_number(key, L"EstimatedSize", (DWORD)(folder_size(version_dir(root, rel.build)) / 1024)) && ok;
|
|
|
|
rc = RegSetValueExW(key, L"GarageShortcuts", 0, REG_MULTI_SZ, (const BYTE*)multi.data(),
|
|
(DWORD)(multi.size() * sizeof(wchar_t)));
|
|
if (rc != ERROR_SUCCESS) {
|
|
log_fail(L"couldnt set GarageShortcuts on the uninstall entry", (DWORD)rc);
|
|
ok = false;
|
|
}
|
|
RegCloseKey(key);
|
|
return ok;
|
|
}
|
|
|
|
static std::wstring g_errorText;
|
|
|
|
static bool fail(const std::wstring& text) {
|
|
g_errorText = text;
|
|
return false;
|
|
}
|
|
|
|
// everything, start to finish. runs on the worker thread
|
|
static bool install() {
|
|
std::wstring error;
|
|
|
|
set_status(L"Looking for the newest version\x2026", -1);
|
|
const wchar_t* arch = native_arch();
|
|
Release rel;
|
|
bool none = false;
|
|
if (!ask_garage(arch, rel, none, error)) {
|
|
if (!none) return fail(error);
|
|
// nothing built for arm. the x64 one runs there all the same
|
|
bool found = false;
|
|
if (lstrcmpW(arch, L"arm64") == 0) {
|
|
found = ask_garage(L"x64", rel, none, error);
|
|
if (!found && !none) return fail(error);
|
|
}
|
|
if (!found) {
|
|
log_line(std::wstring(L"garage has no published windows release for ") + arch);
|
|
return fail(std::wstring(kAppName) + L" has not been released for Windows yet.");
|
|
}
|
|
}
|
|
if (cancelled()) return false;
|
|
|
|
|
|
std::wstring zip = join(g_logDir, num(rel.build) + L".zip");
|
|
std::wstring part = zip + L".part";
|
|
if (!exists(zip)) {
|
|
set_status(L"Downloading\x2026", rel.size > 0 ? 0 : -1);
|
|
if (!download(rel, part, error)) return fail(error);
|
|
if (cancelled()) return false;
|
|
if (!MoveFileExW(lp(part).c_str(), lp(zip).c_str(), MOVEFILE_REPLACE_EXISTING)) {
|
|
DWORD err = GetLastError();
|
|
log_fail(L"couldnt rename " + part, err);
|
|
return fail(L"The download could not be saved.\n\n(" + win_error(err) + L")");
|
|
}
|
|
}
|
|
|
|
set_status(L"Checking the download\x2026", 0);
|
|
std::wstring sum;
|
|
if (!sha256_file(zip, sum)) return fail(L"The download could not be checked.");
|
|
if (!same_text(sum, rel.sha256)) {
|
|
log_line(L"sha256 of the download is " + sum + L", garage says " + rel.sha256);
|
|
// no good to anybody. Retry starts it from nothing
|
|
DeleteFileW(lp(zip).c_str());
|
|
return fail(L"The download arrived damaged. Try again.");
|
|
}
|
|
if (cancelled()) return false;
|
|
|
|
|
|
set_status(L"Installing\x2026", -1);
|
|
std::wstring root = install_root(g_everyone);
|
|
int made = SHCreateDirectoryExW(nullptr, root.c_str(), nullptr);
|
|
if (made != ERROR_SUCCESS && made != ERROR_ALREADY_EXISTS && made != ERROR_FILE_EXISTS) {
|
|
log_fail(L"couldnt make " + root, (DWORD)made);
|
|
return fail(L"The install folder could not be made.\n\n" + root + L"\n(" + win_error((DWORD)made) + L")");
|
|
}
|
|
|
|
std::wstring stub = join(root, GARAGE_EXE_NAME);
|
|
if (!write_stub(stub)) return fail(L"The app could not be copied in to place.\n\n" + root);
|
|
|
|
// the stub needs to know which exe in the zip is the app before its
|
|
// unpacked anything. whatever else is in an old state file stays
|
|
State st;
|
|
std::string text;
|
|
if (read_file(state_path(root), text, 1024 * 1024) && !parse_state(text, st)) {
|
|
log_line(L"the state file in " + root + L" wont read, writing a new one");
|
|
st = State();
|
|
}
|
|
st.exe = GARAGE_EXE_NAME;
|
|
st.set_extra("app_id", GARAGE_APP_ID);
|
|
if (!write_atomic(state_path(root), print_state(st))) {
|
|
DWORD err = GetLastError();
|
|
log_fail(L"couldnt write " + state_path(root), err);
|
|
return fail(L"The install folder could not be written to.\n\n" + root + L"\n(" + win_error(err) + L")");
|
|
}
|
|
|
|
DWORD code = 1;
|
|
if (!run_wait(stub, L"--garage-apply " + quote_arg(zip) + L" " + num(rel.build), root, nullptr, code)) {
|
|
DWORD err = GetLastError();
|
|
log_fail(L"couldnt run " + stub, err);
|
|
return fail(L"The app could not be unpacked.\n\n(" + win_error(err) + L")");
|
|
}
|
|
if (code != 0) {
|
|
log_line(L"the stub couldnt unpack build " + num(rel.build) + L", it exited " + num(code));
|
|
return fail(L"The app could not be unpacked (code " + num(code) + L").\n\nDetails are in " +
|
|
join(root, L"garage_updates.log"));
|
|
}
|
|
|
|
|
|
// from here its in and it runs. a shortcut that wont save is logged and
|
|
// lived with, its not worth telling somebody the install failed over
|
|
std::vector<std::wstring> shortcuts;
|
|
HRESULT com = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
|
|
std::wstring lnkName = file_safe(kAppName) + L".lnk";
|
|
std::wstring programs = shell_folder(g_everyone ? CSIDL_COMMON_PROGRAMS : CSIDL_PROGRAMS);
|
|
if (!programs.empty() && make_shortcut(join(programs, lnkName), stub, root)) {
|
|
shortcuts.push_back(join(programs, lnkName));
|
|
}
|
|
if (g_desktopShortcut) {
|
|
std::wstring desktop = shell_folder(g_everyone ? CSIDL_COMMON_DESKTOPDIRECTORY : CSIDL_DESKTOPDIRECTORY);
|
|
if (!desktop.empty() && make_shortcut(join(desktop, lnkName), stub, root)) {
|
|
shortcuts.push_back(join(desktop, lnkName));
|
|
}
|
|
}
|
|
if (SUCCEEDED(com)) CoUninitialize();
|
|
|
|
if (!write_uninstall_entry(root, stub, rel, shortcuts)) {
|
|
log_line(L"the uninstall entry is missing or half there, the app is installed all the same");
|
|
}
|
|
|
|
if (!DeleteFileW(lp(zip).c_str())) log_fail(L"couldnt delete " + zip, GetLastError());
|
|
return true;
|
|
}
|
|
|
|
static DWORD WINAPI install_thread(LPVOID) {
|
|
g_errorText.clear();
|
|
bool ok = install();
|
|
|
|
EnterCriticalSection(&g_lock);
|
|
g_progress.ok = ok;
|
|
g_progress.error = g_errorText;
|
|
g_progress.done = true;
|
|
LeaveCriticalSection(&g_lock);
|
|
return 0;
|
|
}
|
|
|
|
static bool start_app() {
|
|
std::wstring root = install_root(g_everyone);
|
|
std::wstring stub = join(root, GARAGE_EXE_NAME);
|
|
std::wstring cmd = quote_arg(stub);
|
|
STARTUPINFOW si = {};
|
|
si.cb = sizeof(si);
|
|
PROCESS_INFORMATION pi = {};
|
|
if (!CreateProcessW(stub.c_str(), &cmd[0], nullptr, nullptr, FALSE, 0, nullptr, root.c_str(), &si, &pi)) {
|
|
log_fail(L"installed, but couldnt start " + stub, GetLastError());
|
|
return false;
|
|
}
|
|
CloseHandle(pi.hThread);
|
|
CloseHandle(pi.hProcess);
|
|
return true;
|
|
}
|
|
|
|
// ourselves again, behind the uac prompt. the handle comes back so whoever
|
|
// asked can wait for it
|
|
static bool relaunch_elevated(HWND owner) {
|
|
wchar_t self[MAX_PATH * 4];
|
|
DWORD n = GetModuleFileNameW(nullptr, self, ARRAYSIZE(self));
|
|
if (n == 0 || n >= ARRAYSIZE(self)) {
|
|
log_fail(L"couldnt find our own exe to elevate", GetLastError());
|
|
return false;
|
|
}
|
|
SHELLEXECUTEINFOW sx = {};
|
|
sx.cbSize = sizeof(sx);
|
|
sx.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NOASYNC;
|
|
sx.hwnd = owner;
|
|
sx.lpVerb = L"runas";
|
|
sx.lpFile = self;
|
|
sx.lpParameters = L"--everyone --elevated";
|
|
sx.nShow = SW_SHOWNORMAL;
|
|
if (!ShellExecuteExW(&sx) || !sx.hProcess) {
|
|
log_fail(L"the administrator prompt was turned down, or didnt come up", GetLastError());
|
|
return false;
|
|
}
|
|
g_child = sx.hProcess;
|
|
return true;
|
|
}
|
|
|
|
|
|
// ---- the window ----
|
|
|
|
static std::wstring g_title, g_heading, g_body;
|
|
static TASKDIALOGCONFIG g_cfg;
|
|
static TASKDIALOG_BUTTON g_buttons[1];
|
|
static TASKDIALOG_BUTTON g_radios[2];
|
|
|
|
static HRESULT CALLBACK dialog_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LONG_PTR data);
|
|
|
|
// fills g_cfg for a page. everything it points at is a global, the dialog
|
|
// holds on to the pointers
|
|
static void fill_page(Page page) {
|
|
g_page = page;
|
|
g_cfg = TASKDIALOGCONFIG();
|
|
g_cfg.cbSize = sizeof(g_cfg);
|
|
g_cfg.hInstance = GetModuleHandleW(nullptr);
|
|
g_cfg.pfCallback = dialog_proc;
|
|
g_cfg.dwFlags = TDF_ALLOW_DIALOG_CANCELLATION | TDF_CAN_BE_MINIMIZED;
|
|
g_title = std::wstring(kAppName) + L" Setup";
|
|
g_cfg.pszWindowTitle = g_title.c_str();
|
|
|
|
if (page == PAGE_ERROR) {
|
|
g_cfg.pszMainIcon = TD_ERROR_ICON;
|
|
} else if (g_icon) {
|
|
g_cfg.dwFlags |= TDF_USE_HICON_MAIN;
|
|
g_cfg.hMainIcon = g_icon;
|
|
}
|
|
|
|
switch (page) {
|
|
case PAGE_CHOICE:
|
|
g_heading = std::wstring(L"Install ") + kAppName;
|
|
g_body = L"Who should be able to use it?";
|
|
g_buttons[0].nButtonID = ID_INSTALL;
|
|
g_buttons[0].pszButtonText = L"Install";
|
|
g_radios[0].nButtonID = RADIO_ME;
|
|
g_radios[0].pszButtonText = L"Just me";
|
|
g_radios[1].nButtonID = RADIO_EVERYONE;
|
|
g_radios[1].pszButtonText = L"Everyone on this PC";
|
|
g_cfg.pButtons = g_buttons;
|
|
g_cfg.cButtons = 1;
|
|
g_cfg.pRadioButtons = g_radios;
|
|
g_cfg.cRadioButtons = 2;
|
|
g_cfg.nDefaultRadioButton = g_everyone ? RADIO_EVERYONE : RADIO_ME;
|
|
g_cfg.nDefaultButton = ID_INSTALL;
|
|
g_cfg.dwCommonButtons = TDCBF_CANCEL_BUTTON;
|
|
break;
|
|
|
|
case PAGE_PROGRESS:
|
|
g_heading = std::wstring(L"Installing ") + kAppName;
|
|
g_body = L"Getting ready\x2026";
|
|
g_cfg.dwFlags |= TDF_SHOW_PROGRESS_BAR | TDF_CALLBACK_TIMER;
|
|
g_cfg.dwCommonButtons = TDCBF_CANCEL_BUTTON;
|
|
break;
|
|
|
|
case PAGE_ERROR:
|
|
g_heading = std::wstring(kAppName) + L" could not be installed";
|
|
g_cfg.dwCommonButtons = TDCBF_RETRY_BUTTON | TDCBF_CLOSE_BUTTON;
|
|
g_cfg.nDefaultButton = IDRETRY;
|
|
break;
|
|
}
|
|
g_cfg.pszMainInstruction = g_heading.c_str();
|
|
g_cfg.pszContent = g_body.c_str();
|
|
}
|
|
|
|
static void go_to(HWND hwnd, Page page) {
|
|
fill_page(page);
|
|
SendMessageW(hwnd, TDM_NAVIGATE_PAGE, 0, (LPARAM)&g_cfg);
|
|
}
|
|
|
|
static void set_body(HWND hwnd, const std::wstring& text) {
|
|
g_body = text;
|
|
SendMessageW(hwnd, TDM_SET_ELEMENT_TEXT, TDE_CONTENT, (LPARAM)g_body.c_str());
|
|
}
|
|
|
|
static void show_shield(HWND hwnd) {
|
|
SendMessageW(hwnd, TDM_SET_BUTTON_ELEVATION_REQUIRED_STATE, ID_INSTALL, g_everyone && !is_admin());
|
|
}
|
|
|
|
static void set_marquee(HWND hwnd, bool on) {
|
|
g_marquee = on;
|
|
SendMessageW(hwnd, TDM_SET_MARQUEE_PROGRESS_BAR, on, 0);
|
|
SendMessageW(hwnd, TDM_SET_PROGRESS_BAR_MARQUEE, on, 0);
|
|
}
|
|
|
|
static void start_worker(HWND hwnd) {
|
|
EnterCriticalSection(&g_lock);
|
|
g_progress.status = g_body;
|
|
g_progress.permille = -1;
|
|
g_progress.done = false;
|
|
g_progress.ok = false;
|
|
g_progress.error.clear();
|
|
LeaveCriticalSection(&g_lock);
|
|
InterlockedExchange(&g_cancel, 0);
|
|
g_shown = g_body;
|
|
|
|
SendMessageW(hwnd, TDM_SET_PROGRESS_BAR_RANGE, 0, MAKELPARAM(0, 1000));
|
|
set_marquee(hwnd, true);
|
|
|
|
g_worker = CreateThread(nullptr, 0, install_thread, nullptr, 0, nullptr);
|
|
if (!g_worker) {
|
|
log_fail(L"couldnt start the install thread", GetLastError());
|
|
g_body = L"Windows would not start the install. Try again.";
|
|
go_to(hwnd, PAGE_ERROR);
|
|
}
|
|
}
|
|
|
|
// five times a second while the progress page is up
|
|
static void on_tick(HWND hwnd) {
|
|
if (!g_worker) return;
|
|
|
|
EnterCriticalSection(&g_lock);
|
|
std::wstring status = g_progress.status;
|
|
int permille = g_progress.permille;
|
|
bool done = g_progress.done, ok = g_progress.ok;
|
|
std::wstring error = g_progress.error;
|
|
LeaveCriticalSection(&g_lock);
|
|
|
|
if (!done) {
|
|
if (cancelled()) return;
|
|
if (status != g_shown) {
|
|
g_shown = status;
|
|
set_body(hwnd, status);
|
|
}
|
|
if (permille < 0) {
|
|
if (!g_marquee) set_marquee(hwnd, true);
|
|
} else {
|
|
if (g_marquee) set_marquee(hwnd, false);
|
|
SendMessageW(hwnd, TDM_SET_PROGRESS_BAR_POS, (WPARAM)permille, 0);
|
|
}
|
|
return;
|
|
}
|
|
|
|
WaitForSingleObject(g_worker, INFINITE);
|
|
CloseHandle(g_worker);
|
|
g_worker = nullptr;
|
|
|
|
if (ok || cancelled()) {
|
|
g_exit = ok ? kExitOk : kExitCancelled;
|
|
// with the worker gone a click on cancel is let through, which is
|
|
// how a task dialog gets closed from the inside
|
|
SendMessageW(hwnd, TDM_CLICK_BUTTON, IDCANCEL, 0);
|
|
return;
|
|
}
|
|
g_body = error.empty() ? std::wstring(L"Something went wrong.") : error;
|
|
go_to(hwnd, PAGE_ERROR);
|
|
}
|
|
|
|
// S_OK lets the click close the dialog, S_FALSE keeps it open
|
|
static HRESULT on_button(HWND hwnd, int id) {
|
|
switch (g_page) {
|
|
case PAGE_CHOICE:
|
|
if (id != ID_INSTALL) {
|
|
g_exit = kExitCancelled;
|
|
return S_OK;
|
|
}
|
|
if (g_everyone && !is_admin()) {
|
|
if (relaunch_elevated(hwnd)) return S_OK;
|
|
set_body(hwnd, L"Installing for everyone needs an administrator to allow it. "
|
|
L"Choose Just me to install without one.");
|
|
return S_FALSE;
|
|
}
|
|
go_to(hwnd, PAGE_PROGRESS);
|
|
return S_FALSE;
|
|
|
|
case PAGE_PROGRESS:
|
|
if (!g_worker) return S_OK;
|
|
// the worker is told, and the timer closes us once its let go
|
|
InterlockedExchange(&g_cancel, 1);
|
|
set_body(hwnd, L"Cancelling\x2026");
|
|
return S_FALSE;
|
|
|
|
case PAGE_ERROR:
|
|
if (id == IDRETRY) {
|
|
go_to(hwnd, PAGE_PROGRESS);
|
|
return S_FALSE;
|
|
}
|
|
g_exit = kExitFailed;
|
|
return S_OK;
|
|
}
|
|
return S_OK;
|
|
}
|
|
|
|
static HRESULT CALLBACK dialog_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM, LONG_PTR) {
|
|
switch (msg) {
|
|
case TDN_CREATED:
|
|
case TDN_NAVIGATED:
|
|
if (g_page == PAGE_CHOICE) show_shield(hwnd);
|
|
if (g_page == PAGE_PROGRESS) start_worker(hwnd);
|
|
break;
|
|
case TDN_RADIO_BUTTON_CLICKED:
|
|
g_everyone = (int)wp == RADIO_EVERYONE;
|
|
show_shield(hwnd);
|
|
break;
|
|
case TDN_TIMER:
|
|
on_tick(hwnd);
|
|
break;
|
|
case TDN_BUTTON_CLICKED:
|
|
return on_button(hwnd, (int)wp);
|
|
}
|
|
return S_OK;
|
|
}
|
|
|
|
static void complain(const std::wstring& text) {
|
|
MessageBoxW(nullptr, text.c_str(), (std::wstring(kAppName) + L" Setup").c_str(), MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
|
|
}
|
|
|
|
int WINAPI wWinMain(HINSTANCE inst, HINSTANCE, PWSTR, int) {
|
|
InitializeCriticalSection(&g_lock);
|
|
|
|
wchar_t temp[MAX_PATH + 2];
|
|
DWORD n = GetTempPathW(ARRAYSIZE(temp), temp);
|
|
if (n > 0 && n < ARRAYSIZE(temp)) {
|
|
g_logDir = join(temp, std::wstring(GARAGE_APP_ID) + L"-setup");
|
|
if (!CreateDirectoryW(lp(g_logDir).c_str(), nullptr) && GetLastError() != ERROR_ALREADY_EXISTS) {
|
|
DWORD err = GetLastError();
|
|
g_logDir = std::wstring(temp);
|
|
log_fail(L"couldnt make a folder in temp, using temp itself", err);
|
|
}
|
|
} else {
|
|
complain(L"Windows has no temporary folder to download to.");
|
|
return kExitFailed;
|
|
}
|
|
|
|
int argc = 0;
|
|
wchar_t** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
|
|
for (int i = 1; argv && i < argc; i++) {
|
|
if (lstrcmpW(argv[i], L"--everyone") == 0) g_everyone = true;
|
|
if (lstrcmpW(argv[i], L"--just-me") == 0) g_everyone = false;
|
|
if (lstrcmpW(argv[i], L"--elevated") == 0) g_elevatedChild = true;
|
|
}
|
|
if (argv) LocalFree(argv);
|
|
|
|
g_icon = LoadIconW(inst, MAKEINTRESOURCEW(1));
|
|
|
|
bool choose = g_showChoice && !g_elevatedChild;
|
|
if (!choose && g_everyone && !is_admin()) {
|
|
// nothing to ask and its going in program files, so straight to the prompt
|
|
if (!relaunch_elevated(nullptr)) {
|
|
complain(std::wstring(kAppName) + L" is installed for everyone on this PC, which needs an administrator to allow it.");
|
|
return kExitCancelled;
|
|
}
|
|
}
|
|
|
|
if (!g_child) {
|
|
fill_page(choose ? PAGE_CHOICE : PAGE_PROGRESS);
|
|
int pressed = 0;
|
|
HRESULT hr = TaskDialogIndirect(&g_cfg, &pressed, nullptr, nullptr);
|
|
if (FAILED(hr)) {
|
|
log_fail(L"the setup window wouldnt open", (DWORD)hr);
|
|
complain(L"The setup window could not be opened.");
|
|
return kExitFailed;
|
|
}
|
|
}
|
|
|
|
if (g_child) {
|
|
// the elevated one has its own window and its own retry. we only
|
|
// want to know how it ended
|
|
WaitForSingleObject(g_child, INFINITE);
|
|
DWORD code = (DWORD)kExitFailed;
|
|
GetExitCodeProcess(g_child, &code);
|
|
CloseHandle(g_child);
|
|
g_exit = (int)code;
|
|
}
|
|
|
|
// an elevated copy doesnt start the app, or the app would run as admin.
|
|
// the one that asked for it does, back down here as the user
|
|
if (g_exit == kExitOk && !g_elevatedChild) start_app();
|
|
|
|
DeleteCriticalSection(&g_lock);
|
|
return g_exit;
|
|
}
|