-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathUpdater.cpp
More file actions
344 lines (277 loc) · 8.07 KB
/
Updater.cpp
File metadata and controls
344 lines (277 loc) · 8.07 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
extern "C" {
#include <curl/curl.h>
};
#include "Command.hpp"
#include "Event.hpp"
#include "Features/Hud/Toasts.hpp"
#include "Modules/Console.hpp"
#include "Modules/Engine.hpp"
#include "Updater.hpp"
#include "Utils.hpp"
#include "Utils/json11.hpp"
#include "Version.hpp"
#include <filesystem>
#include <optional>
#include <string>
#include <thread>
#include <utility>
#ifdef _WIN32
# define ASSET_NAME "sar.dll"
# define PDB_ASSET_NAME "sar.pdb"
# define PDB_PATH "sar.pdb"
#else
# define ASSET_NAME "sar.so"
#endif
static CURL *g_curl;
static curl_slist *g_httpHdrs;
static std::thread g_worker;
#define MAX_VERSION_COMPONENTS 6
struct SarVersion {
unsigned components[MAX_VERSION_COMPONENTS];
unsigned pre;
};
static std::optional<SarVersion> getVersionComponents(const char *str) {
SarVersion v = {0};
v.pre = UINT_MAX;
size_t i = 0;
while (true) {
char *end;
v.components[i++] = strtol(str, &end, 10);
str = end;
if (*str == '.') {
++str;
} else {
break;
}
if (i == MAX_VERSION_COMPONENTS) {
return {};
}
}
if (!*str) return v;
if (memcmp(str, "-pre", 4)) return {};
str += 4;
char *end;
v.pre = strtol(str, &end, 10);
if (str == end || *end) return {};
return v;
}
static bool isNewerVersion(const char *verStr) {
auto current = getVersionComponents(SAR_VERSION);
auto version = getVersionComponents(verStr);
if (!current) {
THREAD_PRINT("Cannot compare version numbers on non-release version\n");
return false;
}
if (!version) {
return false;
}
for (size_t i = 0; i < MAX_VERSION_COMPONENTS; ++i) {
if (version->components[i] > current->components[i]) {
return true;
} else if (version->components[i] < current->components[i]) {
return false;
}
}
return version->pre > current->pre;
}
static bool curlPrepare(const char *url, int timeout) {
if (!g_curl) {
g_curl = curl_easy_init();
if (!g_curl) {
return false;
}
g_httpHdrs = curl_slist_append(g_httpHdrs, "Accept: application/vnd.github.v3+json");
g_httpHdrs = curl_slist_append(g_httpHdrs, "User-Agent: SourceAutoRecord");
}
curl_easy_setopt(g_curl, CURLOPT_URL, url);
curl_easy_setopt(g_curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(g_curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(g_curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(g_curl, CURLOPT_HTTPHEADER, g_httpHdrs);
return true;
}
static bool downloadFile(const char *url, const char *path) {
if (!curlPrepare(url, 60)) {
return false;
}
FILE *f = fopen(path, "wb");
if (!f) {
return false;
}
curl_easy_setopt(g_curl, CURLOPT_WRITEFUNCTION, &fwrite);
curl_easy_setopt(g_curl, CURLOPT_WRITEDATA, f);
CURLcode res = curl_easy_perform(g_curl);
fclose(f);
return res == CURLE_OK;
}
static std::string request(const char *url) {
if (!curlPrepare(url, 10)) {
return "";
}
curl_easy_setopt(
g_curl, CURLOPT_WRITEFUNCTION, +[](void *ptr, size_t sz, size_t nmemb, std::string *data) -> size_t {
data->append((char *)ptr, sz * nmemb);
return sz * nmemb;
});
std::string response;
curl_easy_setopt(g_curl, CURLOPT_WRITEDATA, &response);
CURLcode res = curl_easy_perform(g_curl);
return res == CURLE_OK ? response : "";
}
static bool getLatestVersion(std::string *name, std::string *dlUrl, std::string *pdbUrl, bool allowPre) {
json11::Json res;
if (allowPre) {
std::string err;
res = json11::Json::parse(request("https://api.github.com/repos/p2sr/SourceAutoRecord/releases"), err);
if (err != "") {
return false;
}
res = res.array_items()[0];
} else {
std::string err;
res = json11::Json::parse(request("https://api.github.com/repos/p2sr/SourceAutoRecord/releases/latest"), err);
if (err != "") {
return false;
}
}
*name = res["tag_name"].string_value();
if (*name == "") {
return false;
}
for (auto asset : res["assets"].array_items()) {
if (asset["name"].string_value() == ASSET_NAME) {
*dlUrl = asset["browser_download_url"].string_value();
}
#ifdef _WIN32
if (asset["name"].string_value() == PDB_ASSET_NAME) {
*pdbUrl = asset["browser_download_url"].string_value();
}
#endif
}
if (*dlUrl == "") {
return false;
}
#ifdef _WIN32
if (*pdbUrl == "") {
return false;
}
#endif
return true;
}
std::string createTempPath(const char *filename) {
auto base = std::filesystem::temp_directory_path().append(filename);
if (!std::filesystem::exists(base)) {
return base.string();
}
int n = 0;
std::filesystem::path p;
do {
p = base.string() + Utils::ssprintf("_%d", n++);
} while (std::filesystem::exists(p));
return p.string();
}
void checkUpdate(bool allowPre) {
std::string name, dlUrl, pdbUrl;
THREAD_PRINT("Querying for latest version...\n");
if (!getLatestVersion(&name, &dlUrl, &pdbUrl, allowPre)) {
THREAD_PRINT("An error occurred\n");
return;
}
THREAD_PRINT("Latest version is %s\n", name.c_str());
if (!isNewerVersion(name.c_str())) {
THREAD_PRINT("You're all up-to-date!\n");
} else {
THREAD_PRINT("Update with sar_update, or at %s\n", dlUrl.c_str());
}
}
void doUpdate(bool allowPre, bool exitOnSuccess, bool force) {
std::string name, dlUrl, pdbUrl;
THREAD_PRINT("Querying for latest version...\n");
if (!getLatestVersion(&name, &dlUrl, &pdbUrl, allowPre)) {
THREAD_PRINT("An error occurred\n");
return;
}
if (!force && !isNewerVersion(name.c_str())) {
THREAD_PRINT("You're already up-to-date!\n");
return;
}
std::string sar = Utils::GetSARPath();
std::string tmp = createTempPath(ASSET_NAME);
#ifdef _WIN32
std::string tmpPdb = createTempPath(PDB_ASSET_NAME);
#endif
// Step 1: download SAR to the given temporary file
THREAD_PRINT("Downloading SAR %s...\n", name.c_str());
if (!downloadFile(dlUrl.c_str(), tmp.c_str())) {
THREAD_PRINT("An error occurred\n");
return;
}
#ifdef _WIN32
if (!downloadFile(pdbUrl.c_str(), tmpPdb.c_str())) {
THREAD_PRINT("An error occurred\n");
return;
}
#endif
// Step 2: delete the current SAR image. For some reason, on Linux
// we have to delete it, while on Windows we have to move it. Don't
// ask because I don't know
THREAD_PRINT("Deleting old version...\n");
#ifdef _WIN32
std::filesystem::rename(sar, "sar.dll.old-auto");
if (std::filesystem::exists(PDB_PATH)) {
std::filesystem::rename(PDB_PATH, "sar.pdb.old-auto");
}
#else
std::filesystem::remove(sar);
#endif
// Step 3: copy the new SAR image to the location of the old one,
// and then delete the temporary file. We can't just move it
// for........reasons
THREAD_PRINT("Installing...\n", name.c_str());
std::filesystem::copy(tmp, sar);
std::filesystem::remove(tmp);
#ifdef _WIN32
std::filesystem::copy(tmpPdb, PDB_PATH);
std::filesystem::remove(tmpPdb);
#endif
THREAD_PRINT("Success! You should now restart your game.\n");
if (exitOnSuccess) {
Scheduler::OnMainThread([]() {
toastHud.AddToast("update", "SAR has been updated. Your game will now exit.\n");
Scheduler::InHostTicks(120, []() {
engine->ExecuteCommand("quit");
});
});
}
}
CON_COMMAND(sar_check_update, "sar_check_update [release|pre] - check whether the latest version of SAR is being used\n") {
if (args.ArgC() > 2) {
return THREAD_PRINT(sar_check_update.ThisPtr()->m_pszHelpString);
}
bool allowPre = args.ArgC() == 2 && !strcmp(args[1], "pre");
if (g_worker.joinable()) g_worker.join();
g_worker = std::thread(checkUpdate, allowPre);
}
CON_COMMAND(sar_update, "sar_update [release|pre] [exit] [force] - update SAR to the latest version. If exit is given, exit the game upon successful update; if force is given, always re-install, even if it may be a downgrade\n") {
bool allowPre = false, exitOnSuccess = false, force = false;
for (int i = 1; i < args.ArgC(); ++i) {
if (!strcmp(args[i], "pre")) {
allowPre = true;
} else if (!strcmp(args[i], "release")) {
allowPre = false;
} else if (!strcmp(args[i], "exit")) {
exitOnSuccess = true;
} else if (!strcmp(args[i], "force")) {
force = true;
} else {
console->Print("Invalid argument '%s'\n", args[i]);
console->Print(sar_update.ThisPtr()->m_pszHelpString);
return;
}
}
if (g_worker.joinable()) g_worker.join();
g_worker = std::thread(doUpdate, allowPre, exitOnSuccess, force);
}
ON_EVENT(SAR_UNLOAD) {
if (g_worker.joinable()) g_worker.detach();
}