2023-02-28 01:15:26 +00:00
|
|
|
// AUTOGENERATED COPYRIGHT HEADER START
|
|
|
|
// Copyright (C) 2020-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
|
|
|
// AUTOGENERATED COPYRIGHT HEADER END
|
2020-09-09 03:06:15 +00:00
|
|
|
|
|
|
|
#include "updater.hpp"
|
|
|
|
#include "version.hpp"
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "configuration.hpp"
|
|
|
|
#include "plugin.hpp"
|
|
|
|
#include "util/util-logging.hpp"
|
|
|
|
|
|
|
|
#include "warning-disable.hpp"
|
2022-06-11 06:02:41 +00:00
|
|
|
#include <fstream>
|
2020-09-09 03:06:15 +00:00
|
|
|
#include <mutex>
|
2022-06-11 06:02:41 +00:00
|
|
|
#include <regex>
|
2020-09-09 03:06:15 +00:00
|
|
|
#include <string_view>
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-enable.hpp"
|
2021-09-07 02:29:41 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define ST_PREFIX "<%s> "
|
|
|
|
#define D_LOG_ERROR(x, ...) P_LOG_ERROR(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_WARNING(x, ...) P_LOG_WARN(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_INFO(x, ...) P_LOG_INFO(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_DEBUG(x, ...) P_LOG_DEBUG(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define ST_PREFIX "<updater> "
|
|
|
|
#define D_LOG_ERROR(...) P_LOG_ERROR(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_WARNING(...) P_LOG_WARN(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_INFO(...) P_LOG_INFO(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_DEBUG(...) P_LOG_DEBUG(ST_PREFIX __VA_ARGS__)
|
|
|
|
#endif
|
2020-09-09 03:06:15 +00:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// - Cache result in the configuration directory (not as a configuration value).
|
|
|
|
// - Move 'autoupdater.last_checked_at' to out of the configuration.
|
|
|
|
// - Figure out if nightly updates are viable at all.
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
#define ST_CFG_DATASHARING "updater.datasharing"
|
2020-09-09 03:06:15 +00:00
|
|
|
#define ST_CFG_AUTOMATION "updater.automation"
|
|
|
|
#define ST_CFG_CHANNEL "updater.channel"
|
|
|
|
#define ST_CFG_LASTCHECKEDAT "updater.lastcheckedat"
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
streamfx::version_stage streamfx::stage_from_string(std::string_view str)
|
|
|
|
{
|
|
|
|
if (str == "a") {
|
|
|
|
return version_stage::ALPHA;
|
|
|
|
} else if (str == "b") {
|
|
|
|
return version_stage::BETA;
|
|
|
|
} else if (str == "c") {
|
|
|
|
return version_stage::CANDIDATE;
|
|
|
|
} else {
|
|
|
|
return version_stage::STABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view streamfx::stage_to_string(version_stage t)
|
|
|
|
{
|
|
|
|
switch (t) {
|
|
|
|
case version_stage::ALPHA:
|
|
|
|
return "a";
|
|
|
|
case version_stage::BETA:
|
|
|
|
return "b";
|
|
|
|
case version_stage::CANDIDATE:
|
|
|
|
return "c";
|
|
|
|
default:
|
|
|
|
case version_stage::STABLE:
|
|
|
|
return ".";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void streamfx::to_json(nlohmann::json& json, const version_stage& stage)
|
|
|
|
{
|
|
|
|
json = stage_to_string(stage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void streamfx::from_json(const nlohmann::json& json, version_stage& stage)
|
|
|
|
{
|
|
|
|
stage = stage_from_string(json.get<std::string>());
|
|
|
|
}
|
|
|
|
|
2023-05-13 12:35:46 +00:00
|
|
|
streamfx::version_info::version_info() : major(0), minor(0), patch(0), tweak(0), stage(version_stage::STABLE), url(""), name("") {}
|
2022-06-11 06:02:41 +00:00
|
|
|
|
|
|
|
streamfx::version_info::version_info(const std::string text) : version_info()
|
|
|
|
{
|
|
|
|
// text can be:
|
|
|
|
// 0.0.0 (Stable)
|
|
|
|
// 0.0.0a0 (Testing)
|
|
|
|
// 0.0.0b0 (Testing)
|
|
|
|
// 0.0.0c0 (Testing)
|
|
|
|
// 0.0.0_0 (Development)
|
2023-05-13 12:35:46 +00:00
|
|
|
static const std::regex re_version("([0-9]+)\\.([0-9]+)\\.([0-9]+)(([\\._abc]{1,1})([0-9]+|)|)(-g([0-9a-fA-F]{8,8})|)");
|
|
|
|
std::smatch matches;
|
|
|
|
if (std::regex_match(text, matches, re_version, std::regex_constants::match_any | std::regex_constants::match_continuous)) {
|
2022-06-11 06:02:41 +00:00
|
|
|
major = static_cast<uint16_t>(strtoul(matches[1].str().c_str(), nullptr, 10));
|
|
|
|
minor = static_cast<uint16_t>(strtoul(matches[2].str().c_str(), nullptr, 10));
|
|
|
|
patch = static_cast<uint16_t>(strtoul(matches[3].str().c_str(), nullptr, 10));
|
|
|
|
if (matches.size() >= 5) {
|
|
|
|
stage = stage_from_string(matches[5].str());
|
|
|
|
tweak = static_cast<uint16_t>(strtoul(matches[6].str().c_str(), nullptr, 10));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw std::invalid_argument("Provided string is not a version.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void streamfx::to_json(nlohmann::json& json, const version_info& info)
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
|
|
|
auto version = nlohmann::json::object();
|
2022-06-11 06:02:41 +00:00
|
|
|
version["major"] = info.major;
|
|
|
|
version["minor"] = info.minor;
|
|
|
|
version["patch"] = info.patch;
|
|
|
|
version["type"] = info.stage;
|
|
|
|
version["tweak"] = info.tweak;
|
2020-09-09 03:06:15 +00:00
|
|
|
json["version"] = version;
|
|
|
|
json["url"] = info.url;
|
|
|
|
json["name"] = info.name;
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
void streamfx::from_json(const nlohmann::json& json, version_info& info)
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
if (json.find("html_url") == json.end()) {
|
|
|
|
auto version = json.at("version");
|
|
|
|
info.major = version.at("major").get<uint16_t>();
|
|
|
|
info.minor = version.at("minor").get<uint16_t>();
|
|
|
|
info.patch = version.at("patch").get<uint16_t>();
|
|
|
|
info.stage = version.at("type");
|
|
|
|
info.tweak = version.at("tweak").get<uint16_t>();
|
|
|
|
info.url = json.at("url").get<std::string>();
|
|
|
|
info.name = json.at("name").get<std::string>();
|
|
|
|
} else {
|
2020-09-09 03:06:15 +00:00
|
|
|
// This is a response from GitHub.
|
|
|
|
|
|
|
|
// Retrieve entries from the release object.
|
|
|
|
auto entry_tag_name = json.find("tag_name");
|
|
|
|
auto entry_name = json.find("name");
|
|
|
|
auto entry_url = json.find("html_url");
|
2022-06-11 06:02:41 +00:00
|
|
|
if ((entry_tag_name == json.end()) || (entry_name == json.end()) || (entry_url == json.end())) {
|
2020-09-09 03:06:15 +00:00
|
|
|
throw std::runtime_error("JSON is missing one or more required keys.");
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
// Parse the information.
|
|
|
|
std::string tag_name = entry_tag_name->get<std::string>();
|
|
|
|
info = {tag_name};
|
|
|
|
info.url = entry_url->get<std::string>();
|
|
|
|
info.name = entry_name->get<std::string>();
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
bool streamfx::version_info::is_older_than(const version_info other)
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
// 'true' if other is newer, otherwise false.
|
2023-01-17 12:04:40 +00:00
|
|
|
// Except for stable releases, this simply compares the numbers of the version.
|
2022-06-11 06:02:41 +00:00
|
|
|
|
2023-01-17 12:04:40 +00:00
|
|
|
// Compare Major version:
|
|
|
|
// - Theirs is greater: Remote is newer.
|
|
|
|
// - Ours is greater: Remote is older.
|
|
|
|
// - Continue the check.
|
2022-06-11 06:02:41 +00:00
|
|
|
if (major < other.major)
|
2020-09-09 03:06:15 +00:00
|
|
|
return true;
|
2023-01-17 12:04:40 +00:00
|
|
|
if (major > other.major)
|
2020-09-09 03:06:15 +00:00
|
|
|
return false;
|
2023-01-17 12:04:40 +00:00
|
|
|
|
|
|
|
// Compare Minor version:
|
|
|
|
// - Theirs is greater: Remote is newer.
|
|
|
|
// - Ours is greater: Remote is older.
|
|
|
|
// - Continue the check.
|
2022-06-11 06:02:41 +00:00
|
|
|
if (minor < other.minor)
|
2020-09-09 03:06:15 +00:00
|
|
|
return true;
|
2023-01-17 12:04:40 +00:00
|
|
|
if (minor > other.minor)
|
|
|
|
return false;
|
2020-09-09 03:06:15 +00:00
|
|
|
|
2023-01-17 12:04:40 +00:00
|
|
|
// Compare Patch version:
|
|
|
|
// - Theirs is greater: Remote is newer.
|
|
|
|
// - Ours is greater: Remote is older.
|
|
|
|
// - Continue the check.
|
|
|
|
if (patch < other.patch)
|
|
|
|
return true;
|
2022-06-11 06:02:41 +00:00
|
|
|
if (patch > other.patch)
|
2020-09-09 03:06:15 +00:00
|
|
|
return false;
|
2023-01-17 12:04:40 +00:00
|
|
|
|
|
|
|
// Compare Tweak and Stage version:
|
|
|
|
// - Theirs is greater: Remote is newer.
|
|
|
|
// - Ours is greater: Special logic.
|
|
|
|
// - Continue the check.
|
|
|
|
if (tweak < other.tweak)
|
2020-09-09 03:06:15 +00:00
|
|
|
return true;
|
2023-01-17 12:04:40 +00:00
|
|
|
if ((tweak > other.tweak) && (other.stage != version_stage::STABLE)) {
|
|
|
|
// If the remote isn't a stable release, it's always considered older.
|
|
|
|
return false;
|
2020-09-09 03:06:15 +00:00
|
|
|
|
2023-01-17 12:04:40 +00:00
|
|
|
// 0.12.0 vs 0.12.0
|
|
|
|
// Major: equal, continue
|
|
|
|
// Minor: equal, continue
|
|
|
|
// Patch: equal, continue
|
|
|
|
// Tweak: equal, continue
|
|
|
|
// Ours is older?
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a last effort, compare the stage.
|
|
|
|
// - Theirs is greater: Remote is older.
|
|
|
|
// - Ours is greater: Remote is newer.
|
|
|
|
// - Continue the check.
|
2022-06-11 06:02:41 +00:00
|
|
|
if (stage < other.stage)
|
2020-09-09 03:06:15 +00:00
|
|
|
return false;
|
2022-06-11 06:02:41 +00:00
|
|
|
if (stage > other.stage)
|
2020-09-09 03:06:15 +00:00
|
|
|
return true;
|
|
|
|
|
2023-01-17 12:04:40 +00:00
|
|
|
// If there are no further tests, assume this version is older.
|
2020-09-09 03:06:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
streamfx::version_info::operator std::string()
|
|
|
|
{
|
|
|
|
std::vector<char> buffer(25, 0);
|
|
|
|
if (stage != version_stage::STABLE) {
|
2022-08-28 11:44:00 +00:00
|
|
|
auto types = stage_to_string(stage);
|
2023-05-13 12:35:46 +00:00
|
|
|
int len = snprintf(buffer.data(), buffer.size(), "%" PRIu16 ".%" PRIu16 ".%" PRIu16 "%.1s%" PRIu16, major, minor, patch, types.data(), tweak);
|
2022-06-11 06:02:41 +00:00
|
|
|
return std::string(buffer.data(), buffer.data() + len);
|
|
|
|
} else {
|
2022-08-28 11:44:00 +00:00
|
|
|
int len = snprintf(buffer.data(), buffer.size(), "%" PRIu16 ".%" PRIu16 ".%" PRIu16, major, minor, patch);
|
2022-06-11 06:02:41 +00:00
|
|
|
return std::string(buffer.data(), buffer.data() + len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 16:30:15 +00:00
|
|
|
void streamfx::updater::task(streamfx::util::threadpool::task_data_t)
|
2022-08-27 11:17:47 +00:00
|
|
|
{
|
|
|
|
try {
|
2022-08-28 12:53:03 +00:00
|
|
|
auto query_fn = [](std::vector<char>& buffer) {
|
2023-05-13 12:35:46 +00:00
|
|
|
static constexpr std::string_view ST_API_URL = "https://api.github.com/repos/Xaymar/obs-StreamFX/releases?per_page=25&page=1";
|
2022-08-27 11:17:47 +00:00
|
|
|
|
|
|
|
streamfx::util::curl curl;
|
|
|
|
size_t buffer_offset = 0;
|
|
|
|
|
|
|
|
// Set headers (User-Agent is needed so Github can contact us!).
|
|
|
|
curl.set_header("User-Agent", "StreamFX Updater v" STREAMFX_VERSION_STRING);
|
|
|
|
curl.set_header("Accept", "application/vnd.github.v3+json");
|
|
|
|
|
|
|
|
// Set up request.
|
|
|
|
curl.set_option(CURLOPT_HTTPGET, true); // GET
|
2023-05-13 12:35:46 +00:00
|
|
|
curl.set_option(CURLOPT_POST, false); // Not POST
|
2022-08-27 11:17:47 +00:00
|
|
|
curl.set_option(CURLOPT_URL, ST_API_URL);
|
|
|
|
curl.set_option(CURLOPT_TIMEOUT, 30); // 10s until we fail.
|
|
|
|
|
|
|
|
// Callbacks
|
2022-08-28 12:53:03 +00:00
|
|
|
curl.set_write_callback([&buffer, &buffer_offset](void* data, size_t s1, size_t s2) {
|
2022-08-27 11:17:47 +00:00
|
|
|
size_t size = s1 * s2;
|
|
|
|
if (buffer.size() < (size + buffer_offset))
|
|
|
|
buffer.resize(buffer_offset + size);
|
|
|
|
|
|
|
|
memcpy(buffer.data() + buffer_offset, data, size);
|
|
|
|
buffer_offset += size;
|
|
|
|
|
|
|
|
return s1 * s2;
|
|
|
|
});
|
|
|
|
//std::bind(&streamfx::updater::task_write_cb, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)
|
|
|
|
|
|
|
|
// Clear any unknown data and reserve 64KiB of memory.
|
|
|
|
buffer.clear();
|
|
|
|
buffer.reserve(0xFFFF);
|
|
|
|
|
|
|
|
// Finally, execute the request.
|
|
|
|
D_LOG_DEBUG("Querying for latest releases...", "");
|
|
|
|
if (CURLcode res = curl.perform(); res != CURLE_OK) {
|
|
|
|
D_LOG_ERROR("Performing query failed with error: %s", curl_easy_strerror(res));
|
|
|
|
throw std::runtime_error(curl_easy_strerror(res));
|
|
|
|
}
|
2020-09-09 03:06:15 +00:00
|
|
|
|
2022-08-27 11:17:47 +00:00
|
|
|
int32_t status_code = 0;
|
|
|
|
if (CURLcode res = curl.get_info(CURLINFO_HTTP_CODE, status_code); res != CURLE_OK) {
|
|
|
|
D_LOG_ERROR("Retrieving status code failed with error: %s", curl_easy_strerror(res));
|
|
|
|
throw std::runtime_error(curl_easy_strerror(res));
|
|
|
|
}
|
|
|
|
D_LOG_DEBUG("API returned status code %d.", status_code);
|
2022-06-11 06:02:41 +00:00
|
|
|
|
2022-08-27 11:17:47 +00:00
|
|
|
if (status_code != 200) {
|
|
|
|
D_LOG_ERROR("API returned unexpected status code %d.", status_code);
|
|
|
|
throw std::runtime_error("Request failed due to one or more reasons.");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto parse_fn = [this](nlohmann::json json) {
|
|
|
|
// Check if it was parsed as an object.
|
|
|
|
if (json.type() != nlohmann::json::value_t::array) {
|
|
|
|
throw std::runtime_error("Invalid response from API.");
|
|
|
|
}
|
2022-06-11 06:02:41 +00:00
|
|
|
|
2022-08-27 11:17:47 +00:00
|
|
|
// Decide on the latest version for all update channels.
|
|
|
|
std::lock_guard<decltype(_lock)> lock(_lock);
|
|
|
|
_updates.clear();
|
|
|
|
for (auto obj : json) {
|
|
|
|
try {
|
|
|
|
auto info = obj.get<streamfx::version_info>();
|
|
|
|
|
|
|
|
switch (info.stage) {
|
|
|
|
case version_stage::STABLE:
|
|
|
|
if (get_update_info(version_stage::STABLE).is_older_than(info)) {
|
|
|
|
_updates.emplace(version_stage::STABLE, info);
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
case version_stage::CANDIDATE:
|
|
|
|
if (get_update_info(version_stage::CANDIDATE).is_older_than(info)) {
|
|
|
|
_updates.emplace(version_stage::CANDIDATE, info);
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
case version_stage::BETA:
|
|
|
|
if (get_update_info(version_stage::BETA).is_older_than(info)) {
|
|
|
|
_updates.emplace(version_stage::BETA, info);
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
case version_stage::ALPHA:
|
|
|
|
if (get_update_info(version_stage::ALPHA).is_older_than(info)) {
|
|
|
|
_updates.emplace(version_stage::ALPHA, info);
|
|
|
|
}
|
2022-06-11 06:02:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 11:17:47 +00:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
D_LOG_DEBUG("Failed to parse entry, error: %s", ex.what());
|
|
|
|
}
|
2022-06-11 06:02:41 +00:00
|
|
|
}
|
2022-08-27 11:17:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
{ // Query and parse the response.
|
|
|
|
nlohmann::json json;
|
|
|
|
|
|
|
|
// Query the API or parse a crafted response.
|
|
|
|
auto debug_path = streamfx::config_file_path("github_release_query_response.json");
|
|
|
|
if (std::filesystem::exists(debug_path)) {
|
|
|
|
std::ifstream fs{debug_path};
|
|
|
|
json = nlohmann::json::parse(fs);
|
|
|
|
fs.close();
|
|
|
|
} else {
|
|
|
|
std::vector<char> buffer;
|
|
|
|
query_fn(buffer);
|
|
|
|
json = nlohmann::json::parse(buffer.begin(), buffer.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the JSON response from the API.
|
|
|
|
parse_fn(json);
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
2022-06-11 06:02:41 +00:00
|
|
|
|
2022-08-27 11:17:47 +00:00
|
|
|
// Print all update information to the log file.
|
|
|
|
D_LOG_INFO("Current Version: %s", static_cast<std::string>(_current_info).c_str());
|
2023-05-13 12:35:46 +00:00
|
|
|
D_LOG_INFO("Latest Stable Version: %s", static_cast<std::string>(get_update_info(version_stage::STABLE)).c_str());
|
|
|
|
D_LOG_INFO("Latest Candidate Version: %s", static_cast<std::string>(get_update_info(version_stage::CANDIDATE)).c_str());
|
2022-08-27 11:17:47 +00:00
|
|
|
D_LOG_INFO("Latest Beta Version: %s", static_cast<std::string>(get_update_info(version_stage::BETA)).c_str());
|
|
|
|
D_LOG_INFO("Latest Alpha Version: %s", static_cast<std::string>(get_update_info(version_stage::ALPHA)).c_str());
|
|
|
|
if (is_update_available()) {
|
|
|
|
D_LOG_INFO("Update is available.", "");
|
|
|
|
}
|
2022-06-11 06:02:41 +00:00
|
|
|
|
2022-08-27 11:17:47 +00:00
|
|
|
// Notify listeners of the update.
|
|
|
|
events.refreshed.call(*this);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
// Notify about the error.
|
|
|
|
std::string message = ex.what();
|
|
|
|
events.error.call(*this, message);
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool streamfx::updater::can_check()
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
return true;
|
|
|
|
#else
|
2023-05-13 12:35:46 +00:00
|
|
|
auto now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch());
|
2020-09-09 03:06:15 +00:00
|
|
|
auto threshold = (_lastcheckedat + std::chrono::minutes(10));
|
|
|
|
return (now > threshold);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void streamfx::updater::load()
|
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
std::lock_guard<decltype(_lock)> lock(_lock);
|
2020-09-09 03:06:15 +00:00
|
|
|
if (auto config = streamfx::configuration::instance(); config) {
|
|
|
|
auto dataptr = config->get();
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
if (obs_data_has_user_value(dataptr.get(), ST_CFG_DATASHARING))
|
|
|
|
_data_sharing_allowed = obs_data_get_bool(dataptr.get(), ST_CFG_DATASHARING);
|
2020-09-09 03:06:15 +00:00
|
|
|
if (obs_data_has_user_value(dataptr.get(), ST_CFG_AUTOMATION))
|
|
|
|
_automation = obs_data_get_bool(dataptr.get(), ST_CFG_AUTOMATION);
|
|
|
|
if (obs_data_has_user_value(dataptr.get(), ST_CFG_CHANNEL))
|
2022-06-11 06:02:41 +00:00
|
|
|
_channel = static_cast<version_stage>(obs_data_get_int(dataptr.get(), ST_CFG_CHANNEL));
|
2020-09-09 03:06:15 +00:00
|
|
|
if (obs_data_has_user_value(dataptr.get(), ST_CFG_LASTCHECKEDAT))
|
|
|
|
_lastcheckedat = std::chrono::seconds(obs_data_get_int(dataptr.get(), ST_CFG_LASTCHECKEDAT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void streamfx::updater::save()
|
|
|
|
{
|
|
|
|
if (auto config = streamfx::configuration::instance(); config) {
|
|
|
|
auto dataptr = config->get();
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
obs_data_set_bool(dataptr.get(), ST_CFG_DATASHARING, _data_sharing_allowed);
|
2020-09-09 03:06:15 +00:00
|
|
|
obs_data_set_bool(dataptr.get(), ST_CFG_AUTOMATION, _automation);
|
|
|
|
obs_data_set_int(dataptr.get(), ST_CFG_CHANNEL, static_cast<long long>(_channel));
|
|
|
|
obs_data_set_int(dataptr.get(), ST_CFG_LASTCHECKEDAT, static_cast<long long>(_lastcheckedat.count()));
|
2022-09-18 17:53:11 +00:00
|
|
|
|
|
|
|
config->save();
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
streamfx::updater::updater()
|
|
|
|
: _lock(), _task(),
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
_data_sharing_allowed(false), _automation(true), _channel(version_stage::STABLE), _lastcheckedat(),
|
2020-09-09 03:06:15 +00:00
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
_current_info(), _updates(), _dirty(false)
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
|
|
|
// Load information from configuration.
|
|
|
|
load();
|
|
|
|
|
|
|
|
// Build current version information.
|
|
|
|
try {
|
2022-06-11 06:02:41 +00:00
|
|
|
_current_info = {STREAMFX_VERSION_STRING};
|
2020-09-09 03:06:15 +00:00
|
|
|
} catch (...) {
|
2021-09-07 02:29:41 +00:00
|
|
|
D_LOG_ERROR("Failed to parse current version information, results may be inaccurate.", "");
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
streamfx::updater::~updater()
|
|
|
|
{
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
bool streamfx::updater::is_data_sharing_allowed()
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
return _data_sharing_allowed;
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
void streamfx::updater::set_data_sharing_allowed(bool value)
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
_dirty = true;
|
|
|
|
_data_sharing_allowed = value;
|
|
|
|
events.gdpr_changed(*this, _data_sharing_allowed);
|
2020-09-09 03:06:15 +00:00
|
|
|
|
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
std::lock_guard<decltype(_lock)> lock(_lock);
|
2020-09-09 03:06:15 +00:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
D_LOG_INFO("User %s the processing of data.", _data_sharing_allowed ? "allowed" : "disallowed");
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
bool streamfx::updater::is_automated()
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
|
|
|
return _automation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void streamfx::updater::set_automation(bool value)
|
|
|
|
{
|
|
|
|
_automation = value;
|
|
|
|
events.automation_changed(*this, _automation);
|
|
|
|
|
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
std::lock_guard<decltype(_lock)> lock(_lock);
|
2020-09-09 03:06:15 +00:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
D_LOG_INFO("Automatic checks at launch are now %s.", value ? "enabled" : "disabled");
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
streamfx::version_stage streamfx::updater::get_channel()
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
|
|
|
return _channel;
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
void streamfx::updater::set_channel(version_stage value)
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
std::lock_guard<decltype(_lock)> lock(_lock);
|
2020-09-09 03:06:15 +00:00
|
|
|
|
|
|
|
_dirty = true;
|
|
|
|
_channel = value;
|
|
|
|
events.channel_changed(*this, _channel);
|
|
|
|
|
|
|
|
save();
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
D_LOG_INFO("Update channel changed to '%s'.", stage_to_string(value).data());
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void streamfx::updater::refresh()
|
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
if (!_task.expired() || !is_data_sharing_allowed()) {
|
2020-09-09 03:06:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (can_check()) {
|
2022-06-11 06:02:41 +00:00
|
|
|
std::lock_guard<decltype(_lock)> lock(_lock);
|
2020-09-09 03:06:15 +00:00
|
|
|
|
|
|
|
// Update last checked time.
|
2023-05-13 12:35:46 +00:00
|
|
|
_lastcheckedat = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch());
|
2020-09-09 03:06:15 +00:00
|
|
|
save();
|
|
|
|
|
|
|
|
// Spawn a new task.
|
|
|
|
_task = streamfx::threadpool()->push(std::bind(&streamfx::updater::task, this, std::placeholders::_1), nullptr);
|
|
|
|
} else {
|
|
|
|
events.refreshed(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
bool streamfx::updater::is_update_available()
|
|
|
|
{
|
|
|
|
return _current_info.is_older_than(get_update_info());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool streamfx::updater::is_update_available(version_stage channel)
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
return _current_info.is_older_than(get_update_info(channel));
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
streamfx::version_info streamfx::updater::get_current_info()
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
|
|
|
return _current_info;
|
|
|
|
}
|
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
streamfx::version_info streamfx::updater::get_update_info()
|
2020-09-09 03:06:15 +00:00
|
|
|
{
|
2022-06-11 06:02:41 +00:00
|
|
|
return get_update_info(_channel);
|
|
|
|
}
|
2020-09-09 03:06:15 +00:00
|
|
|
|
2022-06-11 06:02:41 +00:00
|
|
|
streamfx::version_info streamfx::updater::get_update_info(version_stage channel)
|
|
|
|
{
|
|
|
|
std::lock_guard<decltype(_lock)> lock(_lock);
|
|
|
|
if (auto iter = _updates.find(channel); iter != _updates.end()) {
|
|
|
|
return iter->second;
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
2020-09-09 03:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<streamfx::updater> streamfx::updater::instance()
|
|
|
|
{
|
|
|
|
static std::weak_ptr<streamfx::updater> _instance;
|
|
|
|
static std::mutex _lock;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(_lock);
|
|
|
|
if (_instance.expired()) {
|
|
|
|
auto ptr = std::make_shared<streamfx::updater>();
|
|
|
|
_instance = ptr;
|
|
|
|
return ptr;
|
|
|
|
} else {
|
|
|
|
return _instance.lock();
|
|
|
|
}
|
|
|
|
}
|