2 #ifndef ENIGMA_NETWORKING_CHECK_FOR_UPDATES_H
3 #define ENIGMA_NETWORKING_CHECK_FOR_UPDATES_H
11 #pragma warning(push, 0)
15 #include <nlohmann/json.hpp>
35 void Parse(
const std::string& tag_name) {
37 ENIGMA_ASSERT_OR_THROW(parts.size() == 3, fmt::format(
"Version tag_name is malformed, expected major.minor.patch (%d.%d.%d) but received {}", tag_name));
40 const auto stoui16 = [](
const std::string& str) -> std::uint16_t {
42 std::stringstream oss(str);
47 major = stoui16(parts[0]);
48 minor = stoui16(parts[1]);
49 patch = stoui16(parts[2]);
54 if (
major > other.major)
return true;
55 if (
major < other.major)
return false;
57 if (
minor > other.minor)
return true;
58 if (
minor < other.minor)
return false;
60 return patch > other.patch;
64 if (
major < other.major)
return true;
65 if (
major > other.major)
return false;
67 if (
minor < other.minor)
return true;
68 if (
minor > other.minor)
return false;
70 return patch < other.patch;
74 return major == other.major &&
minor == other.minor &&
patch == other.patch;
78 return os << v.major <<
'.' << v.minor <<
'.' << v.patch;
106 #define CC1(a, b) a##b
107 #define CC(a, b) CC1(a, b)
108 #define ASSIGN_IF(var, field, json_type) \
109 if (!obj[field].is_null() && CC(obj[field].is_, json_type)()) \
110 (var) = obj[field].get<decltype(var)>();
112 std::unique_ptr<LatestReleaseInfo> info = std::make_unique<LatestReleaseInfo>();
114 ASSIGN_IF(info->tag_name,
"tag_name",
string);
115 ASSIGN_IF(info->created_at,
"created_at",
string);
116 ASSIGN_IF(info->published_at,
"published_at",
string);
118 ASSIGN_IF(info->tarball_url,
"tarball_url",
string);
119 ASSIGN_IF(info->zipball_url,
"zipball_url",
string);
121 info->version.Parse(info->tag_name);
130 std::ostringstream oss;
132 oss <<
"name: " <<
name <<
" ";
134 oss <<
"tag name: " <<
tag_name <<
" ";
140 oss <<
"body: " <<
body <<
" ";
153 cpr::Url url = Constants::Links::ENIGMA_GITHUB_API_LATEST_RELEASE;
154 cpr::Response response = cpr::Get(url);
155 if (response.status_code == cpr::status::HTTP_OK) {
156 nlohmann::json data = nlohmann::json::parse(response.text);
159 ENIGMA_WARN(
"Failed to get latest release info with status code {0}", response.status_code);
160 }
catch (
const std::exception& e) {
161 ENIGMA_WARN(
"Failed to get latest release info with exception {0}", e.what());
#define ASSIGN_IF(var, field, json_type)
#define NS_ENIGMA_BEGIN
Enable/Disable Assertions.
#define ENIGMA_ASSERT_OR_THROW(x, msg)
static std::unique_ptr< LatestReleaseInfo > GetLatestReleaseInfo()
static std::vector< StringType > Split(const StringType &str, const typename std::string::value_type delimiter)
LatestReleaseInfo() noexcept
static std::unique_ptr< LatestReleaseInfo > FromJson(const nlohmann::json &obj)
~LatestReleaseInfo() noexcept=default
std::string toString() noexcept
Version(const std::string &tag_name)
friend std::ostream & operator<<(std::ostream &os, const Version &v) noexcept
constexpr bool operator==(const Version &other) const noexcept
void Parse(const std::string &tag_name)
constexpr bool operator<(const Version &other) const noexcept
constexpr bool operator>(const Version &other) const noexcept