editor_file_system.cpp 119 KB

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