editor_file_system.cpp 120 KB

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