Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/project_manager/engine_update_label.cpp
9898 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 "editor/editor_string_names.h"
35
#include "editor/settings/editor_settings.h"
36
#include "scene/main/http_request.h"
37
38
bool EngineUpdateLabel::_can_check_updates() const {
39
return int(EDITOR_GET("network/connection/network_mode")) == EditorSettings::NETWORK_ONLINE &&
40
UpdateMode(int(EDITOR_GET("network/connection/check_for_updates"))) != UpdateMode::DISABLED;
41
}
42
43
void EngineUpdateLabel::_check_update() {
44
checked_update = true;
45
_set_status(UpdateStatus::BUSY);
46
http->request("https://godotengine.org/versions.json");
47
}
48
49
void EngineUpdateLabel::_http_request_completed(int p_result, int p_response_code, const PackedStringArray &p_headers, const PackedByteArray &p_body) {
50
if (p_result != OK) {
51
_set_status(UpdateStatus::ERROR);
52
_set_message(vformat(TTR("Failed to check for updates. Error: %d."), p_result), theme_cache.error_color);
53
return;
54
}
55
56
if (p_response_code != 200) {
57
_set_status(UpdateStatus::ERROR);
58
_set_message(vformat(TTR("Failed to check for updates. Response code: %d."), p_response_code), theme_cache.error_color);
59
return;
60
}
61
62
Array version_array;
63
{
64
const uint8_t *r = p_body.ptr();
65
String s = String::utf8((const char *)r, p_body.size());
66
67
Variant result = JSON::parse_string(s);
68
if (result == Variant()) {
69
_set_status(UpdateStatus::ERROR);
70
_set_message(TTR("Failed to parse version JSON."), theme_cache.error_color);
71
return;
72
}
73
if (result.get_type() != Variant::ARRAY) {
74
_set_status(UpdateStatus::ERROR);
75
_set_message(TTR("Received JSON data is not a valid version array."), theme_cache.error_color);
76
return;
77
}
78
version_array = result;
79
}
80
81
UpdateMode update_mode = UpdateMode(int(EDITOR_GET("network/connection/check_for_updates")));
82
bool stable_only = update_mode == UpdateMode::NEWEST_STABLE || update_mode == UpdateMode::NEWEST_PATCH;
83
84
const Dictionary current_version_info = Engine::get_singleton()->get_version_info();
85
int current_major = current_version_info.get("major", 0);
86
int current_minor = current_version_info.get("minor", 0);
87
int current_patch = current_version_info.get("patch", 0);
88
89
for (const Variant &data_bit : version_array) {
90
const Dictionary version_info = data_bit;
91
92
const String base_version_string = version_info.get("name", "");
93
const PackedStringArray version_bits = base_version_string.split(".");
94
95
if (version_bits.size() < 2) {
96
continue;
97
}
98
99
int minor = version_bits[1].to_int();
100
if (version_bits[0].to_int() != current_major || minor < current_minor) {
101
continue;
102
}
103
104
int patch = 0;
105
if (version_bits.size() >= 3) {
106
patch = version_bits[2].to_int();
107
}
108
109
if (minor == current_minor && patch < current_patch) {
110
continue;
111
}
112
113
if (update_mode == UpdateMode::NEWEST_PATCH && minor > current_minor) {
114
continue;
115
}
116
117
const Array releases = version_info.get("releases", Array());
118
if (releases.is_empty()) {
119
continue;
120
}
121
122
const Dictionary newest_release = releases[0];
123
const String release_string = newest_release.get("name", "unknown");
124
125
int release_index;
126
VersionType release_type = _get_version_type(release_string, &release_index);
127
128
if (minor > current_minor || patch > current_patch) {
129
if (stable_only && release_type != VersionType::STABLE) {
130
continue;
131
}
132
133
available_newer_version = vformat("%s-%s", base_version_string, release_string);
134
break;
135
}
136
137
int current_version_index;
138
VersionType current_version_type = _get_version_type(current_version_info.get("status", "unknown"), &current_version_index);
139
140
if (int(release_type) > int(current_version_type)) {
141
break;
142
}
143
144
if (int(release_type) == int(current_version_type) && release_index <= current_version_index) {
145
break;
146
}
147
148
available_newer_version = vformat("%s-%s", base_version_string, release_string);
149
break;
150
}
151
152
if (!available_newer_version.is_empty()) {
153
_set_status(UpdateStatus::UPDATE_AVAILABLE);
154
_set_message(vformat(TTR("Update available: %s."), available_newer_version), theme_cache.update_color);
155
} else if (available_newer_version.is_empty()) {
156
_set_status(UpdateStatus::UP_TO_DATE);
157
}
158
}
159
160
void EngineUpdateLabel::_set_message(const String &p_message, const Color &p_color) {
161
if (is_disabled()) {
162
add_theme_color_override("font_disabled_color", p_color);
163
} else {
164
add_theme_color_override(SceneStringName(font_color), p_color);
165
}
166
set_text(p_message);
167
}
168
169
void EngineUpdateLabel::_set_status(UpdateStatus p_status) {
170
status = p_status;
171
if (status == UpdateStatus::BUSY || status == UpdateStatus::UP_TO_DATE) {
172
// Hide the label to prevent unnecessary distraction.
173
hide();
174
return;
175
} else {
176
show();
177
}
178
179
switch (status) {
180
case UpdateStatus::OFFLINE: {
181
set_disabled(false);
182
if (int(EDITOR_GET("network/connection/network_mode")) == EditorSettings::NETWORK_OFFLINE) {
183
_set_message(TTR("Offline mode, update checks disabled."), theme_cache.disabled_color);
184
} else {
185
_set_message(TTR("Update checks disabled."), theme_cache.disabled_color);
186
}
187
set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_OFF);
188
set_tooltip_text("");
189
break;
190
}
191
192
case UpdateStatus::ERROR: {
193
set_disabled(false);
194
set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE);
195
set_tooltip_text(TTR("An error has occurred. Click to try again."));
196
} break;
197
198
case UpdateStatus::UPDATE_AVAILABLE: {
199
set_disabled(false);
200
set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE);
201
set_tooltip_text(TTR("Click to open download page."));
202
} break;
203
204
default: {
205
}
206
}
207
}
208
209
EngineUpdateLabel::VersionType EngineUpdateLabel::_get_version_type(const String &p_string, int *r_index) const {
210
VersionType type = VersionType::UNKNOWN;
211
String index_string;
212
213
static HashMap<String, VersionType> type_map;
214
if (type_map.is_empty()) {
215
type_map["stable"] = VersionType::STABLE;
216
type_map["rc"] = VersionType::RC;
217
type_map["beta"] = VersionType::BETA;
218
type_map["alpha"] = VersionType::ALPHA;
219
type_map["dev"] = VersionType::DEV;
220
}
221
222
for (const KeyValue<String, VersionType> &kv : type_map) {
223
if (p_string.begins_with(kv.key)) {
224
index_string = p_string.trim_prefix(kv.key);
225
type = kv.value;
226
break;
227
}
228
}
229
230
if (r_index) {
231
if (index_string.is_empty()) {
232
*r_index = DEV_VERSION;
233
} else {
234
*r_index = index_string.to_int();
235
}
236
}
237
return type;
238
}
239
240
String EngineUpdateLabel::_extract_sub_string(const String &p_line) const {
241
int j = p_line.find_char('"') + 1;
242
return p_line.substr(j, p_line.find_char('"', j) - j);
243
}
244
245
void EngineUpdateLabel::_notification(int p_what) {
246
switch (p_what) {
247
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
248
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("network/connection")) {
249
break;
250
}
251
252
if (_can_check_updates()) {
253
_check_update();
254
} else {
255
_set_status(UpdateStatus::OFFLINE);
256
}
257
} break;
258
259
case NOTIFICATION_THEME_CHANGED: {
260
theme_cache.default_color = get_theme_color(SceneStringName(font_color), "Button");
261
theme_cache.disabled_color = get_theme_color("font_disabled_color", "Button");
262
theme_cache.error_color = get_theme_color("error_color", EditorStringName(Editor));
263
theme_cache.update_color = get_theme_color("warning_color", EditorStringName(Editor));
264
} break;
265
266
case NOTIFICATION_READY: {
267
if (_can_check_updates()) {
268
_check_update();
269
} else {
270
_set_status(UpdateStatus::OFFLINE);
271
}
272
} break;
273
}
274
}
275
276
void EngineUpdateLabel::_bind_methods() {
277
ADD_SIGNAL(MethodInfo("offline_clicked"));
278
}
279
280
void EngineUpdateLabel::pressed() {
281
switch (status) {
282
case UpdateStatus::OFFLINE: {
283
emit_signal("offline_clicked");
284
} break;
285
286
case UpdateStatus::ERROR: {
287
_check_update();
288
} break;
289
290
case UpdateStatus::UPDATE_AVAILABLE: {
291
OS::get_singleton()->shell_open("https://godotengine.org/download/archive/" + available_newer_version);
292
} break;
293
294
default: {
295
}
296
}
297
}
298
299
EngineUpdateLabel::EngineUpdateLabel() {
300
set_underline_mode(UNDERLINE_MODE_ON_HOVER);
301
302
http = memnew(HTTPRequest);
303
http->set_https_proxy(EDITOR_GET("network/http_proxy/host"), EDITOR_GET("network/http_proxy/port"));
304
http->set_timeout(10.0);
305
add_child(http);
306
http->connect("request_completed", callable_mp(this, &EngineUpdateLabel::_http_request_completed));
307
}
308
309