editor_file_system.cpp 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589
  1. /**************************************************************************/
  2. /* editor_file_system.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 "editor_file_system.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/extension/gdextension_manager.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/resource_saver.h"
  36. #include "core/object/worker_thread_pool.h"
  37. #include "core/os/os.h"
  38. #include "core/variant/variant_parser.h"
  39. #include "editor/editor_help.h"
  40. #include "editor/editor_node.h"
  41. #include "editor/editor_paths.h"
  42. #include "editor/editor_resource_preview.h"
  43. #include "editor/editor_settings.h"
  44. #include "editor/plugins/script_editor_plugin.h"
  45. #include "editor/project_settings_editor.h"
  46. #include "scene/resources/packed_scene.h"
  47. EditorFileSystem *EditorFileSystem::singleton = nullptr;
  48. //the name is the version, to keep compatibility with different versions of Godot
  49. #define CACHE_FILE_NAME "filesystem_cache8"
  50. int EditorFileSystemDirectory::find_file_index(const String &p_file) const {
  51. for (int i = 0; i < files.size(); i++) {
  52. if (files[i]->file == p_file) {
  53. return i;
  54. }
  55. }
  56. return -1;
  57. }
  58. int EditorFileSystemDirectory::find_dir_index(const String &p_dir) const {
  59. for (int i = 0; i < subdirs.size(); i++) {
  60. if (subdirs[i]->name == p_dir) {
  61. return i;
  62. }
  63. }
  64. return -1;
  65. }
  66. void EditorFileSystemDirectory::force_update() {
  67. // We set modified_time to 0 to force `EditorFileSystem::_scan_fs_changes` to search changes in the directory
  68. modified_time = 0;
  69. }
  70. int EditorFileSystemDirectory::get_subdir_count() const {
  71. return subdirs.size();
  72. }
  73. EditorFileSystemDirectory *EditorFileSystemDirectory::get_subdir(int p_idx) {
  74. ERR_FAIL_INDEX_V(p_idx, subdirs.size(), nullptr);
  75. return subdirs[p_idx];
  76. }
  77. int EditorFileSystemDirectory::get_file_count() const {
  78. return files.size();
  79. }
  80. String EditorFileSystemDirectory::get_file(int p_idx) const {
  81. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  82. return files[p_idx]->file;
  83. }
  84. String EditorFileSystemDirectory::get_path() const {
  85. int parents = 0;
  86. const EditorFileSystemDirectory *efd = this;
  87. // Determine the level of nesting.
  88. while (efd->parent) {
  89. parents++;
  90. efd = efd->parent;
  91. }
  92. if (parents == 0) {
  93. return "res://";
  94. }
  95. // Using PackedStringArray, because the path is built in reverse order.
  96. PackedStringArray path_bits;
  97. // Allocate an array based on nesting. It will store path bits.
  98. path_bits.resize(parents + 2); // Last String is empty, so paths end with /.
  99. String *path_write = path_bits.ptrw();
  100. path_write[0] = "res:/";
  101. efd = this;
  102. for (int i = parents; i > 0; i--) {
  103. path_write[i] = efd->name;
  104. efd = efd->parent;
  105. }
  106. return String("/").join(path_bits);
  107. }
  108. String EditorFileSystemDirectory::get_file_path(int p_idx) const {
  109. return get_path().path_join(get_file(p_idx));
  110. }
  111. Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {
  112. ERR_FAIL_INDEX_V(p_idx, files.size(), Vector<String>());
  113. Vector<String> deps;
  114. for (int i = 0; i < files[p_idx]->deps.size(); i++) {
  115. String dep = files[p_idx]->deps[i];
  116. int sep_idx = dep.find("::"); //may contain type information, unwanted
  117. if (sep_idx != -1) {
  118. dep = dep.substr(0, sep_idx);
  119. }
  120. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(dep);
  121. if (uid != ResourceUID::INVALID_ID) {
  122. //return proper dependency resource from uid
  123. if (ResourceUID::get_singleton()->has_id(uid)) {
  124. dep = ResourceUID::get_singleton()->get_id_path(uid);
  125. } else {
  126. continue;
  127. }
  128. }
  129. deps.push_back(dep);
  130. }
  131. return deps;
  132. }
  133. bool EditorFileSystemDirectory::get_file_import_is_valid(int p_idx) const {
  134. ERR_FAIL_INDEX_V(p_idx, files.size(), false);
  135. return files[p_idx]->import_valid;
  136. }
  137. uint64_t EditorFileSystemDirectory::get_file_modified_time(int p_idx) const {
  138. ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
  139. return files[p_idx]->modified_time;
  140. }
  141. uint64_t EditorFileSystemDirectory::get_file_import_modified_time(int p_idx) const {
  142. ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
  143. return files[p_idx]->import_modified_time;
  144. }
  145. String EditorFileSystemDirectory::get_file_script_class_name(int p_idx) const {
  146. return files[p_idx]->script_class_name;
  147. }
  148. String EditorFileSystemDirectory::get_file_script_class_extends(int p_idx) const {
  149. return files[p_idx]->script_class_extends;
  150. }
  151. String EditorFileSystemDirectory::get_file_script_class_icon_path(int p_idx) const {
  152. return files[p_idx]->script_class_icon_path;
  153. }
  154. String EditorFileSystemDirectory::get_file_icon_path(int p_idx) const {
  155. return files[p_idx]->icon_path;
  156. }
  157. StringName EditorFileSystemDirectory::get_file_type(int p_idx) const {
  158. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  159. return files[p_idx]->type;
  160. }
  161. StringName EditorFileSystemDirectory::get_file_resource_script_class(int p_idx) const {
  162. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  163. return files[p_idx]->resource_script_class;
  164. }
  165. String EditorFileSystemDirectory::get_name() {
  166. return name;
  167. }
  168. EditorFileSystemDirectory *EditorFileSystemDirectory::get_parent() {
  169. return parent;
  170. }
  171. void EditorFileSystemDirectory::_bind_methods() {
  172. ClassDB::bind_method(D_METHOD("get_subdir_count"), &EditorFileSystemDirectory::get_subdir_count);
  173. ClassDB::bind_method(D_METHOD("get_subdir", "idx"), &EditorFileSystemDirectory::get_subdir);
  174. ClassDB::bind_method(D_METHOD("get_file_count"), &EditorFileSystemDirectory::get_file_count);
  175. ClassDB::bind_method(D_METHOD("get_file", "idx"), &EditorFileSystemDirectory::get_file);
  176. ClassDB::bind_method(D_METHOD("get_file_path", "idx"), &EditorFileSystemDirectory::get_file_path);
  177. ClassDB::bind_method(D_METHOD("get_file_type", "idx"), &EditorFileSystemDirectory::get_file_type);
  178. ClassDB::bind_method(D_METHOD("get_file_script_class_name", "idx"), &EditorFileSystemDirectory::get_file_script_class_name);
  179. ClassDB::bind_method(D_METHOD("get_file_script_class_extends", "idx"), &EditorFileSystemDirectory::get_file_script_class_extends);
  180. ClassDB::bind_method(D_METHOD("get_file_import_is_valid", "idx"), &EditorFileSystemDirectory::get_file_import_is_valid);
  181. ClassDB::bind_method(D_METHOD("get_name"), &EditorFileSystemDirectory::get_name);
  182. ClassDB::bind_method(D_METHOD("get_path"), &EditorFileSystemDirectory::get_path);
  183. ClassDB::bind_method(D_METHOD("get_parent"), &EditorFileSystemDirectory::get_parent);
  184. ClassDB::bind_method(D_METHOD("find_file_index", "name"), &EditorFileSystemDirectory::find_file_index);
  185. ClassDB::bind_method(D_METHOD("find_dir_index", "name"), &EditorFileSystemDirectory::find_dir_index);
  186. }
  187. EditorFileSystemDirectory::EditorFileSystemDirectory() {
  188. modified_time = 0;
  189. parent = nullptr;
  190. }
  191. EditorFileSystemDirectory::~EditorFileSystemDirectory() {
  192. for (EditorFileSystemDirectory::FileInfo *fi : files) {
  193. memdelete(fi);
  194. }
  195. for (EditorFileSystemDirectory *dir : subdirs) {
  196. memdelete(dir);
  197. }
  198. }
  199. EditorFileSystem::ScannedDirectory::~ScannedDirectory() {
  200. for (ScannedDirectory *dir : subdirs) {
  201. memdelete(dir);
  202. }
  203. }
  204. void EditorFileSystem::_first_scan_filesystem() {
  205. EditorProgress ep = EditorProgress("first_scan_filesystem", TTR("Project initialization"), 5);
  206. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  207. first_scan_root_dir = memnew(ScannedDirectory);
  208. first_scan_root_dir->full_path = "res://";
  209. HashSet<String> existing_class_names;
  210. HashSet<String> extensions;
  211. ep.step(TTR("Scanning file structure..."), 0, true);
  212. nb_files_total = _scan_new_dir(first_scan_root_dir, d);
  213. // Preloading GDExtensions file extensions to prevent looping on all the resource loaders
  214. // for each files in _first_scan_process_scripts.
  215. List<String> gdextension_extensions;
  216. ResourceLoader::get_recognized_extensions_for_type("GDExtension", &gdextension_extensions);
  217. // This loads the global class names from the scripts and ensures that even if the
  218. // global_script_class_cache.cfg was missing or invalid, the global class names are valid in ScriptServer.
  219. // At the same time, to prevent looping multiple times in all files, it looks for extensions.
  220. ep.step(TTR("Loading global class names..."), 1, true);
  221. _first_scan_process_scripts(first_scan_root_dir, gdextension_extensions, existing_class_names, extensions);
  222. // Removing invalid global class to prevent having invalid paths in ScriptServer.
  223. _remove_invalid_global_class_names(existing_class_names);
  224. // Processing extensions to add new extensions or remove invalid ones.
  225. // Important to do it in the first scan so custom types, new class names, custom importers, etc...
  226. // from extensions are ready to go before plugins, autoloads and resources validation/importation.
  227. // At this point, a restart of the editor should not be needed so we don't use the return value.
  228. ep.step(TTR("Verifying GDExtensions..."), 2, true);
  229. GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions);
  230. // Now that all the global class names should be loaded, create autoloads and plugins.
  231. // This is done after loading the global class names because autoloads and plugins can use
  232. // global class names.
  233. ep.step(TTR("Creating autoload scripts..."), 3, true);
  234. ProjectSettingsEditor::get_singleton()->init_autoloads();
  235. ep.step(TTR("Initializing plugins..."), 4, true);
  236. EditorNode::get_singleton()->init_plugins();
  237. ep.step(TTR("Starting file scan..."), 5, true);
  238. }
  239. void EditorFileSystem::_first_scan_process_scripts(const ScannedDirectory *p_scan_dir, List<String> &p_gdextension_extensions, HashSet<String> &p_existing_class_names, HashSet<String> &p_extensions) {
  240. for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
  241. _first_scan_process_scripts(scan_sub_dir, p_gdextension_extensions, p_existing_class_names, p_extensions);
  242. }
  243. for (const String &scan_file : p_scan_dir->files) {
  244. // Optimization to skip the ResourceLoader::get_resource_type for files
  245. // that are not scripts. Some loader get_resource_type methods read the file
  246. // which can be very slow on large projects.
  247. const String ext = scan_file.get_extension().to_lower();
  248. bool is_script = false;
  249. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  250. if (ScriptServer::get_language(i)->get_extension() == ext) {
  251. is_script = true;
  252. break;
  253. }
  254. }
  255. if (is_script) {
  256. const String path = p_scan_dir->full_path.path_join(scan_file);
  257. const String type = ResourceLoader::get_resource_type(path);
  258. if (ClassDB::is_parent_class(type, SNAME("Script"))) {
  259. String script_class_extends;
  260. String script_class_icon_path;
  261. String script_class_name = _get_global_script_class(type, path, &script_class_extends, &script_class_icon_path);
  262. _register_global_class_script(path, path, type, script_class_name, script_class_extends, script_class_icon_path);
  263. if (!script_class_name.is_empty()) {
  264. p_existing_class_names.insert(script_class_name);
  265. }
  266. }
  267. }
  268. // Check for GDExtensions.
  269. if (p_gdextension_extensions.find(ext)) {
  270. const String path = p_scan_dir->full_path.path_join(scan_file);
  271. const String type = ResourceLoader::get_resource_type(path);
  272. if (type == SNAME("GDExtension")) {
  273. p_extensions.insert(path);
  274. }
  275. }
  276. }
  277. }
  278. void EditorFileSystem::_scan_filesystem() {
  279. // On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
  280. ERR_FAIL_COND(!scanning || new_filesystem || (first_scan && !first_scan_root_dir));
  281. //read .fscache
  282. String cpath;
  283. sources_changed.clear();
  284. file_cache.clear();
  285. String project = ProjectSettings::get_singleton()->get_resource_path();
  286. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
  287. {
  288. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::READ);
  289. bool first = true;
  290. if (f.is_valid()) {
  291. //read the disk cache
  292. while (!f->eof_reached()) {
  293. String l = f->get_line().strip_edges();
  294. if (first) {
  295. if (first_scan) {
  296. // only use this on first scan, afterwards it gets ignored
  297. // this is so on first reimport we synchronize versions, then
  298. // we don't care until editor restart. This is for usability mainly so
  299. // your workflow is not killed after changing a setting by forceful reimporting
  300. // everything there is.
  301. filesystem_settings_version_for_import = l.strip_edges();
  302. if (filesystem_settings_version_for_import != ResourceFormatImporter::get_singleton()->get_import_settings_hash()) {
  303. revalidate_import_files = true;
  304. }
  305. }
  306. first = false;
  307. continue;
  308. }
  309. if (l.is_empty()) {
  310. continue;
  311. }
  312. if (l.begins_with("::")) {
  313. Vector<String> split = l.split("::");
  314. ERR_CONTINUE(split.size() != 3);
  315. const String &name = split[1];
  316. cpath = name;
  317. } else {
  318. // The last section (deps) may contain the same splitter, so limit the maxsplit to 8 to get the complete deps.
  319. Vector<String> split = l.split("::", true, 8);
  320. ERR_CONTINUE(split.size() < 9);
  321. String name = split[0];
  322. String file;
  323. file = name;
  324. name = cpath.path_join(name);
  325. FileCache fc;
  326. fc.type = split[1];
  327. if (fc.type.contains_char('/')) {
  328. fc.type = split[1].get_slice("/", 0);
  329. fc.resource_script_class = split[1].get_slice("/", 1);
  330. }
  331. fc.uid = split[2].to_int();
  332. fc.modification_time = split[3].to_int();
  333. fc.import_modification_time = split[4].to_int();
  334. fc.import_valid = split[5].to_int() != 0;
  335. fc.import_group_file = split[6].strip_edges();
  336. fc.script_class_name = split[7].get_slice("<>", 0);
  337. fc.script_class_extends = split[7].get_slice("<>", 1);
  338. fc.script_class_icon_path = split[7].get_slice("<>", 2);
  339. fc.import_md5 = split[7].get_slice("<>", 3);
  340. String dest_paths = split[7].get_slice("<>", 4);
  341. if (!dest_paths.is_empty()) {
  342. fc.import_dest_paths = dest_paths.split("<*>");
  343. }
  344. String deps = split[8].strip_edges();
  345. if (deps.length()) {
  346. Vector<String> dp = deps.split("<>");
  347. for (int i = 0; i < dp.size(); i++) {
  348. const String &path = dp[i];
  349. fc.deps.push_back(path);
  350. }
  351. }
  352. file_cache[name] = fc;
  353. }
  354. }
  355. }
  356. }
  357. const String update_cache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  358. if (first_scan && FileAccess::exists(update_cache)) {
  359. {
  360. Ref<FileAccess> f2 = FileAccess::open(update_cache, FileAccess::READ);
  361. String l = f2->get_line().strip_edges();
  362. while (!l.is_empty()) {
  363. dep_update_list.insert(l);
  364. file_cache.erase(l); // Erase cache for this, so it gets updated.
  365. l = f2->get_line().strip_edges();
  366. }
  367. }
  368. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  369. d->remove(update_cache); // Bye bye update cache.
  370. }
  371. EditorProgressBG scan_progress("efs", "ScanFS", 1000);
  372. ScanProgress sp;
  373. sp.hi = nb_files_total;
  374. sp.progress = &scan_progress;
  375. new_filesystem = memnew(EditorFileSystemDirectory);
  376. new_filesystem->parent = nullptr;
  377. ScannedDirectory *sd;
  378. HashSet<String> *processed_files = nullptr;
  379. // On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
  380. if (first_scan) {
  381. sd = first_scan_root_dir;
  382. // Will be updated on scan.
  383. ResourceUID::get_singleton()->clear();
  384. processed_files = memnew(HashSet<String>());
  385. } else {
  386. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  387. sd = memnew(ScannedDirectory);
  388. sd->full_path = "res://";
  389. nb_files_total = _scan_new_dir(sd, d);
  390. }
  391. _process_file_system(sd, new_filesystem, sp, processed_files);
  392. if (first_scan) {
  393. _process_removed_files(*processed_files);
  394. }
  395. dep_update_list.clear();
  396. file_cache.clear(); //clear caches, no longer needed
  397. if (first_scan) {
  398. memdelete(first_scan_root_dir);
  399. first_scan_root_dir = nullptr;
  400. memdelete(processed_files);
  401. } else {
  402. //on the first scan this is done from the main thread after re-importing
  403. _save_filesystem_cache();
  404. }
  405. scanning = false;
  406. }
  407. void EditorFileSystem::_save_filesystem_cache() {
  408. group_file_cache.clear();
  409. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
  410. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  411. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  412. f->store_line(filesystem_settings_version_for_import);
  413. _save_filesystem_cache(filesystem, f);
  414. }
  415. void EditorFileSystem::_thread_func(void *_userdata) {
  416. EditorFileSystem *sd = (EditorFileSystem *)_userdata;
  417. sd->_scan_filesystem();
  418. }
  419. bool EditorFileSystem::_is_test_for_reimport_needed(const String &p_path, uint64_t p_last_modification_time, uint64_t p_modification_time, uint64_t p_last_import_modification_time, uint64_t p_import_modification_time, const Vector<String> &p_import_dest_paths) {
  420. // The idea here is to trust the cache. If the last modification times in the cache correspond
  421. // to the last modification times of the files on disk, it means the files have not changed since
  422. // the last import, and the files in .godot/imported (p_import_dest_paths) should all be valid.
  423. if (p_last_modification_time != p_modification_time) {
  424. return true;
  425. }
  426. if (p_last_import_modification_time != p_import_modification_time) {
  427. return true;
  428. }
  429. if (reimport_on_missing_imported_files) {
  430. for (const String &path : p_import_dest_paths) {
  431. if (!FileAccess::exists(path)) {
  432. return true;
  433. }
  434. }
  435. }
  436. return false;
  437. }
  438. bool EditorFileSystem::_test_for_reimport(const String &p_path, const String &p_expected_import_md5) {
  439. if (p_expected_import_md5.is_empty()) {
  440. // Marked as reimportation needed.
  441. return true;
  442. }
  443. String new_md5 = FileAccess::get_md5(p_path + ".import");
  444. if (p_expected_import_md5 != new_md5) {
  445. return true;
  446. }
  447. Error err;
  448. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  449. if (f.is_null()) { // No import file, reimport.
  450. return true;
  451. }
  452. VariantParser::StreamFile stream;
  453. stream.f = f;
  454. String assign;
  455. Variant value;
  456. VariantParser::Tag next_tag;
  457. int lines = 0;
  458. String error_text;
  459. Vector<String> to_check;
  460. String importer_name;
  461. String source_file = "";
  462. String source_md5 = "";
  463. Vector<String> dest_files;
  464. String dest_md5 = "";
  465. int version = 0;
  466. bool found_uid = false;
  467. Variant meta;
  468. while (true) {
  469. assign = Variant();
  470. next_tag.fields.clear();
  471. next_tag.name = String();
  472. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  473. if (err == ERR_FILE_EOF) {
  474. break;
  475. } else if (err != OK) {
  476. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
  477. // Parse error, skip and let user attempt manual reimport to avoid reimport loop.
  478. return false;
  479. }
  480. if (!assign.is_empty()) {
  481. if (assign == "valid" && value.operator bool() == false) {
  482. // Invalid import (failed previous import), skip and let user attempt manual reimport to avoid reimport loop.
  483. return false;
  484. }
  485. if (assign.begins_with("path")) {
  486. to_check.push_back(value);
  487. } else if (assign == "files") {
  488. Array fa = value;
  489. for (const Variant &check_path : fa) {
  490. to_check.push_back(check_path);
  491. }
  492. } else if (assign == "importer_version") {
  493. version = value;
  494. } else if (assign == "importer") {
  495. importer_name = value;
  496. } else if (assign == "uid") {
  497. found_uid = true;
  498. } else if (assign == "source_file") {
  499. source_file = value;
  500. } else if (assign == "dest_files") {
  501. dest_files = value;
  502. } else if (assign == "metadata") {
  503. meta = value;
  504. }
  505. } else if (next_tag.name != "remap" && next_tag.name != "deps") {
  506. break;
  507. }
  508. }
  509. if (importer_name == "keep" || importer_name == "skip") {
  510. return false; // Keep mode, do not reimport.
  511. }
  512. if (!found_uid) {
  513. return true; // UID not found, old format, reimport.
  514. }
  515. // Imported files are gone, reimport.
  516. for (const String &E : to_check) {
  517. if (!FileAccess::exists(E)) {
  518. return true;
  519. }
  520. }
  521. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  522. if (importer.is_null()) {
  523. return true; // The importer has possibly changed, try to reimport.
  524. }
  525. if (importer->get_format_version() > version) {
  526. return true; // Version changed, reimport.
  527. }
  528. if (!importer->are_import_settings_valid(p_path, meta)) {
  529. // Reimport settings are out of sync with project settings, reimport.
  530. return true;
  531. }
  532. // Read the md5's from a separate file (so the import parameters aren't dependent on the file version).
  533. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path);
  534. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err);
  535. if (md5s.is_null()) { // No md5's stored for this resource.
  536. return true;
  537. }
  538. VariantParser::StreamFile md5_stream;
  539. md5_stream.f = md5s;
  540. while (true) {
  541. assign = Variant();
  542. next_tag.fields.clear();
  543. next_tag.name = String();
  544. err = VariantParser::parse_tag_assign_eof(&md5_stream, lines, error_text, next_tag, assign, value, nullptr, true);
  545. if (err == ERR_FILE_EOF) {
  546. break;
  547. } else if (err != OK) {
  548. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import.md5:" + itos(lines) + "' error '" + error_text + "'.");
  549. return false; // Parse error.
  550. }
  551. if (!assign.is_empty()) {
  552. if (assign == "source_md5") {
  553. source_md5 = value;
  554. } else if (assign == "dest_md5") {
  555. dest_md5 = value;
  556. }
  557. }
  558. }
  559. // Check source md5 matching.
  560. if (!source_file.is_empty() && source_file != p_path) {
  561. return true; // File was moved, reimport.
  562. }
  563. if (source_md5.is_empty()) {
  564. return true; // Lacks md5, so just reimport.
  565. }
  566. String md5 = FileAccess::get_md5(p_path);
  567. if (md5 != source_md5) {
  568. return true;
  569. }
  570. if (!dest_files.is_empty() && !dest_md5.is_empty()) {
  571. md5 = FileAccess::get_multiple_md5(dest_files);
  572. if (md5 != dest_md5) {
  573. return true;
  574. }
  575. }
  576. return false; // Nothing changed.
  577. }
  578. Vector<String> EditorFileSystem::_get_import_dest_paths(const String &p_path) {
  579. Error err;
  580. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  581. if (f.is_null()) { // No import file, reimport.
  582. return Vector<String>();
  583. }
  584. VariantParser::StreamFile stream;
  585. stream.f = f;
  586. String assign;
  587. Variant value;
  588. VariantParser::Tag next_tag;
  589. int lines = 0;
  590. String error_text;
  591. Vector<String> dest_paths;
  592. String importer_name;
  593. while (true) {
  594. assign = Variant();
  595. next_tag.fields.clear();
  596. next_tag.name = String();
  597. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  598. if (err == ERR_FILE_EOF) {
  599. break;
  600. } else if (err != OK) {
  601. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
  602. // Parse error, skip and let user attempt manual reimport to avoid reimport loop.
  603. return Vector<String>();
  604. }
  605. if (!assign.is_empty()) {
  606. if (assign == "valid" && value.operator bool() == false) {
  607. // Invalid import (failed previous import), skip and let user attempt manual reimport to avoid reimport loop.
  608. return Vector<String>();
  609. }
  610. if (assign.begins_with("path")) {
  611. dest_paths.push_back(value);
  612. } else if (assign == "files") {
  613. Array fa = value;
  614. for (const Variant &dest_path : fa) {
  615. dest_paths.push_back(dest_path);
  616. }
  617. } else if (assign == "importer") {
  618. importer_name = value;
  619. }
  620. } else if (next_tag.name != "remap" && next_tag.name != "deps") {
  621. break;
  622. }
  623. }
  624. if (importer_name == "keep" || importer_name == "skip") {
  625. return Vector<String>();
  626. }
  627. return dest_paths;
  628. }
  629. bool EditorFileSystem::_scan_import_support(const Vector<String> &reimports) {
  630. if (import_support_queries.size() == 0) {
  631. return false;
  632. }
  633. HashMap<String, int> import_support_test;
  634. Vector<bool> import_support_tested;
  635. import_support_tested.resize(import_support_queries.size());
  636. for (int i = 0; i < import_support_queries.size(); i++) {
  637. import_support_tested.write[i] = false;
  638. if (import_support_queries[i]->is_active()) {
  639. Vector<String> extensions = import_support_queries[i]->get_file_extensions();
  640. for (int j = 0; j < extensions.size(); j++) {
  641. import_support_test.insert(extensions[j], i);
  642. }
  643. }
  644. }
  645. if (import_support_test.size() == 0) {
  646. return false; //well nothing to do
  647. }
  648. for (int i = 0; i < reimports.size(); i++) {
  649. HashMap<String, int>::Iterator E = import_support_test.find(reimports[i].get_extension().to_lower());
  650. if (E) {
  651. import_support_tested.write[E->value] = true;
  652. }
  653. }
  654. for (int i = 0; i < import_support_tested.size(); i++) {
  655. if (import_support_tested[i]) {
  656. if (import_support_queries.write[i]->query()) {
  657. return true;
  658. }
  659. }
  660. }
  661. return false;
  662. }
  663. bool EditorFileSystem::_update_scan_actions() {
  664. sources_changed.clear();
  665. // We need to update the script global class names before the reimports to be sure that
  666. // all the importer classes that depends on class names will work.
  667. _update_script_classes();
  668. bool fs_changed = false;
  669. Vector<String> reimports;
  670. Vector<String> reloads;
  671. EditorProgress *ep = nullptr;
  672. if (scan_actions.size() > 1) {
  673. ep = memnew(EditorProgress("_update_scan_actions", TTR("Scanning actions..."), scan_actions.size()));
  674. }
  675. int step_count = 0;
  676. for (const ItemAction &ia : scan_actions) {
  677. switch (ia.action) {
  678. case ItemAction::ACTION_NONE: {
  679. } break;
  680. case ItemAction::ACTION_DIR_ADD: {
  681. int idx = 0;
  682. for (int i = 0; i < ia.dir->subdirs.size(); i++) {
  683. if (ia.new_dir->name.filenocasecmp_to(ia.dir->subdirs[i]->name) < 0) {
  684. break;
  685. }
  686. idx++;
  687. }
  688. if (idx == ia.dir->subdirs.size()) {
  689. ia.dir->subdirs.push_back(ia.new_dir);
  690. } else {
  691. ia.dir->subdirs.insert(idx, ia.new_dir);
  692. }
  693. fs_changed = true;
  694. } break;
  695. case ItemAction::ACTION_DIR_REMOVE: {
  696. ERR_CONTINUE(!ia.dir->parent);
  697. ia.dir->parent->subdirs.erase(ia.dir);
  698. memdelete(ia.dir);
  699. fs_changed = true;
  700. } break;
  701. case ItemAction::ACTION_FILE_ADD: {
  702. int idx = 0;
  703. for (int i = 0; i < ia.dir->files.size(); i++) {
  704. if (ia.new_file->file.filenocasecmp_to(ia.dir->files[i]->file) < 0) {
  705. break;
  706. }
  707. idx++;
  708. }
  709. if (idx == ia.dir->files.size()) {
  710. ia.dir->files.push_back(ia.new_file);
  711. } else {
  712. ia.dir->files.insert(idx, ia.new_file);
  713. }
  714. fs_changed = true;
  715. const String new_file_path = ia.dir->get_file_path(idx);
  716. const ResourceUID::ID existing_id = ResourceLoader::get_resource_uid(new_file_path);
  717. if (existing_id != ResourceUID::INVALID_ID) {
  718. const String old_path = ResourceUID::get_singleton()->get_id_path(existing_id);
  719. if (old_path != new_file_path && FileAccess::exists(old_path)) {
  720. const ResourceUID::ID new_id = ResourceUID::get_singleton()->create_id();
  721. ResourceUID::get_singleton()->add_id(new_id, new_file_path);
  722. ResourceSaver::set_uid(new_file_path, new_id);
  723. WARN_PRINT(vformat("Duplicate UID detected for Resource at \"%s\".\nOld Resource path: \"%s\". The new file UID was changed automatically.", new_file_path, old_path));
  724. } else {
  725. // Re-assign the UID to file, just in case it was pulled from cache.
  726. ResourceSaver::set_uid(new_file_path, existing_id);
  727. }
  728. }
  729. if (ClassDB::is_parent_class(ia.new_file->type, SNAME("Script"))) {
  730. _queue_update_script_class(new_file_path, ia.new_file->type, ia.new_file->script_class_name, ia.new_file->script_class_extends, ia.new_file->script_class_icon_path);
  731. }
  732. if (ia.new_file->type == SNAME("PackedScene")) {
  733. _queue_update_scene_groups(new_file_path);
  734. }
  735. } break;
  736. case ItemAction::ACTION_FILE_REMOVE: {
  737. int idx = ia.dir->find_file_index(ia.file);
  738. ERR_CONTINUE(idx == -1);
  739. String script_class_name = ia.dir->files[idx]->script_class_name;
  740. if (ClassDB::is_parent_class(ia.dir->files[idx]->type, SNAME("Script"))) {
  741. _queue_update_script_class(ia.dir->get_file_path(idx), "", "", "", "");
  742. }
  743. if (ia.dir->files[idx]->type == SNAME("PackedScene")) {
  744. _queue_update_scene_groups(ia.dir->get_file_path(idx));
  745. }
  746. _delete_internal_files(ia.dir->files[idx]->file);
  747. memdelete(ia.dir->files[idx]);
  748. ia.dir->files.remove_at(idx);
  749. // Restore another script with the same global class name if it exists.
  750. if (!script_class_name.is_empty()) {
  751. EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
  752. String old_file = _get_file_by_class_name(filesystem, script_class_name, old_fi);
  753. if (!old_file.is_empty() && old_fi) {
  754. _queue_update_script_class(old_file, old_fi->type, old_fi->script_class_name, old_fi->script_class_extends, old_fi->script_class_icon_path);
  755. }
  756. }
  757. fs_changed = true;
  758. } break;
  759. case ItemAction::ACTION_FILE_TEST_REIMPORT: {
  760. int idx = ia.dir->find_file_index(ia.file);
  761. ERR_CONTINUE(idx == -1);
  762. String full_path = ia.dir->get_file_path(idx);
  763. bool need_reimport = _test_for_reimport(full_path, ia.dir->files[idx]->import_md5);
  764. if (need_reimport) {
  765. // Must reimport.
  766. reimports.push_back(full_path);
  767. Vector<String> dependencies = _get_dependencies(full_path);
  768. for (const String &dep : dependencies) {
  769. const String &dependency_path = dep.contains("::") ? dep.get_slice("::", 0) : dep;
  770. if (import_extensions.has(dep.get_extension())) {
  771. reimports.push_back(dependency_path);
  772. }
  773. }
  774. } else {
  775. // Must not reimport, all was good.
  776. // Update modified times, md5 and destination paths, to avoid reimport.
  777. ia.dir->files[idx]->modified_time = FileAccess::get_modified_time(full_path);
  778. ia.dir->files[idx]->import_modified_time = FileAccess::get_modified_time(full_path + ".import");
  779. if (ia.dir->files[idx]->import_md5.is_empty()) {
  780. ia.dir->files[idx]->import_md5 = FileAccess::get_md5(full_path + ".import");
  781. }
  782. ia.dir->files[idx]->import_dest_paths = _get_import_dest_paths(full_path);
  783. }
  784. fs_changed = true;
  785. } break;
  786. case ItemAction::ACTION_FILE_RELOAD: {
  787. int idx = ia.dir->find_file_index(ia.file);
  788. ERR_CONTINUE(idx == -1);
  789. // Only reloads the resources that are already loaded.
  790. if (ResourceCache::has(ia.dir->get_file_path(idx))) {
  791. reloads.push_back(ia.dir->get_file_path(idx));
  792. }
  793. } break;
  794. }
  795. if (ep) {
  796. ep->step(ia.file, step_count++, false);
  797. }
  798. }
  799. memdelete_notnull(ep);
  800. if (_scan_extensions()) {
  801. //needs editor restart
  802. //extensions also may provide filetypes to be imported, so they must run before importing
  803. if (EditorNode::immediate_confirmation_dialog(TTR("Some extensions need the editor to restart to take effect."), first_scan ? TTR("Restart") : TTR("Save & Restart"), TTR("Continue"))) {
  804. if (!first_scan) {
  805. EditorNode::get_singleton()->save_all_scenes();
  806. }
  807. EditorNode::get_singleton()->restart_editor();
  808. //do not import
  809. return true;
  810. }
  811. }
  812. if (!reimports.is_empty()) {
  813. if (_scan_import_support(reimports)) {
  814. return true;
  815. }
  816. reimport_files(reimports);
  817. } else {
  818. //reimport files will update the uid cache file so if nothing was reimported, update it manually
  819. ResourceUID::get_singleton()->update_cache();
  820. }
  821. if (!reloads.is_empty()) {
  822. // Update global class names, dependencies, etc...
  823. update_files(reloads);
  824. }
  825. if (first_scan) {
  826. //only on first scan this is valid and updated, then settings changed.
  827. revalidate_import_files = false;
  828. filesystem_settings_version_for_import = ResourceFormatImporter::get_singleton()->get_import_settings_hash();
  829. _save_filesystem_cache();
  830. }
  831. // Moving the processing of pending updates before the resources_reload event to be sure all global class names
  832. // are updated. Script.cpp listens on resources_reload and reloads updated scripts.
  833. _process_update_pending();
  834. if (reloads.size()) {
  835. emit_signal(SNAME("resources_reload"), reloads);
  836. }
  837. scan_actions.clear();
  838. return fs_changed;
  839. }
  840. void EditorFileSystem::scan() {
  841. if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/) {
  842. return;
  843. }
  844. if (scanning || scanning_changes || thread.is_started()) {
  845. return;
  846. }
  847. // The first scan must be on the main thread because, after the first scan and update
  848. // of global class names, we load the plugins and autoloads. These need to
  849. // be added on the main thread because they are nodes, and we need to wait for them
  850. // to be loaded to continue the scan and reimportations.
  851. if (first_scan) {
  852. _first_scan_filesystem();
  853. }
  854. _update_extensions();
  855. if (!use_threads) {
  856. scanning = true;
  857. scan_total = 0;
  858. _scan_filesystem();
  859. if (filesystem) {
  860. memdelete(filesystem);
  861. }
  862. //file_type_cache.clear();
  863. filesystem = new_filesystem;
  864. new_filesystem = nullptr;
  865. _update_scan_actions();
  866. // Update all icons so they are loaded for the FileSystemDock.
  867. _update_files_icon_path();
  868. scanning = false;
  869. // Set first_scan to false before the signals so the function doing_first_scan can return false
  870. // in editor_node to start the export if needed.
  871. first_scan = false;
  872. ResourceImporter::load_on_startup = nullptr;
  873. emit_signal(SNAME("filesystem_changed"));
  874. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  875. } else {
  876. ERR_FAIL_COND(thread.is_started());
  877. set_process(true);
  878. Thread::Settings s;
  879. scanning = true;
  880. scan_total = 0;
  881. s.priority = Thread::PRIORITY_LOW;
  882. thread.start(_thread_func, this, s);
  883. }
  884. }
  885. void EditorFileSystem::ScanProgress::increment() {
  886. current++;
  887. float ratio = current / MAX(hi, 1.0f);
  888. if (progress) {
  889. progress->step(ratio * 1000.0f);
  890. }
  891. EditorFileSystem::singleton->scan_total = ratio;
  892. }
  893. int EditorFileSystem::_scan_new_dir(ScannedDirectory *p_dir, Ref<DirAccess> &da) {
  894. List<String> dirs;
  895. List<String> files;
  896. String cd = da->get_current_dir();
  897. da->list_dir_begin();
  898. while (true) {
  899. String f = da->get_next();
  900. if (f.is_empty()) {
  901. break;
  902. }
  903. if (da->current_is_hidden()) {
  904. continue;
  905. }
  906. if (da->current_is_dir()) {
  907. if (f.begins_with(".")) { // Ignore special and . / ..
  908. continue;
  909. }
  910. if (_should_skip_directory(cd.path_join(f))) {
  911. continue;
  912. }
  913. dirs.push_back(f);
  914. } else {
  915. files.push_back(f);
  916. }
  917. }
  918. da->list_dir_end();
  919. dirs.sort_custom<FileNoCaseComparator>();
  920. files.sort_custom<FileNoCaseComparator>();
  921. int nb_files_total_scan = 0;
  922. for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
  923. if (da->change_dir(E->get()) == OK) {
  924. String d = da->get_current_dir();
  925. if (d == cd || !d.begins_with(cd)) {
  926. da->change_dir(cd); //avoid recursion
  927. } else {
  928. ScannedDirectory *sd = memnew(ScannedDirectory);
  929. sd->name = E->get();
  930. sd->full_path = p_dir->full_path.path_join(sd->name);
  931. nb_files_total_scan += _scan_new_dir(sd, da);
  932. p_dir->subdirs.push_back(sd);
  933. da->change_dir("..");
  934. }
  935. } else {
  936. ERR_PRINT("Cannot go into subdir '" + E->get() + "'.");
  937. }
  938. }
  939. p_dir->files = files;
  940. nb_files_total_scan += files.size();
  941. return nb_files_total_scan;
  942. }
  943. void EditorFileSystem::_process_file_system(const ScannedDirectory *p_scan_dir, EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, HashSet<String> *r_processed_files) {
  944. p_dir->modified_time = FileAccess::get_modified_time(p_scan_dir->full_path);
  945. for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
  946. EditorFileSystemDirectory *sub_dir = memnew(EditorFileSystemDirectory);
  947. sub_dir->parent = p_dir;
  948. sub_dir->name = scan_sub_dir->name;
  949. p_dir->subdirs.push_back(sub_dir);
  950. _process_file_system(scan_sub_dir, sub_dir, p_progress, r_processed_files);
  951. }
  952. for (const String &scan_file : p_scan_dir->files) {
  953. String ext = scan_file.get_extension().to_lower();
  954. if (!valid_extensions.has(ext)) {
  955. p_progress.increment();
  956. continue; //invalid
  957. }
  958. String path = p_scan_dir->full_path.path_join(scan_file);
  959. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  960. fi->file = scan_file;
  961. p_dir->files.push_back(fi);
  962. if (r_processed_files) {
  963. r_processed_files->insert(path);
  964. }
  965. FileCache *fc = file_cache.getptr(path);
  966. uint64_t mt = FileAccess::get_modified_time(path);
  967. if (import_extensions.has(ext)) {
  968. //is imported
  969. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  970. if (fc) {
  971. fi->type = fc->type;
  972. fi->resource_script_class = fc->resource_script_class;
  973. fi->uid = fc->uid;
  974. fi->deps = fc->deps;
  975. fi->modified_time = mt;
  976. fi->import_modified_time = import_mt;
  977. fi->import_md5 = fc->import_md5;
  978. fi->import_dest_paths = fc->import_dest_paths;
  979. fi->import_valid = fc->import_valid;
  980. fi->script_class_name = fc->script_class_name;
  981. fi->import_group_file = fc->import_group_file;
  982. fi->script_class_extends = fc->script_class_extends;
  983. fi->script_class_icon_path = fc->script_class_icon_path;
  984. // Ensures backward compatibility when the project is loaded for the first time with the added import_md5
  985. // and import_dest_paths properties in the file cache.
  986. if (fc->import_md5.is_empty()) {
  987. fi->import_md5 = FileAccess::get_md5(path + ".import");
  988. fi->import_dest_paths = _get_import_dest_paths(path);
  989. }
  990. // The method _is_test_for_reimport_needed checks if the files were modified and ensures that
  991. // all the destination files still exist without reading the .import file.
  992. // If something is different, we will queue a test for reimportation that will check
  993. // the md5 of all files and import settings and, if necessary, execute a reimportation.
  994. if (_is_test_for_reimport_needed(path, fc->modification_time, mt, fc->import_modification_time, import_mt, fi->import_dest_paths) ||
  995. (revalidate_import_files && !ResourceFormatImporter::get_singleton()->are_import_settings_valid(path))) {
  996. ItemAction ia;
  997. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  998. ia.dir = p_dir;
  999. ia.file = fi->file;
  1000. scan_actions.push_back(ia);
  1001. }
  1002. if (fc->type.is_empty()) {
  1003. fi->type = ResourceLoader::get_resource_type(path);
  1004. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  1005. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  1006. //there is also the chance that file type changed due to reimport, must probably check this somehow here (or kind of note it for next time in another file?)
  1007. //note: I think this should not happen any longer..
  1008. }
  1009. if (fc->uid == ResourceUID::INVALID_ID) {
  1010. // imported files should always have a UID, so attempt to fetch it.
  1011. fi->uid = ResourceLoader::get_resource_uid(path);
  1012. }
  1013. } else {
  1014. // Using get_resource_import_info() to prevent calling 3 times ResourceFormatImporter::_get_path_and_type.
  1015. ResourceFormatImporter::get_singleton()->get_resource_import_info(path, fi->type, fi->uid, fi->import_group_file);
  1016. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  1017. fi->modified_time = 0;
  1018. fi->import_modified_time = 0;
  1019. fi->import_md5 = "";
  1020. fi->import_dest_paths = Vector<String>();
  1021. fi->import_valid = (fi->type == "TextFile" || fi->type == "OtherFile") ? true : ResourceLoader::is_import_valid(path);
  1022. ItemAction ia;
  1023. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1024. ia.dir = p_dir;
  1025. ia.file = fi->file;
  1026. scan_actions.push_back(ia);
  1027. }
  1028. } else {
  1029. if (fc && fc->modification_time == mt) {
  1030. //not imported, so just update type if changed
  1031. fi->type = fc->type;
  1032. fi->resource_script_class = fc->resource_script_class;
  1033. fi->uid = fc->uid;
  1034. fi->modified_time = mt;
  1035. fi->deps = fc->deps;
  1036. fi->import_modified_time = 0;
  1037. fi->import_md5 = "";
  1038. fi->import_dest_paths = Vector<String>();
  1039. fi->import_valid = true;
  1040. fi->script_class_name = fc->script_class_name;
  1041. fi->script_class_extends = fc->script_class_extends;
  1042. fi->script_class_icon_path = fc->script_class_icon_path;
  1043. if (first_scan && ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  1044. bool update_script = false;
  1045. String old_class_name = fi->script_class_name;
  1046. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  1047. if (old_class_name != fi->script_class_name) {
  1048. update_script = true;
  1049. } else if (!fi->script_class_name.is_empty() && (!ScriptServer::is_global_class(fi->script_class_name) || ScriptServer::get_global_class_path(fi->script_class_name) != path)) {
  1050. // This script has a class name but is not in the global class names or the path of the class has changed.
  1051. update_script = true;
  1052. }
  1053. if (update_script) {
  1054. _queue_update_script_class(path, fi->type, fi->script_class_name, fi->script_class_extends, fi->script_class_icon_path);
  1055. }
  1056. }
  1057. } else {
  1058. //new or modified time
  1059. fi->type = ResourceLoader::get_resource_type(path);
  1060. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  1061. if (fi->type == "" && textfile_extensions.has(ext)) {
  1062. fi->type = "TextFile";
  1063. }
  1064. if (fi->type == "" && other_file_extensions.has(ext)) {
  1065. fi->type = "OtherFile";
  1066. }
  1067. fi->uid = ResourceLoader::get_resource_uid(path);
  1068. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  1069. fi->deps = _get_dependencies(path);
  1070. fi->modified_time = mt;
  1071. fi->import_modified_time = 0;
  1072. fi->import_md5 = "";
  1073. fi->import_dest_paths = Vector<String>();
  1074. fi->import_valid = true;
  1075. // Files in dep_update_list are forced for rescan to update dependencies. They don't need other updates.
  1076. if (!dep_update_list.has(path)) {
  1077. if (ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  1078. _queue_update_script_class(path, fi->type, fi->script_class_name, fi->script_class_extends, fi->script_class_icon_path);
  1079. }
  1080. if (fi->type == SNAME("PackedScene")) {
  1081. _queue_update_scene_groups(path);
  1082. }
  1083. }
  1084. }
  1085. if (ResourceLoader::exists(path) && !ResourceLoader::has_custom_uid_support(path) && !FileAccess::exists(path + ".uid")) {
  1086. // Create a UID file and new UID, if it's invalid.
  1087. Ref<FileAccess> f = FileAccess::open(path + ".uid", FileAccess::WRITE);
  1088. if (f.is_valid()) {
  1089. if (fi->uid == ResourceUID::INVALID_ID) {
  1090. fi->uid = ResourceUID::get_singleton()->create_id();
  1091. } else {
  1092. WARN_PRINT(vformat("Missing .uid file for path \"%s\". The file was re-created from cache.", path));
  1093. }
  1094. f->store_line(ResourceUID::get_singleton()->id_to_text(fi->uid));
  1095. }
  1096. }
  1097. }
  1098. if (fi->uid != ResourceUID::INVALID_ID) {
  1099. if (ResourceUID::get_singleton()->has_id(fi->uid)) {
  1100. // Restrict UID dupe warning to first-scan since we know there are no file moves going on yet.
  1101. if (first_scan) {
  1102. // Warn if we detect files with duplicate UIDs.
  1103. const String other_path = ResourceUID::get_singleton()->get_id_path(fi->uid);
  1104. if (other_path != path) {
  1105. WARN_PRINT(vformat("UID duplicate detected between %s and %s.", path, other_path));
  1106. }
  1107. }
  1108. ResourceUID::get_singleton()->set_id(fi->uid, path);
  1109. } else {
  1110. ResourceUID::get_singleton()->add_id(fi->uid, path);
  1111. }
  1112. }
  1113. p_progress.increment();
  1114. }
  1115. }
  1116. void EditorFileSystem::_process_removed_files(const HashSet<String> &p_processed_files) {
  1117. for (const KeyValue<String, EditorFileSystem::FileCache> &kv : file_cache) {
  1118. if (!p_processed_files.has(kv.key)) {
  1119. if (ClassDB::is_parent_class(kv.value.type, SNAME("Script"))) {
  1120. // A script has been removed from disk since the last startup. The documentation needs to be updated.
  1121. // There's no need to add the path in update_script_paths since that is exclusively for updating global class names,
  1122. // which is handled in _first_scan_filesystem before the full scan to ensure plugins and autoloads can be created.
  1123. MutexLock update_script_lock(update_script_mutex);
  1124. update_script_paths_documentation.insert(kv.key);
  1125. }
  1126. }
  1127. }
  1128. }
  1129. void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, bool p_recursive) {
  1130. uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
  1131. bool updated_dir = false;
  1132. String cd = p_dir->get_path();
  1133. int diff_nb_files = 0;
  1134. if (current_mtime != p_dir->modified_time || using_fat32_or_exfat) {
  1135. updated_dir = true;
  1136. p_dir->modified_time = current_mtime;
  1137. //ooooops, dir changed, see what's going on
  1138. //first mark everything as verified
  1139. for (int i = 0; i < p_dir->files.size(); i++) {
  1140. p_dir->files[i]->verified = false;
  1141. }
  1142. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1143. p_dir->get_subdir(i)->verified = false;
  1144. }
  1145. diff_nb_files -= p_dir->files.size();
  1146. //then scan files and directories and check what's different
  1147. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1148. Error ret = da->change_dir(cd);
  1149. ERR_FAIL_COND_MSG(ret != OK, "Cannot change to '" + cd + "' folder.");
  1150. da->list_dir_begin();
  1151. while (true) {
  1152. String f = da->get_next();
  1153. if (f.is_empty()) {
  1154. break;
  1155. }
  1156. if (da->current_is_hidden()) {
  1157. continue;
  1158. }
  1159. if (da->current_is_dir()) {
  1160. if (f.begins_with(".")) { // Ignore special and . / ..
  1161. continue;
  1162. }
  1163. int idx = p_dir->find_dir_index(f);
  1164. if (idx == -1) {
  1165. String dir_path = cd.path_join(f);
  1166. if (_should_skip_directory(dir_path)) {
  1167. continue;
  1168. }
  1169. ScannedDirectory sd;
  1170. sd.name = f;
  1171. sd.full_path = dir_path;
  1172. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  1173. efd->parent = p_dir;
  1174. efd->name = f;
  1175. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1176. d->change_dir(dir_path);
  1177. int nb_files_dir = _scan_new_dir(&sd, d);
  1178. p_progress.hi += nb_files_dir;
  1179. diff_nb_files += nb_files_dir;
  1180. _process_file_system(&sd, efd, p_progress, nullptr);
  1181. ItemAction ia;
  1182. ia.action = ItemAction::ACTION_DIR_ADD;
  1183. ia.dir = p_dir;
  1184. ia.file = f;
  1185. ia.new_dir = efd;
  1186. scan_actions.push_back(ia);
  1187. } else {
  1188. p_dir->subdirs[idx]->verified = true;
  1189. }
  1190. } else {
  1191. String ext = f.get_extension().to_lower();
  1192. if (!valid_extensions.has(ext)) {
  1193. continue; //invalid
  1194. }
  1195. int idx = p_dir->find_file_index(f);
  1196. if (idx == -1) {
  1197. //never seen this file, add actition to add it
  1198. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1199. fi->file = f;
  1200. String path = cd.path_join(fi->file);
  1201. fi->modified_time = FileAccess::get_modified_time(path);
  1202. fi->import_modified_time = 0;
  1203. fi->import_md5 = "";
  1204. fi->import_dest_paths = Vector<String>();
  1205. fi->type = ResourceLoader::get_resource_type(path);
  1206. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  1207. if (fi->type == "" && textfile_extensions.has(ext)) {
  1208. fi->type = "TextFile";
  1209. }
  1210. if (fi->type == "" && other_file_extensions.has(ext)) {
  1211. fi->type = "OtherFile";
  1212. }
  1213. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  1214. fi->import_valid = (fi->type == "TextFile" || fi->type == "OtherFile") ? true : ResourceLoader::is_import_valid(path);
  1215. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  1216. {
  1217. ItemAction ia;
  1218. ia.action = ItemAction::ACTION_FILE_ADD;
  1219. ia.dir = p_dir;
  1220. ia.file = f;
  1221. ia.new_file = fi;
  1222. scan_actions.push_back(ia);
  1223. }
  1224. if (import_extensions.has(ext)) {
  1225. //if it can be imported, and it was added, it needs to be reimported
  1226. ItemAction ia;
  1227. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1228. ia.dir = p_dir;
  1229. ia.file = f;
  1230. scan_actions.push_back(ia);
  1231. }
  1232. diff_nb_files++;
  1233. } else {
  1234. p_dir->files[idx]->verified = true;
  1235. }
  1236. }
  1237. }
  1238. da->list_dir_end();
  1239. }
  1240. for (int i = 0; i < p_dir->files.size(); i++) {
  1241. if (updated_dir && !p_dir->files[i]->verified) {
  1242. //this file was removed, add action to remove it
  1243. ItemAction ia;
  1244. ia.action = ItemAction::ACTION_FILE_REMOVE;
  1245. ia.dir = p_dir;
  1246. ia.file = p_dir->files[i]->file;
  1247. scan_actions.push_back(ia);
  1248. diff_nb_files--;
  1249. continue;
  1250. }
  1251. String path = cd.path_join(p_dir->files[i]->file);
  1252. if (import_extensions.has(p_dir->files[i]->file.get_extension().to_lower())) {
  1253. // Check here if file must be imported or not.
  1254. // Same logic as in _process_file_system, the last modifications dates
  1255. // needs to be trusted to prevent reading all the .import files and the md5
  1256. // each time the user switch back to Godot.
  1257. uint64_t mt = FileAccess::get_modified_time(path);
  1258. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  1259. if (_is_test_for_reimport_needed(path, p_dir->files[i]->modified_time, mt, p_dir->files[i]->import_modified_time, import_mt, p_dir->files[i]->import_dest_paths)) {
  1260. ItemAction ia;
  1261. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1262. ia.dir = p_dir;
  1263. ia.file = p_dir->files[i]->file;
  1264. scan_actions.push_back(ia);
  1265. }
  1266. } else {
  1267. uint64_t mt = FileAccess::get_modified_time(path);
  1268. if (mt != p_dir->files[i]->modified_time) {
  1269. p_dir->files[i]->modified_time = mt; //save new time, but test for reload
  1270. ItemAction ia;
  1271. ia.action = ItemAction::ACTION_FILE_RELOAD;
  1272. ia.dir = p_dir;
  1273. ia.file = p_dir->files[i]->file;
  1274. scan_actions.push_back(ia);
  1275. }
  1276. }
  1277. p_progress.increment();
  1278. }
  1279. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1280. if ((updated_dir && !p_dir->subdirs[i]->verified) || _should_skip_directory(p_dir->subdirs[i]->get_path())) {
  1281. // Add all the files of the folder to be sure _update_scan_actions process the removed files
  1282. // for global class names.
  1283. diff_nb_files += _insert_actions_delete_files_directory(p_dir->subdirs[i]);
  1284. //this directory was removed or ignored, add action to remove it
  1285. ItemAction ia;
  1286. ia.action = ItemAction::ACTION_DIR_REMOVE;
  1287. ia.dir = p_dir->subdirs[i];
  1288. scan_actions.push_back(ia);
  1289. continue;
  1290. }
  1291. if (p_recursive) {
  1292. _scan_fs_changes(p_dir->get_subdir(i), p_progress);
  1293. }
  1294. }
  1295. nb_files_total = MAX(nb_files_total + diff_nb_files, 0);
  1296. }
  1297. void EditorFileSystem::_delete_internal_files(const String &p_file) {
  1298. if (FileAccess::exists(p_file + ".import")) {
  1299. List<String> paths;
  1300. ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
  1301. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1302. for (const String &E : paths) {
  1303. da->remove(E);
  1304. }
  1305. da->remove(p_file + ".import");
  1306. }
  1307. if (FileAccess::exists(p_file + ".uid")) {
  1308. DirAccess::remove_absolute(p_file + ".uid");
  1309. }
  1310. }
  1311. int EditorFileSystem::_insert_actions_delete_files_directory(EditorFileSystemDirectory *p_dir) {
  1312. int nb_files = 0;
  1313. for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
  1314. ItemAction ia;
  1315. ia.action = ItemAction::ACTION_FILE_REMOVE;
  1316. ia.dir = p_dir;
  1317. ia.file = fi->file;
  1318. scan_actions.push_back(ia);
  1319. nb_files++;
  1320. }
  1321. for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
  1322. nb_files += _insert_actions_delete_files_directory(sub_dir);
  1323. }
  1324. return nb_files;
  1325. }
  1326. void EditorFileSystem::_thread_func_sources(void *_userdata) {
  1327. EditorFileSystem *efs = (EditorFileSystem *)_userdata;
  1328. if (efs->filesystem) {
  1329. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  1330. ScanProgress sp;
  1331. sp.progress = &pr;
  1332. sp.hi = efs->nb_files_total;
  1333. efs->_scan_fs_changes(efs->filesystem, sp);
  1334. }
  1335. efs->scanning_changes_done.set();
  1336. }
  1337. void EditorFileSystem::_remove_invalid_global_class_names(const HashSet<String> &p_existing_class_names) {
  1338. List<StringName> global_classes;
  1339. bool must_save = false;
  1340. ScriptServer::get_global_class_list(&global_classes);
  1341. for (const StringName &class_name : global_classes) {
  1342. if (!p_existing_class_names.has(class_name)) {
  1343. ScriptServer::remove_global_class(class_name);
  1344. must_save = true;
  1345. }
  1346. }
  1347. if (must_save) {
  1348. ScriptServer::save_global_classes();
  1349. }
  1350. }
  1351. String EditorFileSystem::_get_file_by_class_name(EditorFileSystemDirectory *p_dir, const String &p_class_name, EditorFileSystemDirectory::FileInfo *&r_file_info) {
  1352. for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
  1353. if (fi->script_class_name == p_class_name) {
  1354. r_file_info = fi;
  1355. return p_dir->get_path().path_join(fi->file);
  1356. }
  1357. }
  1358. for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
  1359. String file = _get_file_by_class_name(sub_dir, p_class_name, r_file_info);
  1360. if (!file.is_empty()) {
  1361. return file;
  1362. }
  1363. }
  1364. r_file_info = nullptr;
  1365. return "";
  1366. }
  1367. void EditorFileSystem::scan_changes() {
  1368. if (first_scan || // Prevent a premature changes scan from inhibiting the first full scan
  1369. scanning || scanning_changes || thread.is_started()) {
  1370. scan_changes_pending = true;
  1371. set_process(true);
  1372. return;
  1373. }
  1374. _update_extensions();
  1375. sources_changed.clear();
  1376. scanning_changes = true;
  1377. scanning_changes_done.clear();
  1378. if (!use_threads) {
  1379. if (filesystem) {
  1380. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  1381. ScanProgress sp;
  1382. sp.progress = &pr;
  1383. sp.hi = nb_files_total;
  1384. scan_total = 0;
  1385. _scan_fs_changes(filesystem, sp);
  1386. if (_update_scan_actions()) {
  1387. emit_signal(SNAME("filesystem_changed"));
  1388. }
  1389. }
  1390. scanning_changes = false;
  1391. scanning_changes_done.set();
  1392. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1393. } else {
  1394. ERR_FAIL_COND(thread_sources.is_started());
  1395. set_process(true);
  1396. scan_total = 0;
  1397. Thread::Settings s;
  1398. s.priority = Thread::PRIORITY_LOW;
  1399. thread_sources.start(_thread_func_sources, this, s);
  1400. }
  1401. }
  1402. void EditorFileSystem::_notification(int p_what) {
  1403. switch (p_what) {
  1404. case NOTIFICATION_EXIT_TREE: {
  1405. Thread &active_thread = thread.is_started() ? thread : thread_sources;
  1406. if (use_threads && active_thread.is_started()) {
  1407. while (scanning) {
  1408. OS::get_singleton()->delay_usec(1000);
  1409. }
  1410. active_thread.wait_to_finish();
  1411. WARN_PRINT("Scan thread aborted...");
  1412. set_process(false);
  1413. }
  1414. if (filesystem) {
  1415. memdelete(filesystem);
  1416. }
  1417. if (new_filesystem) {
  1418. memdelete(new_filesystem);
  1419. }
  1420. filesystem = nullptr;
  1421. new_filesystem = nullptr;
  1422. } break;
  1423. case NOTIFICATION_PROCESS: {
  1424. if (use_threads) {
  1425. /** This hack exists because of the EditorProgress nature
  1426. * of processing events recursively. This needs to be rewritten
  1427. * at some point entirely, but in the meantime the following
  1428. * hack prevents deadlock on import.
  1429. */
  1430. static bool prevent_recursive_process_hack = false;
  1431. if (prevent_recursive_process_hack) {
  1432. break;
  1433. }
  1434. prevent_recursive_process_hack = true;
  1435. bool done_importing = false;
  1436. if (scanning_changes) {
  1437. if (scanning_changes_done.is_set()) {
  1438. set_process(false);
  1439. if (thread_sources.is_started()) {
  1440. thread_sources.wait_to_finish();
  1441. }
  1442. bool changed = _update_scan_actions();
  1443. // Set first_scan to false before the signals so the function doing_first_scan can return false
  1444. // in editor_node to start the export if needed.
  1445. first_scan = false;
  1446. ResourceImporter::load_on_startup = nullptr;
  1447. if (changed) {
  1448. emit_signal(SNAME("filesystem_changed"));
  1449. }
  1450. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1451. scanning_changes = false; // Changed to false here to prevent recursive triggering of scan thread.
  1452. done_importing = true;
  1453. }
  1454. } else if (!scanning && thread.is_started()) {
  1455. set_process(false);
  1456. if (filesystem) {
  1457. memdelete(filesystem);
  1458. }
  1459. filesystem = new_filesystem;
  1460. new_filesystem = nullptr;
  1461. thread.wait_to_finish();
  1462. _update_scan_actions();
  1463. // Update all icons so they are loaded for the FileSystemDock.
  1464. _update_files_icon_path();
  1465. // Set first_scan to false before the signals so the function doing_first_scan can return false
  1466. // in editor_node to start the export if needed.
  1467. first_scan = false;
  1468. ResourceImporter::load_on_startup = nullptr;
  1469. emit_signal(SNAME("filesystem_changed"));
  1470. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1471. }
  1472. if (done_importing && scan_changes_pending) {
  1473. scan_changes_pending = false;
  1474. scan_changes();
  1475. }
  1476. prevent_recursive_process_hack = false;
  1477. }
  1478. } break;
  1479. }
  1480. }
  1481. bool EditorFileSystem::is_scanning() const {
  1482. return scanning || scanning_changes || first_scan;
  1483. }
  1484. float EditorFileSystem::get_scanning_progress() const {
  1485. return scan_total;
  1486. }
  1487. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  1488. return filesystem;
  1489. }
  1490. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file) {
  1491. if (!p_dir) {
  1492. return; //none
  1493. }
  1494. p_file->store_line("::" + p_dir->get_path() + "::" + String::num_int64(p_dir->modified_time));
  1495. for (int i = 0; i < p_dir->files.size(); i++) {
  1496. const EditorFileSystemDirectory::FileInfo *file_info = p_dir->files[i];
  1497. if (!file_info->import_group_file.is_empty()) {
  1498. group_file_cache.insert(file_info->import_group_file);
  1499. }
  1500. String type = file_info->type;
  1501. if (file_info->resource_script_class) {
  1502. type += "/" + String(file_info->resource_script_class);
  1503. }
  1504. PackedStringArray cache_string;
  1505. cache_string.append(file_info->file);
  1506. cache_string.append(type);
  1507. cache_string.append(itos(file_info->uid));
  1508. cache_string.append(itos(file_info->modified_time));
  1509. cache_string.append(itos(file_info->import_modified_time));
  1510. cache_string.append(itos(file_info->import_valid));
  1511. cache_string.append(file_info->import_group_file);
  1512. cache_string.append(String("<>").join({ file_info->script_class_name, file_info->script_class_extends, file_info->script_class_icon_path, file_info->import_md5, String("<*>").join(file_info->import_dest_paths) }));
  1513. cache_string.append(String("<>").join(file_info->deps));
  1514. p_file->store_line(String("::").join(cache_string));
  1515. }
  1516. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1517. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  1518. }
  1519. }
  1520. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  1521. //todo make faster
  1522. if (!filesystem || scanning) {
  1523. return false;
  1524. }
  1525. String f = ProjectSettings::get_singleton()->localize_path(p_file);
  1526. // Note: Only checks if base directory is case sensitive.
  1527. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1528. bool fs_case_sensitive = dir->is_case_sensitive("res://");
  1529. if (!f.begins_with("res://")) {
  1530. return false;
  1531. }
  1532. f = f.substr(6, f.length());
  1533. f = f.replace("\\", "/");
  1534. Vector<String> path = f.split("/");
  1535. if (path.size() == 0) {
  1536. return false;
  1537. }
  1538. String file = path[path.size() - 1];
  1539. path.resize(path.size() - 1);
  1540. EditorFileSystemDirectory *fs = filesystem;
  1541. for (int i = 0; i < path.size(); i++) {
  1542. if (path[i].begins_with(".")) {
  1543. return false;
  1544. }
  1545. int idx = -1;
  1546. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1547. if (fs_case_sensitive) {
  1548. if (fs->get_subdir(j)->get_name() == path[i]) {
  1549. idx = j;
  1550. break;
  1551. }
  1552. } else {
  1553. if (fs->get_subdir(j)->get_name().to_lower() == path[i].to_lower()) {
  1554. idx = j;
  1555. break;
  1556. }
  1557. }
  1558. }
  1559. if (idx == -1) {
  1560. // Only create a missing directory in memory when it exists on disk.
  1561. if (!dir->dir_exists(fs->get_path().path_join(path[i]))) {
  1562. return false;
  1563. }
  1564. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  1565. efsd->name = path[i];
  1566. efsd->parent = fs;
  1567. int idx2 = 0;
  1568. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1569. if (efsd->name.filenocasecmp_to(fs->get_subdir(j)->get_name()) < 0) {
  1570. break;
  1571. }
  1572. idx2++;
  1573. }
  1574. if (idx2 == fs->get_subdir_count()) {
  1575. fs->subdirs.push_back(efsd);
  1576. } else {
  1577. fs->subdirs.insert(idx2, efsd);
  1578. }
  1579. fs = efsd;
  1580. } else {
  1581. fs = fs->get_subdir(idx);
  1582. }
  1583. }
  1584. int cpos = -1;
  1585. for (int i = 0; i < fs->files.size(); i++) {
  1586. if (fs->files[i]->file == file) {
  1587. cpos = i;
  1588. break;
  1589. }
  1590. }
  1591. r_file_pos = cpos;
  1592. *r_d = fs;
  1593. return cpos != -1;
  1594. }
  1595. String EditorFileSystem::get_file_type(const String &p_file) const {
  1596. EditorFileSystemDirectory *fs = nullptr;
  1597. int cpos = -1;
  1598. if (!_find_file(p_file, &fs, cpos)) {
  1599. return "";
  1600. }
  1601. return fs->files[cpos]->type;
  1602. }
  1603. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  1604. if (!filesystem || scanning) {
  1605. return nullptr;
  1606. }
  1607. EditorFileSystemDirectory *fs = nullptr;
  1608. int cpos = -1;
  1609. if (!_find_file(p_file, &fs, cpos)) {
  1610. return nullptr;
  1611. }
  1612. if (r_index) {
  1613. *r_index = cpos;
  1614. }
  1615. return fs;
  1616. }
  1617. EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
  1618. if (!filesystem || scanning) {
  1619. return nullptr;
  1620. }
  1621. String f = ProjectSettings::get_singleton()->localize_path(p_path);
  1622. if (!f.begins_with("res://")) {
  1623. return nullptr;
  1624. }
  1625. f = f.substr(6, f.length());
  1626. f = f.replace("\\", "/");
  1627. if (f.is_empty()) {
  1628. return filesystem;
  1629. }
  1630. if (f.ends_with("/")) {
  1631. f = f.substr(0, f.length() - 1);
  1632. }
  1633. Vector<String> path = f.split("/");
  1634. if (path.size() == 0) {
  1635. return nullptr;
  1636. }
  1637. EditorFileSystemDirectory *fs = filesystem;
  1638. for (int i = 0; i < path.size(); i++) {
  1639. int idx = -1;
  1640. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1641. if (fs->get_subdir(j)->get_name() == path[i]) {
  1642. idx = j;
  1643. break;
  1644. }
  1645. }
  1646. if (idx == -1) {
  1647. return nullptr;
  1648. } else {
  1649. fs = fs->get_subdir(idx);
  1650. }
  1651. }
  1652. return fs;
  1653. }
  1654. void EditorFileSystem::_save_late_updated_files() {
  1655. //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
  1656. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  1657. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  1658. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  1659. for (const String &E : late_update_files) {
  1660. f->store_line(E);
  1661. }
  1662. }
  1663. Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
  1664. // Avoid error spam on first opening of a not yet imported project by treating the following situation
  1665. // as a benign one, not letting the file open error happen: the resource is of an importable type but
  1666. // it has not been imported yet.
  1667. if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
  1668. const String &internal_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
  1669. if (!internal_path.is_empty() && !FileAccess::exists(internal_path)) { // If path is empty (error), keep the code flow to the error.
  1670. return Vector<String>();
  1671. }
  1672. }
  1673. List<String> deps;
  1674. ResourceLoader::get_dependencies(p_path, &deps);
  1675. Vector<String> ret;
  1676. for (const String &E : deps) {
  1677. ret.push_back(E);
  1678. }
  1679. return ret;
  1680. }
  1681. String EditorFileSystem::_get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const {
  1682. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1683. if (ScriptServer::get_language(i)->handles_global_class_type(p_type)) {
  1684. String global_name;
  1685. String extends;
  1686. String icon_path;
  1687. global_name = ScriptServer::get_language(i)->get_global_class_name(p_path, &extends, &icon_path);
  1688. *r_extends = extends;
  1689. *r_icon_path = icon_path;
  1690. return global_name;
  1691. }
  1692. }
  1693. *r_extends = String();
  1694. *r_icon_path = String();
  1695. return String();
  1696. }
  1697. void EditorFileSystem::_update_file_icon_path(EditorFileSystemDirectory::FileInfo *file_info) {
  1698. String icon_path;
  1699. if (file_info->resource_script_class != StringName()) {
  1700. icon_path = EditorNode::get_editor_data().script_class_get_icon_path(file_info->resource_script_class);
  1701. } else if (file_info->script_class_icon_path.is_empty() && !file_info->deps.is_empty()) {
  1702. const String &script_dep = file_info->deps[0]; // Assuming the first dependency is a script.
  1703. const String &script_path = script_dep.contains("::") ? script_dep.get_slice("::", 2) : script_dep;
  1704. if (!script_path.is_empty()) {
  1705. String *cached = file_icon_cache.getptr(script_path);
  1706. if (cached) {
  1707. icon_path = *cached;
  1708. } else {
  1709. if (ClassDB::is_parent_class(ResourceLoader::get_resource_type(script_path), SNAME("Script"))) {
  1710. int script_file;
  1711. EditorFileSystemDirectory *efsd = find_file(script_path, &script_file);
  1712. if (efsd) {
  1713. icon_path = efsd->files[script_file]->script_class_icon_path;
  1714. }
  1715. }
  1716. file_icon_cache.insert(script_path, icon_path);
  1717. }
  1718. }
  1719. }
  1720. if (icon_path.is_empty() && !file_info->type.is_empty()) {
  1721. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(file_info->type);
  1722. if (icon.is_valid()) {
  1723. icon_path = icon->get_path();
  1724. }
  1725. }
  1726. file_info->icon_path = icon_path;
  1727. }
  1728. void EditorFileSystem::_update_files_icon_path(EditorFileSystemDirectory *edp) {
  1729. if (!edp) {
  1730. edp = filesystem;
  1731. file_icon_cache.clear();
  1732. }
  1733. for (EditorFileSystemDirectory *sub_dir : edp->subdirs) {
  1734. _update_files_icon_path(sub_dir);
  1735. }
  1736. for (EditorFileSystemDirectory::FileInfo *fi : edp->files) {
  1737. _update_file_icon_path(fi);
  1738. }
  1739. }
  1740. void EditorFileSystem::_update_script_classes() {
  1741. if (update_script_paths.is_empty()) {
  1742. // Ensure the global class file is always present; it's essential for exports to work.
  1743. if (!FileAccess::exists(ProjectSettings::get_singleton()->get_global_class_list_path())) {
  1744. ScriptServer::save_global_classes();
  1745. }
  1746. return;
  1747. }
  1748. {
  1749. MutexLock update_script_lock(update_script_mutex);
  1750. EditorProgress *ep = nullptr;
  1751. if (update_script_paths.size() > 1) {
  1752. if (MessageQueue::get_singleton()->is_flushing()) {
  1753. // Use background progress when message queue is flushing.
  1754. ep = memnew(EditorProgress("update_scripts_classes", TTR("Registering global classes..."), update_script_paths.size(), false, true));
  1755. } else {
  1756. ep = memnew(EditorProgress("update_scripts_classes", TTR("Registering global classes..."), update_script_paths.size()));
  1757. }
  1758. }
  1759. int step_count = 0;
  1760. for (const KeyValue<String, ScriptInfo> &E : update_script_paths) {
  1761. _register_global_class_script(E.key, E.key, E.value.type, E.value.script_class_name, E.value.script_class_extends, E.value.script_class_icon_path);
  1762. if (ep) {
  1763. ep->step(E.value.script_class_name, step_count++, false);
  1764. }
  1765. }
  1766. memdelete_notnull(ep);
  1767. update_script_paths.clear();
  1768. }
  1769. ScriptServer::save_global_classes();
  1770. EditorNode::get_editor_data().script_class_save_icon_paths();
  1771. emit_signal("script_classes_updated");
  1772. // Rescan custom loaders and savers.
  1773. // Doing the following here because the `filesystem_changed` signal fires multiple times and isn't always followed by script classes update.
  1774. // So I thought it's better to do this when script classes really get updated
  1775. ResourceLoader::remove_custom_loaders();
  1776. ResourceLoader::add_custom_loaders();
  1777. ResourceSaver::remove_custom_savers();
  1778. ResourceSaver::add_custom_savers();
  1779. }
  1780. void EditorFileSystem::_update_script_documentation() {
  1781. if (update_script_paths_documentation.is_empty()) {
  1782. return;
  1783. }
  1784. MutexLock update_script_lock(update_script_mutex);
  1785. EditorProgress *ep = nullptr;
  1786. if (update_script_paths_documentation.size() > 1) {
  1787. if (MessageQueue::get_singleton()->is_flushing()) {
  1788. // Use background progress when message queue is flushing.
  1789. ep = memnew(EditorProgress("update_script_paths_documentation", TTR("Updating scripts documentation"), update_script_paths_documentation.size(), false, true));
  1790. } else {
  1791. ep = memnew(EditorProgress("update_script_paths_documentation", TTR("Updating scripts documentation"), update_script_paths_documentation.size()));
  1792. }
  1793. }
  1794. int step_count = 0;
  1795. for (const String &path : update_script_paths_documentation) {
  1796. int index = -1;
  1797. EditorFileSystemDirectory *efd = find_file(path, &index);
  1798. if (!efd || index < 0) {
  1799. // The file was removed
  1800. continue;
  1801. }
  1802. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1803. ScriptLanguage *lang = ScriptServer::get_language(i);
  1804. if (lang->supports_documentation() && efd->files[index]->type == lang->get_type()) {
  1805. bool should_reload_script = _should_reload_script(path);
  1806. Ref<Script> scr = ResourceLoader::load(path);
  1807. if (scr.is_null()) {
  1808. continue;
  1809. }
  1810. if (should_reload_script) {
  1811. // Reloading the script from disk. Otherwise, the ResourceLoader::load will
  1812. // return the last loaded version of the script (without the modifications).
  1813. scr->reload_from_file();
  1814. }
  1815. for (const DocData::ClassDoc &cd : scr->get_documentation()) {
  1816. EditorHelp::get_doc_data()->add_doc(cd);
  1817. if (!first_scan) {
  1818. // Update the documentation in the Script Editor if it is open.
  1819. ScriptEditor::get_singleton()->update_doc(cd.name);
  1820. }
  1821. }
  1822. }
  1823. }
  1824. if (ep) {
  1825. ep->step(efd->files[index]->file, step_count++, false);
  1826. }
  1827. }
  1828. memdelete_notnull(ep);
  1829. update_script_paths_documentation.clear();
  1830. }
  1831. bool EditorFileSystem::_should_reload_script(const String &p_path) {
  1832. if (first_scan) {
  1833. return false;
  1834. }
  1835. Ref<Script> scr = ResourceCache::get_ref(p_path);
  1836. if (scr.is_null()) {
  1837. // Not a script or not already loaded.
  1838. return false;
  1839. }
  1840. // Scripts are reloaded via the script editor if they are currently opened.
  1841. if (ScriptEditor::get_singleton()->get_open_scripts().has(scr)) {
  1842. return false;
  1843. }
  1844. return true;
  1845. }
  1846. void EditorFileSystem::_process_update_pending() {
  1847. _update_script_classes();
  1848. // Parse documentation second, as it requires the class names to be loaded
  1849. // because _update_script_documentation loads the scripts completely.
  1850. _update_script_documentation();
  1851. _update_pending_scene_groups();
  1852. }
  1853. void EditorFileSystem::_queue_update_script_class(const String &p_path, const String &p_type, const String &p_script_class_name, const String &p_script_class_extends, const String &p_script_class_icon_path) {
  1854. MutexLock update_script_lock(update_script_mutex);
  1855. ScriptInfo si;
  1856. si.type = p_type;
  1857. si.script_class_name = p_script_class_name;
  1858. si.script_class_extends = p_script_class_extends;
  1859. si.script_class_icon_path = p_script_class_icon_path;
  1860. update_script_paths.insert(p_path, si);
  1861. update_script_paths_documentation.insert(p_path);
  1862. }
  1863. void EditorFileSystem::_update_scene_groups() {
  1864. if (update_scene_paths.is_empty()) {
  1865. return;
  1866. }
  1867. EditorProgress *ep = nullptr;
  1868. if (update_scene_paths.size() > 20) {
  1869. ep = memnew(EditorProgress("update_scene_groups", TTR("Updating Scene Groups"), update_scene_paths.size()));
  1870. }
  1871. int step_count = 0;
  1872. {
  1873. MutexLock update_scene_lock(update_scene_mutex);
  1874. for (const String &path : update_scene_paths) {
  1875. ProjectSettings::get_singleton()->remove_scene_groups_cache(path);
  1876. int index = -1;
  1877. EditorFileSystemDirectory *efd = find_file(path, &index);
  1878. if (!efd || index < 0) {
  1879. // The file was removed.
  1880. continue;
  1881. }
  1882. const HashSet<StringName> scene_groups = PackedScene::get_scene_groups(path);
  1883. if (!scene_groups.is_empty()) {
  1884. ProjectSettings::get_singleton()->add_scene_groups_cache(path, scene_groups);
  1885. }
  1886. if (ep) {
  1887. ep->step(efd->files[index]->file, step_count++, false);
  1888. }
  1889. }
  1890. memdelete_notnull(ep);
  1891. update_scene_paths.clear();
  1892. }
  1893. ProjectSettings::get_singleton()->save_scene_groups_cache();
  1894. }
  1895. void EditorFileSystem::_update_pending_scene_groups() {
  1896. if (!FileAccess::exists(ProjectSettings::get_singleton()->get_scene_groups_cache_path())) {
  1897. _get_all_scenes(get_filesystem(), update_scene_paths);
  1898. _update_scene_groups();
  1899. } else if (!update_scene_paths.is_empty()) {
  1900. _update_scene_groups();
  1901. }
  1902. }
  1903. void EditorFileSystem::_queue_update_scene_groups(const String &p_path) {
  1904. MutexLock update_scene_lock(update_scene_mutex);
  1905. update_scene_paths.insert(p_path);
  1906. }
  1907. void EditorFileSystem::_get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet<String> &r_list) {
  1908. for (int i = 0; i < p_dir->get_file_count(); i++) {
  1909. if (p_dir->get_file_type(i) == SNAME("PackedScene")) {
  1910. r_list.insert(p_dir->get_file_path(i));
  1911. }
  1912. }
  1913. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1914. _get_all_scenes(p_dir->get_subdir(i), r_list);
  1915. }
  1916. }
  1917. void EditorFileSystem::update_file(const String &p_file) {
  1918. ERR_FAIL_COND(p_file.is_empty());
  1919. update_files({ p_file });
  1920. }
  1921. void EditorFileSystem::update_files(const Vector<String> &p_script_paths) {
  1922. bool updated = false;
  1923. bool update_files_icon_cache = false;
  1924. Vector<EditorFileSystemDirectory::FileInfo *> files_to_update_icon_path;
  1925. for (const String &file : p_script_paths) {
  1926. ERR_CONTINUE(file.is_empty());
  1927. EditorFileSystemDirectory *fs = nullptr;
  1928. int cpos = -1;
  1929. if (!_find_file(file, &fs, cpos)) {
  1930. if (!fs) {
  1931. continue;
  1932. }
  1933. }
  1934. if (!FileAccess::exists(file)) {
  1935. //was removed
  1936. _delete_internal_files(file);
  1937. if (cpos != -1) { // Might've never been part of the editor file system (*.* files deleted in Open dialog).
  1938. if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  1939. if (ResourceUID::get_singleton()->has_id(fs->files[cpos]->uid)) {
  1940. ResourceUID::get_singleton()->remove_id(fs->files[cpos]->uid);
  1941. }
  1942. }
  1943. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
  1944. _queue_update_script_class(file, fs->files[cpos]->type, "", "", "");
  1945. if (!fs->files[cpos]->script_class_icon_path.is_empty()) {
  1946. update_files_icon_cache = true;
  1947. }
  1948. }
  1949. if (fs->files[cpos]->type == SNAME("PackedScene")) {
  1950. _queue_update_scene_groups(file);
  1951. }
  1952. memdelete(fs->files[cpos]);
  1953. fs->files.remove_at(cpos);
  1954. updated = true;
  1955. }
  1956. } else {
  1957. String type = ResourceLoader::get_resource_type(file);
  1958. if (type.is_empty() && textfile_extensions.has(file.get_extension())) {
  1959. type = "TextFile";
  1960. }
  1961. if (type.is_empty() && other_file_extensions.has(file.get_extension())) {
  1962. type = "OtherFile";
  1963. }
  1964. String script_class = ResourceLoader::get_resource_script_class(file);
  1965. ResourceUID::ID uid = ResourceLoader::get_resource_uid(file);
  1966. if (cpos == -1) {
  1967. // The file did not exist, it was added.
  1968. int idx = 0;
  1969. String file_name = file.get_file();
  1970. for (int i = 0; i < fs->files.size(); i++) {
  1971. if (file.filenocasecmp_to(fs->files[i]->file) < 0) {
  1972. break;
  1973. }
  1974. idx++;
  1975. }
  1976. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1977. fi->file = file_name;
  1978. fi->import_modified_time = 0;
  1979. fi->import_valid = (type == "TextFile" || type == "OtherFile") ? true : ResourceLoader::is_import_valid(file);
  1980. fi->import_md5 = "";
  1981. fi->import_dest_paths = Vector<String>();
  1982. if (idx == fs->files.size()) {
  1983. fs->files.push_back(fi);
  1984. } else {
  1985. fs->files.insert(idx, fi);
  1986. }
  1987. cpos = idx;
  1988. } else {
  1989. //the file exists and it was updated, and was not added in this step.
  1990. //this means we must force upon next restart to scan it again, to get proper type and dependencies
  1991. late_update_files.insert(file);
  1992. _save_late_updated_files(); //files need to be updated in the re-scan
  1993. }
  1994. const String old_script_class_icon_path = fs->files[cpos]->script_class_icon_path;
  1995. const String old_class_name = fs->files[cpos]->script_class_name;
  1996. fs->files[cpos]->type = type;
  1997. fs->files[cpos]->resource_script_class = script_class;
  1998. fs->files[cpos]->uid = uid;
  1999. fs->files[cpos]->script_class_name = _get_global_script_class(type, file, &fs->files[cpos]->script_class_extends, &fs->files[cpos]->script_class_icon_path);
  2000. fs->files[cpos]->import_group_file = ResourceLoader::get_import_group_file(file);
  2001. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  2002. fs->files[cpos]->deps = _get_dependencies(file);
  2003. fs->files[cpos]->import_valid = (type == "TextFile" || type == "OtherFile") ? true : ResourceLoader::is_import_valid(file);
  2004. if (uid != ResourceUID::INVALID_ID) {
  2005. if (ResourceUID::get_singleton()->has_id(uid)) {
  2006. ResourceUID::get_singleton()->set_id(uid, file);
  2007. } else {
  2008. ResourceUID::get_singleton()->add_id(uid, file);
  2009. }
  2010. ResourceUID::get_singleton()->update_cache();
  2011. }
  2012. // Update preview
  2013. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  2014. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
  2015. _queue_update_script_class(file, fs->files[cpos]->type, fs->files[cpos]->script_class_name, fs->files[cpos]->script_class_extends, fs->files[cpos]->script_class_icon_path);
  2016. }
  2017. if (fs->files[cpos]->type == SNAME("PackedScene")) {
  2018. _queue_update_scene_groups(file);
  2019. }
  2020. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Resource"))) {
  2021. files_to_update_icon_path.push_back(fs->files[cpos]);
  2022. } else if (old_script_class_icon_path != fs->files[cpos]->script_class_icon_path) {
  2023. update_files_icon_cache = true;
  2024. }
  2025. // Restore another script as the global class name if multiple scripts had the same old class name.
  2026. if (!old_class_name.is_empty() && fs->files[cpos]->script_class_name != old_class_name && ClassDB::is_parent_class(type, SNAME("Script"))) {
  2027. EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
  2028. String old_file = _get_file_by_class_name(filesystem, old_class_name, old_fi);
  2029. if (!old_file.is_empty() && old_fi) {
  2030. _queue_update_script_class(old_file, old_fi->type, old_fi->script_class_name, old_fi->script_class_extends, old_fi->script_class_icon_path);
  2031. }
  2032. }
  2033. updated = true;
  2034. }
  2035. }
  2036. if (updated) {
  2037. if (update_files_icon_cache) {
  2038. _update_files_icon_path();
  2039. } else {
  2040. for (EditorFileSystemDirectory::FileInfo *fi : files_to_update_icon_path) {
  2041. _update_file_icon_path(fi);
  2042. }
  2043. }
  2044. if (!is_scanning()) {
  2045. _process_update_pending();
  2046. }
  2047. if (!filesystem_changed_queued) {
  2048. filesystem_changed_queued = true;
  2049. callable_mp(this, &EditorFileSystem::_notify_filesystem_changed).call_deferred();
  2050. }
  2051. }
  2052. }
  2053. void EditorFileSystem::_notify_filesystem_changed() {
  2054. emit_signal("filesystem_changed");
  2055. filesystem_changed_queued = false;
  2056. }
  2057. HashSet<String> EditorFileSystem::get_valid_extensions() const {
  2058. return valid_extensions;
  2059. }
  2060. void EditorFileSystem::_register_global_class_script(const String &p_search_path, const String &p_target_path, const String &p_type, const String &p_script_class_name, const String &p_script_class_extends, const String &p_script_class_icon_path) {
  2061. ScriptServer::remove_global_class_by_path(p_search_path); // First remove, just in case it changed
  2062. if (p_script_class_name.is_empty()) {
  2063. return;
  2064. }
  2065. String lang;
  2066. for (int j = 0; j < ScriptServer::get_language_count(); j++) {
  2067. if (ScriptServer::get_language(j)->handles_global_class_type(p_type)) {
  2068. lang = ScriptServer::get_language(j)->get_name();
  2069. break;
  2070. }
  2071. }
  2072. if (lang.is_empty()) {
  2073. return; // No lang found that can handle this global class
  2074. }
  2075. ScriptServer::add_global_class(p_script_class_name, p_script_class_extends, lang, p_target_path);
  2076. EditorNode::get_editor_data().script_class_set_icon_path(p_script_class_name, p_script_class_icon_path);
  2077. EditorNode::get_editor_data().script_class_set_name(p_target_path, p_script_class_name);
  2078. }
  2079. void EditorFileSystem::register_global_class_script(const String &p_search_path, const String &p_target_path) {
  2080. int index_file;
  2081. EditorFileSystemDirectory *efsd = find_file(p_search_path, &index_file);
  2082. if (efsd) {
  2083. const EditorFileSystemDirectory::FileInfo *fi = efsd->files[index_file];
  2084. EditorFileSystem::get_singleton()->_register_global_class_script(p_search_path, p_target_path, fi->type, fi->script_class_name, fi->script_class_extends, fi->script_class_icon_path);
  2085. } else {
  2086. ScriptServer::remove_global_class_by_path(p_search_path);
  2087. }
  2088. }
  2089. Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) {
  2090. String importer_name;
  2091. HashMap<String, HashMap<StringName, Variant>> source_file_options;
  2092. HashMap<String, ResourceUID::ID> uids;
  2093. HashMap<String, String> base_paths;
  2094. for (int i = 0; i < p_files.size(); i++) {
  2095. Ref<ConfigFile> config;
  2096. config.instantiate();
  2097. Error err = config->load(p_files[i] + ".import");
  2098. ERR_CONTINUE(err != OK);
  2099. ERR_CONTINUE(!config->has_section_key("remap", "importer"));
  2100. String file_importer_name = config->get_value("remap", "importer");
  2101. ERR_CONTINUE(file_importer_name.is_empty());
  2102. if (!importer_name.is_empty() && importer_name != file_importer_name) {
  2103. EditorNode::get_singleton()->show_warning(vformat(TTR("There are multiple importers for different types pointing to file %s, import aborted"), p_group_file));
  2104. ERR_FAIL_V(ERR_FILE_CORRUPT);
  2105. }
  2106. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  2107. if (config->has_section_key("remap", "uid")) {
  2108. String uidt = config->get_value("remap", "uid");
  2109. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  2110. }
  2111. uids[p_files[i]] = uid;
  2112. source_file_options[p_files[i]] = HashMap<StringName, Variant>();
  2113. importer_name = file_importer_name;
  2114. if (importer_name == "keep" || importer_name == "skip") {
  2115. continue; //do nothing
  2116. }
  2117. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2118. ERR_FAIL_COND_V(importer.is_null(), ERR_FILE_CORRUPT);
  2119. List<ResourceImporter::ImportOption> options;
  2120. importer->get_import_options(p_files[i], &options);
  2121. //set default values
  2122. for (const ResourceImporter::ImportOption &E : options) {
  2123. source_file_options[p_files[i]][E.option.name] = E.default_value;
  2124. }
  2125. if (config->has_section("params")) {
  2126. List<String> sk;
  2127. config->get_section_keys("params", &sk);
  2128. for (const String &param : sk) {
  2129. Variant value = config->get_value("params", param);
  2130. //override with whatever is in file
  2131. source_file_options[p_files[i]][param] = value;
  2132. }
  2133. }
  2134. base_paths[p_files[i]] = ResourceFormatImporter::get_singleton()->get_import_base_path(p_files[i]);
  2135. }
  2136. if (importer_name == "keep" || importer_name == "skip") {
  2137. return OK; // (do nothing)
  2138. }
  2139. ERR_FAIL_COND_V(importer_name.is_empty(), ERR_UNCONFIGURED);
  2140. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2141. Error err = importer->import_group_file(p_group_file, source_file_options, base_paths);
  2142. //all went well, overwrite config files with proper remaps and md5s
  2143. for (const KeyValue<String, HashMap<StringName, Variant>> &E : source_file_options) {
  2144. const String &file = E.key;
  2145. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(file);
  2146. Vector<String> dest_paths;
  2147. ResourceUID::ID uid = uids[file];
  2148. {
  2149. Ref<FileAccess> f = FileAccess::open(file + ".import", FileAccess::WRITE);
  2150. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'.");
  2151. //write manually, as order matters ([remap] has to go first for performance).
  2152. f->store_line("[remap]");
  2153. f->store_line("");
  2154. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  2155. int version = importer->get_format_version();
  2156. if (version > 0) {
  2157. f->store_line("importer_version=" + itos(version));
  2158. }
  2159. if (!importer->get_resource_type().is_empty()) {
  2160. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  2161. }
  2162. if (uid == ResourceUID::INVALID_ID) {
  2163. uid = ResourceUID::get_singleton()->create_id();
  2164. }
  2165. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
  2166. if (err == OK) {
  2167. String path = base_path + "." + importer->get_save_extension();
  2168. f->store_line("path=\"" + path + "\"");
  2169. dest_paths.push_back(path);
  2170. }
  2171. f->store_line("group_file=" + Variant(p_group_file).get_construct_string());
  2172. if (err == OK) {
  2173. f->store_line("valid=true");
  2174. } else {
  2175. f->store_line("valid=false");
  2176. }
  2177. f->store_line("[deps]\n");
  2178. f->store_line("");
  2179. f->store_line("source_file=" + Variant(file).get_construct_string());
  2180. if (dest_paths.size()) {
  2181. Array dp;
  2182. for (int i = 0; i < dest_paths.size(); i++) {
  2183. dp.push_back(dest_paths[i]);
  2184. }
  2185. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  2186. }
  2187. f->store_line("[params]");
  2188. f->store_line("");
  2189. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  2190. List<ResourceImporter::ImportOption> options;
  2191. importer->get_import_options(file, &options);
  2192. //set default values
  2193. for (const ResourceImporter::ImportOption &F : options) {
  2194. String base = F.option.name;
  2195. Variant v = F.default_value;
  2196. if (source_file_options[file].has(base)) {
  2197. v = source_file_options[file][base];
  2198. }
  2199. String value;
  2200. VariantWriter::write_to_string(v, value);
  2201. f->store_line(base + "=" + value);
  2202. }
  2203. }
  2204. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  2205. {
  2206. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  2207. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  2208. md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\"");
  2209. if (dest_paths.size()) {
  2210. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  2211. }
  2212. }
  2213. EditorFileSystemDirectory *fs = nullptr;
  2214. int cpos = -1;
  2215. bool found = _find_file(file, &fs, cpos);
  2216. ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, vformat("Can't find file '%s' during group reimport.", file));
  2217. //update modified times, to avoid reimport
  2218. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  2219. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(file + ".import");
  2220. fs->files[cpos]->import_md5 = FileAccess::get_md5(file + ".import");
  2221. fs->files[cpos]->import_dest_paths = dest_paths;
  2222. fs->files[cpos]->deps = _get_dependencies(file);
  2223. fs->files[cpos]->uid = uid;
  2224. fs->files[cpos]->type = importer->get_resource_type();
  2225. if (fs->files[cpos]->type == "" && textfile_extensions.has(file.get_extension())) {
  2226. fs->files[cpos]->type = "TextFile";
  2227. }
  2228. if (fs->files[cpos]->type == "" && other_file_extensions.has(file.get_extension())) {
  2229. fs->files[cpos]->type = "OtherFile";
  2230. }
  2231. fs->files[cpos]->import_valid = err == OK;
  2232. if (ResourceUID::get_singleton()->has_id(uid)) {
  2233. ResourceUID::get_singleton()->set_id(uid, file);
  2234. } else {
  2235. ResourceUID::get_singleton()->add_id(uid, file);
  2236. }
  2237. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  2238. //to reload properly
  2239. Ref<Resource> r = ResourceCache::get_ref(file);
  2240. if (r.is_valid()) {
  2241. if (!r->get_import_path().is_empty()) {
  2242. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(file);
  2243. r->set_import_path(dst_path);
  2244. r->set_import_last_modified_time(0);
  2245. }
  2246. }
  2247. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  2248. }
  2249. return err;
  2250. }
  2251. Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant *p_generator_parameters, bool p_update_file_system) {
  2252. print_verbose(vformat("EditorFileSystem: Importing file: %s", p_file));
  2253. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  2254. EditorFileSystemDirectory *fs = nullptr;
  2255. int cpos = -1;
  2256. if (p_update_file_system) {
  2257. bool found = _find_file(p_file, &fs, cpos);
  2258. ERR_FAIL_COND_V_MSG(!found, ERR_FILE_NOT_FOUND, vformat("Can't find file '%s' during file reimport.", p_file));
  2259. }
  2260. //try to obtain existing params
  2261. HashMap<StringName, Variant> params = p_custom_options;
  2262. String importer_name; //empty by default though
  2263. if (!p_custom_importer.is_empty()) {
  2264. importer_name = p_custom_importer;
  2265. }
  2266. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  2267. Variant generator_parameters;
  2268. if (p_generator_parameters) {
  2269. generator_parameters = *p_generator_parameters;
  2270. }
  2271. if (FileAccess::exists(p_file + ".import")) {
  2272. //use existing
  2273. Ref<ConfigFile> cf;
  2274. cf.instantiate();
  2275. Error err = cf->load(p_file + ".import");
  2276. if (err == OK) {
  2277. if (cf->has_section("params")) {
  2278. List<String> sk;
  2279. cf->get_section_keys("params", &sk);
  2280. for (const String &E : sk) {
  2281. if (!params.has(E)) {
  2282. params[E] = cf->get_value("params", E);
  2283. }
  2284. }
  2285. }
  2286. if (cf->has_section("remap")) {
  2287. if (p_custom_importer.is_empty()) {
  2288. importer_name = cf->get_value("remap", "importer");
  2289. }
  2290. if (cf->has_section_key("remap", "uid")) {
  2291. String uidt = cf->get_value("remap", "uid");
  2292. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  2293. }
  2294. if (!p_generator_parameters) {
  2295. if (cf->has_section_key("remap", "generator_parameters")) {
  2296. generator_parameters = cf->get_value("remap", "generator_parameters");
  2297. }
  2298. }
  2299. }
  2300. }
  2301. }
  2302. if (importer_name == "keep" || importer_name == "skip") {
  2303. //keep files, do nothing.
  2304. if (p_update_file_system) {
  2305. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  2306. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  2307. fs->files[cpos]->import_md5 = FileAccess::get_md5(p_file + ".import");
  2308. fs->files[cpos]->import_dest_paths = Vector<String>();
  2309. fs->files[cpos]->deps.clear();
  2310. fs->files[cpos]->type = "";
  2311. fs->files[cpos]->import_valid = false;
  2312. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  2313. }
  2314. return OK;
  2315. }
  2316. Ref<ResourceImporter> importer;
  2317. bool load_default = false;
  2318. //find the importer
  2319. if (!importer_name.is_empty()) {
  2320. importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2321. }
  2322. if (importer.is_null()) {
  2323. //not found by name, find by extension
  2324. importer = ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_file.get_extension());
  2325. load_default = true;
  2326. if (importer.is_null()) {
  2327. ERR_FAIL_V_MSG(ERR_FILE_CANT_OPEN, "BUG: File queued for import, but can't be imported, importer for type '" + importer_name + "' not found.");
  2328. }
  2329. }
  2330. if (FileAccess::exists(p_file + ".import")) {
  2331. // We only want to handle compat for existing files, not new ones.
  2332. importer->handle_compatibility_options(params);
  2333. }
  2334. //mix with default params, in case a parameter is missing
  2335. List<ResourceImporter::ImportOption> opts;
  2336. importer->get_import_options(p_file, &opts);
  2337. for (const ResourceImporter::ImportOption &E : opts) {
  2338. if (!params.has(E.option.name)) { //this one is not present
  2339. params[E.option.name] = E.default_value;
  2340. }
  2341. }
  2342. if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  2343. //use defaults if exist
  2344. Dictionary d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
  2345. List<Variant> v;
  2346. d.get_key_list(&v);
  2347. for (const Variant &E : v) {
  2348. params[E] = d[E];
  2349. }
  2350. }
  2351. if (uid == ResourceUID::INVALID_ID) {
  2352. uid = ResourceUID::get_singleton()->create_id();
  2353. }
  2354. //finally, perform import!!
  2355. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
  2356. List<String> import_variants;
  2357. List<String> gen_files;
  2358. Variant meta;
  2359. Error err = importer->import(uid, p_file, base_path, params, &import_variants, &gen_files, &meta);
  2360. // As import is complete, save the .import file.
  2361. Vector<String> dest_paths;
  2362. {
  2363. Ref<FileAccess> f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
  2364. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open file from path '" + p_file + ".import'.");
  2365. // Write manually, as order matters ([remap] has to go first for performance).
  2366. f->store_line("[remap]");
  2367. f->store_line("");
  2368. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  2369. int version = importer->get_format_version();
  2370. if (version > 0) {
  2371. f->store_line("importer_version=" + itos(version));
  2372. }
  2373. if (!importer->get_resource_type().is_empty()) {
  2374. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  2375. }
  2376. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
  2377. if (err == OK) {
  2378. if (importer->get_save_extension().is_empty()) {
  2379. //no path
  2380. } else if (import_variants.size()) {
  2381. //import with variants
  2382. for (const String &E : import_variants) {
  2383. String path = base_path.c_escape() + "." + E + "." + importer->get_save_extension();
  2384. f->store_line("path." + E + "=\"" + path + "\"");
  2385. dest_paths.push_back(path);
  2386. }
  2387. } else {
  2388. String path = base_path + "." + importer->get_save_extension();
  2389. f->store_line("path=\"" + path + "\"");
  2390. dest_paths.push_back(path);
  2391. }
  2392. } else {
  2393. f->store_line("valid=false");
  2394. }
  2395. if (meta != Variant()) {
  2396. f->store_line("metadata=" + meta.get_construct_string());
  2397. }
  2398. if (generator_parameters != Variant()) {
  2399. f->store_line("generator_parameters=" + generator_parameters.get_construct_string());
  2400. }
  2401. f->store_line("");
  2402. f->store_line("[deps]\n");
  2403. if (gen_files.size()) {
  2404. Array genf;
  2405. for (const String &E : gen_files) {
  2406. genf.push_back(E);
  2407. dest_paths.push_back(E);
  2408. }
  2409. String value;
  2410. VariantWriter::write_to_string(genf, value);
  2411. f->store_line("files=" + value);
  2412. f->store_line("");
  2413. }
  2414. f->store_line("source_file=" + Variant(p_file).get_construct_string());
  2415. if (dest_paths.size()) {
  2416. Array dp;
  2417. for (int i = 0; i < dest_paths.size(); i++) {
  2418. dp.push_back(dest_paths[i]);
  2419. }
  2420. f->store_line("dest_files=" + Variant(dp).get_construct_string());
  2421. }
  2422. f->store_line("");
  2423. f->store_line("[params]");
  2424. f->store_line("");
  2425. // Store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  2426. for (const ResourceImporter::ImportOption &E : opts) {
  2427. String base = E.option.name;
  2428. String value;
  2429. VariantWriter::write_to_string(params[base], value);
  2430. f->store_line(base + "=" + value);
  2431. }
  2432. }
  2433. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  2434. {
  2435. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  2436. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  2437. md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"");
  2438. if (dest_paths.size()) {
  2439. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  2440. }
  2441. }
  2442. if (p_update_file_system) {
  2443. // Update cpos, newly created files could've changed the index of the reimported p_file.
  2444. _find_file(p_file, &fs, cpos);
  2445. // Update modified times, to avoid reimport.
  2446. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  2447. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  2448. fs->files[cpos]->import_md5 = FileAccess::get_md5(p_file + ".import");
  2449. fs->files[cpos]->import_dest_paths = dest_paths;
  2450. fs->files[cpos]->deps = _get_dependencies(p_file);
  2451. fs->files[cpos]->type = importer->get_resource_type();
  2452. fs->files[cpos]->uid = uid;
  2453. fs->files[cpos]->import_valid = fs->files[cpos]->type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  2454. }
  2455. if (ResourceUID::get_singleton()->has_id(uid)) {
  2456. ResourceUID::get_singleton()->set_id(uid, p_file);
  2457. } else {
  2458. ResourceUID::get_singleton()->add_id(uid, p_file);
  2459. }
  2460. // If file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  2461. // to reload properly.
  2462. Ref<Resource> r = ResourceCache::get_ref(p_file);
  2463. if (r.is_valid()) {
  2464. if (!r->get_import_path().is_empty()) {
  2465. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
  2466. r->set_import_path(dst_path);
  2467. r->set_import_last_modified_time(0);
  2468. }
  2469. }
  2470. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  2471. print_verbose(vformat("EditorFileSystem: \"%s\" import took %d ms.", p_file, OS::get_singleton()->get_ticks_msec() - start_time));
  2472. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_UNRECOGNIZED, "Error importing '" + p_file + "'.");
  2473. return OK;
  2474. }
  2475. void EditorFileSystem::_find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport) {
  2476. int fc = efd->files.size();
  2477. const EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptr();
  2478. for (int i = 0; i < fc; i++) {
  2479. if (groups_to_reimport.has(files[i]->import_group_file)) {
  2480. if (!group_files.has(files[i]->import_group_file)) {
  2481. group_files[files[i]->import_group_file] = Vector<String>();
  2482. }
  2483. group_files[files[i]->import_group_file].push_back(efd->get_file_path(i));
  2484. }
  2485. }
  2486. for (int i = 0; i < efd->get_subdir_count(); i++) {
  2487. _find_group_files(efd->get_subdir(i), group_files, groups_to_reimport);
  2488. }
  2489. }
  2490. void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params) {
  2491. Vector<String> reloads;
  2492. reloads.append(p_file);
  2493. // Emit the resource_reimporting signal for the single file before the actual importation.
  2494. emit_signal(SNAME("resources_reimporting"), reloads);
  2495. _reimport_file(p_file, p_custom_params, p_importer);
  2496. // Emit the resource_reimported signal for the single file we just reimported.
  2497. emit_signal(SNAME("resources_reimported"), reloads);
  2498. }
  2499. Error EditorFileSystem::_copy_file(const String &p_from, const String &p_to) {
  2500. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2501. if (FileAccess::exists(p_from + ".import")) {
  2502. Error err = da->copy(p_from, p_to);
  2503. if (err != OK) {
  2504. return err;
  2505. }
  2506. // Remove uid from .import file to avoid conflict.
  2507. Ref<ConfigFile> cfg;
  2508. cfg.instantiate();
  2509. cfg->load(p_from + ".import");
  2510. cfg->erase_section_key("remap", "uid");
  2511. err = cfg->save(p_to + ".import");
  2512. if (err != OK) {
  2513. return err;
  2514. }
  2515. } else if (ResourceLoader::get_resource_uid(p_from) == ResourceUID::INVALID_ID) {
  2516. // Files which do not use an uid can just be copied.
  2517. Error err = da->copy(p_from, p_to);
  2518. if (err != OK) {
  2519. return err;
  2520. }
  2521. } else {
  2522. // Load the resource and save it again in the new location (this generates a new UID).
  2523. Error err;
  2524. Ref<Resource> res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
  2525. if (err == OK && res.is_valid()) {
  2526. err = ResourceSaver::save(res, p_to, ResourceSaver::FLAG_COMPRESS);
  2527. if (err != OK) {
  2528. return err;
  2529. }
  2530. } else if (err != OK) {
  2531. // When loading files like text files the error is OK but the resource is still null.
  2532. // We can ignore such files.
  2533. return err;
  2534. }
  2535. }
  2536. return OK;
  2537. }
  2538. bool EditorFileSystem::_copy_directory(const String &p_from, const String &p_to, List<CopiedFile> *p_files) {
  2539. Ref<DirAccess> old_dir = DirAccess::open(p_from);
  2540. ERR_FAIL_COND_V(old_dir.is_null(), false);
  2541. Error err = make_dir_recursive(p_to);
  2542. if (err != OK && err != ERR_ALREADY_EXISTS) {
  2543. return false;
  2544. }
  2545. bool success = true;
  2546. old_dir->set_include_navigational(false);
  2547. old_dir->list_dir_begin();
  2548. for (String F = old_dir->_get_next(); !F.is_empty(); F = old_dir->_get_next()) {
  2549. if (old_dir->current_is_dir()) {
  2550. success = _copy_directory(p_from.path_join(F), p_to.path_join(F), p_files) && success;
  2551. } else if (F.get_extension() != "import" && F.get_extension() != "uid") {
  2552. CopiedFile copy;
  2553. copy.from = p_from.path_join(F);
  2554. copy.to = p_to.path_join(F);
  2555. p_files->push_back(copy);
  2556. }
  2557. }
  2558. return success;
  2559. }
  2560. void EditorFileSystem::_queue_refresh_filesystem() {
  2561. if (refresh_queued) {
  2562. return;
  2563. }
  2564. refresh_queued = true;
  2565. get_tree()->connect(SNAME("process_frame"), callable_mp(this, &EditorFileSystem::_refresh_filesystem), CONNECT_ONE_SHOT);
  2566. }
  2567. void EditorFileSystem::_refresh_filesystem() {
  2568. for (const ObjectID &id : folders_to_sort) {
  2569. EditorFileSystemDirectory *dir = Object::cast_to<EditorFileSystemDirectory>(ObjectDB::get_instance(id));
  2570. if (dir) {
  2571. dir->subdirs.sort_custom<DirectoryComparator>();
  2572. }
  2573. }
  2574. folders_to_sort.clear();
  2575. _update_scan_actions();
  2576. emit_signal(SNAME("filesystem_changed"));
  2577. refresh_queued = false;
  2578. }
  2579. void EditorFileSystem::_reimport_thread(uint32_t p_index, ImportThreadData *p_import_data) {
  2580. int file_idx = p_import_data->reimport_from + int(p_index);
  2581. _reimport_file(p_import_data->reimport_files[file_idx].path);
  2582. p_import_data->imported_sem->post();
  2583. }
  2584. void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
  2585. ERR_FAIL_COND_MSG(importing, "Attempted to call reimport_files() recursively, this is not allowed.");
  2586. importing = true;
  2587. Vector<String> reloads;
  2588. EditorProgress *ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
  2589. // The method reimport_files runs on the main thread, and if VSync is enabled
  2590. // or Update Continuously is disabled, Main::Iteration takes longer each frame.
  2591. // Each EditorProgress::step can trigger a redraw, and when there are many files to import,
  2592. // this could lead to a slow import process, especially when the editor is unfocused.
  2593. // Temporarily disabling VSync and low_processor_usage_mode while reimporting fixes this.
  2594. const bool old_low_processor_usage_mode = OS::get_singleton()->is_in_low_processor_usage_mode();
  2595. const DisplayServer::VSyncMode old_vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
  2596. OS::get_singleton()->set_low_processor_usage_mode(false);
  2597. DisplayServer::get_singleton()->window_set_vsync_mode(DisplayServer::VSyncMode::VSYNC_DISABLED);
  2598. Vector<ImportFile> reimport_files;
  2599. HashSet<String> groups_to_reimport;
  2600. for (int i = 0; i < p_files.size(); i++) {
  2601. ep->step(TTR("Preparing files to reimport..."), i, false);
  2602. String file = p_files[i];
  2603. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(file);
  2604. if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
  2605. file = ResourceUID::get_singleton()->get_id_path(uid);
  2606. }
  2607. String group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(file);
  2608. if (group_file_cache.has(file)) {
  2609. // Maybe the file itself is a group!
  2610. groups_to_reimport.insert(file);
  2611. // Groups do not belong to groups.
  2612. group_file = String();
  2613. } else if (groups_to_reimport.has(file)) {
  2614. // Groups do not belong to groups.
  2615. group_file = String();
  2616. } else if (!group_file.is_empty()) {
  2617. // It's a group file, add group to import and skip this file.
  2618. groups_to_reimport.insert(group_file);
  2619. } else {
  2620. // It's a regular file.
  2621. ImportFile ifile;
  2622. ifile.path = file;
  2623. ResourceFormatImporter::get_singleton()->get_import_order_threads_and_importer(file, ifile.order, ifile.threaded, ifile.importer);
  2624. reloads.push_back(file);
  2625. reimport_files.push_back(ifile);
  2626. }
  2627. // Group may have changed, so also update group reference.
  2628. EditorFileSystemDirectory *fs = nullptr;
  2629. int cpos = -1;
  2630. if (_find_file(file, &fs, cpos)) {
  2631. fs->files.write[cpos]->import_group_file = group_file;
  2632. }
  2633. }
  2634. reimport_files.sort();
  2635. ep->step(TTR("Executing pre-reimport operations..."), 0, true);
  2636. // Emit the resource_reimporting signal for the single file before the actual importation.
  2637. emit_signal(SNAME("resources_reimporting"), reloads);
  2638. #ifdef THREADS_ENABLED
  2639. bool use_multiple_threads = GLOBAL_GET("editor/import/use_multiple_threads");
  2640. #else
  2641. bool use_multiple_threads = false;
  2642. #endif
  2643. int from = 0;
  2644. Semaphore imported_sem;
  2645. for (int i = 0; i < reimport_files.size(); i++) {
  2646. if (groups_to_reimport.has(reimport_files[i].path)) {
  2647. from = i + 1;
  2648. continue;
  2649. }
  2650. if (use_multiple_threads && reimport_files[i].threaded) {
  2651. if (i + 1 == reimport_files.size() || reimport_files[i + 1].importer != reimport_files[from].importer || groups_to_reimport.has(reimport_files[i + 1].path)) {
  2652. if (from - i == 0) {
  2653. // Single file, do not use threads.
  2654. ep->step(reimport_files[i].path.get_file(), i, false);
  2655. _reimport_file(reimport_files[i].path);
  2656. } else {
  2657. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(reimport_files[from].importer);
  2658. if (importer.is_null()) {
  2659. ERR_PRINT(vformat("Invalid importer for \"%s\".", reimport_files[from].importer));
  2660. from = i + 1;
  2661. continue;
  2662. }
  2663. importer->import_threaded_begin();
  2664. ImportThreadData tdata;
  2665. tdata.reimport_from = from;
  2666. tdata.reimport_files = reimport_files.ptr();
  2667. tdata.imported_sem = &imported_sem;
  2668. int item_count = i - from + 1;
  2669. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &EditorFileSystem::_reimport_thread, &tdata, item_count, -1, false, vformat(TTR("Import resources of type: %s"), reimport_files[from].importer));
  2670. int imported_count = 0;
  2671. while (true) {
  2672. ep->step(reimport_files[imported_count].path.get_file(), from + imported_count, false);
  2673. imported_sem.wait();
  2674. do {
  2675. imported_count++;
  2676. } while (imported_sem.try_wait());
  2677. if (imported_count == item_count) {
  2678. break;
  2679. }
  2680. }
  2681. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  2682. DEV_ASSERT(!imported_sem.try_wait());
  2683. importer->import_threaded_end();
  2684. }
  2685. from = i + 1;
  2686. }
  2687. } else {
  2688. ep->step(reimport_files[i].path.get_file(), i, false);
  2689. _reimport_file(reimport_files[i].path);
  2690. // We need to increment the counter, maybe the next file is multithreaded
  2691. // and doesn't have the same importer.
  2692. from = i + 1;
  2693. }
  2694. }
  2695. // Reimport groups.
  2696. from = reimport_files.size();
  2697. if (groups_to_reimport.size()) {
  2698. HashMap<String, Vector<String>> group_files;
  2699. _find_group_files(filesystem, group_files, groups_to_reimport);
  2700. for (const KeyValue<String, Vector<String>> &E : group_files) {
  2701. ep->step(E.key.get_file(), from++, false);
  2702. Error err = _reimport_group(E.key, E.value);
  2703. reloads.push_back(E.key);
  2704. reloads.append_array(E.value);
  2705. if (err == OK) {
  2706. _reimport_file(E.key);
  2707. }
  2708. }
  2709. }
  2710. ep->step(TTR("Finalizing Asset Import..."), p_files.size());
  2711. ResourceUID::get_singleton()->update_cache(); // After reimporting, update the cache.
  2712. _save_filesystem_cache();
  2713. memdelete_notnull(ep);
  2714. _process_update_pending();
  2715. // Revert to previous values to restore editor settings for VSync and Update Continuously.
  2716. OS::get_singleton()->set_low_processor_usage_mode(old_low_processor_usage_mode);
  2717. DisplayServer::get_singleton()->window_set_vsync_mode(old_vsync_mode);
  2718. importing = false;
  2719. ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
  2720. ep->step(TTR("Executing post-reimport operations..."), 0, true);
  2721. if (!is_scanning()) {
  2722. emit_signal(SNAME("filesystem_changed"));
  2723. }
  2724. emit_signal(SNAME("resources_reimported"), reloads);
  2725. memdelete_notnull(ep);
  2726. }
  2727. Error EditorFileSystem::reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
  2728. Vector<String> reloads;
  2729. reloads.append(p_file);
  2730. // Emit the resource_reimporting signal for the single file before the actual importation.
  2731. emit_signal(SNAME("resources_reimporting"), reloads);
  2732. Error ret = _reimport_file(p_file, p_custom_options, p_custom_importer, &p_generator_parameters);
  2733. // Emit the resource_reimported signal for the single file we just reimported.
  2734. emit_signal(SNAME("resources_reimported"), reloads);
  2735. return ret;
  2736. }
  2737. Error EditorFileSystem::_resource_import(const String &p_path) {
  2738. Vector<String> files;
  2739. files.push_back(p_path);
  2740. singleton->update_file(p_path);
  2741. singleton->reimport_files(files);
  2742. return OK;
  2743. }
  2744. Ref<Resource> EditorFileSystem::_load_resource_on_startup(ResourceFormatImporter *p_importer, const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, ResourceFormatLoader::CacheMode p_cache_mode) {
  2745. ERR_FAIL_NULL_V(p_importer, Ref<Resource>());
  2746. if (!FileAccess::exists(p_path)) {
  2747. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Failed loading resource: %s. The file doesn't seem to exist.", p_path));
  2748. }
  2749. Ref<Resource> res;
  2750. bool can_retry = true;
  2751. bool retry = true;
  2752. while (retry) {
  2753. retry = false;
  2754. res = p_importer->load_internal(p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode, can_retry);
  2755. if (res.is_null() && can_retry) {
  2756. can_retry = false;
  2757. Error err = singleton->_reimport_file(p_path, HashMap<StringName, Variant>(), "", nullptr, false);
  2758. if (err == OK) {
  2759. retry = true;
  2760. }
  2761. }
  2762. }
  2763. return res;
  2764. }
  2765. bool EditorFileSystem::_should_skip_directory(const String &p_path) {
  2766. String project_data_path = ProjectSettings::get_singleton()->get_project_data_path();
  2767. if (p_path == project_data_path || p_path.begins_with(project_data_path + "/")) {
  2768. return true;
  2769. }
  2770. if (FileAccess::exists(p_path.path_join("project.godot"))) {
  2771. // Skip if another project inside this.
  2772. if (EditorFileSystem::get_singleton()->first_scan) {
  2773. WARN_PRINT_ONCE(vformat("Detected another project.godot at %s. The folder will be ignored.", p_path));
  2774. }
  2775. return true;
  2776. }
  2777. if (FileAccess::exists(p_path.path_join(".gdignore"))) {
  2778. // Skip if a `.gdignore` file is inside this.
  2779. return true;
  2780. }
  2781. return false;
  2782. }
  2783. bool EditorFileSystem::is_group_file(const String &p_path) const {
  2784. return group_file_cache.has(p_path);
  2785. }
  2786. void EditorFileSystem::_move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location) {
  2787. int fc = efd->files.size();
  2788. EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptrw();
  2789. for (int i = 0; i < fc; i++) {
  2790. if (files[i]->import_group_file == p_group_file) {
  2791. files[i]->import_group_file = p_new_location;
  2792. Ref<ConfigFile> config;
  2793. config.instantiate();
  2794. String path = efd->get_file_path(i) + ".import";
  2795. Error err = config->load(path);
  2796. if (err != OK) {
  2797. continue;
  2798. }
  2799. if (config->has_section_key("remap", "group_file")) {
  2800. config->set_value("remap", "group_file", p_new_location);
  2801. }
  2802. List<String> sk;
  2803. config->get_section_keys("params", &sk);
  2804. for (const String &param : sk) {
  2805. //not very clean, but should work
  2806. String value = config->get_value("params", param);
  2807. if (value == p_group_file) {
  2808. config->set_value("params", param, p_new_location);
  2809. }
  2810. }
  2811. config->save(path);
  2812. }
  2813. }
  2814. for (int i = 0; i < efd->get_subdir_count(); i++) {
  2815. _move_group_files(efd->get_subdir(i), p_group_file, p_new_location);
  2816. }
  2817. }
  2818. void EditorFileSystem::move_group_file(const String &p_path, const String &p_new_path) {
  2819. if (get_filesystem()) {
  2820. _move_group_files(get_filesystem(), p_path, p_new_path);
  2821. if (group_file_cache.has(p_path)) {
  2822. group_file_cache.erase(p_path);
  2823. group_file_cache.insert(p_new_path);
  2824. }
  2825. }
  2826. }
  2827. Error EditorFileSystem::make_dir_recursive(const String &p_path, const String &p_base_path) {
  2828. Error err;
  2829. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2830. if (!p_base_path.is_empty()) {
  2831. err = da->change_dir(p_base_path);
  2832. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open base directory '" + p_base_path + "'.");
  2833. }
  2834. if (da->dir_exists(p_path)) {
  2835. return ERR_ALREADY_EXISTS;
  2836. }
  2837. err = da->make_dir_recursive(p_path);
  2838. if (err != OK) {
  2839. return err;
  2840. }
  2841. const String path = da->get_current_dir();
  2842. EditorFileSystemDirectory *parent = get_filesystem_path(path);
  2843. ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
  2844. folders_to_sort.insert(parent->get_instance_id());
  2845. const PackedStringArray folders = p_path.trim_prefix(path).trim_suffix("/").split("/");
  2846. for (const String &folder : folders) {
  2847. const int current = parent->find_dir_index(folder);
  2848. if (current > -1) {
  2849. parent = parent->get_subdir(current);
  2850. continue;
  2851. }
  2852. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  2853. efd->parent = parent;
  2854. efd->name = folder;
  2855. parent->subdirs.push_back(efd);
  2856. parent = efd;
  2857. }
  2858. _queue_refresh_filesystem();
  2859. return OK;
  2860. }
  2861. Error EditorFileSystem::copy_file(const String &p_from, const String &p_to) {
  2862. _copy_file(p_from, p_to);
  2863. EditorFileSystemDirectory *parent = get_filesystem_path(p_to.get_base_dir());
  2864. ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
  2865. ScanProgress sp;
  2866. _scan_fs_changes(parent, sp, false);
  2867. _queue_refresh_filesystem();
  2868. return OK;
  2869. }
  2870. Error EditorFileSystem::copy_directory(const String &p_from, const String &p_to) {
  2871. List<CopiedFile> files;
  2872. bool success = _copy_directory(p_from, p_to, &files);
  2873. EditorProgress *ep = nullptr;
  2874. if (files.size() > 10) {
  2875. ep = memnew(EditorProgress("_copy_files", TTR("Copying files..."), files.size()));
  2876. }
  2877. int i = 0;
  2878. for (const CopiedFile &F : files) {
  2879. if (_copy_file(F.from, F.to) != OK) {
  2880. success = false;
  2881. }
  2882. if (ep) {
  2883. ep->step(F.from.get_file(), i++, false);
  2884. }
  2885. }
  2886. memdelete_notnull(ep);
  2887. EditorFileSystemDirectory *efd = get_filesystem_path(p_to);
  2888. ERR_FAIL_NULL_V(efd, FAILED);
  2889. ERR_FAIL_NULL_V(efd->get_parent(), FAILED);
  2890. folders_to_sort.insert(efd->get_parent()->get_instance_id());
  2891. ScanProgress sp;
  2892. _scan_fs_changes(efd, sp);
  2893. _queue_refresh_filesystem();
  2894. return success ? OK : FAILED;
  2895. }
  2896. ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate) {
  2897. if (!p_path.is_resource_file() || p_path.begins_with(ProjectSettings::get_singleton()->get_project_data_path())) {
  2898. // Saved externally (configuration file) or internal file, do not assign an ID.
  2899. return ResourceUID::INVALID_ID;
  2900. }
  2901. EditorFileSystemDirectory *fs = nullptr;
  2902. int cpos = -1;
  2903. if (!singleton->_find_file(p_path, &fs, cpos)) {
  2904. // Fallback to ResourceLoader if filesystem cache fails (can happen during scanning etc.).
  2905. ResourceUID::ID fallback = ResourceLoader::get_resource_uid(p_path);
  2906. if (fallback != ResourceUID::INVALID_ID) {
  2907. return fallback;
  2908. }
  2909. if (p_generate) {
  2910. return ResourceUID::get_singleton()->create_id(); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
  2911. } else {
  2912. return ResourceUID::INVALID_ID;
  2913. }
  2914. } else if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  2915. return fs->files[cpos]->uid;
  2916. } else if (p_generate) {
  2917. return ResourceUID::get_singleton()->create_id(); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
  2918. } else {
  2919. return ResourceUID::INVALID_ID;
  2920. }
  2921. }
  2922. static void _scan_extensions_dir(EditorFileSystemDirectory *d, HashSet<String> &extensions) {
  2923. int fc = d->get_file_count();
  2924. for (int i = 0; i < fc; i++) {
  2925. if (d->get_file_type(i) == SNAME("GDExtension")) {
  2926. extensions.insert(d->get_file_path(i));
  2927. }
  2928. }
  2929. int dc = d->get_subdir_count();
  2930. for (int i = 0; i < dc; i++) {
  2931. _scan_extensions_dir(d->get_subdir(i), extensions);
  2932. }
  2933. }
  2934. bool EditorFileSystem::_scan_extensions() {
  2935. EditorFileSystemDirectory *d = get_filesystem();
  2936. HashSet<String> extensions;
  2937. _scan_extensions_dir(d, extensions);
  2938. return GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions);
  2939. }
  2940. void EditorFileSystem::_bind_methods() {
  2941. ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
  2942. ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
  2943. ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
  2944. ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
  2945. ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
  2946. ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
  2947. ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
  2948. ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
  2949. ClassDB::bind_method(D_METHOD("reimport_files", "files"), &EditorFileSystem::reimport_files);
  2950. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  2951. ADD_SIGNAL(MethodInfo("script_classes_updated"));
  2952. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  2953. ADD_SIGNAL(MethodInfo("resources_reimporting", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2954. ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2955. ADD_SIGNAL(MethodInfo("resources_reload", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2956. }
  2957. void EditorFileSystem::_update_extensions() {
  2958. valid_extensions.clear();
  2959. import_extensions.clear();
  2960. textfile_extensions.clear();
  2961. other_file_extensions.clear();
  2962. List<String> extensionsl;
  2963. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  2964. for (const String &E : extensionsl) {
  2965. valid_extensions.insert(E);
  2966. }
  2967. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  2968. for (const String &E : textfile_ext) {
  2969. if (valid_extensions.has(E)) {
  2970. continue;
  2971. }
  2972. valid_extensions.insert(E);
  2973. textfile_extensions.insert(E);
  2974. }
  2975. const Vector<String> other_file_ext = ((String)(EDITOR_GET("docks/filesystem/other_file_extensions"))).split(",", false);
  2976. for (const String &E : other_file_ext) {
  2977. if (valid_extensions.has(E)) {
  2978. continue;
  2979. }
  2980. valid_extensions.insert(E);
  2981. other_file_extensions.insert(E);
  2982. }
  2983. extensionsl.clear();
  2984. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  2985. for (const String &E : extensionsl) {
  2986. import_extensions.insert(E);
  2987. }
  2988. }
  2989. void EditorFileSystem::add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2990. ERR_FAIL_COND(import_support_queries.has(p_query));
  2991. import_support_queries.push_back(p_query);
  2992. }
  2993. void EditorFileSystem::remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2994. import_support_queries.erase(p_query);
  2995. }
  2996. EditorFileSystem::EditorFileSystem() {
  2997. #ifdef THREADS_ENABLED
  2998. use_threads = true;
  2999. #endif
  3000. ResourceLoader::import = _resource_import;
  3001. reimport_on_missing_imported_files = GLOBAL_GET("editor/import/reimport_missing_imported_files");
  3002. singleton = this;
  3003. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  3004. filesystem->parent = nullptr;
  3005. new_filesystem = nullptr;
  3006. // This should probably also work on Unix and use the string it returns for FAT32 or exFAT
  3007. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  3008. using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "exFAT");
  3009. scan_total = 0;
  3010. ResourceSaver::set_get_resource_id_for_path(_resource_saver_get_resource_id_for_path);
  3011. // Set the callback method that the ResourceFormatImporter will use
  3012. // if resources are loaded during the first scan.
  3013. ResourceImporter::load_on_startup = _load_resource_on_startup;
  3014. }
  3015. EditorFileSystem::~EditorFileSystem() {
  3016. if (filesystem) {
  3017. memdelete(filesystem);
  3018. }
  3019. filesystem = nullptr;
  3020. ResourceSaver::set_get_resource_id_for_path(nullptr);
  3021. }