Files
OpenRCT2-Web/src/openrct2/Version.cpp
T

110 lines
3.3 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "Version.h"
2020-07-21 23:22:48 +02:00
#include "config/Config.h"
#include "core/Console.hpp"
#include "core/Http.h"
#include "core/Json.hpp"
#include <chrono>
2018-06-22 23:25:16 +02:00
2024-05-10 12:10:21 +02:00
using namespace OpenRCT2;
#ifdef OPENRCT2_BUILD_INFO_HEADER
#include OPENRCT2_BUILD_INFO_HEADER
#endif
2020-07-21 23:22:48 +02:00
const char gVersionInfoTag[] =
#ifdef OPENRCT2_VERSION_TAG
OPENRCT2_VERSION_TAG
#else
2025-01-19 16:59:45 +01:00
"v" kOpenRCT2Version
2020-07-21 23:22:48 +02:00
#endif
;
2018-06-22 23:25:16 +02:00
const char gVersionInfoFull[] = OPENRCT2_NAME ", "
#ifdef OPENRCT2_VERSION_TAG
OPENRCT2_VERSION_TAG
#else
2025-01-19 16:59:45 +01:00
"v" kOpenRCT2Version
2017-04-17 21:38:35 -05:00
#endif
#if defined(OPENRCT2_BRANCH) || defined(OPENRCT2_COMMIT_SHA1_SHORT) || !defined(NDEBUG)
2018-06-22 23:25:16 +02:00
" ("
#if defined(OPENRCT2_BRANCH) && defined(OPENRCT2_COMMIT_SHA1_SHORT)
OPENRCT2_COMMIT_SHA1_SHORT " on " OPENRCT2_BRANCH
#elif defined(OPENRCT2_COMMIT_SHA1_SHORT)
OPENRCT2_COMMIT_SHA1_SHORT
#elif defined(OPENRCT2_BRANCH)
OPENRCT2_BRANCH
#endif
#ifndef NDEBUG
2018-06-22 23:25:16 +02:00
", DEBUG"
#endif
2018-06-22 23:25:16 +02:00
")"
2017-04-17 21:38:35 -05:00
#endif
#ifdef OPENRCT2_BUILD_SERVER
2018-06-22 23:25:16 +02:00
" provided by " OPENRCT2_BUILD_SERVER
#endif
2017-04-17 21:38:35 -05:00
;
2020-07-21 23:22:48 +02:00
2025-01-10 12:07:16 -06:00
#ifdef __EMSCRIPTEN__
// This must be wrapped in extern "C", according to the emscripten docs, "to prevent C++ name mangling"
extern "C" {
const char* GetVersion()
{
return gVersionInfoFull;
}
}
#endif
NewVersionInfo GetLatestVersion()
2020-07-21 23:22:48 +02:00
{
// If the check doesn't succeed, provide current version so we don't bother user
// with invalid data.
std::string tag = gVersionInfoTag;
NewVersionInfo verinfo{ tag, "", "", "" };
#ifndef DISABLE_HTTP
auto now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
2024-05-10 12:10:21 +02:00
auto then = Config::Get().general.LastVersionCheckTime;
using namespace std::chrono_literals;
if (then < now - std::chrono::seconds(24h).count())
2020-07-21 23:22:48 +02:00
{
Http::Request request;
request.url = "https://api.github.com/repos/OpenRCT2/OpenRCT2/releases/latest";
request.method = Http::Method::GET;
Http::Response res;
try
{
res = Do(request);
if (res.status != Http::Status::Ok)
2020-07-21 23:22:48 +02:00
throw std::runtime_error("bad http status");
}
catch (std::exception& e)
{
Console::Error::WriteLine("Failed to download '%s', cause %s", request.url.c_str(), e.what());
return {};
}
2020-08-18 14:27:35 +01:00
json_t root = Json::FromString(res.body);
2020-07-21 23:22:48 +02:00
2020-08-18 14:27:35 +01:00
verinfo.tag = Json::GetString(root["tag_name"]);
verinfo.name = Json::GetString(root["name"]);
verinfo.changelog = Json::GetString(root["body"]);
verinfo.url = Json::GetString(root["html_url"]);
2020-07-21 23:22:48 +02:00
2024-05-10 12:10:21 +02:00
Config::Get().general.LastVersionCheckTime = now;
Config::Save();
2020-07-21 23:22:48 +02:00
}
#endif
return verinfo;
}