editor_file_system.cpp 119 KB

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