Files

131 lines
4.8 KiB
C++
Raw Permalink Normal View History

#define NOMINMAX
#include <windows.h>
#include <shellapi.h>
#include <cstdio>
#include <string>
#include <vector>
#include "Api.h"
#include "Config.h"
#include "UpdateWindow.h"
#include "Utils.h"
#pragma comment(lib, "shell32.lib")
namespace {
void PrintUtf8(const std::wstring& text) {
if (text.empty()) return;
int need = WideCharToMultiByte(CP_UTF8, 0, text.c_str(), static_cast<int>(text.size()),
nullptr, 0, nullptr, nullptr);
if (need <= 0) return;
std::string utf8(static_cast<size_t>(need), '\0');
WideCharToMultiByte(CP_UTF8, 0, text.c_str(), static_cast<int>(text.size()),
&utf8[0], need, nullptr, nullptr);
fwrite(utf8.data(), 1, utf8.size(), stderr);
fputc('\n', stderr);
}
std::wstring ArgValue(const std::vector<std::wstring>& args, size_t& i,
const wchar_t* longName, const wchar_t* shortName) {
std::wstring arg = args[i];
size_t eq = arg.find(L'=');
std::wstring name = (eq == std::wstring::npos) ? arg : arg.substr(0, eq);
if (name != longName && name != shortName) return std::wstring();
if (eq != std::wstring::npos) return arg.substr(eq + 1);
if (i + 1 < args.size()) {
++i;
return args[i];
}
return std::wstring();
}
}
int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE /*prev*/, LPWSTR /*cmdLine*/, int /*show*/) {
int count = 0;
LPWSTR* raw = CommandLineToArgvW(GetCommandLineW(), &count);
std::vector<std::wstring> args;
for (int i = 1; i < count; ++i) args.push_back(raw[i]);
LocalFree(raw);
// --check-only / -c:静默检查版本,不创建窗口,结果用退出码回传给主程序
bool checkOnly = false;
for (size_t i = 0; i < args.size(); ) {
if (args[i] == L"--check-only" || args[i] == L"-c") {
checkOnly = true;
args.erase(args.begin() + i);
} else {
++i;
}
}
updater::UpdateOptions options;
options.appKey = UPDATER_APP_KEY;
options.targetDir = UPDATER_TARGET_DIR;
2026-10-17 22:36:49 +08:00
// --full / -f:显式要求整包安装,区别于"没传版本号"的隐含整包
bool forceFull = false;
// 无命令行参数 = 完整包更新;有参数则按传入信息检查版本并更新补丁包
if (args.empty()) {
options.fullMode = true;
} else {
std::vector<std::wstring> positional;
for (size_t i = 0; i < args.size(); ++i) {
2026-10-17 22:36:49 +08:00
if (args[i] == L"--full" || args[i] == L"-f") {
forceFull = true;
continue;
}
std::wstring key = updater::Utils::Trim(ArgValue(args, i, L"--app-key", L"-k"));
if (!key.empty()) { options.appKey = key; continue; }
std::wstring ver = updater::Utils::Trim(ArgValue(args, i, L"--version", L"-v"));
if (!ver.empty()) { options.clientVersion = ver; continue; }
std::wstring dir = updater::Utils::Trim(ArgValue(args, i, L"--dir", L"-d"));
if (!dir.empty()) { options.targetDir = dir; continue; }
std::wstring launch = updater::Utils::Trim(ArgValue(args, i, L"--launch", L"-l"));
if (!launch.empty()) { options.launchExe = launch; continue; }
if (!args[i].empty() && args[i][0] != L'-') positional.push_back(args[i]);
}
if (positional.size() >= 1) options.appKey = positional[0];
if (positional.size() >= 2) options.clientVersion = positional[1];
if (positional.size() >= 3) options.targetDir = positional[2];
2026-10-17 22:36:49 +08:00
// 空版本号会让 check-update 缺少 version 参数,被服务端判为"版本号参数错误"。
// 整包模式改由 --full 显式指定,这样版本号可以始终带上
options.fullMode = forceFull || options.clientVersion.empty();
}
if (checkOnly) {
if (options.appKey.empty() || options.clientVersion.empty()) return UPDATER_EXIT_FAILED;
updater::Api::Result result =
updater::Api::CheckUpdate(options.appKey, options.clientVersion);
if (!result.ok) {
// 命令行下排查用,主程序拉起时看不到
PrintUtf8(result.error);
// 400 是服务端拒绝了版本参数(版本表里没有这个版本),补丁包无从生成,
// 只能让主程序走完整包重装,跟网络故障区分开
if (result.httpStatus == 400) return UPDATER_EXIT_BAD_VERSION;
return UPDATER_EXIT_FAILED;
}
return result.info.hasUpdate ? UPDATER_EXIT_HAS_UPDATE : UPDATER_EXIT_NO_UPDATE;
}
updater::UpdateWindow window;
if (!window.Create(instance, options)) {
MessageBoxW(nullptr, L"无法创建更新窗口", UPDATER_TITLE, MB_ICONERROR | MB_OK);
return 1;
}
return window.MessageLoop();
}