-
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathupdater.cpp
More file actions
106 lines (94 loc) · 3.65 KB
/
Copy pathupdater.cpp
File metadata and controls
106 lines (94 loc) · 3.65 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <Shlwapi.h>
#include <fstream>
#include <yaml-cpp/yaml.h>
#include <IbUpdate/GitHubUpdater.hpp>
// IbUpdate -> github_api -> cURL
// See https://github.com/microsoft/vcpkg/issues/2621
#pragma comment(lib, "Ws2_32.Lib")
//#pragma comment(lib, "Wldap32.Lib")
#pragma comment(lib, "Crypt32.Lib")
#pragma comment(lib, "Shlwapi.lib")
std::wstring u8_to_u16(std::string u8) {
int u16_size = MultiByteToWideChar(CP_UTF8, 0, u8.c_str(), (int)u8.length(), nullptr, 0);
std::wstring u16(u16_size, L'\0');
MultiByteToWideChar(CP_UTF8, 0, u8.c_str(), (int)u8.length(), u16.data(), u16_size);
return u16;
}
/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <param name="lines"></param>
/// <returns>Ends with '\n'</returns>
std::string truncate_lines(std::string s, size_t lines) {
std::istringstream in(s);
std::ostringstream out;
size_t i = 0;
for (std::string line; i < lines && std::getline(in, line); i++) {
out << line << '\n';
}
if (i == lines)
out << (const char*)u8"……\n";
return out.str();
}
bool check_for_update(bool prerelease, bool quiet, bool silent_error) {
try {
GitHubUpdater updater{ "Chaoses-Ib", "IbEverythingExt", "v0.11.0" };
YAML::Node release = updater.check_for_new_release(prerelease);
if (release.IsNull()) {
if (!quiet)
MessageBoxW(nullptr, L"无可用更新", L"IbEverythingExt", MB_ICONINFORMATION);
return true;
}
std::wostringstream ss;
ss << L"检测到新版本 " << u8_to_u16(release["tag_name"].as<std::string>())
<< L"\n发布时间:" << u8_to_u16(release["published_at"].as<std::string>())
<< L"\n更新内容:\n" << u8_to_u16(truncate_lines(release["body"].as<std::string>(), 10))
<< L"\n是否打开发布页面?";
if (MessageBoxW(nullptr, ss.str().c_str(), L"IbEverythingExt", MB_ICONINFORMATION | MB_YESNO) == IDYES) {
ShellExecuteW(nullptr, nullptr, u8_to_u16(release["html_url"].as<std::string>()).c_str(), nullptr, nullptr, SW_SHOWNORMAL);
}
return true;
}
catch (std::runtime_error& e) {
if (!silent_error) {
std::ostringstream ss;
ss << "检查更新失败:\n" << e.what();
MessageBoxA(nullptr, ss.str().c_str(), "IbEverythingExt", MB_ICONWARNING);
}
return false;
}
}
int wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nShowCmd) {
using namespace std::literals;
// lpCmdLine is empty for unknown reason
//MessageBoxW(nullptr, lpCmdLine, nullptr, 0);
//MessageBoxW(nullptr, GetCommandLineW(), nullptr, 0);
lpCmdLine = GetCommandLineW();
bool quiet = false;
if (std::wstring_view(lpCmdLine) == L"--quiet")
quiet = true;
wchar_t path[MAX_PATH];
GetModuleFileNameW(nullptr, path, std::size(path));
PathRemoveFileSpecW(path);
PathAppendW(path, L"config.yaml");
std::ifstream in(path);
if (!in) {
MessageBoxW(nullptr, L"配置文件 config.yaml 不存在!", L"IbEverythingExt", MB_ICONERROR);
return 1;
}
try {
YAML::Node root = YAML::Load(in);
YAML::Node update = root["update"];
if (update["check"].as<bool>()) {
// try twice
if (!check_for_update(update["prerelease"].as<bool>(), quiet, true))
check_for_update(update["prerelease"].as<bool>(), quiet, false);
}
}
catch (YAML::Exception& e) {
MessageBoxA(nullptr, ("配置文件读取错误:\n"s + e.what()).c_str(), "IbEverythingExt", MB_ICONERROR);
return 1;
}
return 0;
}