| 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 | available_newer_version = String(); |
| 89 | |
| 90 | for (const Variant &data_bit : version_array) { |
| 91 | const Dictionary version_info = data_bit; |
| 92 | |
| 93 | const String base_version_string = version_info.get("name", ""); |
| 94 | const PackedStringArray version_bits = base_version_string.split("."); |
| 95 | |
| 96 | if (version_bits.size() < 2) { |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | int minor = version_bits[1].to_int(); |
| 101 | if (version_bits[0].to_int() != current_major || minor < current_minor) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | int patch = 0; |
| 106 | if (version_bits.size() >= 3) { |
| 107 | patch = version_bits[2].to_int(); |
| 108 | } |
| 109 | |
| 110 | if (minor == current_minor && patch < current_patch) { |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | if (update_mode == UpdateMode::NEWEST_PATCH && minor > current_minor) { |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | const Array releases = version_info.get("releases", Array()); |
| 119 | if (releases.is_empty()) { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | const Dictionary newest_release = releases[0]; |
| 124 | const String release_string = newest_release.get("name", "unknown"); |
| 125 | |
| 126 | int release_index; |
| 127 | VersionType release_type = _get_version_type(release_string, &release_index); |
| 128 | |
| 129 | if (minor > current_minor || patch > current_patch) { |
| 130 | if (stable_only && release_type != VersionType::STABLE) { |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | available_newer_version = vformat("%s-%s", base_version_string, release_string); |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | int current_version_index; |
| 139 | VersionType current_version_type = _get_version_type(current_version_info.get("status", "unknown"), ¤t_version_index); |
| 140 | |
| 141 | if (int(release_type) > int(current_version_type)) { |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | if (int(release_type) == int(current_version_type) && release_index <= current_version_index) { |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | available_newer_version = vformat("%s-%s", base_version_string, release_string); |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | if (!available_newer_version.is_empty()) { |
| 154 | _set_status(UpdateStatus::UPDATE_AVAILABLE); |
| 155 | _set_message(vformat(TTR("Update available: %s."), available_newer_version), theme_cache.update_color); |
| 156 | } else if (available_newer_version.is_empty()) { |
| 157 | _set_status(UpdateStatus::UP_TO_DATE); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void EngineUpdateLabel::_set_message(const String &p_message, const Color &p_color) { |
| 162 | if (is_disabled()) { |
| 163 | add_theme_color_override("font_disabled_color", p_color); |
| 164 | } else { |
| 165 | add_theme_color_override(SceneStringName(font_color), p_color); |
| 166 | } |
| 167 | set_text(p_message); |
| 168 | } |
| 169 | |
| 170 | void EngineUpdateLabel::_set_status(UpdateStatus p_status) { |
| 171 | status = p_status; |
| 172 | if (status == UpdateStatus::BUSY || status == UpdateStatus::UP_TO_DATE) { |
| 173 | // Hide the label to prevent unnecessary distraction. |
| 174 | hide(); |
| 175 | return; |
| 176 | } else { |
| 177 | show(); |
| 178 | } |
| 179 | |
| 180 | switch (status) { |
| 181 | case UpdateStatus::OFFLINE: { |
| 182 | set_disabled(false); |
| 183 | if (int(EDITOR_GET("network/connection/network_mode")) == EditorSettings::NETWORK_OFFLINE) { |
| 184 | _set_message(TTR("Offline mode, update checks disabled."), theme_cache.disabled_color); |
| 185 | } else { |
| 186 | _set_message(TTR("Update checks disabled."), theme_cache.disabled_color); |
| 187 | } |
| 188 | set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_OFF); |
| 189 | set_tooltip_text(""); |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | case UpdateStatus::ERROR: { |
| 194 | set_disabled(false); |
| 195 | set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE); |
| 196 | set_tooltip_text(TTR("An error has occurred. Click to try again.")); |
| 197 | } break; |
| 198 | |
| 199 | case UpdateStatus::UPDATE_AVAILABLE: { |
| 200 | set_disabled(false); |
| 201 | set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE); |
| 202 | set_tooltip_text(TTR("Click to open download page.")); |
| 203 | } break; |
| 204 | |
| 205 | default: { |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | EngineUpdateLabel::VersionType EngineUpdateLabel::_get_version_type(const String &p_string, int *r_index) const { |
| 211 | VersionType type = VersionType::UNKNOWN; |
| 212 | String index_string; |
| 213 | |
| 214 | static HashMap<String, VersionType> type_map; |
| 215 | if (type_map.is_empty()) { |
| 216 | type_map["stable"] = VersionType::STABLE; |
| 217 | type_map["rc"] = VersionType::RC; |
| 218 | type_map["beta"] = VersionType::BETA; |
| 219 | type_map["alpha"] = VersionType::ALPHA; |
| 220 | type_map["dev"] = VersionType::DEV; |
| 221 | } |
| 222 | |
| 223 | for (const KeyValue<String, VersionType> &kv : type_map) { |
| 224 | if (p_string.begins_with(kv.key)) { |
| 225 | index_string = p_string.trim_prefix(kv.key); |
| 226 | type = kv.value; |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (r_index) { |
| 232 | if (index_string.is_empty()) { |
| 233 | *r_index = DEV_VERSION; |
| 234 | } else { |
| 235 | *r_index = index_string.to_int(); |
| 236 | } |
| 237 | } |
| 238 | return type; |
| 239 | } |
| 240 | |
| 241 | String EngineUpdateLabel::_extract_sub_string(const String &p_line) const { |
| 242 | int j = p_line.find_char('"') + 1; |
| 243 | return p_line.substr(j, p_line.find_char('"', j) - j); |
| 244 | } |
| 245 | |
| 246 | void EngineUpdateLabel::_notification(int p_what) { |
| 247 | switch (p_what) { |
| 248 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 249 | if (!EditorSettings::get_singleton()->check_changed_settings_in_group("network/connection")) { |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | if (_can_check_updates()) { |
| 254 | _check_update(); |
| 255 | } else { |
| 256 | _set_status(UpdateStatus::OFFLINE); |
| 257 | } |
| 258 | } break; |
| 259 | |
| 260 | case NOTIFICATION_THEME_CHANGED: { |
| 261 | theme_cache.default_color = get_theme_color(SceneStringName(font_color), "Button"); |
| 262 | theme_cache.disabled_color = get_theme_color("font_disabled_color", "Button"); |
| 263 | theme_cache.error_color = get_theme_color("error_color", EditorStringName(Editor)); |
| 264 | theme_cache.update_color = get_theme_color("warning_color", EditorStringName(Editor)); |
| 265 | } break; |
| 266 | |
| 267 | case NOTIFICATION_READY: { |
| 268 | if (_can_check_updates()) { |
| 269 | _check_update(); |
| 270 | } else { |
| 271 | _set_status(UpdateStatus::OFFLINE); |
| 272 | } |
| 273 | } break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void EngineUpdateLabel::_bind_methods() { |
| 278 | ADD_SIGNAL(MethodInfo("offline_clicked")); |
| 279 | } |
| 280 | |
| 281 | void EngineUpdateLabel::pressed() { |
| 282 | switch (status) { |
| 283 | case UpdateStatus::OFFLINE: { |
| 284 | emit_signal("offline_clicked"); |
| 285 | } break; |
| 286 | |
| 287 | case UpdateStatus::ERROR: { |
| 288 | _check_update(); |
| 289 | } break; |
| 290 | |
| 291 | case UpdateStatus::UPDATE_AVAILABLE: { |
| 292 | OS::get_singleton()->shell_open("https://godotengine.org/download/archive/" + available_newer_version); |
| 293 | } break; |
| 294 | |
| 295 | default: { |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | EngineUpdateLabel::EngineUpdateLabel() { |
| 301 | set_underline_mode(UNDERLINE_MODE_ON_HOVER); |
| 302 | |
| 303 | http = memnew(HTTPRequest); |
| 304 | http->set_https_proxy(EDITOR_GET("network/http_proxy/host"), EDITOR_GET("network/http_proxy/port")); |
| 305 | http->set_timeout(10.0); |
| 306 | add_child(http); |
| 307 | http->connect("request_completed", callable_mp(this, &EngineUpdateLabel::_http_request_completed)); |
| 308 | } |