editor_file_system.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "io/resource_import.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. #include "project_settings.h"
  40. #include "variant_parser.h"
  41. EditorFileSystem *EditorFileSystem::singleton = NULL;
  42. void EditorFileSystemDirectory::sort_files() {
  43. files.sort_custom<FileInfoSort>();
  44. }
  45. int EditorFileSystemDirectory::find_file_index(const String &p_file) const {
  46. for (int i = 0; i < files.size(); i++) {
  47. if (files[i]->file == p_file)
  48. return i;
  49. }
  50. return -1;
  51. }
  52. int EditorFileSystemDirectory::find_dir_index(const String &p_dir) const {
  53. for (int i = 0; i < subdirs.size(); i++) {
  54. if (subdirs[i]->name == p_dir)
  55. return i;
  56. }
  57. return -1;
  58. }
  59. int EditorFileSystemDirectory::get_subdir_count() const {
  60. return subdirs.size();
  61. }
  62. EditorFileSystemDirectory *EditorFileSystemDirectory::get_subdir(int p_idx) {
  63. ERR_FAIL_INDEX_V(p_idx, subdirs.size(), NULL);
  64. return subdirs[p_idx];
  65. }
  66. int EditorFileSystemDirectory::get_file_count() const {
  67. return files.size();
  68. }
  69. String EditorFileSystemDirectory::get_file(int p_idx) const {
  70. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  71. return files[p_idx]->file;
  72. }
  73. String EditorFileSystemDirectory::get_path() const {
  74. String p;
  75. const EditorFileSystemDirectory *d = this;
  76. while (d->parent) {
  77. p = d->name + "/" + p;
  78. d = d->parent;
  79. }
  80. return "res://" + p;
  81. }
  82. String EditorFileSystemDirectory::get_file_path(int p_idx) const {
  83. String file = get_file(p_idx);
  84. const EditorFileSystemDirectory *d = this;
  85. while (d->parent) {
  86. file = d->name + "/" + file;
  87. d = d->parent;
  88. }
  89. return "res://" + file;
  90. }
  91. Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {
  92. ERR_FAIL_INDEX_V(p_idx, files.size(), Vector<String>());
  93. return files[p_idx]->deps;
  94. }
  95. bool EditorFileSystemDirectory::get_file_import_is_valid(int p_idx) const {
  96. ERR_FAIL_INDEX_V(p_idx, files.size(), false);
  97. return files[p_idx]->import_valid;
  98. }
  99. StringName EditorFileSystemDirectory::get_file_type(int p_idx) const {
  100. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  101. return files[p_idx]->type;
  102. }
  103. String EditorFileSystemDirectory::get_name() {
  104. return name;
  105. }
  106. EditorFileSystemDirectory *EditorFileSystemDirectory::get_parent() {
  107. return parent;
  108. }
  109. void EditorFileSystemDirectory::_bind_methods() {
  110. ClassDB::bind_method(D_METHOD("get_subdir_count"), &EditorFileSystemDirectory::get_subdir_count);
  111. ClassDB::bind_method(D_METHOD("get_subdir", "idx"), &EditorFileSystemDirectory::get_subdir);
  112. ClassDB::bind_method(D_METHOD("get_file_count"), &EditorFileSystemDirectory::get_file_count);
  113. ClassDB::bind_method(D_METHOD("get_file", "idx"), &EditorFileSystemDirectory::get_file);
  114. ClassDB::bind_method(D_METHOD("get_file_path", "idx"), &EditorFileSystemDirectory::get_file_path);
  115. ClassDB::bind_method(D_METHOD("get_file_type", "idx"), &EditorFileSystemDirectory::get_file_type);
  116. ClassDB::bind_method(D_METHOD("get_file_import_is_valid", "idx"), &EditorFileSystemDirectory::get_file_import_is_valid);
  117. ClassDB::bind_method(D_METHOD("get_name"), &EditorFileSystemDirectory::get_name);
  118. ClassDB::bind_method(D_METHOD("get_path"), &EditorFileSystemDirectory::get_path);
  119. ClassDB::bind_method(D_METHOD("get_parent"), &EditorFileSystemDirectory::get_parent);
  120. ClassDB::bind_method(D_METHOD("find_file_index", "name"), &EditorFileSystemDirectory::find_file_index);
  121. ClassDB::bind_method(D_METHOD("find_dir_index", "name"), &EditorFileSystemDirectory::find_dir_index);
  122. }
  123. EditorFileSystemDirectory::EditorFileSystemDirectory() {
  124. modified_time = 0;
  125. parent = NULL;
  126. verified = false;
  127. }
  128. EditorFileSystemDirectory::~EditorFileSystemDirectory() {
  129. for (int i = 0; i < files.size(); i++) {
  130. memdelete(files[i]);
  131. }
  132. for (int i = 0; i < subdirs.size(); i++) {
  133. memdelete(subdirs[i]);
  134. }
  135. }
  136. void EditorFileSystem::_scan_filesystem() {
  137. ERR_FAIL_COND(!scanning || new_filesystem);
  138. //read .fscache
  139. String cpath;
  140. sources_changed.clear();
  141. file_cache.clear();
  142. String project = ProjectSettings::get_singleton()->get_resource_path();
  143. String fscache = EditorSettings::get_singleton()->get_project_settings_path().plus_file("filesystem_cache3");
  144. FileAccess *f = FileAccess::open(fscache, FileAccess::READ);
  145. if (f) {
  146. //read the disk cache
  147. while (!f->eof_reached()) {
  148. String l = f->get_line().strip_edges();
  149. if (l == String())
  150. continue;
  151. if (l.begins_with("::")) {
  152. Vector<String> split = l.split("::");
  153. ERR_CONTINUE(split.size() != 3);
  154. String name = split[1];
  155. cpath = name;
  156. } else {
  157. Vector<String> split = l.split("::");
  158. ERR_CONTINUE(split.size() != 6);
  159. String name = split[0];
  160. String file;
  161. file = name;
  162. name = cpath.plus_file(name);
  163. FileCache fc;
  164. fc.type = split[1];
  165. fc.modification_time = split[2].to_int64();
  166. fc.import_modification_time = split[3].to_int64();
  167. fc.import_valid = split[4].to_int64() != 0;
  168. String deps = split[5].strip_edges();
  169. if (deps.length()) {
  170. Vector<String> dp = deps.split("<>");
  171. for (int i = 0; i < dp.size(); i++) {
  172. String path = dp[i];
  173. fc.deps.push_back(path);
  174. }
  175. }
  176. file_cache[name] = fc;
  177. }
  178. }
  179. f->close();
  180. memdelete(f);
  181. }
  182. String update_cache = EditorSettings::get_singleton()->get_project_settings_path().plus_file("filesystem_update3");
  183. if (FileAccess::exists(update_cache)) {
  184. {
  185. FileAccessRef f = FileAccess::open(update_cache, FileAccess::READ);
  186. String l = f->get_line().strip_edges();
  187. while (l != String()) {
  188. file_cache.erase(l); //erase cache for this, so it gets updated
  189. l = f->get_line().strip_edges();
  190. }
  191. }
  192. DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  193. d->remove(update_cache); //bye bye update cache
  194. }
  195. EditorProgressBG scan_progress("efs", "ScanFS", 1000);
  196. ScanProgress sp;
  197. sp.low = 0;
  198. sp.hi = 1;
  199. sp.progress = &scan_progress;
  200. new_filesystem = memnew(EditorFileSystemDirectory);
  201. new_filesystem->parent = NULL;
  202. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  203. d->change_dir("res://");
  204. _scan_new_dir(new_filesystem, d, sp);
  205. file_cache.clear(); //clear caches, no longer needed
  206. memdelete(d);
  207. f = FileAccess::open(fscache, FileAccess::WRITE);
  208. _save_filesystem_cache(new_filesystem, f);
  209. f->close();
  210. memdelete(f);
  211. scanning = false;
  212. }
  213. void EditorFileSystem::_save_filesystem_cache() {
  214. String fscache = EditorSettings::get_singleton()->get_project_settings_path().plus_file("filesystem_cache3");
  215. FileAccess *f = FileAccess::open(fscache, FileAccess::WRITE);
  216. _save_filesystem_cache(filesystem, f);
  217. f->close();
  218. memdelete(f);
  219. }
  220. void EditorFileSystem::_thread_func(void *_userdata) {
  221. EditorFileSystem *sd = (EditorFileSystem *)_userdata;
  222. sd->_scan_filesystem();
  223. }
  224. bool EditorFileSystem::_update_scan_actions() {
  225. sources_changed.clear();
  226. bool fs_changed = false;
  227. Vector<String> reimports;
  228. for (List<ItemAction>::Element *E = scan_actions.front(); E; E = E->next()) {
  229. ItemAction &ia = E->get();
  230. switch (ia.action) {
  231. case ItemAction::ACTION_NONE: {
  232. } break;
  233. case ItemAction::ACTION_DIR_ADD: {
  234. int idx = 0;
  235. for (int i = 0; i < ia.dir->subdirs.size(); i++) {
  236. if (ia.new_dir->name < ia.dir->subdirs[i]->name)
  237. break;
  238. idx++;
  239. }
  240. if (idx == ia.dir->subdirs.size()) {
  241. ia.dir->subdirs.push_back(ia.new_dir);
  242. } else {
  243. ia.dir->subdirs.insert(idx, ia.new_dir);
  244. }
  245. fs_changed = true;
  246. } break;
  247. case ItemAction::ACTION_DIR_REMOVE: {
  248. ERR_CONTINUE(!ia.dir->parent);
  249. ia.dir->parent->subdirs.erase(ia.dir);
  250. memdelete(ia.dir);
  251. fs_changed = true;
  252. } break;
  253. case ItemAction::ACTION_FILE_ADD: {
  254. int idx = 0;
  255. for (int i = 0; i < ia.dir->files.size(); i++) {
  256. if (ia.new_file->file < ia.dir->files[i]->file)
  257. break;
  258. idx++;
  259. }
  260. if (idx == ia.dir->files.size()) {
  261. ia.dir->files.push_back(ia.new_file);
  262. } else {
  263. ia.dir->files.insert(idx, ia.new_file);
  264. }
  265. fs_changed = true;
  266. } break;
  267. case ItemAction::ACTION_FILE_REMOVE: {
  268. int idx = ia.dir->find_file_index(ia.file);
  269. ERR_CONTINUE(idx == -1);
  270. _delete_internal_files(ia.dir->files[idx]->file);
  271. memdelete(ia.dir->files[idx]);
  272. ia.dir->files.remove(idx);
  273. fs_changed = true;
  274. } break;
  275. case ItemAction::ACTION_FILE_REIMPORT: {
  276. int idx = ia.dir->find_file_index(ia.file);
  277. ERR_CONTINUE(idx == -1);
  278. String full_path = ia.dir->get_file_path(idx);
  279. reimports.push_back(full_path);
  280. fs_changed = true;
  281. } break;
  282. }
  283. }
  284. if (reimports.size()) {
  285. reimport_files(reimports);
  286. }
  287. scan_actions.clear();
  288. return fs_changed;
  289. }
  290. void EditorFileSystem::scan() {
  291. if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/)
  292. return;
  293. if (scanning || scanning_changes || thread)
  294. return;
  295. _update_extensions();
  296. abort_scan = false;
  297. if (!use_threads) {
  298. scanning = true;
  299. scan_total = 0;
  300. _scan_filesystem();
  301. if (filesystem)
  302. memdelete(filesystem);
  303. //file_type_cache.clear();
  304. filesystem = new_filesystem;
  305. new_filesystem = NULL;
  306. _update_scan_actions();
  307. scanning = false;
  308. emit_signal("filesystem_changed");
  309. emit_signal("sources_changed", sources_changed.size() > 0);
  310. } else {
  311. ERR_FAIL_COND(thread);
  312. set_process(true);
  313. Thread::Settings s;
  314. scanning = true;
  315. scan_total = 0;
  316. s.priority = Thread::PRIORITY_LOW;
  317. thread = Thread::create(_thread_func, this, s);
  318. //tree->hide();
  319. //progress->show();
  320. }
  321. }
  322. void EditorFileSystem::ScanProgress::update(int p_current, int p_total) const {
  323. float ratio = low + ((hi - low) / p_total) * p_current;
  324. progress->step(ratio * 1000);
  325. EditorFileSystem::singleton->scan_total = ratio;
  326. }
  327. EditorFileSystem::ScanProgress EditorFileSystem::ScanProgress::get_sub(int p_current, int p_total) const {
  328. ScanProgress sp = *this;
  329. float slice = (sp.hi - sp.low) / p_total;
  330. sp.low += slice * p_current;
  331. sp.hi = slice;
  332. return sp;
  333. }
  334. bool EditorFileSystem::_check_missing_imported_files(const String &p_path) {
  335. if (!reimport_on_missing_imported_files)
  336. return true;
  337. Error err;
  338. FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  339. if (!f) {
  340. print_line("could not open import for " + p_path);
  341. return false;
  342. }
  343. VariantParser::StreamFile stream;
  344. stream.f = f;
  345. String assign;
  346. Variant value;
  347. VariantParser::Tag next_tag;
  348. int lines = 0;
  349. String error_text;
  350. List<String> to_check;
  351. while (true) {
  352. assign = Variant();
  353. next_tag.fields.clear();
  354. next_tag.name = String();
  355. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
  356. if (err == ERR_FILE_EOF) {
  357. memdelete(f);
  358. return OK;
  359. } else if (err != OK) {
  360. ERR_PRINTS("ResourceFormatImporter::load - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
  361. memdelete(f);
  362. return false;
  363. }
  364. if (assign != String()) {
  365. if (assign.begins_with("path")) {
  366. to_check.push_back(value);
  367. } else if (assign == "files") {
  368. Array fa = value;
  369. for (int i = 0; i < fa.size(); i++) {
  370. to_check.push_back(fa[i]);
  371. }
  372. }
  373. } else if (next_tag.name != "remap" && next_tag.name != "deps") {
  374. break;
  375. }
  376. }
  377. memdelete(f);
  378. for (List<String>::Element *E = to_check.front(); E; E = E->next()) {
  379. if (!FileAccess::exists(E->get())) {
  380. return false;
  381. }
  382. }
  383. return true;
  384. }
  385. void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess *da, const ScanProgress &p_progress) {
  386. List<String> dirs;
  387. List<String> files;
  388. String cd = da->get_current_dir();
  389. p_dir->modified_time = FileAccess::get_modified_time(cd);
  390. da->list_dir_begin();
  391. while (true) {
  392. bool isdir;
  393. String f = da->get_next(&isdir);
  394. if (f == "")
  395. break;
  396. if (isdir) {
  397. if (f.begins_with(".")) //ignore hidden and . / ..
  398. continue;
  399. if (FileAccess::exists(cd.plus_file(f).plus_file("project.godot"))) // skip if another project inside this
  400. continue;
  401. if (FileAccess::exists(cd.plus_file(f).plus_file(".gdignore"))) // skip if another project inside this
  402. continue;
  403. dirs.push_back(f);
  404. } else {
  405. files.push_back(f);
  406. }
  407. }
  408. da->list_dir_end();
  409. dirs.sort_custom<NaturalNoCaseComparator>();
  410. files.sort_custom<NaturalNoCaseComparator>();
  411. int total = dirs.size() + files.size();
  412. int idx = 0;
  413. for (List<String>::Element *E = dirs.front(); E; E = E->next(), idx++) {
  414. if (da->change_dir(E->get()) == OK) {
  415. String d = da->get_current_dir();
  416. if (d == cd || !d.begins_with(cd)) {
  417. da->change_dir(cd); //avoid recursion
  418. } else {
  419. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  420. efd->parent = p_dir;
  421. efd->name = E->get();
  422. _scan_new_dir(efd, da, p_progress.get_sub(idx, total));
  423. int idx = 0;
  424. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  425. if (efd->name < p_dir->subdirs[i]->name)
  426. break;
  427. idx++;
  428. }
  429. if (idx == p_dir->subdirs.size()) {
  430. p_dir->subdirs.push_back(efd);
  431. } else {
  432. p_dir->subdirs.insert(idx, efd);
  433. }
  434. da->change_dir("..");
  435. }
  436. } else {
  437. ERR_PRINTS("Cannot go into subdir: " + E->get());
  438. }
  439. p_progress.update(idx, total);
  440. }
  441. for (List<String>::Element *E = files.front(); E; E = E->next(), idx++) {
  442. String ext = E->get().get_extension().to_lower();
  443. if (!valid_extensions.has(ext)) {
  444. continue; //invalid
  445. }
  446. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  447. fi->file = E->get();
  448. String path = cd.plus_file(fi->file);
  449. FileCache *fc = file_cache.getptr(path);
  450. uint64_t mt = FileAccess::get_modified_time(path);
  451. if (import_extensions.has(ext)) {
  452. //is imported
  453. uint64_t import_mt = 0;
  454. if (FileAccess::exists(path + ".import")) {
  455. import_mt = FileAccess::get_modified_time(path + ".import");
  456. }
  457. if (fc && fc->modification_time == mt && fc->import_modification_time == import_mt && _check_missing_imported_files(path)) {
  458. fi->type = fc->type;
  459. fi->deps = fc->deps;
  460. fi->modified_time = fc->modification_time;
  461. fi->import_modified_time = fc->import_modification_time;
  462. fi->import_valid = fc->import_valid;
  463. if (fc->type == String()) {
  464. fi->type = ResourceLoader::get_resource_type(path);
  465. //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?)
  466. //note: I think this should not happen any longer..
  467. }
  468. } else {
  469. fi->type = ResourceFormatImporter::get_singleton()->get_resource_type(path);
  470. fi->modified_time = 0;
  471. fi->import_modified_time = 0;
  472. fi->import_valid = ResourceLoader::is_import_valid(path);
  473. ItemAction ia;
  474. ia.action = ItemAction::ACTION_FILE_REIMPORT;
  475. ia.dir = p_dir;
  476. ia.file = E->get();
  477. scan_actions.push_back(ia);
  478. }
  479. } else {
  480. if (fc && fc->modification_time == mt) {
  481. //not imported, so just update type if changed
  482. fi->type = fc->type;
  483. fi->modified_time = fc->modification_time;
  484. fi->deps = fc->deps;
  485. fi->import_modified_time = 0;
  486. fi->import_valid = true;
  487. } else {
  488. //new or modified time
  489. fi->type = ResourceLoader::get_resource_type(path);
  490. fi->deps = _get_dependencies(path);
  491. fi->modified_time = mt;
  492. fi->import_modified_time = 0;
  493. fi->import_valid = true;
  494. }
  495. }
  496. p_dir->files.push_back(fi);
  497. p_progress.update(idx, total);
  498. }
  499. }
  500. void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress) {
  501. uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
  502. bool updated_dir = false;
  503. String cd = p_dir->get_path();
  504. if (current_mtime != p_dir->modified_time) {
  505. updated_dir = true;
  506. p_dir->modified_time = current_mtime;
  507. //ooooops, dir changed, see what's going on
  508. //first mark everything as veryfied
  509. for (int i = 0; i < p_dir->files.size(); i++) {
  510. p_dir->files[i]->verified = false;
  511. }
  512. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  513. p_dir->get_subdir(i)->verified = false;
  514. }
  515. //then scan files and directories and check what's different
  516. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  517. da->change_dir(cd);
  518. da->list_dir_begin();
  519. while (true) {
  520. bool isdir;
  521. String f = da->get_next(&isdir);
  522. if (f == "")
  523. break;
  524. if (isdir) {
  525. if (f.begins_with(".")) //ignore hidden and . / ..
  526. continue;
  527. int idx = p_dir->find_dir_index(f);
  528. if (idx == -1) {
  529. if (FileAccess::exists(cd.plus_file(f).plus_file("project.godot"))) // skip if another project inside this
  530. continue;
  531. if (FileAccess::exists(cd.plus_file(f).plus_file(".gdignore"))) // skip if another project inside this
  532. continue;
  533. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  534. efd->parent = p_dir;
  535. efd->name = f;
  536. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  537. d->change_dir(cd.plus_file(f));
  538. _scan_new_dir(efd, d, p_progress.get_sub(1, 1));
  539. memdelete(d);
  540. ItemAction ia;
  541. ia.action = ItemAction::ACTION_DIR_ADD;
  542. ia.dir = p_dir;
  543. ia.file = f;
  544. ia.new_dir = efd;
  545. scan_actions.push_back(ia);
  546. } else {
  547. p_dir->subdirs[idx]->verified = true;
  548. }
  549. } else {
  550. String ext = f.get_extension().to_lower();
  551. if (!valid_extensions.has(ext))
  552. continue; //invalid
  553. int idx = p_dir->find_file_index(f);
  554. if (idx == -1) {
  555. //never seen this file, add actition to add it
  556. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  557. fi->file = f;
  558. String path = cd.plus_file(fi->file);
  559. fi->modified_time = FileAccess::get_modified_time(path);
  560. fi->import_modified_time = 0;
  561. fi->type = ResourceLoader::get_resource_type(path);
  562. fi->import_valid = ResourceLoader::is_import_valid(path);
  563. {
  564. ItemAction ia;
  565. ia.action = ItemAction::ACTION_FILE_ADD;
  566. ia.dir = p_dir;
  567. ia.file = f;
  568. ia.new_file = fi;
  569. scan_actions.push_back(ia);
  570. }
  571. if (import_extensions.has(ext)) {
  572. //if it can be imported, and it was added, it needs to be reimported
  573. ItemAction ia;
  574. ia.action = ItemAction::ACTION_FILE_REIMPORT;
  575. ia.dir = p_dir;
  576. ia.file = f;
  577. scan_actions.push_back(ia);
  578. }
  579. } else {
  580. p_dir->files[idx]->verified = true;
  581. }
  582. }
  583. }
  584. da->list_dir_end();
  585. memdelete(da);
  586. }
  587. for (int i = 0; i < p_dir->files.size(); i++) {
  588. if (updated_dir && !p_dir->files[i]->verified) {
  589. //this file was removed, add action to remove it
  590. ItemAction ia;
  591. ia.action = ItemAction::ACTION_FILE_REMOVE;
  592. ia.dir = p_dir;
  593. ia.file = p_dir->files[i]->file;
  594. scan_actions.push_back(ia);
  595. continue;
  596. }
  597. if (import_extensions.has(p_dir->files[i]->file.get_extension().to_lower())) {
  598. //check here if file must be imported or not
  599. String path = cd.plus_file(p_dir->files[i]->file);
  600. uint64_t mt = FileAccess::get_modified_time(path);
  601. bool reimport = false;
  602. if (mt != p_dir->files[i]->modified_time) {
  603. reimport = true; //it was modified, must be reimported.
  604. } else if (!FileAccess::exists(path + ".import")) {
  605. reimport = true; //no .import file, obviously reimport
  606. } else {
  607. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  608. if (import_mt != p_dir->files[i]->import_modified_time) {
  609. reimport = true;
  610. } else if (!_check_missing_imported_files(path)) {
  611. reimport = true;
  612. }
  613. }
  614. if (reimport) {
  615. ItemAction ia;
  616. ia.action = ItemAction::ACTION_FILE_REIMPORT;
  617. ia.dir = p_dir;
  618. ia.file = p_dir->files[i]->file;
  619. scan_actions.push_back(ia);
  620. }
  621. }
  622. EditorResourcePreview::get_singleton()->check_for_invalidation(p_dir->get_file_path(i));
  623. }
  624. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  625. if (updated_dir && !p_dir->subdirs[i]->verified) {
  626. //this directory was removed, add action to remove it
  627. ItemAction ia;
  628. ia.action = ItemAction::ACTION_DIR_REMOVE;
  629. ia.dir = p_dir->subdirs[i];
  630. scan_actions.push_back(ia);
  631. continue;
  632. }
  633. _scan_fs_changes(p_dir->get_subdir(i), p_progress);
  634. }
  635. }
  636. void EditorFileSystem::_delete_internal_files(String p_file) {
  637. if (FileAccess::exists(p_file + ".import")) {
  638. List<String> paths;
  639. ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
  640. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  641. for (List<String>::Element *E = paths.front(); E; E = E->next()) {
  642. da->remove(E->get());
  643. }
  644. da->remove(p_file + ".import");
  645. memdelete(da);
  646. }
  647. }
  648. void EditorFileSystem::_thread_func_sources(void *_userdata) {
  649. EditorFileSystem *efs = (EditorFileSystem *)_userdata;
  650. if (efs->filesystem) {
  651. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  652. ScanProgress sp;
  653. sp.progress = &pr;
  654. sp.hi = 1;
  655. sp.low = 0;
  656. efs->_scan_fs_changes(efs->filesystem, sp);
  657. }
  658. efs->scanning_changes_done = true;
  659. }
  660. void EditorFileSystem::get_changed_sources(List<String> *r_changed) {
  661. *r_changed = sources_changed;
  662. }
  663. void EditorFileSystem::scan_changes() {
  664. if (scanning || scanning_changes || thread)
  665. return;
  666. _update_extensions();
  667. sources_changed.clear();
  668. scanning_changes = true;
  669. scanning_changes_done = false;
  670. abort_scan = false;
  671. if (!use_threads) {
  672. if (filesystem) {
  673. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  674. ScanProgress sp;
  675. sp.progress = &pr;
  676. sp.hi = 1;
  677. sp.low = 0;
  678. scan_total = 0;
  679. _scan_fs_changes(filesystem, sp);
  680. if (_update_scan_actions())
  681. emit_signal("filesystem_changed");
  682. }
  683. scanning_changes = false;
  684. scanning_changes_done = true;
  685. emit_signal("sources_changed", sources_changed.size() > 0);
  686. } else {
  687. ERR_FAIL_COND(thread_sources);
  688. set_process(true);
  689. scan_total = 0;
  690. Thread::Settings s;
  691. s.priority = Thread::PRIORITY_LOW;
  692. thread_sources = Thread::create(_thread_func_sources, this, s);
  693. }
  694. }
  695. void EditorFileSystem::_notification(int p_what) {
  696. switch (p_what) {
  697. case NOTIFICATION_ENTER_TREE: {
  698. scan();
  699. } break;
  700. case NOTIFICATION_EXIT_TREE: {
  701. if (use_threads && thread) {
  702. //abort thread if in progress
  703. abort_scan = true;
  704. while (scanning) {
  705. OS::get_singleton()->delay_usec(1000);
  706. }
  707. Thread::wait_to_finish(thread);
  708. memdelete(thread);
  709. thread = NULL;
  710. WARN_PRINTS("Scan thread aborted...");
  711. set_process(false);
  712. }
  713. if (filesystem)
  714. memdelete(filesystem);
  715. if (new_filesystem)
  716. memdelete(new_filesystem);
  717. filesystem = NULL;
  718. new_filesystem = NULL;
  719. } break;
  720. case NOTIFICATION_PROCESS: {
  721. if (use_threads) {
  722. if (scanning_changes) {
  723. if (scanning_changes_done) {
  724. scanning_changes = false;
  725. set_process(false);
  726. Thread::wait_to_finish(thread_sources);
  727. memdelete(thread_sources);
  728. thread_sources = NULL;
  729. if (_update_scan_actions())
  730. emit_signal("filesystem_changed");
  731. emit_signal("sources_changed", sources_changed.size() > 0);
  732. }
  733. } else if (!scanning) {
  734. set_process(false);
  735. if (filesystem)
  736. memdelete(filesystem);
  737. filesystem = new_filesystem;
  738. new_filesystem = NULL;
  739. Thread::wait_to_finish(thread);
  740. memdelete(thread);
  741. thread = NULL;
  742. _update_scan_actions();
  743. emit_signal("filesystem_changed");
  744. emit_signal("sources_changed", sources_changed.size() > 0);
  745. }
  746. }
  747. } break;
  748. }
  749. }
  750. bool EditorFileSystem::is_scanning() const {
  751. return scanning || scanning_changes;
  752. }
  753. float EditorFileSystem::get_scanning_progress() const {
  754. return scan_total;
  755. }
  756. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  757. return filesystem;
  758. }
  759. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file) {
  760. if (!p_dir)
  761. return; //none
  762. p_file->store_line("::" + p_dir->get_path() + "::" + String::num(p_dir->modified_time));
  763. for (int i = 0; i < p_dir->files.size(); i++) {
  764. 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);
  765. s += "::";
  766. for (int j = 0; j < p_dir->files[i]->deps.size(); j++) {
  767. if (j > 0)
  768. s += "<>";
  769. s += p_dir->files[i]->deps[j];
  770. }
  771. p_file->store_line(s);
  772. }
  773. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  774. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  775. }
  776. }
  777. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  778. //todo make faster
  779. if (!filesystem || scanning)
  780. return false;
  781. String f = ProjectSettings::get_singleton()->localize_path(p_file);
  782. if (!f.begins_with("res://"))
  783. return false;
  784. f = f.substr(6, f.length());
  785. f = f.replace("\\", "/");
  786. Vector<String> path = f.split("/");
  787. if (path.size() == 0)
  788. return false;
  789. String file = path[path.size() - 1];
  790. path.resize(path.size() - 1);
  791. EditorFileSystemDirectory *fs = filesystem;
  792. for (int i = 0; i < path.size(); i++) {
  793. if (path[i].begins_with("."))
  794. return false;
  795. int idx = -1;
  796. for (int j = 0; j < fs->get_subdir_count(); j++) {
  797. if (fs->get_subdir(j)->get_name() == path[i]) {
  798. idx = j;
  799. break;
  800. }
  801. }
  802. if (idx == -1) {
  803. //does not exist, create i guess?
  804. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  805. efsd->name = path[i];
  806. efsd->parent = fs;
  807. int idx2 = 0;
  808. for (int j = 0; j < fs->get_subdir_count(); j++) {
  809. if (efsd->name < fs->get_subdir(j)->get_name())
  810. break;
  811. idx2++;
  812. }
  813. if (idx2 == fs->get_subdir_count())
  814. fs->subdirs.push_back(efsd);
  815. else
  816. fs->subdirs.insert(idx2, efsd);
  817. fs = efsd;
  818. } else {
  819. fs = fs->get_subdir(idx);
  820. }
  821. }
  822. int cpos = -1;
  823. for (int i = 0; i < fs->files.size(); i++) {
  824. if (fs->files[i]->file == file) {
  825. cpos = i;
  826. break;
  827. }
  828. }
  829. r_file_pos = cpos;
  830. *r_d = fs;
  831. if (cpos != -1) {
  832. return true;
  833. } else {
  834. return false;
  835. }
  836. }
  837. String EditorFileSystem::get_file_type(const String &p_file) const {
  838. EditorFileSystemDirectory *fs = NULL;
  839. int cpos = -1;
  840. if (!_find_file(p_file, &fs, cpos)) {
  841. return "";
  842. }
  843. return fs->files[cpos]->type;
  844. }
  845. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  846. if (!filesystem || scanning)
  847. return NULL;
  848. EditorFileSystemDirectory *fs = NULL;
  849. int cpos = -1;
  850. if (!_find_file(p_file, &fs, cpos)) {
  851. return NULL;
  852. }
  853. if (r_index)
  854. *r_index = cpos;
  855. return fs;
  856. }
  857. EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
  858. if (!filesystem || scanning)
  859. return NULL;
  860. String f = ProjectSettings::get_singleton()->localize_path(p_path);
  861. if (!f.begins_with("res://"))
  862. return NULL;
  863. f = f.substr(6, f.length());
  864. f = f.replace("\\", "/");
  865. if (f == String())
  866. return filesystem;
  867. if (f.ends_with("/"))
  868. f = f.substr(0, f.length() - 1);
  869. Vector<String> path = f.split("/");
  870. if (path.size() == 0)
  871. return NULL;
  872. EditorFileSystemDirectory *fs = filesystem;
  873. for (int i = 0; i < path.size(); i++) {
  874. int idx = -1;
  875. for (int j = 0; j < fs->get_subdir_count(); j++) {
  876. if (fs->get_subdir(j)->get_name() == path[i]) {
  877. idx = j;
  878. break;
  879. }
  880. }
  881. if (idx == -1) {
  882. return NULL;
  883. } else {
  884. fs = fs->get_subdir(idx);
  885. }
  886. }
  887. return fs;
  888. }
  889. void EditorFileSystem::_save_late_updated_files() {
  890. //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
  891. String fscache = EditorSettings::get_singleton()->get_project_settings_path().plus_file("filesystem_update3");
  892. FileAccessRef f = FileAccess::open(fscache, FileAccess::WRITE);
  893. for (Set<String>::Element *E = late_update_files.front(); E; E = E->next()) {
  894. f->store_line(E->get());
  895. }
  896. }
  897. void EditorFileSystem::_resource_saved(const String &p_path) {
  898. EditorFileSystem::get_singleton()->update_file(p_path);
  899. }
  900. Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
  901. List<String> deps;
  902. ResourceLoader::get_dependencies(p_path, &deps);
  903. Vector<String> ret;
  904. for (List<String>::Element *E = deps.front(); E; E = E->next()) {
  905. ret.push_back(E->get());
  906. }
  907. return ret;
  908. }
  909. void EditorFileSystem::update_file(const String &p_file) {
  910. EditorFileSystemDirectory *fs = NULL;
  911. int cpos = -1;
  912. if (!_find_file(p_file, &fs, cpos)) {
  913. if (!fs)
  914. return;
  915. }
  916. if (!FileAccess::exists(p_file)) {
  917. //was removed
  918. _delete_internal_files(p_file);
  919. memdelete(fs->files[cpos]);
  920. fs->files.remove(cpos);
  921. call_deferred("emit_signal", "filesystem_changed"); //update later
  922. return;
  923. }
  924. String type = ResourceLoader::get_resource_type(p_file);
  925. if (cpos == -1) {
  926. //the file did not exist, it was added
  927. late_added_files.insert(p_file); //remember that it was added. This mean it will be scanned and imported on editor restart
  928. int idx = 0;
  929. for (int i = 0; i < fs->files.size(); i++) {
  930. if (p_file < fs->files[i]->file)
  931. break;
  932. idx++;
  933. }
  934. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  935. fi->file = p_file.get_file();
  936. fi->import_modified_time = 0;
  937. fi->import_valid = ResourceLoader::is_import_valid(p_file);
  938. if (idx == fs->files.size()) {
  939. fs->files.push_back(fi);
  940. } else {
  941. fs->files.insert(idx, fi);
  942. }
  943. cpos = idx;
  944. } else {
  945. //the file exists and it was updated, and was not added in this step.
  946. //this means we must force upon next restart to scan it again, to get proper type and dependencies
  947. late_update_files.insert(p_file);
  948. _save_late_updated_files(); //files need to be updated in the re-scan
  949. }
  950. fs->files[cpos]->type = type;
  951. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  952. fs->files[cpos]->deps = _get_dependencies(p_file);
  953. fs->files[cpos]->import_valid = ResourceLoader::is_import_valid(p_file);
  954. EditorResourcePreview::get_singleton()->call_deferred("check_for_invalidation", p_file);
  955. call_deferred("emit_signal", "filesystem_changed"); //update later
  956. }
  957. void EditorFileSystem::_reimport_file(const String &p_file) {
  958. EditorFileSystemDirectory *fs = NULL;
  959. int cpos = -1;
  960. bool found = _find_file(p_file, &fs, cpos);
  961. ERR_FAIL_COND(!found);
  962. //try to obtain existing params
  963. Map<StringName, Variant> params;
  964. String importer_name;
  965. if (FileAccess::exists(p_file + ".import")) {
  966. //use existing
  967. Ref<ConfigFile> cf;
  968. cf.instance();
  969. Error err = cf->load(p_file + ".import");
  970. if (err == OK) {
  971. List<String> sk;
  972. cf->get_section_keys("params", &sk);
  973. for (List<String>::Element *E = sk.front(); E; E = E->next()) {
  974. params[E->get()] = cf->get_value("params", E->get());
  975. }
  976. importer_name = cf->get_value("remap", "importer");
  977. }
  978. } else {
  979. late_added_files.insert(p_file); //imported files do not call update_file(), but just in case..
  980. }
  981. Ref<ResourceImporter> importer;
  982. bool load_default = false;
  983. //find the importer
  984. if (importer_name != "") {
  985. importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  986. }
  987. if (importer.is_null()) {
  988. //not found by name, find by extension
  989. importer = ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_file.get_extension());
  990. load_default = true;
  991. if (importer.is_null()) {
  992. ERR_PRINT("BUG: File queued for import, but can't be imported!");
  993. ERR_FAIL();
  994. }
  995. }
  996. //mix with default params, in case a parameter is missing
  997. List<ResourceImporter::ImportOption> opts;
  998. importer->get_import_options(&opts);
  999. for (List<ResourceImporter::ImportOption>::Element *E = opts.front(); E; E = E->next()) {
  1000. if (!params.has(E->get().option.name)) { //this one is not present
  1001. params[E->get().option.name] = E->get().default_value;
  1002. }
  1003. }
  1004. if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  1005. //use defaults if exist
  1006. Dictionary d = ProjectSettings::get_singleton()->get("importer_defaults/" + importer->get_importer_name());
  1007. List<Variant> v;
  1008. d.get_key_list(&v);
  1009. for (List<Variant>::Element *E = v.front(); E; E = E->next()) {
  1010. params[E->get()] = d[E->get()];
  1011. }
  1012. }
  1013. //finally, perform import!!
  1014. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
  1015. List<String> import_variants;
  1016. List<String> gen_files;
  1017. Error err = importer->import(p_file, base_path, params, &import_variants, &gen_files);
  1018. if (err != OK) {
  1019. ERR_PRINTS("Error importing: " + p_file);
  1020. }
  1021. //as import is complete, save the .import file
  1022. FileAccess *f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
  1023. ERR_FAIL_COND(!f);
  1024. //write manually, as order matters ([remap] has to go first for performance).
  1025. f->store_line("[remap]");
  1026. f->store_line("");
  1027. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  1028. if (importer->get_resource_type() != "") {
  1029. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  1030. }
  1031. if (err == OK) {
  1032. if (importer->get_save_extension() == "") {
  1033. //no path
  1034. } else if (import_variants.size()) {
  1035. //import with variants
  1036. for (List<String>::Element *E = import_variants.front(); E; E = E->next()) {
  1037. String path = base_path.c_escape() + "." + E->get() + "." + importer->get_save_extension();
  1038. f->store_line("path." + E->get() + "=\"" + path + "\"");
  1039. }
  1040. } else {
  1041. f->store_line("path=\"" + base_path + "." + importer->get_save_extension() + "\"");
  1042. }
  1043. } else {
  1044. f->store_line("valid=false");
  1045. }
  1046. f->store_line("");
  1047. if (gen_files.size()) {
  1048. f->store_line("[gen]");
  1049. Array genf;
  1050. for (List<String>::Element *E = gen_files.front(); E; E = E->next()) {
  1051. genf.push_back(E->get());
  1052. }
  1053. String value;
  1054. VariantWriter::write_to_string(genf, value);
  1055. f->store_line("files=" + value);
  1056. f->store_line("");
  1057. }
  1058. f->store_line("[params]");
  1059. f->store_line("");
  1060. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  1061. for (List<ResourceImporter::ImportOption>::Element *E = opts.front(); E; E = E->next()) {
  1062. String base = E->get().option.name;
  1063. String value;
  1064. VariantWriter::write_to_string(params[base], value);
  1065. f->store_line(base + "=" + value);
  1066. }
  1067. f->close();
  1068. memdelete(f);
  1069. //update modified times, to avoid reimport
  1070. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1071. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  1072. fs->files[cpos]->deps = _get_dependencies(p_file);
  1073. fs->files[cpos]->type = importer->get_resource_type();
  1074. fs->files[cpos]->import_valid = ResourceLoader::is_import_valid(p_file);
  1075. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  1076. //to reload properly
  1077. if (ResourceCache::has(p_file)) {
  1078. Resource *r = ResourceCache::get(p_file);
  1079. if (r->get_import_path() != String()) {
  1080. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
  1081. r->set_import_path(dst_path);
  1082. r->set_import_last_modified_time(0);
  1083. }
  1084. }
  1085. }
  1086. void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
  1087. importing = true;
  1088. EditorProgress pr("reimport", TTR("(Re)Importing Assets"), p_files.size());
  1089. Vector<ImportFile> files;
  1090. for (int i = 0; i < p_files.size(); i++) {
  1091. ImportFile ifile;
  1092. ifile.path = p_files[i];
  1093. ifile.order = ResourceFormatImporter::get_singleton()->get_import_order(p_files[i]);
  1094. files.push_back(ifile);
  1095. }
  1096. files.sort();
  1097. for (int i = 0; i < files.size(); i++) {
  1098. pr.step(files[i].path.get_file(), i);
  1099. _reimport_file(files[i].path);
  1100. }
  1101. _save_filesystem_cache();
  1102. importing = false;
  1103. if (!is_scanning()) {
  1104. emit_signal("filesystem_changed");
  1105. }
  1106. emit_signal("resources_reimported", p_files);
  1107. }
  1108. void EditorFileSystem::_bind_methods() {
  1109. ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
  1110. ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
  1111. ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
  1112. ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
  1113. ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
  1114. ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
  1115. ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
  1116. ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
  1117. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  1118. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  1119. ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::POOL_STRING_ARRAY, "resources")));
  1120. }
  1121. void EditorFileSystem::_update_extensions() {
  1122. valid_extensions.clear();
  1123. import_extensions.clear();
  1124. List<String> extensionsl;
  1125. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  1126. for (List<String>::Element *E = extensionsl.front(); E; E = E->next()) {
  1127. valid_extensions.insert(E->get());
  1128. }
  1129. extensionsl.clear();
  1130. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  1131. for (List<String>::Element *E = extensionsl.front(); E; E = E->next()) {
  1132. import_extensions.insert(E->get());
  1133. }
  1134. }
  1135. EditorFileSystem::EditorFileSystem() {
  1136. reimport_on_missing_imported_files = GLOBAL_DEF("editor/reimport_missing_imported_files", true);
  1137. singleton = this;
  1138. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  1139. filesystem->parent = NULL;
  1140. thread = NULL;
  1141. scanning = false;
  1142. importing = false;
  1143. use_threads = true;
  1144. thread_sources = NULL;
  1145. new_filesystem = NULL;
  1146. abort_scan = false;
  1147. scanning_changes = false;
  1148. scanning_changes_done = false;
  1149. ResourceSaver::set_save_callback(_resource_saved);
  1150. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1151. if (da->change_dir("res://.import") != OK) {
  1152. da->make_dir("res://.import");
  1153. }
  1154. memdelete(da);
  1155. scan_total = 0;
  1156. }
  1157. EditorFileSystem::~EditorFileSystem() {
  1158. }