engine_update_label.cpp 11 KB

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