editor_file_system.cpp 64 KB

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