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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "sys_update_checker.h"
#include "sys_curl.h"
#include "../qcommon/q_version.h"
//#include <httplib.h>
#include "../qcommon/json.hpp"
using json = nlohmann::json;
#include <chrono>
UpdateChecker updateChecker;
void Sys_UpdateChecker_Init()
{
updateChecker.Init();
}
void Sys_UpdateChecker_Process()
{
updateChecker.Process();
}
void Sys_UpdateChecker_Shutdown()
{
updateChecker.Shutdown();
}
UpdateChecker::UpdateChecker()
{
lastMajor = lastMinor = lastPatch = 0;
versionChecked = false;
}
UpdateChecker::~UpdateChecker() {}
void UpdateChecker::Init()
{
CheckInitClientThread();
}
void UpdateChecker::CheckInitClientThread()
{
if (!thread) {
if (CanHaveRequestThread()) {
thread = new UpdateCheckerThread();
thread->Init();
}
} else {
if (!CanHaveRequestThread()) {
Com_DPrintf("Shutting down the update checker thread\n");
delete thread;
thread = NULL;
}
}
}
bool UpdateChecker::CanHaveRequestThread() const
{
if (!Cvar_VariableIntegerValue("net_enabled")) {
// Network has been disabled by a cvar
return false;
}
if (!com_updatecheck_enabled->integer) {
// Update checking has been disabled
return false;
}
#ifdef HAS_LIBCURL
return Com_IsCurlImportValid(&curlImport) ? true : false;
#else
return false;
#endif
}
void UpdateChecker::SetLatestVersion(int major, int minor, int patch)
{
lastMajor = major;
lastMinor = minor;
lastPatch = patch;
versionChecked = true;
}
void UpdateChecker::Process()
{
// Initialize the client thread when necessary
CheckInitClientThread();
std::chrono::time_point<std::chrono::steady_clock> currentTime = std::chrono::steady_clock::now();
if (currentTime < nextMessageTime) {
return;
}
if (!CheckNewVersion()) {
return;
}
Com_Printf(
"New release v%d.%d.%d published *\\(^ o ^)/*. Your current version is v%s. See www.openmohaa.org\n",
lastMajor,
lastMinor,
lastPatch,
PRODUCT_VERSION_NUMBER_STRING
);
nextMessageTime = currentTime + std::chrono::milliseconds(Q_max(1, com_updatecheck_interval->integer) * 60 * 1000);
}
void UpdateChecker::Shutdown()
{
if (thread) {
delete thread;
thread = NULL;
}
}
bool UpdateChecker::CheckNewVersion() const
{
if (!versionChecked) {
return false;
}
if (lastMajor > PRODUCT_VERSION_MAJOR) {
return true;
} else if (lastMajor < PRODUCT_VERSION_MAJOR) {
return false;
}
if (lastMinor > PRODUCT_VERSION_MINOR) {
return true;
} else if (lastMinor < PRODUCT_VERSION_MINOR) {
return false;
}
if (lastPatch > PRODUCT_VERSION_PATCH) {
return true;
}
return false;
}
bool UpdateChecker::CheckNewVersion(int& major, int& minor, int& patch) const
{
if (!CheckNewVersion()) {
return false;
}
major = lastMajor;
minor = lastMinor;
patch = lastPatch;
return true;
}
UpdateCheckerThread::UpdateCheckerThread()
{
handle = NULL;
osThread = NULL;
requestThreadIsActive = qfalse;
shouldBeActive = qfalse;
}
UpdateCheckerThread::~UpdateCheckerThread()
{
Shutdown();
}
void UpdateCheckerThread::Init()
{
shouldBeActive = qtrue;
requestThreadIsActive = qtrue;
osThread = new std::thread(&UpdateCheckerThread::RequestThread, this);
}
void UpdateCheckerThread::Shutdown()
{
if (!shouldBeActive) {
return;
}
shouldBeActive = qfalse;
if (osThread) {
// Notify and shutdown the thread
{
// Wait until the request thread is ready
std::lock_guard<std::mutex> l(clientWakeMutex);
clientWake.notify_all();
}
// Wait for the thread to exit
osThread->join();
delete osThread;
osThread = NULL;
}
}
bool UpdateCheckerThread::IsRoutineActive() const
{
return requestThreadIsActive;
}
void UpdateCheckerThread::InitClient()
{
#ifdef HAS_LIBCURL
CURLcode result;
if (!Com_IsCurlImportValid(&curlImport)) {
Com_DPrintf("Couldn't load cURL.\n");
return;
}
assert(!handle);
handle = curlImport.qcurl_easy_init();
if (!handle) {
Com_DPrintf("Failed to create curl client\n");
return;
}
result = curlImport.qcurl_easy_setopt(
handle, CURLOPT_URL, "https://api.github.com/repos/openmoh/openmohaa/releases/latest"
);
if (result != CURLE_OK) {
Com_DPrintf("Failed to set curl URL: %s\n", curlImport.qcurl_easy_strerror(result));
curlImport.qcurl_easy_cleanup(handle);
handle = NULL;
return;
}
curlImport.qcurl_easy_setopt(handle, CURLOPT_USERAGENT, "curl");
curlImport.qcurl_easy_setopt(handle, CURLOPT_TIMEOUT, 15);
#else
Com_DPrintf("Project was compiled without libcurl, will not check for updates\n");
#endif
}
void UpdateCheckerThread::ShutdownClient()
{
#ifdef HAS_LIBCURL
if (!handle) {
return;
}
curlImport.qcurl_easy_cleanup(handle);
handle = NULL;
#endif
}
bool UpdateCheckerThread::ParseVersionNumber(const char *value, int& major, int& minor, int& patch) const
{
const char *p = value;
const char *pn = value;
if (*p != 'v') {
return false;
}
//
// Parse the major number
//
p++;
for (pn = p; *pn && *pn != '.'; pn++) {};
if (*pn != '.') {
return false;
}
major = std::stoi(std::string(p, pn - p));
//
// Parse the minor number
//
p = pn + 1;
for (pn = p; *pn && *pn != '.'; pn++) {};
if (*pn != '.') {
return false;
}
minor = std::stoi(std::string(p, pn - p));
//
// Parse the patch number
//
p = pn + 1;
for (pn = p; *pn && *pn != '.'; pn++) {};
if (*pn) {
return false;
}
patch = std::stoi(std::string(p, pn - p));
return true;
}
// It shouldn't be possible to receive responses that high from the API
// 4MB
static constexpr unsigned int MAX_BUFFER_SIZE = 4 * 1024 * 1024;
struct WriteCallbackData {
WriteCallbackData()
{
// Reserve a buffer of 64KB
buffer.reserve(64 * 1024);
}
const char *GetData() const { return buffer.data(); }
size_t GetSize() const { return buffer.size(); }
std::string buffer;
};
#ifdef HAS_LIBCURL
size_t WriteCallback(char *contents, size_t size, size_t nmemb, void *userp)
{
WriteCallbackData *callbackData = (WriteCallbackData *)userp;
size_t responseSize = size * nmemb;
if (callbackData->GetSize() + responseSize + 1 > MAX_BUFFER_SIZE) {
return CURL_WRITEFUNC_ERROR;
}
callbackData->buffer.append(contents, responseSize);
return responseSize;
}
#endif
void UpdateCheckerThread::DoRequest()
{
#ifdef HAS_LIBCURL
CURLcode result;
WriteCallbackData callbackData;
curlImport.qcurl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &WriteCallback);
curlImport.qcurl_easy_setopt(handle, CURLOPT_WRITEDATA, &callbackData);
curlImport.qcurl_easy_setopt(handle, CURLOPT_MAXFILESIZE, MAX_BUFFER_SIZE);
struct curl_slist *list = NULL;
list = curlImport.qcurl_slist_append(list, "Accept: application/vnd.github+json");
list = curlImport.qcurl_slist_append(list, "X-GitHub-Api-Version: 2022-11-28");
curlImport.qcurl_easy_setopt(handle, CURLOPT_HTTPHEADER, list);
result = curlImport.qcurl_easy_perform(handle);
curlImport.qcurl_slist_free_all(list);
if (result != CURLE_OK) {
return;
}
nlohmann::json data;
try {
data = nlohmann::json::parse(callbackData.GetData());
} catch (const std::exception& e) {
return;
}
try {
const nlohmann::json::string_t& tagName = data.at("tag_name");
int major, minor, patch;
ParseVersionNumber(tagName.c_str(), major, minor, patch);
updateChecker.SetLatestVersion(major, minor, patch);
} catch (const std::exception& e) {}
#endif
}
void UpdateCheckerThread::RequestThread()
{
// Initialize the curl client
InitClient();
while (handle && shouldBeActive) {
std::unique_lock<std::mutex> l(clientWakeMutex);
DoRequest();
const std::chrono::seconds interval = std::chrono::seconds(Q_max(1, com_updatecheck_interval->integer) * 60);
clientWake.wait_for(l, interval);
}
ShutdownClient();
requestThreadIsActive = qfalse;
}
|