editor_file_system.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/io/resource_importer.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/os/file_access.h"
  35. #include "core/os/os.h"
  36. #include "core/project_settings.h"
  37. #include "core/variant_parser.h"
  38. #include "editor_node.h"
  39. #include "editor_resource_preview.h"
  40. #include "editor_settings.h"
  41. EditorFileSystem *EditorFileSystem::singleton = nullptr;
  42. //the name is the version, to keep compatibility with different versions of Godot
  43. #define CACHE_FILE_NAME "filesystem_cache6"
  44. void EditorFileSystemDirectory::sort_files() {
  45. files.sort_custom<FileInfoSort>();
  46. }
  47. int EditorFileSystemDirectory::find_file_index(const String &p_file) const {
  48. for (int i = 0; i < files.size(); i++) {
  49. if (files[i]->file == p_file)
  50. return i;
  51. }
  52. return -1;
  53. }
  54. int EditorFileSystemDirectory::find_dir_index(const String &p_dir) const {
  55. for (int i = 0; i < subdirs.size(); i++) {
  56. if (subdirs[i]->name == p_dir)
  57. return i;
  58. }
  59. return -1;
  60. }
  61. int EditorFileSystemDirectory::get_subdir_count() const {
  62. return subdirs.size();
  63. }
  64. EditorFileSystemDirectory *EditorFileSystemDirectory::get_subdir(int p_idx) {
  65. ERR_FAIL_INDEX_V(p_idx, subdirs.size(), nullptr);
  66. return subdirs[p_idx];
  67. }
  68. int EditorFileSystemDirectory::get_file_count() const {
  69. return files.size();
  70. }
  71. String EditorFileSystemDirectory::get_file(int p_idx) const {
  72. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  73. return files[p_idx]->file;
  74. }
  75. String EditorFileSystemDirectory::get_path() const {
  76. String p;
  77. const EditorFileSystemDirectory *d = this;
  78. while (d->parent) {
  79. p = d->name.plus_file(p);
  80. d = d->parent;
  81. }
  82. return "res://" + p;
  83. }
  84. String EditorFileSystemDirectory::get_file_path(int p_idx) const {
  85. String file = get_file(p_idx);
  86. const EditorFileSystemDirectory *d = this;
  87. while (d->parent) {
  88. file = d->name.plus_file(file);
  89. d = d->parent;
  90. }
  91. return "res://" + file;
  92. }
  93. Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {
  94. ERR_FAIL_INDEX_V(p_idx, files.size(), Vector<String>());
  95. return files[p_idx]->deps;
  96. }
  97. bool EditorFileSystemDirectory::get_file_import_is_valid(int p_idx) const {
  98. ERR_FAIL_INDEX_V(p_idx, files.size(), false);
  99. return files[p_idx]->import_valid;
  100. }
  101. String EditorFileSystemDirectory::get_file_script_class_name(int p_idx) const {
  102. return files[p_idx]->script_class_name;
  103. }
  104. String EditorFileSystemDirectory::get_file_script_class_extends(int p_idx) const {
  105. return files[p_idx]->script_class_extends;
  106. }
  107. String EditorFileSystemDirectory::get_file_script_class_icon_path(int p_idx) const {
  108. return files[p_idx]->script_class_icon_path;
  109. }
  110. StringName EditorFileSystemDirectory::get_file_type(int p_idx) const {
  111. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  112. return files[p_idx]->type;
  113. }
  114. String EditorFileSystemDirectory::get_name() {
  115. return name;
  116. }
  117. EditorFileSystemDirectory *EditorFileSystemDirectory::get_parent() {
  118. return parent;
  119. }
  120. void EditorFileSystemDirectory::_bind_methods() {
  121. ClassDB::bind_method(D_METHOD("get_subdir_count"), &EditorFileSystemDirectory::get_subdir_count);
  122. ClassDB::bind_method(D_METHOD("get_subdir", "idx"), &EditorFileSystemDirectory::get_subdir);
  123. ClassDB::bind_method(D_METHOD("get_file_count"), &EditorFileSystemDirectory::get_file_count);
  124. ClassDB::bind_method(D_METHOD("get_file", "idx"), &EditorFileSystemDirectory::get_file);
  125. ClassDB::bind_method(D_METHOD("get_file_path", "idx"), &EditorFileSystemDirectory::get_file_path);
  126. ClassDB::bind_method(D_METHOD("get_file_type", "idx"), &EditorFileSystemDirectory::get_file_type);
  127. ClassDB::bind_method(D_METHOD("get_file_script_class_name", "idx"), &EditorFileSystemDirectory::get_file_script_class_name);
  128. ClassDB::bind_method(D_METHOD("get_file_script_class_extends", "idx"), &EditorFileSystemDirectory::get_file_script_class_extends);
  129. ClassDB::bind_method(D_METHOD("get_file_import_is_valid", "idx"), &EditorFileSystemDirectory::get_file_import_is_valid);
  130. ClassDB::bind_method(D_METHOD("get_name"), &EditorFileSystemDirectory::get_name);
  131. ClassDB::bind_method(D_METHOD("get_path"), &EditorFileSystemDirectory::get_path);
  132. ClassDB::bind_method(D_METHOD("get_parent"), &EditorFileSystemDirectory::get_parent);
  133. ClassDB::bind_method(D_METHOD("find_file_index", "name"), &EditorFileSystemDirectory::find_file_index);
  134. ClassDB::bind_method(D_METHOD("find_dir_index", "name"), &EditorFileSystemDirectory::find_dir_index);
  135. }
  136. EditorFileSystemDirectory::EditorFileSystemDirectory() {
  137. modified_time = 0;
  138. parent = nullptr;
  139. verified = false;
  140. }
  141. EditorFileSystemDirectory::~EditorFileSystemDirectory() {
  142. for (int i = 0; i < files.size(); i++) {
  143. memdelete(files[i]);
  144. }
  145. for (int i = 0; i < subdirs.size(); i++) {
  146. memdelete(subdirs[i]);
  147. }
  148. }
  149. void EditorFileSystem::_scan_filesystem() {
  150. ERR_FAIL_COND(!scanning || new_filesystem);
  151. //read .fscache
  152. String cpath;
  153. sources_changed.clear();
  154. file_cache.clear();
  155. String project = ProjectSettings::get_singleton()->get_resource_path();
  156. String fscache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(CACHE_FILE_NAME);
  157. FileAccess *f = FileAccess::open(fscache, FileAccess::READ);
  158. bool first = true;
  159. if (f) {
  160. //read the disk cache
  161. while (!f->eof_reached()) {
  162. String l = f->get_line().strip_edges();
  163. if (first) {
  164. if (first_scan) {
  165. // only use this on first scan, afterwards it gets ignored
  166. // this is so on first reimport we synchronize versions, then
  167. // we don't care until editor restart. This is for usability mainly so
  168. // your workflow is not killed after changing a setting by forceful reimporting
  169. // everything there is.
  170. filesystem_settings_version_for_import = l.strip_edges();
  171. if (filesystem_settings_version_for_import != ResourceFormatImporter::get_singleton()->get_import_settings_hash()) {
  172. revalidate_import_files = true;
  173. }
  174. }
  175. first = false;
  176. continue;
  177. }
  178. if (l == String())
  179. continue;
  180. if (l.begins_with("::")) {
  181. Vector<String> split = l.split("::");
  182. ERR_CONTINUE(split.size() != 3);
  183. String name = split[1];
  184. cpath = name;
  185. } else {
  186. Vector<String> split = l.split("::");
  187. ERR_CONTINUE(split.size() != 8);
  188. String name = split[0];
  189. String file;
  190. file = name;
  191. name = cpath.plus_file(name);
  192. FileCache fc;
  193. fc.type = split[1];
  194. fc.modification_time = split[2].to_int64();
  195. fc.import_modification_time = split[3].to_int64();
  196. fc.import_valid = split[4].to_int64() != 0;
  197. fc.import_group_file = split[5].strip_edges();
  198. fc.script_class_name = split[6].get_slice("<>", 0);
  199. fc.script_class_extends = split[6].get_slice("<>", 1);
  200. fc.script_class_icon_path = split[6].get_slice("<>", 2);
  201. String deps = split[7].strip_edges();
  202. if (deps.length()) {
  203. Vector<String> dp = deps.split("<>");
  204. for (int i = 0; i < dp.size(); i++) {
  205. String path = dp[i];
  206. fc.deps.push_back(path);
  207. }
  208. }
  209. file_cache[name] = fc;
  210. }
  211. }
  212. f->close();
  213. memdelete(f);
  214. }
  215. String update_cache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file("filesystem_update4");
  216. if (FileAccess::exists(update_cache)) {
  217. {
  218. FileAccessRef f2 = FileAccess::open(update_cache, FileAccess::READ);
  219. String l = f2->get_line().strip_edges();
  220. while (l != String()) {
  221. file_cache.erase(l); //erase cache for this, so it gets updated
  222. l = f2->get_line().strip_edges();
  223. }
  224. }
  225. DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  226. d->remove(update_cache); //bye bye update cache
  227. }
  228. EditorProgressBG scan_progress("efs", "ScanFS", 1000);
  229. ScanProgress sp;
  230. sp.low = 0;
  231. sp.hi = 1;
  232. sp.progress = &scan_progress;
  233. new_filesystem = memnew(EditorFileSystemDirectory);
  234. new_filesystem->parent = nullptr;
  235. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  236. d->change_dir("res://");
  237. _scan_new_dir(new_filesystem, d, sp);
  238. file_cache.clear(); //clear caches, no longer needed
  239. memdelete(d);
  240. if (!first_scan) {
  241. //on the first scan this is done from the main thread after re-importing
  242. _save_filesystem_cache();
  243. }
  244. scanning = false;
  245. }
  246. void EditorFileSystem::_save_filesystem_cache() {
  247. group_file_cache.clear();
  248. String fscache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(CACHE_FILE_NAME);
  249. FileAccess *f = FileAccess::open(fscache, FileAccess::WRITE);
  250. ERR_FAIL_COND_MSG(!f, "Cannot create file '" + fscache + "'. Check user write permissions.");
  251. f->store_line(filesystem_settings_version_for_import);
  252. _save_filesystem_cache(filesystem, f);
  253. f->close();
  254. memdelete(f);
  255. }
  256. void EditorFileSystem::_thread_func(void *_userdata) {
  257. EditorFileSystem *sd = (EditorFileSystem *)_userdata;
  258. sd->_scan_filesystem();
  259. }
  260. bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_imported_files) {
  261. if (!reimport_on_missing_imported_files && p_only_imported_files)
  262. return false;
  263. if (!FileAccess::exists(p_path + ".import")) {
  264. return true;
  265. }
  266. if (!ResourceFormatImporter::get_singleton()->are_import_settings_valid(p_path)) {
  267. //reimport settings are not valid, reimport
  268. return true;
  269. }
  270. Error err;
  271. FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  272. if (!f) { //no import file, do reimport
  273. return true;
  274. }
  275. VariantParser::StreamFile stream;
  276. stream.f = f;
  277. String assign;
  278. Variant value;
  279. VariantParser::Tag next_tag;
  280. int lines = 0;
  281. String error_text;
  282. List<String> to_check;
  283. String source_file = "";
  284. String source_md5 = "";
  285. Vector<String> dest_files;
  286. String dest_md5 = "";
  287. while (true) {
  288. assign = Variant();
  289. next_tag.fields.clear();
  290. next_tag.name = String();
  291. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  292. if (err == ERR_FILE_EOF) {
  293. break;
  294. } else if (err != OK) {
  295. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
  296. memdelete(f);
  297. return false; //parse error, try reimport manually (Avoid reimport loop on broken file)
  298. }
  299. if (assign != String()) {
  300. if (assign.begins_with("path")) {
  301. to_check.push_back(value);
  302. } else if (assign == "files") {
  303. Array fa = value;
  304. for (int i = 0; i < fa.size(); i++) {
  305. to_check.push_back(fa[i]);
  306. }
  307. } else if (!p_only_imported_files) {
  308. if (assign == "source_file") {
  309. source_file = value;
  310. } else if (assign == "dest_files") {
  311. dest_files = value;
  312. }
  313. }
  314. } else if (next_tag.name != "remap" && next_tag.name != "deps") {
  315. break;
  316. }
  317. }
  318. memdelete(f);
  319. // Read the md5's from a separate file (so the import parameters aren't dependent on the file version
  320. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path);
  321. FileAccess *md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err);
  322. if (!md5s) { // No md5's stored for this resource
  323. return true;
  324. }
  325. VariantParser::StreamFile md5_stream;
  326. md5_stream.f = md5s;
  327. while (true) {
  328. assign = Variant();
  329. next_tag.fields.clear();
  330. next_tag.name = String();
  331. err = VariantParser::parse_tag_assign_eof(&md5_stream, lines, error_text, next_tag, assign, value, nullptr, true);
  332. if (err == ERR_FILE_EOF) {
  333. break;
  334. } else if (err != OK) {
  335. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import.md5:" + itos(lines) + "' error '" + error_text + "'.");
  336. memdelete(md5s);
  337. return false; // parse error
  338. }
  339. if (assign != String()) {
  340. if (!p_only_imported_files) {
  341. if (assign == "source_md5") {
  342. source_md5 = value;
  343. } else if (assign == "dest_md5") {
  344. dest_md5 = value;
  345. }
  346. }
  347. }
  348. }
  349. memdelete(md5s);
  350. //imported files are gone, reimport
  351. for (List<String>::Element *E = to_check.front(); E; E = E->next()) {
  352. if (!FileAccess::exists(E->get())) {
  353. return true;
  354. }
  355. }
  356. //check source md5 matching
  357. if (!p_only_imported_files) {
  358. if (source_file != String() && source_file != p_path) {
  359. return true; //file was moved, reimport
  360. }
  361. if (source_md5 == String()) {
  362. return true; //lacks md5, so just reimport
  363. }
  364. String md5 = FileAccess::get_md5(p_path);
  365. if (md5 != source_md5) {
  366. return true;
  367. }
  368. if (dest_files.size() && dest_md5 != String()) {
  369. md5 = FileAccess::get_multiple_md5(dest_files);
  370. if (md5 != dest_md5) {
  371. return true;
  372. }
  373. }
  374. }
  375. return false; //nothing changed
  376. }
  377. bool EditorFileSystem::_update_scan_actions() {
  378. sources_changed.clear();
  379. bool fs_changed = false;
  380. Vector<String> reimports;
  381. Vector<String> reloads;
  382. for (List<ItemAction>::Element *E = scan_actions.front(); E; E = E->next()) {
  383. ItemAction &ia = E->get();
  384. switch (ia.action) {
  385. case ItemAction::ACTION_NONE: {
  386. } break;
  387. case ItemAction::ACTION_DIR_ADD: {
  388. int idx = 0;
  389. for (int i = 0; i < ia.dir->subdirs.size(); i++) {
  390. if (ia.new_dir->name < ia.dir->subdirs[i]->name)
  391. break;
  392. idx++;
  393. }
  394. if (idx == ia.dir->subdirs.size()) {
  395. ia.dir->subdirs.push_back(ia.new_dir);
  396. } else {
  397. ia.dir->subdirs.insert(idx, ia.new_dir);
  398. }
  399. fs_changed = true;
  400. } break;
  401. case ItemAction::ACTION_DIR_REMOVE: {
  402. ERR_CONTINUE(!ia.dir->parent);
  403. ia.dir->parent->subdirs.erase(ia.dir);
  404. memdelete(ia.dir);
  405. fs_changed = true;
  406. } break;
  407. case ItemAction::ACTION_FILE_ADD: {
  408. int idx = 0;
  409. for (int i = 0; i < ia.dir->files.size(); i++) {
  410. if (ia.new_file->file < ia.dir->files[i]->file)
  411. break;
  412. idx++;
  413. }
  414. if (idx == ia.dir->files.size()) {
  415. ia.dir->files.push_back(ia.new_file);
  416. } else {
  417. ia.dir->files.insert(idx, ia.new_file);
  418. }
  419. fs_changed = true;
  420. } break;
  421. case ItemAction::ACTION_FILE_REMOVE: {
  422. int idx = ia.dir->find_file_index(ia.file);
  423. ERR_CONTINUE(idx == -1);
  424. _delete_internal_files(ia.dir->files[idx]->file);
  425. memdelete(ia.dir->files[idx]);
  426. ia.dir->files.remove(idx);
  427. fs_changed = true;
  428. } break;
  429. case ItemAction::ACTION_FILE_TEST_REIMPORT: {
  430. int idx = ia.dir->find_file_index(ia.file);
  431. ERR_CONTINUE(idx == -1);
  432. String full_path = ia.dir->get_file_path(idx);
  433. if (_test_for_reimport(full_path, false)) {
  434. //must reimport
  435. reimports.push_back(full_path);
  436. } else {
  437. //must not reimport, all was good
  438. //update modified times, to avoid reimport
  439. ia.dir->files[idx]->modified_time = FileAccess::get_modified_time(full_path);
  440. ia.dir->files[idx]->import_modified_time = FileAccess::get_modified_time(full_path + ".import");
  441. }
  442. fs_changed = true;
  443. } break;
  444. case ItemAction::ACTION_FILE_RELOAD: {
  445. int idx = ia.dir->find_file_index(ia.file);
  446. ERR_CONTINUE(idx == -1);
  447. String full_path = ia.dir->get_file_path(idx);
  448. reloads.push_back(full_path);
  449. } break;
  450. }
  451. }
  452. if (reimports.size()) {
  453. reimport_files(reimports);
  454. }
  455. if (first_scan) {
  456. //only on first scan this is valid and updated, then settings changed.
  457. revalidate_import_files = false;
  458. filesystem_settings_version_for_import = ResourceFormatImporter::get_singleton()->get_import_settings_hash();
  459. _save_filesystem_cache();
  460. }
  461. if (reloads.size()) {
  462. emit_signal("resources_reload", reloads);
  463. }
  464. scan_actions.clear();
  465. return fs_changed;
  466. }
  467. void EditorFileSystem::scan() {
  468. if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/)
  469. return;
  470. if (scanning || scanning_changes || thread)
  471. return;
  472. _update_extensions();
  473. abort_scan = false;
  474. if (!use_threads) {
  475. scanning = true;
  476. scan_total = 0;
  477. _scan_filesystem();
  478. if (filesystem)
  479. memdelete(filesystem);
  480. //file_type_cache.clear();
  481. filesystem = new_filesystem;
  482. new_filesystem = nullptr;
  483. _update_scan_actions();
  484. scanning = false;
  485. emit_signal("filesystem_changed");
  486. emit_signal("sources_changed", sources_changed.size() > 0);
  487. _queue_update_script_classes();
  488. first_scan = false;
  489. } else {
  490. ERR_FAIL_COND(thread);
  491. set_process(true);
  492. Thread::Settings s;
  493. scanning = true;
  494. scan_total = 0;
  495. s.priority = Thread::PRIORITY_LOW;
  496. thread = Thread::create(_thread_func, this, s);
  497. //tree->hide();
  498. //progress->show();
  499. }
  500. }
  501. void EditorFileSystem::ScanProgress::update(int p_current, int p_total) const {
  502. float ratio = low + ((hi - low) / p_total) * p_current;
  503. progress->step(ratio * 1000);
  504. EditorFileSystem::singleton->scan_total = ratio;
  505. }
  506. EditorFileSystem::ScanProgress EditorFileSystem::ScanProgress::get_sub(int p_current, int p_total) const {
  507. ScanProgress sp = *this;
  508. float slice = (sp.hi - sp.low) / p_total;
  509. sp.low += slice * p_current;
  510. sp.hi = slice;
  511. return sp;
  512. }
  513. void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess *da, const ScanProgress &p_progress) {
  514. List<String> dirs;
  515. List<String> files;
  516. String cd = da->get_current_dir();
  517. p_dir->modified_time = FileAccess::get_modified_time(cd);
  518. da->list_dir_begin();
  519. while (true) {
  520. String f = da->get_next();
  521. if (f == "")
  522. break;
  523. if (da->current_is_hidden())
  524. continue;
  525. if (da->current_is_dir()) {
  526. if (f.begins_with(".")) // Ignore special and . / ..
  527. continue;
  528. if (FileAccess::exists(cd.plus_file(f).plus_file("project.godot"))) // skip if another project inside this
  529. continue;
  530. if (FileAccess::exists(cd.plus_file(f).plus_file(".gdignore"))) // skip if another project inside this
  531. continue;
  532. dirs.push_back(f);
  533. } else {
  534. files.push_back(f);
  535. }
  536. }
  537. da->list_dir_end();
  538. dirs.sort_custom<NaturalNoCaseComparator>();
  539. files.sort_custom<NaturalNoCaseComparator>();
  540. int total = dirs.size() + files.size();
  541. int idx = 0;
  542. for (List<String>::Element *E = dirs.front(); E; E = E->next(), idx++) {
  543. if (da->change_dir(E->get()) == OK) {
  544. String d = da->get_current_dir();
  545. if (d == cd || !d.begins_with(cd)) {
  546. da->change_dir(cd); //avoid recursion
  547. } else {
  548. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  549. efd->parent = p_dir;
  550. efd->name = E->get();
  551. _scan_new_dir(efd, da, p_progress.get_sub(idx, total));
  552. int idx2 = 0;
  553. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  554. if (efd->name < p_dir->subdirs[i]->name)
  555. break;
  556. idx2++;
  557. }
  558. if (idx2 == p_dir->subdirs.size()) {
  559. p_dir->subdirs.push_back(efd);
  560. } else {
  561. p_dir->subdirs.insert(idx2, efd);
  562. }
  563. da->change_dir("..");
  564. }
  565. } else {
  566. ERR_PRINT("Cannot go into subdir '" + E->get() + "'.");
  567. }
  568. p_progress.update(idx, total);
  569. }
  570. for (List<String>::Element *E = files.front(); E; E = E->next(), idx++) {
  571. String ext = E->get().get_extension().to_lower();
  572. if (!valid_extensions.has(ext)) {
  573. continue; //invalid
  574. }
  575. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  576. fi->file = E->get();
  577. String path = cd.plus_file(fi->file);
  578. FileCache *fc = file_cache.getptr(path);
  579. uint64_t mt = FileAccess::get_modified_time(path);
  580. if (import_extensions.has(ext)) {
  581. //is imported
  582. uint64_t import_mt = 0;
  583. if (FileAccess::exists(path + ".import")) {
  584. import_mt = FileAccess::get_modified_time(path + ".import");
  585. }
  586. if (fc && fc->modification_time == mt && fc->import_modification_time == import_mt && !_test_for_reimport(path, true)) {
  587. fi->type = fc->type;
  588. fi->deps = fc->deps;
  589. fi->modified_time = fc->modification_time;
  590. fi->import_modified_time = fc->import_modification_time;
  591. fi->import_valid = fc->import_valid;
  592. fi->script_class_name = fc->script_class_name;
  593. fi->import_group_file = fc->import_group_file;
  594. fi->script_class_extends = fc->script_class_extends;
  595. fi->script_class_icon_path = fc->script_class_icon_path;
  596. if (revalidate_import_files && !ResourceFormatImporter::get_singleton()->are_import_settings_valid(path)) {
  597. ItemAction ia;
  598. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  599. ia.dir = p_dir;
  600. ia.file = E->get();
  601. scan_actions.push_back(ia);
  602. }
  603. if (fc->type == String()) {
  604. fi->type = ResourceLoader::get_resource_type(path);
  605. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  606. //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?)
  607. //note: I think this should not happen any longer..
  608. }
  609. } else {
  610. fi->type = ResourceFormatImporter::get_singleton()->get_resource_type(path);
  611. fi->import_group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(path);
  612. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  613. fi->modified_time = 0;
  614. fi->import_modified_time = 0;
  615. fi->import_valid = ResourceLoader::is_import_valid(path);
  616. ItemAction ia;
  617. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  618. ia.dir = p_dir;
  619. ia.file = E->get();
  620. scan_actions.push_back(ia);
  621. }
  622. } else {
  623. if (fc && fc->modification_time == mt) {
  624. //not imported, so just update type if changed
  625. fi->type = fc->type;
  626. fi->modified_time = fc->modification_time;
  627. fi->deps = fc->deps;
  628. fi->import_modified_time = 0;
  629. fi->import_valid = true;
  630. fi->script_class_name = fc->script_class_name;
  631. fi->script_class_extends = fc->script_class_extends;
  632. fi->script_class_icon_path = fc->script_class_icon_path;
  633. } else {
  634. //new or modified time
  635. fi->type = ResourceLoader::get_resource_type(path);
  636. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  637. fi->deps = _get_dependencies(path);
  638. fi->modified_time = mt;
  639. fi->import_modified_time = 0;
  640. fi->import_valid = true;
  641. }
  642. }
  643. p_dir->files.push_back(fi);
  644. p_progress.update(idx, total);
  645. }
  646. }
  647. void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress) {
  648. uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
  649. bool updated_dir = false;
  650. String cd = p_dir->get_path();
  651. if (current_mtime != p_dir->modified_time || using_fat32_or_exfat) {
  652. updated_dir = true;
  653. p_dir->modified_time = current_mtime;
  654. //ooooops, dir changed, see what's going on
  655. //first mark everything as veryfied
  656. for (int i = 0; i < p_dir->files.size(); i++) {
  657. p_dir->files[i]->verified = false;
  658. }
  659. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  660. p_dir->get_subdir(i)->verified = false;
  661. }
  662. //then scan files and directories and check what's different
  663. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  664. da->change_dir(cd);
  665. da->list_dir_begin();
  666. while (true) {
  667. String f = da->get_next();
  668. if (f == "")
  669. break;
  670. if (da->current_is_hidden())
  671. continue;
  672. if (da->current_is_dir()) {
  673. if (f.begins_with(".")) // Ignore special and . / ..
  674. continue;
  675. int idx = p_dir->find_dir_index(f);
  676. if (idx == -1) {
  677. if (FileAccess::exists(cd.plus_file(f).plus_file("project.godot"))) // skip if another project inside this
  678. continue;
  679. if (FileAccess::exists(cd.plus_file(f).plus_file(".gdignore"))) // skip if another project inside this
  680. continue;
  681. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  682. efd->parent = p_dir;
  683. efd->name = f;
  684. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  685. d->change_dir(cd.plus_file(f));
  686. _scan_new_dir(efd, d, p_progress.get_sub(1, 1));
  687. memdelete(d);
  688. ItemAction ia;
  689. ia.action = ItemAction::ACTION_DIR_ADD;
  690. ia.dir = p_dir;
  691. ia.file = f;
  692. ia.new_dir = efd;
  693. scan_actions.push_back(ia);
  694. } else {
  695. p_dir->subdirs[idx]->verified = true;
  696. }
  697. } else {
  698. String ext = f.get_extension().to_lower();
  699. if (!valid_extensions.has(ext))
  700. continue; //invalid
  701. int idx = p_dir->find_file_index(f);
  702. if (idx == -1) {
  703. //never seen this file, add actition to add it
  704. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  705. fi->file = f;
  706. String path = cd.plus_file(fi->file);
  707. fi->modified_time = FileAccess::get_modified_time(path);
  708. fi->import_modified_time = 0;
  709. fi->type = ResourceLoader::get_resource_type(path);
  710. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  711. fi->import_valid = ResourceLoader::is_import_valid(path);
  712. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  713. {
  714. ItemAction ia;
  715. ia.action = ItemAction::ACTION_FILE_ADD;
  716. ia.dir = p_dir;
  717. ia.file = f;
  718. ia.new_file = fi;
  719. scan_actions.push_back(ia);
  720. }
  721. if (import_extensions.has(ext)) {
  722. //if it can be imported, and it was added, it needs to be reimported
  723. ItemAction ia;
  724. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  725. ia.dir = p_dir;
  726. ia.file = f;
  727. scan_actions.push_back(ia);
  728. }
  729. } else {
  730. p_dir->files[idx]->verified = true;
  731. }
  732. }
  733. }
  734. da->list_dir_end();
  735. memdelete(da);
  736. }
  737. for (int i = 0; i < p_dir->files.size(); i++) {
  738. if (updated_dir && !p_dir->files[i]->verified) {
  739. //this file was removed, add action to remove it
  740. ItemAction ia;
  741. ia.action = ItemAction::ACTION_FILE_REMOVE;
  742. ia.dir = p_dir;
  743. ia.file = p_dir->files[i]->file;
  744. scan_actions.push_back(ia);
  745. continue;
  746. }
  747. String path = cd.plus_file(p_dir->files[i]->file);
  748. if (import_extensions.has(p_dir->files[i]->file.get_extension().to_lower())) {
  749. //check here if file must be imported or not
  750. uint64_t mt = FileAccess::get_modified_time(path);
  751. bool reimport = false;
  752. if (mt != p_dir->files[i]->modified_time) {
  753. reimport = true; //it was modified, must be reimported.
  754. } else if (!FileAccess::exists(path + ".import")) {
  755. reimport = true; //no .import file, obviously reimport
  756. } else {
  757. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  758. if (import_mt != p_dir->files[i]->import_modified_time) {
  759. reimport = true;
  760. } else if (_test_for_reimport(path, true)) {
  761. reimport = true;
  762. }
  763. }
  764. if (reimport) {
  765. ItemAction ia;
  766. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  767. ia.dir = p_dir;
  768. ia.file = p_dir->files[i]->file;
  769. scan_actions.push_back(ia);
  770. }
  771. } else if (ResourceCache::has(path)) { //test for potential reload
  772. uint64_t mt = FileAccess::get_modified_time(path);
  773. if (mt != p_dir->files[i]->modified_time) {
  774. p_dir->files[i]->modified_time = mt; //save new time, but test for reload
  775. ItemAction ia;
  776. ia.action = ItemAction::ACTION_FILE_RELOAD;
  777. ia.dir = p_dir;
  778. ia.file = p_dir->files[i]->file;
  779. scan_actions.push_back(ia);
  780. }
  781. }
  782. }
  783. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  784. if (updated_dir && !p_dir->subdirs[i]->verified) {
  785. //this directory was removed, add action to remove it
  786. ItemAction ia;
  787. ia.action = ItemAction::ACTION_DIR_REMOVE;
  788. ia.dir = p_dir->subdirs[i];
  789. scan_actions.push_back(ia);
  790. continue;
  791. }
  792. _scan_fs_changes(p_dir->get_subdir(i), p_progress);
  793. }
  794. }
  795. void EditorFileSystem::_delete_internal_files(String p_file) {
  796. if (FileAccess::exists(p_file + ".import")) {
  797. List<String> paths;
  798. ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
  799. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  800. for (List<String>::Element *E = paths.front(); E; E = E->next()) {
  801. da->remove(E->get());
  802. }
  803. da->remove(p_file + ".import");
  804. memdelete(da);
  805. }
  806. }
  807. void EditorFileSystem::_thread_func_sources(void *_userdata) {
  808. EditorFileSystem *efs = (EditorFileSystem *)_userdata;
  809. if (efs->filesystem) {
  810. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  811. ScanProgress sp;
  812. sp.progress = &pr;
  813. sp.hi = 1;
  814. sp.low = 0;
  815. efs->_scan_fs_changes(efs->filesystem, sp);
  816. }
  817. efs->scanning_changes_done = true;
  818. }
  819. void EditorFileSystem::get_changed_sources(List<String> *r_changed) {
  820. *r_changed = sources_changed;
  821. }
  822. void EditorFileSystem::scan_changes() {
  823. if (first_scan || // Prevent a premature changes scan from inhibiting the first full scan
  824. scanning || scanning_changes || thread) {
  825. scan_changes_pending = true;
  826. set_process(true);
  827. return;
  828. }
  829. _update_extensions();
  830. sources_changed.clear();
  831. scanning_changes = true;
  832. scanning_changes_done = false;
  833. abort_scan = false;
  834. if (!use_threads) {
  835. if (filesystem) {
  836. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  837. ScanProgress sp;
  838. sp.progress = &pr;
  839. sp.hi = 1;
  840. sp.low = 0;
  841. scan_total = 0;
  842. _scan_fs_changes(filesystem, sp);
  843. if (_update_scan_actions())
  844. emit_signal("filesystem_changed");
  845. }
  846. scanning_changes = false;
  847. scanning_changes_done = true;
  848. emit_signal("sources_changed", sources_changed.size() > 0);
  849. } else {
  850. ERR_FAIL_COND(thread_sources);
  851. set_process(true);
  852. scan_total = 0;
  853. Thread::Settings s;
  854. s.priority = Thread::PRIORITY_LOW;
  855. thread_sources = Thread::create(_thread_func_sources, this, s);
  856. }
  857. }
  858. void EditorFileSystem::_notification(int p_what) {
  859. switch (p_what) {
  860. case NOTIFICATION_ENTER_TREE: {
  861. call_deferred("scan"); //this should happen after every editor node entered the tree
  862. } break;
  863. case NOTIFICATION_EXIT_TREE: {
  864. Thread *active_thread = thread ? thread : thread_sources;
  865. if (use_threads && active_thread) {
  866. //abort thread if in progress
  867. abort_scan = true;
  868. while (scanning) {
  869. OS::get_singleton()->delay_usec(1000);
  870. }
  871. Thread::wait_to_finish(active_thread);
  872. memdelete(active_thread);
  873. thread = nullptr;
  874. thread_sources = nullptr;
  875. WARN_PRINT("Scan thread aborted...");
  876. set_process(false);
  877. }
  878. if (filesystem)
  879. memdelete(filesystem);
  880. if (new_filesystem)
  881. memdelete(new_filesystem);
  882. filesystem = nullptr;
  883. new_filesystem = nullptr;
  884. } break;
  885. case NOTIFICATION_PROCESS: {
  886. if (use_threads) {
  887. if (scanning_changes) {
  888. if (scanning_changes_done) {
  889. scanning_changes = false;
  890. set_process(false);
  891. Thread::wait_to_finish(thread_sources);
  892. memdelete(thread_sources);
  893. thread_sources = nullptr;
  894. if (_update_scan_actions())
  895. emit_signal("filesystem_changed");
  896. emit_signal("sources_changed", sources_changed.size() > 0);
  897. _queue_update_script_classes();
  898. first_scan = false;
  899. }
  900. } else if (!scanning) {
  901. set_process(false);
  902. if (filesystem)
  903. memdelete(filesystem);
  904. filesystem = new_filesystem;
  905. new_filesystem = nullptr;
  906. Thread::wait_to_finish(thread);
  907. memdelete(thread);
  908. thread = nullptr;
  909. _update_scan_actions();
  910. emit_signal("filesystem_changed");
  911. emit_signal("sources_changed", sources_changed.size() > 0);
  912. _queue_update_script_classes();
  913. first_scan = false;
  914. }
  915. if (!is_processing() && scan_changes_pending) {
  916. scan_changes_pending = false;
  917. scan_changes();
  918. }
  919. }
  920. } break;
  921. }
  922. }
  923. bool EditorFileSystem::is_scanning() const {
  924. return scanning || scanning_changes;
  925. }
  926. float EditorFileSystem::get_scanning_progress() const {
  927. return scan_total;
  928. }
  929. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  930. return filesystem;
  931. }
  932. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file) {
  933. if (!p_dir)
  934. return; //none
  935. p_file->store_line("::" + p_dir->get_path() + "::" + String::num(p_dir->modified_time));
  936. for (int i = 0; i < p_dir->files.size(); i++) {
  937. if (p_dir->files[i]->import_group_file != String()) {
  938. group_file_cache.insert(p_dir->files[i]->import_group_file);
  939. }
  940. String s = p_dir->files[i]->file + "::" + p_dir->files[i]->type + "::" + 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;
  941. s += "::";
  942. for (int j = 0; j < p_dir->files[i]->deps.size(); j++) {
  943. if (j > 0)
  944. s += "<>";
  945. s += p_dir->files[i]->deps[j];
  946. }
  947. p_file->store_line(s);
  948. }
  949. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  950. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  951. }
  952. }
  953. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  954. //todo make faster
  955. if (!filesystem || scanning)
  956. return false;
  957. String f = ProjectSettings::get_singleton()->localize_path(p_file);
  958. if (!f.begins_with("res://"))
  959. return false;
  960. f = f.substr(6, f.length());
  961. f = f.replace("\\", "/");
  962. Vector<String> path = f.split("/");
  963. if (path.size() == 0)
  964. return false;
  965. String file = path[path.size() - 1];
  966. path.resize(path.size() - 1);
  967. EditorFileSystemDirectory *fs = filesystem;
  968. for (int i = 0; i < path.size(); i++) {
  969. if (path[i].begins_with("."))
  970. return false;
  971. int idx = -1;
  972. for (int j = 0; j < fs->get_subdir_count(); j++) {
  973. if (fs->get_subdir(j)->get_name() == path[i]) {
  974. idx = j;
  975. break;
  976. }
  977. }
  978. if (idx == -1) {
  979. //does not exist, create i guess?
  980. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  981. efsd->name = path[i];
  982. efsd->parent = fs;
  983. int idx2 = 0;
  984. for (int j = 0; j < fs->get_subdir_count(); j++) {
  985. if (efsd->name < fs->get_subdir(j)->get_name())
  986. break;
  987. idx2++;
  988. }
  989. if (idx2 == fs->get_subdir_count())
  990. fs->subdirs.push_back(efsd);
  991. else
  992. fs->subdirs.insert(idx2, efsd);
  993. fs = efsd;
  994. } else {
  995. fs = fs->get_subdir(idx);
  996. }
  997. }
  998. int cpos = -1;
  999. for (int i = 0; i < fs->files.size(); i++) {
  1000. if (fs->files[i]->file == file) {
  1001. cpos = i;
  1002. break;
  1003. }
  1004. }
  1005. r_file_pos = cpos;
  1006. *r_d = fs;
  1007. return cpos != -1;
  1008. }
  1009. String EditorFileSystem::get_file_type(const String &p_file) const {
  1010. EditorFileSystemDirectory *fs = nullptr;
  1011. int cpos = -1;
  1012. if (!_find_file(p_file, &fs, cpos)) {
  1013. return "";
  1014. }
  1015. return fs->files[cpos]->type;
  1016. }
  1017. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  1018. if (!filesystem || scanning)
  1019. return nullptr;
  1020. EditorFileSystemDirectory *fs = nullptr;
  1021. int cpos = -1;
  1022. if (!_find_file(p_file, &fs, cpos)) {
  1023. return nullptr;
  1024. }
  1025. if (r_index)
  1026. *r_index = cpos;
  1027. return fs;
  1028. }
  1029. EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
  1030. if (!filesystem || scanning)
  1031. return nullptr;
  1032. String f = ProjectSettings::get_singleton()->localize_path(p_path);
  1033. if (!f.begins_with("res://"))
  1034. return nullptr;
  1035. f = f.substr(6, f.length());
  1036. f = f.replace("\\", "/");
  1037. if (f == String())
  1038. return filesystem;
  1039. if (f.ends_with("/"))
  1040. f = f.substr(0, f.length() - 1);
  1041. Vector<String> path = f.split("/");
  1042. if (path.size() == 0)
  1043. return nullptr;
  1044. EditorFileSystemDirectory *fs = filesystem;
  1045. for (int i = 0; i < path.size(); i++) {
  1046. int idx = -1;
  1047. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1048. if (fs->get_subdir(j)->get_name() == path[i]) {
  1049. idx = j;
  1050. break;
  1051. }
  1052. }
  1053. if (idx == -1) {
  1054. return nullptr;
  1055. } else {
  1056. fs = fs->get_subdir(idx);
  1057. }
  1058. }
  1059. return fs;
  1060. }
  1061. void EditorFileSystem::_save_late_updated_files() {
  1062. //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
  1063. String fscache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file("filesystem_update4");
  1064. FileAccessRef f = FileAccess::open(fscache, FileAccess::WRITE);
  1065. ERR_FAIL_COND_MSG(!f, "Cannot create file '" + fscache + "'. Check user write permissions.");
  1066. for (Set<String>::Element *E = late_update_files.front(); E; E = E->next()) {
  1067. f->store_line(E->get());
  1068. }
  1069. }
  1070. Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
  1071. List<String> deps;
  1072. ResourceLoader::get_dependencies(p_path, &deps);
  1073. Vector<String> ret;
  1074. for (List<String>::Element *E = deps.front(); E; E = E->next()) {
  1075. ret.push_back(E->get());
  1076. }
  1077. return ret;
  1078. }
  1079. String EditorFileSystem::_get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const {
  1080. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1081. if (ScriptServer::get_language(i)->handles_global_class_type(p_type)) {
  1082. String global_name;
  1083. String extends;
  1084. String icon_path;
  1085. global_name = ScriptServer::get_language(i)->get_global_class_name(p_path, &extends, &icon_path);
  1086. *r_extends = extends;
  1087. *r_icon_path = icon_path;
  1088. return global_name;
  1089. }
  1090. }
  1091. *r_extends = String();
  1092. *r_icon_path = String();
  1093. return String();
  1094. }
  1095. void EditorFileSystem::_scan_script_classes(EditorFileSystemDirectory *p_dir) {
  1096. int filecount = p_dir->files.size();
  1097. const EditorFileSystemDirectory::FileInfo *const *files = p_dir->files.ptr();
  1098. for (int i = 0; i < filecount; i++) {
  1099. if (files[i]->script_class_name == String()) {
  1100. continue;
  1101. }
  1102. String lang;
  1103. for (int j = 0; j < ScriptServer::get_language_count(); j++) {
  1104. if (ScriptServer::get_language(j)->handles_global_class_type(files[i]->type)) {
  1105. lang = ScriptServer::get_language(j)->get_name();
  1106. }
  1107. }
  1108. ScriptServer::add_global_class(files[i]->script_class_name, files[i]->script_class_extends, lang, p_dir->get_file_path(i));
  1109. EditorNode::get_editor_data().script_class_set_icon_path(files[i]->script_class_name, files[i]->script_class_icon_path);
  1110. EditorNode::get_editor_data().script_class_set_name(files[i]->file, files[i]->script_class_name);
  1111. }
  1112. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1113. _scan_script_classes(p_dir->get_subdir(i));
  1114. }
  1115. }
  1116. void EditorFileSystem::update_script_classes() {
  1117. if (!update_script_classes_queued)
  1118. return;
  1119. update_script_classes_queued = false;
  1120. ScriptServer::global_classes_clear();
  1121. if (get_filesystem()) {
  1122. _scan_script_classes(get_filesystem());
  1123. }
  1124. ScriptServer::save_global_classes();
  1125. EditorNode::get_editor_data().script_class_save_icon_paths();
  1126. // Rescan custom loaders and savers.
  1127. // Doing the following here because the `filesystem_changed` signal fires multiple times and isn't always followed by script classes update.
  1128. // So I thought it's better to do this when script classes really get updated
  1129. ResourceLoader::remove_custom_loaders();
  1130. ResourceLoader::add_custom_loaders();
  1131. ResourceSaver::remove_custom_savers();
  1132. ResourceSaver::add_custom_savers();
  1133. }
  1134. void EditorFileSystem::_queue_update_script_classes() {
  1135. if (update_script_classes_queued) {
  1136. return;
  1137. }
  1138. update_script_classes_queued = true;
  1139. call_deferred("update_script_classes");
  1140. }
  1141. void EditorFileSystem::update_file(const String &p_file) {
  1142. EditorFileSystemDirectory *fs = nullptr;
  1143. int cpos = -1;
  1144. if (!_find_file(p_file, &fs, cpos)) {
  1145. if (!fs)
  1146. return;
  1147. }
  1148. if (!FileAccess::exists(p_file)) {
  1149. //was removed
  1150. _delete_internal_files(p_file);
  1151. if (cpos != -1) { // Might've never been part of the editor file system (*.* files deleted in Open dialog).
  1152. memdelete(fs->files[cpos]);
  1153. fs->files.remove(cpos);
  1154. }
  1155. call_deferred("emit_signal", "filesystem_changed"); //update later
  1156. _queue_update_script_classes();
  1157. return;
  1158. }
  1159. String type = ResourceLoader::get_resource_type(p_file);
  1160. if (cpos == -1) {
  1161. //the file did not exist, it was added
  1162. late_added_files.insert(p_file); //remember that it was added. This mean it will be scanned and imported on editor restart
  1163. int idx = 0;
  1164. for (int i = 0; i < fs->files.size(); i++) {
  1165. if (p_file < fs->files[i]->file)
  1166. break;
  1167. idx++;
  1168. }
  1169. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1170. fi->file = p_file.get_file();
  1171. fi->import_modified_time = 0;
  1172. fi->import_valid = ResourceLoader::is_import_valid(p_file);
  1173. if (idx == fs->files.size()) {
  1174. fs->files.push_back(fi);
  1175. } else {
  1176. fs->files.insert(idx, fi);
  1177. }
  1178. cpos = idx;
  1179. } else {
  1180. //the file exists and it was updated, and was not added in this step.
  1181. //this means we must force upon next restart to scan it again, to get proper type and dependencies
  1182. late_update_files.insert(p_file);
  1183. _save_late_updated_files(); //files need to be updated in the re-scan
  1184. }
  1185. fs->files[cpos]->type = type;
  1186. 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);
  1187. fs->files[cpos]->import_group_file = ResourceLoader::get_import_group_file(p_file);
  1188. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1189. fs->files[cpos]->deps = _get_dependencies(p_file);
  1190. fs->files[cpos]->import_valid = ResourceLoader::is_import_valid(p_file);
  1191. // Update preview
  1192. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1193. call_deferred("emit_signal", "filesystem_changed"); //update later
  1194. _queue_update_script_classes();
  1195. }
  1196. Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) {
  1197. String importer_name;
  1198. Map<String, Map<StringName, Variant>> source_file_options;
  1199. Map<String, String> base_paths;
  1200. for (int i = 0; i < p_files.size(); i++) {
  1201. Ref<ConfigFile> config;
  1202. config.instance();
  1203. Error err = config->load(p_files[i] + ".import");
  1204. ERR_CONTINUE(err != OK);
  1205. ERR_CONTINUE(!config->has_section_key("remap", "importer"));
  1206. String file_importer_name = config->get_value("remap", "importer");
  1207. ERR_CONTINUE(file_importer_name == String());
  1208. if (importer_name != String() && importer_name != file_importer_name) {
  1209. print_line("one importer '" + importer_name + "' the other '" + file_importer_name + "'.");
  1210. EditorNode::get_singleton()->show_warning(vformat(TTR("There are multiple importers for different types pointing to file %s, import aborted"), p_group_file));
  1211. ERR_FAIL_V(ERR_FILE_CORRUPT);
  1212. }
  1213. source_file_options[p_files[i]] = Map<StringName, Variant>();
  1214. importer_name = file_importer_name;
  1215. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1216. ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT);
  1217. List<ResourceImporter::ImportOption> options;
  1218. importer->get_import_options(&options);
  1219. //set default values
  1220. for (List<ResourceImporter::ImportOption>::Element *E = options.front(); E; E = E->next()) {
  1221. source_file_options[p_files[i]][E->get().option.name] = E->get().default_value;
  1222. }
  1223. if (config->has_section("params")) {
  1224. List<String> sk;
  1225. config->get_section_keys("params", &sk);
  1226. for (List<String>::Element *E = sk.front(); E; E = E->next()) {
  1227. String param = E->get();
  1228. Variant value = config->get_value("params", param);
  1229. //override with whathever is in file
  1230. source_file_options[p_files[i]][param] = value;
  1231. }
  1232. }
  1233. base_paths[p_files[i]] = ResourceFormatImporter::get_singleton()->get_import_base_path(p_files[i]);
  1234. }
  1235. ERR_FAIL_COND_V(importer_name == String(), ERR_UNCONFIGURED);
  1236. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1237. Error err = importer->import_group_file(p_group_file, source_file_options, base_paths);
  1238. //all went well, overwrite config files with proper remaps and md5s
  1239. for (Map<String, Map<StringName, Variant>>::Element *E = source_file_options.front(); E; E = E->next()) {
  1240. const String &file = E->key();
  1241. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(file);
  1242. FileAccessRef f = FileAccess::open(file + ".import", FileAccess::WRITE);
  1243. ERR_FAIL_COND_V_MSG(!f, ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'.");
  1244. //write manually, as order matters ([remap] has to go first for performance).
  1245. f->store_line("[remap]");
  1246. f->store_line("");
  1247. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  1248. if (importer->get_resource_type() != "") {
  1249. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  1250. }
  1251. Vector<String> dest_paths;
  1252. if (err == OK) {
  1253. String path = base_path + "." + importer->get_save_extension();
  1254. f->store_line("path=\"" + path + "\"");
  1255. dest_paths.push_back(path);
  1256. }
  1257. f->store_line("group_file=" + Variant(p_group_file).get_construct_string());
  1258. if (err == OK) {
  1259. f->store_line("valid=true");
  1260. } else {
  1261. f->store_line("valid=false");
  1262. }
  1263. f->store_line("[deps]\n");
  1264. f->store_line("");
  1265. f->store_line("source_file=" + Variant(file).get_construct_string());
  1266. if (dest_paths.size()) {
  1267. Array dp;
  1268. for (int i = 0; i < dest_paths.size(); i++) {
  1269. dp.push_back(dest_paths[i]);
  1270. }
  1271. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  1272. }
  1273. f->store_line("[params]");
  1274. f->store_line("");
  1275. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  1276. List<ResourceImporter::ImportOption> options;
  1277. importer->get_import_options(&options);
  1278. //set default values
  1279. for (List<ResourceImporter::ImportOption>::Element *F = options.front(); F; F = F->next()) {
  1280. String base = F->get().option.name;
  1281. Variant v = F->get().default_value;
  1282. if (source_file_options[file].has(base)) {
  1283. v = source_file_options[file][base];
  1284. }
  1285. String value;
  1286. VariantWriter::write_to_string(v, value);
  1287. f->store_line(base + "=" + value);
  1288. }
  1289. f->close();
  1290. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  1291. FileAccessRef md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  1292. ERR_FAIL_COND_V_MSG(!md5s, ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  1293. md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\"");
  1294. if (dest_paths.size()) {
  1295. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  1296. }
  1297. md5s->close();
  1298. EditorFileSystemDirectory *fs = nullptr;
  1299. int cpos = -1;
  1300. bool found = _find_file(file, &fs, cpos);
  1301. ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, "Can't find file '" + file + "'.");
  1302. //update modified times, to avoid reimport
  1303. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  1304. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(file + ".import");
  1305. fs->files[cpos]->deps = _get_dependencies(file);
  1306. fs->files[cpos]->type = importer->get_resource_type();
  1307. fs->files[cpos]->import_valid = err == OK;
  1308. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  1309. //to reload properly
  1310. if (ResourceCache::has(file)) {
  1311. Resource *r = ResourceCache::get(file);
  1312. if (r->get_import_path() != String()) {
  1313. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(file);
  1314. r->set_import_path(dst_path);
  1315. r->set_import_last_modified_time(0);
  1316. }
  1317. }
  1318. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  1319. }
  1320. return err;
  1321. }
  1322. void EditorFileSystem::_reimport_file(const String &p_file) {
  1323. EditorFileSystemDirectory *fs = nullptr;
  1324. int cpos = -1;
  1325. bool found = _find_file(p_file, &fs, cpos);
  1326. ERR_FAIL_COND_MSG(!found, "Can't find file '" + p_file + "'.");
  1327. //try to obtain existing params
  1328. Map<StringName, Variant> params;
  1329. String importer_name;
  1330. if (FileAccess::exists(p_file + ".import")) {
  1331. //use existing
  1332. Ref<ConfigFile> cf;
  1333. cf.instance();
  1334. Error err = cf->load(p_file + ".import");
  1335. if (err == OK) {
  1336. if (cf->has_section("params")) {
  1337. List<String> sk;
  1338. cf->get_section_keys("params", &sk);
  1339. for (List<String>::Element *E = sk.front(); E; E = E->next()) {
  1340. params[E->get()] = cf->get_value("params", E->get());
  1341. }
  1342. }
  1343. if (cf->has_section("remap")) {
  1344. importer_name = cf->get_value("remap", "importer");
  1345. }
  1346. }
  1347. } else {
  1348. late_added_files.insert(p_file); //imported files do not call update_file(), but just in case..
  1349. }
  1350. Ref<ResourceImporter> importer;
  1351. bool load_default = false;
  1352. //find the importer
  1353. if (importer_name != "") {
  1354. importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1355. }
  1356. if (importer.is_null()) {
  1357. //not found by name, find by extension
  1358. importer = ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_file.get_extension());
  1359. load_default = true;
  1360. if (importer.is_null()) {
  1361. ERR_PRINT("BUG: File queued for import, but can't be imported!");
  1362. ERR_FAIL();
  1363. }
  1364. }
  1365. //mix with default params, in case a parameter is missing
  1366. List<ResourceImporter::ImportOption> opts;
  1367. importer->get_import_options(&opts);
  1368. for (List<ResourceImporter::ImportOption>::Element *E = opts.front(); E; E = E->next()) {
  1369. if (!params.has(E->get().option.name)) { //this one is not present
  1370. params[E->get().option.name] = E->get().default_value;
  1371. }
  1372. }
  1373. if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  1374. //use defaults if exist
  1375. Dictionary d = ProjectSettings::get_singleton()->get("importer_defaults/" + importer->get_importer_name());
  1376. List<Variant> v;
  1377. d.get_key_list(&v);
  1378. for (List<Variant>::Element *E = v.front(); E; E = E->next()) {
  1379. params[E->get()] = d[E->get()];
  1380. }
  1381. }
  1382. //finally, perform import!!
  1383. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
  1384. List<String> import_variants;
  1385. List<String> gen_files;
  1386. Variant metadata;
  1387. Error err = importer->import(p_file, base_path, params, &import_variants, &gen_files, &metadata);
  1388. if (err != OK) {
  1389. ERR_PRINT("Error importing '" + p_file + "'.");
  1390. }
  1391. //as import is complete, save the .import file
  1392. FileAccess *f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
  1393. ERR_FAIL_COND_MSG(!f, "Cannot open file from path '" + p_file + ".import'.");
  1394. //write manually, as order matters ([remap] has to go first for performance).
  1395. f->store_line("[remap]");
  1396. f->store_line("");
  1397. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  1398. if (importer->get_resource_type() != "") {
  1399. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  1400. }
  1401. Vector<String> dest_paths;
  1402. if (err == OK) {
  1403. if (importer->get_save_extension() == "") {
  1404. //no path
  1405. } else if (import_variants.size()) {
  1406. //import with variants
  1407. for (List<String>::Element *E = import_variants.front(); E; E = E->next()) {
  1408. String path = base_path.c_escape() + "." + E->get() + "." + importer->get_save_extension();
  1409. f->store_line("path." + E->get() + "=\"" + path + "\"");
  1410. dest_paths.push_back(path);
  1411. }
  1412. } else {
  1413. String path = base_path + "." + importer->get_save_extension();
  1414. f->store_line("path=\"" + path + "\"");
  1415. dest_paths.push_back(path);
  1416. }
  1417. } else {
  1418. f->store_line("valid=false");
  1419. }
  1420. if (metadata != Variant()) {
  1421. f->store_line("metadata=" + metadata.get_construct_string());
  1422. }
  1423. f->store_line("");
  1424. f->store_line("[deps]\n");
  1425. if (gen_files.size()) {
  1426. Array genf;
  1427. for (List<String>::Element *E = gen_files.front(); E; E = E->next()) {
  1428. genf.push_back(E->get());
  1429. dest_paths.push_back(E->get());
  1430. }
  1431. String value;
  1432. VariantWriter::write_to_string(genf, value);
  1433. f->store_line("files=" + value);
  1434. f->store_line("");
  1435. }
  1436. f->store_line("source_file=" + Variant(p_file).get_construct_string());
  1437. if (dest_paths.size()) {
  1438. Array dp;
  1439. for (int i = 0; i < dest_paths.size(); i++) {
  1440. dp.push_back(dest_paths[i]);
  1441. }
  1442. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  1443. }
  1444. f->store_line("[params]");
  1445. f->store_line("");
  1446. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  1447. for (List<ResourceImporter::ImportOption>::Element *E = opts.front(); E; E = E->next()) {
  1448. String base = E->get().option.name;
  1449. String value;
  1450. VariantWriter::write_to_string(params[base], value);
  1451. f->store_line(base + "=" + value);
  1452. }
  1453. f->close();
  1454. memdelete(f);
  1455. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  1456. FileAccess *md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  1457. ERR_FAIL_COND_MSG(!md5s, "Cannot open MD5 file '" + base_path + ".md5'.");
  1458. md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"");
  1459. if (dest_paths.size()) {
  1460. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  1461. }
  1462. md5s->close();
  1463. memdelete(md5s);
  1464. //update modified times, to avoid reimport
  1465. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1466. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  1467. fs->files[cpos]->deps = _get_dependencies(p_file);
  1468. fs->files[cpos]->type = importer->get_resource_type();
  1469. fs->files[cpos]->import_valid = ResourceLoader::is_import_valid(p_file);
  1470. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  1471. //to reload properly
  1472. if (ResourceCache::has(p_file)) {
  1473. Resource *r = ResourceCache::get(p_file);
  1474. if (r->get_import_path() != String()) {
  1475. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
  1476. r->set_import_path(dst_path);
  1477. r->set_import_last_modified_time(0);
  1478. }
  1479. }
  1480. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1481. }
  1482. void EditorFileSystem::_find_group_files(EditorFileSystemDirectory *efd, Map<String, Vector<String>> &group_files, Set<String> &groups_to_reimport) {
  1483. int fc = efd->files.size();
  1484. const EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptr();
  1485. for (int i = 0; i < fc; i++) {
  1486. if (groups_to_reimport.has(files[i]->import_group_file)) {
  1487. if (!group_files.has(files[i]->import_group_file)) {
  1488. group_files[files[i]->import_group_file] = Vector<String>();
  1489. }
  1490. group_files[files[i]->import_group_file].push_back(efd->get_file_path(i));
  1491. }
  1492. }
  1493. for (int i = 0; i < efd->get_subdir_count(); i++) {
  1494. _find_group_files(efd->get_subdir(i), group_files, groups_to_reimport);
  1495. }
  1496. }
  1497. void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
  1498. { //check that .import folder exists
  1499. DirAccess *da = DirAccess::open("res://");
  1500. if (da->change_dir(".import") != OK) {
  1501. Error err = da->make_dir(".import");
  1502. if (err) {
  1503. memdelete(da);
  1504. ERR_FAIL_MSG("Failed to create 'res://.import' folder.");
  1505. }
  1506. }
  1507. memdelete(da);
  1508. }
  1509. importing = true;
  1510. EditorProgress pr("reimport", TTR("(Re)Importing Assets"), p_files.size());
  1511. Vector<ImportFile> files;
  1512. Set<String> groups_to_reimport;
  1513. for (int i = 0; i < p_files.size(); i++) {
  1514. String group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(p_files[i]);
  1515. if (group_file_cache.has(p_files[i])) {
  1516. //maybe the file itself is a group!
  1517. groups_to_reimport.insert(p_files[i]);
  1518. //groups do not belong to grups
  1519. group_file = String();
  1520. } else if (group_file != String()) {
  1521. //it's a group file, add group to import and skip this file
  1522. groups_to_reimport.insert(group_file);
  1523. } else {
  1524. //it's a regular file
  1525. ImportFile ifile;
  1526. ifile.path = p_files[i];
  1527. ifile.order = ResourceFormatImporter::get_singleton()->get_import_order(p_files[i]);
  1528. files.push_back(ifile);
  1529. }
  1530. //group may have changed, so also update group reference
  1531. EditorFileSystemDirectory *fs = nullptr;
  1532. int cpos = -1;
  1533. if (_find_file(p_files[i], &fs, cpos)) {
  1534. fs->files.write[cpos]->import_group_file = group_file;
  1535. }
  1536. }
  1537. files.sort();
  1538. for (int i = 0; i < files.size(); i++) {
  1539. pr.step(files[i].path.get_file(), i);
  1540. _reimport_file(files[i].path);
  1541. }
  1542. //reimport groups
  1543. if (groups_to_reimport.size()) {
  1544. Map<String, Vector<String>> group_files;
  1545. _find_group_files(filesystem, group_files, groups_to_reimport);
  1546. for (Map<String, Vector<String>>::Element *E = group_files.front(); E; E = E->next()) {
  1547. Error err = _reimport_group(E->key(), E->get());
  1548. if (err == OK) {
  1549. _reimport_file(E->key());
  1550. }
  1551. }
  1552. }
  1553. _save_filesystem_cache();
  1554. importing = false;
  1555. if (!is_scanning()) {
  1556. emit_signal("filesystem_changed");
  1557. }
  1558. emit_signal("resources_reimported", p_files);
  1559. }
  1560. Error EditorFileSystem::_resource_import(const String &p_path) {
  1561. Vector<String> files;
  1562. files.push_back(p_path);
  1563. singleton->update_file(p_path);
  1564. singleton->reimport_files(files);
  1565. return OK;
  1566. }
  1567. bool EditorFileSystem::is_group_file(const String &p_path) const {
  1568. return group_file_cache.has(p_path);
  1569. }
  1570. void EditorFileSystem::_move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location) {
  1571. int fc = efd->files.size();
  1572. EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptrw();
  1573. for (int i = 0; i < fc; i++) {
  1574. if (files[i]->import_group_file == p_group_file) {
  1575. files[i]->import_group_file = p_new_location;
  1576. Ref<ConfigFile> config;
  1577. config.instance();
  1578. String path = efd->get_file_path(i) + ".import";
  1579. Error err = config->load(path);
  1580. if (err != OK) {
  1581. continue;
  1582. }
  1583. if (config->has_section_key("remap", "group_file")) {
  1584. config->set_value("remap", "group_file", p_new_location);
  1585. }
  1586. List<String> sk;
  1587. config->get_section_keys("params", &sk);
  1588. for (List<String>::Element *E = sk.front(); E; E = E->next()) {
  1589. //not very clean, but should work
  1590. String param = E->get();
  1591. String value = config->get_value("params", param);
  1592. if (value == p_group_file) {
  1593. config->set_value("params", param, p_new_location);
  1594. }
  1595. }
  1596. config->save(path);
  1597. }
  1598. }
  1599. for (int i = 0; i < efd->get_subdir_count(); i++) {
  1600. _move_group_files(efd->get_subdir(i), p_group_file, p_new_location);
  1601. }
  1602. }
  1603. void EditorFileSystem::move_group_file(const String &p_path, const String &p_new_path) {
  1604. if (get_filesystem()) {
  1605. _move_group_files(get_filesystem(), p_path, p_new_path);
  1606. if (group_file_cache.has(p_path)) {
  1607. group_file_cache.erase(p_path);
  1608. group_file_cache.insert(p_new_path);
  1609. }
  1610. }
  1611. }
  1612. void EditorFileSystem::_bind_methods() {
  1613. ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
  1614. ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
  1615. ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
  1616. ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
  1617. ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
  1618. ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
  1619. ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
  1620. ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
  1621. ClassDB::bind_method(D_METHOD("update_script_classes"), &EditorFileSystem::update_script_classes);
  1622. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  1623. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  1624. ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  1625. ADD_SIGNAL(MethodInfo("resources_reload", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  1626. }
  1627. void EditorFileSystem::_update_extensions() {
  1628. valid_extensions.clear();
  1629. import_extensions.clear();
  1630. List<String> extensionsl;
  1631. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  1632. for (List<String>::Element *E = extensionsl.front(); E; E = E->next()) {
  1633. valid_extensions.insert(E->get());
  1634. }
  1635. extensionsl.clear();
  1636. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  1637. for (List<String>::Element *E = extensionsl.front(); E; E = E->next()) {
  1638. import_extensions.insert(E->get());
  1639. }
  1640. }
  1641. EditorFileSystem::EditorFileSystem() {
  1642. ResourceLoader::import = _resource_import;
  1643. reimport_on_missing_imported_files = GLOBAL_DEF("editor/reimport_missing_imported_files", true);
  1644. singleton = this;
  1645. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  1646. filesystem->parent = nullptr;
  1647. thread = nullptr;
  1648. scanning = false;
  1649. importing = false;
  1650. use_threads = true;
  1651. thread_sources = nullptr;
  1652. new_filesystem = nullptr;
  1653. abort_scan = false;
  1654. scanning_changes = false;
  1655. scanning_changes_done = false;
  1656. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1657. if (da->change_dir("res://.import") != OK) {
  1658. da->make_dir("res://.import");
  1659. }
  1660. // This should probably also work on Unix and use the string it returns for FAT32 or exFAT
  1661. using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "exFAT");
  1662. memdelete(da);
  1663. scan_total = 0;
  1664. update_script_classes_queued = false;
  1665. first_scan = true;
  1666. scan_changes_pending = false;
  1667. revalidate_import_files = false;
  1668. }
  1669. EditorFileSystem::~EditorFileSystem() {
  1670. }