editor_file_system.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  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_cache5"
  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 + "/" + 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 + "/" + 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 dont 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() != 7);
  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.script_class_name = split[5].get_slice("<>", 0);
  198. fc.script_class_extends = split[5].get_slice("<>", 1);
  199. fc.script_class_icon_path = split[5].get_slice("<>", 2);
  200. String deps = split[6].strip_edges();
  201. if (deps.length()) {
  202. Vector<String> dp = deps.split("<>");
  203. for (int i = 0; i < dp.size(); i++) {
  204. String path = dp[i];
  205. fc.deps.push_back(path);
  206. }
  207. }
  208. file_cache[name] = fc;
  209. }
  210. }
  211. f->close();
  212. memdelete(f);
  213. }
  214. String update_cache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file("filesystem_update4");
  215. if (FileAccess::exists(update_cache)) {
  216. {
  217. FileAccessRef f2 = FileAccess::open(update_cache, FileAccess::READ);
  218. String l = f2->get_line().strip_edges();
  219. while (l != String()) {
  220. file_cache.erase(l); //erase cache for this, so it gets updated
  221. l = f2->get_line().strip_edges();
  222. }
  223. }
  224. DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  225. d->remove(update_cache); //bye bye update cache
  226. }
  227. EditorProgressBG scan_progress("efs", "ScanFS", 1000);
  228. ScanProgress sp;
  229. sp.low = 0;
  230. sp.hi = 1;
  231. sp.progress = &scan_progress;
  232. new_filesystem = memnew(EditorFileSystemDirectory);
  233. new_filesystem->parent = NULL;
  234. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  235. d->change_dir("res://");
  236. _scan_new_dir(new_filesystem, d, sp);
  237. file_cache.clear(); //clear caches, no longer needed
  238. memdelete(d);
  239. if (!first_scan) {
  240. //on the first scan this is done from the main thread after re-importing
  241. _save_filesystem_cache();
  242. }
  243. scanning = false;
  244. }
  245. void EditorFileSystem::_save_filesystem_cache() {
  246. String fscache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(CACHE_FILE_NAME);
  247. FileAccess *f = FileAccess::open(fscache, FileAccess::WRITE);
  248. if (f == NULL) {
  249. ERR_EXPLAIN("Cannot create file '" + fscache + "'. Check user write permissions.");
  250. ERR_FAIL();
  251. }
  252. f->store_line(filesystem_settings_version_for_import);
  253. _save_filesystem_cache(filesystem, f);
  254. f->close();
  255. memdelete(f);
  256. }
  257. void EditorFileSystem::_thread_func(void *_userdata) {
  258. EditorFileSystem *sd = (EditorFileSystem *)_userdata;
  259. sd->_scan_filesystem();
  260. }
  261. bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_imported_files) {
  262. if (!reimport_on_missing_imported_files && p_only_imported_files)
  263. return false;
  264. if (!FileAccess::exists(p_path + ".import")) {
  265. return true;
  266. }
  267. if (!ResourceFormatImporter::get_singleton()->are_import_settings_valid(p_path)) {
  268. //reimport settings are not valid, reimport
  269. return true;
  270. }
  271. Error err;
  272. FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  273. if (!f) { //no import file, do reimport
  274. return true;
  275. }
  276. VariantParser::StreamFile stream;
  277. stream.f = f;
  278. String assign;
  279. Variant value;
  280. VariantParser::Tag next_tag;
  281. int lines = 0;
  282. String error_text;
  283. List<String> to_check;
  284. String source_file = "";
  285. String source_md5 = "";
  286. Vector<String> dest_files;
  287. String dest_md5 = "";
  288. while (true) {
  289. assign = Variant();
  290. next_tag.fields.clear();
  291. next_tag.name = String();
  292. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
  293. if (err == ERR_FILE_EOF) {
  294. break;
  295. } else if (err != OK) {
  296. ERR_PRINTS("ResourceFormatImporter::load - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
  297. memdelete(f);
  298. return false; //parse error, try reimport manually (Avoid reimport loop on broken file)
  299. }
  300. if (assign != String()) {
  301. if (assign.begins_with("path")) {
  302. to_check.push_back(value);
  303. } else if (assign == "files") {
  304. Array fa = value;
  305. for (int i = 0; i < fa.size(); i++) {
  306. to_check.push_back(fa[i]);
  307. }
  308. } else if (!p_only_imported_files) {
  309. if (assign == "source_file") {
  310. source_file = value;
  311. } else if (assign == "dest_files") {
  312. dest_files = value;
  313. }
  314. }
  315. } else if (next_tag.name != "remap" && next_tag.name != "deps") {
  316. break;
  317. }
  318. }
  319. memdelete(f);
  320. // Read the md5's from a separate file (so the import parameters aren't dependent on the file version
  321. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path);
  322. FileAccess *md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err);
  323. if (!md5s) { // No md5's stored for this resource
  324. return true;
  325. }
  326. VariantParser::StreamFile md5_stream;
  327. md5_stream.f = md5s;
  328. while (true) {
  329. assign = Variant();
  330. next_tag.fields.clear();
  331. next_tag.name = String();
  332. err = VariantParser::parse_tag_assign_eof(&md5_stream, lines, error_text, next_tag, assign, value, NULL, true);
  333. if (err == ERR_FILE_EOF) {
  334. break;
  335. } else if (err != OK) {
  336. ERR_PRINTS("ResourceFormatImporter::load - " + p_path + ".import.md5:" + itos(lines) + " error: " + error_text);
  337. memdelete(md5s);
  338. return false; // parse error
  339. }
  340. if (assign != String()) {
  341. if (!p_only_imported_files) {
  342. if (assign == "source_md5") {
  343. source_md5 = value;
  344. } else if (assign == "dest_md5") {
  345. dest_md5 = value;
  346. }
  347. }
  348. }
  349. }
  350. memdelete(md5s);
  351. //imported files are gone, reimport
  352. for (List<String>::Element *E = to_check.front(); E; E = E->next()) {
  353. if (!FileAccess::exists(E->get())) {
  354. return true;
  355. }
  356. }
  357. //check source md5 matching
  358. if (!p_only_imported_files) {
  359. if (source_file != String() && source_file != p_path) {
  360. return true; //file was moved, reimport
  361. }
  362. if (source_md5 == String()) {
  363. return true; //lacks md5, so just reimport
  364. }
  365. String md5 = FileAccess::get_md5(p_path);
  366. if (md5 != source_md5) {
  367. return true;
  368. }
  369. if (dest_files.size() && dest_md5 != String()) {
  370. md5 = FileAccess::get_multiple_md5(dest_files);
  371. if (md5 != dest_md5) {
  372. return true;
  373. }
  374. }
  375. }
  376. return false; //nothing changed
  377. }
  378. bool EditorFileSystem::_update_scan_actions() {
  379. sources_changed.clear();
  380. bool fs_changed = false;
  381. Vector<String> reimports;
  382. Vector<String> reloads;
  383. for (List<ItemAction>::Element *E = scan_actions.front(); E; E = E->next()) {
  384. ItemAction &ia = E->get();
  385. switch (ia.action) {
  386. case ItemAction::ACTION_NONE: {
  387. } break;
  388. case ItemAction::ACTION_DIR_ADD: {
  389. int idx = 0;
  390. for (int i = 0; i < ia.dir->subdirs.size(); i++) {
  391. if (ia.new_dir->name < ia.dir->subdirs[i]->name)
  392. break;
  393. idx++;
  394. }
  395. if (idx == ia.dir->subdirs.size()) {
  396. ia.dir->subdirs.push_back(ia.new_dir);
  397. } else {
  398. ia.dir->subdirs.insert(idx, ia.new_dir);
  399. }
  400. fs_changed = true;
  401. } break;
  402. case ItemAction::ACTION_DIR_REMOVE: {
  403. ERR_CONTINUE(!ia.dir->parent);
  404. ia.dir->parent->subdirs.erase(ia.dir);
  405. memdelete(ia.dir);
  406. fs_changed = true;
  407. } break;
  408. case ItemAction::ACTION_FILE_ADD: {
  409. int idx = 0;
  410. for (int i = 0; i < ia.dir->files.size(); i++) {
  411. if (ia.new_file->file < ia.dir->files[i]->file)
  412. break;
  413. idx++;
  414. }
  415. if (idx == ia.dir->files.size()) {
  416. ia.dir->files.push_back(ia.new_file);
  417. } else {
  418. ia.dir->files.insert(idx, ia.new_file);
  419. }
  420. fs_changed = true;
  421. } break;
  422. case ItemAction::ACTION_FILE_REMOVE: {
  423. int idx = ia.dir->find_file_index(ia.file);
  424. ERR_CONTINUE(idx == -1);
  425. _delete_internal_files(ia.dir->files[idx]->file);
  426. memdelete(ia.dir->files[idx]);
  427. ia.dir->files.remove(idx);
  428. fs_changed = true;
  429. } break;
  430. case ItemAction::ACTION_FILE_TEST_REIMPORT: {
  431. int idx = ia.dir->find_file_index(ia.file);
  432. ERR_CONTINUE(idx == -1);
  433. String full_path = ia.dir->get_file_path(idx);
  434. if (_test_for_reimport(full_path, false)) {
  435. //must reimport
  436. reimports.push_back(full_path);
  437. } else {
  438. //must not reimport, all was good
  439. //update modified times, to avoid reimport
  440. ia.dir->files[idx]->modified_time = FileAccess::get_modified_time(full_path);
  441. ia.dir->files[idx]->import_modified_time = FileAccess::get_modified_time(full_path + ".import");
  442. }
  443. fs_changed = true;
  444. } break;
  445. case ItemAction::ACTION_FILE_RELOAD: {
  446. int idx = ia.dir->find_file_index(ia.file);
  447. ERR_CONTINUE(idx == -1);
  448. String full_path = ia.dir->get_file_path(idx);
  449. reloads.push_back(full_path);
  450. } break;
  451. }
  452. }
  453. if (reimports.size()) {
  454. reimport_files(reimports);
  455. }
  456. if (first_scan) {
  457. //only on first scan this is valid and updated, then settings changed.
  458. revalidate_import_files = false;
  459. filesystem_settings_version_for_import = ResourceFormatImporter::get_singleton()->get_import_settings_hash();
  460. _save_filesystem_cache();
  461. }
  462. if (reloads.size()) {
  463. emit_signal("resources_reload", reloads);
  464. }
  465. scan_actions.clear();
  466. return fs_changed;
  467. }
  468. void EditorFileSystem::scan() {
  469. if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/)
  470. return;
  471. if (scanning || scanning_changes || thread)
  472. return;
  473. _update_extensions();
  474. abort_scan = false;
  475. if (!use_threads) {
  476. scanning = true;
  477. scan_total = 0;
  478. _scan_filesystem();
  479. if (filesystem)
  480. memdelete(filesystem);
  481. //file_type_cache.clear();
  482. filesystem = new_filesystem;
  483. new_filesystem = NULL;
  484. _update_scan_actions();
  485. scanning = false;
  486. emit_signal("filesystem_changed");
  487. emit_signal("sources_changed", sources_changed.size() > 0);
  488. _queue_update_script_classes();
  489. first_scan = false;
  490. } else {
  491. ERR_FAIL_COND(thread);
  492. set_process(true);
  493. Thread::Settings s;
  494. scanning = true;
  495. scan_total = 0;
  496. s.priority = Thread::PRIORITY_LOW;
  497. thread = Thread::create(_thread_func, this, s);
  498. //tree->hide();
  499. //progress->show();
  500. }
  501. }
  502. void EditorFileSystem::ScanProgress::update(int p_current, int p_total) const {
  503. float ratio = low + ((hi - low) / p_total) * p_current;
  504. progress->step(ratio * 1000);
  505. EditorFileSystem::singleton->scan_total = ratio;
  506. }
  507. EditorFileSystem::ScanProgress EditorFileSystem::ScanProgress::get_sub(int p_current, int p_total) const {
  508. ScanProgress sp = *this;
  509. float slice = (sp.hi - sp.low) / p_total;
  510. sp.low += slice * p_current;
  511. sp.hi = slice;
  512. return sp;
  513. }
  514. void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess *da, const ScanProgress &p_progress) {
  515. List<String> dirs;
  516. List<String> files;
  517. String cd = da->get_current_dir();
  518. p_dir->modified_time = FileAccess::get_modified_time(cd);
  519. da->list_dir_begin();
  520. while (true) {
  521. bool isdir;
  522. String f = da->get_next(&isdir);
  523. if (f == "")
  524. break;
  525. if (isdir) {
  526. if (f.begins_with(".")) //ignore hidden and . / ..
  527. continue;
  528. if (FileAccess::exists(cd.plus_file(f).plus_file("project.godot"))) // skip if another project inside this
  529. continue;
  530. if (FileAccess::exists(cd.plus_file(f).plus_file(".gdignore"))) // skip if another project inside this
  531. continue;
  532. dirs.push_back(f);
  533. } else {
  534. files.push_back(f);
  535. }
  536. }
  537. da->list_dir_end();
  538. dirs.sort_custom<NaturalNoCaseComparator>();
  539. files.sort_custom<NaturalNoCaseComparator>();
  540. int total = dirs.size() + files.size();
  541. int idx = 0;
  542. for (List<String>::Element *E = dirs.front(); E; E = E->next(), idx++) {
  543. if (da->change_dir(E->get()) == OK) {
  544. String d = da->get_current_dir();
  545. if (d == cd || !d.begins_with(cd)) {
  546. da->change_dir(cd); //avoid recursion
  547. } else {
  548. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  549. efd->parent = p_dir;
  550. efd->name = E->get();
  551. _scan_new_dir(efd, da, p_progress.get_sub(idx, total));
  552. int idx2 = 0;
  553. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  554. if (efd->name < p_dir->subdirs[i]->name)
  555. break;
  556. idx2++;
  557. }
  558. if (idx2 == p_dir->subdirs.size()) {
  559. p_dir->subdirs.push_back(efd);
  560. } else {
  561. p_dir->subdirs.insert(idx2, efd);
  562. }
  563. da->change_dir("..");
  564. }
  565. } else {
  566. ERR_PRINTS("Cannot go into subdir: " + E->get());
  567. }
  568. p_progress.update(idx, total);
  569. }
  570. for (List<String>::Element *E = files.front(); E; E = E->next(), idx++) {
  571. String ext = E->get().get_extension().to_lower();
  572. if (!valid_extensions.has(ext)) {
  573. continue; //invalid
  574. }
  575. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  576. fi->file = E->get();
  577. String path = cd.plus_file(fi->file);
  578. FileCache *fc = file_cache.getptr(path);
  579. uint64_t mt = FileAccess::get_modified_time(path);
  580. if (import_extensions.has(ext)) {
  581. //is imported
  582. uint64_t import_mt = 0;
  583. if (FileAccess::exists(path + ".import")) {
  584. import_mt = FileAccess::get_modified_time(path + ".import");
  585. }
  586. if (fc && fc->modification_time == mt && fc->import_modification_time == import_mt && !_test_for_reimport(path, true)) {
  587. fi->type = fc->type;
  588. fi->deps = fc->deps;
  589. fi->modified_time = fc->modification_time;
  590. fi->import_modified_time = fc->import_modification_time;
  591. fi->import_valid = fc->import_valid;
  592. fi->script_class_name = fc->script_class_name;
  593. fi->script_class_extends = fc->script_class_extends;
  594. fi->script_class_icon_path = fc->script_class_icon_path;
  595. if (revalidate_import_files && !ResourceFormatImporter::get_singleton()->are_import_settings_valid(path)) {
  596. ItemAction ia;
  597. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  598. ia.dir = p_dir;
  599. ia.file = E->get();
  600. scan_actions.push_back(ia);
  601. }
  602. if (fc->type == String()) {
  603. fi->type = ResourceLoader::get_resource_type(path);
  604. //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?)
  605. //note: I think this should not happen any longer..
  606. }
  607. } else {
  608. fi->type = ResourceFormatImporter::get_singleton()->get_resource_type(path);
  609. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  610. fi->modified_time = 0;
  611. fi->import_modified_time = 0;
  612. fi->import_valid = ResourceLoader::is_import_valid(path);
  613. ItemAction ia;
  614. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  615. ia.dir = p_dir;
  616. ia.file = E->get();
  617. scan_actions.push_back(ia);
  618. }
  619. } else {
  620. if (fc && fc->modification_time == mt) {
  621. //not imported, so just update type if changed
  622. fi->type = fc->type;
  623. fi->modified_time = fc->modification_time;
  624. fi->deps = fc->deps;
  625. fi->import_modified_time = 0;
  626. fi->import_valid = true;
  627. fi->script_class_name = fc->script_class_name;
  628. fi->script_class_extends = fc->script_class_extends;
  629. fi->script_class_icon_path = fc->script_class_icon_path;
  630. } else {
  631. //new or modified time
  632. fi->type = ResourceLoader::get_resource_type(path);
  633. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  634. fi->deps = _get_dependencies(path);
  635. fi->modified_time = mt;
  636. fi->import_modified_time = 0;
  637. fi->import_valid = true;
  638. }
  639. }
  640. p_dir->files.push_back(fi);
  641. p_progress.update(idx, total);
  642. }
  643. }
  644. void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress) {
  645. uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
  646. bool updated_dir = false;
  647. String cd = p_dir->get_path();
  648. if (current_mtime != p_dir->modified_time || using_fat32_or_exfat) {
  649. updated_dir = true;
  650. p_dir->modified_time = current_mtime;
  651. //ooooops, dir changed, see what's going on
  652. //first mark everything as veryfied
  653. for (int i = 0; i < p_dir->files.size(); i++) {
  654. p_dir->files[i]->verified = false;
  655. }
  656. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  657. p_dir->get_subdir(i)->verified = false;
  658. }
  659. //then scan files and directories and check what's different
  660. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  661. da->change_dir(cd);
  662. da->list_dir_begin();
  663. while (true) {
  664. bool isdir;
  665. String f = da->get_next(&isdir);
  666. if (f == "")
  667. break;
  668. if (isdir) {
  669. if (f.begins_with(".")) //ignore hidden and . / ..
  670. continue;
  671. int idx = p_dir->find_dir_index(f);
  672. if (idx == -1) {
  673. if (FileAccess::exists(cd.plus_file(f).plus_file("project.godot"))) // skip if another project inside this
  674. continue;
  675. if (FileAccess::exists(cd.plus_file(f).plus_file(".gdignore"))) // skip if another project inside this
  676. continue;
  677. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  678. efd->parent = p_dir;
  679. efd->name = f;
  680. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  681. d->change_dir(cd.plus_file(f));
  682. _scan_new_dir(efd, d, p_progress.get_sub(1, 1));
  683. memdelete(d);
  684. ItemAction ia;
  685. ia.action = ItemAction::ACTION_DIR_ADD;
  686. ia.dir = p_dir;
  687. ia.file = f;
  688. ia.new_dir = efd;
  689. scan_actions.push_back(ia);
  690. } else {
  691. p_dir->subdirs[idx]->verified = true;
  692. }
  693. } else {
  694. String ext = f.get_extension().to_lower();
  695. if (!valid_extensions.has(ext))
  696. continue; //invalid
  697. int idx = p_dir->find_file_index(f);
  698. if (idx == -1) {
  699. //never seen this file, add actition to add it
  700. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  701. fi->file = f;
  702. String path = cd.plus_file(fi->file);
  703. fi->modified_time = FileAccess::get_modified_time(path);
  704. fi->import_modified_time = 0;
  705. fi->type = ResourceLoader::get_resource_type(path);
  706. fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
  707. fi->import_valid = ResourceLoader::is_import_valid(path);
  708. {
  709. ItemAction ia;
  710. ia.action = ItemAction::ACTION_FILE_ADD;
  711. ia.dir = p_dir;
  712. ia.file = f;
  713. ia.new_file = fi;
  714. scan_actions.push_back(ia);
  715. }
  716. if (import_extensions.has(ext)) {
  717. //if it can be imported, and it was added, it needs to be reimported
  718. ItemAction ia;
  719. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  720. ia.dir = p_dir;
  721. ia.file = f;
  722. scan_actions.push_back(ia);
  723. }
  724. } else {
  725. p_dir->files[idx]->verified = true;
  726. }
  727. }
  728. }
  729. da->list_dir_end();
  730. memdelete(da);
  731. }
  732. for (int i = 0; i < p_dir->files.size(); i++) {
  733. if (updated_dir && !p_dir->files[i]->verified) {
  734. //this file was removed, add action to remove it
  735. ItemAction ia;
  736. ia.action = ItemAction::ACTION_FILE_REMOVE;
  737. ia.dir = p_dir;
  738. ia.file = p_dir->files[i]->file;
  739. scan_actions.push_back(ia);
  740. continue;
  741. }
  742. String path = cd.plus_file(p_dir->files[i]->file);
  743. if (import_extensions.has(p_dir->files[i]->file.get_extension().to_lower())) {
  744. //check here if file must be imported or not
  745. uint64_t mt = FileAccess::get_modified_time(path);
  746. bool reimport = false;
  747. if (mt != p_dir->files[i]->modified_time) {
  748. reimport = true; //it was modified, must be reimported.
  749. } else if (!FileAccess::exists(path + ".import")) {
  750. reimport = true; //no .import file, obviously reimport
  751. } else {
  752. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  753. if (import_mt != p_dir->files[i]->import_modified_time) {
  754. reimport = true;
  755. } else if (_test_for_reimport(path, true)) {
  756. reimport = true;
  757. }
  758. }
  759. if (reimport) {
  760. ItemAction ia;
  761. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  762. ia.dir = p_dir;
  763. ia.file = p_dir->files[i]->file;
  764. scan_actions.push_back(ia);
  765. }
  766. } else if (ResourceCache::has(path)) { //test for potential reload
  767. uint64_t mt = FileAccess::get_modified_time(path);
  768. if (mt != p_dir->files[i]->modified_time) {
  769. p_dir->files[i]->modified_time = mt; //save new time, but test for reload
  770. ItemAction ia;
  771. ia.action = ItemAction::ACTION_FILE_RELOAD;
  772. ia.dir = p_dir;
  773. ia.file = p_dir->files[i]->file;
  774. scan_actions.push_back(ia);
  775. }
  776. }
  777. }
  778. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  779. if (updated_dir && !p_dir->subdirs[i]->verified) {
  780. //this directory was removed, add action to remove it
  781. ItemAction ia;
  782. ia.action = ItemAction::ACTION_DIR_REMOVE;
  783. ia.dir = p_dir->subdirs[i];
  784. scan_actions.push_back(ia);
  785. continue;
  786. }
  787. _scan_fs_changes(p_dir->get_subdir(i), p_progress);
  788. }
  789. }
  790. void EditorFileSystem::_delete_internal_files(String p_file) {
  791. if (FileAccess::exists(p_file + ".import")) {
  792. List<String> paths;
  793. ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
  794. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  795. for (List<String>::Element *E = paths.front(); E; E = E->next()) {
  796. da->remove(E->get());
  797. }
  798. da->remove(p_file + ".import");
  799. memdelete(da);
  800. }
  801. }
  802. void EditorFileSystem::_thread_func_sources(void *_userdata) {
  803. EditorFileSystem *efs = (EditorFileSystem *)_userdata;
  804. if (efs->filesystem) {
  805. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  806. ScanProgress sp;
  807. sp.progress = &pr;
  808. sp.hi = 1;
  809. sp.low = 0;
  810. efs->_scan_fs_changes(efs->filesystem, sp);
  811. }
  812. efs->scanning_changes_done = true;
  813. }
  814. void EditorFileSystem::get_changed_sources(List<String> *r_changed) {
  815. *r_changed = sources_changed;
  816. }
  817. void EditorFileSystem::scan_changes() {
  818. if (scanning || scanning_changes || thread)
  819. return;
  820. _update_extensions();
  821. sources_changed.clear();
  822. scanning_changes = true;
  823. scanning_changes_done = false;
  824. abort_scan = false;
  825. if (!use_threads) {
  826. if (filesystem) {
  827. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  828. ScanProgress sp;
  829. sp.progress = &pr;
  830. sp.hi = 1;
  831. sp.low = 0;
  832. scan_total = 0;
  833. _scan_fs_changes(filesystem, sp);
  834. if (_update_scan_actions())
  835. emit_signal("filesystem_changed");
  836. }
  837. scanning_changes = false;
  838. scanning_changes_done = true;
  839. emit_signal("sources_changed", sources_changed.size() > 0);
  840. } else {
  841. ERR_FAIL_COND(thread_sources);
  842. set_process(true);
  843. scan_total = 0;
  844. Thread::Settings s;
  845. s.priority = Thread::PRIORITY_LOW;
  846. thread_sources = Thread::create(_thread_func_sources, this, s);
  847. }
  848. }
  849. void EditorFileSystem::_notification(int p_what) {
  850. switch (p_what) {
  851. case NOTIFICATION_ENTER_TREE: {
  852. call_deferred("scan"); //this should happen after every editor node entered the tree
  853. } break;
  854. case NOTIFICATION_EXIT_TREE: {
  855. if (use_threads && thread) {
  856. //abort thread if in progress
  857. abort_scan = true;
  858. while (scanning) {
  859. OS::get_singleton()->delay_usec(1000);
  860. }
  861. Thread::wait_to_finish(thread);
  862. memdelete(thread);
  863. thread = NULL;
  864. WARN_PRINTS("Scan thread aborted...");
  865. set_process(false);
  866. }
  867. if (filesystem)
  868. memdelete(filesystem);
  869. if (new_filesystem)
  870. memdelete(new_filesystem);
  871. filesystem = NULL;
  872. new_filesystem = NULL;
  873. } break;
  874. case NOTIFICATION_PROCESS: {
  875. if (use_threads) {
  876. if (scanning_changes) {
  877. if (scanning_changes_done) {
  878. scanning_changes = false;
  879. set_process(false);
  880. Thread::wait_to_finish(thread_sources);
  881. memdelete(thread_sources);
  882. thread_sources = NULL;
  883. if (_update_scan_actions())
  884. emit_signal("filesystem_changed");
  885. emit_signal("sources_changed", sources_changed.size() > 0);
  886. _queue_update_script_classes();
  887. first_scan = false;
  888. }
  889. } else if (!scanning) {
  890. set_process(false);
  891. if (filesystem)
  892. memdelete(filesystem);
  893. filesystem = new_filesystem;
  894. new_filesystem = NULL;
  895. Thread::wait_to_finish(thread);
  896. memdelete(thread);
  897. thread = NULL;
  898. _update_scan_actions();
  899. emit_signal("filesystem_changed");
  900. emit_signal("sources_changed", sources_changed.size() > 0);
  901. _queue_update_script_classes();
  902. first_scan = false;
  903. }
  904. }
  905. } break;
  906. }
  907. }
  908. bool EditorFileSystem::is_scanning() const {
  909. return scanning || scanning_changes;
  910. }
  911. float EditorFileSystem::get_scanning_progress() const {
  912. return scan_total;
  913. }
  914. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  915. return filesystem;
  916. }
  917. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file) {
  918. if (!p_dir)
  919. return; //none
  920. p_file->store_line("::" + p_dir->get_path() + "::" + String::num(p_dir->modified_time));
  921. for (int i = 0; i < p_dir->files.size(); i++) {
  922. 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]->script_class_name + "<>" + p_dir->files[i]->script_class_extends + "<>" + p_dir->files[i]->script_class_icon_path;
  923. s += "::";
  924. for (int j = 0; j < p_dir->files[i]->deps.size(); j++) {
  925. if (j > 0)
  926. s += "<>";
  927. s += p_dir->files[i]->deps[j];
  928. }
  929. p_file->store_line(s);
  930. }
  931. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  932. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  933. }
  934. }
  935. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  936. //todo make faster
  937. if (!filesystem || scanning)
  938. return false;
  939. String f = ProjectSettings::get_singleton()->localize_path(p_file);
  940. if (!f.begins_with("res://"))
  941. return false;
  942. f = f.substr(6, f.length());
  943. f = f.replace("\\", "/");
  944. Vector<String> path = f.split("/");
  945. if (path.size() == 0)
  946. return false;
  947. String file = path[path.size() - 1];
  948. path.resize(path.size() - 1);
  949. EditorFileSystemDirectory *fs = filesystem;
  950. for (int i = 0; i < path.size(); i++) {
  951. if (path[i].begins_with("."))
  952. return false;
  953. int idx = -1;
  954. for (int j = 0; j < fs->get_subdir_count(); j++) {
  955. if (fs->get_subdir(j)->get_name() == path[i]) {
  956. idx = j;
  957. break;
  958. }
  959. }
  960. if (idx == -1) {
  961. //does not exist, create i guess?
  962. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  963. efsd->name = path[i];
  964. efsd->parent = fs;
  965. int idx2 = 0;
  966. for (int j = 0; j < fs->get_subdir_count(); j++) {
  967. if (efsd->name < fs->get_subdir(j)->get_name())
  968. break;
  969. idx2++;
  970. }
  971. if (idx2 == fs->get_subdir_count())
  972. fs->subdirs.push_back(efsd);
  973. else
  974. fs->subdirs.insert(idx2, efsd);
  975. fs = efsd;
  976. } else {
  977. fs = fs->get_subdir(idx);
  978. }
  979. }
  980. int cpos = -1;
  981. for (int i = 0; i < fs->files.size(); i++) {
  982. if (fs->files[i]->file == file) {
  983. cpos = i;
  984. break;
  985. }
  986. }
  987. r_file_pos = cpos;
  988. *r_d = fs;
  989. if (cpos != -1) {
  990. return true;
  991. } else {
  992. return false;
  993. }
  994. }
  995. String EditorFileSystem::get_file_type(const String &p_file) const {
  996. EditorFileSystemDirectory *fs = NULL;
  997. int cpos = -1;
  998. if (!_find_file(p_file, &fs, cpos)) {
  999. return "";
  1000. }
  1001. return fs->files[cpos]->type;
  1002. }
  1003. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  1004. if (!filesystem || scanning)
  1005. return NULL;
  1006. EditorFileSystemDirectory *fs = NULL;
  1007. int cpos = -1;
  1008. if (!_find_file(p_file, &fs, cpos)) {
  1009. return NULL;
  1010. }
  1011. if (r_index)
  1012. *r_index = cpos;
  1013. return fs;
  1014. }
  1015. EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
  1016. if (!filesystem || scanning)
  1017. return NULL;
  1018. String f = ProjectSettings::get_singleton()->localize_path(p_path);
  1019. if (!f.begins_with("res://"))
  1020. return NULL;
  1021. f = f.substr(6, f.length());
  1022. f = f.replace("\\", "/");
  1023. if (f == String())
  1024. return filesystem;
  1025. if (f.ends_with("/"))
  1026. f = f.substr(0, f.length() - 1);
  1027. Vector<String> path = f.split("/");
  1028. if (path.size() == 0)
  1029. return NULL;
  1030. EditorFileSystemDirectory *fs = filesystem;
  1031. for (int i = 0; i < path.size(); i++) {
  1032. int idx = -1;
  1033. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1034. if (fs->get_subdir(j)->get_name() == path[i]) {
  1035. idx = j;
  1036. break;
  1037. }
  1038. }
  1039. if (idx == -1) {
  1040. return NULL;
  1041. } else {
  1042. fs = fs->get_subdir(idx);
  1043. }
  1044. }
  1045. return fs;
  1046. }
  1047. void EditorFileSystem::_save_late_updated_files() {
  1048. //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
  1049. String fscache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file("filesystem_update4");
  1050. FileAccessRef f = FileAccess::open(fscache, FileAccess::WRITE);
  1051. if (!f) {
  1052. ERR_EXPLAIN("Cannot create file '" + fscache + "'. Check user write permissions.");
  1053. ERR_FAIL();
  1054. }
  1055. for (Set<String>::Element *E = late_update_files.front(); E; E = E->next()) {
  1056. f->store_line(E->get());
  1057. }
  1058. }
  1059. Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
  1060. List<String> deps;
  1061. ResourceLoader::get_dependencies(p_path, &deps);
  1062. Vector<String> ret;
  1063. for (List<String>::Element *E = deps.front(); E; E = E->next()) {
  1064. ret.push_back(E->get());
  1065. }
  1066. return ret;
  1067. }
  1068. String EditorFileSystem::_get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const {
  1069. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1070. if (ScriptServer::get_language(i)->handles_global_class_type(p_type)) {
  1071. String global_name;
  1072. String extends;
  1073. String icon_path;
  1074. global_name = ScriptServer::get_language(i)->get_global_class_name(p_path, &extends, &icon_path);
  1075. *r_extends = extends;
  1076. *r_icon_path = icon_path;
  1077. return global_name;
  1078. }
  1079. }
  1080. *r_extends = String();
  1081. *r_icon_path = String();
  1082. return String();
  1083. }
  1084. void EditorFileSystem::_scan_script_classes(EditorFileSystemDirectory *p_dir) {
  1085. int filecount = p_dir->files.size();
  1086. const EditorFileSystemDirectory::FileInfo *const *files = p_dir->files.ptr();
  1087. for (int i = 0; i < filecount; i++) {
  1088. if (files[i]->script_class_name == String()) {
  1089. continue;
  1090. }
  1091. String lang;
  1092. for (int j = 0; j < ScriptServer::get_language_count(); j++) {
  1093. if (ScriptServer::get_language(j)->handles_global_class_type(files[i]->type)) {
  1094. lang = ScriptServer::get_language(j)->get_name();
  1095. }
  1096. }
  1097. ScriptServer::add_global_class(files[i]->script_class_name, files[i]->script_class_extends, lang, p_dir->get_file_path(i));
  1098. EditorNode::get_editor_data().script_class_set_icon_path(files[i]->script_class_name, files[i]->script_class_icon_path);
  1099. EditorNode::get_editor_data().script_class_set_name(files[i]->file, files[i]->script_class_name);
  1100. }
  1101. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1102. _scan_script_classes(p_dir->get_subdir(i));
  1103. }
  1104. }
  1105. void EditorFileSystem::update_script_classes() {
  1106. if (!update_script_classes_queued)
  1107. return;
  1108. update_script_classes_queued = false;
  1109. ScriptServer::global_classes_clear();
  1110. if (get_filesystem()) {
  1111. _scan_script_classes(get_filesystem());
  1112. }
  1113. ScriptServer::save_global_classes();
  1114. EditorNode::get_editor_data().script_class_save_icon_paths();
  1115. // Rescan custom loaders and savers.
  1116. // Doing the following here because the `filesystem_changed` signal fires multiple times and isn't always followed by script classes update.
  1117. // So I thought it's better to do this when script classes really get updated
  1118. ResourceLoader::remove_custom_loaders();
  1119. ResourceLoader::add_custom_loaders();
  1120. ResourceSaver::remove_custom_savers();
  1121. ResourceSaver::add_custom_savers();
  1122. }
  1123. void EditorFileSystem::_queue_update_script_classes() {
  1124. if (update_script_classes_queued) {
  1125. return;
  1126. }
  1127. update_script_classes_queued = true;
  1128. call_deferred("update_script_classes");
  1129. }
  1130. void EditorFileSystem::update_file(const String &p_file) {
  1131. EditorFileSystemDirectory *fs = NULL;
  1132. int cpos = -1;
  1133. if (!_find_file(p_file, &fs, cpos)) {
  1134. if (!fs)
  1135. return;
  1136. }
  1137. if (!FileAccess::exists(p_file)) {
  1138. //was removed
  1139. _delete_internal_files(p_file);
  1140. if (cpos != -1) { // Might've never been part of the editor file system (*.* files deleted in Open dialog).
  1141. memdelete(fs->files[cpos]);
  1142. fs->files.remove(cpos);
  1143. }
  1144. call_deferred("emit_signal", "filesystem_changed"); //update later
  1145. _queue_update_script_classes();
  1146. return;
  1147. }
  1148. String type = ResourceLoader::get_resource_type(p_file);
  1149. if (cpos == -1) {
  1150. //the file did not exist, it was added
  1151. late_added_files.insert(p_file); //remember that it was added. This mean it will be scanned and imported on editor restart
  1152. int idx = 0;
  1153. for (int i = 0; i < fs->files.size(); i++) {
  1154. if (p_file < fs->files[i]->file)
  1155. break;
  1156. idx++;
  1157. }
  1158. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1159. fi->file = p_file.get_file();
  1160. fi->import_modified_time = 0;
  1161. fi->import_valid = ResourceLoader::is_import_valid(p_file);
  1162. if (idx == fs->files.size()) {
  1163. fs->files.push_back(fi);
  1164. } else {
  1165. fs->files.insert(idx, fi);
  1166. }
  1167. cpos = idx;
  1168. } else {
  1169. //the file exists and it was updated, and was not added in this step.
  1170. //this means we must force upon next restart to scan it again, to get proper type and dependencies
  1171. late_update_files.insert(p_file);
  1172. _save_late_updated_files(); //files need to be updated in the re-scan
  1173. }
  1174. fs->files[cpos]->type = type;
  1175. 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);
  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. void EditorFileSystem::_reimport_file(const String &p_file) {
  1185. EditorFileSystemDirectory *fs = NULL;
  1186. int cpos = -1;
  1187. bool found = _find_file(p_file, &fs, cpos);
  1188. ERR_FAIL_COND(!found);
  1189. //try to obtain existing params
  1190. Map<StringName, Variant> params;
  1191. String importer_name;
  1192. if (FileAccess::exists(p_file + ".import")) {
  1193. //use existing
  1194. Ref<ConfigFile> cf;
  1195. cf.instance();
  1196. Error err = cf->load(p_file + ".import");
  1197. if (err == OK) {
  1198. if (cf->has_section("params")) {
  1199. List<String> sk;
  1200. cf->get_section_keys("params", &sk);
  1201. for (List<String>::Element *E = sk.front(); E; E = E->next()) {
  1202. params[E->get()] = cf->get_value("params", E->get());
  1203. }
  1204. }
  1205. if (cf->has_section("remap")) {
  1206. importer_name = cf->get_value("remap", "importer");
  1207. }
  1208. }
  1209. } else {
  1210. late_added_files.insert(p_file); //imported files do not call update_file(), but just in case..
  1211. }
  1212. Ref<ResourceImporter> importer;
  1213. bool load_default = false;
  1214. //find the importer
  1215. if (importer_name != "") {
  1216. importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1217. }
  1218. if (importer.is_null()) {
  1219. //not found by name, find by extension
  1220. importer = ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_file.get_extension());
  1221. load_default = true;
  1222. if (importer.is_null()) {
  1223. ERR_PRINT("BUG: File queued for import, but can't be imported!");
  1224. ERR_FAIL();
  1225. }
  1226. }
  1227. //mix with default params, in case a parameter is missing
  1228. List<ResourceImporter::ImportOption> opts;
  1229. importer->get_import_options(&opts);
  1230. for (List<ResourceImporter::ImportOption>::Element *E = opts.front(); E; E = E->next()) {
  1231. if (!params.has(E->get().option.name)) { //this one is not present
  1232. params[E->get().option.name] = E->get().default_value;
  1233. }
  1234. }
  1235. if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  1236. //use defaults if exist
  1237. Dictionary d = ProjectSettings::get_singleton()->get("importer_defaults/" + importer->get_importer_name());
  1238. List<Variant> v;
  1239. d.get_key_list(&v);
  1240. for (List<Variant>::Element *E = v.front(); E; E = E->next()) {
  1241. params[E->get()] = d[E->get()];
  1242. }
  1243. }
  1244. //finally, perform import!!
  1245. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
  1246. List<String> import_variants;
  1247. List<String> gen_files;
  1248. Variant metadata;
  1249. Error err = importer->import(p_file, base_path, params, &import_variants, &gen_files, &metadata);
  1250. if (err != OK) {
  1251. ERR_PRINTS("Error importing: " + p_file);
  1252. }
  1253. //as import is complete, save the .import file
  1254. FileAccess *f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
  1255. ERR_FAIL_COND(!f);
  1256. //write manually, as order matters ([remap] has to go first for performance).
  1257. f->store_line("[remap]");
  1258. f->store_line("");
  1259. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  1260. if (importer->get_resource_type() != "") {
  1261. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  1262. }
  1263. Vector<String> dest_paths;
  1264. if (err == OK) {
  1265. if (importer->get_save_extension() == "") {
  1266. //no path
  1267. } else if (import_variants.size()) {
  1268. //import with variants
  1269. for (List<String>::Element *E = import_variants.front(); E; E = E->next()) {
  1270. String path = base_path.c_escape() + "." + E->get() + "." + importer->get_save_extension();
  1271. f->store_line("path." + E->get() + "=\"" + path + "\"");
  1272. dest_paths.push_back(path);
  1273. }
  1274. } else {
  1275. String path = base_path + "." + importer->get_save_extension();
  1276. f->store_line("path=\"" + path + "\"");
  1277. dest_paths.push_back(path);
  1278. }
  1279. } else {
  1280. f->store_line("valid=false");
  1281. }
  1282. if (metadata != Variant()) {
  1283. f->store_line("metadata=" + metadata.get_construct_string());
  1284. }
  1285. f->store_line("");
  1286. f->store_line("[deps]\n");
  1287. if (gen_files.size()) {
  1288. Array genf;
  1289. for (List<String>::Element *E = gen_files.front(); E; E = E->next()) {
  1290. genf.push_back(E->get());
  1291. dest_paths.push_back(E->get());
  1292. }
  1293. String value;
  1294. VariantWriter::write_to_string(genf, value);
  1295. f->store_line("files=" + value);
  1296. f->store_line("");
  1297. }
  1298. f->store_line("source_file=" + Variant(p_file).get_construct_string());
  1299. if (dest_paths.size()) {
  1300. Array dp;
  1301. for (int i = 0; i < dest_paths.size(); i++) {
  1302. dp.push_back(dest_paths[i]);
  1303. }
  1304. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  1305. }
  1306. f->store_line("[params]");
  1307. f->store_line("");
  1308. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  1309. for (List<ResourceImporter::ImportOption>::Element *E = opts.front(); E; E = E->next()) {
  1310. String base = E->get().option.name;
  1311. String value;
  1312. VariantWriter::write_to_string(params[base], value);
  1313. f->store_line(base + "=" + value);
  1314. }
  1315. f->close();
  1316. memdelete(f);
  1317. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  1318. FileAccess *md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  1319. ERR_FAIL_COND(!md5s);
  1320. md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"");
  1321. if (dest_paths.size()) {
  1322. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  1323. }
  1324. md5s->close();
  1325. memdelete(md5s);
  1326. //update modified times, to avoid reimport
  1327. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1328. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  1329. fs->files[cpos]->deps = _get_dependencies(p_file);
  1330. fs->files[cpos]->type = importer->get_resource_type();
  1331. fs->files[cpos]->import_valid = ResourceLoader::is_import_valid(p_file);
  1332. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  1333. //to reload properly
  1334. if (ResourceCache::has(p_file)) {
  1335. Resource *r = ResourceCache::get(p_file);
  1336. if (r->get_import_path() != String()) {
  1337. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
  1338. r->set_import_path(dst_path);
  1339. r->set_import_last_modified_time(0);
  1340. }
  1341. }
  1342. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1343. }
  1344. void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
  1345. { //check that .import folder exists
  1346. DirAccess *da = DirAccess::open("res://");
  1347. if (da->change_dir(".import") != OK) {
  1348. Error err = da->make_dir(".import");
  1349. if (err) {
  1350. memdelete(da);
  1351. ERR_EXPLAIN("Failed to create 'res://.import' folder.");
  1352. ERR_FAIL_COND(err != OK);
  1353. }
  1354. }
  1355. memdelete(da);
  1356. }
  1357. importing = true;
  1358. EditorProgress pr("reimport", TTR("(Re)Importing Assets"), p_files.size());
  1359. Vector<ImportFile> files;
  1360. for (int i = 0; i < p_files.size(); i++) {
  1361. ImportFile ifile;
  1362. ifile.path = p_files[i];
  1363. ifile.order = ResourceFormatImporter::get_singleton()->get_import_order(p_files[i]);
  1364. files.push_back(ifile);
  1365. }
  1366. files.sort();
  1367. for (int i = 0; i < files.size(); i++) {
  1368. pr.step(files[i].path.get_file(), i);
  1369. _reimport_file(files[i].path);
  1370. }
  1371. _save_filesystem_cache();
  1372. importing = false;
  1373. if (!is_scanning()) {
  1374. emit_signal("filesystem_changed");
  1375. }
  1376. emit_signal("resources_reimported", p_files);
  1377. }
  1378. Error EditorFileSystem::_resource_import(const String &p_path) {
  1379. Vector<String> files;
  1380. files.push_back(p_path);
  1381. singleton->update_file(p_path);
  1382. singleton->reimport_files(files);
  1383. return OK;
  1384. }
  1385. void EditorFileSystem::_bind_methods() {
  1386. ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
  1387. ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
  1388. ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
  1389. ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
  1390. ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
  1391. ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
  1392. ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
  1393. ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
  1394. ClassDB::bind_method(D_METHOD("update_script_classes"), &EditorFileSystem::update_script_classes);
  1395. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  1396. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  1397. ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::POOL_STRING_ARRAY, "resources")));
  1398. ADD_SIGNAL(MethodInfo("resources_reload", PropertyInfo(Variant::POOL_STRING_ARRAY, "resources")));
  1399. }
  1400. void EditorFileSystem::_update_extensions() {
  1401. valid_extensions.clear();
  1402. import_extensions.clear();
  1403. List<String> extensionsl;
  1404. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  1405. for (List<String>::Element *E = extensionsl.front(); E; E = E->next()) {
  1406. valid_extensions.insert(E->get());
  1407. }
  1408. extensionsl.clear();
  1409. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  1410. for (List<String>::Element *E = extensionsl.front(); E; E = E->next()) {
  1411. import_extensions.insert(E->get());
  1412. }
  1413. }
  1414. EditorFileSystem::EditorFileSystem() {
  1415. ResourceLoader::import = _resource_import;
  1416. reimport_on_missing_imported_files = GLOBAL_DEF("editor/reimport_missing_imported_files", true);
  1417. singleton = this;
  1418. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  1419. filesystem->parent = NULL;
  1420. thread = NULL;
  1421. scanning = false;
  1422. importing = false;
  1423. use_threads = true;
  1424. thread_sources = NULL;
  1425. new_filesystem = NULL;
  1426. abort_scan = false;
  1427. scanning_changes = false;
  1428. scanning_changes_done = false;
  1429. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1430. if (da->change_dir("res://.import") != OK) {
  1431. da->make_dir("res://.import");
  1432. }
  1433. // This should probably also work on Unix and use the string it returns for FAT32 or exFAT
  1434. using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "exFAT");
  1435. memdelete(da);
  1436. scan_total = 0;
  1437. update_script_classes_queued = false;
  1438. first_scan = true;
  1439. revalidate_import_files = false;
  1440. }
  1441. EditorFileSystem::~EditorFileSystem() {
  1442. }