editor_file_system.cpp 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565
  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. if (ClassDB::is_parent_class(ia.new_file->type, SNAME("Script"))) {
  716. _queue_update_script_class(ia.dir->get_file_path(idx), ia.new_file->type, ia.new_file->script_class_name, ia.new_file->script_class_extends, ia.new_file->script_class_icon_path);
  717. }
  718. if (ia.new_file->type == SNAME("PackedScene")) {
  719. _queue_update_scene_groups(ia.dir->get_file_path(idx));
  720. }
  721. } break;
  722. case ItemAction::ACTION_FILE_REMOVE: {
  723. int idx = ia.dir->find_file_index(ia.file);
  724. ERR_CONTINUE(idx == -1);
  725. String script_class_name = ia.dir->files[idx]->script_class_name;
  726. if (ClassDB::is_parent_class(ia.dir->files[idx]->type, SNAME("Script"))) {
  727. _queue_update_script_class(ia.dir->get_file_path(idx), "", "", "", "");
  728. }
  729. if (ia.dir->files[idx]->type == SNAME("PackedScene")) {
  730. _queue_update_scene_groups(ia.dir->get_file_path(idx));
  731. }
  732. _delete_internal_files(ia.dir->files[idx]->file);
  733. memdelete(ia.dir->files[idx]);
  734. ia.dir->files.remove_at(idx);
  735. // Restore another script with the same global class name if it exists.
  736. if (!script_class_name.is_empty()) {
  737. EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
  738. String old_file = _get_file_by_class_name(filesystem, script_class_name, old_fi);
  739. if (!old_file.is_empty() && old_fi) {
  740. _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);
  741. }
  742. }
  743. fs_changed = true;
  744. } break;
  745. case ItemAction::ACTION_FILE_TEST_REIMPORT: {
  746. int idx = ia.dir->find_file_index(ia.file);
  747. ERR_CONTINUE(idx == -1);
  748. String full_path = ia.dir->get_file_path(idx);
  749. bool need_reimport = _test_for_reimport(full_path, ia.dir->files[idx]->import_md5);
  750. if (need_reimport) {
  751. // Must reimport.
  752. reimports.push_back(full_path);
  753. Vector<String> dependencies = _get_dependencies(full_path);
  754. for (const String &dep : dependencies) {
  755. const String &dependency_path = dep.contains("::") ? dep.get_slice("::", 0) : dep;
  756. if (import_extensions.has(dep.get_extension())) {
  757. reimports.push_back(dependency_path);
  758. }
  759. }
  760. } else {
  761. // Must not reimport, all was good.
  762. // Update modified times, md5 and destination paths, to avoid reimport.
  763. ia.dir->files[idx]->modified_time = FileAccess::get_modified_time(full_path);
  764. ia.dir->files[idx]->import_modified_time = FileAccess::get_modified_time(full_path + ".import");
  765. if (ia.dir->files[idx]->import_md5.is_empty()) {
  766. ia.dir->files[idx]->import_md5 = FileAccess::get_md5(full_path + ".import");
  767. }
  768. ia.dir->files[idx]->import_dest_paths = _get_import_dest_paths(full_path);
  769. }
  770. fs_changed = true;
  771. } break;
  772. case ItemAction::ACTION_FILE_RELOAD: {
  773. int idx = ia.dir->find_file_index(ia.file);
  774. ERR_CONTINUE(idx == -1);
  775. // Only reloads the resources that are already loaded.
  776. if (ResourceCache::has(ia.dir->get_file_path(idx))) {
  777. reloads.push_back(ia.dir->get_file_path(idx));
  778. }
  779. } break;
  780. }
  781. if (ep) {
  782. ep->step(ia.file, step_count++, false);
  783. }
  784. }
  785. memdelete_notnull(ep);
  786. if (_scan_extensions()) {
  787. //needs editor restart
  788. //extensions also may provide filetypes to be imported, so they must run before importing
  789. 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"))) {
  790. if (!first_scan) {
  791. EditorNode::get_singleton()->save_all_scenes();
  792. }
  793. EditorNode::get_singleton()->restart_editor();
  794. //do not import
  795. return true;
  796. }
  797. }
  798. if (!reimports.is_empty()) {
  799. if (_scan_import_support(reimports)) {
  800. return true;
  801. }
  802. reimport_files(reimports);
  803. } else {
  804. //reimport files will update the uid cache file so if nothing was reimported, update it manually
  805. ResourceUID::get_singleton()->update_cache();
  806. }
  807. if (!reloads.is_empty()) {
  808. // Update global class names, dependencies, etc...
  809. update_files(reloads);
  810. }
  811. if (first_scan) {
  812. //only on first scan this is valid and updated, then settings changed.
  813. revalidate_import_files = false;
  814. filesystem_settings_version_for_import = ResourceFormatImporter::get_singleton()->get_import_settings_hash();
  815. _save_filesystem_cache();
  816. }
  817. // Moving the processing of pending updates before the resources_reload event to be sure all global class names
  818. // are updated. Script.cpp listens on resources_reload and reloads updated scripts.
  819. _process_update_pending();
  820. if (reloads.size()) {
  821. emit_signal(SNAME("resources_reload"), reloads);
  822. }
  823. scan_actions.clear();
  824. return fs_changed;
  825. }
  826. void EditorFileSystem::scan() {
  827. if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/) {
  828. return;
  829. }
  830. if (scanning || scanning_changes || thread.is_started()) {
  831. return;
  832. }
  833. // The first scan must be on the main thread because, after the first scan and update
  834. // of global class names, we load the plugins and autoloads. These need to
  835. // be added on the main thread because they are nodes, and we need to wait for them
  836. // to be loaded to continue the scan and reimportations.
  837. if (first_scan) {
  838. _first_scan_filesystem();
  839. }
  840. _update_extensions();
  841. if (!use_threads) {
  842. scanning = true;
  843. scan_total = 0;
  844. _scan_filesystem();
  845. if (filesystem) {
  846. memdelete(filesystem);
  847. }
  848. //file_type_cache.clear();
  849. filesystem = new_filesystem;
  850. new_filesystem = nullptr;
  851. _update_scan_actions();
  852. // Update all icons so they are loaded for the FileSystemDock.
  853. _update_files_icon_path();
  854. scanning = false;
  855. // Set first_scan to false before the signals so the function doing_first_scan can return false
  856. // in editor_node to start the export if needed.
  857. first_scan = false;
  858. ResourceImporter::load_on_startup = nullptr;
  859. emit_signal(SNAME("filesystem_changed"));
  860. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  861. } else {
  862. ERR_FAIL_COND(thread.is_started());
  863. set_process(true);
  864. Thread::Settings s;
  865. scanning = true;
  866. scan_total = 0;
  867. s.priority = Thread::PRIORITY_LOW;
  868. thread.start(_thread_func, this, s);
  869. }
  870. }
  871. void EditorFileSystem::ScanProgress::increment() {
  872. current++;
  873. float ratio = current / MAX(hi, 1.0f);
  874. if (progress) {
  875. progress->step(ratio * 1000.0f);
  876. }
  877. EditorFileSystem::singleton->scan_total = ratio;
  878. }
  879. int EditorFileSystem::_scan_new_dir(ScannedDirectory *p_dir, Ref<DirAccess> &da) {
  880. List<String> dirs;
  881. List<String> files;
  882. String cd = da->get_current_dir();
  883. da->list_dir_begin();
  884. while (true) {
  885. String f = da->get_next();
  886. if (f.is_empty()) {
  887. break;
  888. }
  889. if (da->current_is_hidden()) {
  890. continue;
  891. }
  892. if (da->current_is_dir()) {
  893. if (f.begins_with(".")) { // Ignore special and . / ..
  894. continue;
  895. }
  896. if (_should_skip_directory(cd.path_join(f))) {
  897. continue;
  898. }
  899. dirs.push_back(f);
  900. } else {
  901. files.push_back(f);
  902. }
  903. }
  904. da->list_dir_end();
  905. dirs.sort_custom<FileNoCaseComparator>();
  906. files.sort_custom<FileNoCaseComparator>();
  907. int nb_files_total_scan = 0;
  908. for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
  909. if (da->change_dir(E->get()) == OK) {
  910. String d = da->get_current_dir();
  911. if (d == cd || !d.begins_with(cd)) {
  912. da->change_dir(cd); //avoid recursion
  913. } else {
  914. ScannedDirectory *sd = memnew(ScannedDirectory);
  915. sd->name = E->get();
  916. sd->full_path = p_dir->full_path.path_join(sd->name);
  917. nb_files_total_scan += _scan_new_dir(sd, da);
  918. p_dir->subdirs.push_back(sd);
  919. da->change_dir("..");
  920. }
  921. } else {
  922. ERR_PRINT("Cannot go into subdir '" + E->get() + "'.");
  923. }
  924. }
  925. p_dir->files = files;
  926. nb_files_total_scan += files.size();
  927. return nb_files_total_scan;
  928. }
  929. void EditorFileSystem::_process_file_system(const ScannedDirectory *p_scan_dir, EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, HashSet<String> *r_processed_files) {
  930. p_dir->modified_time = FileAccess::get_modified_time(p_scan_dir->full_path);
  931. for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
  932. EditorFileSystemDirectory *sub_dir = memnew(EditorFileSystemDirectory);
  933. sub_dir->parent = p_dir;
  934. sub_dir->name = scan_sub_dir->name;
  935. p_dir->subdirs.push_back(sub_dir);
  936. _process_file_system(scan_sub_dir, sub_dir, p_progress, r_processed_files);
  937. }
  938. for (const String &scan_file : p_scan_dir->files) {
  939. String ext = scan_file.get_extension().to_lower();
  940. if (!valid_extensions.has(ext)) {
  941. p_progress.increment();
  942. continue; //invalid
  943. }
  944. String path = p_scan_dir->full_path.path_join(scan_file);
  945. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  946. fi->file = scan_file;
  947. p_dir->files.push_back(fi);
  948. if (r_processed_files) {
  949. r_processed_files->insert(path);
  950. }
  951. FileCache *fc = file_cache.getptr(path);
  952. uint64_t mt = FileAccess::get_modified_time(path);
  953. if (import_extensions.has(ext)) {
  954. //is imported
  955. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  956. if (fc) {
  957. fi->type = fc->type;
  958. fi->resource_script_class = fc->resource_script_class;
  959. fi->uid = fc->uid;
  960. fi->deps = fc->deps;
  961. fi->modified_time = mt;
  962. fi->import_modified_time = import_mt;
  963. fi->import_md5 = fc->import_md5;
  964. fi->import_dest_paths = fc->import_dest_paths;
  965. fi->import_valid = fc->import_valid;
  966. fi->script_class_name = fc->script_class_name;
  967. fi->import_group_file = fc->import_group_file;
  968. fi->script_class_extends = fc->script_class_extends;
  969. fi->script_class_icon_path = fc->script_class_icon_path;
  970. // Ensures backward compatibility when the project is loaded for the first time with the added import_md5
  971. // and import_dest_paths properties in the file cache.
  972. if (fc->import_md5.is_empty()) {
  973. fi->import_md5 = FileAccess::get_md5(path + ".import");
  974. fi->import_dest_paths = _get_import_dest_paths(path);
  975. }
  976. // The method _is_test_for_reimport_needed checks if the files were modified and ensures that
  977. // all the destination files still exist without reading the .import file.
  978. // If something is different, we will queue a test for reimportation that will check
  979. // the md5 of all files and import settings and, if necessary, execute a reimportation.
  980. if (_is_test_for_reimport_needed(path, fc->modification_time, mt, fc->import_modification_time, import_mt, fi->import_dest_paths) ||
  981. (revalidate_import_files && !ResourceFormatImporter::get_singleton()->are_import_settings_valid(path))) {
  982. ItemAction ia;
  983. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  984. ia.dir = p_dir;
  985. ia.file = fi->file;
  986. scan_actions.push_back(ia);
  987. }
  988. if (fc->type.is_empty()) {
  989. fi->type = ResourceLoader::get_resource_type(path);
  990. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  991. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  992. //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?)
  993. //note: I think this should not happen any longer..
  994. }
  995. if (fc->uid == ResourceUID::INVALID_ID) {
  996. // imported files should always have a UID, so attempt to fetch it.
  997. fi->uid = ResourceLoader::get_resource_uid(path);
  998. }
  999. } else {
  1000. // Using get_resource_import_info() to prevent calling 3 times ResourceFormatImporter::_get_path_and_type.
  1001. ResourceFormatImporter::get_singleton()->get_resource_import_info(path, fi->type, fi->uid, fi->import_group_file);
  1002. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  1003. fi->modified_time = 0;
  1004. fi->import_modified_time = 0;
  1005. fi->import_md5 = "";
  1006. fi->import_dest_paths = Vector<String>();
  1007. fi->import_valid = (fi->type == "TextFile" || fi->type == "OtherFile") ? true : ResourceLoader::is_import_valid(path);
  1008. ItemAction ia;
  1009. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1010. ia.dir = p_dir;
  1011. ia.file = fi->file;
  1012. scan_actions.push_back(ia);
  1013. }
  1014. } else {
  1015. if (fc && fc->modification_time == mt) {
  1016. //not imported, so just update type if changed
  1017. fi->type = fc->type;
  1018. fi->resource_script_class = fc->resource_script_class;
  1019. fi->uid = fc->uid;
  1020. fi->modified_time = mt;
  1021. fi->deps = fc->deps;
  1022. fi->import_modified_time = 0;
  1023. fi->import_md5 = "";
  1024. fi->import_dest_paths = Vector<String>();
  1025. fi->import_valid = true;
  1026. fi->script_class_name = fc->script_class_name;
  1027. fi->script_class_extends = fc->script_class_extends;
  1028. fi->script_class_icon_path = fc->script_class_icon_path;
  1029. if (first_scan && ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  1030. bool update_script = false;
  1031. String old_class_name = fi->script_class_name;
  1032. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  1033. if (old_class_name != fi->script_class_name) {
  1034. update_script = true;
  1035. } 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)) {
  1036. // This script has a class name but is not in the global class names or the path of the class has changed.
  1037. update_script = true;
  1038. }
  1039. if (update_script) {
  1040. _queue_update_script_class(path, fi->type, fi->script_class_name, fi->script_class_extends, fi->script_class_icon_path);
  1041. }
  1042. }
  1043. } else {
  1044. //new or modified time
  1045. fi->type = ResourceLoader::get_resource_type(path);
  1046. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  1047. if (fi->type == "" && textfile_extensions.has(ext)) {
  1048. fi->type = "TextFile";
  1049. }
  1050. if (fi->type == "" && other_file_extensions.has(ext)) {
  1051. fi->type = "OtherFile";
  1052. }
  1053. fi->uid = ResourceLoader::get_resource_uid(path);
  1054. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  1055. fi->deps = _get_dependencies(path);
  1056. fi->modified_time = mt;
  1057. fi->import_modified_time = 0;
  1058. fi->import_md5 = "";
  1059. fi->import_dest_paths = Vector<String>();
  1060. fi->import_valid = true;
  1061. // Files in dep_update_list are forced for rescan to update dependencies. They don't need other updates.
  1062. if (!dep_update_list.has(path)) {
  1063. if (ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  1064. _queue_update_script_class(path, fi->type, fi->script_class_name, fi->script_class_extends, fi->script_class_icon_path);
  1065. }
  1066. if (fi->type == SNAME("PackedScene")) {
  1067. _queue_update_scene_groups(path);
  1068. }
  1069. }
  1070. }
  1071. if (fi->uid == ResourceUID::INVALID_ID && ResourceLoader::exists(path) && !ResourceLoader::has_custom_uid_support(path) && !FileAccess::exists(path + ".uid")) {
  1072. // Create a UID.
  1073. Ref<FileAccess> f = FileAccess::open(path + ".uid", FileAccess::WRITE);
  1074. if (f.is_valid()) {
  1075. fi->uid = ResourceUID::get_singleton()->create_id();
  1076. f->store_line(ResourceUID::get_singleton()->id_to_text(fi->uid));
  1077. }
  1078. }
  1079. }
  1080. if (fi->uid != ResourceUID::INVALID_ID) {
  1081. if (ResourceUID::get_singleton()->has_id(fi->uid)) {
  1082. // Restrict UID dupe warning to first-scan since we know there are no file moves going on yet.
  1083. if (first_scan) {
  1084. // Warn if we detect files with duplicate UIDs.
  1085. const String other_path = ResourceUID::get_singleton()->get_id_path(fi->uid);
  1086. if (other_path != path) {
  1087. WARN_PRINT(vformat("UID duplicate detected between %s and %s.", path, other_path));
  1088. }
  1089. }
  1090. ResourceUID::get_singleton()->set_id(fi->uid, path);
  1091. } else {
  1092. ResourceUID::get_singleton()->add_id(fi->uid, path);
  1093. }
  1094. }
  1095. p_progress.increment();
  1096. }
  1097. }
  1098. void EditorFileSystem::_process_removed_files(const HashSet<String> &p_processed_files) {
  1099. for (const KeyValue<String, EditorFileSystem::FileCache> &kv : file_cache) {
  1100. if (!p_processed_files.has(kv.key)) {
  1101. if (ClassDB::is_parent_class(kv.value.type, SNAME("Script"))) {
  1102. // A script has been removed from disk since the last startup. The documentation needs to be updated.
  1103. // There's no need to add the path in update_script_paths since that is exclusively for updating global class names,
  1104. // which is handled in _first_scan_filesystem before the full scan to ensure plugins and autoloads can be created.
  1105. MutexLock update_script_lock(update_script_mutex);
  1106. update_script_paths_documentation.insert(kv.key);
  1107. }
  1108. }
  1109. }
  1110. }
  1111. void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, bool p_recursive) {
  1112. uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
  1113. bool updated_dir = false;
  1114. String cd = p_dir->get_path();
  1115. int diff_nb_files = 0;
  1116. if (current_mtime != p_dir->modified_time || using_fat32_or_exfat) {
  1117. updated_dir = true;
  1118. p_dir->modified_time = current_mtime;
  1119. //ooooops, dir changed, see what's going on
  1120. //first mark everything as verified
  1121. for (int i = 0; i < p_dir->files.size(); i++) {
  1122. p_dir->files[i]->verified = false;
  1123. }
  1124. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1125. p_dir->get_subdir(i)->verified = false;
  1126. }
  1127. diff_nb_files -= p_dir->files.size();
  1128. //then scan files and directories and check what's different
  1129. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1130. Error ret = da->change_dir(cd);
  1131. ERR_FAIL_COND_MSG(ret != OK, "Cannot change to '" + cd + "' folder.");
  1132. da->list_dir_begin();
  1133. while (true) {
  1134. String f = da->get_next();
  1135. if (f.is_empty()) {
  1136. break;
  1137. }
  1138. if (da->current_is_hidden()) {
  1139. continue;
  1140. }
  1141. if (da->current_is_dir()) {
  1142. if (f.begins_with(".")) { // Ignore special and . / ..
  1143. continue;
  1144. }
  1145. int idx = p_dir->find_dir_index(f);
  1146. if (idx == -1) {
  1147. String dir_path = cd.path_join(f);
  1148. if (_should_skip_directory(dir_path)) {
  1149. continue;
  1150. }
  1151. ScannedDirectory sd;
  1152. sd.name = f;
  1153. sd.full_path = dir_path;
  1154. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  1155. efd->parent = p_dir;
  1156. efd->name = f;
  1157. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1158. d->change_dir(dir_path);
  1159. int nb_files_dir = _scan_new_dir(&sd, d);
  1160. p_progress.hi += nb_files_dir;
  1161. diff_nb_files += nb_files_dir;
  1162. _process_file_system(&sd, efd, p_progress, nullptr);
  1163. ItemAction ia;
  1164. ia.action = ItemAction::ACTION_DIR_ADD;
  1165. ia.dir = p_dir;
  1166. ia.file = f;
  1167. ia.new_dir = efd;
  1168. scan_actions.push_back(ia);
  1169. } else {
  1170. p_dir->subdirs[idx]->verified = true;
  1171. }
  1172. } else {
  1173. String ext = f.get_extension().to_lower();
  1174. if (!valid_extensions.has(ext)) {
  1175. continue; //invalid
  1176. }
  1177. int idx = p_dir->find_file_index(f);
  1178. if (idx == -1) {
  1179. //never seen this file, add actition to add it
  1180. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1181. fi->file = f;
  1182. String path = cd.path_join(fi->file);
  1183. fi->modified_time = FileAccess::get_modified_time(path);
  1184. fi->import_modified_time = 0;
  1185. fi->import_md5 = "";
  1186. fi->import_dest_paths = Vector<String>();
  1187. fi->type = ResourceLoader::get_resource_type(path);
  1188. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  1189. if (fi->type == "" && textfile_extensions.has(ext)) {
  1190. fi->type = "TextFile";
  1191. }
  1192. if (fi->type == "" && other_file_extensions.has(ext)) {
  1193. fi->type = "OtherFile";
  1194. }
  1195. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  1196. fi->import_valid = (fi->type == "TextFile" || fi->type == "OtherFile") ? true : ResourceLoader::is_import_valid(path);
  1197. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  1198. {
  1199. ItemAction ia;
  1200. ia.action = ItemAction::ACTION_FILE_ADD;
  1201. ia.dir = p_dir;
  1202. ia.file = f;
  1203. ia.new_file = fi;
  1204. scan_actions.push_back(ia);
  1205. }
  1206. if (import_extensions.has(ext)) {
  1207. //if it can be imported, and it was added, it needs to be reimported
  1208. ItemAction ia;
  1209. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1210. ia.dir = p_dir;
  1211. ia.file = f;
  1212. scan_actions.push_back(ia);
  1213. }
  1214. diff_nb_files++;
  1215. } else {
  1216. p_dir->files[idx]->verified = true;
  1217. }
  1218. }
  1219. }
  1220. da->list_dir_end();
  1221. }
  1222. for (int i = 0; i < p_dir->files.size(); i++) {
  1223. if (updated_dir && !p_dir->files[i]->verified) {
  1224. //this file was removed, add action to remove it
  1225. ItemAction ia;
  1226. ia.action = ItemAction::ACTION_FILE_REMOVE;
  1227. ia.dir = p_dir;
  1228. ia.file = p_dir->files[i]->file;
  1229. scan_actions.push_back(ia);
  1230. diff_nb_files--;
  1231. continue;
  1232. }
  1233. String path = cd.path_join(p_dir->files[i]->file);
  1234. if (import_extensions.has(p_dir->files[i]->file.get_extension().to_lower())) {
  1235. // Check here if file must be imported or not.
  1236. // Same logic as in _process_file_system, the last modifications dates
  1237. // needs to be trusted to prevent reading all the .import files and the md5
  1238. // each time the user switch back to Godot.
  1239. uint64_t mt = FileAccess::get_modified_time(path);
  1240. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  1241. 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)) {
  1242. ItemAction ia;
  1243. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1244. ia.dir = p_dir;
  1245. ia.file = p_dir->files[i]->file;
  1246. scan_actions.push_back(ia);
  1247. }
  1248. } else {
  1249. uint64_t mt = FileAccess::get_modified_time(path);
  1250. if (mt != p_dir->files[i]->modified_time) {
  1251. p_dir->files[i]->modified_time = mt; //save new time, but test for reload
  1252. ItemAction ia;
  1253. ia.action = ItemAction::ACTION_FILE_RELOAD;
  1254. ia.dir = p_dir;
  1255. ia.file = p_dir->files[i]->file;
  1256. scan_actions.push_back(ia);
  1257. }
  1258. }
  1259. p_progress.increment();
  1260. }
  1261. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1262. if ((updated_dir && !p_dir->subdirs[i]->verified) || _should_skip_directory(p_dir->subdirs[i]->get_path())) {
  1263. // Add all the files of the folder to be sure _update_scan_actions process the removed files
  1264. // for global class names.
  1265. diff_nb_files += _insert_actions_delete_files_directory(p_dir->subdirs[i]);
  1266. //this directory was removed or ignored, add action to remove it
  1267. ItemAction ia;
  1268. ia.action = ItemAction::ACTION_DIR_REMOVE;
  1269. ia.dir = p_dir->subdirs[i];
  1270. scan_actions.push_back(ia);
  1271. continue;
  1272. }
  1273. if (p_recursive) {
  1274. _scan_fs_changes(p_dir->get_subdir(i), p_progress);
  1275. }
  1276. }
  1277. nb_files_total = MAX(nb_files_total + diff_nb_files, 0);
  1278. }
  1279. void EditorFileSystem::_delete_internal_files(const String &p_file) {
  1280. if (FileAccess::exists(p_file + ".import")) {
  1281. List<String> paths;
  1282. ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
  1283. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1284. for (const String &E : paths) {
  1285. da->remove(E);
  1286. }
  1287. da->remove(p_file + ".import");
  1288. }
  1289. if (FileAccess::exists(p_file + ".uid")) {
  1290. DirAccess::remove_absolute(p_file + ".uid");
  1291. }
  1292. }
  1293. int EditorFileSystem::_insert_actions_delete_files_directory(EditorFileSystemDirectory *p_dir) {
  1294. int nb_files = 0;
  1295. for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
  1296. ItemAction ia;
  1297. ia.action = ItemAction::ACTION_FILE_REMOVE;
  1298. ia.dir = p_dir;
  1299. ia.file = fi->file;
  1300. scan_actions.push_back(ia);
  1301. nb_files++;
  1302. }
  1303. for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
  1304. nb_files += _insert_actions_delete_files_directory(sub_dir);
  1305. }
  1306. return nb_files;
  1307. }
  1308. void EditorFileSystem::_thread_func_sources(void *_userdata) {
  1309. EditorFileSystem *efs = (EditorFileSystem *)_userdata;
  1310. if (efs->filesystem) {
  1311. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  1312. ScanProgress sp;
  1313. sp.progress = &pr;
  1314. sp.hi = efs->nb_files_total;
  1315. efs->_scan_fs_changes(efs->filesystem, sp);
  1316. }
  1317. efs->scanning_changes_done.set();
  1318. }
  1319. void EditorFileSystem::_remove_invalid_global_class_names(const HashSet<String> &p_existing_class_names) {
  1320. List<StringName> global_classes;
  1321. bool must_save = false;
  1322. ScriptServer::get_global_class_list(&global_classes);
  1323. for (const StringName &class_name : global_classes) {
  1324. if (!p_existing_class_names.has(class_name)) {
  1325. ScriptServer::remove_global_class(class_name);
  1326. must_save = true;
  1327. }
  1328. }
  1329. if (must_save) {
  1330. ScriptServer::save_global_classes();
  1331. }
  1332. }
  1333. String EditorFileSystem::_get_file_by_class_name(EditorFileSystemDirectory *p_dir, const String &p_class_name, EditorFileSystemDirectory::FileInfo *&r_file_info) {
  1334. for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
  1335. if (fi->script_class_name == p_class_name) {
  1336. r_file_info = fi;
  1337. return p_dir->get_path().path_join(fi->file);
  1338. }
  1339. }
  1340. for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
  1341. String file = _get_file_by_class_name(sub_dir, p_class_name, r_file_info);
  1342. if (!file.is_empty()) {
  1343. return file;
  1344. }
  1345. }
  1346. r_file_info = nullptr;
  1347. return "";
  1348. }
  1349. void EditorFileSystem::scan_changes() {
  1350. if (first_scan || // Prevent a premature changes scan from inhibiting the first full scan
  1351. scanning || scanning_changes || thread.is_started()) {
  1352. scan_changes_pending = true;
  1353. set_process(true);
  1354. return;
  1355. }
  1356. _update_extensions();
  1357. sources_changed.clear();
  1358. scanning_changes = true;
  1359. scanning_changes_done.clear();
  1360. if (!use_threads) {
  1361. if (filesystem) {
  1362. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  1363. ScanProgress sp;
  1364. sp.progress = &pr;
  1365. sp.hi = nb_files_total;
  1366. scan_total = 0;
  1367. _scan_fs_changes(filesystem, sp);
  1368. if (_update_scan_actions()) {
  1369. emit_signal(SNAME("filesystem_changed"));
  1370. }
  1371. }
  1372. scanning_changes = false;
  1373. scanning_changes_done.set();
  1374. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1375. } else {
  1376. ERR_FAIL_COND(thread_sources.is_started());
  1377. set_process(true);
  1378. scan_total = 0;
  1379. Thread::Settings s;
  1380. s.priority = Thread::PRIORITY_LOW;
  1381. thread_sources.start(_thread_func_sources, this, s);
  1382. }
  1383. }
  1384. void EditorFileSystem::_notification(int p_what) {
  1385. switch (p_what) {
  1386. case NOTIFICATION_EXIT_TREE: {
  1387. Thread &active_thread = thread.is_started() ? thread : thread_sources;
  1388. if (use_threads && active_thread.is_started()) {
  1389. while (scanning) {
  1390. OS::get_singleton()->delay_usec(1000);
  1391. }
  1392. active_thread.wait_to_finish();
  1393. WARN_PRINT("Scan thread aborted...");
  1394. set_process(false);
  1395. }
  1396. if (filesystem) {
  1397. memdelete(filesystem);
  1398. }
  1399. if (new_filesystem) {
  1400. memdelete(new_filesystem);
  1401. }
  1402. filesystem = nullptr;
  1403. new_filesystem = nullptr;
  1404. } break;
  1405. case NOTIFICATION_PROCESS: {
  1406. if (use_threads) {
  1407. /** This hack exists because of the EditorProgress nature
  1408. * of processing events recursively. This needs to be rewritten
  1409. * at some point entirely, but in the meantime the following
  1410. * hack prevents deadlock on import.
  1411. */
  1412. static bool prevent_recursive_process_hack = false;
  1413. if (prevent_recursive_process_hack) {
  1414. break;
  1415. }
  1416. prevent_recursive_process_hack = true;
  1417. bool done_importing = false;
  1418. if (scanning_changes) {
  1419. if (scanning_changes_done.is_set()) {
  1420. set_process(false);
  1421. if (thread_sources.is_started()) {
  1422. thread_sources.wait_to_finish();
  1423. }
  1424. bool changed = _update_scan_actions();
  1425. // Set first_scan to false before the signals so the function doing_first_scan can return false
  1426. // in editor_node to start the export if needed.
  1427. first_scan = false;
  1428. ResourceImporter::load_on_startup = nullptr;
  1429. if (changed) {
  1430. emit_signal(SNAME("filesystem_changed"));
  1431. }
  1432. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1433. scanning_changes = false; // Changed to false here to prevent recursive triggering of scan thread.
  1434. done_importing = true;
  1435. }
  1436. } else if (!scanning && thread.is_started()) {
  1437. set_process(false);
  1438. if (filesystem) {
  1439. memdelete(filesystem);
  1440. }
  1441. filesystem = new_filesystem;
  1442. new_filesystem = nullptr;
  1443. thread.wait_to_finish();
  1444. _update_scan_actions();
  1445. // Update all icons so they are loaded for the FileSystemDock.
  1446. _update_files_icon_path();
  1447. // Set first_scan to false before the signals so the function doing_first_scan can return false
  1448. // in editor_node to start the export if needed.
  1449. first_scan = false;
  1450. ResourceImporter::load_on_startup = nullptr;
  1451. emit_signal(SNAME("filesystem_changed"));
  1452. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1453. }
  1454. if (done_importing && scan_changes_pending) {
  1455. scan_changes_pending = false;
  1456. scan_changes();
  1457. }
  1458. prevent_recursive_process_hack = false;
  1459. }
  1460. } break;
  1461. }
  1462. }
  1463. bool EditorFileSystem::is_scanning() const {
  1464. return scanning || scanning_changes || first_scan;
  1465. }
  1466. float EditorFileSystem::get_scanning_progress() const {
  1467. return scan_total;
  1468. }
  1469. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  1470. return filesystem;
  1471. }
  1472. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file) {
  1473. if (!p_dir) {
  1474. return; //none
  1475. }
  1476. p_file->store_line("::" + p_dir->get_path() + "::" + String::num(p_dir->modified_time));
  1477. for (int i = 0; i < p_dir->files.size(); i++) {
  1478. const EditorFileSystemDirectory::FileInfo *file_info = p_dir->files[i];
  1479. if (!file_info->import_group_file.is_empty()) {
  1480. group_file_cache.insert(file_info->import_group_file);
  1481. }
  1482. String type = file_info->type;
  1483. if (file_info->resource_script_class) {
  1484. type += "/" + String(file_info->resource_script_class);
  1485. }
  1486. PackedStringArray cache_string;
  1487. cache_string.append(file_info->file);
  1488. cache_string.append(type);
  1489. cache_string.append(itos(file_info->uid));
  1490. cache_string.append(itos(file_info->modified_time));
  1491. cache_string.append(itos(file_info->import_modified_time));
  1492. cache_string.append(itos(file_info->import_valid));
  1493. cache_string.append(file_info->import_group_file);
  1494. 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) }));
  1495. cache_string.append(String("<>").join(file_info->deps));
  1496. p_file->store_line(String("::").join(cache_string));
  1497. }
  1498. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1499. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  1500. }
  1501. }
  1502. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  1503. //todo make faster
  1504. if (!filesystem || scanning) {
  1505. return false;
  1506. }
  1507. String f = ProjectSettings::get_singleton()->localize_path(p_file);
  1508. // Note: Only checks if base directory is case sensitive.
  1509. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1510. bool fs_case_sensitive = dir->is_case_sensitive("res://");
  1511. if (!f.begins_with("res://")) {
  1512. return false;
  1513. }
  1514. f = f.substr(6, f.length());
  1515. f = f.replace("\\", "/");
  1516. Vector<String> path = f.split("/");
  1517. if (path.size() == 0) {
  1518. return false;
  1519. }
  1520. String file = path[path.size() - 1];
  1521. path.resize(path.size() - 1);
  1522. EditorFileSystemDirectory *fs = filesystem;
  1523. for (int i = 0; i < path.size(); i++) {
  1524. if (path[i].begins_with(".")) {
  1525. return false;
  1526. }
  1527. int idx = -1;
  1528. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1529. if (fs_case_sensitive) {
  1530. if (fs->get_subdir(j)->get_name() == path[i]) {
  1531. idx = j;
  1532. break;
  1533. }
  1534. } else {
  1535. if (fs->get_subdir(j)->get_name().to_lower() == path[i].to_lower()) {
  1536. idx = j;
  1537. break;
  1538. }
  1539. }
  1540. }
  1541. if (idx == -1) {
  1542. // Only create a missing directory in memory when it exists on disk.
  1543. if (!dir->dir_exists(fs->get_path().path_join(path[i]))) {
  1544. return false;
  1545. }
  1546. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  1547. efsd->name = path[i];
  1548. efsd->parent = fs;
  1549. int idx2 = 0;
  1550. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1551. if (efsd->name.filenocasecmp_to(fs->get_subdir(j)->get_name()) < 0) {
  1552. break;
  1553. }
  1554. idx2++;
  1555. }
  1556. if (idx2 == fs->get_subdir_count()) {
  1557. fs->subdirs.push_back(efsd);
  1558. } else {
  1559. fs->subdirs.insert(idx2, efsd);
  1560. }
  1561. fs = efsd;
  1562. } else {
  1563. fs = fs->get_subdir(idx);
  1564. }
  1565. }
  1566. int cpos = -1;
  1567. for (int i = 0; i < fs->files.size(); i++) {
  1568. if (fs->files[i]->file == file) {
  1569. cpos = i;
  1570. break;
  1571. }
  1572. }
  1573. r_file_pos = cpos;
  1574. *r_d = fs;
  1575. return cpos != -1;
  1576. }
  1577. String EditorFileSystem::get_file_type(const String &p_file) const {
  1578. EditorFileSystemDirectory *fs = nullptr;
  1579. int cpos = -1;
  1580. if (!_find_file(p_file, &fs, cpos)) {
  1581. return "";
  1582. }
  1583. return fs->files[cpos]->type;
  1584. }
  1585. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  1586. if (!filesystem || scanning) {
  1587. return nullptr;
  1588. }
  1589. EditorFileSystemDirectory *fs = nullptr;
  1590. int cpos = -1;
  1591. if (!_find_file(p_file, &fs, cpos)) {
  1592. return nullptr;
  1593. }
  1594. if (r_index) {
  1595. *r_index = cpos;
  1596. }
  1597. return fs;
  1598. }
  1599. EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
  1600. if (!filesystem || scanning) {
  1601. return nullptr;
  1602. }
  1603. String f = ProjectSettings::get_singleton()->localize_path(p_path);
  1604. if (!f.begins_with("res://")) {
  1605. return nullptr;
  1606. }
  1607. f = f.substr(6, f.length());
  1608. f = f.replace("\\", "/");
  1609. if (f.is_empty()) {
  1610. return filesystem;
  1611. }
  1612. if (f.ends_with("/")) {
  1613. f = f.substr(0, f.length() - 1);
  1614. }
  1615. Vector<String> path = f.split("/");
  1616. if (path.size() == 0) {
  1617. return nullptr;
  1618. }
  1619. EditorFileSystemDirectory *fs = filesystem;
  1620. for (int i = 0; i < path.size(); i++) {
  1621. int idx = -1;
  1622. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1623. if (fs->get_subdir(j)->get_name() == path[i]) {
  1624. idx = j;
  1625. break;
  1626. }
  1627. }
  1628. if (idx == -1) {
  1629. return nullptr;
  1630. } else {
  1631. fs = fs->get_subdir(idx);
  1632. }
  1633. }
  1634. return fs;
  1635. }
  1636. void EditorFileSystem::_save_late_updated_files() {
  1637. //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
  1638. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  1639. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  1640. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  1641. for (const String &E : late_update_files) {
  1642. f->store_line(E);
  1643. }
  1644. }
  1645. Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
  1646. // Avoid error spam on first opening of a not yet imported project by treating the following situation
  1647. // as a benign one, not letting the file open error happen: the resource is of an importable type but
  1648. // it has not been imported yet.
  1649. if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
  1650. const String &internal_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
  1651. if (!internal_path.is_empty() && !FileAccess::exists(internal_path)) { // If path is empty (error), keep the code flow to the error.
  1652. return Vector<String>();
  1653. }
  1654. }
  1655. List<String> deps;
  1656. ResourceLoader::get_dependencies(p_path, &deps);
  1657. Vector<String> ret;
  1658. for (const String &E : deps) {
  1659. ret.push_back(E);
  1660. }
  1661. return ret;
  1662. }
  1663. String EditorFileSystem::_get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const {
  1664. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1665. if (ScriptServer::get_language(i)->handles_global_class_type(p_type)) {
  1666. String global_name;
  1667. String extends;
  1668. String icon_path;
  1669. global_name = ScriptServer::get_language(i)->get_global_class_name(p_path, &extends, &icon_path);
  1670. *r_extends = extends;
  1671. *r_icon_path = icon_path;
  1672. return global_name;
  1673. }
  1674. }
  1675. *r_extends = String();
  1676. *r_icon_path = String();
  1677. return String();
  1678. }
  1679. void EditorFileSystem::_update_file_icon_path(EditorFileSystemDirectory::FileInfo *file_info) {
  1680. String icon_path;
  1681. if (file_info->resource_script_class != StringName()) {
  1682. icon_path = EditorNode::get_editor_data().script_class_get_icon_path(file_info->resource_script_class);
  1683. } else if (file_info->script_class_icon_path.is_empty() && !file_info->deps.is_empty()) {
  1684. const String &script_dep = file_info->deps[0]; // Assuming the first dependency is a script.
  1685. const String &script_path = script_dep.contains("::") ? script_dep.get_slice("::", 2) : script_dep;
  1686. if (!script_path.is_empty()) {
  1687. String *cached = file_icon_cache.getptr(script_path);
  1688. if (cached) {
  1689. icon_path = *cached;
  1690. } else {
  1691. if (ClassDB::is_parent_class(ResourceLoader::get_resource_type(script_path), SNAME("Script"))) {
  1692. int script_file;
  1693. EditorFileSystemDirectory *efsd = find_file(script_path, &script_file);
  1694. if (efsd) {
  1695. icon_path = efsd->files[script_file]->script_class_icon_path;
  1696. }
  1697. }
  1698. file_icon_cache.insert(script_path, icon_path);
  1699. }
  1700. }
  1701. }
  1702. if (icon_path.is_empty() && !file_info->type.is_empty()) {
  1703. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(file_info->type);
  1704. if (icon.is_valid()) {
  1705. icon_path = icon->get_path();
  1706. }
  1707. }
  1708. file_info->icon_path = icon_path;
  1709. }
  1710. void EditorFileSystem::_update_files_icon_path(EditorFileSystemDirectory *edp) {
  1711. if (!edp) {
  1712. edp = filesystem;
  1713. file_icon_cache.clear();
  1714. }
  1715. for (EditorFileSystemDirectory *sub_dir : edp->subdirs) {
  1716. _update_files_icon_path(sub_dir);
  1717. }
  1718. for (EditorFileSystemDirectory::FileInfo *fi : edp->files) {
  1719. _update_file_icon_path(fi);
  1720. }
  1721. }
  1722. void EditorFileSystem::_update_script_classes() {
  1723. if (update_script_paths.is_empty()) {
  1724. // Ensure the global class file is always present; it's essential for exports to work.
  1725. if (!FileAccess::exists(ProjectSettings::get_singleton()->get_global_class_list_path())) {
  1726. ScriptServer::save_global_classes();
  1727. }
  1728. return;
  1729. }
  1730. {
  1731. MutexLock update_script_lock(update_script_mutex);
  1732. EditorProgress *ep = nullptr;
  1733. if (update_script_paths.size() > 1) {
  1734. if (MessageQueue::get_singleton()->is_flushing()) {
  1735. // Use background progress when message queue is flushing.
  1736. ep = memnew(EditorProgress("update_scripts_classes", TTR("Registering global classes..."), update_script_paths.size(), false, true));
  1737. } else {
  1738. ep = memnew(EditorProgress("update_scripts_classes", TTR("Registering global classes..."), update_script_paths.size()));
  1739. }
  1740. }
  1741. int step_count = 0;
  1742. for (const KeyValue<String, ScriptInfo> &E : update_script_paths) {
  1743. _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);
  1744. if (ep) {
  1745. ep->step(E.value.script_class_name, step_count++, false);
  1746. }
  1747. }
  1748. memdelete_notnull(ep);
  1749. update_script_paths.clear();
  1750. }
  1751. ScriptServer::save_global_classes();
  1752. EditorNode::get_editor_data().script_class_save_icon_paths();
  1753. emit_signal("script_classes_updated");
  1754. // Rescan custom loaders and savers.
  1755. // Doing the following here because the `filesystem_changed` signal fires multiple times and isn't always followed by script classes update.
  1756. // So I thought it's better to do this when script classes really get updated
  1757. ResourceLoader::remove_custom_loaders();
  1758. ResourceLoader::add_custom_loaders();
  1759. ResourceSaver::remove_custom_savers();
  1760. ResourceSaver::add_custom_savers();
  1761. }
  1762. void EditorFileSystem::_update_script_documentation() {
  1763. if (update_script_paths_documentation.is_empty()) {
  1764. return;
  1765. }
  1766. MutexLock update_script_lock(update_script_mutex);
  1767. EditorProgress *ep = nullptr;
  1768. if (update_script_paths_documentation.size() > 1) {
  1769. if (MessageQueue::get_singleton()->is_flushing()) {
  1770. // Use background progress when message queue is flushing.
  1771. ep = memnew(EditorProgress("update_script_paths_documentation", TTR("Updating scripts documentation"), update_script_paths_documentation.size(), false, true));
  1772. } else {
  1773. ep = memnew(EditorProgress("update_script_paths_documentation", TTR("Updating scripts documentation"), update_script_paths_documentation.size()));
  1774. }
  1775. }
  1776. int step_count = 0;
  1777. for (const String &path : update_script_paths_documentation) {
  1778. int index = -1;
  1779. EditorFileSystemDirectory *efd = find_file(path, &index);
  1780. if (!efd || index < 0) {
  1781. // The file was removed
  1782. continue;
  1783. }
  1784. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1785. ScriptLanguage *lang = ScriptServer::get_language(i);
  1786. if (lang->supports_documentation() && efd->files[index]->type == lang->get_type()) {
  1787. bool should_reload_script = _should_reload_script(path);
  1788. Ref<Script> scr = ResourceLoader::load(path);
  1789. if (scr.is_null()) {
  1790. continue;
  1791. }
  1792. if (should_reload_script) {
  1793. // Reloading the script from disk. Otherwise, the ResourceLoader::load will
  1794. // return the last loaded version of the script (without the modifications).
  1795. scr->reload_from_file();
  1796. }
  1797. for (const DocData::ClassDoc &cd : scr->get_documentation()) {
  1798. EditorHelp::get_doc_data()->add_doc(cd);
  1799. if (!first_scan) {
  1800. // Update the documentation in the Script Editor if it is open.
  1801. ScriptEditor::get_singleton()->update_doc(cd.name);
  1802. }
  1803. }
  1804. }
  1805. }
  1806. if (ep) {
  1807. ep->step(efd->files[index]->file, step_count++, false);
  1808. }
  1809. }
  1810. memdelete_notnull(ep);
  1811. update_script_paths_documentation.clear();
  1812. }
  1813. bool EditorFileSystem::_should_reload_script(const String &p_path) {
  1814. if (first_scan) {
  1815. return false;
  1816. }
  1817. Ref<Script> scr = ResourceCache::get_ref(p_path);
  1818. if (scr.is_null()) {
  1819. // Not a script or not already loaded.
  1820. return false;
  1821. }
  1822. // Scripts are reloaded via the script editor if they are currently opened.
  1823. if (ScriptEditor::get_singleton()->get_open_scripts().has(scr)) {
  1824. return false;
  1825. }
  1826. return true;
  1827. }
  1828. void EditorFileSystem::_process_update_pending() {
  1829. _update_script_classes();
  1830. // Parse documentation second, as it requires the class names to be loaded
  1831. // because _update_script_documentation loads the scripts completely.
  1832. _update_script_documentation();
  1833. _update_pending_scene_groups();
  1834. }
  1835. 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) {
  1836. MutexLock update_script_lock(update_script_mutex);
  1837. ScriptInfo si;
  1838. si.type = p_type;
  1839. si.script_class_name = p_script_class_name;
  1840. si.script_class_extends = p_script_class_extends;
  1841. si.script_class_icon_path = p_script_class_icon_path;
  1842. update_script_paths.insert(p_path, si);
  1843. update_script_paths_documentation.insert(p_path);
  1844. }
  1845. void EditorFileSystem::_update_scene_groups() {
  1846. if (update_scene_paths.is_empty()) {
  1847. return;
  1848. }
  1849. EditorProgress *ep = nullptr;
  1850. if (update_scene_paths.size() > 20) {
  1851. ep = memnew(EditorProgress("update_scene_groups", TTR("Updating Scene Groups"), update_scene_paths.size()));
  1852. }
  1853. int step_count = 0;
  1854. {
  1855. MutexLock update_scene_lock(update_scene_mutex);
  1856. for (const String &path : update_scene_paths) {
  1857. ProjectSettings::get_singleton()->remove_scene_groups_cache(path);
  1858. int index = -1;
  1859. EditorFileSystemDirectory *efd = find_file(path, &index);
  1860. if (!efd || index < 0) {
  1861. // The file was removed.
  1862. continue;
  1863. }
  1864. const HashSet<StringName> scene_groups = PackedScene::get_scene_groups(path);
  1865. if (!scene_groups.is_empty()) {
  1866. ProjectSettings::get_singleton()->add_scene_groups_cache(path, scene_groups);
  1867. }
  1868. if (ep) {
  1869. ep->step(efd->files[index]->file, step_count++, false);
  1870. }
  1871. }
  1872. memdelete_notnull(ep);
  1873. update_scene_paths.clear();
  1874. }
  1875. ProjectSettings::get_singleton()->save_scene_groups_cache();
  1876. }
  1877. void EditorFileSystem::_update_pending_scene_groups() {
  1878. if (!FileAccess::exists(ProjectSettings::get_singleton()->get_scene_groups_cache_path())) {
  1879. _get_all_scenes(get_filesystem(), update_scene_paths);
  1880. _update_scene_groups();
  1881. } else if (!update_scene_paths.is_empty()) {
  1882. _update_scene_groups();
  1883. }
  1884. }
  1885. void EditorFileSystem::_queue_update_scene_groups(const String &p_path) {
  1886. MutexLock update_scene_lock(update_scene_mutex);
  1887. update_scene_paths.insert(p_path);
  1888. }
  1889. void EditorFileSystem::_get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet<String> &r_list) {
  1890. for (int i = 0; i < p_dir->get_file_count(); i++) {
  1891. if (p_dir->get_file_type(i) == SNAME("PackedScene")) {
  1892. r_list.insert(p_dir->get_file_path(i));
  1893. }
  1894. }
  1895. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1896. _get_all_scenes(p_dir->get_subdir(i), r_list);
  1897. }
  1898. }
  1899. void EditorFileSystem::update_file(const String &p_file) {
  1900. ERR_FAIL_COND(p_file.is_empty());
  1901. update_files({ p_file });
  1902. }
  1903. void EditorFileSystem::update_files(const Vector<String> &p_script_paths) {
  1904. bool updated = false;
  1905. bool update_files_icon_cache = false;
  1906. Vector<EditorFileSystemDirectory::FileInfo *> files_to_update_icon_path;
  1907. for (const String &file : p_script_paths) {
  1908. ERR_CONTINUE(file.is_empty());
  1909. EditorFileSystemDirectory *fs = nullptr;
  1910. int cpos = -1;
  1911. if (!_find_file(file, &fs, cpos)) {
  1912. if (!fs) {
  1913. continue;
  1914. }
  1915. }
  1916. if (!FileAccess::exists(file)) {
  1917. //was removed
  1918. _delete_internal_files(file);
  1919. if (cpos != -1) { // Might've never been part of the editor file system (*.* files deleted in Open dialog).
  1920. if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  1921. if (ResourceUID::get_singleton()->has_id(fs->files[cpos]->uid)) {
  1922. ResourceUID::get_singleton()->remove_id(fs->files[cpos]->uid);
  1923. }
  1924. }
  1925. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
  1926. _queue_update_script_class(file, fs->files[cpos]->type, "", "", "");
  1927. if (!fs->files[cpos]->script_class_icon_path.is_empty()) {
  1928. update_files_icon_cache = true;
  1929. }
  1930. }
  1931. if (fs->files[cpos]->type == SNAME("PackedScene")) {
  1932. _queue_update_scene_groups(file);
  1933. }
  1934. memdelete(fs->files[cpos]);
  1935. fs->files.remove_at(cpos);
  1936. updated = true;
  1937. }
  1938. } else {
  1939. String type = ResourceLoader::get_resource_type(file);
  1940. if (type.is_empty() && textfile_extensions.has(file.get_extension())) {
  1941. type = "TextFile";
  1942. }
  1943. if (type.is_empty() && other_file_extensions.has(file.get_extension())) {
  1944. type = "OtherFile";
  1945. }
  1946. String script_class = ResourceLoader::get_resource_script_class(file);
  1947. ResourceUID::ID uid = ResourceLoader::get_resource_uid(file);
  1948. if (cpos == -1) {
  1949. // The file did not exist, it was added.
  1950. int idx = 0;
  1951. String file_name = file.get_file();
  1952. for (int i = 0; i < fs->files.size(); i++) {
  1953. if (file.filenocasecmp_to(fs->files[i]->file) < 0) {
  1954. break;
  1955. }
  1956. idx++;
  1957. }
  1958. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1959. fi->file = file_name;
  1960. fi->import_modified_time = 0;
  1961. fi->import_valid = (type == "TextFile" || type == "OtherFile") ? true : ResourceLoader::is_import_valid(file);
  1962. fi->import_md5 = "";
  1963. fi->import_dest_paths = Vector<String>();
  1964. if (idx == fs->files.size()) {
  1965. fs->files.push_back(fi);
  1966. } else {
  1967. fs->files.insert(idx, fi);
  1968. }
  1969. cpos = idx;
  1970. } else {
  1971. //the file exists and it was updated, and was not added in this step.
  1972. //this means we must force upon next restart to scan it again, to get proper type and dependencies
  1973. late_update_files.insert(file);
  1974. _save_late_updated_files(); //files need to be updated in the re-scan
  1975. }
  1976. const String old_script_class_icon_path = fs->files[cpos]->script_class_icon_path;
  1977. const String old_class_name = fs->files[cpos]->script_class_name;
  1978. fs->files[cpos]->type = type;
  1979. fs->files[cpos]->resource_script_class = script_class;
  1980. fs->files[cpos]->uid = uid;
  1981. 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);
  1982. fs->files[cpos]->import_group_file = ResourceLoader::get_import_group_file(file);
  1983. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  1984. fs->files[cpos]->deps = _get_dependencies(file);
  1985. fs->files[cpos]->import_valid = (type == "TextFile" || type == "OtherFile") ? true : ResourceLoader::is_import_valid(file);
  1986. if (uid != ResourceUID::INVALID_ID) {
  1987. if (ResourceUID::get_singleton()->has_id(uid)) {
  1988. ResourceUID::get_singleton()->set_id(uid, file);
  1989. } else {
  1990. ResourceUID::get_singleton()->add_id(uid, file);
  1991. }
  1992. ResourceUID::get_singleton()->update_cache();
  1993. }
  1994. // Update preview
  1995. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  1996. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
  1997. _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);
  1998. }
  1999. if (fs->files[cpos]->type == SNAME("PackedScene")) {
  2000. _queue_update_scene_groups(file);
  2001. }
  2002. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Resource"))) {
  2003. files_to_update_icon_path.push_back(fs->files[cpos]);
  2004. } else if (old_script_class_icon_path != fs->files[cpos]->script_class_icon_path) {
  2005. update_files_icon_cache = true;
  2006. }
  2007. // Restore another script as the global class name if multiple scripts had the same old class name.
  2008. if (!old_class_name.is_empty() && fs->files[cpos]->script_class_name != old_class_name && ClassDB::is_parent_class(type, SNAME("Script"))) {
  2009. EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
  2010. String old_file = _get_file_by_class_name(filesystem, old_class_name, old_fi);
  2011. if (!old_file.is_empty() && old_fi) {
  2012. _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);
  2013. }
  2014. }
  2015. updated = true;
  2016. }
  2017. }
  2018. if (updated) {
  2019. if (update_files_icon_cache) {
  2020. _update_files_icon_path();
  2021. } else {
  2022. for (EditorFileSystemDirectory::FileInfo *fi : files_to_update_icon_path) {
  2023. _update_file_icon_path(fi);
  2024. }
  2025. }
  2026. if (!is_scanning()) {
  2027. _process_update_pending();
  2028. }
  2029. if (!filesystem_changed_queued) {
  2030. filesystem_changed_queued = true;
  2031. callable_mp(this, &EditorFileSystem::_notify_filesystem_changed).call_deferred();
  2032. }
  2033. }
  2034. }
  2035. void EditorFileSystem::_notify_filesystem_changed() {
  2036. emit_signal("filesystem_changed");
  2037. filesystem_changed_queued = false;
  2038. }
  2039. HashSet<String> EditorFileSystem::get_valid_extensions() const {
  2040. return valid_extensions;
  2041. }
  2042. 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) {
  2043. ScriptServer::remove_global_class_by_path(p_search_path); // First remove, just in case it changed
  2044. if (p_script_class_name.is_empty()) {
  2045. return;
  2046. }
  2047. String lang;
  2048. for (int j = 0; j < ScriptServer::get_language_count(); j++) {
  2049. if (ScriptServer::get_language(j)->handles_global_class_type(p_type)) {
  2050. lang = ScriptServer::get_language(j)->get_name();
  2051. break;
  2052. }
  2053. }
  2054. if (lang.is_empty()) {
  2055. return; // No lang found that can handle this global class
  2056. }
  2057. ScriptServer::add_global_class(p_script_class_name, p_script_class_extends, lang, p_target_path);
  2058. EditorNode::get_editor_data().script_class_set_icon_path(p_script_class_name, p_script_class_icon_path);
  2059. EditorNode::get_editor_data().script_class_set_name(p_target_path, p_script_class_name);
  2060. }
  2061. void EditorFileSystem::register_global_class_script(const String &p_search_path, const String &p_target_path) {
  2062. int index_file;
  2063. EditorFileSystemDirectory *efsd = find_file(p_search_path, &index_file);
  2064. if (efsd) {
  2065. const EditorFileSystemDirectory::FileInfo *fi = efsd->files[index_file];
  2066. 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);
  2067. } else {
  2068. ScriptServer::remove_global_class_by_path(p_search_path);
  2069. }
  2070. }
  2071. Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) {
  2072. String importer_name;
  2073. HashMap<String, HashMap<StringName, Variant>> source_file_options;
  2074. HashMap<String, ResourceUID::ID> uids;
  2075. HashMap<String, String> base_paths;
  2076. for (int i = 0; i < p_files.size(); i++) {
  2077. Ref<ConfigFile> config;
  2078. config.instantiate();
  2079. Error err = config->load(p_files[i] + ".import");
  2080. ERR_CONTINUE(err != OK);
  2081. ERR_CONTINUE(!config->has_section_key("remap", "importer"));
  2082. String file_importer_name = config->get_value("remap", "importer");
  2083. ERR_CONTINUE(file_importer_name.is_empty());
  2084. if (!importer_name.is_empty() && importer_name != file_importer_name) {
  2085. EditorNode::get_singleton()->show_warning(vformat(TTR("There are multiple importers for different types pointing to file %s, import aborted"), p_group_file));
  2086. ERR_FAIL_V(ERR_FILE_CORRUPT);
  2087. }
  2088. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  2089. if (config->has_section_key("remap", "uid")) {
  2090. String uidt = config->get_value("remap", "uid");
  2091. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  2092. }
  2093. uids[p_files[i]] = uid;
  2094. source_file_options[p_files[i]] = HashMap<StringName, Variant>();
  2095. importer_name = file_importer_name;
  2096. if (importer_name == "keep" || importer_name == "skip") {
  2097. continue; //do nothing
  2098. }
  2099. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2100. ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT);
  2101. List<ResourceImporter::ImportOption> options;
  2102. importer->get_import_options(p_files[i], &options);
  2103. //set default values
  2104. for (const ResourceImporter::ImportOption &E : options) {
  2105. source_file_options[p_files[i]][E.option.name] = E.default_value;
  2106. }
  2107. if (config->has_section("params")) {
  2108. List<String> sk;
  2109. config->get_section_keys("params", &sk);
  2110. for (const String &param : sk) {
  2111. Variant value = config->get_value("params", param);
  2112. //override with whatever is in file
  2113. source_file_options[p_files[i]][param] = value;
  2114. }
  2115. }
  2116. base_paths[p_files[i]] = ResourceFormatImporter::get_singleton()->get_import_base_path(p_files[i]);
  2117. }
  2118. if (importer_name == "keep" || importer_name == "skip") {
  2119. return OK; // (do nothing)
  2120. }
  2121. ERR_FAIL_COND_V(importer_name.is_empty(), ERR_UNCONFIGURED);
  2122. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2123. Error err = importer->import_group_file(p_group_file, source_file_options, base_paths);
  2124. //all went well, overwrite config files with proper remaps and md5s
  2125. for (const KeyValue<String, HashMap<StringName, Variant>> &E : source_file_options) {
  2126. const String &file = E.key;
  2127. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(file);
  2128. Vector<String> dest_paths;
  2129. ResourceUID::ID uid = uids[file];
  2130. {
  2131. Ref<FileAccess> f = FileAccess::open(file + ".import", FileAccess::WRITE);
  2132. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'.");
  2133. //write manually, as order matters ([remap] has to go first for performance).
  2134. f->store_line("[remap]");
  2135. f->store_line("");
  2136. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  2137. int version = importer->get_format_version();
  2138. if (version > 0) {
  2139. f->store_line("importer_version=" + itos(version));
  2140. }
  2141. if (!importer->get_resource_type().is_empty()) {
  2142. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  2143. }
  2144. if (uid == ResourceUID::INVALID_ID) {
  2145. uid = ResourceUID::get_singleton()->create_id();
  2146. }
  2147. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
  2148. if (err == OK) {
  2149. String path = base_path + "." + importer->get_save_extension();
  2150. f->store_line("path=\"" + path + "\"");
  2151. dest_paths.push_back(path);
  2152. }
  2153. f->store_line("group_file=" + Variant(p_group_file).get_construct_string());
  2154. if (err == OK) {
  2155. f->store_line("valid=true");
  2156. } else {
  2157. f->store_line("valid=false");
  2158. }
  2159. f->store_line("[deps]\n");
  2160. f->store_line("");
  2161. f->store_line("source_file=" + Variant(file).get_construct_string());
  2162. if (dest_paths.size()) {
  2163. Array dp;
  2164. for (int i = 0; i < dest_paths.size(); i++) {
  2165. dp.push_back(dest_paths[i]);
  2166. }
  2167. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  2168. }
  2169. f->store_line("[params]");
  2170. f->store_line("");
  2171. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  2172. List<ResourceImporter::ImportOption> options;
  2173. importer->get_import_options(file, &options);
  2174. //set default values
  2175. for (const ResourceImporter::ImportOption &F : options) {
  2176. String base = F.option.name;
  2177. Variant v = F.default_value;
  2178. if (source_file_options[file].has(base)) {
  2179. v = source_file_options[file][base];
  2180. }
  2181. String value;
  2182. VariantWriter::write_to_string(v, value);
  2183. f->store_line(base + "=" + value);
  2184. }
  2185. }
  2186. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  2187. {
  2188. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  2189. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  2190. md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\"");
  2191. if (dest_paths.size()) {
  2192. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  2193. }
  2194. }
  2195. EditorFileSystemDirectory *fs = nullptr;
  2196. int cpos = -1;
  2197. bool found = _find_file(file, &fs, cpos);
  2198. ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, vformat("Can't find file '%s' during group reimport.", file));
  2199. //update modified times, to avoid reimport
  2200. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  2201. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(file + ".import");
  2202. fs->files[cpos]->import_md5 = FileAccess::get_md5(file + ".import");
  2203. fs->files[cpos]->import_dest_paths = dest_paths;
  2204. fs->files[cpos]->deps = _get_dependencies(file);
  2205. fs->files[cpos]->uid = uid;
  2206. fs->files[cpos]->type = importer->get_resource_type();
  2207. if (fs->files[cpos]->type == "" && textfile_extensions.has(file.get_extension())) {
  2208. fs->files[cpos]->type = "TextFile";
  2209. }
  2210. if (fs->files[cpos]->type == "" && other_file_extensions.has(file.get_extension())) {
  2211. fs->files[cpos]->type = "OtherFile";
  2212. }
  2213. fs->files[cpos]->import_valid = err == OK;
  2214. if (ResourceUID::get_singleton()->has_id(uid)) {
  2215. ResourceUID::get_singleton()->set_id(uid, file);
  2216. } else {
  2217. ResourceUID::get_singleton()->add_id(uid, file);
  2218. }
  2219. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  2220. //to reload properly
  2221. Ref<Resource> r = ResourceCache::get_ref(file);
  2222. if (r.is_valid()) {
  2223. if (!r->get_import_path().is_empty()) {
  2224. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(file);
  2225. r->set_import_path(dst_path);
  2226. r->set_import_last_modified_time(0);
  2227. }
  2228. }
  2229. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  2230. }
  2231. return err;
  2232. }
  2233. 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) {
  2234. print_verbose(vformat("EditorFileSystem: Importing file: %s", p_file));
  2235. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  2236. EditorFileSystemDirectory *fs = nullptr;
  2237. int cpos = -1;
  2238. if (p_update_file_system) {
  2239. bool found = _find_file(p_file, &fs, cpos);
  2240. ERR_FAIL_COND_V_MSG(!found, ERR_FILE_NOT_FOUND, vformat("Can't find file '%s' during file reimport.", p_file));
  2241. }
  2242. //try to obtain existing params
  2243. HashMap<StringName, Variant> params = p_custom_options;
  2244. String importer_name; //empty by default though
  2245. if (!p_custom_importer.is_empty()) {
  2246. importer_name = p_custom_importer;
  2247. }
  2248. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  2249. Variant generator_parameters;
  2250. if (p_generator_parameters) {
  2251. generator_parameters = *p_generator_parameters;
  2252. }
  2253. if (FileAccess::exists(p_file + ".import")) {
  2254. //use existing
  2255. Ref<ConfigFile> cf;
  2256. cf.instantiate();
  2257. Error err = cf->load(p_file + ".import");
  2258. if (err == OK) {
  2259. if (cf->has_section("params")) {
  2260. List<String> sk;
  2261. cf->get_section_keys("params", &sk);
  2262. for (const String &E : sk) {
  2263. if (!params.has(E)) {
  2264. params[E] = cf->get_value("params", E);
  2265. }
  2266. }
  2267. }
  2268. if (cf->has_section("remap")) {
  2269. if (p_custom_importer.is_empty()) {
  2270. importer_name = cf->get_value("remap", "importer");
  2271. }
  2272. if (cf->has_section_key("remap", "uid")) {
  2273. String uidt = cf->get_value("remap", "uid");
  2274. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  2275. }
  2276. if (!p_generator_parameters) {
  2277. if (cf->has_section_key("remap", "generator_parameters")) {
  2278. generator_parameters = cf->get_value("remap", "generator_parameters");
  2279. }
  2280. }
  2281. }
  2282. }
  2283. }
  2284. if (importer_name == "keep" || importer_name == "skip") {
  2285. //keep files, do nothing.
  2286. if (p_update_file_system) {
  2287. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  2288. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  2289. fs->files[cpos]->import_md5 = FileAccess::get_md5(p_file + ".import");
  2290. fs->files[cpos]->import_dest_paths = Vector<String>();
  2291. fs->files[cpos]->deps.clear();
  2292. fs->files[cpos]->type = "";
  2293. fs->files[cpos]->import_valid = false;
  2294. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  2295. }
  2296. return OK;
  2297. }
  2298. Ref<ResourceImporter> importer;
  2299. bool load_default = false;
  2300. //find the importer
  2301. if (!importer_name.is_empty()) {
  2302. importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2303. }
  2304. if (importer.is_null()) {
  2305. //not found by name, find by extension
  2306. importer = ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_file.get_extension());
  2307. load_default = true;
  2308. if (importer.is_null()) {
  2309. 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.");
  2310. }
  2311. }
  2312. if (FileAccess::exists(p_file + ".import")) {
  2313. // We only want to handle compat for existing files, not new ones.
  2314. importer->handle_compatibility_options(params);
  2315. }
  2316. //mix with default params, in case a parameter is missing
  2317. List<ResourceImporter::ImportOption> opts;
  2318. importer->get_import_options(p_file, &opts);
  2319. for (const ResourceImporter::ImportOption &E : opts) {
  2320. if (!params.has(E.option.name)) { //this one is not present
  2321. params[E.option.name] = E.default_value;
  2322. }
  2323. }
  2324. if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  2325. //use defaults if exist
  2326. Dictionary d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
  2327. List<Variant> v;
  2328. d.get_key_list(&v);
  2329. for (const Variant &E : v) {
  2330. params[E] = d[E];
  2331. }
  2332. }
  2333. if (uid == ResourceUID::INVALID_ID) {
  2334. uid = ResourceUID::get_singleton()->create_id();
  2335. }
  2336. //finally, perform import!!
  2337. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
  2338. List<String> import_variants;
  2339. List<String> gen_files;
  2340. Variant meta;
  2341. Error err = importer->import(uid, p_file, base_path, params, &import_variants, &gen_files, &meta);
  2342. // As import is complete, save the .import file.
  2343. Vector<String> dest_paths;
  2344. {
  2345. Ref<FileAccess> f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
  2346. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open file from path '" + p_file + ".import'.");
  2347. // Write manually, as order matters ([remap] has to go first for performance).
  2348. f->store_line("[remap]");
  2349. f->store_line("");
  2350. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  2351. int version = importer->get_format_version();
  2352. if (version > 0) {
  2353. f->store_line("importer_version=" + itos(version));
  2354. }
  2355. if (!importer->get_resource_type().is_empty()) {
  2356. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  2357. }
  2358. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
  2359. if (err == OK) {
  2360. if (importer->get_save_extension().is_empty()) {
  2361. //no path
  2362. } else if (import_variants.size()) {
  2363. //import with variants
  2364. for (const String &E : import_variants) {
  2365. String path = base_path.c_escape() + "." + E + "." + importer->get_save_extension();
  2366. f->store_line("path." + E + "=\"" + path + "\"");
  2367. dest_paths.push_back(path);
  2368. }
  2369. } else {
  2370. String path = base_path + "." + importer->get_save_extension();
  2371. f->store_line("path=\"" + path + "\"");
  2372. dest_paths.push_back(path);
  2373. }
  2374. } else {
  2375. f->store_line("valid=false");
  2376. }
  2377. if (meta != Variant()) {
  2378. f->store_line("metadata=" + meta.get_construct_string());
  2379. }
  2380. if (generator_parameters != Variant()) {
  2381. f->store_line("generator_parameters=" + generator_parameters.get_construct_string());
  2382. }
  2383. f->store_line("");
  2384. f->store_line("[deps]\n");
  2385. if (gen_files.size()) {
  2386. Array genf;
  2387. for (const String &E : gen_files) {
  2388. genf.push_back(E);
  2389. dest_paths.push_back(E);
  2390. }
  2391. String value;
  2392. VariantWriter::write_to_string(genf, value);
  2393. f->store_line("files=" + value);
  2394. f->store_line("");
  2395. }
  2396. f->store_line("source_file=" + Variant(p_file).get_construct_string());
  2397. if (dest_paths.size()) {
  2398. Array dp;
  2399. for (int i = 0; i < dest_paths.size(); i++) {
  2400. dp.push_back(dest_paths[i]);
  2401. }
  2402. f->store_line("dest_files=" + Variant(dp).get_construct_string());
  2403. }
  2404. f->store_line("");
  2405. f->store_line("[params]");
  2406. f->store_line("");
  2407. // Store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  2408. for (const ResourceImporter::ImportOption &E : opts) {
  2409. String base = E.option.name;
  2410. String value;
  2411. VariantWriter::write_to_string(params[base], value);
  2412. f->store_line(base + "=" + value);
  2413. }
  2414. }
  2415. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  2416. {
  2417. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  2418. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  2419. md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"");
  2420. if (dest_paths.size()) {
  2421. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  2422. }
  2423. }
  2424. if (p_update_file_system) {
  2425. // Update cpos, newly created files could've changed the index of the reimported p_file.
  2426. _find_file(p_file, &fs, cpos);
  2427. // Update modified times, to avoid reimport.
  2428. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  2429. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  2430. fs->files[cpos]->import_md5 = FileAccess::get_md5(p_file + ".import");
  2431. fs->files[cpos]->import_dest_paths = dest_paths;
  2432. fs->files[cpos]->deps = _get_dependencies(p_file);
  2433. fs->files[cpos]->type = importer->get_resource_type();
  2434. fs->files[cpos]->uid = uid;
  2435. fs->files[cpos]->import_valid = fs->files[cpos]->type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  2436. }
  2437. if (ResourceUID::get_singleton()->has_id(uid)) {
  2438. ResourceUID::get_singleton()->set_id(uid, p_file);
  2439. } else {
  2440. ResourceUID::get_singleton()->add_id(uid, p_file);
  2441. }
  2442. // If file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  2443. // to reload properly.
  2444. Ref<Resource> r = ResourceCache::get_ref(p_file);
  2445. if (r.is_valid()) {
  2446. if (!r->get_import_path().is_empty()) {
  2447. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
  2448. r->set_import_path(dst_path);
  2449. r->set_import_last_modified_time(0);
  2450. }
  2451. }
  2452. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  2453. print_verbose(vformat("EditorFileSystem: \"%s\" import took %d ms.", p_file, OS::get_singleton()->get_ticks_msec() - start_time));
  2454. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_UNRECOGNIZED, "Error importing '" + p_file + "'.");
  2455. return OK;
  2456. }
  2457. void EditorFileSystem::_find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport) {
  2458. int fc = efd->files.size();
  2459. const EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptr();
  2460. for (int i = 0; i < fc; i++) {
  2461. if (groups_to_reimport.has(files[i]->import_group_file)) {
  2462. if (!group_files.has(files[i]->import_group_file)) {
  2463. group_files[files[i]->import_group_file] = Vector<String>();
  2464. }
  2465. group_files[files[i]->import_group_file].push_back(efd->get_file_path(i));
  2466. }
  2467. }
  2468. for (int i = 0; i < efd->get_subdir_count(); i++) {
  2469. _find_group_files(efd->get_subdir(i), group_files, groups_to_reimport);
  2470. }
  2471. }
  2472. void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params) {
  2473. Vector<String> reloads;
  2474. reloads.append(p_file);
  2475. // Emit the resource_reimporting signal for the single file before the actual importation.
  2476. emit_signal(SNAME("resources_reimporting"), reloads);
  2477. _reimport_file(p_file, p_custom_params, p_importer);
  2478. // Emit the resource_reimported signal for the single file we just reimported.
  2479. emit_signal(SNAME("resources_reimported"), reloads);
  2480. }
  2481. Error EditorFileSystem::_copy_file(const String &p_from, const String &p_to) {
  2482. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2483. if (FileAccess::exists(p_from + ".import")) {
  2484. Error err = da->copy(p_from, p_to);
  2485. if (err != OK) {
  2486. return err;
  2487. }
  2488. // Remove uid from .import file to avoid conflict.
  2489. Ref<ConfigFile> cfg;
  2490. cfg.instantiate();
  2491. cfg->load(p_from + ".import");
  2492. cfg->erase_section_key("remap", "uid");
  2493. err = cfg->save(p_to + ".import");
  2494. if (err != OK) {
  2495. return err;
  2496. }
  2497. } else if (ResourceLoader::get_resource_uid(p_from) == ResourceUID::INVALID_ID) {
  2498. // Files which do not use an uid can just be copied.
  2499. Error err = da->copy(p_from, p_to);
  2500. if (err != OK) {
  2501. return err;
  2502. }
  2503. } else {
  2504. // Load the resource and save it again in the new location (this generates a new UID).
  2505. Error err;
  2506. Ref<Resource> res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
  2507. if (err == OK && res.is_valid()) {
  2508. err = ResourceSaver::save(res, p_to, ResourceSaver::FLAG_COMPRESS);
  2509. if (err != OK) {
  2510. return err;
  2511. }
  2512. } else if (err != OK) {
  2513. // When loading files like text files the error is OK but the resource is still null.
  2514. // We can ignore such files.
  2515. return err;
  2516. }
  2517. }
  2518. return OK;
  2519. }
  2520. bool EditorFileSystem::_copy_directory(const String &p_from, const String &p_to, List<CopiedFile> *p_files) {
  2521. Ref<DirAccess> old_dir = DirAccess::open(p_from);
  2522. ERR_FAIL_COND_V(old_dir.is_null(), false);
  2523. Error err = make_dir_recursive(p_to);
  2524. if (err != OK && err != ERR_ALREADY_EXISTS) {
  2525. return false;
  2526. }
  2527. bool success = true;
  2528. old_dir->set_include_navigational(false);
  2529. old_dir->list_dir_begin();
  2530. for (String F = old_dir->_get_next(); !F.is_empty(); F = old_dir->_get_next()) {
  2531. if (old_dir->current_is_dir()) {
  2532. success = _copy_directory(p_from.path_join(F), p_to.path_join(F), p_files) && success;
  2533. } else if (F.get_extension() != "import") {
  2534. CopiedFile copy;
  2535. copy.from = p_from.path_join(F);
  2536. copy.to = p_to.path_join(F);
  2537. p_files->push_back(copy);
  2538. }
  2539. }
  2540. return success;
  2541. }
  2542. void EditorFileSystem::_queue_refresh_filesystem() {
  2543. if (refresh_queued) {
  2544. return;
  2545. }
  2546. refresh_queued = true;
  2547. get_tree()->connect(SNAME("process_frame"), callable_mp(this, &EditorFileSystem::_refresh_filesystem), CONNECT_ONE_SHOT);
  2548. }
  2549. void EditorFileSystem::_refresh_filesystem() {
  2550. for (const ObjectID &id : folders_to_sort) {
  2551. EditorFileSystemDirectory *dir = Object::cast_to<EditorFileSystemDirectory>(ObjectDB::get_instance(id));
  2552. if (dir) {
  2553. dir->subdirs.sort_custom<DirectoryComparator>();
  2554. }
  2555. }
  2556. folders_to_sort.clear();
  2557. _update_scan_actions();
  2558. emit_signal(SNAME("filesystem_changed"));
  2559. refresh_queued = false;
  2560. }
  2561. void EditorFileSystem::_reimport_thread(uint32_t p_index, ImportThreadData *p_import_data) {
  2562. int current_max = p_import_data->reimport_from + int(p_index);
  2563. p_import_data->max_index.exchange_if_greater(current_max);
  2564. _reimport_file(p_import_data->reimport_files[current_max].path);
  2565. }
  2566. void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
  2567. ERR_FAIL_COND_MSG(importing, "Attempted to call reimport_files() recursively, this is not allowed.");
  2568. importing = true;
  2569. Vector<String> reloads;
  2570. EditorProgress *ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
  2571. // The method reimport_files runs on the main thread, and if VSync is enabled
  2572. // or Update Continuously is disabled, Main::Iteration takes longer each frame.
  2573. // Each EditorProgress::step can trigger a redraw, and when there are many files to import,
  2574. // this could lead to a slow import process, especially when the editor is unfocused.
  2575. // Temporarily disabling VSync and low_processor_usage_mode while reimporting fixes this.
  2576. const bool old_low_processor_usage_mode = OS::get_singleton()->is_in_low_processor_usage_mode();
  2577. const DisplayServer::VSyncMode old_vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
  2578. OS::get_singleton()->set_low_processor_usage_mode(false);
  2579. DisplayServer::get_singleton()->window_set_vsync_mode(DisplayServer::VSyncMode::VSYNC_DISABLED);
  2580. Vector<ImportFile> reimport_files;
  2581. HashSet<String> groups_to_reimport;
  2582. for (int i = 0; i < p_files.size(); i++) {
  2583. ep->step(TTR("Preparing files to reimport..."), i, false);
  2584. String file = p_files[i];
  2585. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(file);
  2586. if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
  2587. file = ResourceUID::get_singleton()->get_id_path(uid);
  2588. }
  2589. String group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(file);
  2590. if (group_file_cache.has(file)) {
  2591. // Maybe the file itself is a group!
  2592. groups_to_reimport.insert(file);
  2593. // Groups do not belong to groups.
  2594. group_file = String();
  2595. } else if (groups_to_reimport.has(file)) {
  2596. // Groups do not belong to groups.
  2597. group_file = String();
  2598. } else if (!group_file.is_empty()) {
  2599. // It's a group file, add group to import and skip this file.
  2600. groups_to_reimport.insert(group_file);
  2601. } else {
  2602. // It's a regular file.
  2603. ImportFile ifile;
  2604. ifile.path = file;
  2605. ResourceFormatImporter::get_singleton()->get_import_order_threads_and_importer(file, ifile.order, ifile.threaded, ifile.importer);
  2606. reloads.push_back(file);
  2607. reimport_files.push_back(ifile);
  2608. }
  2609. // Group may have changed, so also update group reference.
  2610. EditorFileSystemDirectory *fs = nullptr;
  2611. int cpos = -1;
  2612. if (_find_file(file, &fs, cpos)) {
  2613. fs->files.write[cpos]->import_group_file = group_file;
  2614. }
  2615. }
  2616. reimport_files.sort();
  2617. ep->step(TTR("Executing pre-reimport operations..."), 0, true);
  2618. // Emit the resource_reimporting signal for the single file before the actual importation.
  2619. emit_signal(SNAME("resources_reimporting"), reloads);
  2620. #ifdef THREADS_ENABLED
  2621. bool use_multiple_threads = GLOBAL_GET("editor/import/use_multiple_threads");
  2622. #else
  2623. bool use_multiple_threads = false;
  2624. #endif
  2625. int from = 0;
  2626. for (int i = 0; i < reimport_files.size(); i++) {
  2627. if (groups_to_reimport.has(reimport_files[i].path)) {
  2628. from = i + 1;
  2629. continue;
  2630. }
  2631. if (use_multiple_threads && reimport_files[i].threaded) {
  2632. if (i + 1 == reimport_files.size() || reimport_files[i + 1].importer != reimport_files[from].importer || groups_to_reimport.has(reimport_files[i + 1].path)) {
  2633. if (from - i == 0) {
  2634. // Single file, do not use threads.
  2635. ep->step(reimport_files[i].path.get_file(), i, false);
  2636. _reimport_file(reimport_files[i].path);
  2637. } else {
  2638. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(reimport_files[from].importer);
  2639. if (importer.is_null()) {
  2640. ERR_PRINT(vformat("Invalid importer for \"%s\".", reimport_files[from].importer));
  2641. from = i + 1;
  2642. continue;
  2643. }
  2644. importer->import_threaded_begin();
  2645. ImportThreadData tdata;
  2646. tdata.max_index.set(from);
  2647. tdata.reimport_from = from;
  2648. tdata.reimport_files = reimport_files.ptr();
  2649. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &EditorFileSystem::_reimport_thread, &tdata, i - from + 1, -1, false, vformat(TTR("Import resources of type: %s"), reimport_files[from].importer));
  2650. int current_index = from - 1;
  2651. do {
  2652. if (current_index < tdata.max_index.get()) {
  2653. current_index = tdata.max_index.get();
  2654. ep->step(reimport_files[current_index].path.get_file(), current_index, false);
  2655. }
  2656. OS::get_singleton()->delay_usec(1);
  2657. } while (!WorkerThreadPool::get_singleton()->is_group_task_completed(group_task));
  2658. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  2659. importer->import_threaded_end();
  2660. }
  2661. from = i + 1;
  2662. }
  2663. } else {
  2664. ep->step(reimport_files[i].path.get_file(), i, false);
  2665. _reimport_file(reimport_files[i].path);
  2666. // We need to increment the counter, maybe the next file is multithreaded
  2667. // and doesn't have the same importer.
  2668. from = i + 1;
  2669. }
  2670. }
  2671. // Reimport groups.
  2672. from = reimport_files.size();
  2673. if (groups_to_reimport.size()) {
  2674. HashMap<String, Vector<String>> group_files;
  2675. _find_group_files(filesystem, group_files, groups_to_reimport);
  2676. for (const KeyValue<String, Vector<String>> &E : group_files) {
  2677. ep->step(E.key.get_file(), from++, false);
  2678. Error err = _reimport_group(E.key, E.value);
  2679. reloads.push_back(E.key);
  2680. reloads.append_array(E.value);
  2681. if (err == OK) {
  2682. _reimport_file(E.key);
  2683. }
  2684. }
  2685. }
  2686. ep->step(TTR("Finalizing Asset Import..."), p_files.size());
  2687. ResourceUID::get_singleton()->update_cache(); // After reimporting, update the cache.
  2688. _save_filesystem_cache();
  2689. memdelete_notnull(ep);
  2690. _process_update_pending();
  2691. // Revert to previous values to restore editor settings for VSync and Update Continuously.
  2692. OS::get_singleton()->set_low_processor_usage_mode(old_low_processor_usage_mode);
  2693. DisplayServer::get_singleton()->window_set_vsync_mode(old_vsync_mode);
  2694. importing = false;
  2695. ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
  2696. ep->step(TTR("Executing post-reimport operations..."), 0, true);
  2697. if (!is_scanning()) {
  2698. emit_signal(SNAME("filesystem_changed"));
  2699. }
  2700. emit_signal(SNAME("resources_reimported"), reloads);
  2701. memdelete_notnull(ep);
  2702. }
  2703. Error EditorFileSystem::reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
  2704. ERR_FAIL_COND_V_MSG(!importing, ERR_INVALID_PARAMETER, "Can only append files to import during a current reimport process.");
  2705. Vector<String> reloads;
  2706. reloads.append(p_file);
  2707. // Emit the resource_reimporting signal for the single file before the actual importation.
  2708. emit_signal(SNAME("resources_reimporting"), reloads);
  2709. Error ret = _reimport_file(p_file, p_custom_options, p_custom_importer, &p_generator_parameters);
  2710. // Emit the resource_reimported signal for the single file we just reimported.
  2711. emit_signal(SNAME("resources_reimported"), reloads);
  2712. return ret;
  2713. }
  2714. Error EditorFileSystem::_resource_import(const String &p_path) {
  2715. Vector<String> files;
  2716. files.push_back(p_path);
  2717. singleton->update_file(p_path);
  2718. singleton->reimport_files(files);
  2719. return OK;
  2720. }
  2721. 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) {
  2722. ERR_FAIL_NULL_V(p_importer, Ref<Resource>());
  2723. if (!FileAccess::exists(p_path)) {
  2724. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Failed loading resource: %s. The file doesn't seem to exist.", p_path));
  2725. }
  2726. Ref<Resource> res;
  2727. bool can_retry = true;
  2728. bool retry = true;
  2729. while (retry) {
  2730. retry = false;
  2731. res = p_importer->load_internal(p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode, can_retry);
  2732. if (res.is_null() && can_retry) {
  2733. can_retry = false;
  2734. Error err = singleton->_reimport_file(p_path, HashMap<StringName, Variant>(), "", nullptr, false);
  2735. if (err == OK) {
  2736. retry = true;
  2737. }
  2738. }
  2739. }
  2740. return res;
  2741. }
  2742. bool EditorFileSystem::_should_skip_directory(const String &p_path) {
  2743. String project_data_path = ProjectSettings::get_singleton()->get_project_data_path();
  2744. if (p_path == project_data_path || p_path.begins_with(project_data_path + "/")) {
  2745. return true;
  2746. }
  2747. if (FileAccess::exists(p_path.path_join("project.godot"))) {
  2748. // Skip if another project inside this.
  2749. if (EditorFileSystem::get_singleton()->first_scan) {
  2750. WARN_PRINT_ONCE(vformat("Detected another project.godot at %s. The folder will be ignored.", p_path));
  2751. }
  2752. return true;
  2753. }
  2754. if (FileAccess::exists(p_path.path_join(".gdignore"))) {
  2755. // Skip if a `.gdignore` file is inside this.
  2756. return true;
  2757. }
  2758. return false;
  2759. }
  2760. bool EditorFileSystem::is_group_file(const String &p_path) const {
  2761. return group_file_cache.has(p_path);
  2762. }
  2763. void EditorFileSystem::_move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location) {
  2764. int fc = efd->files.size();
  2765. EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptrw();
  2766. for (int i = 0; i < fc; i++) {
  2767. if (files[i]->import_group_file == p_group_file) {
  2768. files[i]->import_group_file = p_new_location;
  2769. Ref<ConfigFile> config;
  2770. config.instantiate();
  2771. String path = efd->get_file_path(i) + ".import";
  2772. Error err = config->load(path);
  2773. if (err != OK) {
  2774. continue;
  2775. }
  2776. if (config->has_section_key("remap", "group_file")) {
  2777. config->set_value("remap", "group_file", p_new_location);
  2778. }
  2779. List<String> sk;
  2780. config->get_section_keys("params", &sk);
  2781. for (const String &param : sk) {
  2782. //not very clean, but should work
  2783. String value = config->get_value("params", param);
  2784. if (value == p_group_file) {
  2785. config->set_value("params", param, p_new_location);
  2786. }
  2787. }
  2788. config->save(path);
  2789. }
  2790. }
  2791. for (int i = 0; i < efd->get_subdir_count(); i++) {
  2792. _move_group_files(efd->get_subdir(i), p_group_file, p_new_location);
  2793. }
  2794. }
  2795. void EditorFileSystem::move_group_file(const String &p_path, const String &p_new_path) {
  2796. if (get_filesystem()) {
  2797. _move_group_files(get_filesystem(), p_path, p_new_path);
  2798. if (group_file_cache.has(p_path)) {
  2799. group_file_cache.erase(p_path);
  2800. group_file_cache.insert(p_new_path);
  2801. }
  2802. }
  2803. }
  2804. Error EditorFileSystem::make_dir_recursive(const String &p_path, const String &p_base_path) {
  2805. Error err;
  2806. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2807. if (!p_base_path.is_empty()) {
  2808. err = da->change_dir(p_base_path);
  2809. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open base directory '" + p_base_path + "'.");
  2810. }
  2811. if (da->dir_exists(p_path)) {
  2812. return ERR_ALREADY_EXISTS;
  2813. }
  2814. err = da->make_dir_recursive(p_path);
  2815. if (err != OK) {
  2816. return err;
  2817. }
  2818. const String path = da->get_current_dir();
  2819. EditorFileSystemDirectory *parent = get_filesystem_path(path);
  2820. ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
  2821. folders_to_sort.insert(parent->get_instance_id());
  2822. const PackedStringArray folders = p_path.trim_prefix(path).trim_suffix("/").split("/");
  2823. for (const String &folder : folders) {
  2824. const int current = parent->find_dir_index(folder);
  2825. if (current > -1) {
  2826. parent = parent->get_subdir(current);
  2827. continue;
  2828. }
  2829. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  2830. efd->parent = parent;
  2831. efd->name = folder;
  2832. parent->subdirs.push_back(efd);
  2833. parent = efd;
  2834. }
  2835. _queue_refresh_filesystem();
  2836. return OK;
  2837. }
  2838. Error EditorFileSystem::copy_file(const String &p_from, const String &p_to) {
  2839. _copy_file(p_from, p_to);
  2840. EditorFileSystemDirectory *parent = get_filesystem_path(p_to.get_base_dir());
  2841. ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
  2842. ScanProgress sp;
  2843. _scan_fs_changes(parent, sp, false);
  2844. _queue_refresh_filesystem();
  2845. return OK;
  2846. }
  2847. Error EditorFileSystem::copy_directory(const String &p_from, const String &p_to) {
  2848. List<CopiedFile> files;
  2849. bool success = _copy_directory(p_from, p_to, &files);
  2850. EditorProgress *ep = nullptr;
  2851. if (files.size() > 10) {
  2852. ep = memnew(EditorProgress("_copy_files", TTR("Copying files..."), files.size()));
  2853. }
  2854. int i = 0;
  2855. for (const CopiedFile &F : files) {
  2856. if (_copy_file(F.from, F.to) != OK) {
  2857. success = false;
  2858. }
  2859. if (ep) {
  2860. ep->step(F.from.get_file(), i++, false);
  2861. }
  2862. }
  2863. memdelete_notnull(ep);
  2864. EditorFileSystemDirectory *efd = get_filesystem_path(p_to);
  2865. ERR_FAIL_NULL_V(efd, FAILED);
  2866. ERR_FAIL_NULL_V(efd->get_parent(), FAILED);
  2867. folders_to_sort.insert(efd->get_parent()->get_instance_id());
  2868. ScanProgress sp;
  2869. _scan_fs_changes(efd, sp);
  2870. _queue_refresh_filesystem();
  2871. return success ? OK : FAILED;
  2872. }
  2873. ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate) {
  2874. if (!p_path.is_resource_file() || p_path.begins_with(ProjectSettings::get_singleton()->get_project_data_path())) {
  2875. // Saved externally (configuration file) or internal file, do not assign an ID.
  2876. return ResourceUID::INVALID_ID;
  2877. }
  2878. EditorFileSystemDirectory *fs = nullptr;
  2879. int cpos = -1;
  2880. if (!singleton->_find_file(p_path, &fs, cpos)) {
  2881. // Fallback to ResourceLoader if filesystem cache fails (can happen during scanning etc.).
  2882. ResourceUID::ID fallback = ResourceLoader::get_resource_uid(p_path);
  2883. if (fallback != ResourceUID::INVALID_ID) {
  2884. return fallback;
  2885. }
  2886. if (p_generate) {
  2887. 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.
  2888. } else {
  2889. return ResourceUID::INVALID_ID;
  2890. }
  2891. } else if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  2892. return fs->files[cpos]->uid;
  2893. } else if (p_generate) {
  2894. 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.
  2895. } else {
  2896. return ResourceUID::INVALID_ID;
  2897. }
  2898. }
  2899. static void _scan_extensions_dir(EditorFileSystemDirectory *d, HashSet<String> &extensions) {
  2900. int fc = d->get_file_count();
  2901. for (int i = 0; i < fc; i++) {
  2902. if (d->get_file_type(i) == SNAME("GDExtension")) {
  2903. extensions.insert(d->get_file_path(i));
  2904. }
  2905. }
  2906. int dc = d->get_subdir_count();
  2907. for (int i = 0; i < dc; i++) {
  2908. _scan_extensions_dir(d->get_subdir(i), extensions);
  2909. }
  2910. }
  2911. bool EditorFileSystem::_scan_extensions() {
  2912. EditorFileSystemDirectory *d = get_filesystem();
  2913. HashSet<String> extensions;
  2914. _scan_extensions_dir(d, extensions);
  2915. return GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions);
  2916. }
  2917. void EditorFileSystem::_bind_methods() {
  2918. ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
  2919. ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
  2920. ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
  2921. ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
  2922. ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
  2923. ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
  2924. ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
  2925. ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
  2926. ClassDB::bind_method(D_METHOD("reimport_files", "files"), &EditorFileSystem::reimport_files);
  2927. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  2928. ADD_SIGNAL(MethodInfo("script_classes_updated"));
  2929. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  2930. ADD_SIGNAL(MethodInfo("resources_reimporting", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2931. ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2932. ADD_SIGNAL(MethodInfo("resources_reload", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2933. }
  2934. void EditorFileSystem::_update_extensions() {
  2935. valid_extensions.clear();
  2936. import_extensions.clear();
  2937. textfile_extensions.clear();
  2938. other_file_extensions.clear();
  2939. List<String> extensionsl;
  2940. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  2941. for (const String &E : extensionsl) {
  2942. valid_extensions.insert(E);
  2943. }
  2944. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  2945. for (const String &E : textfile_ext) {
  2946. if (valid_extensions.has(E)) {
  2947. continue;
  2948. }
  2949. valid_extensions.insert(E);
  2950. textfile_extensions.insert(E);
  2951. }
  2952. const Vector<String> other_file_ext = ((String)(EDITOR_GET("docks/filesystem/other_file_extensions"))).split(",", false);
  2953. for (const String &E : other_file_ext) {
  2954. if (valid_extensions.has(E)) {
  2955. continue;
  2956. }
  2957. valid_extensions.insert(E);
  2958. other_file_extensions.insert(E);
  2959. }
  2960. extensionsl.clear();
  2961. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  2962. for (const String &E : extensionsl) {
  2963. import_extensions.insert(E);
  2964. }
  2965. }
  2966. void EditorFileSystem::add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2967. ERR_FAIL_COND(import_support_queries.has(p_query));
  2968. import_support_queries.push_back(p_query);
  2969. }
  2970. void EditorFileSystem::remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2971. import_support_queries.erase(p_query);
  2972. }
  2973. EditorFileSystem::EditorFileSystem() {
  2974. #ifdef THREADS_ENABLED
  2975. use_threads = true;
  2976. #endif
  2977. ResourceLoader::import = _resource_import;
  2978. reimport_on_missing_imported_files = GLOBAL_GET("editor/import/reimport_missing_imported_files");
  2979. singleton = this;
  2980. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  2981. filesystem->parent = nullptr;
  2982. new_filesystem = nullptr;
  2983. // This should probably also work on Unix and use the string it returns for FAT32 or exFAT
  2984. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2985. using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "exFAT");
  2986. scan_total = 0;
  2987. ResourceSaver::set_get_resource_id_for_path(_resource_saver_get_resource_id_for_path);
  2988. // Set the callback method that the ResourceFormatImporter will use
  2989. // if resources are loaded during the first scan.
  2990. ResourceImporter::load_on_startup = _load_resource_on_startup;
  2991. }
  2992. EditorFileSystem::~EditorFileSystem() {
  2993. if (filesystem) {
  2994. memdelete(filesystem);
  2995. }
  2996. filesystem = nullptr;
  2997. ResourceSaver::set_get_resource_id_for_path(nullptr);
  2998. }