engine_update_label.cpp 10 KB

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