editor_file_system.cpp 65 KB

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