editor_file_system.cpp 73 KB

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