2
0

editor_file_system.cpp 62 KB

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