editor_file_system.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072
  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/file_access.h"
  34. #include "core/io/resource_importer.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/io/resource_saver.h"
  37. #include "core/object/worker_thread_pool.h"
  38. #include "core/os/os.h"
  39. #include "core/variant/variant_parser.h"
  40. #include "editor/editor_help.h"
  41. #include "editor/editor_node.h"
  42. #include "editor/editor_paths.h"
  43. #include "editor/editor_resource_preview.h"
  44. #include "editor/editor_settings.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. String p;
  86. const EditorFileSystemDirectory *d = this;
  87. while (d->parent) {
  88. p = d->name.path_join(p);
  89. d = d->parent;
  90. }
  91. return "res://" + p;
  92. }
  93. String EditorFileSystemDirectory::get_file_path(int p_idx) const {
  94. String file = get_file(p_idx);
  95. const EditorFileSystemDirectory *d = this;
  96. while (d->parent) {
  97. file = d->name.path_join(file);
  98. d = d->parent;
  99. }
  100. return "res://" + file;
  101. }
  102. Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {
  103. ERR_FAIL_INDEX_V(p_idx, files.size(), Vector<String>());
  104. Vector<String> deps;
  105. for (int i = 0; i < files[p_idx]->deps.size(); i++) {
  106. String dep = files[p_idx]->deps[i];
  107. int sep_idx = dep.find("::"); //may contain type information, unwanted
  108. if (sep_idx != -1) {
  109. dep = dep.substr(0, sep_idx);
  110. }
  111. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(dep);
  112. if (uid != ResourceUID::INVALID_ID) {
  113. //return proper dependency resource from uid
  114. if (ResourceUID::get_singleton()->has_id(uid)) {
  115. dep = ResourceUID::get_singleton()->get_id_path(uid);
  116. } else {
  117. continue;
  118. }
  119. }
  120. deps.push_back(dep);
  121. }
  122. return deps;
  123. }
  124. bool EditorFileSystemDirectory::get_file_import_is_valid(int p_idx) const {
  125. ERR_FAIL_INDEX_V(p_idx, files.size(), false);
  126. return files[p_idx]->import_valid;
  127. }
  128. uint64_t EditorFileSystemDirectory::get_file_modified_time(int p_idx) const {
  129. ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
  130. return files[p_idx]->modified_time;
  131. }
  132. uint64_t EditorFileSystemDirectory::get_file_import_modified_time(int p_idx) const {
  133. ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
  134. return files[p_idx]->import_modified_time;
  135. }
  136. String EditorFileSystemDirectory::get_file_script_class_name(int p_idx) const {
  137. return files[p_idx]->script_class_name;
  138. }
  139. String EditorFileSystemDirectory::get_file_script_class_extends(int p_idx) const {
  140. return files[p_idx]->script_class_extends;
  141. }
  142. String EditorFileSystemDirectory::get_file_script_class_icon_path(int p_idx) const {
  143. return files[p_idx]->script_class_icon_path;
  144. }
  145. String EditorFileSystemDirectory::get_file_icon_path(int p_idx) const {
  146. return files[p_idx]->icon_path;
  147. }
  148. StringName EditorFileSystemDirectory::get_file_type(int p_idx) const {
  149. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  150. return files[p_idx]->type;
  151. }
  152. StringName EditorFileSystemDirectory::get_file_resource_script_class(int p_idx) const {
  153. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  154. return files[p_idx]->resource_script_class;
  155. }
  156. String EditorFileSystemDirectory::get_name() {
  157. return name;
  158. }
  159. EditorFileSystemDirectory *EditorFileSystemDirectory::get_parent() {
  160. return parent;
  161. }
  162. void EditorFileSystemDirectory::_bind_methods() {
  163. ClassDB::bind_method(D_METHOD("get_subdir_count"), &EditorFileSystemDirectory::get_subdir_count);
  164. ClassDB::bind_method(D_METHOD("get_subdir", "idx"), &EditorFileSystemDirectory::get_subdir);
  165. ClassDB::bind_method(D_METHOD("get_file_count"), &EditorFileSystemDirectory::get_file_count);
  166. ClassDB::bind_method(D_METHOD("get_file", "idx"), &EditorFileSystemDirectory::get_file);
  167. ClassDB::bind_method(D_METHOD("get_file_path", "idx"), &EditorFileSystemDirectory::get_file_path);
  168. ClassDB::bind_method(D_METHOD("get_file_type", "idx"), &EditorFileSystemDirectory::get_file_type);
  169. ClassDB::bind_method(D_METHOD("get_file_script_class_name", "idx"), &EditorFileSystemDirectory::get_file_script_class_name);
  170. ClassDB::bind_method(D_METHOD("get_file_script_class_extends", "idx"), &EditorFileSystemDirectory::get_file_script_class_extends);
  171. ClassDB::bind_method(D_METHOD("get_file_import_is_valid", "idx"), &EditorFileSystemDirectory::get_file_import_is_valid);
  172. ClassDB::bind_method(D_METHOD("get_name"), &EditorFileSystemDirectory::get_name);
  173. ClassDB::bind_method(D_METHOD("get_path"), &EditorFileSystemDirectory::get_path);
  174. ClassDB::bind_method(D_METHOD("get_parent"), &EditorFileSystemDirectory::get_parent);
  175. ClassDB::bind_method(D_METHOD("find_file_index", "name"), &EditorFileSystemDirectory::find_file_index);
  176. ClassDB::bind_method(D_METHOD("find_dir_index", "name"), &EditorFileSystemDirectory::find_dir_index);
  177. }
  178. EditorFileSystemDirectory::EditorFileSystemDirectory() {
  179. modified_time = 0;
  180. parent = nullptr;
  181. }
  182. EditorFileSystemDirectory::~EditorFileSystemDirectory() {
  183. for (EditorFileSystemDirectory::FileInfo *fi : files) {
  184. memdelete(fi);
  185. }
  186. for (EditorFileSystemDirectory *dir : subdirs) {
  187. memdelete(dir);
  188. }
  189. }
  190. EditorFileSystem::ScannedDirectory::~ScannedDirectory() {
  191. for (ScannedDirectory *dir : subdirs) {
  192. memdelete(dir);
  193. }
  194. }
  195. void EditorFileSystem::_first_scan_filesystem() {
  196. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  197. first_scan_root_dir = memnew(ScannedDirectory);
  198. first_scan_root_dir->full_path = "res://";
  199. HashSet<String> existing_class_names;
  200. nb_files_total = _scan_new_dir(first_scan_root_dir, d);
  201. // This loads the global class names from the scripts and ensures that even if the
  202. // global_script_class_cache.cfg was missing or invalid, the global class names are valid in ScriptServer.
  203. _first_scan_process_scripts(first_scan_root_dir, existing_class_names);
  204. // Removing invalid global class to prevent having invalid paths in ScriptServer.
  205. _remove_invalid_global_class_names(existing_class_names);
  206. // Now that all the global class names should be loaded, create autoloads and plugins.
  207. // This is done after loading the global class names because autoloads and plugins can use
  208. // global class names.
  209. ProjectSettingsEditor::get_singleton()->init_autoloads();
  210. EditorNode::get_singleton()->init_plugins();
  211. }
  212. void EditorFileSystem::_first_scan_process_scripts(const ScannedDirectory *p_scan_dir, HashSet<String> &p_existing_class_names) {
  213. for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
  214. _first_scan_process_scripts(scan_sub_dir, p_existing_class_names);
  215. }
  216. for (const String &scan_file : p_scan_dir->files) {
  217. String path = p_scan_dir->full_path.path_join(scan_file);
  218. String type = ResourceLoader::get_resource_type(path);
  219. if (ClassDB::is_parent_class(type, SNAME("Script"))) {
  220. String script_class_extends;
  221. String script_class_icon_path;
  222. String script_class_name = _get_global_script_class(type, path, &script_class_extends, &script_class_icon_path);
  223. _register_global_class_script(path, path, type, script_class_name, script_class_extends, script_class_icon_path);
  224. if (!script_class_name.is_empty()) {
  225. p_existing_class_names.insert(script_class_name);
  226. }
  227. }
  228. }
  229. }
  230. void EditorFileSystem::_scan_filesystem() {
  231. // On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
  232. ERR_FAIL_COND(!scanning || new_filesystem || (first_scan && !first_scan_root_dir));
  233. //read .fscache
  234. String cpath;
  235. sources_changed.clear();
  236. file_cache.clear();
  237. String project = ProjectSettings::get_singleton()->get_resource_path();
  238. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
  239. {
  240. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::READ);
  241. bool first = true;
  242. if (f.is_valid()) {
  243. //read the disk cache
  244. while (!f->eof_reached()) {
  245. String l = f->get_line().strip_edges();
  246. if (first) {
  247. if (first_scan) {
  248. // only use this on first scan, afterwards it gets ignored
  249. // this is so on first reimport we synchronize versions, then
  250. // we don't care until editor restart. This is for usability mainly so
  251. // your workflow is not killed after changing a setting by forceful reimporting
  252. // everything there is.
  253. filesystem_settings_version_for_import = l.strip_edges();
  254. if (filesystem_settings_version_for_import != ResourceFormatImporter::get_singleton()->get_import_settings_hash()) {
  255. revalidate_import_files = true;
  256. }
  257. }
  258. first = false;
  259. continue;
  260. }
  261. if (l.is_empty()) {
  262. continue;
  263. }
  264. if (l.begins_with("::")) {
  265. Vector<String> split = l.split("::");
  266. ERR_CONTINUE(split.size() != 3);
  267. const String &name = split[1];
  268. cpath = name;
  269. } else {
  270. // The last section (deps) may contain the same splitter, so limit the maxsplit to 8 to get the complete deps.
  271. Vector<String> split = l.split("::", true, 8);
  272. ERR_CONTINUE(split.size() < 9);
  273. String name = split[0];
  274. String file;
  275. file = name;
  276. name = cpath.path_join(name);
  277. FileCache fc;
  278. fc.type = split[1];
  279. if (fc.type.contains("/")) {
  280. fc.type = split[1].get_slice("/", 0);
  281. fc.resource_script_class = split[1].get_slice("/", 1);
  282. }
  283. fc.uid = split[2].to_int();
  284. fc.modification_time = split[3].to_int();
  285. fc.import_modification_time = split[4].to_int();
  286. fc.import_valid = split[5].to_int() != 0;
  287. fc.import_group_file = split[6].strip_edges();
  288. fc.script_class_name = split[7].get_slice("<>", 0);
  289. fc.script_class_extends = split[7].get_slice("<>", 1);
  290. fc.script_class_icon_path = split[7].get_slice("<>", 2);
  291. String deps = split[8].strip_edges();
  292. if (deps.length()) {
  293. Vector<String> dp = deps.split("<>");
  294. for (int i = 0; i < dp.size(); i++) {
  295. const String &path = dp[i];
  296. fc.deps.push_back(path);
  297. }
  298. }
  299. file_cache[name] = fc;
  300. }
  301. }
  302. }
  303. }
  304. const String update_cache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  305. if (first_scan && FileAccess::exists(update_cache)) {
  306. {
  307. Ref<FileAccess> f2 = FileAccess::open(update_cache, FileAccess::READ);
  308. String l = f2->get_line().strip_edges();
  309. while (!l.is_empty()) {
  310. dep_update_list.insert(l);
  311. file_cache.erase(l); // Erase cache for this, so it gets updated.
  312. l = f2->get_line().strip_edges();
  313. }
  314. }
  315. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  316. d->remove(update_cache); // Bye bye update cache.
  317. }
  318. EditorProgressBG scan_progress("efs", "ScanFS", 1000);
  319. ScanProgress sp;
  320. sp.hi = nb_files_total;
  321. sp.progress = &scan_progress;
  322. new_filesystem = memnew(EditorFileSystemDirectory);
  323. new_filesystem->parent = nullptr;
  324. ScannedDirectory *sd;
  325. // On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
  326. if (first_scan) {
  327. sd = first_scan_root_dir;
  328. // Will be updated on scan.
  329. ResourceUID::get_singleton()->clear();
  330. } else {
  331. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  332. sd = memnew(ScannedDirectory);
  333. sd->full_path = "res://";
  334. nb_files_total = _scan_new_dir(sd, d);
  335. }
  336. _process_file_system(sd, new_filesystem, sp);
  337. dep_update_list.clear();
  338. file_cache.clear(); //clear caches, no longer needed
  339. if (first_scan) {
  340. memdelete(first_scan_root_dir);
  341. first_scan_root_dir = nullptr;
  342. } else {
  343. //on the first scan this is done from the main thread after re-importing
  344. _save_filesystem_cache();
  345. }
  346. scanning = false;
  347. }
  348. void EditorFileSystem::_save_filesystem_cache() {
  349. group_file_cache.clear();
  350. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
  351. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  352. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  353. f->store_line(filesystem_settings_version_for_import);
  354. _save_filesystem_cache(filesystem, f);
  355. }
  356. void EditorFileSystem::_thread_func(void *_userdata) {
  357. EditorFileSystem *sd = (EditorFileSystem *)_userdata;
  358. sd->_scan_filesystem();
  359. }
  360. bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_imported_files) {
  361. if (!reimport_on_missing_imported_files && p_only_imported_files) {
  362. return false;
  363. }
  364. if (!FileAccess::exists(p_path + ".import")) {
  365. return true;
  366. }
  367. Error err;
  368. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  369. if (f.is_null()) { // No import file, reimport.
  370. return true;
  371. }
  372. VariantParser::StreamFile stream;
  373. stream.f = f;
  374. String assign;
  375. Variant value;
  376. VariantParser::Tag next_tag;
  377. int lines = 0;
  378. String error_text;
  379. List<String> to_check;
  380. String importer_name;
  381. String source_file = "";
  382. String source_md5 = "";
  383. Vector<String> dest_files;
  384. String dest_md5 = "";
  385. int version = 0;
  386. bool found_uid = false;
  387. while (true) {
  388. assign = Variant();
  389. next_tag.fields.clear();
  390. next_tag.name = String();
  391. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  392. if (err == ERR_FILE_EOF) {
  393. break;
  394. } else if (err != OK) {
  395. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
  396. // Parse error, skip and let user attempt manual reimport to avoid reimport loop.
  397. return false;
  398. }
  399. if (!assign.is_empty()) {
  400. if (assign == "valid" && value.operator bool() == false) {
  401. // Invalid import (failed previous import), skip and let user attempt manual reimport to avoid reimport loop.
  402. return false;
  403. }
  404. if (assign.begins_with("path")) {
  405. to_check.push_back(value);
  406. } else if (assign == "files") {
  407. Array fa = value;
  408. for (int i = 0; i < fa.size(); i++) {
  409. to_check.push_back(fa[i]);
  410. }
  411. } else if (assign == "importer_version") {
  412. version = value;
  413. } else if (assign == "importer") {
  414. importer_name = value;
  415. } else if (assign == "uid") {
  416. found_uid = true;
  417. } else if (!p_only_imported_files) {
  418. if (assign == "source_file") {
  419. source_file = value;
  420. } else if (assign == "dest_files") {
  421. dest_files = value;
  422. }
  423. }
  424. } else if (next_tag.name != "remap" && next_tag.name != "deps") {
  425. break;
  426. }
  427. }
  428. if (!ResourceFormatImporter::get_singleton()->are_import_settings_valid(p_path)) {
  429. // Reimport settings are out of sync with project settings, reimport.
  430. return true;
  431. }
  432. if (importer_name == "keep" || importer_name == "skip") {
  433. return false; //keep mode, do not reimport
  434. }
  435. if (!found_uid) {
  436. return true; //UID not found, old format, reimport.
  437. }
  438. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  439. if (importer.is_null()) {
  440. return true; // the importer has possibly changed, try to reimport.
  441. }
  442. if (importer->get_format_version() > version) {
  443. return true; // version changed, reimport
  444. }
  445. // Read the md5's from a separate file (so the import parameters aren't dependent on the file version
  446. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path);
  447. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err);
  448. if (md5s.is_null()) { // No md5's stored for this resource
  449. return true;
  450. }
  451. VariantParser::StreamFile md5_stream;
  452. md5_stream.f = md5s;
  453. while (true) {
  454. assign = Variant();
  455. next_tag.fields.clear();
  456. next_tag.name = String();
  457. err = VariantParser::parse_tag_assign_eof(&md5_stream, lines, error_text, next_tag, assign, value, nullptr, true);
  458. if (err == ERR_FILE_EOF) {
  459. break;
  460. } else if (err != OK) {
  461. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import.md5:" + itos(lines) + "' error '" + error_text + "'.");
  462. return false; // parse error
  463. }
  464. if (!assign.is_empty()) {
  465. if (!p_only_imported_files) {
  466. if (assign == "source_md5") {
  467. source_md5 = value;
  468. } else if (assign == "dest_md5") {
  469. dest_md5 = value;
  470. }
  471. }
  472. }
  473. }
  474. //imported files are gone, reimport
  475. for (const String &E : to_check) {
  476. if (!FileAccess::exists(E)) {
  477. return true;
  478. }
  479. }
  480. //check source md5 matching
  481. if (!p_only_imported_files) {
  482. if (!source_file.is_empty() && source_file != p_path) {
  483. return true; //file was moved, reimport
  484. }
  485. if (source_md5.is_empty()) {
  486. return true; //lacks md5, so just reimport
  487. }
  488. String md5 = FileAccess::get_md5(p_path);
  489. if (md5 != source_md5) {
  490. return true;
  491. }
  492. if (dest_files.size() && !dest_md5.is_empty()) {
  493. md5 = FileAccess::get_multiple_md5(dest_files);
  494. if (md5 != dest_md5) {
  495. return true;
  496. }
  497. }
  498. }
  499. return false; //nothing changed
  500. }
  501. bool EditorFileSystem::_scan_import_support(const Vector<String> &reimports) {
  502. if (import_support_queries.size() == 0) {
  503. return false;
  504. }
  505. HashMap<String, int> import_support_test;
  506. Vector<bool> import_support_tested;
  507. import_support_tested.resize(import_support_queries.size());
  508. for (int i = 0; i < import_support_queries.size(); i++) {
  509. import_support_tested.write[i] = false;
  510. if (import_support_queries[i]->is_active()) {
  511. Vector<String> extensions = import_support_queries[i]->get_file_extensions();
  512. for (int j = 0; j < extensions.size(); j++) {
  513. import_support_test.insert(extensions[j], i);
  514. }
  515. }
  516. }
  517. if (import_support_test.size() == 0) {
  518. return false; //well nothing to do
  519. }
  520. for (int i = 0; i < reimports.size(); i++) {
  521. HashMap<String, int>::Iterator E = import_support_test.find(reimports[i].get_extension().to_lower());
  522. if (E) {
  523. import_support_tested.write[E->value] = true;
  524. }
  525. }
  526. for (int i = 0; i < import_support_tested.size(); i++) {
  527. if (import_support_tested[i]) {
  528. if (import_support_queries.write[i]->query()) {
  529. return true;
  530. }
  531. }
  532. }
  533. return false;
  534. }
  535. bool EditorFileSystem::_update_scan_actions() {
  536. sources_changed.clear();
  537. // We need to update the script global class names before the reimports to be sure that
  538. // all the importer classes that depends on class names will work.
  539. _update_script_classes();
  540. bool fs_changed = false;
  541. Vector<String> reimports;
  542. Vector<String> reloads;
  543. for (const ItemAction &ia : scan_actions) {
  544. switch (ia.action) {
  545. case ItemAction::ACTION_NONE: {
  546. } break;
  547. case ItemAction::ACTION_DIR_ADD: {
  548. int idx = 0;
  549. for (int i = 0; i < ia.dir->subdirs.size(); i++) {
  550. if (ia.new_dir->name.filenocasecmp_to(ia.dir->subdirs[i]->name) < 0) {
  551. break;
  552. }
  553. idx++;
  554. }
  555. if (idx == ia.dir->subdirs.size()) {
  556. ia.dir->subdirs.push_back(ia.new_dir);
  557. } else {
  558. ia.dir->subdirs.insert(idx, ia.new_dir);
  559. }
  560. fs_changed = true;
  561. } break;
  562. case ItemAction::ACTION_DIR_REMOVE: {
  563. ERR_CONTINUE(!ia.dir->parent);
  564. ia.dir->parent->subdirs.erase(ia.dir);
  565. memdelete(ia.dir);
  566. fs_changed = true;
  567. } break;
  568. case ItemAction::ACTION_FILE_ADD: {
  569. int idx = 0;
  570. for (int i = 0; i < ia.dir->files.size(); i++) {
  571. if (ia.new_file->file.filenocasecmp_to(ia.dir->files[i]->file) < 0) {
  572. break;
  573. }
  574. idx++;
  575. }
  576. if (idx == ia.dir->files.size()) {
  577. ia.dir->files.push_back(ia.new_file);
  578. } else {
  579. ia.dir->files.insert(idx, ia.new_file);
  580. }
  581. fs_changed = true;
  582. if (ClassDB::is_parent_class(ia.new_file->type, SNAME("Script"))) {
  583. _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);
  584. }
  585. if (ia.new_file->type == SNAME("PackedScene")) {
  586. _queue_update_scene_groups(ia.dir->get_file_path(idx));
  587. }
  588. } break;
  589. case ItemAction::ACTION_FILE_REMOVE: {
  590. int idx = ia.dir->find_file_index(ia.file);
  591. ERR_CONTINUE(idx == -1);
  592. String script_class_name = ia.dir->files[idx]->script_class_name;
  593. if (ClassDB::is_parent_class(ia.dir->files[idx]->type, SNAME("Script"))) {
  594. _queue_update_script_class(ia.dir->get_file_path(idx), "", "", "", "");
  595. }
  596. if (ia.dir->files[idx]->type == SNAME("PackedScene")) {
  597. _queue_update_scene_groups(ia.dir->get_file_path(idx));
  598. }
  599. _delete_internal_files(ia.dir->files[idx]->file);
  600. memdelete(ia.dir->files[idx]);
  601. ia.dir->files.remove_at(idx);
  602. // Restore another script with the same global class name if it exists.
  603. if (!script_class_name.is_empty()) {
  604. EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
  605. String old_file = _get_file_by_class_name(filesystem, script_class_name, old_fi);
  606. if (!old_file.is_empty() && old_fi) {
  607. _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);
  608. }
  609. }
  610. fs_changed = true;
  611. } break;
  612. case ItemAction::ACTION_FILE_TEST_REIMPORT: {
  613. int idx = ia.dir->find_file_index(ia.file);
  614. ERR_CONTINUE(idx == -1);
  615. String full_path = ia.dir->get_file_path(idx);
  616. bool need_reimport = _test_for_reimport(full_path, false);
  617. // Workaround GH-94416 for the Android editor for now.
  618. // `import_mt` seems to always be 0 and force a reimport on any fs scan.
  619. #ifndef ANDROID_ENABLED
  620. if (!need_reimport && FileAccess::exists(full_path + ".import")) {
  621. uint64_t import_mt = ia.dir->get_file_import_modified_time(idx);
  622. if (import_mt != FileAccess::get_modified_time(full_path + ".import")) {
  623. need_reimport = true;
  624. }
  625. }
  626. #endif
  627. if (need_reimport) {
  628. //must reimport
  629. reimports.push_back(full_path);
  630. Vector<String> dependencies = _get_dependencies(full_path);
  631. for (const String &dep : dependencies) {
  632. const String &dependency_path = dep.contains("::") ? dep.get_slice("::", 0) : dep;
  633. if (import_extensions.has(dep.get_extension())) {
  634. reimports.push_back(dependency_path);
  635. }
  636. }
  637. } else {
  638. //must not reimport, all was good
  639. //update modified times, to avoid reimport
  640. ia.dir->files[idx]->modified_time = FileAccess::get_modified_time(full_path);
  641. ia.dir->files[idx]->import_modified_time = FileAccess::get_modified_time(full_path + ".import");
  642. }
  643. fs_changed = true;
  644. } break;
  645. case ItemAction::ACTION_FILE_RELOAD: {
  646. int idx = ia.dir->find_file_index(ia.file);
  647. ERR_CONTINUE(idx == -1);
  648. String full_path = ia.dir->get_file_path(idx);
  649. const EditorFileSystemDirectory::FileInfo *fi = ia.dir->files[idx];
  650. if (ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  651. _queue_update_script_class(full_path, fi->type, fi->script_class_name, fi->script_class_extends, fi->script_class_icon_path);
  652. }
  653. if (fi->type == SNAME("PackedScene")) {
  654. _queue_update_scene_groups(full_path);
  655. }
  656. reloads.push_back(full_path);
  657. } break;
  658. }
  659. }
  660. if (_scan_extensions()) {
  661. //needs editor restart
  662. //extensions also may provide filetypes to be imported, so they must run before importing
  663. 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"))) {
  664. if (!first_scan) {
  665. EditorNode::get_singleton()->save_all_scenes();
  666. }
  667. EditorNode::get_singleton()->restart_editor();
  668. //do not import
  669. return true;
  670. }
  671. }
  672. if (reimports.size()) {
  673. if (_scan_import_support(reimports)) {
  674. return true;
  675. }
  676. reimport_files(reimports);
  677. } else {
  678. //reimport files will update the uid cache file so if nothing was reimported, update it manually
  679. ResourceUID::get_singleton()->update_cache();
  680. }
  681. if (first_scan) {
  682. //only on first scan this is valid and updated, then settings changed.
  683. revalidate_import_files = false;
  684. filesystem_settings_version_for_import = ResourceFormatImporter::get_singleton()->get_import_settings_hash();
  685. _save_filesystem_cache();
  686. }
  687. // Moving the processing of pending updates before the resources_reload event to be sure all global class names
  688. // are updated. Script.cpp listens on resources_reload and reloads updated scripts.
  689. _process_update_pending();
  690. if (reloads.size()) {
  691. emit_signal(SNAME("resources_reload"), reloads);
  692. }
  693. scan_actions.clear();
  694. return fs_changed;
  695. }
  696. void EditorFileSystem::scan() {
  697. if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/) {
  698. return;
  699. }
  700. if (scanning || scanning_changes || thread.is_started()) {
  701. return;
  702. }
  703. // The first scan must be on the main thread because, after the first scan and update
  704. // of global class names, we load the plugins and autoloads. These need to
  705. // be added on the main thread because they are nodes, and we need to wait for them
  706. // to be loaded to continue the scan and reimportations.
  707. if (first_scan) {
  708. _first_scan_filesystem();
  709. }
  710. _update_extensions();
  711. if (!use_threads) {
  712. scanning = true;
  713. scan_total = 0;
  714. _scan_filesystem();
  715. if (filesystem) {
  716. memdelete(filesystem);
  717. }
  718. //file_type_cache.clear();
  719. filesystem = new_filesystem;
  720. new_filesystem = nullptr;
  721. _update_scan_actions();
  722. // Update all icons so they are loaded for the FileSystemDock.
  723. _update_files_icon_path();
  724. scanning = false;
  725. // Set first_scan to false before the signals so the function doing_first_scan can return false
  726. // in editor_node to start the export if needed.
  727. first_scan = false;
  728. emit_signal(SNAME("filesystem_changed"));
  729. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  730. } else {
  731. ERR_FAIL_COND(thread.is_started());
  732. set_process(true);
  733. Thread::Settings s;
  734. scanning = true;
  735. scan_total = 0;
  736. s.priority = Thread::PRIORITY_LOW;
  737. thread.start(_thread_func, this, s);
  738. //tree->hide();
  739. //progress->show();
  740. }
  741. }
  742. void EditorFileSystem::ScanProgress::increment() {
  743. current++;
  744. float ratio = current / MAX(hi, 1.0f);
  745. progress->step(ratio * 1000.0f);
  746. EditorFileSystem::singleton->scan_total = ratio;
  747. }
  748. int EditorFileSystem::_scan_new_dir(ScannedDirectory *p_dir, Ref<DirAccess> &da) {
  749. List<String> dirs;
  750. List<String> files;
  751. String cd = da->get_current_dir();
  752. da->list_dir_begin();
  753. while (true) {
  754. String f = da->get_next();
  755. if (f.is_empty()) {
  756. break;
  757. }
  758. if (da->current_is_hidden()) {
  759. continue;
  760. }
  761. if (da->current_is_dir()) {
  762. if (f.begins_with(".")) { // Ignore special and . / ..
  763. continue;
  764. }
  765. if (_should_skip_directory(cd.path_join(f))) {
  766. continue;
  767. }
  768. dirs.push_back(f);
  769. } else {
  770. files.push_back(f);
  771. }
  772. }
  773. da->list_dir_end();
  774. dirs.sort_custom<FileNoCaseComparator>();
  775. files.sort_custom<FileNoCaseComparator>();
  776. int nb_files_total_scan = 0;
  777. for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
  778. if (da->change_dir(E->get()) == OK) {
  779. String d = da->get_current_dir();
  780. if (d == cd || !d.begins_with(cd)) {
  781. da->change_dir(cd); //avoid recursion
  782. } else {
  783. ScannedDirectory *sd = memnew(ScannedDirectory);
  784. sd->name = E->get();
  785. sd->full_path = p_dir->full_path.path_join(sd->name);
  786. nb_files_total_scan += _scan_new_dir(sd, da);
  787. p_dir->subdirs.push_back(sd);
  788. da->change_dir("..");
  789. }
  790. } else {
  791. ERR_PRINT("Cannot go into subdir '" + E->get() + "'.");
  792. }
  793. }
  794. p_dir->files = files;
  795. nb_files_total_scan += files.size();
  796. return nb_files_total_scan;
  797. }
  798. void EditorFileSystem::_process_file_system(const ScannedDirectory *p_scan_dir, EditorFileSystemDirectory *p_dir, ScanProgress &p_progress) {
  799. p_dir->modified_time = FileAccess::get_modified_time(p_scan_dir->full_path);
  800. for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
  801. EditorFileSystemDirectory *sub_dir = memnew(EditorFileSystemDirectory);
  802. sub_dir->parent = p_dir;
  803. sub_dir->name = scan_sub_dir->name;
  804. p_dir->subdirs.push_back(sub_dir);
  805. _process_file_system(scan_sub_dir, sub_dir, p_progress);
  806. }
  807. for (const String &scan_file : p_scan_dir->files) {
  808. String ext = scan_file.get_extension().to_lower();
  809. if (!valid_extensions.has(ext)) {
  810. p_progress.increment();
  811. continue; //invalid
  812. }
  813. String path = p_scan_dir->full_path.path_join(scan_file);
  814. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  815. fi->file = scan_file;
  816. p_dir->files.push_back(fi);
  817. FileCache *fc = file_cache.getptr(path);
  818. uint64_t mt = FileAccess::get_modified_time(path);
  819. if (import_extensions.has(ext)) {
  820. //is imported
  821. uint64_t import_mt = 0;
  822. if (FileAccess::exists(path + ".import")) {
  823. import_mt = FileAccess::get_modified_time(path + ".import");
  824. }
  825. if (fc && fc->modification_time == mt && fc->import_modification_time == import_mt && !_test_for_reimport(path, true)) {
  826. fi->type = fc->type;
  827. fi->resource_script_class = fc->resource_script_class;
  828. fi->uid = fc->uid;
  829. fi->deps = fc->deps;
  830. fi->modified_time = fc->modification_time;
  831. fi->import_modified_time = fc->import_modification_time;
  832. fi->import_valid = fc->import_valid;
  833. fi->script_class_name = fc->script_class_name;
  834. fi->import_group_file = fc->import_group_file;
  835. fi->script_class_extends = fc->script_class_extends;
  836. fi->script_class_icon_path = fc->script_class_icon_path;
  837. if (revalidate_import_files && !ResourceFormatImporter::get_singleton()->are_import_settings_valid(path)) {
  838. ItemAction ia;
  839. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  840. ia.dir = p_dir;
  841. ia.file = fi->file;
  842. scan_actions.push_back(ia);
  843. }
  844. if (fc->type.is_empty()) {
  845. fi->type = ResourceLoader::get_resource_type(path);
  846. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  847. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  848. //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?)
  849. //note: I think this should not happen any longer..
  850. }
  851. if (fc->uid == ResourceUID::INVALID_ID) {
  852. // imported files should always have a UID, so attempt to fetch it.
  853. fi->uid = ResourceLoader::get_resource_uid(path);
  854. }
  855. } else {
  856. fi->type = ResourceFormatImporter::get_singleton()->get_resource_type(path);
  857. fi->uid = ResourceFormatImporter::get_singleton()->get_resource_uid(path);
  858. fi->import_group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(path);
  859. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  860. fi->modified_time = 0;
  861. fi->import_modified_time = 0;
  862. fi->import_valid = fi->type == "TextFile" ? true : ResourceLoader::is_import_valid(path);
  863. ItemAction ia;
  864. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  865. ia.dir = p_dir;
  866. ia.file = fi->file;
  867. scan_actions.push_back(ia);
  868. }
  869. } else {
  870. if (fc && fc->modification_time == mt) {
  871. //not imported, so just update type if changed
  872. fi->type = fc->type;
  873. fi->resource_script_class = fc->resource_script_class;
  874. fi->uid = fc->uid;
  875. fi->modified_time = fc->modification_time;
  876. fi->deps = fc->deps;
  877. fi->import_modified_time = 0;
  878. fi->import_valid = true;
  879. fi->script_class_name = fc->script_class_name;
  880. fi->script_class_extends = fc->script_class_extends;
  881. fi->script_class_icon_path = fc->script_class_icon_path;
  882. if (first_scan && ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  883. bool update_script = false;
  884. String old_class_name = fi->script_class_name;
  885. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  886. if (old_class_name != fi->script_class_name) {
  887. update_script = true;
  888. } 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)) {
  889. // This script has a class name but is not in the global class names or the path of the class has changed.
  890. update_script = true;
  891. }
  892. if (update_script) {
  893. _queue_update_script_class(path, fi->type, fi->script_class_name, fi->script_class_extends, fi->script_class_icon_path);
  894. }
  895. }
  896. } else {
  897. //new or modified time
  898. fi->type = ResourceLoader::get_resource_type(path);
  899. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  900. if (fi->type == "" && textfile_extensions.has(ext)) {
  901. fi->type = "TextFile";
  902. }
  903. fi->uid = ResourceLoader::get_resource_uid(path);
  904. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  905. fi->deps = _get_dependencies(path);
  906. fi->modified_time = mt;
  907. fi->import_modified_time = 0;
  908. fi->import_valid = true;
  909. // Files in dep_update_list are forced for rescan to update dependencies. They don't need other updates.
  910. if (!dep_update_list.has(path)) {
  911. if (ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  912. _queue_update_script_class(path, fi->type, fi->script_class_name, fi->script_class_extends, fi->script_class_icon_path);
  913. }
  914. if (fi->type == SNAME("PackedScene")) {
  915. _queue_update_scene_groups(path);
  916. }
  917. }
  918. }
  919. }
  920. if (fi->uid != ResourceUID::INVALID_ID) {
  921. if (ResourceUID::get_singleton()->has_id(fi->uid)) {
  922. ResourceUID::get_singleton()->set_id(fi->uid, path);
  923. } else {
  924. ResourceUID::get_singleton()->add_id(fi->uid, path);
  925. }
  926. }
  927. p_progress.increment();
  928. }
  929. }
  930. void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanProgress &p_progress) {
  931. uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
  932. bool updated_dir = false;
  933. String cd = p_dir->get_path();
  934. int diff_nb_files = 0;
  935. if (current_mtime != p_dir->modified_time || using_fat32_or_exfat) {
  936. updated_dir = true;
  937. p_dir->modified_time = current_mtime;
  938. //ooooops, dir changed, see what's going on
  939. //first mark everything as verified
  940. for (int i = 0; i < p_dir->files.size(); i++) {
  941. p_dir->files[i]->verified = false;
  942. }
  943. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  944. p_dir->get_subdir(i)->verified = false;
  945. }
  946. diff_nb_files -= p_dir->files.size();
  947. //then scan files and directories and check what's different
  948. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  949. Error ret = da->change_dir(cd);
  950. ERR_FAIL_COND_MSG(ret != OK, "Cannot change to '" + cd + "' folder.");
  951. da->list_dir_begin();
  952. while (true) {
  953. String f = da->get_next();
  954. if (f.is_empty()) {
  955. break;
  956. }
  957. if (da->current_is_hidden()) {
  958. continue;
  959. }
  960. if (da->current_is_dir()) {
  961. if (f.begins_with(".")) { // Ignore special and . / ..
  962. continue;
  963. }
  964. int idx = p_dir->find_dir_index(f);
  965. if (idx == -1) {
  966. String dir_path = cd.path_join(f);
  967. if (_should_skip_directory(dir_path)) {
  968. continue;
  969. }
  970. ScannedDirectory sd;
  971. sd.name = f;
  972. sd.full_path = dir_path;
  973. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  974. efd->parent = p_dir;
  975. efd->name = f;
  976. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  977. d->change_dir(dir_path);
  978. int nb_files_dir = _scan_new_dir(&sd, d);
  979. p_progress.hi += nb_files_dir;
  980. diff_nb_files += nb_files_dir;
  981. _process_file_system(&sd, efd, p_progress);
  982. ItemAction ia;
  983. ia.action = ItemAction::ACTION_DIR_ADD;
  984. ia.dir = p_dir;
  985. ia.file = f;
  986. ia.new_dir = efd;
  987. scan_actions.push_back(ia);
  988. } else {
  989. p_dir->subdirs[idx]->verified = true;
  990. }
  991. } else {
  992. String ext = f.get_extension().to_lower();
  993. if (!valid_extensions.has(ext)) {
  994. continue; //invalid
  995. }
  996. int idx = p_dir->find_file_index(f);
  997. if (idx == -1) {
  998. //never seen this file, add actition to add it
  999. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1000. fi->file = f;
  1001. String path = cd.path_join(fi->file);
  1002. fi->modified_time = FileAccess::get_modified_time(path);
  1003. fi->import_modified_time = 0;
  1004. fi->type = ResourceLoader::get_resource_type(path);
  1005. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  1006. if (fi->type == "" && textfile_extensions.has(ext)) {
  1007. fi->type = "TextFile";
  1008. }
  1009. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  1010. fi->import_valid = fi->type == "TextFile" ? true : ResourceLoader::is_import_valid(path);
  1011. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  1012. {
  1013. ItemAction ia;
  1014. ia.action = ItemAction::ACTION_FILE_ADD;
  1015. ia.dir = p_dir;
  1016. ia.file = f;
  1017. ia.new_file = fi;
  1018. scan_actions.push_back(ia);
  1019. }
  1020. if (import_extensions.has(ext)) {
  1021. //if it can be imported, and it was added, it needs to be reimported
  1022. ItemAction ia;
  1023. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1024. ia.dir = p_dir;
  1025. ia.file = f;
  1026. scan_actions.push_back(ia);
  1027. }
  1028. diff_nb_files++;
  1029. } else {
  1030. p_dir->files[idx]->verified = true;
  1031. }
  1032. }
  1033. }
  1034. da->list_dir_end();
  1035. }
  1036. for (int i = 0; i < p_dir->files.size(); i++) {
  1037. if (updated_dir && !p_dir->files[i]->verified) {
  1038. //this file was removed, add action to remove it
  1039. ItemAction ia;
  1040. ia.action = ItemAction::ACTION_FILE_REMOVE;
  1041. ia.dir = p_dir;
  1042. ia.file = p_dir->files[i]->file;
  1043. scan_actions.push_back(ia);
  1044. diff_nb_files--;
  1045. continue;
  1046. }
  1047. String path = cd.path_join(p_dir->files[i]->file);
  1048. if (import_extensions.has(p_dir->files[i]->file.get_extension().to_lower())) {
  1049. //check here if file must be imported or not
  1050. uint64_t mt = FileAccess::get_modified_time(path);
  1051. bool reimport = false;
  1052. if (mt != p_dir->files[i]->modified_time) {
  1053. reimport = true; //it was modified, must be reimported.
  1054. } else if (!FileAccess::exists(path + ".import")) {
  1055. reimport = true; //no .import file, obviously reimport
  1056. } else {
  1057. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  1058. if (import_mt != p_dir->files[i]->import_modified_time) {
  1059. reimport = true;
  1060. } else if (_test_for_reimport(path, true)) {
  1061. reimport = true;
  1062. }
  1063. }
  1064. if (reimport) {
  1065. ItemAction ia;
  1066. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1067. ia.dir = p_dir;
  1068. ia.file = p_dir->files[i]->file;
  1069. scan_actions.push_back(ia);
  1070. }
  1071. } else if (ResourceCache::has(path)) { //test for potential reload
  1072. uint64_t mt = FileAccess::get_modified_time(path);
  1073. if (mt != p_dir->files[i]->modified_time) {
  1074. p_dir->files[i]->modified_time = mt; //save new time, but test for reload
  1075. ItemAction ia;
  1076. ia.action = ItemAction::ACTION_FILE_RELOAD;
  1077. ia.dir = p_dir;
  1078. ia.file = p_dir->files[i]->file;
  1079. scan_actions.push_back(ia);
  1080. }
  1081. }
  1082. p_progress.increment();
  1083. }
  1084. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1085. if ((updated_dir && !p_dir->subdirs[i]->verified) || _should_skip_directory(p_dir->subdirs[i]->get_path())) {
  1086. // Add all the files of the folder to be sure _update_scan_actions process the removed files
  1087. // for global class names.
  1088. diff_nb_files += _insert_actions_delete_files_directory(p_dir->subdirs[i]);
  1089. //this directory was removed or ignored, add action to remove it
  1090. ItemAction ia;
  1091. ia.action = ItemAction::ACTION_DIR_REMOVE;
  1092. ia.dir = p_dir->subdirs[i];
  1093. scan_actions.push_back(ia);
  1094. continue;
  1095. }
  1096. _scan_fs_changes(p_dir->get_subdir(i), p_progress);
  1097. }
  1098. nb_files_total = MAX(nb_files_total + diff_nb_files, 0);
  1099. }
  1100. void EditorFileSystem::_delete_internal_files(const String &p_file) {
  1101. if (FileAccess::exists(p_file + ".import")) {
  1102. List<String> paths;
  1103. ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
  1104. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1105. for (const String &E : paths) {
  1106. da->remove(E);
  1107. }
  1108. da->remove(p_file + ".import");
  1109. }
  1110. }
  1111. int EditorFileSystem::_insert_actions_delete_files_directory(EditorFileSystemDirectory *p_dir) {
  1112. int nb_files = 0;
  1113. for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
  1114. ItemAction ia;
  1115. ia.action = ItemAction::ACTION_FILE_REMOVE;
  1116. ia.dir = p_dir;
  1117. ia.file = fi->file;
  1118. scan_actions.push_back(ia);
  1119. nb_files++;
  1120. }
  1121. for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
  1122. nb_files += _insert_actions_delete_files_directory(sub_dir);
  1123. }
  1124. return nb_files;
  1125. }
  1126. void EditorFileSystem::_thread_func_sources(void *_userdata) {
  1127. EditorFileSystem *efs = (EditorFileSystem *)_userdata;
  1128. if (efs->filesystem) {
  1129. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  1130. ScanProgress sp;
  1131. sp.progress = &pr;
  1132. sp.hi = efs->nb_files_total;
  1133. efs->_scan_fs_changes(efs->filesystem, sp);
  1134. }
  1135. efs->scanning_changes_done.set();
  1136. }
  1137. void EditorFileSystem::_remove_invalid_global_class_names(const HashSet<String> &p_existing_class_names) {
  1138. List<StringName> global_classes;
  1139. bool must_save = false;
  1140. ScriptServer::get_global_class_list(&global_classes);
  1141. for (const StringName &class_name : global_classes) {
  1142. if (!p_existing_class_names.has(class_name)) {
  1143. ScriptServer::remove_global_class(class_name);
  1144. must_save = true;
  1145. }
  1146. }
  1147. if (must_save) {
  1148. ScriptServer::save_global_classes();
  1149. }
  1150. }
  1151. String EditorFileSystem::_get_file_by_class_name(EditorFileSystemDirectory *p_dir, const String &p_class_name, EditorFileSystemDirectory::FileInfo *&r_file_info) {
  1152. for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
  1153. if (fi->script_class_name == p_class_name) {
  1154. r_file_info = fi;
  1155. return p_dir->get_path().path_join(fi->file);
  1156. }
  1157. }
  1158. for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
  1159. String file = _get_file_by_class_name(sub_dir, p_class_name, r_file_info);
  1160. if (!file.is_empty()) {
  1161. return file;
  1162. }
  1163. }
  1164. r_file_info = nullptr;
  1165. return "";
  1166. }
  1167. void EditorFileSystem::scan_changes() {
  1168. if (first_scan || // Prevent a premature changes scan from inhibiting the first full scan
  1169. scanning || scanning_changes || thread.is_started()) {
  1170. scan_changes_pending = true;
  1171. set_process(true);
  1172. return;
  1173. }
  1174. _update_extensions();
  1175. sources_changed.clear();
  1176. scanning_changes = true;
  1177. scanning_changes_done.clear();
  1178. if (!use_threads) {
  1179. if (filesystem) {
  1180. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  1181. ScanProgress sp;
  1182. sp.progress = &pr;
  1183. sp.hi = nb_files_total;
  1184. scan_total = 0;
  1185. _scan_fs_changes(filesystem, sp);
  1186. if (_update_scan_actions()) {
  1187. emit_signal(SNAME("filesystem_changed"));
  1188. }
  1189. }
  1190. scanning_changes = false;
  1191. scanning_changes_done.set();
  1192. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1193. } else {
  1194. ERR_FAIL_COND(thread_sources.is_started());
  1195. set_process(true);
  1196. scan_total = 0;
  1197. Thread::Settings s;
  1198. s.priority = Thread::PRIORITY_LOW;
  1199. thread_sources.start(_thread_func_sources, this, s);
  1200. }
  1201. }
  1202. void EditorFileSystem::_notification(int p_what) {
  1203. switch (p_what) {
  1204. case NOTIFICATION_EXIT_TREE: {
  1205. Thread &active_thread = thread.is_started() ? thread : thread_sources;
  1206. if (use_threads && active_thread.is_started()) {
  1207. while (scanning) {
  1208. OS::get_singleton()->delay_usec(1000);
  1209. }
  1210. active_thread.wait_to_finish();
  1211. WARN_PRINT("Scan thread aborted...");
  1212. set_process(false);
  1213. }
  1214. if (filesystem) {
  1215. memdelete(filesystem);
  1216. }
  1217. if (new_filesystem) {
  1218. memdelete(new_filesystem);
  1219. }
  1220. filesystem = nullptr;
  1221. new_filesystem = nullptr;
  1222. } break;
  1223. case NOTIFICATION_PROCESS: {
  1224. if (use_threads) {
  1225. /** This hack exists because of the EditorProgress nature
  1226. * of processing events recursively. This needs to be rewritten
  1227. * at some point entirely, but in the meantime the following
  1228. * hack prevents deadlock on import.
  1229. */
  1230. static bool prevent_recursive_process_hack = false;
  1231. if (prevent_recursive_process_hack) {
  1232. break;
  1233. }
  1234. prevent_recursive_process_hack = true;
  1235. bool done_importing = false;
  1236. if (scanning_changes) {
  1237. if (scanning_changes_done.is_set()) {
  1238. set_process(false);
  1239. if (thread_sources.is_started()) {
  1240. thread_sources.wait_to_finish();
  1241. }
  1242. bool changed = _update_scan_actions();
  1243. // Set first_scan to false before the signals so the function doing_first_scan can return false
  1244. // in editor_node to start the export if needed.
  1245. first_scan = false;
  1246. if (changed) {
  1247. emit_signal(SNAME("filesystem_changed"));
  1248. }
  1249. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1250. scanning_changes = false; // Changed to false here to prevent recursive triggering of scan thread.
  1251. done_importing = true;
  1252. }
  1253. } else if (!scanning && thread.is_started()) {
  1254. set_process(false);
  1255. if (filesystem) {
  1256. memdelete(filesystem);
  1257. }
  1258. filesystem = new_filesystem;
  1259. new_filesystem = nullptr;
  1260. thread.wait_to_finish();
  1261. _update_scan_actions();
  1262. // Update all icons so they are loaded for the FileSystemDock.
  1263. _update_files_icon_path();
  1264. // Set first_scan to false before the signals so the function doing_first_scan can return false
  1265. // in editor_node to start the export if needed.
  1266. first_scan = false;
  1267. emit_signal(SNAME("filesystem_changed"));
  1268. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1269. }
  1270. if (done_importing && scan_changes_pending) {
  1271. scan_changes_pending = false;
  1272. scan_changes();
  1273. }
  1274. prevent_recursive_process_hack = false;
  1275. }
  1276. } break;
  1277. }
  1278. }
  1279. bool EditorFileSystem::is_scanning() const {
  1280. return scanning || scanning_changes || first_scan;
  1281. }
  1282. float EditorFileSystem::get_scanning_progress() const {
  1283. return scan_total;
  1284. }
  1285. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  1286. return filesystem;
  1287. }
  1288. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file) {
  1289. if (!p_dir) {
  1290. return; //none
  1291. }
  1292. p_file->store_line("::" + p_dir->get_path() + "::" + String::num(p_dir->modified_time));
  1293. for (int i = 0; i < p_dir->files.size(); i++) {
  1294. const EditorFileSystemDirectory::FileInfo *file_info = p_dir->files[i];
  1295. if (!file_info->import_group_file.is_empty()) {
  1296. group_file_cache.insert(file_info->import_group_file);
  1297. }
  1298. String type = file_info->type;
  1299. if (file_info->resource_script_class) {
  1300. type += "/" + String(file_info->resource_script_class);
  1301. }
  1302. PackedStringArray cache_string;
  1303. cache_string.append(file_info->file);
  1304. cache_string.append(type);
  1305. cache_string.append(itos(file_info->uid));
  1306. cache_string.append(itos(file_info->modified_time));
  1307. cache_string.append(itos(file_info->import_modified_time));
  1308. cache_string.append(itos(file_info->import_valid));
  1309. cache_string.append(file_info->import_group_file);
  1310. cache_string.append(String("<>").join({ file_info->script_class_name, file_info->script_class_extends, file_info->script_class_icon_path }));
  1311. cache_string.append(String("<>").join(file_info->deps));
  1312. p_file->store_line(String("::").join(cache_string));
  1313. }
  1314. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1315. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  1316. }
  1317. }
  1318. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  1319. //todo make faster
  1320. if (!filesystem || scanning) {
  1321. return false;
  1322. }
  1323. String f = ProjectSettings::get_singleton()->localize_path(p_file);
  1324. // Note: Only checks if base directory is case sensitive.
  1325. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1326. bool fs_case_sensitive = dir->is_case_sensitive("res://");
  1327. if (!f.begins_with("res://")) {
  1328. return false;
  1329. }
  1330. f = f.substr(6, f.length());
  1331. f = f.replace("\\", "/");
  1332. Vector<String> path = f.split("/");
  1333. if (path.size() == 0) {
  1334. return false;
  1335. }
  1336. String file = path[path.size() - 1];
  1337. path.resize(path.size() - 1);
  1338. EditorFileSystemDirectory *fs = filesystem;
  1339. for (int i = 0; i < path.size(); i++) {
  1340. if (path[i].begins_with(".")) {
  1341. return false;
  1342. }
  1343. int idx = -1;
  1344. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1345. if (fs_case_sensitive) {
  1346. if (fs->get_subdir(j)->get_name() == path[i]) {
  1347. idx = j;
  1348. break;
  1349. }
  1350. } else {
  1351. if (fs->get_subdir(j)->get_name().to_lower() == path[i].to_lower()) {
  1352. idx = j;
  1353. break;
  1354. }
  1355. }
  1356. }
  1357. if (idx == -1) {
  1358. // Only create a missing directory in memory when it exists on disk.
  1359. if (!dir->dir_exists(fs->get_path().path_join(path[i]))) {
  1360. return false;
  1361. }
  1362. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  1363. efsd->name = path[i];
  1364. efsd->parent = fs;
  1365. int idx2 = 0;
  1366. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1367. if (efsd->name.filenocasecmp_to(fs->get_subdir(j)->get_name()) < 0) {
  1368. break;
  1369. }
  1370. idx2++;
  1371. }
  1372. if (idx2 == fs->get_subdir_count()) {
  1373. fs->subdirs.push_back(efsd);
  1374. } else {
  1375. fs->subdirs.insert(idx2, efsd);
  1376. }
  1377. fs = efsd;
  1378. } else {
  1379. fs = fs->get_subdir(idx);
  1380. }
  1381. }
  1382. int cpos = -1;
  1383. for (int i = 0; i < fs->files.size(); i++) {
  1384. if (fs->files[i]->file == file) {
  1385. cpos = i;
  1386. break;
  1387. }
  1388. }
  1389. r_file_pos = cpos;
  1390. *r_d = fs;
  1391. return cpos != -1;
  1392. }
  1393. String EditorFileSystem::get_file_type(const String &p_file) const {
  1394. EditorFileSystemDirectory *fs = nullptr;
  1395. int cpos = -1;
  1396. if (!_find_file(p_file, &fs, cpos)) {
  1397. return "";
  1398. }
  1399. return fs->files[cpos]->type;
  1400. }
  1401. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  1402. if (!filesystem || scanning) {
  1403. return nullptr;
  1404. }
  1405. EditorFileSystemDirectory *fs = nullptr;
  1406. int cpos = -1;
  1407. if (!_find_file(p_file, &fs, cpos)) {
  1408. return nullptr;
  1409. }
  1410. if (r_index) {
  1411. *r_index = cpos;
  1412. }
  1413. return fs;
  1414. }
  1415. EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
  1416. if (!filesystem || scanning) {
  1417. return nullptr;
  1418. }
  1419. String f = ProjectSettings::get_singleton()->localize_path(p_path);
  1420. if (!f.begins_with("res://")) {
  1421. return nullptr;
  1422. }
  1423. f = f.substr(6, f.length());
  1424. f = f.replace("\\", "/");
  1425. if (f.is_empty()) {
  1426. return filesystem;
  1427. }
  1428. if (f.ends_with("/")) {
  1429. f = f.substr(0, f.length() - 1);
  1430. }
  1431. Vector<String> path = f.split("/");
  1432. if (path.size() == 0) {
  1433. return nullptr;
  1434. }
  1435. EditorFileSystemDirectory *fs = filesystem;
  1436. for (int i = 0; i < path.size(); i++) {
  1437. int idx = -1;
  1438. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1439. if (fs->get_subdir(j)->get_name() == path[i]) {
  1440. idx = j;
  1441. break;
  1442. }
  1443. }
  1444. if (idx == -1) {
  1445. return nullptr;
  1446. } else {
  1447. fs = fs->get_subdir(idx);
  1448. }
  1449. }
  1450. return fs;
  1451. }
  1452. void EditorFileSystem::_save_late_updated_files() {
  1453. //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
  1454. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  1455. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  1456. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  1457. for (const String &E : late_update_files) {
  1458. f->store_line(E);
  1459. }
  1460. }
  1461. Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
  1462. // Avoid error spam on first opening of a not yet imported project by treating the following situation
  1463. // as a benign one, not letting the file open error happen: the resource is of an importable type but
  1464. // it has not been imported yet.
  1465. if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
  1466. const String &internal_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
  1467. if (!internal_path.is_empty() && !FileAccess::exists(internal_path)) { // If path is empty (error), keep the code flow to the error.
  1468. return Vector<String>();
  1469. }
  1470. }
  1471. List<String> deps;
  1472. ResourceLoader::get_dependencies(p_path, &deps);
  1473. Vector<String> ret;
  1474. for (const String &E : deps) {
  1475. ret.push_back(E);
  1476. }
  1477. return ret;
  1478. }
  1479. String EditorFileSystem::_get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const {
  1480. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1481. if (ScriptServer::get_language(i)->handles_global_class_type(p_type)) {
  1482. String global_name;
  1483. String extends;
  1484. String icon_path;
  1485. global_name = ScriptServer::get_language(i)->get_global_class_name(p_path, &extends, &icon_path);
  1486. *r_extends = extends;
  1487. *r_icon_path = icon_path;
  1488. return global_name;
  1489. }
  1490. }
  1491. *r_extends = String();
  1492. *r_icon_path = String();
  1493. return String();
  1494. }
  1495. void EditorFileSystem::_update_file_icon_path(EditorFileSystemDirectory::FileInfo *file_info) {
  1496. String icon_path;
  1497. if (file_info->resource_script_class != StringName()) {
  1498. icon_path = EditorNode::get_editor_data().script_class_get_icon_path(file_info->resource_script_class);
  1499. } else if (file_info->script_class_icon_path.is_empty() && !file_info->deps.is_empty()) {
  1500. const String &script_dep = file_info->deps[0]; // Assuming the first dependency is a script.
  1501. const String &script_path = script_dep.contains("::") ? script_dep.get_slice("::", 2) : script_dep;
  1502. if (!script_path.is_empty()) {
  1503. String *cached = file_icon_cache.getptr(script_path);
  1504. if (cached) {
  1505. icon_path = *cached;
  1506. } else {
  1507. if (ClassDB::is_parent_class(ResourceLoader::get_resource_type(script_path), SNAME("Script"))) {
  1508. int script_file;
  1509. EditorFileSystemDirectory *efsd = find_file(script_path, &script_file);
  1510. if (efsd) {
  1511. icon_path = efsd->files[script_file]->script_class_icon_path;
  1512. }
  1513. }
  1514. file_icon_cache.insert(script_path, icon_path);
  1515. }
  1516. }
  1517. }
  1518. file_info->icon_path = icon_path;
  1519. }
  1520. void EditorFileSystem::_update_files_icon_path(EditorFileSystemDirectory *edp) {
  1521. if (!edp) {
  1522. edp = filesystem;
  1523. file_icon_cache.clear();
  1524. }
  1525. for (EditorFileSystemDirectory *sub_dir : edp->subdirs) {
  1526. _update_files_icon_path(sub_dir);
  1527. }
  1528. for (EditorFileSystemDirectory::FileInfo *fi : edp->files) {
  1529. _update_file_icon_path(fi);
  1530. }
  1531. }
  1532. void EditorFileSystem::_update_script_classes() {
  1533. if (update_script_paths.is_empty()) {
  1534. // Ensure the global class file is always present; it's essential for exports to work.
  1535. if (!FileAccess::exists(ProjectSettings::get_singleton()->get_global_class_list_path())) {
  1536. ScriptServer::save_global_classes();
  1537. }
  1538. return;
  1539. }
  1540. update_script_mutex.lock();
  1541. for (const KeyValue<String, ScriptInfo> &E : update_script_paths) {
  1542. _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);
  1543. }
  1544. update_script_paths.clear();
  1545. update_script_mutex.unlock();
  1546. ScriptServer::save_global_classes();
  1547. EditorNode::get_editor_data().script_class_save_icon_paths();
  1548. emit_signal("script_classes_updated");
  1549. // Rescan custom loaders and savers.
  1550. // Doing the following here because the `filesystem_changed` signal fires multiple times and isn't always followed by script classes update.
  1551. // So I thought it's better to do this when script classes really get updated
  1552. ResourceLoader::remove_custom_loaders();
  1553. ResourceLoader::add_custom_loaders();
  1554. ResourceSaver::remove_custom_savers();
  1555. ResourceSaver::add_custom_savers();
  1556. }
  1557. void EditorFileSystem::_update_script_documentation() {
  1558. if (update_script_paths_documentation.is_empty()) {
  1559. return;
  1560. }
  1561. update_script_mutex.lock();
  1562. for (const String &path : update_script_paths_documentation) {
  1563. int index = -1;
  1564. EditorFileSystemDirectory *efd = find_file(path, &index);
  1565. if (!efd || index < 0) {
  1566. // The file was removed
  1567. continue;
  1568. }
  1569. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1570. ScriptLanguage *lang = ScriptServer::get_language(i);
  1571. if (lang->supports_documentation() && efd->files[index]->type == lang->get_type()) {
  1572. Ref<Script> scr = ResourceLoader::load(path);
  1573. if (scr.is_null()) {
  1574. continue;
  1575. }
  1576. Vector<DocData::ClassDoc> docs = scr->get_documentation();
  1577. for (int j = 0; j < docs.size(); j++) {
  1578. EditorHelp::get_doc_data()->add_doc(docs[j]);
  1579. }
  1580. }
  1581. }
  1582. }
  1583. update_script_paths_documentation.clear();
  1584. update_script_mutex.unlock();
  1585. }
  1586. void EditorFileSystem::_process_update_pending() {
  1587. _update_script_classes();
  1588. // Parse documentation second, as it requires the class names to be loaded
  1589. // because _update_script_documentation loads the scripts completely.
  1590. _update_script_documentation();
  1591. _update_pending_scene_groups();
  1592. }
  1593. 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) {
  1594. update_script_mutex.lock();
  1595. ScriptInfo si;
  1596. si.type = p_type;
  1597. si.script_class_name = p_script_class_name;
  1598. si.script_class_extends = p_script_class_extends;
  1599. si.script_class_icon_path = p_script_class_icon_path;
  1600. update_script_paths.insert(p_path, si);
  1601. update_script_paths_documentation.insert(p_path);
  1602. update_script_mutex.unlock();
  1603. }
  1604. void EditorFileSystem::_update_scene_groups() {
  1605. if (update_scene_paths.is_empty()) {
  1606. return;
  1607. }
  1608. EditorProgress *ep = nullptr;
  1609. if (update_scene_paths.size() > 20) {
  1610. ep = memnew(EditorProgress("update_scene_groups", TTR("Update Scene Groups"), update_scene_paths.size()));
  1611. }
  1612. int step_count = 0;
  1613. update_scene_mutex.lock();
  1614. for (const String &path : update_scene_paths) {
  1615. ProjectSettings::get_singleton()->remove_scene_groups_cache(path);
  1616. int index = -1;
  1617. EditorFileSystemDirectory *efd = find_file(path, &index);
  1618. if (!efd || index < 0) {
  1619. // The file was removed.
  1620. continue;
  1621. }
  1622. const HashSet<StringName> scene_groups = PackedScene::get_scene_groups(path);
  1623. if (!scene_groups.is_empty()) {
  1624. ProjectSettings::get_singleton()->add_scene_groups_cache(path, scene_groups);
  1625. }
  1626. if (ep) {
  1627. ep->step(TTR("Updating Scene Groups..."), step_count++);
  1628. }
  1629. }
  1630. memdelete_notnull(ep);
  1631. update_scene_paths.clear();
  1632. update_scene_mutex.unlock();
  1633. ProjectSettings::get_singleton()->save_scene_groups_cache();
  1634. }
  1635. void EditorFileSystem::_update_pending_scene_groups() {
  1636. if (!FileAccess::exists(ProjectSettings::get_singleton()->get_scene_groups_cache_path())) {
  1637. _get_all_scenes(get_filesystem(), update_scene_paths);
  1638. _update_scene_groups();
  1639. } else if (!update_scene_paths.is_empty()) {
  1640. _update_scene_groups();
  1641. }
  1642. }
  1643. void EditorFileSystem::_queue_update_scene_groups(const String &p_path) {
  1644. update_scene_mutex.lock();
  1645. update_scene_paths.insert(p_path);
  1646. update_scene_mutex.unlock();
  1647. }
  1648. void EditorFileSystem::_get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet<String> &r_list) {
  1649. for (int i = 0; i < p_dir->get_file_count(); i++) {
  1650. if (p_dir->get_file_type(i) == SNAME("PackedScene")) {
  1651. r_list.insert(p_dir->get_file_path(i));
  1652. }
  1653. }
  1654. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1655. _get_all_scenes(p_dir->get_subdir(i), r_list);
  1656. }
  1657. }
  1658. void EditorFileSystem::update_file(const String &p_file) {
  1659. ERR_FAIL_COND(p_file.is_empty());
  1660. update_files({ p_file });
  1661. }
  1662. void EditorFileSystem::update_files(const Vector<String> &p_script_paths) {
  1663. bool updated = false;
  1664. bool update_files_icon_cache = false;
  1665. Vector<EditorFileSystemDirectory::FileInfo *> files_to_update_icon_path;
  1666. for (const String &file : p_script_paths) {
  1667. ERR_CONTINUE(file.is_empty());
  1668. EditorFileSystemDirectory *fs = nullptr;
  1669. int cpos = -1;
  1670. if (!_find_file(file, &fs, cpos)) {
  1671. if (!fs) {
  1672. continue;
  1673. }
  1674. }
  1675. if (!FileAccess::exists(file)) {
  1676. //was removed
  1677. _delete_internal_files(file);
  1678. if (cpos != -1) { // Might've never been part of the editor file system (*.* files deleted in Open dialog).
  1679. if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  1680. if (ResourceUID::get_singleton()->has_id(fs->files[cpos]->uid)) {
  1681. ResourceUID::get_singleton()->remove_id(fs->files[cpos]->uid);
  1682. }
  1683. }
  1684. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
  1685. _queue_update_script_class(file, fs->files[cpos]->type, "", "", "");
  1686. if (!fs->files[cpos]->script_class_icon_path.is_empty()) {
  1687. update_files_icon_cache = true;
  1688. }
  1689. }
  1690. if (fs->files[cpos]->type == SNAME("PackedScene")) {
  1691. _queue_update_scene_groups(file);
  1692. }
  1693. memdelete(fs->files[cpos]);
  1694. fs->files.remove_at(cpos);
  1695. updated = true;
  1696. }
  1697. } else {
  1698. String type = ResourceLoader::get_resource_type(file);
  1699. if (type.is_empty() && textfile_extensions.has(file.get_extension())) {
  1700. type = "TextFile";
  1701. }
  1702. String script_class = ResourceLoader::get_resource_script_class(file);
  1703. ResourceUID::ID uid = ResourceLoader::get_resource_uid(file);
  1704. if (cpos == -1) {
  1705. // The file did not exist, it was added.
  1706. int idx = 0;
  1707. String file_name = file.get_file();
  1708. for (int i = 0; i < fs->files.size(); i++) {
  1709. if (file.filenocasecmp_to(fs->files[i]->file) < 0) {
  1710. break;
  1711. }
  1712. idx++;
  1713. }
  1714. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1715. fi->file = file_name;
  1716. fi->import_modified_time = 0;
  1717. fi->import_valid = type == "TextFile" ? true : ResourceLoader::is_import_valid(file);
  1718. if (idx == fs->files.size()) {
  1719. fs->files.push_back(fi);
  1720. } else {
  1721. fs->files.insert(idx, fi);
  1722. }
  1723. cpos = idx;
  1724. } else {
  1725. //the file exists and it was updated, and was not added in this step.
  1726. //this means we must force upon next restart to scan it again, to get proper type and dependencies
  1727. late_update_files.insert(file);
  1728. _save_late_updated_files(); //files need to be updated in the re-scan
  1729. }
  1730. const String old_script_class_icon_path = fs->files[cpos]->script_class_icon_path;
  1731. const String old_class_name = fs->files[cpos]->script_class_name;
  1732. fs->files[cpos]->type = type;
  1733. fs->files[cpos]->resource_script_class = script_class;
  1734. fs->files[cpos]->uid = uid;
  1735. 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);
  1736. fs->files[cpos]->import_group_file = ResourceLoader::get_import_group_file(file);
  1737. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  1738. fs->files[cpos]->deps = _get_dependencies(file);
  1739. fs->files[cpos]->import_valid = type == "TextFile" ? true : ResourceLoader::is_import_valid(file);
  1740. if (uid != ResourceUID::INVALID_ID) {
  1741. if (ResourceUID::get_singleton()->has_id(uid)) {
  1742. ResourceUID::get_singleton()->set_id(uid, file);
  1743. } else {
  1744. ResourceUID::get_singleton()->add_id(uid, file);
  1745. }
  1746. ResourceUID::get_singleton()->update_cache();
  1747. }
  1748. // Update preview
  1749. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  1750. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
  1751. _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);
  1752. }
  1753. if (fs->files[cpos]->type == SNAME("PackedScene")) {
  1754. _queue_update_scene_groups(file);
  1755. }
  1756. if (fs->files[cpos]->type == SNAME("Resource")) {
  1757. files_to_update_icon_path.push_back(fs->files[cpos]);
  1758. } else if (old_script_class_icon_path != fs->files[cpos]->script_class_icon_path) {
  1759. update_files_icon_cache = true;
  1760. }
  1761. // Restore another script as the global class name if multiple scripts had the same old class name.
  1762. if (!old_class_name.is_empty() && fs->files[cpos]->script_class_name != old_class_name && ClassDB::is_parent_class(type, SNAME("Script"))) {
  1763. EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
  1764. String old_file = _get_file_by_class_name(filesystem, old_class_name, old_fi);
  1765. if (!old_file.is_empty() && old_fi) {
  1766. _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);
  1767. }
  1768. }
  1769. updated = true;
  1770. }
  1771. }
  1772. if (updated) {
  1773. if (update_files_icon_cache) {
  1774. _update_files_icon_path();
  1775. } else {
  1776. for (EditorFileSystemDirectory::FileInfo *fi : files_to_update_icon_path) {
  1777. _update_file_icon_path(fi);
  1778. }
  1779. }
  1780. if (!is_scanning()) {
  1781. _process_update_pending();
  1782. }
  1783. call_deferred(SNAME("emit_signal"), "filesystem_changed"); // Update later
  1784. }
  1785. }
  1786. HashSet<String> EditorFileSystem::get_valid_extensions() const {
  1787. return valid_extensions;
  1788. }
  1789. 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) {
  1790. ScriptServer::remove_global_class_by_path(p_search_path); // First remove, just in case it changed
  1791. if (p_script_class_name.is_empty()) {
  1792. return;
  1793. }
  1794. String lang;
  1795. for (int j = 0; j < ScriptServer::get_language_count(); j++) {
  1796. if (ScriptServer::get_language(j)->handles_global_class_type(p_type)) {
  1797. lang = ScriptServer::get_language(j)->get_name();
  1798. break;
  1799. }
  1800. }
  1801. if (lang.is_empty()) {
  1802. return; // No lang found that can handle this global class
  1803. }
  1804. ScriptServer::add_global_class(p_script_class_name, p_script_class_extends, lang, p_target_path);
  1805. EditorNode::get_editor_data().script_class_set_icon_path(p_script_class_name, p_script_class_icon_path);
  1806. EditorNode::get_editor_data().script_class_set_name(p_target_path, p_script_class_name);
  1807. }
  1808. void EditorFileSystem::register_global_class_script(const String &p_search_path, const String &p_target_path) {
  1809. int index_file;
  1810. EditorFileSystemDirectory *efsd = find_file(p_search_path, &index_file);
  1811. if (efsd) {
  1812. const EditorFileSystemDirectory::FileInfo *fi = efsd->files[index_file];
  1813. 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);
  1814. } else {
  1815. ScriptServer::remove_global_class_by_path(p_search_path);
  1816. }
  1817. }
  1818. Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) {
  1819. String importer_name;
  1820. HashMap<String, HashMap<StringName, Variant>> source_file_options;
  1821. HashMap<String, ResourceUID::ID> uids;
  1822. HashMap<String, String> base_paths;
  1823. for (int i = 0; i < p_files.size(); i++) {
  1824. Ref<ConfigFile> config;
  1825. config.instantiate();
  1826. Error err = config->load(p_files[i] + ".import");
  1827. ERR_CONTINUE(err != OK);
  1828. ERR_CONTINUE(!config->has_section_key("remap", "importer"));
  1829. String file_importer_name = config->get_value("remap", "importer");
  1830. ERR_CONTINUE(file_importer_name.is_empty());
  1831. if (!importer_name.is_empty() && importer_name != file_importer_name) {
  1832. EditorNode::get_singleton()->show_warning(vformat(TTR("There are multiple importers for different types pointing to file %s, import aborted"), p_group_file));
  1833. ERR_FAIL_V(ERR_FILE_CORRUPT);
  1834. }
  1835. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  1836. if (config->has_section_key("remap", "uid")) {
  1837. String uidt = config->get_value("remap", "uid");
  1838. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  1839. }
  1840. uids[p_files[i]] = uid;
  1841. source_file_options[p_files[i]] = HashMap<StringName, Variant>();
  1842. importer_name = file_importer_name;
  1843. if (importer_name == "keep" || importer_name == "skip") {
  1844. continue; //do nothing
  1845. }
  1846. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1847. ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT);
  1848. List<ResourceImporter::ImportOption> options;
  1849. importer->get_import_options(p_files[i], &options);
  1850. //set default values
  1851. for (const ResourceImporter::ImportOption &E : options) {
  1852. source_file_options[p_files[i]][E.option.name] = E.default_value;
  1853. }
  1854. if (config->has_section("params")) {
  1855. List<String> sk;
  1856. config->get_section_keys("params", &sk);
  1857. for (const String &param : sk) {
  1858. Variant value = config->get_value("params", param);
  1859. //override with whatever is in file
  1860. source_file_options[p_files[i]][param] = value;
  1861. }
  1862. }
  1863. base_paths[p_files[i]] = ResourceFormatImporter::get_singleton()->get_import_base_path(p_files[i]);
  1864. }
  1865. if (importer_name == "keep" || importer_name == "skip") {
  1866. return OK; // (do nothing)
  1867. }
  1868. ERR_FAIL_COND_V(importer_name.is_empty(), ERR_UNCONFIGURED);
  1869. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1870. Error err = importer->import_group_file(p_group_file, source_file_options, base_paths);
  1871. //all went well, overwrite config files with proper remaps and md5s
  1872. for (const KeyValue<String, HashMap<StringName, Variant>> &E : source_file_options) {
  1873. const String &file = E.key;
  1874. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(file);
  1875. Vector<String> dest_paths;
  1876. ResourceUID::ID uid = uids[file];
  1877. {
  1878. Ref<FileAccess> f = FileAccess::open(file + ".import", FileAccess::WRITE);
  1879. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'.");
  1880. //write manually, as order matters ([remap] has to go first for performance).
  1881. f->store_line("[remap]");
  1882. f->store_line("");
  1883. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  1884. int version = importer->get_format_version();
  1885. if (version > 0) {
  1886. f->store_line("importer_version=" + itos(version));
  1887. }
  1888. if (!importer->get_resource_type().is_empty()) {
  1889. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  1890. }
  1891. if (uid == ResourceUID::INVALID_ID) {
  1892. uid = ResourceUID::get_singleton()->create_id();
  1893. }
  1894. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
  1895. if (err == OK) {
  1896. String path = base_path + "." + importer->get_save_extension();
  1897. f->store_line("path=\"" + path + "\"");
  1898. dest_paths.push_back(path);
  1899. }
  1900. f->store_line("group_file=" + Variant(p_group_file).get_construct_string());
  1901. if (err == OK) {
  1902. f->store_line("valid=true");
  1903. } else {
  1904. f->store_line("valid=false");
  1905. }
  1906. f->store_line("[deps]\n");
  1907. f->store_line("");
  1908. f->store_line("source_file=" + Variant(file).get_construct_string());
  1909. if (dest_paths.size()) {
  1910. Array dp;
  1911. for (int i = 0; i < dest_paths.size(); i++) {
  1912. dp.push_back(dest_paths[i]);
  1913. }
  1914. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  1915. }
  1916. f->store_line("[params]");
  1917. f->store_line("");
  1918. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  1919. List<ResourceImporter::ImportOption> options;
  1920. importer->get_import_options(file, &options);
  1921. //set default values
  1922. for (const ResourceImporter::ImportOption &F : options) {
  1923. String base = F.option.name;
  1924. Variant v = F.default_value;
  1925. if (source_file_options[file].has(base)) {
  1926. v = source_file_options[file][base];
  1927. }
  1928. String value;
  1929. VariantWriter::write_to_string(v, value);
  1930. f->store_line(base + "=" + value);
  1931. }
  1932. }
  1933. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  1934. {
  1935. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  1936. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  1937. md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\"");
  1938. if (dest_paths.size()) {
  1939. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  1940. }
  1941. }
  1942. EditorFileSystemDirectory *fs = nullptr;
  1943. int cpos = -1;
  1944. bool found = _find_file(file, &fs, cpos);
  1945. ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, "Can't find file '" + file + "'.");
  1946. //update modified times, to avoid reimport
  1947. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  1948. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(file + ".import");
  1949. fs->files[cpos]->deps = _get_dependencies(file);
  1950. fs->files[cpos]->uid = uid;
  1951. fs->files[cpos]->type = importer->get_resource_type();
  1952. if (fs->files[cpos]->type == "" && textfile_extensions.has(file.get_extension())) {
  1953. fs->files[cpos]->type = "TextFile";
  1954. }
  1955. fs->files[cpos]->import_valid = err == OK;
  1956. if (ResourceUID::get_singleton()->has_id(uid)) {
  1957. ResourceUID::get_singleton()->set_id(uid, file);
  1958. } else {
  1959. ResourceUID::get_singleton()->add_id(uid, file);
  1960. }
  1961. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  1962. //to reload properly
  1963. Ref<Resource> r = ResourceCache::get_ref(file);
  1964. if (r.is_valid()) {
  1965. if (!r->get_import_path().is_empty()) {
  1966. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(file);
  1967. r->set_import_path(dst_path);
  1968. r->set_import_last_modified_time(0);
  1969. }
  1970. }
  1971. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  1972. }
  1973. return err;
  1974. }
  1975. Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant *p_generator_parameters) {
  1976. print_verbose(vformat("EditorFileSystem: Importing file: %s", p_file));
  1977. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  1978. EditorFileSystemDirectory *fs = nullptr;
  1979. int cpos = -1;
  1980. bool found = _find_file(p_file, &fs, cpos);
  1981. ERR_FAIL_COND_V_MSG(!found, ERR_FILE_NOT_FOUND, "Can't find file '" + p_file + "'.");
  1982. //try to obtain existing params
  1983. HashMap<StringName, Variant> params = p_custom_options;
  1984. String importer_name; //empty by default though
  1985. if (!p_custom_importer.is_empty()) {
  1986. importer_name = p_custom_importer;
  1987. }
  1988. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  1989. Variant generator_parameters;
  1990. if (p_generator_parameters) {
  1991. generator_parameters = *p_generator_parameters;
  1992. }
  1993. if (FileAccess::exists(p_file + ".import")) {
  1994. //use existing
  1995. Ref<ConfigFile> cf;
  1996. cf.instantiate();
  1997. Error err = cf->load(p_file + ".import");
  1998. if (err == OK) {
  1999. if (cf->has_section("params")) {
  2000. List<String> sk;
  2001. cf->get_section_keys("params", &sk);
  2002. for (const String &E : sk) {
  2003. if (!params.has(E)) {
  2004. params[E] = cf->get_value("params", E);
  2005. }
  2006. }
  2007. }
  2008. if (cf->has_section("remap")) {
  2009. if (p_custom_importer.is_empty()) {
  2010. importer_name = cf->get_value("remap", "importer");
  2011. }
  2012. if (cf->has_section_key("remap", "uid")) {
  2013. String uidt = cf->get_value("remap", "uid");
  2014. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  2015. }
  2016. if (!p_generator_parameters) {
  2017. if (cf->has_section_key("remap", "generator_parameters")) {
  2018. generator_parameters = cf->get_value("remap", "generator_parameters");
  2019. }
  2020. }
  2021. }
  2022. }
  2023. }
  2024. if (importer_name == "keep" || importer_name == "skip") {
  2025. //keep files, do nothing.
  2026. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  2027. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  2028. fs->files[cpos]->deps.clear();
  2029. fs->files[cpos]->type = "";
  2030. fs->files[cpos]->import_valid = false;
  2031. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  2032. return OK;
  2033. }
  2034. Ref<ResourceImporter> importer;
  2035. bool load_default = false;
  2036. //find the importer
  2037. if (!importer_name.is_empty()) {
  2038. importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2039. }
  2040. if (importer.is_null()) {
  2041. //not found by name, find by extension
  2042. importer = ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_file.get_extension());
  2043. load_default = true;
  2044. if (importer.is_null()) {
  2045. 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.");
  2046. }
  2047. }
  2048. if (FileAccess::exists(p_file + ".import")) {
  2049. // We only want to handle compat for existing files, not new ones.
  2050. importer->handle_compatibility_options(params);
  2051. }
  2052. //mix with default params, in case a parameter is missing
  2053. List<ResourceImporter::ImportOption> opts;
  2054. importer->get_import_options(p_file, &opts);
  2055. for (const ResourceImporter::ImportOption &E : opts) {
  2056. if (!params.has(E.option.name)) { //this one is not present
  2057. params[E.option.name] = E.default_value;
  2058. }
  2059. }
  2060. if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  2061. //use defaults if exist
  2062. Dictionary d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
  2063. List<Variant> v;
  2064. d.get_key_list(&v);
  2065. for (const Variant &E : v) {
  2066. params[E] = d[E];
  2067. }
  2068. }
  2069. //finally, perform import!!
  2070. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
  2071. List<String> import_variants;
  2072. List<String> gen_files;
  2073. Variant meta;
  2074. Error err = importer->import(p_file, base_path, params, &import_variants, &gen_files, &meta);
  2075. // As import is complete, save the .import file.
  2076. Vector<String> dest_paths;
  2077. {
  2078. Ref<FileAccess> f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
  2079. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open file from path '" + p_file + ".import'.");
  2080. // Write manually, as order matters ([remap] has to go first for performance).
  2081. f->store_line("[remap]");
  2082. f->store_line("");
  2083. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  2084. int version = importer->get_format_version();
  2085. if (version > 0) {
  2086. f->store_line("importer_version=" + itos(version));
  2087. }
  2088. if (!importer->get_resource_type().is_empty()) {
  2089. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  2090. }
  2091. if (uid == ResourceUID::INVALID_ID) {
  2092. uid = ResourceUID::get_singleton()->create_id();
  2093. }
  2094. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
  2095. if (err == OK) {
  2096. if (importer->get_save_extension().is_empty()) {
  2097. //no path
  2098. } else if (import_variants.size()) {
  2099. //import with variants
  2100. for (const String &E : import_variants) {
  2101. String path = base_path.c_escape() + "." + E + "." + importer->get_save_extension();
  2102. f->store_line("path." + E + "=\"" + path + "\"");
  2103. dest_paths.push_back(path);
  2104. }
  2105. } else {
  2106. String path = base_path + "." + importer->get_save_extension();
  2107. f->store_line("path=\"" + path + "\"");
  2108. dest_paths.push_back(path);
  2109. }
  2110. } else {
  2111. f->store_line("valid=false");
  2112. }
  2113. if (meta != Variant()) {
  2114. f->store_line("metadata=" + meta.get_construct_string());
  2115. }
  2116. if (generator_parameters != Variant()) {
  2117. f->store_line("generator_parameters=" + generator_parameters.get_construct_string());
  2118. }
  2119. f->store_line("");
  2120. f->store_line("[deps]\n");
  2121. if (gen_files.size()) {
  2122. Array genf;
  2123. for (const String &E : gen_files) {
  2124. genf.push_back(E);
  2125. dest_paths.push_back(E);
  2126. }
  2127. String value;
  2128. VariantWriter::write_to_string(genf, value);
  2129. f->store_line("files=" + value);
  2130. f->store_line("");
  2131. }
  2132. f->store_line("source_file=" + Variant(p_file).get_construct_string());
  2133. if (dest_paths.size()) {
  2134. Array dp;
  2135. for (int i = 0; i < dest_paths.size(); i++) {
  2136. dp.push_back(dest_paths[i]);
  2137. }
  2138. f->store_line("dest_files=" + Variant(dp).get_construct_string());
  2139. }
  2140. f->store_line("");
  2141. f->store_line("[params]");
  2142. f->store_line("");
  2143. // Store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  2144. for (const ResourceImporter::ImportOption &E : opts) {
  2145. String base = E.option.name;
  2146. String value;
  2147. VariantWriter::write_to_string(params[base], value);
  2148. f->store_line(base + "=" + value);
  2149. }
  2150. }
  2151. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  2152. {
  2153. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  2154. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  2155. md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"");
  2156. if (dest_paths.size()) {
  2157. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  2158. }
  2159. }
  2160. // Update cpos, newly created files could've changed the index of the reimported p_file.
  2161. _find_file(p_file, &fs, cpos);
  2162. // Update modified times, to avoid reimport.
  2163. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  2164. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  2165. fs->files[cpos]->deps = _get_dependencies(p_file);
  2166. fs->files[cpos]->type = importer->get_resource_type();
  2167. fs->files[cpos]->uid = uid;
  2168. fs->files[cpos]->import_valid = fs->files[cpos]->type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  2169. if (ResourceUID::get_singleton()->has_id(uid)) {
  2170. ResourceUID::get_singleton()->set_id(uid, p_file);
  2171. } else {
  2172. ResourceUID::get_singleton()->add_id(uid, p_file);
  2173. }
  2174. // If file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  2175. // to reload properly.
  2176. Ref<Resource> r = ResourceCache::get_ref(p_file);
  2177. if (r.is_valid()) {
  2178. if (!r->get_import_path().is_empty()) {
  2179. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
  2180. r->set_import_path(dst_path);
  2181. r->set_import_last_modified_time(0);
  2182. }
  2183. }
  2184. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  2185. print_verbose(vformat("EditorFileSystem: \"%s\" import took %d ms.", p_file, OS::get_singleton()->get_ticks_msec() - start_time));
  2186. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_UNRECOGNIZED, "Error importing '" + p_file + "'.");
  2187. return OK;
  2188. }
  2189. void EditorFileSystem::_find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport) {
  2190. int fc = efd->files.size();
  2191. const EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptr();
  2192. for (int i = 0; i < fc; i++) {
  2193. if (groups_to_reimport.has(files[i]->import_group_file)) {
  2194. if (!group_files.has(files[i]->import_group_file)) {
  2195. group_files[files[i]->import_group_file] = Vector<String>();
  2196. }
  2197. group_files[files[i]->import_group_file].push_back(efd->get_file_path(i));
  2198. }
  2199. }
  2200. for (int i = 0; i < efd->get_subdir_count(); i++) {
  2201. _find_group_files(efd->get_subdir(i), group_files, groups_to_reimport);
  2202. }
  2203. }
  2204. void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params) {
  2205. Vector<String> reloads;
  2206. reloads.append(p_file);
  2207. // Emit the resource_reimporting signal for the single file before the actual importation.
  2208. emit_signal(SNAME("resources_reimporting"), reloads);
  2209. _reimport_file(p_file, p_custom_params, p_importer);
  2210. // Emit the resource_reimported signal for the single file we just reimported.
  2211. emit_signal(SNAME("resources_reimported"), reloads);
  2212. }
  2213. void EditorFileSystem::_reimport_thread(uint32_t p_index, ImportThreadData *p_import_data) {
  2214. int current_max = p_import_data->reimport_from + int(p_index);
  2215. p_import_data->max_index.exchange_if_greater(current_max);
  2216. _reimport_file(p_import_data->reimport_files[current_max].path);
  2217. }
  2218. void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
  2219. ERR_FAIL_COND_MSG(importing, "Attempted to call reimport_files() recursively, this is not allowed.");
  2220. importing = true;
  2221. Vector<String> reloads;
  2222. EditorProgress pr("reimport", TTR("(Re)Importing Assets"), p_files.size());
  2223. Vector<ImportFile> reimport_files;
  2224. HashSet<String> groups_to_reimport;
  2225. for (int i = 0; i < p_files.size(); i++) {
  2226. String file = p_files[i];
  2227. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(file);
  2228. if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
  2229. file = ResourceUID::get_singleton()->get_id_path(uid);
  2230. }
  2231. String group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(file);
  2232. if (group_file_cache.has(file)) {
  2233. // Maybe the file itself is a group!
  2234. groups_to_reimport.insert(file);
  2235. // Groups do not belong to groups.
  2236. group_file = String();
  2237. } else if (groups_to_reimport.has(file)) {
  2238. // Groups do not belong to groups.
  2239. group_file = String();
  2240. } else if (!group_file.is_empty()) {
  2241. // It's a group file, add group to import and skip this file.
  2242. groups_to_reimport.insert(group_file);
  2243. } else {
  2244. // It's a regular file.
  2245. ImportFile ifile;
  2246. ifile.path = file;
  2247. ResourceFormatImporter::get_singleton()->get_import_order_threads_and_importer(file, ifile.order, ifile.threaded, ifile.importer);
  2248. reloads.push_back(file);
  2249. reimport_files.push_back(ifile);
  2250. }
  2251. // Group may have changed, so also update group reference.
  2252. EditorFileSystemDirectory *fs = nullptr;
  2253. int cpos = -1;
  2254. if (_find_file(file, &fs, cpos)) {
  2255. fs->files.write[cpos]->import_group_file = group_file;
  2256. }
  2257. }
  2258. reimport_files.sort();
  2259. // Emit the resource_reimporting signal for the single file before the actual importation.
  2260. emit_signal(SNAME("resources_reimporting"), reloads);
  2261. #ifdef THREADS_ENABLED
  2262. bool use_multiple_threads = GLOBAL_GET("editor/import/use_multiple_threads");
  2263. #else
  2264. bool use_multiple_threads = false;
  2265. #endif
  2266. int from = 0;
  2267. for (int i = 0; i < reimport_files.size(); i++) {
  2268. if (groups_to_reimport.has(reimport_files[i].path)) {
  2269. from = i + 1;
  2270. continue;
  2271. }
  2272. if (use_multiple_threads && reimport_files[i].threaded) {
  2273. if (i + 1 == reimport_files.size() || reimport_files[i + 1].importer != reimport_files[from].importer || groups_to_reimport.has(reimport_files[i + 1].path)) {
  2274. if (from - i == 0) {
  2275. // Single file, do not use threads.
  2276. pr.step(reimport_files[i].path.get_file(), i);
  2277. _reimport_file(reimport_files[i].path);
  2278. } else {
  2279. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(reimport_files[from].importer);
  2280. if (importer.is_null()) {
  2281. ERR_PRINT(vformat("Invalid importer for \"%s\".", reimport_files[from].importer));
  2282. from = i + 1;
  2283. continue;
  2284. }
  2285. importer->import_threaded_begin();
  2286. ImportThreadData tdata;
  2287. tdata.max_index.set(from);
  2288. tdata.reimport_from = from;
  2289. tdata.reimport_files = reimport_files.ptr();
  2290. 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));
  2291. int current_index = from - 1;
  2292. do {
  2293. if (current_index < tdata.max_index.get()) {
  2294. current_index = tdata.max_index.get();
  2295. pr.step(reimport_files[current_index].path.get_file(), current_index);
  2296. }
  2297. OS::get_singleton()->delay_usec(1);
  2298. } while (!WorkerThreadPool::get_singleton()->is_group_task_completed(group_task));
  2299. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  2300. importer->import_threaded_end();
  2301. }
  2302. from = i + 1;
  2303. }
  2304. } else {
  2305. pr.step(reimport_files[i].path.get_file(), i);
  2306. _reimport_file(reimport_files[i].path);
  2307. // We need to increment the counter, maybe the next file is multithreaded
  2308. // and doesn't have the same importer.
  2309. from = i + 1;
  2310. }
  2311. }
  2312. // Reimport groups.
  2313. from = reimport_files.size();
  2314. if (groups_to_reimport.size()) {
  2315. HashMap<String, Vector<String>> group_files;
  2316. _find_group_files(filesystem, group_files, groups_to_reimport);
  2317. for (const KeyValue<String, Vector<String>> &E : group_files) {
  2318. pr.step(E.key.get_file(), from++);
  2319. Error err = _reimport_group(E.key, E.value);
  2320. reloads.push_back(E.key);
  2321. reloads.append_array(E.value);
  2322. if (err == OK) {
  2323. _reimport_file(E.key);
  2324. }
  2325. }
  2326. }
  2327. ResourceUID::get_singleton()->update_cache(); // After reimporting, update the cache.
  2328. _save_filesystem_cache();
  2329. _process_update_pending();
  2330. importing = false;
  2331. if (!is_scanning()) {
  2332. emit_signal(SNAME("filesystem_changed"));
  2333. }
  2334. emit_signal(SNAME("resources_reimported"), reloads);
  2335. }
  2336. Error EditorFileSystem::reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
  2337. ERR_FAIL_COND_V_MSG(!importing, ERR_INVALID_PARAMETER, "Can only append files to import during a current reimport process.");
  2338. Vector<String> reloads;
  2339. reloads.append(p_file);
  2340. // Emit the resource_reimporting signal for the single file before the actual importation.
  2341. emit_signal(SNAME("resources_reimporting"), reloads);
  2342. Error ret = _reimport_file(p_file, p_custom_options, p_custom_importer, &p_generator_parameters);
  2343. // Emit the resource_reimported signal for the single file we just reimported.
  2344. emit_signal(SNAME("resources_reimported"), reloads);
  2345. return ret;
  2346. }
  2347. Error EditorFileSystem::_resource_import(const String &p_path) {
  2348. Vector<String> files;
  2349. files.push_back(p_path);
  2350. singleton->update_file(p_path);
  2351. singleton->reimport_files(files);
  2352. return OK;
  2353. }
  2354. bool EditorFileSystem::_should_skip_directory(const String &p_path) {
  2355. String project_data_path = ProjectSettings::get_singleton()->get_project_data_path();
  2356. if (p_path == project_data_path || p_path.begins_with(project_data_path + "/")) {
  2357. return true;
  2358. }
  2359. if (FileAccess::exists(p_path.path_join("project.godot"))) {
  2360. // Skip if another project inside this.
  2361. if (EditorFileSystem::get_singleton()->first_scan) {
  2362. WARN_PRINT_ONCE(vformat("Detected another project.godot at %s. The folder will be ignored.", p_path));
  2363. }
  2364. return true;
  2365. }
  2366. if (FileAccess::exists(p_path.path_join(".gdignore"))) {
  2367. // Skip if a `.gdignore` file is inside this.
  2368. return true;
  2369. }
  2370. return false;
  2371. }
  2372. bool EditorFileSystem::is_group_file(const String &p_path) const {
  2373. return group_file_cache.has(p_path);
  2374. }
  2375. void EditorFileSystem::_move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location) {
  2376. int fc = efd->files.size();
  2377. EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptrw();
  2378. for (int i = 0; i < fc; i++) {
  2379. if (files[i]->import_group_file == p_group_file) {
  2380. files[i]->import_group_file = p_new_location;
  2381. Ref<ConfigFile> config;
  2382. config.instantiate();
  2383. String path = efd->get_file_path(i) + ".import";
  2384. Error err = config->load(path);
  2385. if (err != OK) {
  2386. continue;
  2387. }
  2388. if (config->has_section_key("remap", "group_file")) {
  2389. config->set_value("remap", "group_file", p_new_location);
  2390. }
  2391. List<String> sk;
  2392. config->get_section_keys("params", &sk);
  2393. for (const String &param : sk) {
  2394. //not very clean, but should work
  2395. String value = config->get_value("params", param);
  2396. if (value == p_group_file) {
  2397. config->set_value("params", param, p_new_location);
  2398. }
  2399. }
  2400. config->save(path);
  2401. }
  2402. }
  2403. for (int i = 0; i < efd->get_subdir_count(); i++) {
  2404. _move_group_files(efd->get_subdir(i), p_group_file, p_new_location);
  2405. }
  2406. }
  2407. void EditorFileSystem::move_group_file(const String &p_path, const String &p_new_path) {
  2408. if (get_filesystem()) {
  2409. _move_group_files(get_filesystem(), p_path, p_new_path);
  2410. if (group_file_cache.has(p_path)) {
  2411. group_file_cache.erase(p_path);
  2412. group_file_cache.insert(p_new_path);
  2413. }
  2414. }
  2415. }
  2416. ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate) {
  2417. if (!p_path.is_resource_file() || p_path.begins_with(ProjectSettings::get_singleton()->get_project_data_path())) {
  2418. // Saved externally (configuration file) or internal file, do not assign an ID.
  2419. return ResourceUID::INVALID_ID;
  2420. }
  2421. EditorFileSystemDirectory *fs = nullptr;
  2422. int cpos = -1;
  2423. if (!singleton->_find_file(p_path, &fs, cpos)) {
  2424. // Fallback to ResourceLoader if filesystem cache fails (can happen during scanning etc.).
  2425. ResourceUID::ID fallback = ResourceLoader::get_resource_uid(p_path);
  2426. if (fallback != ResourceUID::INVALID_ID) {
  2427. return fallback;
  2428. }
  2429. if (p_generate) {
  2430. 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.
  2431. } else {
  2432. return ResourceUID::INVALID_ID;
  2433. }
  2434. } else if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  2435. return fs->files[cpos]->uid;
  2436. } else if (p_generate) {
  2437. 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.
  2438. } else {
  2439. return ResourceUID::INVALID_ID;
  2440. }
  2441. }
  2442. static void _scan_extensions_dir(EditorFileSystemDirectory *d, HashSet<String> &extensions) {
  2443. int fc = d->get_file_count();
  2444. for (int i = 0; i < fc; i++) {
  2445. if (d->get_file_type(i) == SNAME("GDExtension")) {
  2446. extensions.insert(d->get_file_path(i));
  2447. }
  2448. }
  2449. int dc = d->get_subdir_count();
  2450. for (int i = 0; i < dc; i++) {
  2451. _scan_extensions_dir(d->get_subdir(i), extensions);
  2452. }
  2453. }
  2454. bool EditorFileSystem::_scan_extensions() {
  2455. EditorFileSystemDirectory *d = get_filesystem();
  2456. HashSet<String> extensions;
  2457. _scan_extensions_dir(d, extensions);
  2458. //verify against loaded extensions
  2459. Vector<String> extensions_added;
  2460. Vector<String> extensions_removed;
  2461. for (const String &E : extensions) {
  2462. if (!GDExtensionManager::get_singleton()->is_extension_loaded(E)) {
  2463. extensions_added.push_back(E);
  2464. }
  2465. }
  2466. Vector<String> loaded_extensions = GDExtensionManager::get_singleton()->get_loaded_extensions();
  2467. for (int i = 0; i < loaded_extensions.size(); i++) {
  2468. if (!extensions.has(loaded_extensions[i])) {
  2469. extensions_removed.push_back(loaded_extensions[i]);
  2470. }
  2471. }
  2472. String extension_list_config_file = GDExtension::get_extension_list_config_file();
  2473. if (extensions.size()) {
  2474. if (extensions_added.size() || extensions_removed.size()) { //extensions were added or removed
  2475. Ref<FileAccess> f = FileAccess::open(extension_list_config_file, FileAccess::WRITE);
  2476. for (const String &E : extensions) {
  2477. f->store_line(E);
  2478. }
  2479. }
  2480. } else {
  2481. if (loaded_extensions.size() || FileAccess::exists(extension_list_config_file)) { //extensions were removed
  2482. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2483. da->remove(extension_list_config_file);
  2484. }
  2485. }
  2486. bool needs_restart = false;
  2487. for (int i = 0; i < extensions_added.size(); i++) {
  2488. GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->load_extension(extensions_added[i]);
  2489. if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) {
  2490. needs_restart = true;
  2491. }
  2492. }
  2493. for (int i = 0; i < extensions_removed.size(); i++) {
  2494. GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->unload_extension(extensions_removed[i]);
  2495. if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) {
  2496. needs_restart = true;
  2497. }
  2498. }
  2499. return needs_restart;
  2500. }
  2501. void EditorFileSystem::_bind_methods() {
  2502. ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
  2503. ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
  2504. ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
  2505. ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
  2506. ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
  2507. ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
  2508. ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
  2509. ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
  2510. ClassDB::bind_method(D_METHOD("reimport_files", "files"), &EditorFileSystem::reimport_files);
  2511. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  2512. ADD_SIGNAL(MethodInfo("script_classes_updated"));
  2513. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  2514. ADD_SIGNAL(MethodInfo("resources_reimporting", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2515. ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2516. ADD_SIGNAL(MethodInfo("resources_reload", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2517. }
  2518. void EditorFileSystem::_update_extensions() {
  2519. valid_extensions.clear();
  2520. import_extensions.clear();
  2521. textfile_extensions.clear();
  2522. List<String> extensionsl;
  2523. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  2524. for (const String &E : extensionsl) {
  2525. valid_extensions.insert(E);
  2526. }
  2527. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  2528. for (const String &E : textfile_ext) {
  2529. if (valid_extensions.has(E)) {
  2530. continue;
  2531. }
  2532. valid_extensions.insert(E);
  2533. textfile_extensions.insert(E);
  2534. }
  2535. extensionsl.clear();
  2536. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  2537. for (const String &E : extensionsl) {
  2538. import_extensions.insert(E);
  2539. }
  2540. }
  2541. void EditorFileSystem::add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2542. ERR_FAIL_COND(import_support_queries.has(p_query));
  2543. import_support_queries.push_back(p_query);
  2544. }
  2545. void EditorFileSystem::remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2546. import_support_queries.erase(p_query);
  2547. }
  2548. EditorFileSystem::EditorFileSystem() {
  2549. #ifdef THREADS_ENABLED
  2550. use_threads = true;
  2551. #endif
  2552. ResourceLoader::import = _resource_import;
  2553. reimport_on_missing_imported_files = GLOBAL_GET("editor/import/reimport_missing_imported_files");
  2554. singleton = this;
  2555. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  2556. filesystem->parent = nullptr;
  2557. new_filesystem = nullptr;
  2558. // This should probably also work on Unix and use the string it returns for FAT32 or exFAT
  2559. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2560. using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "exFAT");
  2561. scan_total = 0;
  2562. ResourceSaver::set_get_resource_id_for_path(_resource_saver_get_resource_id_for_path);
  2563. }
  2564. EditorFileSystem::~EditorFileSystem() {
  2565. ResourceSaver::set_get_resource_id_for_path(nullptr);
  2566. }