editor_file_system.cpp 63 KB

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