Path: blob/master/editor/project_manager/engine_update_label.cpp
20829 views
/**************************************************************************/1/* engine_update_label.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "engine_update_label.h"3132#include "core/io/json.h"33#include "core/version.h"34#include "editor/editor_string_names.h"35#include "editor/settings/editor_settings.h"36#include "scene/main/http_request.h"3738bool EngineUpdateLabel::_can_check_updates() const {39return int(EDITOR_GET("network/connection/network_mode")) == EditorSettings::NETWORK_ONLINE &&40UpdateMode(int(EDITOR_GET("network/connection/check_for_updates"))) != UpdateMode::DISABLED;41}4243void EngineUpdateLabel::_check_update() {44checked_update = true;45_set_status(UpdateStatus::BUSY);46http->request("https://godotengine.org/versions.json");47}4849void EngineUpdateLabel::_http_request_completed(int p_result, int p_response_code, const PackedStringArray &p_headers, const PackedByteArray &p_body) {50if (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);53return;54}5556if (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);59return;60}6162Array version_array;63{64const uint8_t *r = p_body.ptr();65String s = String::utf8((const char *)r, p_body.size());6667Variant result = JSON::parse_string(s);68if (result == Variant()) {69_set_status(UpdateStatus::ERROR);70_set_message(TTR("Failed to parse version JSON."), theme_cache.error_color);71return;72}73if (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);76return;77}78version_array = result;79}8081UpdateMode update_mode = UpdateMode(int(EDITOR_GET("network/connection/check_for_updates")));82if (update_mode == UpdateMode::AUTO) {83if (_get_version_type(GODOT_VERSION_STATUS) == VersionType::STABLE) {84update_mode = UpdateMode::NEWEST_STABLE;85} else {86update_mode = UpdateMode::NEWEST_UNSTABLE;87}88}89bool stable_only = update_mode == UpdateMode::NEWEST_STABLE || update_mode == UpdateMode::NEWEST_PATCH;9091available_newer_version = String();92for (const Variant &data_bit : version_array) {93const Dictionary version_info = data_bit;9495const String base_version_string = version_info.get("name", "");96const PackedStringArray version_bits = base_version_string.split(".");9798if (version_bits.size() < 2) {99continue;100}101102int minor = version_bits[1].to_int();103if (version_bits[0].to_int() != GODOT_VERSION_MAJOR || minor < GODOT_VERSION_MINOR) {104continue;105}106107int patch = 0;108if (version_bits.size() >= 3) {109patch = version_bits[2].to_int();110}111112if (minor == GODOT_VERSION_MINOR && patch < GODOT_VERSION_PATCH) {113continue;114}115116if (update_mode == UpdateMode::NEWEST_PATCH && minor > GODOT_VERSION_MINOR) {117continue;118}119120const Array releases = version_info.get("releases", Array());121if (releases.is_empty()) {122continue;123}124125const Dictionary newest_release = releases[0];126const String release_string = newest_release.get("name", "unknown");127128int release_index;129VersionType release_type = _get_version_type(release_string, &release_index);130131if (minor > GODOT_VERSION_MINOR || patch > GODOT_VERSION_PATCH) {132if (stable_only && release_type != VersionType::STABLE) {133continue;134}135136available_newer_version = vformat("%s-%s", base_version_string, release_string);137break;138}139140int current_version_index;141VersionType current_version_type = _get_version_type(GODOT_VERSION_STATUS, ¤t_version_index);142143if (int(release_type) > int(current_version_type)) {144break;145}146147if (int(release_type) == int(current_version_type) && release_index <= current_version_index) {148break;149}150151available_newer_version = vformat("%s-%s", base_version_string, release_string);152break;153}154155if (!available_newer_version.is_empty()) {156_set_status(UpdateStatus::UPDATE_AVAILABLE);157_set_message(vformat(TTR("Update available: %s."), available_newer_version), theme_cache.update_color);158} else if (available_newer_version.is_empty()) {159_set_status(UpdateStatus::UP_TO_DATE);160}161}162163void EngineUpdateLabel::_set_message(const String &p_message, const Color &p_color) {164if (is_disabled()) {165add_theme_color_override("font_disabled_color", p_color);166} else {167add_theme_color_override(SceneStringName(font_color), p_color);168}169set_text(p_message);170}171172void EngineUpdateLabel::_set_status(UpdateStatus p_status) {173status = p_status;174if (status == UpdateStatus::BUSY || status == UpdateStatus::UP_TO_DATE) {175// Hide the label to prevent unnecessary distraction.176hide();177return;178} else {179show();180}181182switch (status) {183case UpdateStatus::OFFLINE: {184set_disabled(false);185if (int(EDITOR_GET("network/connection/network_mode")) == EditorSettings::NETWORK_OFFLINE) {186_set_message(TTR("Offline mode, update checks disabled."), theme_cache.disabled_color);187} else {188_set_message(TTR("Update checks disabled."), theme_cache.disabled_color);189}190set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_OFF);191set_tooltip_text("");192break;193}194195case UpdateStatus::ERROR: {196set_disabled(false);197set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE);198set_tooltip_text(TTR("An error has occurred. Click to try again."));199} break;200201case UpdateStatus::UPDATE_AVAILABLE: {202set_disabled(false);203set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE);204set_tooltip_text(TTR("Click to open download page."));205} break;206207default: {208}209}210}211212EngineUpdateLabel::VersionType EngineUpdateLabel::_get_version_type(const String &p_string, int *r_index) const {213VersionType type = VersionType::UNKNOWN;214String index_string;215216static HashMap<String, VersionType> type_map;217if (type_map.is_empty()) {218type_map["stable"] = VersionType::STABLE;219type_map["rc"] = VersionType::RC;220type_map["beta"] = VersionType::BETA;221type_map["alpha"] = VersionType::ALPHA;222type_map["dev"] = VersionType::DEV;223}224225for (const KeyValue<String, VersionType> &kv : type_map) {226if (p_string.begins_with(kv.key)) {227index_string = p_string.trim_prefix(kv.key);228type = kv.value;229break;230}231}232233if (r_index) {234if (index_string.is_empty()) {235*r_index = DEV_VERSION;236} else {237*r_index = index_string.to_int();238}239}240return type;241}242243String EngineUpdateLabel::_extract_sub_string(const String &p_line) const {244int j = p_line.find_char('"') + 1;245return p_line.substr(j, p_line.find_char('"', j) - j);246}247248void EngineUpdateLabel::_notification(int p_what) {249switch (p_what) {250case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {251if (!EditorSettings::get_singleton()->check_changed_settings_in_group("network/connection")) {252break;253}254255if (_can_check_updates()) {256_check_update();257} else {258_set_status(UpdateStatus::OFFLINE);259}260} break;261262case NOTIFICATION_THEME_CHANGED: {263theme_cache.default_color = get_theme_color(SceneStringName(font_color), "Button");264theme_cache.disabled_color = get_theme_color("font_disabled_color", "Button");265theme_cache.error_color = get_theme_color("error_color", EditorStringName(Editor));266theme_cache.update_color = get_theme_color("warning_color", EditorStringName(Editor));267} break;268269case NOTIFICATION_READY: {270if (_can_check_updates()) {271_check_update();272} else {273_set_status(UpdateStatus::OFFLINE);274}275} break;276}277}278279void EngineUpdateLabel::_bind_methods() {280ADD_SIGNAL(MethodInfo("offline_clicked"));281}282283void EngineUpdateLabel::pressed() {284switch (status) {285case UpdateStatus::OFFLINE: {286emit_signal("offline_clicked");287} break;288289case UpdateStatus::ERROR: {290_check_update();291} break;292293case UpdateStatus::UPDATE_AVAILABLE: {294OS::get_singleton()->shell_open("https://godotengine.org/download/archive/" + available_newer_version);295} break;296297default: {298}299}300}301302EngineUpdateLabel::EngineUpdateLabel() {303set_underline_mode(UNDERLINE_MODE_ON_HOVER);304305http = memnew(HTTPRequest);306http->set_https_proxy(EDITOR_GET("network/http_proxy/host"), EDITOR_GET("network/http_proxy/port"));307http->set_timeout(10.0);308add_child(http);309http->connect("request_completed", callable_mp(this, &EngineUpdateLabel::_http_request_completed));310}311312313