2017-02-03 14:11:58 +01:00
|
|
|
/*****************************************************************************
|
2022-10-01 09:42:14 +02:00
|
|
|
* Copyright (c) 2014-2022 OpenRCT2 developers
|
2017-02-03 14:11:58 +01:00
|
|
|
*
|
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
|
2017-02-03 14:11:58 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2017-02-03 14:11:58 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#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
|
|
|
|
2017-02-03 14:11:58 +01:00
|
|
|
#ifdef OPENRCT2_BUILD_INFO_HEADER
|
2018-07-21 16:17:06 +02:00
|
|
|
# include OPENRCT2_BUILD_INFO_HEADER
|
2017-02-03 14:11:58 +01:00
|
|
|
#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 ", "
|
2018-06-12 00:25:04 +02:00
|
|
|
#ifdef OPENRCT2_VERSION_TAG
|
|
|
|
OPENRCT2_VERSION_TAG
|
|
|
|
#else
|
2018-06-22 23:25:16 +02:00
|
|
|
"v" OPENRCT2_VERSION
|
2017-04-18 04:38:35 +02:00
|
|
|
#endif
|
2018-06-12 00:25:04 +02:00
|
|
|
#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)
|
2018-06-12 00:25:04 +02:00
|
|
|
OPENRCT2_COMMIT_SHA1_SHORT " on " OPENRCT2_BRANCH
|
2018-07-21 16:17:06 +02:00
|
|
|
# elif defined(OPENRCT2_COMMIT_SHA1_SHORT)
|
2018-06-12 00:25:04 +02:00
|
|
|
OPENRCT2_COMMIT_SHA1_SHORT
|
2018-07-21 16:17:06 +02:00
|
|
|
# elif defined(OPENRCT2_BRANCH)
|
2018-06-12 00:25:04 +02:00
|
|
|
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-18 04:38:35 +02:00
|
|
|
#endif
|
|
|
|
#ifdef OPENRCT2_BUILD_SERVER
|
2018-06-22 23:25:16 +02:00
|
|
|
" provided by " OPENRCT2_BUILD_SERVER
|
2017-02-03 14:11:58 +01:00
|
|
|
#endif
|
2017-04-18 04:38:35 +02: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();
|
|
|
|
auto then = gConfigGeneral.last_version_check_time;
|
|
|
|
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);
|
2020-10-15 03:04:39 +02:00
|
|
|
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 15:27:35 +02:00
|
|
|
json_t root = Json::FromString(res.body);
|
2020-07-21 23:22:48 +02:00
|
|
|
|
2020-08-18 15:27:35 +02: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
|
|
|
|
|
|
|
gConfigGeneral.last_version_check_time = now;
|
|
|
|
config_save_default();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return verinfo;
|
|
|
|
}
|