engine_update_label.cpp 10 KB

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