48 lines
2.1 KiB
C++
48 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include <windows.h>
|
|
#include <shobjidl.h>
|
|
#include <wrl/client.h>
|
|
#include <filesystem>
|
|
#include <string>
|
|
|
|
namespace octo_updater {
|
|
|
|
inline bool ensure_repair_shortcut(const std::filesystem::path& root) {
|
|
namespace fs = std::filesystem;
|
|
using Microsoft::WRL::ComPtr;
|
|
const auto exe = root / L"OctoUpdater.exe";
|
|
const auto shortcut = root / L"Repair client files.lnk";
|
|
constexpr wchar_t arguments[] = L"--repair --no-launch";
|
|
std::error_code ec;
|
|
if (!fs::is_regular_file(exe, ec)) return false;
|
|
|
|
const HRESULT init = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
|
|
if (FAILED(init) && init != RPC_E_CHANGED_MODE) return false;
|
|
const bool ready = [&] {
|
|
ComPtr<IShellLinkW> link;
|
|
ComPtr<IPersistFile> file;
|
|
if (FAILED(CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(link.GetAddressOf()))) ||
|
|
FAILED(link.As(&file))) return false;
|
|
if (SUCCEEDED(file->Load(shortcut.c_str(), STGM_READ))) {
|
|
wchar_t target[MAX_PATH]{}, directory[MAX_PATH]{}, args[128]{};
|
|
if (SUCCEEDED(link->GetPath(target, MAX_PATH, nullptr, SLGP_RAWPATH)) &&
|
|
SUCCEEDED(link->GetWorkingDirectory(directory, MAX_PATH)) &&
|
|
SUCCEEDED(link->GetArguments(args, 128)) && std::wstring(args) == arguments &&
|
|
fs::equivalent(target, exe, ec) && fs::equivalent(directory, root, ec)) return true;
|
|
}
|
|
if (FAILED(link->SetPath(exe.c_str())) || FAILED(link->SetWorkingDirectory(root.c_str())) ||
|
|
FAILED(link->SetArguments(arguments)) || FAILED(link->SetDescription(L"Check and restore OctoWoW client files"))) return false;
|
|
auto temporary = shortcut;
|
|
temporary += L"." + std::to_wstring(GetCurrentProcessId()) + L".lnk";
|
|
if (SUCCEEDED(file->Save(temporary.c_str(), FALSE)) &&
|
|
MoveFileExW(temporary.c_str(), shortcut.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) return true;
|
|
fs::remove(temporary, ec);
|
|
return false;
|
|
}();
|
|
if (SUCCEEDED(init)) CoUninitialize();
|
|
return ready;
|
|
}
|
|
|
|
} // namespace octo_updater
|