Path: blob/master/editor/project_manager/engine_update_label.cpp
9898 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 "editor/editor_string_names.h"34#include "editor/settings/editor_settings.h"35#include "scene/main/http_request.h"3637bool EngineUpdateLabel::_can_check_updates() const {38return int(EDITOR_GET("network/connection/network_mode")) == EditorSettings::NETWORK_ONLINE &&39UpdateMode(int(EDITOR_GET("network/connection/check_for_updates"))) != UpdateMode::DISABLED;40}4142void EngineUpdateLabel::_check_update() {43checked_update = true;44_set_status(UpdateStatus::BUSY);45http->request("https://godotengine.org/versions.json");46}4748void EngineUpdateLabel::_http_request_completed(int p_result, int p_response_code, const PackedStringArray &p_headers, const PackedByteArray &p_body) {49if (p_result != OK) {50_set_status(UpdateStatus::ERROR);51_set_message(vformat(TTR("Failed to check for updates. Error: %d."), p_result), theme_cache.error_color);52return;53}5455if (p_response_code != 200) {56_set_status(UpdateStatus::ERROR);57_set_message(vformat(TTR("Failed to check for updates. Response code: %d."), p_response_code), theme_cache.error_color);58return;59}6061Array version_array;62{63const uint8_t *r = p_body.ptr();64String s = String::utf8((const char *)r, p_body.size());6566Variant result = JSON::parse_string(s);67if (result == Variant()) {68_set_status(UpdateStatus::ERROR);69_set_message(TTR("Failed to parse version JSON."), theme_cache.error_color);70return;71}72if (result.get_type() != Variant::ARRAY) {73_set_status(UpdateStatus::ERROR);74_set_message(TTR("Received JSON data is not a valid version array."), theme_cache.error_color);75return;76}77version_array = result;78}7980UpdateMode update_mode = UpdateMode(int(EDITOR_GET("network/connection/check_for_updates")));81bool stable_only = update_mode == UpdateMode::NEWEST_STABLE || update_mode == UpdateMode::NEWEST_PATCH;8283const Dictionary current_version_info = Engine::get_singleton()->get_version_info();84int current_major = current_version_info.get("major", 0);85int current_minor = current_version_info.get("minor", 0);86int current_patch = current_version_info.get("patch", 0);8788for (const Variant &data_bit : version_array) {89const Dictionary version_info = data_bit;9091const String base_version_string = version_info.get("name", "");92const PackedStringArray version_bits = base_version_string.split(".");9394if (version_bits.size() < 2) {95continue;96}9798int minor = version_bits[1].to_int();99if (version_bits[0].to_int() != current_major || minor < current_minor) {100continue;101}102103int patch = 0;104if (version_bits.size() >= 3) {105patch = version_bits[2].to_int();106}107108if (minor == current_minor && patch < current_patch) {109continue;110}111112if (update_mode == UpdateMode::NEWEST_PATCH && minor > current_minor) {113continue;114}115116const Array releases = version_info.get("releases", Array());117if (releases.is_empty()) {118continue;119}120121const Dictionary newest_release = releases[0];122const String release_string = newest_release.get("name", "unknown");123124int release_index;125VersionType release_type = _get_version_type(release_string, &release_index);126127if (minor > current_minor || patch > current_patch) {128if (stable_only && release_type != VersionType::STABLE) {129continue;130}131132available_newer_version = vformat("%s-%s", base_version_string, release_string);133break;134}135136int current_version_index;137VersionType current_version_type = _get_version_type(current_version_info.get("status", "unknown"), ¤t_version_index);138139if (int(release_type) > int(current_version_type)) {140break;141}142143if (int(release_type) == int(current_version_type) && release_index <= current_version_index) {144break;145}146147available_newer_version = vformat("%s-%s", base_version_string, release_string);148break;149}150151if (!available_newer_version.is_empty()) {152_set_status(UpdateStatus::UPDATE_AVAILABLE);153_set_message(vformat(TTR("Update available: %s."), available_newer_version), theme_cache.update_color);154} else if (available_newer_version.is_empty()) {155_set_status(UpdateStatus::UP_TO_DATE);156}157}158159void EngineUpdateLabel::_set_message(const String &p_message, const Color &p_color) {160if (is_disabled()) {161add_theme_color_override("font_disabled_color", p_color);162} else {163add_theme_color_override(SceneStringName(font_color), p_color);164}165set_text(p_message);166}167168void EngineUpdateLabel::_set_status(UpdateStatus p_status) {169status = p_status;170if (status == UpdateStatus::BUSY || status == UpdateStatus::UP_TO_DATE) {171// Hide the label to prevent unnecessary distraction.172hide();173return;174} else {175show();176}177178switch (status) {179case UpdateStatus::OFFLINE: {180set_disabled(false);181if (int(EDITOR_GET("network/connection/network_mode")) == EditorSettings::NETWORK_OFFLINE) {182_set_message(TTR("Offline mode, update checks disabled."), theme_cache.disabled_color);183} else {184_set_message(TTR("Update checks disabled."), theme_cache.disabled_color);185}186set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_OFF);187set_tooltip_text("");188break;189}190191case UpdateStatus::ERROR: {192set_disabled(false);193set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE);194set_tooltip_text(TTR("An error has occurred. Click to try again."));195} break;196197case UpdateStatus::UPDATE_AVAILABLE: {198set_disabled(false);199set_accessibility_live(DisplayServer::AccessibilityLiveMode::LIVE_POLITE);200set_tooltip_text(TTR("Click to open download page."));201} break;202203default: {204}205}206}207208EngineUpdateLabel::VersionType EngineUpdateLabel::_get_version_type(const String &p_string, int *r_index) const {209VersionType type = VersionType::UNKNOWN;210String index_string;211212static HashMap<String, VersionType> type_map;213if (type_map.is_empty()) {214type_map["stable"] = VersionType::STABLE;215type_map["rc"] = VersionType::RC;216type_map["beta"] = VersionType::BETA;217type_map["alpha"] = VersionType::ALPHA;218type_map["dev"] = VersionType::DEV;219}220221for (const KeyValue<String, VersionType> &kv : type_map) {222if (p_string.begins_with(kv.key)) {223index_string = p_string.trim_prefix(kv.key);224type = kv.value;225break;226}227}228229if (r_index) {230if (index_string.is_empty()) {231*r_index = DEV_VERSION;232} else {233*r_index = index_string.to_int();234}235}236return type;237}238239String EngineUpdateLabel::_extract_sub_string(const String &p_line) const {240int j = p_line.find_char('"') + 1;241return p_line.substr(j, p_line.find_char('"', j) - j);242}243244void EngineUpdateLabel::_notification(int p_what) {245switch (p_what) {246case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {247if (!EditorSettings::get_singleton()->check_changed_settings_in_group("network/connection")) {248break;249}250251if (_can_check_updates()) {252_check_update();253} else {254_set_status(UpdateStatus::OFFLINE);255}256} break;257258case NOTIFICATION_THEME_CHANGED: {259theme_cache.default_color = get_theme_color(SceneStringName(font_color), "Button");260theme_cache.disabled_color = get_theme_color("font_disabled_color", "Button");261theme_cache.error_color = get_theme_color("error_color", EditorStringName(Editor));262theme_cache.update_color = get_theme_color("warning_color", EditorStringName(Editor));263} break;264265case NOTIFICATION_READY: {266if (_can_check_updates()) {267_check_update();268} else {269_set_status(UpdateStatus::OFFLINE);270}271} break;272}273}274275void EngineUpdateLabel::_bind_methods() {276ADD_SIGNAL(MethodInfo("offline_clicked"));277}278279void EngineUpdateLabel::pressed() {280switch (status) {281case UpdateStatus::OFFLINE: {282emit_signal("offline_clicked");283} break;284285case UpdateStatus::ERROR: {286_check_update();287} break;288289case UpdateStatus::UPDATE_AVAILABLE: {290OS::get_singleton()->shell_open("https://godotengine.org/download/archive/" + available_newer_version);291} break;292293default: {294}295}296}297298EngineUpdateLabel::EngineUpdateLabel() {299set_underline_mode(UNDERLINE_MODE_ON_HOVER);300301http = memnew(HTTPRequest);302http->set_https_proxy(EDITOR_GET("network/http_proxy/host"), EDITOR_GET("network/http_proxy/port"));303http->set_timeout(10.0);304add_child(http);305http->connect("request_completed", callable_mp(this, &EngineUpdateLabel::_http_request_completed));306}307308309