editor_file_system.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436
  1. /*************************************************************************/
  2. /* editor_file_system.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/native_extension_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_node.h"
  41. #include "editor/editor_paths.h"
  42. #include "editor/editor_resource_preview.h"
  43. #include "editor/editor_settings.h"
  44. EditorFileSystem *EditorFileSystem::singleton = nullptr;
  45. //the name is the version, to keep compatibility with different versions of Godot
  46. #define CACHE_FILE_NAME "filesystem_cache7"
  47. void EditorFileSystemDirectory::sort_files() {
  48. files.sort_custom<FileInfoSort>();
  49. }
  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. String EditorFileSystemDirectory::get_file_script_class_name(int p_idx) const {
  133. return files[p_idx]->script_class_name;
  134. }
  135. String EditorFileSystemDirectory::get_file_script_class_extends(int p_idx) const {
  136. return files[p_idx]->script_class_extends;
  137. }
  138. String EditorFileSystemDirectory::get_file_script_class_icon_path(int p_idx) const {
  139. return files[p_idx]->script_class_icon_path;
  140. }
  141. StringName EditorFileSystemDirectory::get_file_type(int p_idx) const {
  142. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  143. return files[p_idx]->type;
  144. }
  145. String EditorFileSystemDirectory::get_name() {
  146. return name;
  147. }
  148. EditorFileSystemDirectory *EditorFileSystemDirectory::get_parent() {
  149. return parent;
  150. }
  151. void EditorFileSystemDirectory::_bind_methods() {
  152. ClassDB::bind_method(D_METHOD("get_subdir_count"), &EditorFileSystemDirectory::get_subdir_count);
  153. ClassDB::bind_method(D_METHOD("get_subdir", "idx"), &EditorFileSystemDirectory::get_subdir);
  154. ClassDB::bind_method(D_METHOD("get_file_count"), &EditorFileSystemDirectory::get_file_count);
  155. ClassDB::bind_method(D_METHOD("get_file", "idx"), &EditorFileSystemDirectory::get_file);
  156. ClassDB::bind_method(D_METHOD("get_file_path", "idx"), &EditorFileSystemDirectory::get_file_path);
  157. ClassDB::bind_method(D_METHOD("get_file_type", "idx"), &EditorFileSystemDirectory::get_file_type);
  158. ClassDB::bind_method(D_METHOD("get_file_script_class_name", "idx"), &EditorFileSystemDirectory::get_file_script_class_name);
  159. ClassDB::bind_method(D_METHOD("get_file_script_class_extends", "idx"), &EditorFileSystemDirectory::get_file_script_class_extends);
  160. ClassDB::bind_method(D_METHOD("get_file_import_is_valid", "idx"), &EditorFileSystemDirectory::get_file_import_is_valid);
  161. ClassDB::bind_method(D_METHOD("get_name"), &EditorFileSystemDirectory::get_name);
  162. ClassDB::bind_method(D_METHOD("get_path"), &EditorFileSystemDirectory::get_path);
  163. ClassDB::bind_method(D_METHOD("get_parent"), &EditorFileSystemDirectory::get_parent);
  164. ClassDB::bind_method(D_METHOD("find_file_index", "name"), &EditorFileSystemDirectory::find_file_index);
  165. ClassDB::bind_method(D_METHOD("find_dir_index", "name"), &EditorFileSystemDirectory::find_dir_index);
  166. }
  167. EditorFileSystemDirectory::EditorFileSystemDirectory() {
  168. modified_time = 0;
  169. parent = nullptr;
  170. }
  171. EditorFileSystemDirectory::~EditorFileSystemDirectory() {
  172. for (int i = 0; i < files.size(); i++) {
  173. memdelete(files[i]);
  174. }
  175. for (int i = 0; i < subdirs.size(); i++) {
  176. memdelete(subdirs[i]);
  177. }
  178. }
  179. void EditorFileSystem::_scan_filesystem() {
  180. ERR_FAIL_COND(!scanning || new_filesystem);
  181. //read .fscache
  182. String cpath;
  183. sources_changed.clear();
  184. file_cache.clear();
  185. String project = ProjectSettings::get_singleton()->get_resource_path();
  186. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
  187. {
  188. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::READ);
  189. bool first = true;
  190. if (f.is_valid()) {
  191. //read the disk cache
  192. while (!f->eof_reached()) {
  193. String l = f->get_line().strip_edges();
  194. if (first) {
  195. if (first_scan) {
  196. // only use this on first scan, afterwards it gets ignored
  197. // this is so on first reimport we synchronize versions, then
  198. // we don't care until editor restart. This is for usability mainly so
  199. // your workflow is not killed after changing a setting by forceful reimporting
  200. // everything there is.
  201. filesystem_settings_version_for_import = l.strip_edges();
  202. if (filesystem_settings_version_for_import != ResourceFormatImporter::get_singleton()->get_import_settings_hash()) {
  203. revalidate_import_files = true;
  204. }
  205. }
  206. first = false;
  207. continue;
  208. }
  209. if (l.is_empty()) {
  210. continue;
  211. }
  212. if (l.begins_with("::")) {
  213. Vector<String> split = l.split("::");
  214. ERR_CONTINUE(split.size() != 3);
  215. String name = split[1];
  216. cpath = name;
  217. } else {
  218. Vector<String> split = l.split("::");
  219. ERR_CONTINUE(split.size() != 9);
  220. String name = split[0];
  221. String file;
  222. file = name;
  223. name = cpath.path_join(name);
  224. FileCache fc;
  225. fc.type = split[1];
  226. fc.uid = split[2].to_int();
  227. fc.modification_time = split[3].to_int();
  228. fc.import_modification_time = split[4].to_int();
  229. fc.import_valid = split[5].to_int() != 0;
  230. fc.import_group_file = split[6].strip_edges();
  231. fc.script_class_name = split[7].get_slice("<>", 0);
  232. fc.script_class_extends = split[7].get_slice("<>", 1);
  233. fc.script_class_icon_path = split[7].get_slice("<>", 2);
  234. String deps = split[8].strip_edges();
  235. if (deps.length()) {
  236. Vector<String> dp = deps.split("<>");
  237. for (int i = 0; i < dp.size(); i++) {
  238. String path = dp[i];
  239. fc.deps.push_back(path);
  240. }
  241. }
  242. file_cache[name] = fc;
  243. }
  244. }
  245. }
  246. }
  247. String update_cache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  248. if (FileAccess::exists(update_cache)) {
  249. {
  250. Ref<FileAccess> f2 = FileAccess::open(update_cache, FileAccess::READ);
  251. String l = f2->get_line().strip_edges();
  252. while (!l.is_empty()) {
  253. file_cache.erase(l); //erase cache for this, so it gets updated
  254. l = f2->get_line().strip_edges();
  255. }
  256. }
  257. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  258. d->remove(update_cache); //bye bye update cache
  259. }
  260. EditorProgressBG scan_progress("efs", "ScanFS", 1000);
  261. ScanProgress sp;
  262. sp.low = 0;
  263. sp.hi = 1;
  264. sp.progress = &scan_progress;
  265. new_filesystem = memnew(EditorFileSystemDirectory);
  266. new_filesystem->parent = nullptr;
  267. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  268. d->change_dir("res://");
  269. _scan_new_dir(new_filesystem, d, sp);
  270. file_cache.clear(); //clear caches, no longer needed
  271. if (!first_scan) {
  272. //on the first scan this is done from the main thread after re-importing
  273. _save_filesystem_cache();
  274. }
  275. scanning = false;
  276. }
  277. void EditorFileSystem::_save_filesystem_cache() {
  278. group_file_cache.clear();
  279. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
  280. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  281. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  282. f->store_line(filesystem_settings_version_for_import);
  283. _save_filesystem_cache(filesystem, f);
  284. }
  285. void EditorFileSystem::_thread_func(void *_userdata) {
  286. EditorFileSystem *sd = (EditorFileSystem *)_userdata;
  287. sd->_scan_filesystem();
  288. }
  289. bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_imported_files) {
  290. if (!reimport_on_missing_imported_files && p_only_imported_files) {
  291. return false;
  292. }
  293. if (!FileAccess::exists(p_path + ".import")) {
  294. return true;
  295. }
  296. if (!ResourceFormatImporter::get_singleton()->are_import_settings_valid(p_path)) {
  297. //reimport settings are not valid, reimport
  298. return true;
  299. }
  300. Error err;
  301. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  302. if (f.is_null()) { //no import file, do reimport
  303. return true;
  304. }
  305. VariantParser::StreamFile stream;
  306. stream.f = f;
  307. String assign;
  308. Variant value;
  309. VariantParser::Tag next_tag;
  310. int lines = 0;
  311. String error_text;
  312. List<String> to_check;
  313. String importer_name;
  314. String source_file = "";
  315. String source_md5 = "";
  316. Vector<String> dest_files;
  317. String dest_md5 = "";
  318. int version = 0;
  319. bool found_uid = false;
  320. while (true) {
  321. assign = Variant();
  322. next_tag.fields.clear();
  323. next_tag.name = String();
  324. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  325. if (err == ERR_FILE_EOF) {
  326. break;
  327. } else if (err != OK) {
  328. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
  329. return false; //parse error, try reimport manually (Avoid reimport loop on broken file)
  330. }
  331. if (!assign.is_empty()) {
  332. if (assign.begins_with("path")) {
  333. to_check.push_back(value);
  334. } else if (assign == "files") {
  335. Array fa = value;
  336. for (int i = 0; i < fa.size(); i++) {
  337. to_check.push_back(fa[i]);
  338. }
  339. } else if (assign == "importer_version") {
  340. version = value;
  341. } else if (assign == "importer") {
  342. importer_name = value;
  343. } else if (assign == "uid") {
  344. found_uid = true;
  345. } else if (!p_only_imported_files) {
  346. if (assign == "source_file") {
  347. source_file = value;
  348. } else if (assign == "dest_files") {
  349. dest_files = value;
  350. }
  351. }
  352. } else if (next_tag.name != "remap" && next_tag.name != "deps") {
  353. break;
  354. }
  355. }
  356. if (importer_name == "keep") {
  357. return false; //keep mode, do not reimport
  358. }
  359. if (!found_uid) {
  360. return true; //UUID not found, old format, reimport.
  361. }
  362. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  363. if (importer.is_null()) {
  364. return true; // the importer has possibly changed, try to reimport.
  365. }
  366. if (importer->get_format_version() > version) {
  367. return true; // version changed, reimport
  368. }
  369. // Read the md5's from a separate file (so the import parameters aren't dependent on the file version
  370. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path);
  371. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err);
  372. if (md5s.is_null()) { // No md5's stored for this resource
  373. return true;
  374. }
  375. VariantParser::StreamFile md5_stream;
  376. md5_stream.f = md5s;
  377. while (true) {
  378. assign = Variant();
  379. next_tag.fields.clear();
  380. next_tag.name = String();
  381. err = VariantParser::parse_tag_assign_eof(&md5_stream, lines, error_text, next_tag, assign, value, nullptr, true);
  382. if (err == ERR_FILE_EOF) {
  383. break;
  384. } else if (err != OK) {
  385. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import.md5:" + itos(lines) + "' error '" + error_text + "'.");
  386. return false; // parse error
  387. }
  388. if (!assign.is_empty()) {
  389. if (!p_only_imported_files) {
  390. if (assign == "source_md5") {
  391. source_md5 = value;
  392. } else if (assign == "dest_md5") {
  393. dest_md5 = value;
  394. }
  395. }
  396. }
  397. }
  398. //imported files are gone, reimport
  399. for (const String &E : to_check) {
  400. if (!FileAccess::exists(E)) {
  401. return true;
  402. }
  403. }
  404. //check source md5 matching
  405. if (!p_only_imported_files) {
  406. if (!source_file.is_empty() && source_file != p_path) {
  407. return true; //file was moved, reimport
  408. }
  409. if (source_md5.is_empty()) {
  410. return true; //lacks md5, so just reimport
  411. }
  412. String md5 = FileAccess::get_md5(p_path);
  413. if (md5 != source_md5) {
  414. return true;
  415. }
  416. if (dest_files.size() && !dest_md5.is_empty()) {
  417. md5 = FileAccess::get_multiple_md5(dest_files);
  418. if (md5 != dest_md5) {
  419. return true;
  420. }
  421. }
  422. }
  423. return false; //nothing changed
  424. }
  425. bool EditorFileSystem::_scan_import_support(Vector<String> reimports) {
  426. if (import_support_queries.size() == 0) {
  427. return false;
  428. }
  429. HashMap<String, int> import_support_test;
  430. Vector<bool> import_support_tested;
  431. import_support_tested.resize(import_support_queries.size());
  432. for (int i = 0; i < import_support_queries.size(); i++) {
  433. import_support_tested.write[i] = false;
  434. if (import_support_queries[i]->is_active()) {
  435. Vector<String> extensions = import_support_queries[i]->get_file_extensions();
  436. for (int j = 0; j < extensions.size(); j++) {
  437. import_support_test.insert(extensions[j], i);
  438. }
  439. }
  440. }
  441. if (import_support_test.size() == 0) {
  442. return false; //well nothing to do
  443. }
  444. for (int i = 0; i < reimports.size(); i++) {
  445. HashMap<String, int>::Iterator E = import_support_test.find(reimports[i].get_extension());
  446. if (E) {
  447. import_support_tested.write[E->value] = true;
  448. }
  449. }
  450. for (int i = 0; i < import_support_tested.size(); i++) {
  451. if (import_support_tested[i]) {
  452. if (import_support_queries.write[i]->query()) {
  453. return true;
  454. }
  455. }
  456. }
  457. return false;
  458. }
  459. bool EditorFileSystem::_update_scan_actions() {
  460. sources_changed.clear();
  461. bool fs_changed = false;
  462. Vector<String> reimports;
  463. Vector<String> reloads;
  464. for (const ItemAction &ia : scan_actions) {
  465. switch (ia.action) {
  466. case ItemAction::ACTION_NONE: {
  467. } break;
  468. case ItemAction::ACTION_DIR_ADD: {
  469. int idx = 0;
  470. for (int i = 0; i < ia.dir->subdirs.size(); i++) {
  471. if (ia.new_dir->name.naturalnocasecmp_to(ia.dir->subdirs[i]->name) < 0) {
  472. break;
  473. }
  474. idx++;
  475. }
  476. if (idx == ia.dir->subdirs.size()) {
  477. ia.dir->subdirs.push_back(ia.new_dir);
  478. } else {
  479. ia.dir->subdirs.insert(idx, ia.new_dir);
  480. }
  481. fs_changed = true;
  482. } break;
  483. case ItemAction::ACTION_DIR_REMOVE: {
  484. ERR_CONTINUE(!ia.dir->parent);
  485. ia.dir->parent->subdirs.erase(ia.dir);
  486. memdelete(ia.dir);
  487. fs_changed = true;
  488. } break;
  489. case ItemAction::ACTION_FILE_ADD: {
  490. int idx = 0;
  491. for (int i = 0; i < ia.dir->files.size(); i++) {
  492. if (ia.new_file->file.naturalnocasecmp_to(ia.dir->files[i]->file) < 0) {
  493. break;
  494. }
  495. idx++;
  496. }
  497. if (idx == ia.dir->files.size()) {
  498. ia.dir->files.push_back(ia.new_file);
  499. } else {
  500. ia.dir->files.insert(idx, ia.new_file);
  501. }
  502. fs_changed = true;
  503. } break;
  504. case ItemAction::ACTION_FILE_REMOVE: {
  505. int idx = ia.dir->find_file_index(ia.file);
  506. ERR_CONTINUE(idx == -1);
  507. _delete_internal_files(ia.dir->files[idx]->file);
  508. memdelete(ia.dir->files[idx]);
  509. ia.dir->files.remove_at(idx);
  510. fs_changed = true;
  511. } break;
  512. case ItemAction::ACTION_FILE_TEST_REIMPORT: {
  513. int idx = ia.dir->find_file_index(ia.file);
  514. ERR_CONTINUE(idx == -1);
  515. String full_path = ia.dir->get_file_path(idx);
  516. if (_test_for_reimport(full_path, false)) {
  517. //must reimport
  518. reimports.push_back(full_path);
  519. reimports.append_array(_get_dependencies(full_path));
  520. } else {
  521. //must not reimport, all was good
  522. //update modified times, to avoid reimport
  523. ia.dir->files[idx]->modified_time = FileAccess::get_modified_time(full_path);
  524. ia.dir->files[idx]->import_modified_time = FileAccess::get_modified_time(full_path + ".import");
  525. }
  526. fs_changed = true;
  527. } break;
  528. case ItemAction::ACTION_FILE_RELOAD: {
  529. int idx = ia.dir->find_file_index(ia.file);
  530. ERR_CONTINUE(idx == -1);
  531. String full_path = ia.dir->get_file_path(idx);
  532. reloads.push_back(full_path);
  533. } break;
  534. }
  535. }
  536. if (_scan_extensions()) {
  537. //needs editor restart
  538. //extensions also may provide filetypes to be imported, so they must run before importing
  539. 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"))) {
  540. if (!first_scan) {
  541. EditorNode::get_singleton()->save_all_scenes();
  542. }
  543. EditorNode::get_singleton()->restart_editor();
  544. //do not import
  545. return true;
  546. }
  547. }
  548. if (reimports.size()) {
  549. if (_scan_import_support(reimports)) {
  550. return true;
  551. }
  552. reimport_files(reimports);
  553. } else {
  554. //reimport files will update the uid cache file so if nothing was reimported, update it manually
  555. ResourceUID::get_singleton()->update_cache();
  556. }
  557. if (first_scan) {
  558. //only on first scan this is valid and updated, then settings changed.
  559. revalidate_import_files = false;
  560. filesystem_settings_version_for_import = ResourceFormatImporter::get_singleton()->get_import_settings_hash();
  561. _save_filesystem_cache();
  562. }
  563. if (reloads.size()) {
  564. emit_signal(SNAME("resources_reload"), reloads);
  565. }
  566. scan_actions.clear();
  567. return fs_changed;
  568. }
  569. void EditorFileSystem::scan() {
  570. if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/) {
  571. return;
  572. }
  573. if (scanning || scanning_changes || thread.is_started()) {
  574. return;
  575. }
  576. _update_extensions();
  577. abort_scan = false;
  578. if (!use_threads) {
  579. scanning = true;
  580. scan_total = 0;
  581. _scan_filesystem();
  582. if (filesystem) {
  583. memdelete(filesystem);
  584. }
  585. //file_type_cache.clear();
  586. filesystem = new_filesystem;
  587. new_filesystem = nullptr;
  588. _update_scan_actions();
  589. scanning = false;
  590. emit_signal(SNAME("filesystem_changed"));
  591. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  592. _queue_update_script_classes();
  593. first_scan = false;
  594. } else {
  595. ERR_FAIL_COND(thread.is_started());
  596. set_process(true);
  597. Thread::Settings s;
  598. scanning = true;
  599. scan_total = 0;
  600. s.priority = Thread::PRIORITY_LOW;
  601. thread.start(_thread_func, this, s);
  602. //tree->hide();
  603. //progress->show();
  604. }
  605. }
  606. void EditorFileSystem::ScanProgress::update(int p_current, int p_total) const {
  607. float ratio = low + ((hi - low) / p_total) * p_current;
  608. progress->step(ratio * 1000);
  609. EditorFileSystem::singleton->scan_total = ratio;
  610. }
  611. EditorFileSystem::ScanProgress EditorFileSystem::ScanProgress::get_sub(int p_current, int p_total) const {
  612. ScanProgress sp = *this;
  613. float slice = (sp.hi - sp.low) / p_total;
  614. sp.low += slice * p_current;
  615. sp.hi = slice;
  616. return sp;
  617. }
  618. void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, Ref<DirAccess> &da, const ScanProgress &p_progress) {
  619. List<String> dirs;
  620. List<String> files;
  621. String cd = da->get_current_dir();
  622. p_dir->modified_time = FileAccess::get_modified_time(cd);
  623. da->list_dir_begin();
  624. while (true) {
  625. String f = da->get_next();
  626. if (f.is_empty()) {
  627. break;
  628. }
  629. if (da->current_is_hidden()) {
  630. continue;
  631. }
  632. if (da->current_is_dir()) {
  633. if (f.begins_with(".")) { // Ignore special and . / ..
  634. continue;
  635. }
  636. if (_should_skip_directory(cd.path_join(f))) {
  637. continue;
  638. }
  639. dirs.push_back(f);
  640. } else {
  641. files.push_back(f);
  642. }
  643. }
  644. da->list_dir_end();
  645. dirs.sort_custom<NaturalNoCaseComparator>();
  646. files.sort_custom<NaturalNoCaseComparator>();
  647. int total = dirs.size() + files.size();
  648. int idx = 0;
  649. for (List<String>::Element *E = dirs.front(); E; E = E->next(), idx++) {
  650. if (da->change_dir(E->get()) == OK) {
  651. String d = da->get_current_dir();
  652. if (d == cd || !d.begins_with(cd)) {
  653. da->change_dir(cd); //avoid recursion
  654. } else {
  655. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  656. efd->parent = p_dir;
  657. efd->name = E->get();
  658. _scan_new_dir(efd, da, p_progress.get_sub(idx, total));
  659. int idx2 = 0;
  660. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  661. if (efd->name.naturalnocasecmp_to(p_dir->subdirs[i]->name) < 0) {
  662. break;
  663. }
  664. idx2++;
  665. }
  666. if (idx2 == p_dir->subdirs.size()) {
  667. p_dir->subdirs.push_back(efd);
  668. } else {
  669. p_dir->subdirs.insert(idx2, efd);
  670. }
  671. da->change_dir("..");
  672. }
  673. } else {
  674. ERR_PRINT("Cannot go into subdir '" + E->get() + "'.");
  675. }
  676. p_progress.update(idx, total);
  677. }
  678. for (List<String>::Element *E = files.front(); E; E = E->next(), idx++) {
  679. String ext = E->get().get_extension().to_lower();
  680. if (!valid_extensions.has(ext)) {
  681. continue; //invalid
  682. }
  683. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  684. fi->file = E->get();
  685. String path = cd.path_join(fi->file);
  686. FileCache *fc = file_cache.getptr(path);
  687. uint64_t mt = FileAccess::get_modified_time(path);
  688. if (import_extensions.has(ext)) {
  689. //is imported
  690. uint64_t import_mt = 0;
  691. if (FileAccess::exists(path + ".import")) {
  692. import_mt = FileAccess::get_modified_time(path + ".import");
  693. }
  694. if (fc && fc->modification_time == mt && fc->import_modification_time == import_mt && !_test_for_reimport(path, true)) {
  695. fi->type = fc->type;
  696. fi->uid = fc->uid;
  697. fi->deps = fc->deps;
  698. fi->modified_time = fc->modification_time;
  699. fi->import_modified_time = fc->import_modification_time;
  700. fi->import_valid = fc->import_valid;
  701. fi->script_class_name = fc->script_class_name;
  702. fi->import_group_file = fc->import_group_file;
  703. fi->script_class_extends = fc->script_class_extends;
  704. fi->script_class_icon_path = fc->script_class_icon_path;
  705. if (revalidate_import_files && !ResourceFormatImporter::get_singleton()->are_import_settings_valid(path)) {
  706. ItemAction ia;
  707. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  708. ia.dir = p_dir;
  709. ia.file = E->get();
  710. scan_actions.push_back(ia);
  711. }
  712. if (fc->type.is_empty()) {
  713. fi->type = ResourceLoader::get_resource_type(path);
  714. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  715. //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?)
  716. //note: I think this should not happen any longer..
  717. }
  718. if (fc->uid == ResourceUID::INVALID_ID) {
  719. // imported files should always have a UUID, so attempt to fetch it.
  720. fi->uid = ResourceLoader::get_resource_uid(path);
  721. }
  722. } else {
  723. fi->type = ResourceFormatImporter::get_singleton()->get_resource_type(path);
  724. fi->uid = ResourceFormatImporter::get_singleton()->get_resource_uid(path);
  725. fi->import_group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(path);
  726. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  727. fi->modified_time = 0;
  728. fi->import_modified_time = 0;
  729. fi->import_valid = fi->type == "TextFile" ? true : ResourceLoader::is_import_valid(path);
  730. ItemAction ia;
  731. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  732. ia.dir = p_dir;
  733. ia.file = E->get();
  734. scan_actions.push_back(ia);
  735. }
  736. } else {
  737. if (fc && fc->modification_time == mt) {
  738. //not imported, so just update type if changed
  739. fi->type = fc->type;
  740. fi->uid = fc->uid;
  741. fi->modified_time = fc->modification_time;
  742. fi->deps = fc->deps;
  743. fi->import_modified_time = 0;
  744. fi->import_valid = true;
  745. fi->script_class_name = fc->script_class_name;
  746. fi->script_class_extends = fc->script_class_extends;
  747. fi->script_class_icon_path = fc->script_class_icon_path;
  748. } else {
  749. //new or modified time
  750. fi->type = ResourceLoader::get_resource_type(path);
  751. if (fi->type == "" && textfile_extensions.has(ext)) {
  752. fi->type = "TextFile";
  753. }
  754. fi->uid = ResourceLoader::get_resource_uid(path);
  755. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  756. fi->deps = _get_dependencies(path);
  757. fi->modified_time = mt;
  758. fi->import_modified_time = 0;
  759. fi->import_valid = true;
  760. }
  761. }
  762. if (fi->uid != ResourceUID::INVALID_ID) {
  763. if (ResourceUID::get_singleton()->has_id(fi->uid)) {
  764. ResourceUID::get_singleton()->set_id(fi->uid, path);
  765. } else {
  766. ResourceUID::get_singleton()->add_id(fi->uid, path);
  767. }
  768. }
  769. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  770. ScriptLanguage *lang = ScriptServer::get_language(i);
  771. if (lang->supports_documentation() && fi->type == lang->get_type()) {
  772. Ref<Script> script = ResourceLoader::load(path);
  773. if (script == nullptr) {
  774. continue;
  775. }
  776. Vector<DocData::ClassDoc> docs = script->get_documentation();
  777. for (int j = 0; j < docs.size(); j++) {
  778. EditorHelp::get_doc_data()->add_doc(docs[j]);
  779. }
  780. }
  781. }
  782. p_dir->files.push_back(fi);
  783. p_progress.update(idx, total);
  784. }
  785. }
  786. void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress) {
  787. uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
  788. bool updated_dir = false;
  789. String cd = p_dir->get_path();
  790. if (current_mtime != p_dir->modified_time || using_fat32_or_exfat) {
  791. updated_dir = true;
  792. p_dir->modified_time = current_mtime;
  793. //ooooops, dir changed, see what's going on
  794. //first mark everything as veryfied
  795. for (int i = 0; i < p_dir->files.size(); i++) {
  796. p_dir->files[i]->verified = false;
  797. }
  798. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  799. p_dir->get_subdir(i)->verified = false;
  800. }
  801. //then scan files and directories and check what's different
  802. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  803. Error ret = da->change_dir(cd);
  804. ERR_FAIL_COND_MSG(ret != OK, "Cannot change to '" + cd + "' folder.");
  805. da->list_dir_begin();
  806. while (true) {
  807. String f = da->get_next();
  808. if (f.is_empty()) {
  809. break;
  810. }
  811. if (da->current_is_hidden()) {
  812. continue;
  813. }
  814. if (da->current_is_dir()) {
  815. if (f.begins_with(".")) { // Ignore special and . / ..
  816. continue;
  817. }
  818. int idx = p_dir->find_dir_index(f);
  819. if (idx == -1) {
  820. if (_should_skip_directory(cd.path_join(f))) {
  821. continue;
  822. }
  823. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  824. efd->parent = p_dir;
  825. efd->name = f;
  826. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  827. d->change_dir(cd.path_join(f));
  828. _scan_new_dir(efd, d, p_progress.get_sub(1, 1));
  829. ItemAction ia;
  830. ia.action = ItemAction::ACTION_DIR_ADD;
  831. ia.dir = p_dir;
  832. ia.file = f;
  833. ia.new_dir = efd;
  834. scan_actions.push_back(ia);
  835. } else {
  836. p_dir->subdirs[idx]->verified = true;
  837. }
  838. } else {
  839. String ext = f.get_extension().to_lower();
  840. if (!valid_extensions.has(ext)) {
  841. continue; //invalid
  842. }
  843. int idx = p_dir->find_file_index(f);
  844. if (idx == -1) {
  845. //never seen this file, add actition to add it
  846. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  847. fi->file = f;
  848. String path = cd.path_join(fi->file);
  849. fi->modified_time = FileAccess::get_modified_time(path);
  850. fi->import_modified_time = 0;
  851. fi->type = ResourceLoader::get_resource_type(path);
  852. if (fi->type == "" && textfile_extensions.has(ext)) {
  853. fi->type = "TextFile";
  854. }
  855. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  856. fi->import_valid = fi->type == "TextFile" ? true : ResourceLoader::is_import_valid(path);
  857. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  858. {
  859. ItemAction ia;
  860. ia.action = ItemAction::ACTION_FILE_ADD;
  861. ia.dir = p_dir;
  862. ia.file = f;
  863. ia.new_file = fi;
  864. scan_actions.push_back(ia);
  865. }
  866. if (import_extensions.has(ext)) {
  867. //if it can be imported, and it was added, it needs to be reimported
  868. ItemAction ia;
  869. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  870. ia.dir = p_dir;
  871. ia.file = f;
  872. scan_actions.push_back(ia);
  873. }
  874. } else {
  875. p_dir->files[idx]->verified = true;
  876. }
  877. }
  878. }
  879. da->list_dir_end();
  880. }
  881. for (int i = 0; i < p_dir->files.size(); i++) {
  882. if (updated_dir && !p_dir->files[i]->verified) {
  883. //this file was removed, add action to remove it
  884. ItemAction ia;
  885. ia.action = ItemAction::ACTION_FILE_REMOVE;
  886. ia.dir = p_dir;
  887. ia.file = p_dir->files[i]->file;
  888. scan_actions.push_back(ia);
  889. continue;
  890. }
  891. String path = cd.path_join(p_dir->files[i]->file);
  892. if (import_extensions.has(p_dir->files[i]->file.get_extension().to_lower())) {
  893. //check here if file must be imported or not
  894. uint64_t mt = FileAccess::get_modified_time(path);
  895. bool reimport = false;
  896. if (mt != p_dir->files[i]->modified_time) {
  897. reimport = true; //it was modified, must be reimported.
  898. } else if (!FileAccess::exists(path + ".import")) {
  899. reimport = true; //no .import file, obviously reimport
  900. } else {
  901. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  902. if (import_mt != p_dir->files[i]->import_modified_time) {
  903. reimport = true;
  904. } else if (_test_for_reimport(path, true)) {
  905. reimport = true;
  906. }
  907. }
  908. if (reimport) {
  909. ItemAction ia;
  910. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  911. ia.dir = p_dir;
  912. ia.file = p_dir->files[i]->file;
  913. scan_actions.push_back(ia);
  914. }
  915. } else if (ResourceCache::has(path)) { //test for potential reload
  916. uint64_t mt = FileAccess::get_modified_time(path);
  917. if (mt != p_dir->files[i]->modified_time) {
  918. p_dir->files[i]->modified_time = mt; //save new time, but test for reload
  919. ItemAction ia;
  920. ia.action = ItemAction::ACTION_FILE_RELOAD;
  921. ia.dir = p_dir;
  922. ia.file = p_dir->files[i]->file;
  923. scan_actions.push_back(ia);
  924. }
  925. }
  926. }
  927. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  928. if ((updated_dir && !p_dir->subdirs[i]->verified) || _should_skip_directory(p_dir->subdirs[i]->get_path())) {
  929. //this directory was removed or ignored, add action to remove it
  930. ItemAction ia;
  931. ia.action = ItemAction::ACTION_DIR_REMOVE;
  932. ia.dir = p_dir->subdirs[i];
  933. scan_actions.push_back(ia);
  934. continue;
  935. }
  936. _scan_fs_changes(p_dir->get_subdir(i), p_progress);
  937. }
  938. }
  939. void EditorFileSystem::_delete_internal_files(String p_file) {
  940. if (FileAccess::exists(p_file + ".import")) {
  941. List<String> paths;
  942. ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
  943. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  944. for (const String &E : paths) {
  945. da->remove(E);
  946. }
  947. da->remove(p_file + ".import");
  948. }
  949. }
  950. void EditorFileSystem::_thread_func_sources(void *_userdata) {
  951. EditorFileSystem *efs = (EditorFileSystem *)_userdata;
  952. if (efs->filesystem) {
  953. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  954. ScanProgress sp;
  955. sp.progress = &pr;
  956. sp.hi = 1;
  957. sp.low = 0;
  958. efs->_scan_fs_changes(efs->filesystem, sp);
  959. }
  960. efs->scanning_changes_done = true;
  961. }
  962. void EditorFileSystem::scan_changes() {
  963. if (first_scan || // Prevent a premature changes scan from inhibiting the first full scan
  964. scanning || scanning_changes || thread.is_started()) {
  965. scan_changes_pending = true;
  966. set_process(true);
  967. return;
  968. }
  969. _update_extensions();
  970. sources_changed.clear();
  971. scanning_changes = true;
  972. scanning_changes_done = false;
  973. abort_scan = false;
  974. if (!use_threads) {
  975. if (filesystem) {
  976. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  977. ScanProgress sp;
  978. sp.progress = &pr;
  979. sp.hi = 1;
  980. sp.low = 0;
  981. scan_total = 0;
  982. _scan_fs_changes(filesystem, sp);
  983. if (_update_scan_actions()) {
  984. emit_signal(SNAME("filesystem_changed"));
  985. }
  986. }
  987. scanning_changes = false;
  988. scanning_changes_done = true;
  989. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  990. } else {
  991. ERR_FAIL_COND(thread_sources.is_started());
  992. set_process(true);
  993. scan_total = 0;
  994. Thread::Settings s;
  995. s.priority = Thread::PRIORITY_LOW;
  996. thread_sources.start(_thread_func_sources, this, s);
  997. }
  998. }
  999. void EditorFileSystem::_notification(int p_what) {
  1000. switch (p_what) {
  1001. case NOTIFICATION_EXIT_TREE: {
  1002. Thread &active_thread = thread.is_started() ? thread : thread_sources;
  1003. if (use_threads && active_thread.is_started()) {
  1004. //abort thread if in progress
  1005. abort_scan = true;
  1006. while (scanning) {
  1007. OS::get_singleton()->delay_usec(1000);
  1008. }
  1009. active_thread.wait_to_finish();
  1010. WARN_PRINT("Scan thread aborted...");
  1011. set_process(false);
  1012. }
  1013. if (filesystem) {
  1014. memdelete(filesystem);
  1015. }
  1016. if (new_filesystem) {
  1017. memdelete(new_filesystem);
  1018. }
  1019. filesystem = nullptr;
  1020. new_filesystem = nullptr;
  1021. } break;
  1022. case NOTIFICATION_PROCESS: {
  1023. if (use_threads) {
  1024. if (scanning_changes) {
  1025. if (scanning_changes_done) {
  1026. scanning_changes = false;
  1027. set_process(false);
  1028. thread_sources.wait_to_finish();
  1029. if (_update_scan_actions()) {
  1030. emit_signal(SNAME("filesystem_changed"));
  1031. }
  1032. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1033. _queue_update_script_classes();
  1034. first_scan = false;
  1035. }
  1036. } else if (!scanning && thread.is_started()) {
  1037. set_process(false);
  1038. if (filesystem) {
  1039. memdelete(filesystem);
  1040. }
  1041. filesystem = new_filesystem;
  1042. new_filesystem = nullptr;
  1043. thread.wait_to_finish();
  1044. _update_scan_actions();
  1045. emit_signal(SNAME("filesystem_changed"));
  1046. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1047. _queue_update_script_classes();
  1048. first_scan = false;
  1049. }
  1050. if (!is_processing() && scan_changes_pending) {
  1051. scan_changes_pending = false;
  1052. scan_changes();
  1053. }
  1054. }
  1055. } break;
  1056. }
  1057. }
  1058. bool EditorFileSystem::is_scanning() const {
  1059. return scanning || scanning_changes;
  1060. }
  1061. float EditorFileSystem::get_scanning_progress() const {
  1062. return scan_total;
  1063. }
  1064. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  1065. return filesystem;
  1066. }
  1067. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file) {
  1068. if (!p_dir) {
  1069. return; //none
  1070. }
  1071. p_file->store_line("::" + p_dir->get_path() + "::" + String::num(p_dir->modified_time));
  1072. for (int i = 0; i < p_dir->files.size(); i++) {
  1073. if (!p_dir->files[i]->import_group_file.is_empty()) {
  1074. group_file_cache.insert(p_dir->files[i]->import_group_file);
  1075. }
  1076. String s = p_dir->files[i]->file + "::" + p_dir->files[i]->type + "::" + itos(p_dir->files[i]->uid) + "::" + itos(p_dir->files[i]->modified_time) + "::" + itos(p_dir->files[i]->import_modified_time) + "::" + itos(p_dir->files[i]->import_valid) + "::" + p_dir->files[i]->import_group_file + "::" + p_dir->files[i]->script_class_name + "<>" + p_dir->files[i]->script_class_extends + "<>" + p_dir->files[i]->script_class_icon_path;
  1077. s += "::";
  1078. for (int j = 0; j < p_dir->files[i]->deps.size(); j++) {
  1079. if (j > 0) {
  1080. s += "<>";
  1081. }
  1082. s += p_dir->files[i]->deps[j];
  1083. }
  1084. p_file->store_line(s);
  1085. }
  1086. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1087. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  1088. }
  1089. }
  1090. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  1091. //todo make faster
  1092. if (!filesystem || scanning) {
  1093. return false;
  1094. }
  1095. String f = ProjectSettings::get_singleton()->localize_path(p_file);
  1096. if (!f.begins_with("res://")) {
  1097. return false;
  1098. }
  1099. f = f.substr(6, f.length());
  1100. f = f.replace("\\", "/");
  1101. Vector<String> path = f.split("/");
  1102. if (path.size() == 0) {
  1103. return false;
  1104. }
  1105. String file = path[path.size() - 1];
  1106. path.resize(path.size() - 1);
  1107. EditorFileSystemDirectory *fs = filesystem;
  1108. for (int i = 0; i < path.size(); i++) {
  1109. if (path[i].begins_with(".")) {
  1110. return false;
  1111. }
  1112. int idx = -1;
  1113. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1114. if (fs->get_subdir(j)->get_name() == path[i]) {
  1115. idx = j;
  1116. break;
  1117. }
  1118. }
  1119. if (idx == -1) {
  1120. //does not exist, create i guess?
  1121. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  1122. efsd->name = path[i];
  1123. efsd->parent = fs;
  1124. int idx2 = 0;
  1125. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1126. if (efsd->name.naturalnocasecmp_to(fs->get_subdir(j)->get_name()) < 0) {
  1127. break;
  1128. }
  1129. idx2++;
  1130. }
  1131. if (idx2 == fs->get_subdir_count()) {
  1132. fs->subdirs.push_back(efsd);
  1133. } else {
  1134. fs->subdirs.insert(idx2, efsd);
  1135. }
  1136. fs = efsd;
  1137. } else {
  1138. fs = fs->get_subdir(idx);
  1139. }
  1140. }
  1141. int cpos = -1;
  1142. for (int i = 0; i < fs->files.size(); i++) {
  1143. if (fs->files[i]->file == file) {
  1144. cpos = i;
  1145. break;
  1146. }
  1147. }
  1148. r_file_pos = cpos;
  1149. *r_d = fs;
  1150. return cpos != -1;
  1151. }
  1152. String EditorFileSystem::get_file_type(const String &p_file) const {
  1153. EditorFileSystemDirectory *fs = nullptr;
  1154. int cpos = -1;
  1155. if (!_find_file(p_file, &fs, cpos)) {
  1156. return "";
  1157. }
  1158. return fs->files[cpos]->type;
  1159. }
  1160. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  1161. if (!filesystem || scanning) {
  1162. return nullptr;
  1163. }
  1164. EditorFileSystemDirectory *fs = nullptr;
  1165. int cpos = -1;
  1166. if (!_find_file(p_file, &fs, cpos)) {
  1167. return nullptr;
  1168. }
  1169. if (r_index) {
  1170. *r_index = cpos;
  1171. }
  1172. return fs;
  1173. }
  1174. EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
  1175. if (!filesystem || scanning) {
  1176. return nullptr;
  1177. }
  1178. String f = ProjectSettings::get_singleton()->localize_path(p_path);
  1179. if (!f.begins_with("res://")) {
  1180. return nullptr;
  1181. }
  1182. f = f.substr(6, f.length());
  1183. f = f.replace("\\", "/");
  1184. if (f.is_empty()) {
  1185. return filesystem;
  1186. }
  1187. if (f.ends_with("/")) {
  1188. f = f.substr(0, f.length() - 1);
  1189. }
  1190. Vector<String> path = f.split("/");
  1191. if (path.size() == 0) {
  1192. return nullptr;
  1193. }
  1194. EditorFileSystemDirectory *fs = filesystem;
  1195. for (int i = 0; i < path.size(); i++) {
  1196. int idx = -1;
  1197. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1198. if (fs->get_subdir(j)->get_name() == path[i]) {
  1199. idx = j;
  1200. break;
  1201. }
  1202. }
  1203. if (idx == -1) {
  1204. return nullptr;
  1205. } else {
  1206. fs = fs->get_subdir(idx);
  1207. }
  1208. }
  1209. return fs;
  1210. }
  1211. void EditorFileSystem::_save_late_updated_files() {
  1212. //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
  1213. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  1214. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  1215. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  1216. for (const String &E : late_update_files) {
  1217. f->store_line(E);
  1218. }
  1219. }
  1220. Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
  1221. List<String> deps;
  1222. ResourceLoader::get_dependencies(p_path, &deps);
  1223. Vector<String> ret;
  1224. for (const String &E : deps) {
  1225. ret.push_back(E);
  1226. }
  1227. return ret;
  1228. }
  1229. String EditorFileSystem::_get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const {
  1230. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1231. if (ScriptServer::get_language(i)->handles_global_class_type(p_type)) {
  1232. String global_name;
  1233. String extends;
  1234. String icon_path;
  1235. global_name = ScriptServer::get_language(i)->get_global_class_name(p_path, &extends, &icon_path);
  1236. *r_extends = extends;
  1237. *r_icon_path = icon_path;
  1238. return global_name;
  1239. }
  1240. }
  1241. *r_extends = String();
  1242. *r_icon_path = String();
  1243. return String();
  1244. }
  1245. void EditorFileSystem::_scan_script_classes(EditorFileSystemDirectory *p_dir) {
  1246. int filecount = p_dir->files.size();
  1247. const EditorFileSystemDirectory::FileInfo *const *files = p_dir->files.ptr();
  1248. for (int i = 0; i < filecount; i++) {
  1249. if (files[i]->script_class_name.is_empty()) {
  1250. continue;
  1251. }
  1252. String lang;
  1253. for (int j = 0; j < ScriptServer::get_language_count(); j++) {
  1254. if (ScriptServer::get_language(j)->handles_global_class_type(files[i]->type)) {
  1255. lang = ScriptServer::get_language(j)->get_name();
  1256. }
  1257. }
  1258. ScriptServer::add_global_class(files[i]->script_class_name, files[i]->script_class_extends, lang, p_dir->get_file_path(i));
  1259. EditorNode::get_editor_data().script_class_set_icon_path(files[i]->script_class_name, files[i]->script_class_icon_path);
  1260. EditorNode::get_editor_data().script_class_set_name(files[i]->file, files[i]->script_class_name);
  1261. }
  1262. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1263. _scan_script_classes(p_dir->get_subdir(i));
  1264. }
  1265. }
  1266. void EditorFileSystem::update_script_classes() {
  1267. if (!update_script_classes_queued.is_set()) {
  1268. return;
  1269. }
  1270. update_script_classes_queued.clear();
  1271. ScriptServer::global_classes_clear();
  1272. if (get_filesystem()) {
  1273. _scan_script_classes(get_filesystem());
  1274. }
  1275. ScriptServer::save_global_classes();
  1276. EditorNode::get_editor_data().script_class_save_icon_paths();
  1277. // Rescan custom loaders and savers.
  1278. // Doing the following here because the `filesystem_changed` signal fires multiple times and isn't always followed by script classes update.
  1279. // So I thought it's better to do this when script classes really get updated
  1280. ResourceLoader::remove_custom_loaders();
  1281. ResourceLoader::add_custom_loaders();
  1282. ResourceSaver::remove_custom_savers();
  1283. ResourceSaver::add_custom_savers();
  1284. }
  1285. void EditorFileSystem::_queue_update_script_classes() {
  1286. if (update_script_classes_queued.is_set()) {
  1287. return;
  1288. }
  1289. update_script_classes_queued.set();
  1290. call_deferred(SNAME("update_script_classes"));
  1291. }
  1292. void EditorFileSystem::update_file(const String &p_file) {
  1293. EditorFileSystemDirectory *fs = nullptr;
  1294. int cpos = -1;
  1295. if (!_find_file(p_file, &fs, cpos)) {
  1296. if (!fs) {
  1297. return;
  1298. }
  1299. }
  1300. if (!FileAccess::exists(p_file)) {
  1301. //was removed
  1302. _delete_internal_files(p_file);
  1303. if (cpos != -1) { // Might've never been part of the editor file system (*.* files deleted in Open dialog).
  1304. if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  1305. if (ResourceUID::get_singleton()->has_id(fs->files[cpos]->uid)) {
  1306. ResourceUID::get_singleton()->remove_id(fs->files[cpos]->uid);
  1307. }
  1308. }
  1309. memdelete(fs->files[cpos]);
  1310. fs->files.remove_at(cpos);
  1311. }
  1312. call_deferred(SNAME("emit_signal"), "filesystem_changed"); //update later
  1313. _queue_update_script_classes();
  1314. return;
  1315. }
  1316. String type = ResourceLoader::get_resource_type(p_file);
  1317. if (type.is_empty() && textfile_extensions.has(p_file.get_extension())) {
  1318. type = "TextFile";
  1319. }
  1320. ResourceUID::ID uid = ResourceLoader::get_resource_uid(p_file);
  1321. if (cpos == -1) {
  1322. // The file did not exist, it was added.
  1323. int idx = 0;
  1324. String file_name = p_file.get_file();
  1325. for (int i = 0; i < fs->files.size(); i++) {
  1326. if (p_file.naturalnocasecmp_to(fs->files[i]->file) < 0) {
  1327. break;
  1328. }
  1329. idx++;
  1330. }
  1331. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1332. fi->file = file_name;
  1333. fi->import_modified_time = 0;
  1334. fi->import_valid = type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  1335. if (idx == fs->files.size()) {
  1336. fs->files.push_back(fi);
  1337. } else {
  1338. fs->files.insert(idx, fi);
  1339. }
  1340. cpos = idx;
  1341. } else {
  1342. //the file exists and it was updated, and was not added in this step.
  1343. //this means we must force upon next restart to scan it again, to get proper type and dependencies
  1344. late_update_files.insert(p_file);
  1345. _save_late_updated_files(); //files need to be updated in the re-scan
  1346. }
  1347. fs->files[cpos]->type = type;
  1348. fs->files[cpos]->uid = uid;
  1349. fs->files[cpos]->script_class_name = _get_global_script_class(type, p_file, &fs->files[cpos]->script_class_extends, &fs->files[cpos]->script_class_icon_path);
  1350. fs->files[cpos]->import_group_file = ResourceLoader::get_import_group_file(p_file);
  1351. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1352. fs->files[cpos]->deps = _get_dependencies(p_file);
  1353. fs->files[cpos]->import_valid = type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  1354. if (uid != ResourceUID::INVALID_ID) {
  1355. if (ResourceUID::get_singleton()->has_id(uid)) {
  1356. ResourceUID::get_singleton()->set_id(uid, p_file);
  1357. } else {
  1358. ResourceUID::get_singleton()->add_id(uid, p_file);
  1359. }
  1360. ResourceUID::get_singleton()->update_cache();
  1361. }
  1362. // Update preview
  1363. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1364. call_deferred(SNAME("emit_signal"), "filesystem_changed"); //update later
  1365. _queue_update_script_classes();
  1366. }
  1367. HashSet<String> EditorFileSystem::get_valid_extensions() const {
  1368. return valid_extensions;
  1369. }
  1370. Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) {
  1371. String importer_name;
  1372. HashMap<String, HashMap<StringName, Variant>> source_file_options;
  1373. HashMap<String, String> base_paths;
  1374. for (int i = 0; i < p_files.size(); i++) {
  1375. Ref<ConfigFile> config;
  1376. config.instantiate();
  1377. Error err = config->load(p_files[i] + ".import");
  1378. ERR_CONTINUE(err != OK);
  1379. ERR_CONTINUE(!config->has_section_key("remap", "importer"));
  1380. String file_importer_name = config->get_value("remap", "importer");
  1381. ERR_CONTINUE(file_importer_name.is_empty());
  1382. if (!importer_name.is_empty() && importer_name != file_importer_name) {
  1383. EditorNode::get_singleton()->show_warning(vformat(TTR("There are multiple importers for different types pointing to file %s, import aborted"), p_group_file));
  1384. ERR_FAIL_V(ERR_FILE_CORRUPT);
  1385. }
  1386. source_file_options[p_files[i]] = HashMap<StringName, Variant>();
  1387. importer_name = file_importer_name;
  1388. if (importer_name == "keep") {
  1389. continue; //do nothing
  1390. }
  1391. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1392. ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT);
  1393. List<ResourceImporter::ImportOption> options;
  1394. importer->get_import_options(p_files[i], &options);
  1395. //set default values
  1396. for (const ResourceImporter::ImportOption &E : options) {
  1397. source_file_options[p_files[i]][E.option.name] = E.default_value;
  1398. }
  1399. if (config->has_section("params")) {
  1400. List<String> sk;
  1401. config->get_section_keys("params", &sk);
  1402. for (const String &param : sk) {
  1403. Variant value = config->get_value("params", param);
  1404. //override with whatever is in file
  1405. source_file_options[p_files[i]][param] = value;
  1406. }
  1407. }
  1408. base_paths[p_files[i]] = ResourceFormatImporter::get_singleton()->get_import_base_path(p_files[i]);
  1409. }
  1410. if (importer_name == "keep") {
  1411. return OK; // (do nothing)
  1412. }
  1413. ERR_FAIL_COND_V(importer_name.is_empty(), ERR_UNCONFIGURED);
  1414. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1415. Error err = importer->import_group_file(p_group_file, source_file_options, base_paths);
  1416. //all went well, overwrite config files with proper remaps and md5s
  1417. for (const KeyValue<String, HashMap<StringName, Variant>> &E : source_file_options) {
  1418. const String &file = E.key;
  1419. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(file);
  1420. Vector<String> dest_paths;
  1421. {
  1422. Ref<FileAccess> f = FileAccess::open(file + ".import", FileAccess::WRITE);
  1423. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'.");
  1424. //write manually, as order matters ([remap] has to go first for performance).
  1425. f->store_line("[remap]");
  1426. f->store_line("");
  1427. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  1428. int version = importer->get_format_version();
  1429. if (version > 0) {
  1430. f->store_line("importer_version=" + itos(version));
  1431. }
  1432. if (!importer->get_resource_type().is_empty()) {
  1433. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  1434. }
  1435. if (err == OK) {
  1436. String path = base_path + "." + importer->get_save_extension();
  1437. f->store_line("path=\"" + path + "\"");
  1438. dest_paths.push_back(path);
  1439. }
  1440. f->store_line("group_file=" + Variant(p_group_file).get_construct_string());
  1441. if (err == OK) {
  1442. f->store_line("valid=true");
  1443. } else {
  1444. f->store_line("valid=false");
  1445. }
  1446. f->store_line("[deps]\n");
  1447. f->store_line("");
  1448. f->store_line("source_file=" + Variant(file).get_construct_string());
  1449. if (dest_paths.size()) {
  1450. Array dp;
  1451. for (int i = 0; i < dest_paths.size(); i++) {
  1452. dp.push_back(dest_paths[i]);
  1453. }
  1454. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  1455. }
  1456. f->store_line("[params]");
  1457. f->store_line("");
  1458. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  1459. List<ResourceImporter::ImportOption> options;
  1460. importer->get_import_options(file, &options);
  1461. //set default values
  1462. for (const ResourceImporter::ImportOption &F : options) {
  1463. String base = F.option.name;
  1464. Variant v = F.default_value;
  1465. if (source_file_options[file].has(base)) {
  1466. v = source_file_options[file][base];
  1467. }
  1468. String value;
  1469. VariantWriter::write_to_string(v, value);
  1470. f->store_line(base + "=" + value);
  1471. }
  1472. }
  1473. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  1474. {
  1475. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  1476. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  1477. md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\"");
  1478. if (dest_paths.size()) {
  1479. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  1480. }
  1481. }
  1482. EditorFileSystemDirectory *fs = nullptr;
  1483. int cpos = -1;
  1484. bool found = _find_file(file, &fs, cpos);
  1485. ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, "Can't find file '" + file + "'.");
  1486. //update modified times, to avoid reimport
  1487. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  1488. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(file + ".import");
  1489. fs->files[cpos]->deps = _get_dependencies(file);
  1490. fs->files[cpos]->type = importer->get_resource_type();
  1491. if (fs->files[cpos]->type == "" && textfile_extensions.has(file.get_extension())) {
  1492. fs->files[cpos]->type = "TextFile";
  1493. }
  1494. fs->files[cpos]->import_valid = err == OK;
  1495. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  1496. //to reload properly
  1497. Ref<Resource> r = ResourceCache::get_ref(file);
  1498. if (r.is_valid()) {
  1499. if (!r->get_import_path().is_empty()) {
  1500. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(file);
  1501. r->set_import_path(dst_path);
  1502. r->set_import_last_modified_time(0);
  1503. }
  1504. }
  1505. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  1506. }
  1507. return err;
  1508. }
  1509. void EditorFileSystem::_reimport_file(const String &p_file, const HashMap<StringName, Variant> *p_custom_options, const String &p_custom_importer) {
  1510. EditorFileSystemDirectory *fs = nullptr;
  1511. int cpos = -1;
  1512. bool found = _find_file(p_file, &fs, cpos);
  1513. ERR_FAIL_COND_MSG(!found, "Can't find file '" + p_file + "'.");
  1514. //try to obtain existing params
  1515. HashMap<StringName, Variant> params;
  1516. String importer_name; //empty by default though
  1517. if (!p_custom_importer.is_empty()) {
  1518. importer_name = p_custom_importer;
  1519. }
  1520. if (p_custom_options != nullptr) {
  1521. params = *p_custom_options;
  1522. }
  1523. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  1524. if (FileAccess::exists(p_file + ".import")) {
  1525. //use existing
  1526. if (p_custom_options == nullptr) {
  1527. Ref<ConfigFile> cf;
  1528. cf.instantiate();
  1529. Error err = cf->load(p_file + ".import");
  1530. if (err == OK) {
  1531. if (cf->has_section("params")) {
  1532. List<String> sk;
  1533. cf->get_section_keys("params", &sk);
  1534. for (const String &E : sk) {
  1535. params[E] = cf->get_value("params", E);
  1536. }
  1537. }
  1538. if (cf->has_section("remap")) {
  1539. if (p_custom_importer.is_empty()) {
  1540. importer_name = cf->get_value("remap", "importer");
  1541. }
  1542. if (cf->has_section_key("remap", "uid")) {
  1543. String uidt = cf->get_value("remap", "uid");
  1544. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  1545. }
  1546. }
  1547. }
  1548. }
  1549. }
  1550. if (importer_name == "keep") {
  1551. //keep files, do nothing.
  1552. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1553. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  1554. fs->files[cpos]->deps.clear();
  1555. fs->files[cpos]->type = "";
  1556. fs->files[cpos]->import_valid = false;
  1557. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1558. return;
  1559. }
  1560. Ref<ResourceImporter> importer;
  1561. bool load_default = false;
  1562. //find the importer
  1563. if (!importer_name.is_empty()) {
  1564. importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1565. }
  1566. if (importer.is_null()) {
  1567. //not found by name, find by extension
  1568. importer = ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_file.get_extension());
  1569. load_default = true;
  1570. if (importer.is_null()) {
  1571. ERR_PRINT("BUG: File queued for import, but can't be imported, importer for type '" + importer_name + "' not found.");
  1572. ERR_FAIL();
  1573. }
  1574. }
  1575. //mix with default params, in case a parameter is missing
  1576. List<ResourceImporter::ImportOption> opts;
  1577. importer->get_import_options(p_file, &opts);
  1578. for (const ResourceImporter::ImportOption &E : opts) {
  1579. if (!params.has(E.option.name)) { //this one is not present
  1580. params[E.option.name] = E.default_value;
  1581. }
  1582. }
  1583. if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  1584. //use defaults if exist
  1585. Dictionary d = ProjectSettings::get_singleton()->get("importer_defaults/" + importer->get_importer_name());
  1586. List<Variant> v;
  1587. d.get_key_list(&v);
  1588. for (const Variant &E : v) {
  1589. params[E] = d[E];
  1590. }
  1591. }
  1592. //finally, perform import!!
  1593. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
  1594. List<String> import_variants;
  1595. List<String> gen_files;
  1596. Variant metadata;
  1597. Error err = importer->import(p_file, base_path, params, &import_variants, &gen_files, &metadata);
  1598. if (err != OK) {
  1599. ERR_PRINT("Error importing '" + p_file + "'.");
  1600. }
  1601. //as import is complete, save the .import file
  1602. Vector<String> dest_paths;
  1603. {
  1604. Ref<FileAccess> f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
  1605. ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file from path '" + p_file + ".import'.");
  1606. //write manually, as order matters ([remap] has to go first for performance).
  1607. f->store_line("[remap]");
  1608. f->store_line("");
  1609. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  1610. int version = importer->get_format_version();
  1611. if (version > 0) {
  1612. f->store_line("importer_version=" + itos(version));
  1613. }
  1614. if (!importer->get_resource_type().is_empty()) {
  1615. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  1616. }
  1617. if (uid == ResourceUID::INVALID_ID) {
  1618. uid = ResourceUID::get_singleton()->create_id();
  1619. }
  1620. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); //store in readable format
  1621. if (err == OK) {
  1622. if (importer->get_save_extension().is_empty()) {
  1623. //no path
  1624. } else if (import_variants.size()) {
  1625. //import with variants
  1626. for (const String &E : import_variants) {
  1627. String path = base_path.c_escape() + "." + E + "." + importer->get_save_extension();
  1628. f->store_line("path." + E + "=\"" + path + "\"");
  1629. dest_paths.push_back(path);
  1630. }
  1631. } else {
  1632. String path = base_path + "." + importer->get_save_extension();
  1633. f->store_line("path=\"" + path + "\"");
  1634. dest_paths.push_back(path);
  1635. }
  1636. } else {
  1637. f->store_line("valid=false");
  1638. }
  1639. if (metadata != Variant()) {
  1640. f->store_line("metadata=" + metadata.get_construct_string());
  1641. }
  1642. f->store_line("");
  1643. f->store_line("[deps]\n");
  1644. if (gen_files.size()) {
  1645. Array genf;
  1646. for (const String &E : gen_files) {
  1647. genf.push_back(E);
  1648. dest_paths.push_back(E);
  1649. }
  1650. String value;
  1651. VariantWriter::write_to_string(genf, value);
  1652. f->store_line("files=" + value);
  1653. f->store_line("");
  1654. }
  1655. f->store_line("source_file=" + Variant(p_file).get_construct_string());
  1656. if (dest_paths.size()) {
  1657. Array dp;
  1658. for (int i = 0; i < dest_paths.size(); i++) {
  1659. dp.push_back(dest_paths[i]);
  1660. }
  1661. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  1662. }
  1663. f->store_line("[params]");
  1664. f->store_line("");
  1665. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  1666. for (const ResourceImporter::ImportOption &E : opts) {
  1667. String base = E.option.name;
  1668. String value;
  1669. VariantWriter::write_to_string(params[base], value);
  1670. f->store_line(base + "=" + value);
  1671. }
  1672. }
  1673. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  1674. {
  1675. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  1676. ERR_FAIL_COND_MSG(md5s.is_null(), "Cannot open MD5 file '" + base_path + ".md5'.");
  1677. md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"");
  1678. if (dest_paths.size()) {
  1679. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  1680. }
  1681. }
  1682. //update modified times, to avoid reimport
  1683. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1684. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  1685. fs->files[cpos]->deps = _get_dependencies(p_file);
  1686. fs->files[cpos]->type = importer->get_resource_type();
  1687. fs->files[cpos]->uid = uid;
  1688. fs->files[cpos]->import_valid = fs->files[cpos]->type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  1689. if (ResourceUID::get_singleton()->has_id(uid)) {
  1690. ResourceUID::get_singleton()->set_id(uid, p_file);
  1691. } else {
  1692. ResourceUID::get_singleton()->add_id(uid, p_file);
  1693. }
  1694. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  1695. //to reload properly
  1696. Ref<Resource> r = ResourceCache::get_ref(p_file);
  1697. if (r.is_valid()) {
  1698. if (!r->get_import_path().is_empty()) {
  1699. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
  1700. r->set_import_path(dst_path);
  1701. r->set_import_last_modified_time(0);
  1702. }
  1703. }
  1704. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1705. }
  1706. void EditorFileSystem::_find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport) {
  1707. int fc = efd->files.size();
  1708. const EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptr();
  1709. for (int i = 0; i < fc; i++) {
  1710. if (groups_to_reimport.has(files[i]->import_group_file)) {
  1711. if (!group_files.has(files[i]->import_group_file)) {
  1712. group_files[files[i]->import_group_file] = Vector<String>();
  1713. }
  1714. group_files[files[i]->import_group_file].push_back(efd->get_file_path(i));
  1715. }
  1716. }
  1717. for (int i = 0; i < efd->get_subdir_count(); i++) {
  1718. _find_group_files(efd->get_subdir(i), group_files, groups_to_reimport);
  1719. }
  1720. }
  1721. void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params) {
  1722. _reimport_file(p_file, &p_custom_params, p_importer);
  1723. }
  1724. void EditorFileSystem::_reimport_thread(uint32_t p_index, ImportThreadData *p_import_data) {
  1725. p_import_data->max_index = MAX(p_import_data->reimport_from + int(p_index), p_import_data->max_index);
  1726. _reimport_file(p_import_data->reimport_files[p_import_data->reimport_from + p_index].path);
  1727. }
  1728. void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
  1729. importing = true;
  1730. EditorProgress pr("reimport", TTR("(Re)Importing Assets"), p_files.size());
  1731. Vector<ImportFile> reimport_files;
  1732. HashSet<String> groups_to_reimport;
  1733. for (int i = 0; i < p_files.size(); i++) {
  1734. String file = p_files[i];
  1735. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(file);
  1736. if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
  1737. file = ResourceUID::get_singleton()->get_id_path(uid);
  1738. }
  1739. String group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(file);
  1740. if (group_file_cache.has(file)) {
  1741. //maybe the file itself is a group!
  1742. groups_to_reimport.insert(file);
  1743. //groups do not belong to grups
  1744. group_file = String();
  1745. } else if (!group_file.is_empty()) {
  1746. //it's a group file, add group to import and skip this file
  1747. groups_to_reimport.insert(group_file);
  1748. } else {
  1749. //it's a regular file
  1750. ImportFile ifile;
  1751. ifile.path = file;
  1752. ResourceFormatImporter::get_singleton()->get_import_order_threads_and_importer(file, ifile.order, ifile.threaded, ifile.importer);
  1753. reimport_files.push_back(ifile);
  1754. }
  1755. //group may have changed, so also update group reference
  1756. EditorFileSystemDirectory *fs = nullptr;
  1757. int cpos = -1;
  1758. if (_find_file(file, &fs, cpos)) {
  1759. fs->files.write[cpos]->import_group_file = group_file;
  1760. }
  1761. }
  1762. reimport_files.sort();
  1763. bool use_threads = GLOBAL_GET("editor/import/use_multiple_threads");
  1764. int from = 0;
  1765. for (int i = 0; i < reimport_files.size(); i++) {
  1766. if (use_threads && reimport_files[i].threaded) {
  1767. if (i + 1 == reimport_files.size() || reimport_files[i + 1].importer != reimport_files[from].importer) {
  1768. if (from - i == 0) {
  1769. //single file, do not use threads
  1770. pr.step(reimport_files[i].path.get_file(), i);
  1771. _reimport_file(reimport_files[i].path);
  1772. } else {
  1773. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(reimport_files[from].importer);
  1774. ERR_CONTINUE(!importer.is_valid());
  1775. importer->import_threaded_begin();
  1776. ImportThreadData data;
  1777. data.max_index = from;
  1778. data.reimport_from = from;
  1779. data.reimport_files = reimport_files.ptr();
  1780. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &EditorFileSystem::_reimport_thread, &data, i - from + 1, -1, false, vformat(TTR("Import resources of type: %s"), reimport_files[from].importer));
  1781. int current_index = from - 1;
  1782. do {
  1783. if (current_index < data.max_index) {
  1784. current_index = data.max_index;
  1785. pr.step(reimport_files[current_index].path.get_file(), current_index);
  1786. }
  1787. OS::get_singleton()->delay_usec(1);
  1788. } while (!WorkerThreadPool::get_singleton()->is_group_task_completed(group_task));
  1789. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  1790. importer->import_threaded_end();
  1791. }
  1792. from = i + 1;
  1793. }
  1794. } else {
  1795. pr.step(reimport_files[i].path.get_file(), i);
  1796. _reimport_file(reimport_files[i].path);
  1797. }
  1798. }
  1799. //reimport groups
  1800. if (groups_to_reimport.size()) {
  1801. HashMap<String, Vector<String>> group_files;
  1802. _find_group_files(filesystem, group_files, groups_to_reimport);
  1803. for (const KeyValue<String, Vector<String>> &E : group_files) {
  1804. Error err = _reimport_group(E.key, E.value);
  1805. if (err == OK) {
  1806. _reimport_file(E.key);
  1807. }
  1808. }
  1809. }
  1810. ResourceUID::get_singleton()->update_cache(); //after reimporting, update the cache
  1811. _save_filesystem_cache();
  1812. importing = false;
  1813. if (!is_scanning()) {
  1814. emit_signal(SNAME("filesystem_changed"));
  1815. }
  1816. emit_signal(SNAME("resources_reimported"), p_files);
  1817. }
  1818. Error EditorFileSystem::_resource_import(const String &p_path) {
  1819. Vector<String> files;
  1820. files.push_back(p_path);
  1821. singleton->update_file(p_path);
  1822. singleton->reimport_files(files);
  1823. return OK;
  1824. }
  1825. bool EditorFileSystem::_should_skip_directory(const String &p_path) {
  1826. String project_data_path = ProjectSettings::get_singleton()->get_project_data_path();
  1827. if (p_path == project_data_path || p_path.begins_with(project_data_path + "/")) {
  1828. return true;
  1829. }
  1830. if (FileAccess::exists(p_path.path_join("project.godot"))) {
  1831. // skip if another project inside this
  1832. return true;
  1833. }
  1834. if (FileAccess::exists(p_path.path_join(".gdignore"))) {
  1835. // skip if a `.gdignore` file is inside this
  1836. return true;
  1837. }
  1838. return false;
  1839. }
  1840. bool EditorFileSystem::is_group_file(const String &p_path) const {
  1841. return group_file_cache.has(p_path);
  1842. }
  1843. void EditorFileSystem::_move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location) {
  1844. int fc = efd->files.size();
  1845. EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptrw();
  1846. for (int i = 0; i < fc; i++) {
  1847. if (files[i]->import_group_file == p_group_file) {
  1848. files[i]->import_group_file = p_new_location;
  1849. Ref<ConfigFile> config;
  1850. config.instantiate();
  1851. String path = efd->get_file_path(i) + ".import";
  1852. Error err = config->load(path);
  1853. if (err != OK) {
  1854. continue;
  1855. }
  1856. if (config->has_section_key("remap", "group_file")) {
  1857. config->set_value("remap", "group_file", p_new_location);
  1858. }
  1859. List<String> sk;
  1860. config->get_section_keys("params", &sk);
  1861. for (const String &param : sk) {
  1862. //not very clean, but should work
  1863. String value = config->get_value("params", param);
  1864. if (value == p_group_file) {
  1865. config->set_value("params", param, p_new_location);
  1866. }
  1867. }
  1868. config->save(path);
  1869. }
  1870. }
  1871. for (int i = 0; i < efd->get_subdir_count(); i++) {
  1872. _move_group_files(efd->get_subdir(i), p_group_file, p_new_location);
  1873. }
  1874. }
  1875. void EditorFileSystem::move_group_file(const String &p_path, const String &p_new_path) {
  1876. if (get_filesystem()) {
  1877. _move_group_files(get_filesystem(), p_path, p_new_path);
  1878. if (group_file_cache.has(p_path)) {
  1879. group_file_cache.erase(p_path);
  1880. group_file_cache.insert(p_new_path);
  1881. }
  1882. }
  1883. }
  1884. ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate) {
  1885. if (!p_path.is_resource_file() || p_path.begins_with(ProjectSettings::get_singleton()->get_project_data_path())) {
  1886. //saved externally (configuration file) or internal file, do not assign an ID.
  1887. return ResourceUID::INVALID_ID;
  1888. }
  1889. EditorFileSystemDirectory *fs = nullptr;
  1890. int cpos = -1;
  1891. if (!singleton->_find_file(p_path, &fs, cpos)) {
  1892. if (p_generate) {
  1893. return ResourceUID::get_singleton()->create_id(); //just create a new one, we will be notified of save anyway and fetch the right UUID at that time, to keep things simple.
  1894. } else {
  1895. return ResourceUID::INVALID_ID;
  1896. }
  1897. } else if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  1898. return fs->files[cpos]->uid;
  1899. } else if (p_generate) {
  1900. return ResourceUID::get_singleton()->create_id(); //just create a new one, we will be notified of save anyway and fetch the right UUID at that time, to keep things simple.
  1901. } else {
  1902. return ResourceUID::INVALID_ID;
  1903. }
  1904. }
  1905. static void _scan_extensions_dir(EditorFileSystemDirectory *d, HashSet<String> &extensions) {
  1906. int fc = d->get_file_count();
  1907. for (int i = 0; i < fc; i++) {
  1908. if (d->get_file_type(i) == SNAME("NativeExtension")) {
  1909. extensions.insert(d->get_file_path(i));
  1910. }
  1911. }
  1912. int dc = d->get_subdir_count();
  1913. for (int i = 0; i < dc; i++) {
  1914. _scan_extensions_dir(d->get_subdir(i), extensions);
  1915. }
  1916. }
  1917. bool EditorFileSystem::_scan_extensions() {
  1918. EditorFileSystemDirectory *d = get_filesystem();
  1919. HashSet<String> extensions;
  1920. _scan_extensions_dir(d, extensions);
  1921. //verify against loaded extensions
  1922. Vector<String> extensions_added;
  1923. Vector<String> extensions_removed;
  1924. for (const String &E : extensions) {
  1925. if (!NativeExtensionManager::get_singleton()->is_extension_loaded(E)) {
  1926. extensions_added.push_back(E);
  1927. }
  1928. }
  1929. Vector<String> loaded_extensions = NativeExtensionManager::get_singleton()->get_loaded_extensions();
  1930. for (int i = 0; i < loaded_extensions.size(); i++) {
  1931. if (!extensions.has(loaded_extensions[i])) {
  1932. extensions_removed.push_back(loaded_extensions[i]);
  1933. }
  1934. }
  1935. String extension_list_config_file = NativeExtension::get_extension_list_config_file();
  1936. if (extensions.size()) {
  1937. if (extensions_added.size() || extensions_removed.size()) { //extensions were added or removed
  1938. Ref<FileAccess> f = FileAccess::open(extension_list_config_file, FileAccess::WRITE);
  1939. for (const String &E : extensions) {
  1940. f->store_line(E);
  1941. }
  1942. }
  1943. } else {
  1944. if (loaded_extensions.size() || FileAccess::exists(extension_list_config_file)) { //extensions were removed
  1945. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1946. da->remove(extension_list_config_file);
  1947. }
  1948. }
  1949. bool needs_restart = false;
  1950. for (int i = 0; i < extensions_added.size(); i++) {
  1951. NativeExtensionManager::LoadStatus st = NativeExtensionManager::get_singleton()->load_extension(extensions_added[i]);
  1952. if (st == NativeExtensionManager::LOAD_STATUS_FAILED) {
  1953. EditorNode::get_singleton()->add_io_error("Error loading extension: " + extensions_added[i]);
  1954. } else if (st == NativeExtensionManager::LOAD_STATUS_NEEDS_RESTART) {
  1955. needs_restart = true;
  1956. }
  1957. }
  1958. for (int i = 0; i < extensions_removed.size(); i++) {
  1959. NativeExtensionManager::LoadStatus st = NativeExtensionManager::get_singleton()->unload_extension(extensions_removed[i]);
  1960. if (st == NativeExtensionManager::LOAD_STATUS_FAILED) {
  1961. EditorNode::get_singleton()->add_io_error("Error removing extension: " + extensions_added[i]);
  1962. } else if (st == NativeExtensionManager::LOAD_STATUS_NEEDS_RESTART) {
  1963. needs_restart = true;
  1964. }
  1965. }
  1966. return needs_restart;
  1967. }
  1968. void EditorFileSystem::_bind_methods() {
  1969. ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
  1970. ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
  1971. ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
  1972. ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
  1973. ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
  1974. ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
  1975. ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
  1976. ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
  1977. ClassDB::bind_method(D_METHOD("update_script_classes"), &EditorFileSystem::update_script_classes);
  1978. ClassDB::bind_method(D_METHOD("reimport_files", "files"), &EditorFileSystem::reimport_files);
  1979. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  1980. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  1981. ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  1982. ADD_SIGNAL(MethodInfo("resources_reload", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  1983. }
  1984. void EditorFileSystem::_update_extensions() {
  1985. valid_extensions.clear();
  1986. import_extensions.clear();
  1987. textfile_extensions.clear();
  1988. List<String> extensionsl;
  1989. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  1990. for (const String &E : extensionsl) {
  1991. valid_extensions.insert(E);
  1992. }
  1993. const Vector<String> textfile_ext = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false);
  1994. for (const String &E : textfile_ext) {
  1995. if (valid_extensions.has(E)) {
  1996. continue;
  1997. }
  1998. valid_extensions.insert(E);
  1999. textfile_extensions.insert(E);
  2000. }
  2001. extensionsl.clear();
  2002. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  2003. for (const String &E : extensionsl) {
  2004. import_extensions.insert(E);
  2005. }
  2006. }
  2007. void EditorFileSystem::add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2008. ERR_FAIL_COND(import_support_queries.find(p_query) != -1);
  2009. import_support_queries.push_back(p_query);
  2010. }
  2011. void EditorFileSystem::remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2012. import_support_queries.erase(p_query);
  2013. }
  2014. EditorFileSystem::EditorFileSystem() {
  2015. ResourceLoader::import = _resource_import;
  2016. reimport_on_missing_imported_files = GLOBAL_DEF("editor/import/reimport_missing_imported_files", true);
  2017. GLOBAL_DEF("editor/import/use_multiple_threads", true);
  2018. singleton = this;
  2019. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  2020. filesystem->parent = nullptr;
  2021. new_filesystem = nullptr;
  2022. // This should probably also work on Unix and use the string it returns for FAT32 or exFAT
  2023. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2024. using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "exFAT");
  2025. scan_total = 0;
  2026. update_script_classes_queued.clear();
  2027. ResourceUID::get_singleton()->clear(); //will be updated on scan
  2028. ResourceSaver::set_get_resource_id_for_path(_resource_saver_get_resource_id_for_path);
  2029. }
  2030. EditorFileSystem::~EditorFileSystem() {
  2031. ResourceSaver::set_get_resource_id_for_path(nullptr);
  2032. }