editor_file_system.cpp 61 KB

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