2
0

editor_file_system.cpp 63 KB

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