-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpdateChecker.cpp
More file actions
142 lines (117 loc) · 3.56 KB
/
Copy pathUpdateChecker.cpp
File metadata and controls
142 lines (117 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "UpdateChecker.h"
#include <string_view>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <sstream>
Version Version::Parse(std::string_view str)
{
VersionStage stage = RELEASE;
if (str.starts_with('v')) str.remove_prefix(1);
if (str.ends_with('a'))
{
stage = ALPHA;
str.remove_suffix(1);
}
if (str.ends_with('b'))
{
stage = BETA;
str.remove_suffix(1);
}
Version v{};
std::sscanf(str.data(), "%d.%d.%d", &v.major, &v.minor, &v.patch);
v.stage = stage;
return v;
}
const std::string Version::ToString() const
{
std::ostringstream result;
result << "v" << major << "." << minor << "." << patch;
if (stage == ALPHA) result << "-Alpha";
if (stage == BETA) result << "-Beta";
return result.str();
}
UpdateChecker::UpdateCheckResult UpdateChecker::CheckForUpdate()
{
#pragma region Github version retrieval
std::cout << "Update check begin." << std::endl;
try
{
CURL* curl = curl_easy_init();
if (!curl)
{
std::cout << " Failed to initialize Curl" << std::endl;
return { UpdateStatus::CHECK_ERROR, {} };
}
std::string response;
curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/MrCHB1/Comet/releases/latest");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Comet");
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
+[](char* ptr, size_t size, size_t nmemb, void* userdata)
{
auto* str = static_cast<std::string*>(userdata);
str->append(ptr, size * nmemb);
return size * nmemb;
});
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
CURLcode result = curl_easy_perform(curl); // CRASHES HERE
long httpCode = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
curl_easy_cleanup(curl);
if (result != CURLE_OK)
{
std::cout << " Curl error: "
<< curl_easy_strerror(result) << std::endl;
return { UpdateStatus::CHECK_ERROR, {} };
}
if (httpCode != 200)
{
std::cout << " GitHub returned HTTP " << httpCode << "." << std::endl;
return { UpdateStatus::CHECK_ERROR, {} };
}
#pragma endregion
auto json = nlohmann::json::parse(response, nullptr, false);
if (json.is_discarded())
{
std::cout << " Invalid JSON retrieved." << std::endl;
return { UpdateStatus::CHECK_ERROR, {} };
}
std::string latestVersionStr = json.value("tag_name", "");
std::string latestVersionURL = json.value("html_url", "");
if (latestVersionStr.empty())
{
std::cout << " JSON retrieved but did not have a tag." << std::endl;
return { UpdateStatus::CHECK_ERROR, {} };
}
Version latestVersion = Version::Parse(latestVersionStr);
if (latestVersion > currVersion)
{
std::cout << " Update available: "
<< latestVersionStr << std::endl;
return { UpdateStatus::UPDATE_AVAILABLE, latestVersion, latestVersionURL };
}
if (latestVersion < currVersion)
{
std::cout << " Latest release is behind this build." << std::endl;
}
else
{
std::cout << " Already up to date." << std::endl;
}
std::cout << " Retrieved version: " << latestVersionStr << std::endl;
std::cout << " Build (current) version: " << COMET_VERSION << std::endl;
return { UpdateStatus::UP_TO_DATE, latestVersion, latestVersionURL };
}
catch (const std::exception& e)
{
std::cout << " Exception: " << e.what() << std::endl;
return { UpdateStatus::CHECK_ERROR, {} };
}
catch (...)
{
std::cout << " Something went wrong while trying to check for an update." << std::endl;
return { UpdateStatus::CHECK_ERROR, {} };
}
}