engine_update_label.cpp 10 KB

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