Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/project_manager/engine_update_label.cpp
20829 views
1
/**************************************************************************/
2
/* engine_update_label.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "engine_update_label.h"
32
33
#include "core/io/json.h"
34
#include "core/version.h"
35
#include "editor/editor_string_names.h"
36
#include "editor/settings/editor_settings.h"
37
#include "scene/main/http_request.h"
38
39
bool EngineUpdateLabel::_can_check_updates() const {
40
return int(EDITOR_GET("network/connection/network_mode")) == EditorSettings::NETWORK_ONLINE &&
41
UpdateMode(int(EDITOR_GET("network/connection/check_for_updates"))) != UpdateMode::DISABLED;
42
}
43
44
void EngineUpdateLabel::_check_update() {
45
checked_update = true;
46
_set_status(UpdateStatus::BUSY);
47
http->request("https://godotengine.org/versions.json");
48
}
49
50
void EngineUpdateLabel::_http_request_completed(int p_result, int p_response_code, const PackedStringArray &p_headers, const PackedByteArray &p_body) {
51
if (p_result != OK) {
52
_set_status(UpdateStatus::ERROR);
53
_set_message(vformat(TTR("Failed to check for updates. Error: %d."), p_result), theme_cache.error_color);
54
return;
55
}
56
57
if (p_response_code != 200) {
58
_set_status(UpdateStatus::ERROR);
59
_set_message(vformat(TTR("Failed to check for updates. Response code: %d."), p_response_code), theme_cache.error_color);
60
return;
61
}
62
63
Array version_array;
64
{
65
const uint8_t *r = p_body.ptr();
66
String s = String::utf8((const char *)r, p_body.size());
67
68
Variant result = JSON::parse_string(s);
69
if (result == Variant()) {
70
_set_status(UpdateStatus::ERROR);
71
_set_message(TTR("Failed to parse version JSON."), theme_cache.error_color);
72
return;
73
}
74
if (result.get_type() != Variant::ARRAY) {
75
_set_status(UpdateStatus::ERROR);
76
_set_message(TTR("Received JSON data is not a valid version array."), theme_cache.error_color);
77
return;
78
}
79
version_array = result;
80
}
81
82
UpdateMode update_mode = UpdateMode(int(EDITOR_GET("network/connection/check_for_updates")));
83
if (update_mode == UpdateMode::AUTO) {
84
if (_get_version_type(GODOT_VERSION_STATUS) == VersionType::STABLE) {
85
update_mode = UpdateMode::NEWEST_STABLE;
86
} else {
87
update_mode = UpdateMode::NEWEST_UNSTABLE;
88
}
89
}
90
bool stable_only = update_mode == UpdateMode::NEWEST_STABLE || update_mode == UpdateMode::NEWEST_PATCH;
91
92
available_newer_version = String();
93
for (const Variant &data_bit : version_array) {
94
const Dictionary version_info = data_bit;
95
96
const String base_version_string = version_info.get("name", "");
97
const PackedStringArray version_bits = base_version_string.split(".");
98
99
if (version_bits.size() < 2) {
100
continue;
101
}
102
103
int minor = version_bits[1].to_int();
104
if (version_bits[0].to_int() != GODOT_VERSION_MAJOR || minor < GODOT_VERSION_MINOR) {
105
continue;
106
}
107
108
int patch = 0;
109
if (version_bits.size() >= 3) {
110
patch = version_bits[2].to_int();
111
}
112
113
if (minor == GODOT_VERSION_MINOR && patch < GODOT_VERSION_PATCH) {
114
continue;
115
}
116
117
if (update_mode == UpdateMode::NEWEST_PATCH && minor > GODOT_VERSION_MINOR) {
118
continue;
119
}
120
121
const Array releases = version_info.get("releases", Array());
122
if (releases.is_empty()) {
123
continue;
124
}
125
126
const Dictionary newest_release = releases[0];
127
const String release_string = newest_release.get("name", "unknown");
128
129
int release_index;
130
VersionType release_type = _get_version_type(release_string, &release_index);
131
132
if (minor > GODOT_VERSION_MINOR || patch > GODOT_VERSION_PATCH) {
133
if (stable_only && release_type != VersionType::STABLE) {
134
continue;
135
}
136
137
available_newer_version = vformat("%s-%s", base_version_string, release_string);
138
break;
139
}
140
141
int current_version_index;
142
VersionType current_version_type = _get_version_type(GODOT_VERSION_STATUS, &current_version_index);
143
144
if (int(release_type) > int(current_version_type)) {
145
break;
146
}
147
148
if (int(release_type) == int(current_version_type) && release_index <= current_version_index) {
149
break;
150
}
151
152
available_newer_version = vformat("%s-%s", base_version_string, release_string);
153
break;
154
}
155
156
if (!available_newer_version.is_empty()) {
157
_set_status(UpdateStatus::UPDATE_AVAILABLE);
158
_set_message(vformat(TTR("Update available: %s."), available_newer_version), theme_cache.update_color);
159
} else if (available_newer_version.is_empty()) {
160
_set_status(UpdateStatus::UP_TO_DATE);
161
}
162
}
163
164
void EngineUpdateLabel::_set_message(const String &p_message, const Color &p_color) {
165
if (is_disabled()) {
166
add_theme_color_override("font_disabled_color", p_color);
167
} else {
168
add_theme_color_override(SceneStringName(font_color), p_color);
169
}
170
set_text(p_message);
171
}
172
173
void EngineUpdateLabel::_set_status(UpdateStatus p_status) {
174
status = p_status;
175
if (status == UpdateStatus::BUSY || status == UpdateStatus::UP_TO_DATE) {
176
// Hide the label to prevent unnecessary distraction.
177
hide();
178
return;
179
} else {
180
show();
181
}
182
183
switch (status) {
184
case UpdateStatus::OFFLINE: {
185
set_disabled(false);
186
if (int(EDITOR_GET("network/connection/network_mode")) == EditorSettings::NETWORK_OFFLINE) {
187
_set_message(TTR("Offline mode, update checks disabled."), theme_cache.disabled_color);
188
} else {
189
_set_message(TTR("Update checks disabled."), theme_cache.disabled_color);
190
}
191
set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_OFF);
192
set_tooltip_text("");
193
break;
194
}
195
196
case UpdateStatus::ERROR: {
197
set_disabled(false);
198
set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE);
199
set_tooltip_text(TTR("An error has occurred. Click to try again."));
200
} break;
201
202
case UpdateStatus::UPDATE_AVAILABLE: {
203
set_disabled(false);
204
set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE);
205
set_tooltip_text(TTR("Click to open download page."));
206
} break;
207
208
default: {
209
}
210
}
211
}
212
213
EngineUpdateLabel::VersionType EngineUpdateLabel::_get_version_type(const String &p_string, int *r_index) const {
214
VersionType type = VersionType::UNKNOWN;
215
String index_string;
216
217
static HashMap<String, VersionType> type_map;
218
if (type_map.is_empty()) {
219
type_map["stable"] = VersionType::STABLE;
220
type_map["rc"] = VersionType::RC;
221
type_map["beta"] = VersionType::BETA;
222
type_map["alpha"] = VersionType::ALPHA;
223
type_map["dev"] = VersionType::DEV;
224
}
225
226
for (const KeyValue<String, VersionType> &kv : type_map) {
227
if (p_string.begins_with(kv.key)) {
228
index_string = p_string.trim_prefix(kv.key);
229
type = kv.value;
230
break;
231
}
232
}
233
234
if (r_index) {
235
if (index_string.is_empty()) {
236
*r_index = DEV_VERSION;
237
} else {
238
*r_index = index_string.to_int();
239
}
240
}
241
return type;
242
}
243
244
String EngineUpdateLabel::_extract_sub_string(const String &p_line) const {
245
int j = p_line.find_char('"') + 1;
246
return p_line.substr(j, p_line.find_char('"', j) - j);
247
}
248
249
void EngineUpdateLabel::_notification(int p_what) {
250
switch (p_what) {
251
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
252
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("network/connection")) {
253
break;
254
}
255
256
if (_can_check_updates()) {
257
_check_update();
258
} else {
259
_set_status(UpdateStatus::OFFLINE);
260
}
261
} break;
262
263
case NOTIFICATION_THEME_CHANGED: {
264
theme_cache.default_color = get_theme_color(SceneStringName(font_color), "Button");
265
theme_cache.disabled_color = get_theme_color("font_disabled_color", "Button");
266
theme_cache.error_color = get_theme_color("error_color", EditorStringName(Editor));
267
theme_cache.update_color = get_theme_color("warning_color", EditorStringName(Editor));
268
} break;
269
270
case NOTIFICATION_READY: {
271
if (_can_check_updates()) {
272
_check_update();
273
} else {
274
_set_status(UpdateStatus::OFFLINE);
275
}
276
} break;
277
}
278
}
279
280
void EngineUpdateLabel::_bind_methods() {
281
ADD_SIGNAL(MethodInfo("offline_clicked"));
282
}
283
284
void EngineUpdateLabel::pressed() {
285
switch (status) {
286
case UpdateStatus::OFFLINE: {
287
emit_signal("offline_clicked");
288
} break;
289
290
case UpdateStatus::ERROR: {
291
_check_update();
292
} break;
293
294
case UpdateStatus::UPDATE_AVAILABLE: {
295
OS::get_singleton()->shell_open("https://godotengine.org/download/archive/" + available_newer_version);
296
} break;
297
298
default: {
299
}
300
}
301
}
302
303
EngineUpdateLabel::EngineUpdateLabel() {
304
set_underline_mode(UNDERLINE_MODE_ON_HOVER);
305
306
http = memnew(HTTPRequest);
307
http->set_https_proxy(EDITOR_GET("network/http_proxy/host"), EDITOR_GET("network/http_proxy/port"));
308
http->set_timeout(10.0);
309
add_child(http);
310
http->connect("request_completed", callable_mp(this, &EngineUpdateLabel::_http_request_completed));
311
}
312
313