editor_file_system.cpp 81 KB

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