asset_library_editor_plugin.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. /**************************************************************************/
  2. /* asset_library_editor_plugin.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 "asset_library_editor_plugin.h"
  31. #include "core/input/input.h"
  32. #include "core/io/json.h"
  33. #include "core/io/stream_peer_tls.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/version.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_paths.h"
  38. #include "editor/editor_scale.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/editor_string_names.h"
  41. #include "editor/gui/editor_file_dialog.h"
  42. #include "editor/project_settings_editor.h"
  43. #include "scene/gui/menu_button.h"
  44. #include "scene/resources/image_texture.h"
  45. static inline void setup_http_request(HTTPRequest *request) {
  46. request->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
  47. const String proxy_host = EDITOR_GET("network/http_proxy/host");
  48. const int proxy_port = EDITOR_GET("network/http_proxy/port");
  49. request->set_http_proxy(proxy_host, proxy_port);
  50. request->set_https_proxy(proxy_host, proxy_port);
  51. }
  52. void EditorAssetLibraryItem::configure(const String &p_title, int p_asset_id, const String &p_category, int p_category_id, const String &p_author, int p_author_id, const String &p_cost) {
  53. title->set_text(p_title);
  54. asset_id = p_asset_id;
  55. category->set_text(p_category);
  56. category_id = p_category_id;
  57. author->set_text(p_author);
  58. author_id = p_author_id;
  59. price->set_text(p_cost);
  60. }
  61. void EditorAssetLibraryItem::set_image(int p_type, int p_index, const Ref<Texture2D> &p_image) {
  62. ERR_FAIL_COND(p_type != EditorAssetLibrary::IMAGE_QUEUE_ICON);
  63. ERR_FAIL_COND(p_index != 0);
  64. icon->set_texture_normal(p_image);
  65. }
  66. void EditorAssetLibraryItem::_notification(int p_what) {
  67. switch (p_what) {
  68. case NOTIFICATION_ENTER_TREE: {
  69. icon->set_texture_normal(get_editor_theme_icon(SNAME("ProjectIconLoading")));
  70. category->add_theme_color_override("font_color", Color(0.5, 0.5, 0.5));
  71. author->add_theme_color_override("font_color", Color(0.5, 0.5, 0.5));
  72. price->add_theme_color_override("font_color", Color(0.5, 0.5, 0.5));
  73. } break;
  74. }
  75. }
  76. void EditorAssetLibraryItem::_asset_clicked() {
  77. emit_signal(SNAME("asset_selected"), asset_id);
  78. }
  79. void EditorAssetLibraryItem::_category_clicked() {
  80. emit_signal(SNAME("category_selected"), category_id);
  81. }
  82. void EditorAssetLibraryItem::_author_clicked() {
  83. emit_signal(SNAME("author_selected"), author_id);
  84. }
  85. void EditorAssetLibraryItem::_bind_methods() {
  86. ClassDB::bind_method("set_image", &EditorAssetLibraryItem::set_image);
  87. ADD_SIGNAL(MethodInfo("asset_selected"));
  88. ADD_SIGNAL(MethodInfo("category_selected"));
  89. ADD_SIGNAL(MethodInfo("author_selected"));
  90. }
  91. EditorAssetLibraryItem::EditorAssetLibraryItem() {
  92. Ref<StyleBoxEmpty> border;
  93. border.instantiate();
  94. border->set_content_margin_all(5 * EDSCALE);
  95. add_theme_style_override("panel", border);
  96. HBoxContainer *hb = memnew(HBoxContainer);
  97. // Add some spacing to visually separate the icon from the asset details.
  98. hb->add_theme_constant_override("separation", 15 * EDSCALE);
  99. add_child(hb);
  100. icon = memnew(TextureButton);
  101. icon->set_custom_minimum_size(Size2(64, 64) * EDSCALE);
  102. icon->set_default_cursor_shape(CURSOR_POINTING_HAND);
  103. icon->connect("pressed", callable_mp(this, &EditorAssetLibraryItem::_asset_clicked));
  104. hb->add_child(icon);
  105. VBoxContainer *vb = memnew(VBoxContainer);
  106. hb->add_child(vb);
  107. vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  108. title = memnew(LinkButton);
  109. title->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  110. title->connect("pressed", callable_mp(this, &EditorAssetLibraryItem::_asset_clicked));
  111. vb->add_child(title);
  112. category = memnew(LinkButton);
  113. category->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  114. category->connect("pressed", callable_mp(this, &EditorAssetLibraryItem::_category_clicked));
  115. vb->add_child(category);
  116. author = memnew(LinkButton);
  117. author->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  118. author->connect("pressed", callable_mp(this, &EditorAssetLibraryItem::_author_clicked));
  119. vb->add_child(author);
  120. price = memnew(Label);
  121. vb->add_child(price);
  122. set_custom_minimum_size(Size2(250, 100) * EDSCALE);
  123. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  124. }
  125. //////////////////////////////////////////////////////////////////////////////
  126. void EditorAssetLibraryItemDescription::set_image(int p_type, int p_index, const Ref<Texture2D> &p_image) {
  127. switch (p_type) {
  128. case EditorAssetLibrary::IMAGE_QUEUE_ICON: {
  129. item->call("set_image", p_type, p_index, p_image);
  130. icon = p_image;
  131. } break;
  132. case EditorAssetLibrary::IMAGE_QUEUE_THUMBNAIL: {
  133. for (int i = 0; i < preview_images.size(); i++) {
  134. if (preview_images[i].id == p_index) {
  135. if (preview_images[i].is_video) {
  136. Ref<Image> overlay = previews->get_editor_theme_icon(SNAME("PlayOverlay"))->get_image();
  137. Ref<Image> thumbnail = p_image->get_image();
  138. thumbnail = thumbnail->duplicate();
  139. Point2i overlay_pos = Point2i((thumbnail->get_width() - overlay->get_width()) / 2, (thumbnail->get_height() - overlay->get_height()) / 2);
  140. // Overlay and thumbnail need the same format for `blend_rect` to work.
  141. thumbnail->convert(Image::FORMAT_RGBA8);
  142. thumbnail->blend_rect(overlay, overlay->get_used_rect(), overlay_pos);
  143. preview_images[i].button->set_icon(ImageTexture::create_from_image(thumbnail));
  144. // Make it clearer that clicking it will open an external link
  145. preview_images[i].button->set_default_cursor_shape(Control::CURSOR_POINTING_HAND);
  146. } else {
  147. preview_images[i].button->set_icon(p_image);
  148. }
  149. break;
  150. }
  151. }
  152. } break;
  153. case EditorAssetLibrary::IMAGE_QUEUE_SCREENSHOT: {
  154. for (int i = 0; i < preview_images.size(); i++) {
  155. if (preview_images[i].id == p_index) {
  156. preview_images.write[i].image = p_image;
  157. if (preview_images[i].button->is_pressed()) {
  158. _preview_click(p_index);
  159. }
  160. break;
  161. }
  162. }
  163. } break;
  164. }
  165. }
  166. void EditorAssetLibraryItemDescription::_notification(int p_what) {
  167. switch (p_what) {
  168. case NOTIFICATION_ENTER_TREE:
  169. case NOTIFICATION_THEME_CHANGED: {
  170. previews_bg->add_theme_style_override("panel", previews->get_theme_stylebox(SNAME("normal"), SNAME("TextEdit")));
  171. } break;
  172. }
  173. }
  174. void EditorAssetLibraryItemDescription::_bind_methods() {
  175. ClassDB::bind_method(D_METHOD("set_image"), &EditorAssetLibraryItemDescription::set_image);
  176. }
  177. void EditorAssetLibraryItemDescription::_link_click(const String &p_url) {
  178. ERR_FAIL_COND(!p_url.begins_with("http"));
  179. OS::get_singleton()->shell_open(p_url);
  180. }
  181. void EditorAssetLibraryItemDescription::_preview_click(int p_id) {
  182. for (int i = 0; i < preview_images.size(); i++) {
  183. if (preview_images[i].id == p_id) {
  184. preview_images[i].button->set_pressed(true);
  185. if (!preview_images[i].is_video) {
  186. if (preview_images[i].image.is_valid()) {
  187. preview->set_texture(preview_images[i].image);
  188. child_controls_changed();
  189. }
  190. } else {
  191. _link_click(preview_images[i].video_link);
  192. }
  193. } else {
  194. preview_images[i].button->set_pressed(false);
  195. }
  196. }
  197. }
  198. void EditorAssetLibraryItemDescription::configure(const String &p_title, int p_asset_id, const String &p_category, int p_category_id, const String &p_author, int p_author_id, const String &p_cost, int p_version, const String &p_version_string, const String &p_description, const String &p_download_url, const String &p_browse_url, const String &p_sha256_hash) {
  199. asset_id = p_asset_id;
  200. title = p_title;
  201. download_url = p_download_url;
  202. sha256 = p_sha256_hash;
  203. item->configure(p_title, p_asset_id, p_category, p_category_id, p_author, p_author_id, p_cost);
  204. description->clear();
  205. description->add_text(TTR("Version:") + " " + p_version_string + "\n");
  206. description->add_text(TTR("Contents:") + " ");
  207. description->push_meta(p_browse_url);
  208. description->add_text(TTR("View Files"));
  209. description->pop();
  210. description->add_text("\n" + TTR("Description:") + "\n\n");
  211. description->append_text(p_description);
  212. description->set_selection_enabled(true);
  213. description->set_context_menu_enabled(true);
  214. set_title(p_title);
  215. }
  216. void EditorAssetLibraryItemDescription::add_preview(int p_id, bool p_video, const String &p_url) {
  217. Preview new_preview;
  218. new_preview.id = p_id;
  219. new_preview.video_link = p_url;
  220. new_preview.is_video = p_video;
  221. new_preview.button = memnew(Button);
  222. new_preview.button->set_icon(previews->get_editor_theme_icon(SNAME("ThumbnailWait")));
  223. new_preview.button->set_toggle_mode(true);
  224. new_preview.button->connect("pressed", callable_mp(this, &EditorAssetLibraryItemDescription::_preview_click).bind(p_id));
  225. preview_hb->add_child(new_preview.button);
  226. if (!p_video) {
  227. new_preview.image = previews->get_editor_theme_icon(SNAME("ThumbnailWait"));
  228. }
  229. preview_images.push_back(new_preview);
  230. if (preview_images.size() == 1 && !p_video) {
  231. _preview_click(p_id);
  232. }
  233. }
  234. EditorAssetLibraryItemDescription::EditorAssetLibraryItemDescription() {
  235. HBoxContainer *hbox = memnew(HBoxContainer);
  236. add_child(hbox);
  237. VBoxContainer *desc_vbox = memnew(VBoxContainer);
  238. hbox->add_child(desc_vbox);
  239. hbox->add_theme_constant_override("separation", 15 * EDSCALE);
  240. item = memnew(EditorAssetLibraryItem);
  241. desc_vbox->add_child(item);
  242. desc_vbox->set_custom_minimum_size(Size2(440 * EDSCALE, 0));
  243. description = memnew(RichTextLabel);
  244. desc_vbox->add_child(description);
  245. description->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  246. description->connect("meta_clicked", callable_mp(this, &EditorAssetLibraryItemDescription::_link_click));
  247. description->add_theme_constant_override("line_separation", Math::round(5 * EDSCALE));
  248. VBoxContainer *previews_vbox = memnew(VBoxContainer);
  249. hbox->add_child(previews_vbox);
  250. previews_vbox->add_theme_constant_override("separation", 15 * EDSCALE);
  251. previews_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  252. previews_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  253. preview = memnew(TextureRect);
  254. previews_vbox->add_child(preview);
  255. preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  256. preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  257. preview->set_custom_minimum_size(Size2(640 * EDSCALE, 345 * EDSCALE));
  258. preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  259. preview->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  260. previews_bg = memnew(PanelContainer);
  261. previews_vbox->add_child(previews_bg);
  262. previews_bg->set_custom_minimum_size(Size2(640 * EDSCALE, 101 * EDSCALE));
  263. previews = memnew(ScrollContainer);
  264. previews_bg->add_child(previews);
  265. previews->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  266. preview_hb = memnew(HBoxContainer);
  267. preview_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  268. previews->add_child(preview_hb);
  269. set_ok_button_text(TTR("Download"));
  270. set_cancel_button_text(TTR("Close"));
  271. }
  272. ///////////////////////////////////////////////////////////////////////////////////
  273. void EditorAssetLibraryItemDownload::_http_download_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
  274. String error_text;
  275. switch (p_status) {
  276. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH:
  277. case HTTPRequest::RESULT_CONNECTION_ERROR:
  278. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED: {
  279. error_text = TTR("Connection error, please try again.");
  280. status->set_text(TTR("Can't connect."));
  281. } break;
  282. case HTTPRequest::RESULT_CANT_CONNECT:
  283. case HTTPRequest::RESULT_TLS_HANDSHAKE_ERROR: {
  284. error_text = TTR("Can't connect to host:") + " " + host;
  285. status->set_text(TTR("Can't connect."));
  286. } break;
  287. case HTTPRequest::RESULT_NO_RESPONSE: {
  288. error_text = TTR("No response from host:") + " " + host;
  289. status->set_text(TTR("No response."));
  290. } break;
  291. case HTTPRequest::RESULT_CANT_RESOLVE: {
  292. error_text = TTR("Can't resolve hostname:") + " " + host;
  293. status->set_text(TTR("Can't resolve."));
  294. } break;
  295. case HTTPRequest::RESULT_REQUEST_FAILED: {
  296. error_text = TTR("Request failed, return code:") + " " + itos(p_code);
  297. status->set_text(TTR("Request failed."));
  298. } break;
  299. case HTTPRequest::RESULT_DOWNLOAD_FILE_CANT_OPEN:
  300. case HTTPRequest::RESULT_DOWNLOAD_FILE_WRITE_ERROR: {
  301. error_text = TTR("Cannot save response to:") + " " + download->get_download_file();
  302. status->set_text(TTR("Write error."));
  303. } break;
  304. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  305. error_text = TTR("Request failed, too many redirects");
  306. status->set_text(TTR("Redirect loop."));
  307. } break;
  308. case HTTPRequest::RESULT_TIMEOUT: {
  309. error_text = TTR("Request failed, timeout");
  310. status->set_text(TTR("Timeout."));
  311. } break;
  312. default: {
  313. if (p_code != 200) {
  314. error_text = TTR("Request failed, return code:") + " " + itos(p_code);
  315. status->set_text(TTR("Failed:") + " " + itos(p_code));
  316. } else if (!sha256.is_empty()) {
  317. String download_sha256 = FileAccess::get_sha256(download->get_download_file());
  318. if (sha256 != download_sha256) {
  319. error_text = TTR("Bad download hash, assuming file has been tampered with.") + "\n";
  320. error_text += TTR("Expected:") + " " + sha256 + "\n" + TTR("Got:") + " " + download_sha256;
  321. status->set_text(TTR("Failed SHA-256 hash check"));
  322. }
  323. }
  324. } break;
  325. }
  326. if (!error_text.is_empty()) {
  327. download_error->set_text(TTR("Asset Download Error:") + "\n" + error_text);
  328. download_error->popup_centered();
  329. // Let the user retry the download.
  330. retry_button->show();
  331. return;
  332. }
  333. install_button->set_disabled(false);
  334. status->set_text(TTR("Ready to install!"));
  335. // Make the progress bar invisible but don't reflow other Controls around it.
  336. progress->set_modulate(Color(0, 0, 0, 0));
  337. set_process(false);
  338. // Automatically prompt for installation once the download is completed.
  339. install();
  340. }
  341. void EditorAssetLibraryItemDownload::configure(const String &p_title, int p_asset_id, const Ref<Texture2D> &p_preview, const String &p_download_url, const String &p_sha256_hash) {
  342. title->set_text(p_title);
  343. icon->set_texture(p_preview);
  344. asset_id = p_asset_id;
  345. if (!p_preview.is_valid()) {
  346. icon->set_texture(get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
  347. }
  348. host = p_download_url;
  349. sha256 = p_sha256_hash;
  350. _make_request();
  351. }
  352. void EditorAssetLibraryItemDownload::_notification(int p_what) {
  353. switch (p_what) {
  354. case NOTIFICATION_ENTER_TREE:
  355. case NOTIFICATION_THEME_CHANGED: {
  356. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("AssetLib")));
  357. status->add_theme_color_override("font_color", get_theme_color(SNAME("status_color"), SNAME("AssetLib")));
  358. dismiss_button->set_texture_normal(get_theme_icon(SNAME("dismiss"), SNAME("AssetLib")));
  359. } break;
  360. case NOTIFICATION_PROCESS: {
  361. // Make the progress bar visible again when retrying the download.
  362. progress->set_modulate(Color(1, 1, 1, 1));
  363. if (download->get_downloaded_bytes() > 0) {
  364. progress->set_max(download->get_body_size());
  365. progress->set_value(download->get_downloaded_bytes());
  366. }
  367. int cstatus = download->get_http_client_status();
  368. if (cstatus == HTTPClient::STATUS_BODY) {
  369. if (download->get_body_size() > 0) {
  370. status->set_text(vformat(
  371. TTR("Downloading (%s / %s)..."),
  372. String::humanize_size(download->get_downloaded_bytes()),
  373. String::humanize_size(download->get_body_size())));
  374. } else {
  375. // Total file size is unknown, so it cannot be displayed.
  376. progress->set_modulate(Color(0, 0, 0, 0));
  377. status->set_text(vformat(
  378. TTR("Downloading...") + " (%s)",
  379. String::humanize_size(download->get_downloaded_bytes())));
  380. }
  381. }
  382. if (cstatus != prev_status) {
  383. switch (cstatus) {
  384. case HTTPClient::STATUS_RESOLVING: {
  385. status->set_text(TTR("Resolving..."));
  386. progress->set_max(1);
  387. progress->set_value(0);
  388. } break;
  389. case HTTPClient::STATUS_CONNECTING: {
  390. status->set_text(TTR("Connecting..."));
  391. progress->set_max(1);
  392. progress->set_value(0);
  393. } break;
  394. case HTTPClient::STATUS_REQUESTING: {
  395. status->set_text(TTR("Requesting..."));
  396. progress->set_max(1);
  397. progress->set_value(0);
  398. } break;
  399. default: {
  400. }
  401. }
  402. prev_status = cstatus;
  403. }
  404. } break;
  405. }
  406. }
  407. void EditorAssetLibraryItemDownload::_close() {
  408. // Clean up downloaded file.
  409. DirAccess::remove_file_or_error(download->get_download_file());
  410. queue_free();
  411. }
  412. bool EditorAssetLibraryItemDownload::can_install() const {
  413. return !install_button->is_disabled();
  414. }
  415. void EditorAssetLibraryItemDownload::install() {
  416. String file = download->get_download_file();
  417. if (external_install) {
  418. emit_signal(SNAME("install_asset"), file, title->get_text());
  419. return;
  420. }
  421. asset_installer->set_asset_name(title->get_text());
  422. asset_installer->open(file, 1);
  423. }
  424. void EditorAssetLibraryItemDownload::_make_request() {
  425. // Hide the Retry button if we've just pressed it.
  426. retry_button->hide();
  427. download->cancel_request();
  428. download->set_download_file(EditorPaths::get_singleton()->get_cache_dir().path_join("tmp_asset_" + itos(asset_id)) + ".zip");
  429. Error err = download->request(host);
  430. if (err != OK) {
  431. status->set_text(TTR("Error making request"));
  432. } else {
  433. set_process(true);
  434. }
  435. }
  436. void EditorAssetLibraryItemDownload::_bind_methods() {
  437. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  438. }
  439. EditorAssetLibraryItemDownload::EditorAssetLibraryItemDownload() {
  440. panel = memnew(PanelContainer);
  441. add_child(panel);
  442. HBoxContainer *hb = memnew(HBoxContainer);
  443. panel->add_child(hb);
  444. icon = memnew(TextureRect);
  445. icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  446. icon->set_v_size_flags(0);
  447. hb->add_child(icon);
  448. VBoxContainer *vb = memnew(VBoxContainer);
  449. hb->add_child(vb);
  450. vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  451. HBoxContainer *title_hb = memnew(HBoxContainer);
  452. vb->add_child(title_hb);
  453. title = memnew(Label);
  454. title_hb->add_child(title);
  455. title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  456. dismiss_button = memnew(TextureButton);
  457. dismiss_button->connect("pressed", callable_mp(this, &EditorAssetLibraryItemDownload::_close));
  458. title_hb->add_child(dismiss_button);
  459. title->set_clip_text(true);
  460. vb->add_spacer();
  461. status = memnew(Label(TTR("Idle")));
  462. vb->add_child(status);
  463. progress = memnew(ProgressBar);
  464. vb->add_child(progress);
  465. HBoxContainer *hb2 = memnew(HBoxContainer);
  466. vb->add_child(hb2);
  467. hb2->add_spacer();
  468. install_button = memnew(Button);
  469. install_button->set_text(TTR("Install..."));
  470. install_button->set_disabled(true);
  471. install_button->connect("pressed", callable_mp(this, &EditorAssetLibraryItemDownload::install));
  472. retry_button = memnew(Button);
  473. retry_button->set_text(TTR("Retry"));
  474. retry_button->connect("pressed", callable_mp(this, &EditorAssetLibraryItemDownload::_make_request));
  475. // Only show the Retry button in case of a failure.
  476. retry_button->hide();
  477. hb2->add_child(retry_button);
  478. hb2->add_child(install_button);
  479. set_custom_minimum_size(Size2(310, 0) * EDSCALE);
  480. download = memnew(HTTPRequest);
  481. panel->add_child(download);
  482. download->connect("request_completed", callable_mp(this, &EditorAssetLibraryItemDownload::_http_download_completed));
  483. setup_http_request(download);
  484. download_error = memnew(AcceptDialog);
  485. panel->add_child(download_error);
  486. download_error->set_title(TTR("Download Error"));
  487. asset_installer = memnew(EditorAssetInstaller);
  488. panel->add_child(asset_installer);
  489. asset_installer->connect("confirmed", callable_mp(this, &EditorAssetLibraryItemDownload::_close));
  490. prev_status = -1;
  491. external_install = false;
  492. }
  493. ////////////////////////////////////////////////////////////////////////////////
  494. void EditorAssetLibrary::_notification(int p_what) {
  495. switch (p_what) {
  496. case NOTIFICATION_READY: {
  497. add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("AssetLib")));
  498. error_label->move_to_front();
  499. } break;
  500. case NOTIFICATION_ENTER_TREE:
  501. case NOTIFICATION_THEME_CHANGED: {
  502. error_tr->set_texture(get_editor_theme_icon(SNAME("Error")));
  503. filter->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  504. library_scroll_bg->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  505. downloads_scroll->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  506. error_label->add_theme_color_override("color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  507. } break;
  508. case NOTIFICATION_VISIBILITY_CHANGED: {
  509. if (is_visible()) {
  510. #ifndef ANDROID_ENABLED
  511. // Focus the search box automatically when switching to the Templates tab (in the Project Manager)
  512. // or switching to the AssetLib tab (in the editor).
  513. // The Project Manager's project filter box is automatically focused in the project manager code.
  514. filter->grab_focus();
  515. #endif
  516. if (initial_loading) {
  517. _repository_changed(0); // Update when shown for the first time.
  518. }
  519. }
  520. } break;
  521. case NOTIFICATION_PROCESS: {
  522. HTTPClient::Status s = request->get_http_client_status();
  523. const bool loading = s != HTTPClient::STATUS_DISCONNECTED;
  524. if (loading) {
  525. library_scroll->set_modulate(Color(1, 1, 1, 0.5));
  526. } else {
  527. library_scroll->set_modulate(Color(1, 1, 1, 1));
  528. }
  529. const bool no_downloads = downloads_hb->get_child_count() == 0;
  530. if (no_downloads == downloads_scroll->is_visible()) {
  531. downloads_scroll->set_visible(!no_downloads);
  532. }
  533. } break;
  534. case NOTIFICATION_RESIZED: {
  535. _update_asset_items_columns();
  536. } break;
  537. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  538. _update_repository_options();
  539. setup_http_request(request);
  540. } break;
  541. }
  542. }
  543. void EditorAssetLibrary::_update_repository_options() {
  544. Dictionary default_urls;
  545. default_urls["godotengine.org (Official)"] = "https://godotengine.org/asset-library/api";
  546. Dictionary available_urls = _EDITOR_DEF("asset_library/available_urls", default_urls, true);
  547. repository->clear();
  548. Array keys = available_urls.keys();
  549. for (int i = 0; i < keys.size(); i++) {
  550. String key = keys[i];
  551. repository->add_item(key);
  552. repository->set_item_metadata(i, available_urls[key]);
  553. }
  554. }
  555. void EditorAssetLibrary::shortcut_input(const Ref<InputEvent> &p_event) {
  556. ERR_FAIL_COND(p_event.is_null());
  557. const Ref<InputEventKey> key = p_event;
  558. if (key.is_valid() && key->is_pressed()) {
  559. if (key->is_match(InputEventKey::create_reference(KeyModifierMask::CMD_OR_CTRL | Key::F)) && is_visible_in_tree()) {
  560. filter->grab_focus();
  561. filter->select_all();
  562. accept_event();
  563. }
  564. }
  565. }
  566. void EditorAssetLibrary::_install_asset() {
  567. ERR_FAIL_COND(!description);
  568. EditorAssetLibraryItemDownload *d = _get_asset_in_progress(description->get_asset_id());
  569. if (d) {
  570. d->install();
  571. return;
  572. }
  573. EditorAssetLibraryItemDownload *download = memnew(EditorAssetLibraryItemDownload);
  574. downloads_hb->add_child(download);
  575. download->configure(description->get_title(), description->get_asset_id(), description->get_preview_icon(), description->get_download_url(), description->get_sha256());
  576. if (templates_only) {
  577. download->set_external_install(true);
  578. download->connect("install_asset", callable_mp(this, &EditorAssetLibrary::_install_external_asset));
  579. }
  580. }
  581. const char *EditorAssetLibrary::sort_key[SORT_MAX] = {
  582. "updated",
  583. "updated",
  584. "name",
  585. "name",
  586. "cost",
  587. "cost",
  588. };
  589. const char *EditorAssetLibrary::sort_text[SORT_MAX] = {
  590. TTRC("Recently Updated"),
  591. TTRC("Least Recently Updated"),
  592. TTRC("Name (A-Z)"),
  593. TTRC("Name (Z-A)"),
  594. TTRC("License (A-Z)"), // "cost" stores the SPDX license name in the Godot Asset Library.
  595. TTRC("License (Z-A)"), // "cost" stores the SPDX license name in the Godot Asset Library.
  596. };
  597. const char *EditorAssetLibrary::support_key[SUPPORT_MAX] = {
  598. "official",
  599. "community",
  600. "testing",
  601. };
  602. const char *EditorAssetLibrary::support_text[SUPPORT_MAX] = {
  603. TTRC("Official"),
  604. TTRC("Community"),
  605. TTRC("Testing"),
  606. };
  607. void EditorAssetLibrary::_select_author(int p_id) {
  608. // Open author window.
  609. }
  610. void EditorAssetLibrary::_select_category(int p_id) {
  611. for (int i = 0; i < categories->get_item_count(); i++) {
  612. if (i == 0) {
  613. continue;
  614. }
  615. int id = categories->get_item_metadata(i);
  616. if (id == p_id) {
  617. categories->select(i);
  618. _search();
  619. break;
  620. }
  621. }
  622. }
  623. void EditorAssetLibrary::_select_asset(int p_id) {
  624. _api_request("asset/" + itos(p_id), REQUESTING_ASSET);
  625. }
  626. void EditorAssetLibrary::_image_update(bool use_cache, bool final, const PackedByteArray &p_data, int p_queue_id) {
  627. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  628. if (obj) {
  629. bool image_set = false;
  630. PackedByteArray image_data = p_data;
  631. if (use_cache) {
  632. String cache_filename_base = EditorPaths::get_singleton()->get_cache_dir().path_join("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
  633. Ref<FileAccess> file = FileAccess::open(cache_filename_base + ".data", FileAccess::READ);
  634. if (file.is_valid()) {
  635. PackedByteArray cached_data;
  636. int len = file->get_32();
  637. cached_data.resize(len);
  638. uint8_t *w = cached_data.ptrw();
  639. file->get_buffer(w, len);
  640. image_data = cached_data;
  641. }
  642. }
  643. int len = image_data.size();
  644. const uint8_t *r = image_data.ptr();
  645. Ref<Image> image = Ref<Image>(memnew(Image));
  646. uint8_t png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
  647. uint8_t jpg_signature[3] = { 255, 216, 255 };
  648. uint8_t webp_signature[4] = { 82, 73, 70, 70 };
  649. uint8_t bmp_signature[2] = { 66, 77 };
  650. if (r) {
  651. if ((memcmp(&r[0], &png_signature[0], 8) == 0) && Image::_png_mem_loader_func) {
  652. image->copy_internals_from(Image::_png_mem_loader_func(r, len));
  653. } else if ((memcmp(&r[0], &jpg_signature[0], 3) == 0) && Image::_jpg_mem_loader_func) {
  654. image->copy_internals_from(Image::_jpg_mem_loader_func(r, len));
  655. } else if ((memcmp(&r[0], &webp_signature[0], 4) == 0) && Image::_webp_mem_loader_func) {
  656. image->copy_internals_from(Image::_webp_mem_loader_func(r, len));
  657. } else if ((memcmp(&r[0], &bmp_signature[0], 2) == 0) && Image::_bmp_mem_loader_func) {
  658. image->copy_internals_from(Image::_bmp_mem_loader_func(r, len));
  659. } else if (Image::_svg_scalable_mem_loader_func) {
  660. image->copy_internals_from(Image::_svg_scalable_mem_loader_func(r, len, 1.0));
  661. }
  662. }
  663. if (!image->is_empty()) {
  664. switch (image_queue[p_queue_id].image_type) {
  665. case IMAGE_QUEUE_ICON:
  666. image->resize(64 * EDSCALE, 64 * EDSCALE, Image::INTERPOLATE_LANCZOS);
  667. break;
  668. case IMAGE_QUEUE_THUMBNAIL: {
  669. float max_height = 85 * EDSCALE;
  670. float scale_ratio = max_height / (image->get_height() * EDSCALE);
  671. if (scale_ratio < 1) {
  672. image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
  673. }
  674. } break;
  675. case IMAGE_QUEUE_SCREENSHOT: {
  676. float max_height = 397 * EDSCALE;
  677. float scale_ratio = max_height / (image->get_height() * EDSCALE);
  678. if (scale_ratio < 1) {
  679. image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
  680. }
  681. } break;
  682. }
  683. Ref<ImageTexture> tex = ImageTexture::create_from_image(image);
  684. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, tex);
  685. image_set = true;
  686. }
  687. if (!image_set && final) {
  688. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
  689. }
  690. }
  691. }
  692. void EditorAssetLibrary::_image_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data, int p_queue_id) {
  693. ERR_FAIL_COND(!image_queue.has(p_queue_id));
  694. if (p_status == HTTPRequest::RESULT_SUCCESS && p_code < HTTPClient::RESPONSE_BAD_REQUEST) {
  695. if (p_code != HTTPClient::RESPONSE_NOT_MODIFIED) {
  696. for (int i = 0; i < headers.size(); i++) {
  697. if (headers[i].findn("ETag:") == 0) { // Save etag
  698. String cache_filename_base = EditorPaths::get_singleton()->get_cache_dir().path_join("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
  699. String new_etag = headers[i].substr(headers[i].find(":") + 1, headers[i].length()).strip_edges();
  700. Ref<FileAccess> file = FileAccess::open(cache_filename_base + ".etag", FileAccess::WRITE);
  701. if (file.is_valid()) {
  702. file->store_line(new_etag);
  703. }
  704. int len = p_data.size();
  705. const uint8_t *r = p_data.ptr();
  706. file = FileAccess::open(cache_filename_base + ".data", FileAccess::WRITE);
  707. if (file.is_valid()) {
  708. file->store_32(len);
  709. file->store_buffer(r, len);
  710. }
  711. break;
  712. }
  713. }
  714. }
  715. _image_update(p_code == HTTPClient::RESPONSE_NOT_MODIFIED, true, p_data, p_queue_id);
  716. } else {
  717. WARN_PRINT("Error getting image file from URL: " + image_queue[p_queue_id].image_url);
  718. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  719. if (obj) {
  720. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
  721. }
  722. }
  723. image_queue[p_queue_id].request->queue_free();
  724. image_queue.erase(p_queue_id);
  725. _update_image_queue();
  726. }
  727. void EditorAssetLibrary::_update_image_queue() {
  728. const int max_images = 6;
  729. int current_images = 0;
  730. List<int> to_delete;
  731. for (KeyValue<int, ImageQueue> &E : image_queue) {
  732. if (!E.value.active && current_images < max_images) {
  733. String cache_filename_base = EditorPaths::get_singleton()->get_cache_dir().path_join("assetimage_" + E.value.image_url.md5_text());
  734. Vector<String> headers;
  735. if (FileAccess::exists(cache_filename_base + ".etag") && FileAccess::exists(cache_filename_base + ".data")) {
  736. Ref<FileAccess> file = FileAccess::open(cache_filename_base + ".etag", FileAccess::READ);
  737. if (file.is_valid()) {
  738. headers.push_back("If-None-Match: " + file->get_line());
  739. }
  740. }
  741. Error err = E.value.request->request(E.value.image_url, headers);
  742. if (err != OK) {
  743. to_delete.push_back(E.key);
  744. } else {
  745. E.value.active = true;
  746. }
  747. current_images++;
  748. } else if (E.value.active) {
  749. current_images++;
  750. }
  751. }
  752. while (to_delete.size()) {
  753. image_queue[to_delete.front()->get()].request->queue_free();
  754. image_queue.erase(to_delete.front()->get());
  755. to_delete.pop_front();
  756. }
  757. }
  758. void EditorAssetLibrary::_request_image(ObjectID p_for, String p_image_url, ImageType p_type, int p_image_index) {
  759. ImageQueue iq;
  760. iq.image_url = p_image_url;
  761. iq.image_index = p_image_index;
  762. iq.image_type = p_type;
  763. iq.request = memnew(HTTPRequest);
  764. setup_http_request(iq.request);
  765. iq.target = p_for;
  766. iq.queue_id = ++last_queue_id;
  767. iq.active = false;
  768. iq.request->connect("request_completed", callable_mp(this, &EditorAssetLibrary::_image_request_completed).bind(iq.queue_id));
  769. image_queue[iq.queue_id] = iq;
  770. add_child(iq.request);
  771. _image_update(true, false, PackedByteArray(), iq.queue_id);
  772. _update_image_queue();
  773. }
  774. void EditorAssetLibrary::_repository_changed(int p_repository_id) {
  775. library_error->hide();
  776. library_info->set_text(TTR("Loading..."));
  777. library_info->show();
  778. asset_top_page->hide();
  779. asset_bottom_page->hide();
  780. asset_items->hide();
  781. filter->set_editable(false);
  782. sort->set_disabled(true);
  783. categories->set_disabled(true);
  784. support->set_disabled(true);
  785. host = repository->get_item_metadata(p_repository_id);
  786. if (templates_only) {
  787. _api_request("configure", REQUESTING_CONFIG, "?type=project");
  788. } else {
  789. _api_request("configure", REQUESTING_CONFIG);
  790. }
  791. }
  792. void EditorAssetLibrary::_support_toggled(int p_support) {
  793. support->get_popup()->set_item_checked(p_support, !support->get_popup()->is_item_checked(p_support));
  794. _search();
  795. }
  796. void EditorAssetLibrary::_rerun_search(int p_ignore) {
  797. _search();
  798. }
  799. void EditorAssetLibrary::_search(int p_page) {
  800. String args;
  801. if (templates_only) {
  802. args += "?type=project&";
  803. } else {
  804. args += "?";
  805. }
  806. args += String() + "sort=" + sort_key[sort->get_selected()];
  807. // We use the "branch" version, i.e. major.minor, as patch releases should be compatible
  808. args += "&godot_version=" + String(VERSION_BRANCH);
  809. String support_list;
  810. for (int i = 0; i < SUPPORT_MAX; i++) {
  811. if (support->get_popup()->is_item_checked(i)) {
  812. support_list += String(support_key[i]) + "+";
  813. }
  814. }
  815. if (!support_list.is_empty()) {
  816. args += "&support=" + support_list.substr(0, support_list.length() - 1);
  817. }
  818. if (categories->get_selected() > 0) {
  819. args += "&category=" + itos(categories->get_item_metadata(categories->get_selected()));
  820. }
  821. // Sorting options with an odd index are always the reverse of the previous one
  822. if (sort->get_selected() % 2 == 1) {
  823. args += "&reverse=true";
  824. }
  825. if (!filter->get_text().is_empty()) {
  826. args += "&filter=" + filter->get_text().uri_encode();
  827. }
  828. if (p_page > 0) {
  829. args += "&page=" + itos(p_page);
  830. }
  831. _api_request("asset", REQUESTING_SEARCH, args);
  832. }
  833. void EditorAssetLibrary::_search_text_changed(const String &p_text) {
  834. filter_debounce_timer->start();
  835. }
  836. void EditorAssetLibrary::_filter_debounce_timer_timeout() {
  837. _search();
  838. }
  839. void EditorAssetLibrary::_request_current_config() {
  840. _repository_changed(repository->get_selected());
  841. }
  842. HBoxContainer *EditorAssetLibrary::_make_pages(int p_page, int p_page_count, int p_page_len, int p_total_items, int p_current_items) {
  843. HBoxContainer *hbc = memnew(HBoxContainer);
  844. if (p_page_count < 2) {
  845. return hbc;
  846. }
  847. //do the mario
  848. int from = p_page - 5;
  849. if (from < 0) {
  850. from = 0;
  851. }
  852. int to = from + 10;
  853. if (to > p_page_count) {
  854. to = p_page_count;
  855. }
  856. hbc->add_spacer();
  857. hbc->add_theme_constant_override("separation", 5 * EDSCALE);
  858. Button *first = memnew(Button);
  859. first->set_text(TTR("First", "Pagination"));
  860. if (p_page != 0) {
  861. first->connect("pressed", callable_mp(this, &EditorAssetLibrary::_search).bind(0));
  862. } else {
  863. first->set_disabled(true);
  864. first->set_focus_mode(Control::FOCUS_NONE);
  865. }
  866. hbc->add_child(first);
  867. Button *prev = memnew(Button);
  868. prev->set_text(TTR("Previous", "Pagination"));
  869. if (p_page > 0) {
  870. prev->connect("pressed", callable_mp(this, &EditorAssetLibrary::_search).bind(p_page - 1));
  871. } else {
  872. prev->set_disabled(true);
  873. prev->set_focus_mode(Control::FOCUS_NONE);
  874. }
  875. hbc->add_child(prev);
  876. hbc->add_child(memnew(VSeparator));
  877. for (int i = from; i < to; i++) {
  878. if (i == p_page) {
  879. Button *current = memnew(Button);
  880. // Keep the extended padding for the currently active page (see below).
  881. current->set_text(vformat(" %d ", i + 1));
  882. current->set_disabled(true);
  883. current->set_focus_mode(Control::FOCUS_NONE);
  884. hbc->add_child(current);
  885. } else {
  886. Button *current = memnew(Button);
  887. // Add padding to make page number buttons easier to click.
  888. current->set_text(vformat(" %d ", i + 1));
  889. current->connect("pressed", callable_mp(this, &EditorAssetLibrary::_search).bind(i));
  890. hbc->add_child(current);
  891. }
  892. }
  893. Button *next = memnew(Button);
  894. next->set_text(TTR("Next", "Pagination"));
  895. if (p_page < p_page_count - 1) {
  896. next->connect("pressed", callable_mp(this, &EditorAssetLibrary::_search).bind(p_page + 1));
  897. } else {
  898. next->set_disabled(true);
  899. next->set_focus_mode(Control::FOCUS_NONE);
  900. }
  901. hbc->add_child(memnew(VSeparator));
  902. hbc->add_child(next);
  903. Button *last = memnew(Button);
  904. last->set_text(TTR("Last", "Pagination"));
  905. if (p_page != p_page_count - 1) {
  906. last->connect("pressed", callable_mp(this, &EditorAssetLibrary::_search).bind(p_page_count - 1));
  907. } else {
  908. last->set_disabled(true);
  909. last->set_focus_mode(Control::FOCUS_NONE);
  910. }
  911. hbc->add_child(last);
  912. hbc->add_spacer();
  913. return hbc;
  914. }
  915. void EditorAssetLibrary::_api_request(const String &p_request, RequestType p_request_type, const String &p_arguments) {
  916. if (requesting != REQUESTING_NONE) {
  917. request->cancel_request();
  918. }
  919. requesting = p_request_type;
  920. error_hb->hide();
  921. request->request(host + "/" + p_request + p_arguments);
  922. }
  923. void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
  924. String str;
  925. {
  926. int datalen = p_data.size();
  927. const uint8_t *r = p_data.ptr();
  928. str.parse_utf8((const char *)r, datalen);
  929. }
  930. bool error_abort = true;
  931. switch (p_status) {
  932. case HTTPRequest::RESULT_CANT_RESOLVE: {
  933. error_label->set_text(TTR("Can't resolve hostname:") + " " + host);
  934. } break;
  935. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  936. case HTTPRequest::RESULT_CONNECTION_ERROR:
  937. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
  938. error_label->set_text(TTR("Connection error, please try again."));
  939. } break;
  940. case HTTPRequest::RESULT_TLS_HANDSHAKE_ERROR:
  941. case HTTPRequest::RESULT_CANT_CONNECT: {
  942. error_label->set_text(TTR("Can't connect to host:") + " " + host);
  943. } break;
  944. case HTTPRequest::RESULT_NO_RESPONSE: {
  945. error_label->set_text(TTR("No response from host:") + " " + host);
  946. } break;
  947. case HTTPRequest::RESULT_REQUEST_FAILED: {
  948. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  949. } break;
  950. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  951. error_label->set_text(TTR("Request failed, too many redirects"));
  952. } break;
  953. default: {
  954. if (p_code != 200) {
  955. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  956. } else {
  957. error_abort = false;
  958. }
  959. } break;
  960. }
  961. if (error_abort) {
  962. if (requesting == REQUESTING_CONFIG) {
  963. library_info->hide();
  964. library_error->show();
  965. }
  966. error_hb->show();
  967. return;
  968. }
  969. Dictionary d;
  970. {
  971. JSON json;
  972. json.parse(str);
  973. d = json.get_data();
  974. }
  975. RequestType requested = requesting;
  976. requesting = REQUESTING_NONE;
  977. switch (requested) {
  978. case REQUESTING_CONFIG: {
  979. categories->clear();
  980. categories->add_item(TTR("All"));
  981. categories->set_item_metadata(0, 0);
  982. if (d.has("categories")) {
  983. Array clist = d["categories"];
  984. for (int i = 0; i < clist.size(); i++) {
  985. Dictionary cat = clist[i];
  986. if (!cat.has("name") || !cat.has("id")) {
  987. continue;
  988. }
  989. String name = cat["name"];
  990. int id = cat["id"];
  991. categories->add_item(name);
  992. categories->set_item_metadata(-1, id);
  993. category_map[cat["id"]] = name;
  994. }
  995. }
  996. filter->set_editable(true);
  997. sort->set_disabled(false);
  998. categories->set_disabled(false);
  999. support->set_disabled(false);
  1000. _search();
  1001. } break;
  1002. case REQUESTING_SEARCH: {
  1003. initial_loading = false;
  1004. if (asset_items) {
  1005. memdelete(asset_items);
  1006. }
  1007. if (asset_top_page) {
  1008. memdelete(asset_top_page);
  1009. }
  1010. if (asset_bottom_page) {
  1011. memdelete(asset_bottom_page);
  1012. }
  1013. int page = 0;
  1014. int pages = 1;
  1015. int page_len = 10;
  1016. int total_items = 1;
  1017. Array result;
  1018. if (d.has("page")) {
  1019. page = d["page"];
  1020. }
  1021. if (d.has("pages")) {
  1022. pages = d["pages"];
  1023. }
  1024. if (d.has("page_length")) {
  1025. page_len = d["page_length"];
  1026. }
  1027. if (d.has("total")) {
  1028. total_items = d["total"];
  1029. }
  1030. if (d.has("result")) {
  1031. result = d["result"];
  1032. }
  1033. asset_top_page = _make_pages(page, pages, page_len, total_items, result.size());
  1034. library_vb->add_child(asset_top_page);
  1035. asset_items = memnew(GridContainer);
  1036. _update_asset_items_columns();
  1037. asset_items->add_theme_constant_override("h_separation", 10 * EDSCALE);
  1038. asset_items->add_theme_constant_override("v_separation", 10 * EDSCALE);
  1039. library_vb->add_child(asset_items);
  1040. asset_bottom_page = _make_pages(page, pages, page_len, total_items, result.size());
  1041. library_vb->add_child(asset_bottom_page);
  1042. if (result.is_empty()) {
  1043. String support_list;
  1044. for (int i = 0; i < SUPPORT_MAX; i++) {
  1045. if (support->get_popup()->is_item_checked(i)) {
  1046. if (!support_list.is_empty()) {
  1047. support_list += ", ";
  1048. }
  1049. support_list += TTRGET(support_text[i]);
  1050. }
  1051. }
  1052. if (support_list.is_empty()) {
  1053. support_list = "-";
  1054. }
  1055. if (!filter->get_text().is_empty()) {
  1056. library_info->set_text(
  1057. vformat(TTR("No results for \"%s\" for support level(s): %s."), filter->get_text(), support_list));
  1058. } else {
  1059. // No results, even though the user didn't search for anything specific.
  1060. // This is typically because the version number changed recently
  1061. // and no assets compatible with the new version have been published yet.
  1062. library_info->set_text(
  1063. vformat(TTR("No results compatible with %s %s for support level(s): %s.\nCheck the enabled support levels using the 'Support' button in the top-right corner."), String(VERSION_SHORT_NAME).capitalize(), String(VERSION_BRANCH), support_list));
  1064. }
  1065. library_info->show();
  1066. } else {
  1067. library_info->hide();
  1068. }
  1069. for (int i = 0; i < result.size(); i++) {
  1070. Dictionary r = result[i];
  1071. ERR_CONTINUE(!r.has("title"));
  1072. ERR_CONTINUE(!r.has("asset_id"));
  1073. ERR_CONTINUE(!r.has("author"));
  1074. ERR_CONTINUE(!r.has("author_id"));
  1075. ERR_CONTINUE(!r.has("category_id"));
  1076. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  1077. ERR_CONTINUE(!r.has("cost"));
  1078. EditorAssetLibraryItem *item = memnew(EditorAssetLibraryItem);
  1079. asset_items->add_child(item);
  1080. item->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"]);
  1081. item->connect("asset_selected", callable_mp(this, &EditorAssetLibrary::_select_asset));
  1082. item->connect("author_selected", callable_mp(this, &EditorAssetLibrary::_select_author));
  1083. item->connect("category_selected", callable_mp(this, &EditorAssetLibrary::_select_category));
  1084. if (r.has("icon_url") && !r["icon_url"].operator String().is_empty()) {
  1085. _request_image(item->get_instance_id(), r["icon_url"], IMAGE_QUEUE_ICON, 0);
  1086. }
  1087. }
  1088. if (!result.is_empty()) {
  1089. library_scroll->set_v_scroll(0);
  1090. }
  1091. } break;
  1092. case REQUESTING_ASSET: {
  1093. Dictionary r = d;
  1094. ERR_FAIL_COND(!r.has("title"));
  1095. ERR_FAIL_COND(!r.has("asset_id"));
  1096. ERR_FAIL_COND(!r.has("author"));
  1097. ERR_FAIL_COND(!r.has("author_id"));
  1098. ERR_FAIL_COND(!r.has("version"));
  1099. ERR_FAIL_COND(!r.has("version_string"));
  1100. ERR_FAIL_COND(!r.has("category_id"));
  1101. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  1102. ERR_FAIL_COND(!r.has("cost"));
  1103. ERR_FAIL_COND(!r.has("description"));
  1104. ERR_FAIL_COND(!r.has("download_url"));
  1105. ERR_FAIL_COND(!r.has("download_hash"));
  1106. ERR_FAIL_COND(!r.has("browse_url"));
  1107. if (description) {
  1108. memdelete(description);
  1109. }
  1110. description = memnew(EditorAssetLibraryItemDescription);
  1111. add_child(description);
  1112. description->popup_centered();
  1113. description->connect("confirmed", callable_mp(this, &EditorAssetLibrary::_install_asset));
  1114. description->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"], r["version"], r["version_string"], r["description"], r["download_url"], r["browse_url"], r["download_hash"]);
  1115. EditorAssetLibraryItemDownload *download_item = _get_asset_in_progress(description->get_asset_id());
  1116. if (download_item) {
  1117. if (download_item->can_install()) {
  1118. description->set_ok_button_text(TTR("Install"));
  1119. description->get_ok_button()->set_disabled(false);
  1120. } else {
  1121. description->set_ok_button_text(TTR("Downloading..."));
  1122. description->get_ok_button()->set_disabled(true);
  1123. }
  1124. } else {
  1125. description->set_ok_button_text(TTR("Download"));
  1126. description->get_ok_button()->set_disabled(false);
  1127. }
  1128. if (r.has("icon_url") && !r["icon_url"].operator String().is_empty()) {
  1129. _request_image(description->get_instance_id(), r["icon_url"], IMAGE_QUEUE_ICON, 0);
  1130. }
  1131. if (d.has("previews")) {
  1132. Array previews = d["previews"];
  1133. for (int i = 0; i < previews.size(); i++) {
  1134. Dictionary p = previews[i];
  1135. ERR_CONTINUE(!p.has("type"));
  1136. ERR_CONTINUE(!p.has("link"));
  1137. bool is_video = p.has("type") && String(p["type"]) == "video";
  1138. String video_url;
  1139. if (is_video && p.has("link")) {
  1140. video_url = p["link"];
  1141. }
  1142. description->add_preview(i, is_video, video_url);
  1143. if (p.has("thumbnail")) {
  1144. _request_image(description->get_instance_id(), p["thumbnail"], IMAGE_QUEUE_THUMBNAIL, i);
  1145. }
  1146. if (!is_video) {
  1147. _request_image(description->get_instance_id(), p["link"], IMAGE_QUEUE_SCREENSHOT, i);
  1148. }
  1149. }
  1150. }
  1151. } break;
  1152. default:
  1153. break;
  1154. }
  1155. }
  1156. void EditorAssetLibrary::_asset_file_selected(const String &p_file) {
  1157. if (asset_installer) {
  1158. memdelete(asset_installer);
  1159. asset_installer = nullptr;
  1160. }
  1161. asset_installer = memnew(EditorAssetInstaller);
  1162. asset_installer->set_asset_name(p_file.get_basename());
  1163. add_child(asset_installer);
  1164. asset_installer->open(p_file);
  1165. }
  1166. void EditorAssetLibrary::_asset_open() {
  1167. asset_open->popup_file_dialog();
  1168. }
  1169. void EditorAssetLibrary::_manage_plugins() {
  1170. ProjectSettingsEditor::get_singleton()->popup_project_settings(true);
  1171. ProjectSettingsEditor::get_singleton()->set_plugins_page();
  1172. }
  1173. EditorAssetLibraryItemDownload *EditorAssetLibrary::_get_asset_in_progress(int p_asset_id) const {
  1174. for (int i = 0; i < downloads_hb->get_child_count(); i++) {
  1175. EditorAssetLibraryItemDownload *d = Object::cast_to<EditorAssetLibraryItemDownload>(downloads_hb->get_child(i));
  1176. if (d && d->get_asset_id() == p_asset_id) {
  1177. return d;
  1178. }
  1179. }
  1180. return nullptr;
  1181. }
  1182. void EditorAssetLibrary::_install_external_asset(String p_zip_path, String p_title) {
  1183. emit_signal(SNAME("install_asset"), p_zip_path, p_title);
  1184. }
  1185. void EditorAssetLibrary::_update_asset_items_columns() {
  1186. int new_columns = get_size().x / (450.0 * EDSCALE);
  1187. new_columns = MAX(1, new_columns);
  1188. if (new_columns != asset_items->get_columns()) {
  1189. asset_items->set_columns(new_columns);
  1190. }
  1191. }
  1192. void EditorAssetLibrary::disable_community_support() {
  1193. support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, false);
  1194. }
  1195. void EditorAssetLibrary::_bind_methods() {
  1196. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  1197. }
  1198. EditorAssetLibrary::EditorAssetLibrary(bool p_templates_only) {
  1199. requesting = REQUESTING_NONE;
  1200. templates_only = p_templates_only;
  1201. initial_loading = true;
  1202. VBoxContainer *library_main = memnew(VBoxContainer);
  1203. add_child(library_main);
  1204. HBoxContainer *search_hb = memnew(HBoxContainer);
  1205. library_main->add_child(search_hb);
  1206. library_main->add_theme_constant_override("separation", 10 * EDSCALE);
  1207. filter = memnew(LineEdit);
  1208. if (templates_only) {
  1209. filter->set_placeholder(TTR("Search Templates, Projects, and Demos"));
  1210. } else {
  1211. filter->set_placeholder(TTR("Search Assets (Excluding Templates, Projects, and Demos)"));
  1212. }
  1213. filter->set_clear_button_enabled(true);
  1214. search_hb->add_child(filter);
  1215. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1216. filter->connect("text_changed", callable_mp(this, &EditorAssetLibrary::_search_text_changed));
  1217. // Perform a search automatically if the user hasn't entered any text for a certain duration.
  1218. // This way, the user doesn't need to press Enter to initiate their search.
  1219. filter_debounce_timer = memnew(Timer);
  1220. filter_debounce_timer->set_one_shot(true);
  1221. filter_debounce_timer->set_wait_time(0.25);
  1222. filter_debounce_timer->connect("timeout", callable_mp(this, &EditorAssetLibrary::_filter_debounce_timer_timeout));
  1223. search_hb->add_child(filter_debounce_timer);
  1224. if (!p_templates_only) {
  1225. search_hb->add_child(memnew(VSeparator));
  1226. }
  1227. Button *open_asset = memnew(Button);
  1228. open_asset->set_text(TTR("Import..."));
  1229. search_hb->add_child(open_asset);
  1230. open_asset->connect("pressed", callable_mp(this, &EditorAssetLibrary::_asset_open));
  1231. Button *plugins = memnew(Button);
  1232. plugins->set_text(TTR("Plugins..."));
  1233. search_hb->add_child(plugins);
  1234. plugins->connect("pressed", callable_mp(this, &EditorAssetLibrary::_manage_plugins));
  1235. if (p_templates_only) {
  1236. open_asset->hide();
  1237. plugins->hide();
  1238. }
  1239. HBoxContainer *search_hb2 = memnew(HBoxContainer);
  1240. library_main->add_child(search_hb2);
  1241. search_hb2->add_child(memnew(Label(TTR("Sort:") + " ")));
  1242. sort = memnew(OptionButton);
  1243. for (int i = 0; i < SORT_MAX; i++) {
  1244. sort->add_item(TTRGET(sort_text[i]));
  1245. }
  1246. search_hb2->add_child(sort);
  1247. sort->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1248. sort->set_clip_text(true);
  1249. sort->connect("item_selected", callable_mp(this, &EditorAssetLibrary::_rerun_search));
  1250. search_hb2->add_child(memnew(VSeparator));
  1251. search_hb2->add_child(memnew(Label(TTR("Category:") + " ")));
  1252. categories = memnew(OptionButton);
  1253. categories->add_item(TTR("All"));
  1254. search_hb2->add_child(categories);
  1255. categories->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1256. categories->set_clip_text(true);
  1257. categories->connect("item_selected", callable_mp(this, &EditorAssetLibrary::_rerun_search));
  1258. search_hb2->add_child(memnew(VSeparator));
  1259. search_hb2->add_child(memnew(Label(TTR("Site:") + " ")));
  1260. repository = memnew(OptionButton);
  1261. _update_repository_options();
  1262. repository->connect("item_selected", callable_mp(this, &EditorAssetLibrary::_repository_changed));
  1263. search_hb2->add_child(repository);
  1264. repository->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1265. repository->set_clip_text(true);
  1266. search_hb2->add_child(memnew(VSeparator));
  1267. support = memnew(MenuButton);
  1268. search_hb2->add_child(support);
  1269. support->set_text(TTR("Support"));
  1270. support->get_popup()->set_hide_on_checkable_item_selection(false);
  1271. support->get_popup()->add_check_item(TTRGET(support_text[SUPPORT_OFFICIAL]), SUPPORT_OFFICIAL);
  1272. support->get_popup()->add_check_item(TTRGET(support_text[SUPPORT_COMMUNITY]), SUPPORT_COMMUNITY);
  1273. support->get_popup()->add_check_item(TTRGET(support_text[SUPPORT_TESTING]), SUPPORT_TESTING);
  1274. support->get_popup()->set_item_checked(SUPPORT_OFFICIAL, true);
  1275. support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, true);
  1276. support->get_popup()->connect("id_pressed", callable_mp(this, &EditorAssetLibrary::_support_toggled));
  1277. /////////
  1278. library_scroll_bg = memnew(PanelContainer);
  1279. library_main->add_child(library_scroll_bg);
  1280. library_scroll_bg->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1281. library_scroll = memnew(ScrollContainer);
  1282. library_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1283. library_scroll_bg->add_child(library_scroll);
  1284. Ref<StyleBoxEmpty> border2;
  1285. border2.instantiate();
  1286. border2->set_content_margin_individual(15 * EDSCALE, 15 * EDSCALE, 35 * EDSCALE, 15 * EDSCALE);
  1287. PanelContainer *library_vb_border = memnew(PanelContainer);
  1288. library_scroll->add_child(library_vb_border);
  1289. library_vb_border->add_theme_style_override("panel", border2);
  1290. library_vb_border->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1291. library_vb = memnew(VBoxContainer);
  1292. library_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1293. library_vb_border->add_child(library_vb);
  1294. library_info = memnew(Label);
  1295. library_info->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1296. library_vb->add_child(library_info);
  1297. library_error = memnew(VBoxContainer);
  1298. library_error->hide();
  1299. library_vb->add_child(library_error);
  1300. library_error_label = memnew(Label(TTR("Failed to get repository configuration.")));
  1301. library_error_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1302. library_error->add_child(library_error_label);
  1303. library_error_retry = memnew(Button(TTR("Retry")));
  1304. library_error_retry->set_h_size_flags(SIZE_SHRINK_CENTER);
  1305. library_error_retry->connect("pressed", callable_mp(this, &EditorAssetLibrary::_request_current_config));
  1306. library_error->add_child(library_error_retry);
  1307. asset_top_page = memnew(HBoxContainer);
  1308. library_vb->add_child(asset_top_page);
  1309. asset_items = memnew(GridContainer);
  1310. _update_asset_items_columns();
  1311. asset_items->add_theme_constant_override("h_separation", 10 * EDSCALE);
  1312. asset_items->add_theme_constant_override("v_separation", 10 * EDSCALE);
  1313. library_vb->add_child(asset_items);
  1314. asset_bottom_page = memnew(HBoxContainer);
  1315. library_vb->add_child(asset_bottom_page);
  1316. request = memnew(HTTPRequest);
  1317. add_child(request);
  1318. setup_http_request(request);
  1319. request->connect("request_completed", callable_mp(this, &EditorAssetLibrary::_http_request_completed));
  1320. last_queue_id = 0;
  1321. library_vb->add_theme_constant_override("separation", 20 * EDSCALE);
  1322. error_hb = memnew(HBoxContainer);
  1323. library_main->add_child(error_hb);
  1324. error_label = memnew(Label);
  1325. error_hb->add_child(error_label);
  1326. error_tr = memnew(TextureRect);
  1327. error_tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1328. error_hb->add_child(error_tr);
  1329. description = nullptr;
  1330. set_process(true);
  1331. set_process_shortcut_input(true); // Global shortcuts since there is no main element to be focused.
  1332. downloads_scroll = memnew(ScrollContainer);
  1333. downloads_scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1334. library_main->add_child(downloads_scroll);
  1335. downloads_hb = memnew(HBoxContainer);
  1336. downloads_scroll->add_child(downloads_hb);
  1337. asset_open = memnew(EditorFileDialog);
  1338. asset_open->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1339. asset_open->add_filter("*.zip", TTR("Assets ZIP File"));
  1340. asset_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1341. add_child(asset_open);
  1342. asset_open->connect("file_selected", callable_mp(this, &EditorAssetLibrary::_asset_file_selected));
  1343. asset_installer = nullptr;
  1344. }
  1345. ///////
  1346. bool AssetLibraryEditorPlugin::is_available() {
  1347. #ifdef WEB_ENABLED
  1348. // Asset Library can't work on Web editor for now as most assets are sourced
  1349. // directly from GitHub which does not set CORS.
  1350. return false;
  1351. #else
  1352. return StreamPeerTLS::is_available();
  1353. #endif
  1354. }
  1355. void AssetLibraryEditorPlugin::make_visible(bool p_visible) {
  1356. if (p_visible) {
  1357. addon_library->show();
  1358. } else {
  1359. addon_library->hide();
  1360. }
  1361. }
  1362. AssetLibraryEditorPlugin::AssetLibraryEditorPlugin() {
  1363. addon_library = memnew(EditorAssetLibrary);
  1364. addon_library->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1365. EditorNode::get_singleton()->get_main_screen_control()->add_child(addon_library);
  1366. addon_library->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1367. addon_library->hide();
  1368. }
  1369. AssetLibraryEditorPlugin::~AssetLibraryEditorPlugin() {
  1370. }