editor_file_system.cpp 117 KB

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