editor_file_system.cpp 63 KB

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