editor_file_system.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "editor_node.h"
  32. #include "editor_resource_preview.h"
  33. #include "editor_settings.h"
  34. #include "globals.h"
  35. #include "io/resource_loader.h"
  36. #include "io/resource_saver.h"
  37. #include "os/file_access.h"
  38. #include "os/os.h"
  39. EditorFileSystem *EditorFileSystem::singleton = NULL;
  40. void EditorFileSystemDirectory::sort_files() {
  41. files.sort_custom<FileInfoSort>();
  42. }
  43. int EditorFileSystemDirectory::find_file_index(const String &p_file) const {
  44. for (int i = 0; i < files.size(); i++) {
  45. if (files[i]->file == p_file)
  46. return i;
  47. }
  48. return -1;
  49. }
  50. int EditorFileSystemDirectory::find_dir_index(const String &p_dir) const {
  51. for (int i = 0; i < subdirs.size(); i++) {
  52. if (subdirs[i]->name == p_dir)
  53. return i;
  54. }
  55. return -1;
  56. }
  57. int EditorFileSystemDirectory::get_subdir_count() const {
  58. return subdirs.size();
  59. }
  60. EditorFileSystemDirectory *EditorFileSystemDirectory::get_subdir(int p_idx) {
  61. ERR_FAIL_INDEX_V(p_idx, subdirs.size(), NULL);
  62. return subdirs[p_idx];
  63. }
  64. int EditorFileSystemDirectory::get_file_count() const {
  65. return files.size();
  66. }
  67. String EditorFileSystemDirectory::get_file(int p_idx) const {
  68. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  69. return files[p_idx]->file;
  70. }
  71. String EditorFileSystemDirectory::get_path() const {
  72. String p;
  73. const EditorFileSystemDirectory *d = this;
  74. while (d->parent) {
  75. p = d->name + "/" + p;
  76. d = d->parent;
  77. }
  78. return "res://" + p;
  79. }
  80. String EditorFileSystemDirectory::get_file_path(int p_idx) const {
  81. String file = get_file(p_idx);
  82. const EditorFileSystemDirectory *d = this;
  83. while (d->parent) {
  84. file = d->name + "/" + file;
  85. d = d->parent;
  86. }
  87. return "res://" + file;
  88. }
  89. bool EditorFileSystemDirectory::get_file_meta(int p_idx) const {
  90. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  91. return files[p_idx]->meta.enabled;
  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]->meta.deps;
  96. }
  97. Vector<String> EditorFileSystemDirectory::get_missing_sources(int p_idx) const {
  98. ERR_FAIL_INDEX_V(p_idx, files.size(), Vector<String>());
  99. Vector<String> missing;
  100. for (int i = 0; i < files[p_idx]->meta.sources.size(); i++) {
  101. if (files[p_idx]->meta.sources[i].missing)
  102. missing.push_back(files[p_idx]->meta.sources[i].path);
  103. }
  104. return missing;
  105. }
  106. bool EditorFileSystemDirectory::is_missing_sources(int p_idx) const {
  107. ERR_FAIL_INDEX_V(p_idx, files.size(), false);
  108. for (int i = 0; i < files[p_idx]->meta.sources.size(); i++) {
  109. if (files[p_idx]->meta.sources[i].missing)
  110. return true;
  111. }
  112. return false;
  113. }
  114. bool EditorFileSystemDirectory::have_sources_changed(int p_idx) const {
  115. ERR_FAIL_INDEX_V(p_idx, files.size(), false);
  116. return files[p_idx]->meta.sources_changed;
  117. }
  118. int EditorFileSystemDirectory::get_source_count(int p_idx) const {
  119. ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
  120. if (!files[p_idx]->meta.enabled)
  121. return 0;
  122. return files[p_idx]->meta.sources.size();
  123. }
  124. String EditorFileSystemDirectory::get_source_file(int p_idx, int p_source) const {
  125. ERR_FAIL_INDEX_V(p_idx, files.size(), String());
  126. ERR_FAIL_INDEX_V(p_source, files[p_idx]->meta.sources.size(), String());
  127. if (!files[p_idx]->meta.enabled)
  128. return String();
  129. return files[p_idx]->meta.sources[p_source].path;
  130. }
  131. bool EditorFileSystemDirectory::is_source_file_missing(int p_idx, int p_source) const {
  132. ERR_FAIL_INDEX_V(p_idx, files.size(), false);
  133. ERR_FAIL_INDEX_V(p_source, files[p_idx]->meta.sources.size(), false);
  134. if (!files[p_idx]->meta.enabled)
  135. return false;
  136. return files[p_idx]->meta.sources[p_source].missing;
  137. }
  138. StringName EditorFileSystemDirectory::get_file_type(int p_idx) const {
  139. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  140. return files[p_idx]->type;
  141. }
  142. String EditorFileSystemDirectory::get_name() {
  143. return name;
  144. }
  145. EditorFileSystemDirectory *EditorFileSystemDirectory::get_parent() {
  146. return parent;
  147. }
  148. void EditorFileSystemDirectory::_bind_methods() {
  149. ObjectTypeDB::bind_method(_MD("get_subdir_count"), &EditorFileSystemDirectory::get_subdir_count);
  150. ObjectTypeDB::bind_method(_MD("get_subdir", "idx"), &EditorFileSystemDirectory::get_subdir);
  151. ObjectTypeDB::bind_method(_MD("get_file_count"), &EditorFileSystemDirectory::get_file_count);
  152. ObjectTypeDB::bind_method(_MD("get_file", "idx"), &EditorFileSystemDirectory::get_file);
  153. ObjectTypeDB::bind_method(_MD("get_file_path", "idx"), &EditorFileSystemDirectory::get_file_path);
  154. ObjectTypeDB::bind_method(_MD("get_file_types", "idx"), &EditorFileSystemDirectory::get_file_type);
  155. ObjectTypeDB::bind_method(_MD("is_missing_sources", "idx"), &EditorFileSystemDirectory::is_missing_sources);
  156. ObjectTypeDB::bind_method(_MD("get_name"), &EditorFileSystemDirectory::get_name);
  157. ObjectTypeDB::bind_method(_MD("get_parent"), &EditorFileSystemDirectory::get_parent);
  158. }
  159. EditorFileSystemDirectory::EditorFileSystemDirectory() {
  160. modified_time = 0;
  161. parent = NULL;
  162. verified = false;
  163. }
  164. EditorFileSystemDirectory::~EditorFileSystemDirectory() {
  165. for (int i = 0; i < files.size(); i++) {
  166. memdelete(files[i]);
  167. }
  168. for (int i = 0; i < subdirs.size(); i++) {
  169. memdelete(subdirs[i]);
  170. }
  171. }
  172. EditorFileSystemDirectory::ImportMeta EditorFileSystem::_get_meta(const String &p_path) {
  173. Ref<ResourceImportMetadata> imd = ResourceLoader::load_import_metadata(p_path);
  174. EditorFileSystemDirectory::ImportMeta m;
  175. if (imd.is_null()) {
  176. m.enabled = false;
  177. m.sources_changed = false;
  178. } else {
  179. m.enabled = true;
  180. m.sources_changed = false;
  181. for (int i = 0; i < imd->get_source_count(); i++) {
  182. EditorFileSystemDirectory::ImportMeta::Source s;
  183. s.path = imd->get_source_path(i);
  184. s.md5 = imd->get_source_md5(i);
  185. s.modified_time = 0;
  186. s.missing = false;
  187. m.sources.push_back(s);
  188. }
  189. m.import_editor = imd->get_editor();
  190. }
  191. List<String> deps;
  192. ResourceLoader::get_dependencies(p_path, &deps);
  193. for (List<String>::Element *E = deps.front(); E; E = E->next()) {
  194. m.deps.push_back(E->get());
  195. }
  196. return m;
  197. }
  198. void EditorFileSystem::_scan_filesystem() {
  199. ERR_FAIL_COND(!scanning || new_filesystem);
  200. //read .fscache
  201. String cpath;
  202. sources_changed.clear();
  203. file_cache.clear();
  204. String project = Globals::get_singleton()->get_resource_path();
  205. String fscache = EditorSettings::get_singleton()->get_project_settings_path().plus_file("filesystem_cache");
  206. FileAccess *f = FileAccess::open(fscache, FileAccess::READ);
  207. if (f) {
  208. //read the disk cache
  209. while (!f->eof_reached()) {
  210. String l = f->get_line().strip_edges();
  211. if (l == String())
  212. continue;
  213. if (l.begins_with("::")) {
  214. Vector<String> split = l.split("::");
  215. ERR_CONTINUE(split.size() != 3);
  216. String name = split[1];
  217. cpath = name;
  218. } else {
  219. Vector<String> split = l.split("::");
  220. ERR_CONTINUE(split.size() != 5);
  221. String name = split[0];
  222. String file;
  223. file = name;
  224. name = cpath.plus_file(name);
  225. FileCache fc;
  226. fc.type = split[1];
  227. fc.modification_time = split[2].to_int64();
  228. String meta = split[3].strip_edges();
  229. fc.meta.enabled = false;
  230. if (meta.find("<>") != -1) {
  231. Vector<String> spl = meta.split("<>");
  232. int sc = spl.size() - 1;
  233. if (sc % 3 == 0) {
  234. fc.meta.enabled = true;
  235. fc.meta.import_editor = spl[0];
  236. fc.meta.sources.resize(sc / 3);
  237. for (int i = 0; i < fc.meta.sources.size(); i++) {
  238. fc.meta.sources[i].path = spl[1 + i * 3 + 0];
  239. fc.meta.sources[i].md5 = spl[1 + i * 3 + 1];
  240. fc.meta.sources[i].modified_time = spl[1 + i * 3 + 2].to_int64();
  241. }
  242. }
  243. }
  244. String deps = split[4].strip_edges();
  245. if (deps.length()) {
  246. Vector<String> dp = deps.split("<>");
  247. for (int i = 0; i < dp.size(); i++) {
  248. String path = dp[i];
  249. fc.meta.deps.push_back(path);
  250. }
  251. }
  252. file_cache[name] = fc;
  253. }
  254. }
  255. f->close();
  256. memdelete(f);
  257. }
  258. EditorProgressBG scan_progress("efs", "ScanFS", 1000);
  259. ScanProgress sp;
  260. sp.low = 0;
  261. sp.hi = 1;
  262. sp.progress = &scan_progress;
  263. new_filesystem = memnew(EditorFileSystemDirectory);
  264. new_filesystem->parent = NULL;
  265. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  266. d->change_dir("res://");
  267. _scan_new_dir(new_filesystem, d, sp);
  268. file_cache.clear(); //clear caches, no longer needed
  269. memdelete(d);
  270. //save back the findings
  271. // String fscache = EditorSettings::get_singleton()->get_project_settings_path().plus_file("file_cache");
  272. f = FileAccess::open(fscache, FileAccess::WRITE);
  273. _save_filesystem_cache(new_filesystem, f);
  274. f->close();
  275. memdelete(f);
  276. scanning = false;
  277. }
  278. void EditorFileSystem::_thread_func(void *_userdata) {
  279. EditorFileSystem *sd = (EditorFileSystem *)_userdata;
  280. sd->_scan_filesystem();
  281. }
  282. bool EditorFileSystem::_update_scan_actions() {
  283. sources_changed.clear();
  284. bool fs_changed = false;
  285. for (List<ItemAction>::Element *E = scan_actions.front(); E; E = E->next()) {
  286. ItemAction &ia = E->get();
  287. switch (ia.action) {
  288. case ItemAction::ACTION_NONE: {
  289. } break;
  290. case ItemAction::ACTION_DIR_ADD: {
  291. //print_line("*ACTION ADD DIR: "+ia.new_dir->get_name());
  292. int idx = 0;
  293. for (int i = 0; i < ia.dir->subdirs.size(); i++) {
  294. if (ia.new_dir->name < ia.dir->subdirs[i]->name)
  295. break;
  296. idx++;
  297. }
  298. if (idx == ia.dir->subdirs.size()) {
  299. ia.dir->subdirs.push_back(ia.new_dir);
  300. } else {
  301. ia.dir->subdirs.insert(idx, ia.new_dir);
  302. }
  303. fs_changed = true;
  304. } break;
  305. case ItemAction::ACTION_DIR_REMOVE: {
  306. ERR_CONTINUE(!ia.dir->parent);
  307. //print_line("*ACTION REMOVE DIR: "+ia.dir->get_name());
  308. ia.dir->parent->subdirs.erase(ia.dir);
  309. memdelete(ia.dir);
  310. fs_changed = true;
  311. } break;
  312. case ItemAction::ACTION_FILE_ADD: {
  313. int idx = 0;
  314. for (int i = 0; i < ia.dir->files.size(); i++) {
  315. if (ia.new_file->file < ia.dir->files[i]->file)
  316. break;
  317. idx++;
  318. }
  319. if (idx == ia.dir->files.size()) {
  320. ia.dir->files.push_back(ia.new_file);
  321. } else {
  322. ia.dir->files.insert(idx, ia.new_file);
  323. }
  324. fs_changed = true;
  325. //print_line("*ACTION ADD FILE: "+ia.new_file->file);
  326. } break;
  327. case ItemAction::ACTION_FILE_REMOVE: {
  328. int idx = ia.dir->find_file_index(ia.file);
  329. ERR_CONTINUE(idx == -1);
  330. memdelete(ia.dir->files[idx]);
  331. ia.dir->files.remove(idx);
  332. fs_changed = true;
  333. //print_line("*ACTION REMOVE FILE: "+ia.file);
  334. } break;
  335. case ItemAction::ACTION_FILE_SOURCES_CHANGED: {
  336. int idx = ia.dir->find_file_index(ia.file);
  337. ERR_CONTINUE(idx == -1);
  338. String full_path = ia.dir->get_file_path(idx);
  339. sources_changed.push_back(full_path);
  340. } break;
  341. }
  342. }
  343. scan_actions.clear();
  344. return fs_changed;
  345. }
  346. void EditorFileSystem::scan() {
  347. if (bool(Globals::get_singleton()->get("debug/disable_scan")))
  348. return;
  349. if (scanning || scanning_sources || thread)
  350. return;
  351. abort_scan = false;
  352. if (!use_threads) {
  353. scanning = true;
  354. scan_total = 0;
  355. _scan_filesystem();
  356. if (filesystem)
  357. memdelete(filesystem);
  358. // file_type_cache.clear();
  359. filesystem = new_filesystem;
  360. new_filesystem = NULL;
  361. _update_scan_actions();
  362. scanning = false;
  363. emit_signal("filesystem_changed");
  364. emit_signal("sources_changed", sources_changed.size() > 0);
  365. } else {
  366. ERR_FAIL_COND(thread);
  367. set_process(true);
  368. Thread::Settings s;
  369. scanning = true;
  370. scan_total = 0;
  371. s.priority = Thread::PRIORITY_LOW;
  372. thread = Thread::create(_thread_func, this, s);
  373. //tree->hide();
  374. //progress->show();
  375. }
  376. }
  377. bool EditorFileSystem::_check_meta_sources(EditorFileSystemDirectory::ImportMeta &p_meta) {
  378. if (p_meta.enabled) {
  379. for (int j = 0; j < p_meta.sources.size(); j++) {
  380. String src = EditorImportPlugin::expand_source_path(p_meta.sources[j].path);
  381. if (!FileAccess::exists(src)) {
  382. p_meta.sources[j].missing = true;
  383. continue;
  384. }
  385. p_meta.sources[j].missing = false;
  386. uint64_t mt = FileAccess::get_modified_time(src);
  387. if (mt != p_meta.sources[j].modified_time) {
  388. //scan
  389. String md5 = FileAccess::get_md5(src);
  390. //print_line("checking: "+src);
  391. //print_line("md5: "+md5);
  392. //print_line("vs: "+p_meta.sources[j].md5);
  393. if (md5 != p_meta.sources[j].md5) {
  394. //really changed
  395. return true;
  396. }
  397. p_meta.sources[j].modified_time = mt;
  398. }
  399. }
  400. }
  401. return false;
  402. }
  403. void EditorFileSystem::ScanProgress::update(int p_current, int p_total) const {
  404. float ratio = low + ((hi - low) / p_total) * p_current;
  405. progress->step(ratio * 1000);
  406. EditorFileSystem::singleton->scan_total = ratio;
  407. }
  408. EditorFileSystem::ScanProgress EditorFileSystem::ScanProgress::get_sub(int p_current, int p_total) const {
  409. ScanProgress sp = *this;
  410. float slice = (sp.hi - sp.low) / p_total;
  411. sp.low += slice * p_current;
  412. sp.hi = slice;
  413. return sp;
  414. }
  415. void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess *da, const ScanProgress &p_progress) {
  416. List<String> dirs;
  417. List<String> files;
  418. String cd = da->get_current_dir();
  419. p_dir->modified_time = FileAccess::get_modified_time(cd);
  420. da->list_dir_begin();
  421. while (true) {
  422. bool isdir;
  423. String f = da->get_next(&isdir);
  424. if (f == "")
  425. break;
  426. if (isdir) {
  427. if (f.begins_with(".")) //ignore hidden and . / ..
  428. continue;
  429. if (FileAccess::exists(cd.plus_file(f).plus_file("engine.cfg"))) // skip if another project inside this
  430. continue;
  431. dirs.push_back(f);
  432. } else {
  433. files.push_back(f);
  434. }
  435. }
  436. da->list_dir_end();
  437. dirs.sort();
  438. files.sort();
  439. int total = dirs.size() + files.size();
  440. int idx = 0;
  441. for (List<String>::Element *E = dirs.front(); E; E = E->next(), idx++) {
  442. if (da->change_dir(E->get()) == OK) {
  443. String d = da->get_current_dir();
  444. if (d == cd || !d.begins_with(cd)) {
  445. da->change_dir(cd); //avoid recursion
  446. } else {
  447. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  448. efd->parent = p_dir;
  449. efd->name = E->get();
  450. _scan_new_dir(efd, da, p_progress.get_sub(idx, total));
  451. int idx = 0;
  452. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  453. if (efd->name < p_dir->subdirs[i]->name)
  454. break;
  455. idx++;
  456. }
  457. if (idx == p_dir->subdirs.size()) {
  458. p_dir->subdirs.push_back(efd);
  459. } else {
  460. p_dir->subdirs.insert(idx, efd);
  461. }
  462. da->change_dir("..");
  463. }
  464. } else {
  465. ERR_PRINTS("Cannot go into subdir: " + E->get());
  466. }
  467. p_progress.update(idx, total);
  468. }
  469. for (List<String>::Element *E = files.front(); E; E = E->next(), idx++) {
  470. String ext = E->get().extension().to_lower();
  471. if (!valid_extensions.has(ext))
  472. continue; //invalid
  473. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  474. fi->file = E->get();
  475. String path = cd.plus_file(fi->file);
  476. FileCache *fc = file_cache.getptr(path);
  477. uint64_t mt = FileAccess::get_modified_time(path);
  478. if (fc && fc->modification_time == mt) {
  479. fi->meta = fc->meta;
  480. fi->type = fc->type;
  481. fi->modified_time = fc->modification_time;
  482. } else {
  483. fi->meta = _get_meta(path);
  484. fi->type = ResourceLoader::get_resource_type(path);
  485. fi->modified_time = mt;
  486. }
  487. if (fi->meta.enabled) {
  488. if (_check_meta_sources(fi->meta)) {
  489. ItemAction ia;
  490. ia.action = ItemAction::ACTION_FILE_SOURCES_CHANGED;
  491. ia.dir = p_dir;
  492. ia.file = E->get();
  493. scan_actions.push_back(ia);
  494. fi->meta.sources_changed = true;
  495. } else {
  496. fi->meta.sources_changed = false;
  497. }
  498. } else {
  499. fi->meta.sources_changed = true;
  500. }
  501. p_dir->files.push_back(fi);
  502. p_progress.update(idx, total);
  503. }
  504. }
  505. void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress) {
  506. uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
  507. bool updated_dir = false;
  508. //print_line("dir: "+p_dir->get_path()+" MODTIME: "+itos(p_dir->modified_time)+" CTIME: "+itos(current_mtime));
  509. if (current_mtime != p_dir->modified_time) {
  510. updated_dir = true;
  511. p_dir->modified_time = current_mtime;
  512. //ooooops, dir changed, see what's going on
  513. //first mark everything as veryfied
  514. for (int i = 0; i < p_dir->files.size(); i++) {
  515. p_dir->files[i]->verified = false;
  516. }
  517. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  518. p_dir->get_subdir(i)->verified = false;
  519. }
  520. //then scan files and directories and check what's different
  521. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  522. String cd = p_dir->get_path();
  523. da->change_dir(cd);
  524. da->list_dir_begin();
  525. while (true) {
  526. bool isdir;
  527. String f = da->get_next(&isdir);
  528. if (f == "")
  529. break;
  530. if (isdir) {
  531. if (f.begins_with(".")) //ignore hidden and . / ..
  532. continue;
  533. int idx = p_dir->find_dir_index(f);
  534. if (idx == -1) {
  535. if (FileAccess::exists(cd.plus_file(f).plus_file("engine.cfg"))) // skip if another project inside this
  536. continue;
  537. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  538. efd->parent = p_dir;
  539. efd->name = f;
  540. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  541. d->change_dir(cd.plus_file(f));
  542. _scan_new_dir(efd, d, p_progress.get_sub(1, 1));
  543. memdelete(d);
  544. ItemAction ia;
  545. ia.action = ItemAction::ACTION_DIR_ADD;
  546. ia.dir = p_dir;
  547. ia.file = f;
  548. ia.new_dir = efd;
  549. scan_actions.push_back(ia);
  550. } else {
  551. p_dir->subdirs[idx]->verified = true;
  552. }
  553. } else {
  554. String ext = f.extension().to_lower();
  555. if (!valid_extensions.has(ext))
  556. continue; //invalid
  557. int idx = p_dir->find_file_index(f);
  558. if (idx == -1) {
  559. //never seen this file, add actition to add it
  560. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  561. fi->file = f;
  562. String path = cd.plus_file(fi->file);
  563. fi->modified_time = FileAccess::get_modified_time(path);
  564. fi->meta = _get_meta(path);
  565. fi->type = ResourceLoader::get_resource_type(path);
  566. {
  567. ItemAction ia;
  568. ia.action = ItemAction::ACTION_FILE_ADD;
  569. ia.dir = p_dir;
  570. ia.file = f;
  571. ia.new_file = fi;
  572. scan_actions.push_back(ia);
  573. }
  574. //take the chance and scan sources
  575. if (_check_meta_sources(fi->meta)) {
  576. ItemAction ia;
  577. ia.action = ItemAction::ACTION_FILE_SOURCES_CHANGED;
  578. ia.dir = p_dir;
  579. ia.file = f;
  580. scan_actions.push_back(ia);
  581. fi->meta.sources_changed = true;
  582. } else {
  583. fi->meta.sources_changed = false;
  584. }
  585. } else {
  586. p_dir->files[idx]->verified = true;
  587. }
  588. }
  589. }
  590. da->list_dir_end();
  591. memdelete(da);
  592. }
  593. for (int i = 0; i < p_dir->files.size(); i++) {
  594. if (updated_dir && !p_dir->files[i]->verified) {
  595. //this file was removed, add action to remove it
  596. ItemAction ia;
  597. ia.action = ItemAction::ACTION_FILE_REMOVE;
  598. ia.dir = p_dir;
  599. ia.file = p_dir->files[i]->file;
  600. scan_actions.push_back(ia);
  601. continue;
  602. }
  603. if (_check_meta_sources(p_dir->files[i]->meta)) {
  604. ItemAction ia;
  605. ia.action = ItemAction::ACTION_FILE_SOURCES_CHANGED;
  606. ia.dir = p_dir;
  607. ia.file = p_dir->files[i]->file;
  608. scan_actions.push_back(ia);
  609. p_dir->files[i]->meta.sources_changed = true;
  610. } else {
  611. p_dir->files[i]->meta.sources_changed = false;
  612. }
  613. EditorResourcePreview::get_singleton()->check_for_invalidation(p_dir->get_file_path(i));
  614. }
  615. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  616. if (updated_dir && !p_dir->subdirs[i]->verified) {
  617. //this directory was removed, add action to remove it
  618. ItemAction ia;
  619. ia.action = ItemAction::ACTION_DIR_REMOVE;
  620. ia.dir = p_dir->subdirs[i];
  621. scan_actions.push_back(ia);
  622. continue;
  623. }
  624. _scan_fs_changes(p_dir->get_subdir(i), p_progress);
  625. }
  626. }
  627. void EditorFileSystem::_thread_func_sources(void *_userdata) {
  628. EditorFileSystem *efs = (EditorFileSystem *)_userdata;
  629. if (efs->filesystem) {
  630. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  631. ScanProgress sp;
  632. sp.progress = &pr;
  633. sp.hi = 1;
  634. sp.low = 0;
  635. efs->_scan_fs_changes(efs->filesystem, sp);
  636. }
  637. efs->scanning_sources_done = true;
  638. }
  639. void EditorFileSystem::get_changed_sources(List<String> *r_changed) {
  640. *r_changed = sources_changed;
  641. }
  642. void EditorFileSystem::scan_sources() {
  643. if (scanning || scanning_sources || thread)
  644. return;
  645. sources_changed.clear();
  646. scanning_sources = true;
  647. scanning_sources_done = false;
  648. abort_scan = false;
  649. if (!use_threads) {
  650. if (filesystem) {
  651. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  652. ScanProgress sp;
  653. sp.progress = &pr;
  654. sp.hi = 1;
  655. sp.low = 0;
  656. scan_total = 0;
  657. _scan_fs_changes(filesystem, sp);
  658. if (_update_scan_actions())
  659. emit_signal("filesystem_changed");
  660. }
  661. scanning_sources = false;
  662. scanning_sources_done = true;
  663. emit_signal("sources_changed", sources_changed.size() > 0);
  664. } else {
  665. ERR_FAIL_COND(thread_sources);
  666. set_process(true);
  667. scan_total = 0;
  668. Thread::Settings s;
  669. s.priority = Thread::PRIORITY_LOW;
  670. thread_sources = Thread::create(_thread_func_sources, this, s);
  671. //tree->hide();
  672. //print_line("SCAN BEGIN!");
  673. //progress->show();
  674. }
  675. }
  676. void EditorFileSystem::_notification(int p_what) {
  677. switch (p_what) {
  678. case NOTIFICATION_ENTER_TREE: {
  679. scan();
  680. } break;
  681. case NOTIFICATION_EXIT_TREE: {
  682. if (use_threads && thread) {
  683. //abort thread if in progress
  684. abort_scan = true;
  685. while (scanning) {
  686. OS::get_singleton()->delay_usec(1000);
  687. }
  688. Thread::wait_to_finish(thread);
  689. memdelete(thread);
  690. thread = NULL;
  691. WARN_PRINTS("Scan thread aborted...");
  692. set_process(false);
  693. }
  694. if (filesystem)
  695. memdelete(filesystem);
  696. if (new_filesystem)
  697. memdelete(new_filesystem);
  698. filesystem = NULL;
  699. new_filesystem = NULL;
  700. } break;
  701. case NOTIFICATION_PROCESS: {
  702. if (use_threads) {
  703. if (scanning_sources) {
  704. if (scanning_sources_done) {
  705. scanning_sources = false;
  706. set_process(false);
  707. Thread::wait_to_finish(thread_sources);
  708. memdelete(thread_sources);
  709. thread_sources = NULL;
  710. if (_update_scan_actions())
  711. emit_signal("filesystem_changed");
  712. //print_line("sources changed: "+itos(sources_changed.size()));
  713. emit_signal("sources_changed", sources_changed.size() > 0);
  714. }
  715. } else if (!scanning) {
  716. set_process(false);
  717. if (filesystem)
  718. memdelete(filesystem);
  719. filesystem = new_filesystem;
  720. new_filesystem = NULL;
  721. Thread::wait_to_finish(thread);
  722. memdelete(thread);
  723. thread = NULL;
  724. _update_scan_actions();
  725. emit_signal("filesystem_changed");
  726. emit_signal("sources_changed", sources_changed.size() > 0);
  727. //print_line("initial sources changed: "+itos(sources_changed.size()));
  728. } else {
  729. //progress->set_text("Scanning...\n"+itos(total*100)+"%");
  730. }
  731. }
  732. } break;
  733. }
  734. }
  735. bool EditorFileSystem::is_scanning() const {
  736. return scanning;
  737. }
  738. float EditorFileSystem::get_scanning_progress() const {
  739. return scan_total;
  740. }
  741. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  742. return filesystem;
  743. }
  744. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file) {
  745. if (!p_dir)
  746. return; //none
  747. p_file->store_line("::" + p_dir->get_path() + "::" + String::num(p_dir->modified_time));
  748. for (int i = 0; i < p_dir->files.size(); i++) {
  749. String s = p_dir->files[i]->file + "::" + p_dir->files[i]->type + "::" + String::num(p_dir->files[i]->modified_time) + "::";
  750. if (p_dir->files[i]->meta.enabled) {
  751. s += p_dir->files[i]->meta.import_editor;
  752. for (int j = 0; j < p_dir->files[i]->meta.sources.size(); j++) {
  753. s += "<>" + p_dir->files[i]->meta.sources[j].path;
  754. s += "<>" + p_dir->files[i]->meta.sources[j].md5;
  755. s += "<>" + String::num(p_dir->files[i]->meta.sources[j].modified_time);
  756. }
  757. }
  758. s += "::";
  759. for (int j = 0; j < p_dir->files[i]->meta.deps.size(); j++) {
  760. if (j > 0)
  761. s += "<>";
  762. s += p_dir->files[i]->meta.deps[j];
  763. }
  764. p_file->store_line(s);
  765. }
  766. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  767. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  768. }
  769. }
  770. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  771. //todo make faster
  772. if (!filesystem || scanning)
  773. return false;
  774. String f = Globals::get_singleton()->localize_path(p_file);
  775. if (!f.begins_with("res://"))
  776. return false;
  777. f = f.substr(6, f.length());
  778. f = f.replace("\\", "/");
  779. Vector<String> path = f.split("/");
  780. if (path.size() == 0)
  781. return false;
  782. String file = path[path.size() - 1];
  783. path.resize(path.size() - 1);
  784. EditorFileSystemDirectory *fs = filesystem;
  785. for (int i = 0; i < path.size(); i++) {
  786. int idx = -1;
  787. for (int j = 0; j < fs->get_subdir_count(); j++) {
  788. if (fs->get_subdir(j)->get_name() == path[i]) {
  789. idx = j;
  790. break;
  791. }
  792. }
  793. if (idx == -1) {
  794. //does not exist, create i guess?
  795. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  796. efsd->name = path[i];
  797. efsd->parent = fs;
  798. int idx2 = 0;
  799. for (int j = 0; j < fs->get_subdir_count(); j++) {
  800. if (efsd->name < fs->get_subdir(j)->get_name())
  801. break;
  802. idx2++;
  803. }
  804. if (idx2 == fs->get_subdir_count())
  805. fs->subdirs.push_back(efsd);
  806. else
  807. fs->subdirs.insert(idx2, efsd);
  808. fs = efsd;
  809. } else {
  810. fs = fs->get_subdir(idx);
  811. }
  812. }
  813. int cpos = -1;
  814. for (int i = 0; i < fs->files.size(); i++) {
  815. if (fs->files[i]->file == file) {
  816. cpos = i;
  817. break;
  818. }
  819. }
  820. r_file_pos = cpos;
  821. *r_d = fs;
  822. if (cpos != -1) {
  823. return true;
  824. } else {
  825. return false;
  826. }
  827. }
  828. String EditorFileSystem::get_file_type(const String &p_file) const {
  829. EditorFileSystemDirectory *fs = NULL;
  830. int cpos = -1;
  831. if (!_find_file(p_file, &fs, cpos)) {
  832. return "";
  833. }
  834. return fs->files[cpos]->type;
  835. }
  836. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  837. if (!filesystem || scanning)
  838. return NULL;
  839. EditorFileSystemDirectory *fs = NULL;
  840. int cpos = -1;
  841. if (!_find_file(p_file, &fs, cpos)) {
  842. return NULL;
  843. }
  844. if (r_index)
  845. *r_index = cpos;
  846. return fs;
  847. }
  848. EditorFileSystemDirectory *EditorFileSystem::get_path(const String &p_path) {
  849. if (!filesystem || scanning)
  850. return NULL;
  851. String f = Globals::get_singleton()->localize_path(p_path);
  852. if (!f.begins_with("res://"))
  853. return NULL;
  854. f = f.substr(6, f.length());
  855. f = f.replace("\\", "/");
  856. if (f == String())
  857. return filesystem;
  858. if (f.ends_with("/"))
  859. f = f.substr(0, f.length() - 1);
  860. Vector<String> path = f.split("/");
  861. if (path.size() == 0)
  862. return NULL;
  863. EditorFileSystemDirectory *fs = filesystem;
  864. for (int i = 0; i < path.size(); i++) {
  865. int idx = -1;
  866. for (int j = 0; j < fs->get_subdir_count(); j++) {
  867. if (fs->get_subdir(j)->get_name() == path[i]) {
  868. idx = j;
  869. break;
  870. }
  871. }
  872. if (idx == -1) {
  873. return NULL;
  874. } else {
  875. fs = fs->get_subdir(idx);
  876. }
  877. }
  878. return fs;
  879. }
  880. void EditorFileSystem::_resource_saved(const String &p_path) {
  881. //print_line("resource saved: "+p_path);
  882. EditorFileSystem::get_singleton()->update_file(p_path);
  883. }
  884. String EditorFileSystem::_find_first_from_source(EditorFileSystemDirectory *p_dir, const String &p_src) const {
  885. for (int i = 0; i < p_dir->files.size(); i++) {
  886. for (int j = 0; j < p_dir->files[i]->meta.sources.size(); j++) {
  887. if (p_dir->files[i]->meta.sources[j].path == p_src)
  888. return p_dir->get_file_path(i);
  889. }
  890. }
  891. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  892. String ret = _find_first_from_source(p_dir->subdirs[i], p_src);
  893. if (ret.length() > 0)
  894. return ret;
  895. }
  896. return String();
  897. }
  898. String EditorFileSystem::find_resource_from_source(const String &p_path) const {
  899. if (filesystem)
  900. return _find_first_from_source(filesystem, p_path);
  901. return String();
  902. }
  903. void EditorFileSystem::update_file(const String &p_file) {
  904. EditorFileSystemDirectory *fs = NULL;
  905. int cpos = -1;
  906. if (!_find_file(p_file, &fs, cpos)) {
  907. if (!fs)
  908. return;
  909. }
  910. if (!FileAccess::exists(p_file)) {
  911. //was removed
  912. memdelete(fs->files[cpos]);
  913. fs->files.remove(cpos);
  914. call_deferred("emit_signal", "filesystem_changed"); //update later
  915. return;
  916. }
  917. String type = ResourceLoader::get_resource_type(p_file);
  918. if (cpos == -1) {
  919. int idx = 0;
  920. for (int i = 0; i < fs->files.size(); i++) {
  921. if (p_file < fs->files[i]->file)
  922. break;
  923. idx++;
  924. }
  925. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  926. fi->file = p_file.get_file();
  927. if (idx == fs->files.size()) {
  928. fs->files.push_back(fi);
  929. } else {
  930. fs->files.insert(idx, fi);
  931. }
  932. cpos = idx;
  933. }
  934. //print_line("UPDATING: "+p_file);
  935. fs->files[cpos]->type = type;
  936. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  937. fs->files[cpos]->meta = _get_meta(p_file);
  938. EditorResourcePreview::get_singleton()->call_deferred("check_for_invalidation", p_file);
  939. call_deferred("emit_signal", "filesystem_changed"); //update later
  940. }
  941. void EditorFileSystem::_bind_methods() {
  942. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  943. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  944. }
  945. EditorFileSystem::EditorFileSystem() {
  946. singleton = this;
  947. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  948. filesystem->parent = NULL;
  949. thread = NULL;
  950. scanning = false;
  951. use_threads = true;
  952. thread_sources = NULL;
  953. new_filesystem = NULL;
  954. abort_scan = false;
  955. scanning_sources = false;
  956. scanning_sources_done = false;
  957. ResourceSaver::set_save_callback(_resource_saved);
  958. List<String> extensionsl;
  959. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  960. for (List<String>::Element *E = extensionsl.front(); E; E = E->next()) {
  961. valid_extensions.insert(E->get());
  962. }
  963. scan_total = 0;
  964. }
  965. EditorFileSystem::~EditorFileSystem() {
  966. }