| 72 | #endif |
| 73 | |
| 74 | NewVersionInfo GetLatestVersion() |
| 75 | { |
| 76 | // If the check doesn't succeed, provide current version so we don't bother user |
| 77 | // with invalid data. |
| 78 | std::string tag = gVersionInfoTag; |
| 79 | NewVersionInfo verinfo{ tag, "", "" }; |
| 80 | #if !defined(DISABLE_HTTP) && !defined(DISABLE_VERSION_CHECKER) |
| 81 | auto now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); |
| 82 | auto then = Config::Get().general.lastVersionCheckTime; |
| 83 | using namespace std::chrono_literals; |
| 84 | |
| 85 | if (then < now - std::chrono::seconds(24h).count()) |
| 86 | { |
| 87 | Http::Request request; |
| 88 | request.url = "https://api.github.com/repos/OpenRCT2/OpenRCT2/releases/latest"; |
| 89 | request.method = Http::Method::GET; |
| 90 | |
| 91 | Http::Response res; |
| 92 | try |
| 93 | { |
| 94 | res = Do(request); |
| 95 | if (res.status != Http::Status::Ok) |
| 96 | throw std::runtime_error("bad http status"); |
| 97 | } |
| 98 | catch (std::exception& e) |
| 99 | { |
| 100 | Console::Error::WriteLine("Failed to download '%s', cause %s", request.url.c_str(), e.what()); |
| 101 | return {}; |
| 102 | } |
| 103 | |
| 104 | json_t root = Json::FromString(res.body); |
| 105 | |
| 106 | verinfo.tag = Json::GetString(root["tag_name"]); |
| 107 | verinfo.name = Json::GetString(root["name"]); |
| 108 | verinfo.changelog = Json::GetString(root["body"]); |
| 109 | |
| 110 | Config::Get().general.lastVersionCheckTime = now; |
| 111 | Config::Save(); |
| 112 | } |
| 113 | #endif |
| 114 | return verinfo; |
| 115 | } |