mirror of
https://github.com/openmoh/openmohaa.git
synced 2025-04-28 13:47:58 +03:00
Add HTTP support and update checking
This commit is contained in:
parent
2c72908f76
commit
01381ed084
104 changed files with 111355 additions and 25 deletions
|
@ -35,12 +35,25 @@ if (APPLE)
|
|||
set(SOURCES_PLATFORM_SPECIFIC ${SOURCES_PLATFORM_SPECIFIC} "${CMAKE_SOURCE_DIR}/code/sys/sys_osx.m")
|
||||
endif()
|
||||
|
||||
set(SOURCES_PLATFORM_COMMON
|
||||
"${CMAKE_SOURCE_DIR}/code/sys/sys_update_checker.cpp"
|
||||
)
|
||||
|
||||
add_library(syslib INTERFACE)
|
||||
target_sources(syslib INTERFACE ${SOURCES_PLATFORM_SPECIFIC})
|
||||
target_compile_features(syslib INTERFACE cxx_nullptr)
|
||||
target_sources(syslib INTERFACE ${SOURCES_PLATFORM_SPECIFIC} ${SOURCES_PLATFORM_COMMON})
|
||||
target_compile_features(syslib INTERFACE cxx_nullptr cxx_std_17)
|
||||
target_compile_features(syslib INTERFACE c_variadic_macros)
|
||||
target_link_libraries(syslib INTERFACE qcommon)
|
||||
|
||||
find_package(OpenSSL)
|
||||
|
||||
if (NOT ${OPENSSL_FOUND})
|
||||
message(WARNING "OpenSSL is not available, cpp-httplib won't work with SSL/TLS websites")
|
||||
endif()
|
||||
|
||||
add_subdirectory("../cpp-httplib-0.19.0" "cpp-httplib-0.19.0")
|
||||
target_link_libraries(syslib INTERFACE httplib)
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(syslib INTERFACE wsock32 ws2_32)
|
||||
target_link_libraries(syslib INTERFACE winmm)
|
||||
|
|
|
@ -22,13 +22,26 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
|
||||
#include "../qcommon/q_version.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Setting DEFAULT_BASEDIR to an empty string will make
|
||||
// Sys_DefaultInstallPath() return the current directory instead
|
||||
// This was the usual behavior in Quake III Arena.
|
||||
#define DEFAULT_BASEDIR ""
|
||||
|
||||
void Sys_InitEx();
|
||||
void Sys_ShutdownEx();
|
||||
|
||||
void Sys_PrepareBackTrace();
|
||||
void Sys_PrintBackTrace();
|
||||
void Sys_PlatformInit_New();
|
||||
void Sys_PlatformInit_New();
|
||||
|
||||
void Sys_UpdateChecker_Init();
|
||||
void Sys_UpdateChecker_Process();
|
||||
void Sys_UpdateChecker_Shutdown();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
#include "../sys_local.h"
|
||||
#include "../win_localization.h"
|
||||
#include "../sys_loadlib.h"
|
||||
#include "../sys_update_checker.h"
|
||||
|
||||
// a pointer to the last piece of data retrieved from the clipboard is stored here,
|
||||
// so that it can be cleaned up when new data is retrieved, preventing memory leaks
|
||||
|
@ -319,4 +320,15 @@ void VM_Forced_Unload_Done(void) {
|
|||
void Sys_InitEx()
|
||||
{
|
||||
Sys_InitLocalization();
|
||||
Sys_UpdateChecker_Init();
|
||||
}
|
||||
|
||||
void Sys_ShutdownEx()
|
||||
{
|
||||
Sys_UpdateChecker_Shutdown();
|
||||
}
|
||||
|
||||
void Sys_ProcessBackgroundTasks()
|
||||
{
|
||||
Sys_UpdateChecker_Process();
|
||||
}
|
||||
|
|
|
@ -268,6 +268,8 @@ Single exit point (regular exit or in case of error)
|
|||
*/
|
||||
static __attribute__ ((noreturn)) void Sys_Exit( int exitCode )
|
||||
{
|
||||
Sys_ShutdownEx();
|
||||
|
||||
CON_Shutdown( );
|
||||
|
||||
#ifndef DEDICATED
|
||||
|
|
250
code/sys/sys_update_checker.cpp
Normal file
250
code/sys/sys_update_checker.cpp
Normal file
|
@ -0,0 +1,250 @@
|
|||
/*
|
||||
===========================================================================
|
||||
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 "../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;
|
||||
client = NULL;
|
||||
thread = NULL;
|
||||
}
|
||||
|
||||
UpdateChecker::~UpdateChecker()
|
||||
{
|
||||
if (client) {
|
||||
Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateChecker::Init()
|
||||
{
|
||||
assert(!client);
|
||||
assert(!thread);
|
||||
|
||||
try {
|
||||
httplib::Client *updateCheckerClient = new httplib::Client("https://api.github.com");
|
||||
|
||||
client = updateCheckerClient;
|
||||
thread = new std::thread(&UpdateChecker::RequestThread, this);
|
||||
} catch (const std::exception& e) {
|
||||
client = NULL;
|
||||
thread = NULL;
|
||||
Com_DPrintf("Failed to create update checker: %s\n", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateChecker::Process()
|
||||
{
|
||||
std::chrono::time_point<std::chrono::steady_clock> currentTime = std::chrono::steady_clock::now();
|
||||
if (currentTime < lastMessageTime + std::chrono::milliseconds(std::max(1, com_updateCheckInterval->integer) * 60 * 1000)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CheckNewVersion()) {
|
||||
return;
|
||||
}
|
||||
lastMessageTime = currentTime;
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
void UpdateChecker::Shutdown()
|
||||
{
|
||||
ShutdownClient();
|
||||
ShutdownThread();
|
||||
}
|
||||
|
||||
void UpdateChecker::ShutdownClient()
|
||||
{
|
||||
std::lock_guard<std::shared_mutex> l(clientMutex);
|
||||
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
delete (httplib::Client *)client;
|
||||
client = NULL;
|
||||
}
|
||||
|
||||
void UpdateChecker::ShutdownThread()
|
||||
{
|
||||
if (!thread) {
|
||||
return;
|
||||
}
|
||||
|
||||
thread->join();
|
||||
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::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;
|
||||
}
|
||||
|
||||
void UpdateChecker::DoRequest()
|
||||
{
|
||||
std::lock_guard<std::shared_mutex> l(clientMutex);
|
||||
|
||||
httplib::Client *updateCheckerClient = (httplib::Client *)client;
|
||||
|
||||
if (!updateCheckerClient) {
|
||||
return;
|
||||
}
|
||||
|
||||
httplib::Result result = updateCheckerClient->Get("/repos/openmoh/openmohaa/releases/latest");
|
||||
if (result.error() != httplib::Error::Success) {
|
||||
return;
|
||||
}
|
||||
httplib::Response response = result.value();
|
||||
nlohmann::json data = nlohmann::json::parse(response.body);
|
||||
|
||||
try {
|
||||
const nlohmann::json::string_t& tagName = data.at("tag_name");
|
||||
|
||||
int major, minor, patch;
|
||||
ParseVersionNumber(tagName.c_str(), major, minor, patch);
|
||||
|
||||
lastMajor = major;
|
||||
lastMinor = minor;
|
||||
lastPatch = patch;
|
||||
|
||||
versionChecked = true;
|
||||
} catch (std::out_of_range&) {}
|
||||
}
|
||||
|
||||
void UpdateChecker::RequestThread()
|
||||
{
|
||||
std::chrono::time_point<std::chrono::steady_clock> currentTime = std::chrono::steady_clock::now();
|
||||
std::chrono::time_point<std::chrono::steady_clock> lastCheckTime;
|
||||
|
||||
while (client) {
|
||||
currentTime = std::chrono::steady_clock::now();
|
||||
if (currentTime >= lastCheckTime + std::chrono::milliseconds(std::max(1, com_updateCheckInterval->integer) * 60 * 1000)) {
|
||||
lastCheckTime = currentTime;
|
||||
|
||||
DoRequest();
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
}
|
||||
}
|
85
code/sys/sys_update_checker.h
Normal file
85
code/sys/sys_update_checker.h
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
===========================================================================
|
||||
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
|
||||
===========================================================================
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sys_local.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void Sys_UpdateChecker_Init();
|
||||
void Sys_UpdateChecker_Process();
|
||||
void Sys_UpdateChecker_Shutdown();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
# include <thread>
|
||||
# include <shared_mutex>
|
||||
# include <chrono>
|
||||
|
||||
class UpdateChecker
|
||||
{
|
||||
public:
|
||||
UpdateChecker();
|
||||
~UpdateChecker();
|
||||
|
||||
void Init();
|
||||
void Process();
|
||||
void Shutdown();
|
||||
|
||||
bool CheckNewVersion() const;
|
||||
|
||||
private:
|
||||
void ShutdownClient();
|
||||
void ShutdownThread();
|
||||
void RequestThread();
|
||||
void DoRequest();
|
||||
bool ParseVersionNumber(const char *value, int& major, int& minor, int& patch) const;
|
||||
|
||||
private:
|
||||
//
|
||||
// Version information
|
||||
//
|
||||
int lastMajor;
|
||||
int lastMinor;
|
||||
int lastPatch;
|
||||
bool versionChecked;
|
||||
|
||||
std::chrono::time_point<std::chrono::steady_clock> lastMessageTime;
|
||||
|
||||
//
|
||||
// Thread-related variables
|
||||
//
|
||||
void *client;
|
||||
std::shared_mutex clientMutex;
|
||||
std::thread *thread;
|
||||
};
|
||||
|
||||
extern UpdateChecker updateChecker;
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue