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

96 lines
3.0 KiB
C++
Raw Normal View History

/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 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
#ifdef OPENRCT2_BUILD_INFO_HEADER
2018-07-21 16:17:06 +02:00
# 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
"v" OPENRCT2_VERSION
#endif
;
2018-06-22 23:25:16 +02:00
const char gVersionInfoFull[] = OPENRCT2_NAME ", "
#ifdef OPENRCT2_VERSION_TAG
OPENRCT2_VERSION_TAG
#else
2018-06-22 23:25:16 +02:00
"v" OPENRCT2_VERSION
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
" ("
2018-07-21 16:17:06 +02:00
# if defined(OPENRCT2_BRANCH) && defined(OPENRCT2_COMMIT_SHA1_SHORT)
OPENRCT2_COMMIT_SHA1_SHORT " on " OPENRCT2_BRANCH
2018-07-21 16:17:06 +02:00
# elif defined(OPENRCT2_COMMIT_SHA1_SHORT)
OPENRCT2_COMMIT_SHA1_SHORT
2018-07-21 16:17:06 +02:00
# elif defined(OPENRCT2_BRANCH)
OPENRCT2_BRANCH
2018-07-21 16:17:06 +02:00
# endif
# ifndef NDEBUG
2018-06-22 23:25:16 +02:00
", DEBUG"
2018-07-21 16:17:06 +02:00
# 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
NewVersionInfo get_latest_version()
{
// 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();
2022-10-16 17:27:01 +02:00
auto then = gConfigGeneral.LastVersionCheckTime;
2020-07-21 23:22:48 +02:00
if (then < now - 24 * 60 * 60)
{
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
2022-10-16 17:27:01 +02:00
gConfigGeneral.LastVersionCheckTime = now;
ConfigSaveDefault();
2020-07-21 23:22:48 +02:00
}
#endif
return verinfo;
}