editor_file_system.cpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  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 verified
  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. if (thread_sources.is_started()) {
  1055. thread_sources.wait_to_finish();
  1056. }
  1057. bool changed = _update_scan_actions();
  1058. _update_pending_script_classes();
  1059. if (changed) {
  1060. emit_signal(SNAME("filesystem_changed"));
  1061. }
  1062. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1063. first_scan = false;
  1064. scanning_changes = false; // Changed to false here to prevent recursive triggering of scan thread.
  1065. done_importing = true;
  1066. }
  1067. } else if (!scanning && thread.is_started()) {
  1068. set_process(false);
  1069. if (filesystem) {
  1070. memdelete(filesystem);
  1071. }
  1072. filesystem = new_filesystem;
  1073. new_filesystem = nullptr;
  1074. thread.wait_to_finish();
  1075. _update_scan_actions();
  1076. _update_pending_script_classes();
  1077. emit_signal(SNAME("filesystem_changed"));
  1078. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1079. first_scan = false;
  1080. }
  1081. if (done_importing && scan_changes_pending) {
  1082. scan_changes_pending = false;
  1083. scan_changes();
  1084. }
  1085. prevent_recursive_process_hack = false;
  1086. }
  1087. } break;
  1088. }
  1089. }
  1090. bool EditorFileSystem::is_scanning() const {
  1091. return scanning || scanning_changes;
  1092. }
  1093. float EditorFileSystem::get_scanning_progress() const {
  1094. return scan_total;
  1095. }
  1096. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  1097. return filesystem;
  1098. }
  1099. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file) {
  1100. if (!p_dir) {
  1101. return; //none
  1102. }
  1103. p_file->store_line("::" + p_dir->get_path() + "::" + String::num(p_dir->modified_time));
  1104. for (int i = 0; i < p_dir->files.size(); i++) {
  1105. if (!p_dir->files[i]->import_group_file.is_empty()) {
  1106. group_file_cache.insert(p_dir->files[i]->import_group_file);
  1107. }
  1108. String type = p_dir->files[i]->type;
  1109. if (p_dir->files[i]->resource_script_class) {
  1110. type += "/" + String(p_dir->files[i]->resource_script_class);
  1111. }
  1112. 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;
  1113. s += "::";
  1114. for (int j = 0; j < p_dir->files[i]->deps.size(); j++) {
  1115. if (j > 0) {
  1116. s += "<>";
  1117. }
  1118. s += p_dir->files[i]->deps[j];
  1119. }
  1120. p_file->store_line(s);
  1121. }
  1122. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1123. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  1124. }
  1125. }
  1126. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  1127. //todo make faster
  1128. if (!filesystem || scanning) {
  1129. return false;
  1130. }
  1131. String f = ProjectSettings::get_singleton()->localize_path(p_file);
  1132. if (!f.begins_with("res://")) {
  1133. return false;
  1134. }
  1135. f = f.substr(6, f.length());
  1136. f = f.replace("\\", "/");
  1137. Vector<String> path = f.split("/");
  1138. if (path.size() == 0) {
  1139. return false;
  1140. }
  1141. String file = path[path.size() - 1];
  1142. path.resize(path.size() - 1);
  1143. EditorFileSystemDirectory *fs = filesystem;
  1144. for (int i = 0; i < path.size(); i++) {
  1145. if (path[i].begins_with(".")) {
  1146. return false;
  1147. }
  1148. int idx = -1;
  1149. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1150. if (fs->get_subdir(j)->get_name() == path[i]) {
  1151. idx = j;
  1152. break;
  1153. }
  1154. }
  1155. if (idx == -1) {
  1156. //does not exist, create i guess?
  1157. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  1158. efsd->name = path[i];
  1159. efsd->parent = fs;
  1160. int idx2 = 0;
  1161. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1162. if (efsd->name.naturalnocasecmp_to(fs->get_subdir(j)->get_name()) < 0) {
  1163. break;
  1164. }
  1165. idx2++;
  1166. }
  1167. if (idx2 == fs->get_subdir_count()) {
  1168. fs->subdirs.push_back(efsd);
  1169. } else {
  1170. fs->subdirs.insert(idx2, efsd);
  1171. }
  1172. fs = efsd;
  1173. } else {
  1174. fs = fs->get_subdir(idx);
  1175. }
  1176. }
  1177. int cpos = -1;
  1178. for (int i = 0; i < fs->files.size(); i++) {
  1179. if (fs->files[i]->file == file) {
  1180. cpos = i;
  1181. break;
  1182. }
  1183. }
  1184. r_file_pos = cpos;
  1185. *r_d = fs;
  1186. return cpos != -1;
  1187. }
  1188. String EditorFileSystem::get_file_type(const String &p_file) const {
  1189. EditorFileSystemDirectory *fs = nullptr;
  1190. int cpos = -1;
  1191. if (!_find_file(p_file, &fs, cpos)) {
  1192. return "";
  1193. }
  1194. return fs->files[cpos]->type;
  1195. }
  1196. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  1197. if (!filesystem || scanning) {
  1198. return nullptr;
  1199. }
  1200. EditorFileSystemDirectory *fs = nullptr;
  1201. int cpos = -1;
  1202. if (!_find_file(p_file, &fs, cpos)) {
  1203. return nullptr;
  1204. }
  1205. if (r_index) {
  1206. *r_index = cpos;
  1207. }
  1208. return fs;
  1209. }
  1210. EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
  1211. if (!filesystem || scanning) {
  1212. return nullptr;
  1213. }
  1214. String f = ProjectSettings::get_singleton()->localize_path(p_path);
  1215. if (!f.begins_with("res://")) {
  1216. return nullptr;
  1217. }
  1218. f = f.substr(6, f.length());
  1219. f = f.replace("\\", "/");
  1220. if (f.is_empty()) {
  1221. return filesystem;
  1222. }
  1223. if (f.ends_with("/")) {
  1224. f = f.substr(0, f.length() - 1);
  1225. }
  1226. Vector<String> path = f.split("/");
  1227. if (path.size() == 0) {
  1228. return nullptr;
  1229. }
  1230. EditorFileSystemDirectory *fs = filesystem;
  1231. for (int i = 0; i < path.size(); i++) {
  1232. int idx = -1;
  1233. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1234. if (fs->get_subdir(j)->get_name() == path[i]) {
  1235. idx = j;
  1236. break;
  1237. }
  1238. }
  1239. if (idx == -1) {
  1240. return nullptr;
  1241. } else {
  1242. fs = fs->get_subdir(idx);
  1243. }
  1244. }
  1245. return fs;
  1246. }
  1247. void EditorFileSystem::_save_late_updated_files() {
  1248. //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
  1249. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  1250. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  1251. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  1252. for (const String &E : late_update_files) {
  1253. f->store_line(E);
  1254. }
  1255. }
  1256. Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
  1257. // Avoid error spam on first opening of a not yet imported project by treating the following situation
  1258. // as a benign one, not letting the file open error happen: the resource is of an importable type but
  1259. // it has not been imported yet.
  1260. if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
  1261. const String &internal_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
  1262. if (!internal_path.is_empty() && !FileAccess::exists(internal_path)) { // If path is empty (error), keep the code flow to the error.
  1263. return Vector<String>();
  1264. }
  1265. }
  1266. List<String> deps;
  1267. ResourceLoader::get_dependencies(p_path, &deps);
  1268. Vector<String> ret;
  1269. for (const String &E : deps) {
  1270. ret.push_back(E);
  1271. }
  1272. return ret;
  1273. }
  1274. String EditorFileSystem::_get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const {
  1275. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1276. if (ScriptServer::get_language(i)->handles_global_class_type(p_type)) {
  1277. String global_name;
  1278. String extends;
  1279. String icon_path;
  1280. global_name = ScriptServer::get_language(i)->get_global_class_name(p_path, &extends, &icon_path);
  1281. *r_extends = extends;
  1282. *r_icon_path = icon_path;
  1283. return global_name;
  1284. }
  1285. }
  1286. *r_extends = String();
  1287. *r_icon_path = String();
  1288. return String();
  1289. }
  1290. void EditorFileSystem::_update_script_classes() {
  1291. update_script_mutex.lock();
  1292. for (const String &path : update_script_paths) {
  1293. ScriptServer::remove_global_class_by_path(path); // First remove, just in case it changed
  1294. int index = -1;
  1295. EditorFileSystemDirectory *efd = find_file(path, &index);
  1296. if (!efd || index < 0) {
  1297. // The file was removed
  1298. continue;
  1299. }
  1300. if (!efd->files[index]->script_class_name.is_empty()) {
  1301. String lang;
  1302. for (int j = 0; j < ScriptServer::get_language_count(); j++) {
  1303. if (ScriptServer::get_language(j)->handles_global_class_type(efd->files[index]->type)) {
  1304. lang = ScriptServer::get_language(j)->get_name();
  1305. }
  1306. }
  1307. if (lang.is_empty()) {
  1308. continue; // No lang found that can handle this global class
  1309. }
  1310. ScriptServer::add_global_class(efd->files[index]->script_class_name, efd->files[index]->script_class_extends, lang, path);
  1311. EditorNode::get_editor_data().script_class_set_icon_path(efd->files[index]->script_class_name, efd->files[index]->script_class_icon_path);
  1312. EditorNode::get_editor_data().script_class_set_name(path, efd->files[index]->script_class_name);
  1313. }
  1314. }
  1315. // Parse documentation second, as it requires the class names to be correct and registered
  1316. for (const String &path : update_script_paths) {
  1317. int index = -1;
  1318. EditorFileSystemDirectory *efd = find_file(path, &index);
  1319. if (!efd || index < 0) {
  1320. // The file was removed
  1321. continue;
  1322. }
  1323. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1324. ScriptLanguage *lang = ScriptServer::get_language(i);
  1325. if (lang->supports_documentation() && efd->files[index]->type == lang->get_type()) {
  1326. Ref<Script> scr = ResourceLoader::load(path);
  1327. if (scr.is_null()) {
  1328. continue;
  1329. }
  1330. Vector<DocData::ClassDoc> docs = scr->get_documentation();
  1331. for (int j = 0; j < docs.size(); j++) {
  1332. EditorHelp::get_doc_data()->add_doc(docs[j]);
  1333. }
  1334. }
  1335. }
  1336. }
  1337. update_script_paths.clear();
  1338. update_script_mutex.unlock();
  1339. ScriptServer::save_global_classes();
  1340. EditorNode::get_editor_data().script_class_save_icon_paths();
  1341. emit_signal("script_classes_updated");
  1342. // Rescan custom loaders and savers.
  1343. // Doing the following here because the `filesystem_changed` signal fires multiple times and isn't always followed by script classes update.
  1344. // So I thought it's better to do this when script classes really get updated
  1345. ResourceLoader::remove_custom_loaders();
  1346. ResourceLoader::add_custom_loaders();
  1347. ResourceSaver::remove_custom_savers();
  1348. ResourceSaver::add_custom_savers();
  1349. }
  1350. void EditorFileSystem::_update_pending_script_classes() {
  1351. if (!update_script_paths.is_empty()) {
  1352. _update_script_classes();
  1353. } else {
  1354. // In case the class cache file was removed somehow, regenerate it.
  1355. if (!FileAccess::exists(ScriptServer::get_global_class_cache_file_path())) {
  1356. ScriptServer::save_global_classes();
  1357. }
  1358. }
  1359. }
  1360. void EditorFileSystem::_queue_update_script_class(const String &p_path) {
  1361. update_script_mutex.lock();
  1362. update_script_paths.insert(p_path);
  1363. update_script_mutex.unlock();
  1364. }
  1365. void EditorFileSystem::update_file(const String &p_file) {
  1366. ERR_FAIL_COND(p_file.is_empty());
  1367. EditorFileSystemDirectory *fs = nullptr;
  1368. int cpos = -1;
  1369. if (!_find_file(p_file, &fs, cpos)) {
  1370. if (!fs) {
  1371. return;
  1372. }
  1373. }
  1374. if (!FileAccess::exists(p_file)) {
  1375. //was removed
  1376. _delete_internal_files(p_file);
  1377. if (cpos != -1) { // Might've never been part of the editor file system (*.* files deleted in Open dialog).
  1378. if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  1379. if (ResourceUID::get_singleton()->has_id(fs->files[cpos]->uid)) {
  1380. ResourceUID::get_singleton()->remove_id(fs->files[cpos]->uid);
  1381. }
  1382. }
  1383. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
  1384. _queue_update_script_class(p_file);
  1385. }
  1386. memdelete(fs->files[cpos]);
  1387. fs->files.remove_at(cpos);
  1388. }
  1389. _update_pending_script_classes();
  1390. call_deferred(SNAME("emit_signal"), "filesystem_changed"); //update later
  1391. return;
  1392. }
  1393. String type = ResourceLoader::get_resource_type(p_file);
  1394. if (type.is_empty() && textfile_extensions.has(p_file.get_extension())) {
  1395. type = "TextFile";
  1396. }
  1397. String script_class = ResourceLoader::get_resource_script_class(p_file);
  1398. ResourceUID::ID uid = ResourceLoader::get_resource_uid(p_file);
  1399. if (cpos == -1) {
  1400. // The file did not exist, it was added.
  1401. int idx = 0;
  1402. String file_name = p_file.get_file();
  1403. for (int i = 0; i < fs->files.size(); i++) {
  1404. if (p_file.naturalnocasecmp_to(fs->files[i]->file) < 0) {
  1405. break;
  1406. }
  1407. idx++;
  1408. }
  1409. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1410. fi->file = file_name;
  1411. fi->import_modified_time = 0;
  1412. fi->import_valid = type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  1413. if (idx == fs->files.size()) {
  1414. fs->files.push_back(fi);
  1415. } else {
  1416. fs->files.insert(idx, fi);
  1417. }
  1418. cpos = idx;
  1419. } else {
  1420. //the file exists and it was updated, and was not added in this step.
  1421. //this means we must force upon next restart to scan it again, to get proper type and dependencies
  1422. late_update_files.insert(p_file);
  1423. _save_late_updated_files(); //files need to be updated in the re-scan
  1424. }
  1425. fs->files[cpos]->type = type;
  1426. fs->files[cpos]->resource_script_class = script_class;
  1427. fs->files[cpos]->uid = uid;
  1428. 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);
  1429. fs->files[cpos]->import_group_file = ResourceLoader::get_import_group_file(p_file);
  1430. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1431. fs->files[cpos]->deps = _get_dependencies(p_file);
  1432. fs->files[cpos]->import_valid = type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  1433. if (uid != ResourceUID::INVALID_ID) {
  1434. if (ResourceUID::get_singleton()->has_id(uid)) {
  1435. ResourceUID::get_singleton()->set_id(uid, p_file);
  1436. } else {
  1437. ResourceUID::get_singleton()->add_id(uid, p_file);
  1438. }
  1439. ResourceUID::get_singleton()->update_cache();
  1440. }
  1441. // Update preview
  1442. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1443. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
  1444. _queue_update_script_class(p_file);
  1445. }
  1446. _update_pending_script_classes();
  1447. call_deferred(SNAME("emit_signal"), "filesystem_changed"); //update later
  1448. }
  1449. HashSet<String> EditorFileSystem::get_valid_extensions() const {
  1450. return valid_extensions;
  1451. }
  1452. Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) {
  1453. String importer_name;
  1454. HashMap<String, HashMap<StringName, Variant>> source_file_options;
  1455. HashMap<String, ResourceUID::ID> uids;
  1456. HashMap<String, String> base_paths;
  1457. for (int i = 0; i < p_files.size(); i++) {
  1458. Ref<ConfigFile> config;
  1459. config.instantiate();
  1460. Error err = config->load(p_files[i] + ".import");
  1461. ERR_CONTINUE(err != OK);
  1462. ERR_CONTINUE(!config->has_section_key("remap", "importer"));
  1463. String file_importer_name = config->get_value("remap", "importer");
  1464. ERR_CONTINUE(file_importer_name.is_empty());
  1465. if (!importer_name.is_empty() && importer_name != file_importer_name) {
  1466. EditorNode::get_singleton()->show_warning(vformat(TTR("There are multiple importers for different types pointing to file %s, import aborted"), p_group_file));
  1467. ERR_FAIL_V(ERR_FILE_CORRUPT);
  1468. }
  1469. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  1470. if (config->has_section_key("remap", "uid")) {
  1471. String uidt = config->get_value("remap", "uid");
  1472. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  1473. }
  1474. uids[p_files[i]] = uid;
  1475. source_file_options[p_files[i]] = HashMap<StringName, Variant>();
  1476. importer_name = file_importer_name;
  1477. if (importer_name == "keep") {
  1478. continue; //do nothing
  1479. }
  1480. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1481. ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT);
  1482. List<ResourceImporter::ImportOption> options;
  1483. importer->get_import_options(p_files[i], &options);
  1484. //set default values
  1485. for (const ResourceImporter::ImportOption &E : options) {
  1486. source_file_options[p_files[i]][E.option.name] = E.default_value;
  1487. }
  1488. if (config->has_section("params")) {
  1489. List<String> sk;
  1490. config->get_section_keys("params", &sk);
  1491. for (const String &param : sk) {
  1492. Variant value = config->get_value("params", param);
  1493. //override with whatever is in file
  1494. source_file_options[p_files[i]][param] = value;
  1495. }
  1496. }
  1497. base_paths[p_files[i]] = ResourceFormatImporter::get_singleton()->get_import_base_path(p_files[i]);
  1498. }
  1499. if (importer_name == "keep") {
  1500. return OK; // (do nothing)
  1501. }
  1502. ERR_FAIL_COND_V(importer_name.is_empty(), ERR_UNCONFIGURED);
  1503. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1504. Error err = importer->import_group_file(p_group_file, source_file_options, base_paths);
  1505. //all went well, overwrite config files with proper remaps and md5s
  1506. for (const KeyValue<String, HashMap<StringName, Variant>> &E : source_file_options) {
  1507. const String &file = E.key;
  1508. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(file);
  1509. Vector<String> dest_paths;
  1510. ResourceUID::ID uid = uids[file];
  1511. {
  1512. Ref<FileAccess> f = FileAccess::open(file + ".import", FileAccess::WRITE);
  1513. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'.");
  1514. //write manually, as order matters ([remap] has to go first for performance).
  1515. f->store_line("[remap]");
  1516. f->store_line("");
  1517. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  1518. int version = importer->get_format_version();
  1519. if (version > 0) {
  1520. f->store_line("importer_version=" + itos(version));
  1521. }
  1522. if (!importer->get_resource_type().is_empty()) {
  1523. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  1524. }
  1525. if (uid == ResourceUID::INVALID_ID) {
  1526. uid = ResourceUID::get_singleton()->create_id();
  1527. }
  1528. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
  1529. if (err == OK) {
  1530. String path = base_path + "." + importer->get_save_extension();
  1531. f->store_line("path=\"" + path + "\"");
  1532. dest_paths.push_back(path);
  1533. }
  1534. f->store_line("group_file=" + Variant(p_group_file).get_construct_string());
  1535. if (err == OK) {
  1536. f->store_line("valid=true");
  1537. } else {
  1538. f->store_line("valid=false");
  1539. }
  1540. f->store_line("[deps]\n");
  1541. f->store_line("");
  1542. f->store_line("source_file=" + Variant(file).get_construct_string());
  1543. if (dest_paths.size()) {
  1544. Array dp;
  1545. for (int i = 0; i < dest_paths.size(); i++) {
  1546. dp.push_back(dest_paths[i]);
  1547. }
  1548. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  1549. }
  1550. f->store_line("[params]");
  1551. f->store_line("");
  1552. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  1553. List<ResourceImporter::ImportOption> options;
  1554. importer->get_import_options(file, &options);
  1555. //set default values
  1556. for (const ResourceImporter::ImportOption &F : options) {
  1557. String base = F.option.name;
  1558. Variant v = F.default_value;
  1559. if (source_file_options[file].has(base)) {
  1560. v = source_file_options[file][base];
  1561. }
  1562. String value;
  1563. VariantWriter::write_to_string(v, value);
  1564. f->store_line(base + "=" + value);
  1565. }
  1566. }
  1567. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  1568. {
  1569. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  1570. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  1571. md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\"");
  1572. if (dest_paths.size()) {
  1573. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  1574. }
  1575. }
  1576. EditorFileSystemDirectory *fs = nullptr;
  1577. int cpos = -1;
  1578. bool found = _find_file(file, &fs, cpos);
  1579. ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, "Can't find file '" + file + "'.");
  1580. //update modified times, to avoid reimport
  1581. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  1582. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(file + ".import");
  1583. fs->files[cpos]->deps = _get_dependencies(file);
  1584. fs->files[cpos]->uid = uid;
  1585. fs->files[cpos]->type = importer->get_resource_type();
  1586. if (fs->files[cpos]->type == "" && textfile_extensions.has(file.get_extension())) {
  1587. fs->files[cpos]->type = "TextFile";
  1588. }
  1589. fs->files[cpos]->import_valid = err == OK;
  1590. if (ResourceUID::get_singleton()->has_id(uid)) {
  1591. ResourceUID::get_singleton()->set_id(uid, file);
  1592. } else {
  1593. ResourceUID::get_singleton()->add_id(uid, file);
  1594. }
  1595. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  1596. //to reload properly
  1597. Ref<Resource> r = ResourceCache::get_ref(file);
  1598. if (r.is_valid()) {
  1599. if (!r->get_import_path().is_empty()) {
  1600. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(file);
  1601. r->set_import_path(dst_path);
  1602. r->set_import_last_modified_time(0);
  1603. }
  1604. }
  1605. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  1606. }
  1607. return err;
  1608. }
  1609. Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant *p_generator_parameters) {
  1610. EditorFileSystemDirectory *fs = nullptr;
  1611. int cpos = -1;
  1612. bool found = _find_file(p_file, &fs, cpos);
  1613. ERR_FAIL_COND_V_MSG(!found, ERR_FILE_NOT_FOUND, "Can't find file '" + p_file + "'.");
  1614. //try to obtain existing params
  1615. HashMap<StringName, Variant> params = p_custom_options;
  1616. String importer_name; //empty by default though
  1617. if (!p_custom_importer.is_empty()) {
  1618. importer_name = p_custom_importer;
  1619. }
  1620. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  1621. Variant generator_parameters;
  1622. if (p_generator_parameters) {
  1623. generator_parameters = *p_generator_parameters;
  1624. }
  1625. if (FileAccess::exists(p_file + ".import")) {
  1626. //use existing
  1627. Ref<ConfigFile> cf;
  1628. cf.instantiate();
  1629. Error err = cf->load(p_file + ".import");
  1630. if (err == OK) {
  1631. if (cf->has_section("params")) {
  1632. List<String> sk;
  1633. cf->get_section_keys("params", &sk);
  1634. for (const String &E : sk) {
  1635. if (!params.has(E)) {
  1636. params[E] = cf->get_value("params", E);
  1637. }
  1638. }
  1639. }
  1640. if (cf->has_section("remap")) {
  1641. if (p_custom_importer.is_empty()) {
  1642. importer_name = cf->get_value("remap", "importer");
  1643. }
  1644. if (cf->has_section_key("remap", "uid")) {
  1645. String uidt = cf->get_value("remap", "uid");
  1646. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  1647. }
  1648. if (!p_generator_parameters) {
  1649. if (cf->has_section_key("remap", "generator_parameters")) {
  1650. generator_parameters = cf->get_value("remap", "generator_parameters");
  1651. }
  1652. }
  1653. }
  1654. }
  1655. }
  1656. if (importer_name == "keep") {
  1657. //keep files, do nothing.
  1658. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1659. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  1660. fs->files[cpos]->deps.clear();
  1661. fs->files[cpos]->type = "";
  1662. fs->files[cpos]->import_valid = false;
  1663. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1664. return OK;
  1665. }
  1666. Ref<ResourceImporter> importer;
  1667. bool load_default = false;
  1668. //find the importer
  1669. if (!importer_name.is_empty()) {
  1670. importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  1671. }
  1672. if (importer.is_null()) {
  1673. //not found by name, find by extension
  1674. importer = ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_file.get_extension());
  1675. load_default = true;
  1676. if (importer.is_null()) {
  1677. 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.");
  1678. }
  1679. }
  1680. //mix with default params, in case a parameter is missing
  1681. List<ResourceImporter::ImportOption> opts;
  1682. importer->get_import_options(p_file, &opts);
  1683. for (const ResourceImporter::ImportOption &E : opts) {
  1684. if (!params.has(E.option.name)) { //this one is not present
  1685. params[E.option.name] = E.default_value;
  1686. }
  1687. }
  1688. if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  1689. //use defaults if exist
  1690. Dictionary d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
  1691. List<Variant> v;
  1692. d.get_key_list(&v);
  1693. for (const Variant &E : v) {
  1694. params[E] = d[E];
  1695. }
  1696. }
  1697. //finally, perform import!!
  1698. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
  1699. List<String> import_variants;
  1700. List<String> gen_files;
  1701. Variant meta;
  1702. Error err = importer->import(p_file, base_path, params, &import_variants, &gen_files, &meta);
  1703. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_UNRECOGNIZED, "Error importing '" + p_file + "'.");
  1704. //as import is complete, save the .import file
  1705. Vector<String> dest_paths;
  1706. {
  1707. Ref<FileAccess> f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
  1708. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open file from path '" + p_file + ".import'.");
  1709. //write manually, as order matters ([remap] has to go first for performance).
  1710. f->store_line("[remap]");
  1711. f->store_line("");
  1712. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  1713. int version = importer->get_format_version();
  1714. if (version > 0) {
  1715. f->store_line("importer_version=" + itos(version));
  1716. }
  1717. if (!importer->get_resource_type().is_empty()) {
  1718. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  1719. }
  1720. if (uid == ResourceUID::INVALID_ID) {
  1721. uid = ResourceUID::get_singleton()->create_id();
  1722. }
  1723. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); //store in readable format
  1724. if (err == OK) {
  1725. if (importer->get_save_extension().is_empty()) {
  1726. //no path
  1727. } else if (import_variants.size()) {
  1728. //import with variants
  1729. for (const String &E : import_variants) {
  1730. String path = base_path.c_escape() + "." + E + "." + importer->get_save_extension();
  1731. f->store_line("path." + E + "=\"" + path + "\"");
  1732. dest_paths.push_back(path);
  1733. }
  1734. } else {
  1735. String path = base_path + "." + importer->get_save_extension();
  1736. f->store_line("path=\"" + path + "\"");
  1737. dest_paths.push_back(path);
  1738. }
  1739. } else {
  1740. f->store_line("valid=false");
  1741. }
  1742. if (meta != Variant()) {
  1743. f->store_line("metadata=" + meta.get_construct_string());
  1744. }
  1745. if (generator_parameters != Variant()) {
  1746. f->store_line("generator_parameters=" + generator_parameters.get_construct_string());
  1747. }
  1748. f->store_line("");
  1749. f->store_line("[deps]\n");
  1750. if (gen_files.size()) {
  1751. Array genf;
  1752. for (const String &E : gen_files) {
  1753. genf.push_back(E);
  1754. dest_paths.push_back(E);
  1755. }
  1756. String value;
  1757. VariantWriter::write_to_string(genf, value);
  1758. f->store_line("files=" + value);
  1759. f->store_line("");
  1760. }
  1761. f->store_line("source_file=" + Variant(p_file).get_construct_string());
  1762. if (dest_paths.size()) {
  1763. Array dp;
  1764. for (int i = 0; i < dest_paths.size(); i++) {
  1765. dp.push_back(dest_paths[i]);
  1766. }
  1767. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  1768. }
  1769. f->store_line("[params]");
  1770. f->store_line("");
  1771. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  1772. for (const ResourceImporter::ImportOption &E : opts) {
  1773. String base = E.option.name;
  1774. String value;
  1775. VariantWriter::write_to_string(params[base], value);
  1776. f->store_line(base + "=" + value);
  1777. }
  1778. }
  1779. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  1780. {
  1781. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  1782. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  1783. md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"");
  1784. if (dest_paths.size()) {
  1785. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  1786. }
  1787. }
  1788. // Update cpos, newly created files could've changed the index of the reimported p_file.
  1789. _find_file(p_file, &fs, cpos);
  1790. //update modified times, to avoid reimport
  1791. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  1792. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  1793. fs->files[cpos]->deps = _get_dependencies(p_file);
  1794. fs->files[cpos]->type = importer->get_resource_type();
  1795. fs->files[cpos]->uid = uid;
  1796. fs->files[cpos]->import_valid = fs->files[cpos]->type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  1797. if (ResourceUID::get_singleton()->has_id(uid)) {
  1798. ResourceUID::get_singleton()->set_id(uid, p_file);
  1799. } else {
  1800. ResourceUID::get_singleton()->add_id(uid, p_file);
  1801. }
  1802. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  1803. //to reload properly
  1804. Ref<Resource> r = ResourceCache::get_ref(p_file);
  1805. if (r.is_valid()) {
  1806. if (!r->get_import_path().is_empty()) {
  1807. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
  1808. r->set_import_path(dst_path);
  1809. r->set_import_last_modified_time(0);
  1810. }
  1811. }
  1812. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1813. return OK;
  1814. }
  1815. void EditorFileSystem::_find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport) {
  1816. int fc = efd->files.size();
  1817. const EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptr();
  1818. for (int i = 0; i < fc; i++) {
  1819. if (groups_to_reimport.has(files[i]->import_group_file)) {
  1820. if (!group_files.has(files[i]->import_group_file)) {
  1821. group_files[files[i]->import_group_file] = Vector<String>();
  1822. }
  1823. group_files[files[i]->import_group_file].push_back(efd->get_file_path(i));
  1824. }
  1825. }
  1826. for (int i = 0; i < efd->get_subdir_count(); i++) {
  1827. _find_group_files(efd->get_subdir(i), group_files, groups_to_reimport);
  1828. }
  1829. }
  1830. void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params) {
  1831. _reimport_file(p_file, p_custom_params, p_importer);
  1832. // Emit the resource_reimported signal for the single file we just reimported.
  1833. Vector<String> reloads;
  1834. reloads.append(p_file);
  1835. emit_signal(SNAME("resources_reimported"), reloads);
  1836. }
  1837. void EditorFileSystem::_reimport_thread(uint32_t p_index, ImportThreadData *p_import_data) {
  1838. p_import_data->max_index = MAX(p_import_data->reimport_from + int(p_index), p_import_data->max_index);
  1839. _reimport_file(p_import_data->reimport_files[p_import_data->reimport_from + p_index].path);
  1840. }
  1841. void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
  1842. ERR_FAIL_COND_MSG(importing, "Attempted to call reimport_files() recursively, this is not allowed.");
  1843. importing = true;
  1844. Vector<String> reloads;
  1845. EditorProgress pr("reimport", TTR("(Re)Importing Assets"), p_files.size());
  1846. Vector<ImportFile> reimport_files;
  1847. HashSet<String> groups_to_reimport;
  1848. for (int i = 0; i < p_files.size(); i++) {
  1849. String file = p_files[i];
  1850. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(file);
  1851. if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
  1852. file = ResourceUID::get_singleton()->get_id_path(uid);
  1853. }
  1854. String group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(file);
  1855. if (group_file_cache.has(file)) {
  1856. // Maybe the file itself is a group!
  1857. groups_to_reimport.insert(file);
  1858. // Groups do not belong to groups.
  1859. group_file = String();
  1860. } else if (groups_to_reimport.has(file)) {
  1861. // Groups do not belong to groups.
  1862. group_file = String();
  1863. } else if (!group_file.is_empty()) {
  1864. // It's a group file, add group to import and skip this file.
  1865. groups_to_reimport.insert(group_file);
  1866. } else {
  1867. // It's a regular file.
  1868. ImportFile ifile;
  1869. ifile.path = file;
  1870. ResourceFormatImporter::get_singleton()->get_import_order_threads_and_importer(file, ifile.order, ifile.threaded, ifile.importer);
  1871. reloads.push_back(file);
  1872. reimport_files.push_back(ifile);
  1873. }
  1874. // Group may have changed, so also update group reference.
  1875. EditorFileSystemDirectory *fs = nullptr;
  1876. int cpos = -1;
  1877. if (_find_file(file, &fs, cpos)) {
  1878. fs->files.write[cpos]->import_group_file = group_file;
  1879. }
  1880. }
  1881. reimport_files.sort();
  1882. bool use_multiple_threads = GLOBAL_GET("editor/import/use_multiple_threads");
  1883. int from = 0;
  1884. for (int i = 0; i < reimport_files.size(); i++) {
  1885. if (groups_to_reimport.has(reimport_files[i].path)) {
  1886. continue;
  1887. }
  1888. if (use_multiple_threads && reimport_files[i].threaded) {
  1889. if (i + 1 == reimport_files.size() || reimport_files[i + 1].importer != reimport_files[from].importer) {
  1890. if (from - i == 0) {
  1891. // Single file, do not use threads.
  1892. pr.step(reimport_files[i].path.get_file(), i);
  1893. _reimport_file(reimport_files[i].path);
  1894. } else {
  1895. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(reimport_files[from].importer);
  1896. ERR_CONTINUE(!importer.is_valid());
  1897. importer->import_threaded_begin();
  1898. ImportThreadData tdata;
  1899. tdata.max_index = from;
  1900. tdata.reimport_from = from;
  1901. tdata.reimport_files = reimport_files.ptr();
  1902. 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));
  1903. int current_index = from - 1;
  1904. do {
  1905. if (current_index < tdata.max_index) {
  1906. current_index = tdata.max_index;
  1907. pr.step(reimport_files[current_index].path.get_file(), current_index);
  1908. }
  1909. OS::get_singleton()->delay_usec(1);
  1910. } while (!WorkerThreadPool::get_singleton()->is_group_task_completed(group_task));
  1911. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  1912. importer->import_threaded_end();
  1913. }
  1914. from = i + 1;
  1915. }
  1916. } else {
  1917. pr.step(reimport_files[i].path.get_file(), i);
  1918. _reimport_file(reimport_files[i].path);
  1919. }
  1920. }
  1921. // Reimport groups.
  1922. from = reimport_files.size();
  1923. if (groups_to_reimport.size()) {
  1924. HashMap<String, Vector<String>> group_files;
  1925. _find_group_files(filesystem, group_files, groups_to_reimport);
  1926. for (const KeyValue<String, Vector<String>> &E : group_files) {
  1927. pr.step(E.key.get_file(), from++);
  1928. Error err = _reimport_group(E.key, E.value);
  1929. reloads.push_back(E.key);
  1930. reloads.append_array(E.value);
  1931. if (err == OK) {
  1932. _reimport_file(E.key);
  1933. }
  1934. }
  1935. }
  1936. ResourceUID::get_singleton()->update_cache(); // After reimporting, update the cache.
  1937. _save_filesystem_cache();
  1938. _update_pending_script_classes();
  1939. importing = false;
  1940. if (!is_scanning()) {
  1941. emit_signal(SNAME("filesystem_changed"));
  1942. }
  1943. emit_signal(SNAME("resources_reimported"), reloads);
  1944. }
  1945. Error EditorFileSystem::reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
  1946. ERR_FAIL_COND_V_MSG(!importing, ERR_INVALID_PARAMETER, "Can only append files to import during a current reimport process.");
  1947. Error ret = _reimport_file(p_file, p_custom_options, p_custom_importer, &p_generator_parameters);
  1948. // Emit the resource_reimported signal for the single file we just reimported.
  1949. Vector<String> reloads;
  1950. reloads.append(p_file);
  1951. emit_signal(SNAME("resources_reimported"), reloads);
  1952. return ret;
  1953. }
  1954. Error EditorFileSystem::_resource_import(const String &p_path) {
  1955. Vector<String> files;
  1956. files.push_back(p_path);
  1957. singleton->update_file(p_path);
  1958. singleton->reimport_files(files);
  1959. return OK;
  1960. }
  1961. bool EditorFileSystem::_should_skip_directory(const String &p_path) {
  1962. String project_data_path = ProjectSettings::get_singleton()->get_project_data_path();
  1963. if (p_path == project_data_path || p_path.begins_with(project_data_path + "/")) {
  1964. return true;
  1965. }
  1966. if (FileAccess::exists(p_path.path_join("project.godot"))) {
  1967. // Skip if another project inside this.
  1968. WARN_PRINT_ONCE(vformat("Detected another project.godot at %s. The folder will be ignored.", p_path));
  1969. return true;
  1970. }
  1971. if (FileAccess::exists(p_path.path_join(".gdignore"))) {
  1972. // Skip if a `.gdignore` file is inside this.
  1973. return true;
  1974. }
  1975. return false;
  1976. }
  1977. bool EditorFileSystem::is_group_file(const String &p_path) const {
  1978. return group_file_cache.has(p_path);
  1979. }
  1980. void EditorFileSystem::_move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location) {
  1981. int fc = efd->files.size();
  1982. EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptrw();
  1983. for (int i = 0; i < fc; i++) {
  1984. if (files[i]->import_group_file == p_group_file) {
  1985. files[i]->import_group_file = p_new_location;
  1986. Ref<ConfigFile> config;
  1987. config.instantiate();
  1988. String path = efd->get_file_path(i) + ".import";
  1989. Error err = config->load(path);
  1990. if (err != OK) {
  1991. continue;
  1992. }
  1993. if (config->has_section_key("remap", "group_file")) {
  1994. config->set_value("remap", "group_file", p_new_location);
  1995. }
  1996. List<String> sk;
  1997. config->get_section_keys("params", &sk);
  1998. for (const String &param : sk) {
  1999. //not very clean, but should work
  2000. String value = config->get_value("params", param);
  2001. if (value == p_group_file) {
  2002. config->set_value("params", param, p_new_location);
  2003. }
  2004. }
  2005. config->save(path);
  2006. }
  2007. }
  2008. for (int i = 0; i < efd->get_subdir_count(); i++) {
  2009. _move_group_files(efd->get_subdir(i), p_group_file, p_new_location);
  2010. }
  2011. }
  2012. void EditorFileSystem::move_group_file(const String &p_path, const String &p_new_path) {
  2013. if (get_filesystem()) {
  2014. _move_group_files(get_filesystem(), p_path, p_new_path);
  2015. if (group_file_cache.has(p_path)) {
  2016. group_file_cache.erase(p_path);
  2017. group_file_cache.insert(p_new_path);
  2018. }
  2019. }
  2020. }
  2021. ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate) {
  2022. if (!p_path.is_resource_file() || p_path.begins_with(ProjectSettings::get_singleton()->get_project_data_path())) {
  2023. // Saved externally (configuration file) or internal file, do not assign an ID.
  2024. return ResourceUID::INVALID_ID;
  2025. }
  2026. EditorFileSystemDirectory *fs = nullptr;
  2027. int cpos = -1;
  2028. if (!singleton->_find_file(p_path, &fs, cpos)) {
  2029. // Fallback to ResourceLoader if filesystem cache fails (can happen during scanning etc.).
  2030. ResourceUID::ID fallback = ResourceLoader::get_resource_uid(p_path);
  2031. if (fallback != ResourceUID::INVALID_ID) {
  2032. return fallback;
  2033. }
  2034. if (p_generate) {
  2035. 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.
  2036. } else {
  2037. return ResourceUID::INVALID_ID;
  2038. }
  2039. } else if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  2040. return fs->files[cpos]->uid;
  2041. } else if (p_generate) {
  2042. 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.
  2043. } else {
  2044. return ResourceUID::INVALID_ID;
  2045. }
  2046. }
  2047. static void _scan_extensions_dir(EditorFileSystemDirectory *d, HashSet<String> &extensions) {
  2048. int fc = d->get_file_count();
  2049. for (int i = 0; i < fc; i++) {
  2050. if (d->get_file_type(i) == SNAME("GDExtension")) {
  2051. extensions.insert(d->get_file_path(i));
  2052. }
  2053. }
  2054. int dc = d->get_subdir_count();
  2055. for (int i = 0; i < dc; i++) {
  2056. _scan_extensions_dir(d->get_subdir(i), extensions);
  2057. }
  2058. }
  2059. bool EditorFileSystem::_scan_extensions() {
  2060. EditorFileSystemDirectory *d = get_filesystem();
  2061. HashSet<String> extensions;
  2062. _scan_extensions_dir(d, extensions);
  2063. //verify against loaded extensions
  2064. Vector<String> extensions_added;
  2065. Vector<String> extensions_removed;
  2066. for (const String &E : extensions) {
  2067. if (!GDExtensionManager::get_singleton()->is_extension_loaded(E)) {
  2068. extensions_added.push_back(E);
  2069. }
  2070. }
  2071. Vector<String> loaded_extensions = GDExtensionManager::get_singleton()->get_loaded_extensions();
  2072. for (int i = 0; i < loaded_extensions.size(); i++) {
  2073. if (!extensions.has(loaded_extensions[i])) {
  2074. extensions_removed.push_back(loaded_extensions[i]);
  2075. }
  2076. }
  2077. String extension_list_config_file = GDExtension::get_extension_list_config_file();
  2078. if (extensions.size()) {
  2079. if (extensions_added.size() || extensions_removed.size()) { //extensions were added or removed
  2080. Ref<FileAccess> f = FileAccess::open(extension_list_config_file, FileAccess::WRITE);
  2081. for (const String &E : extensions) {
  2082. f->store_line(E);
  2083. }
  2084. }
  2085. } else {
  2086. if (loaded_extensions.size() || FileAccess::exists(extension_list_config_file)) { //extensions were removed
  2087. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2088. da->remove(extension_list_config_file);
  2089. }
  2090. }
  2091. bool needs_restart = false;
  2092. for (int i = 0; i < extensions_added.size(); i++) {
  2093. GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->load_extension(extensions_added[i]);
  2094. if (st == GDExtensionManager::LOAD_STATUS_FAILED) {
  2095. EditorNode::get_singleton()->add_io_error("Error loading extension: " + extensions_added[i]);
  2096. } else if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) {
  2097. needs_restart = true;
  2098. }
  2099. }
  2100. for (int i = 0; i < extensions_removed.size(); i++) {
  2101. GDExtensionManager::LoadStatus st = GDExtensionManager::get_singleton()->unload_extension(extensions_removed[i]);
  2102. if (st == GDExtensionManager::LOAD_STATUS_FAILED) {
  2103. EditorNode::get_singleton()->add_io_error("Error removing extension: " + extensions_added[i]);
  2104. } else if (st == GDExtensionManager::LOAD_STATUS_NEEDS_RESTART) {
  2105. needs_restart = true;
  2106. }
  2107. }
  2108. return needs_restart;
  2109. }
  2110. void EditorFileSystem::_bind_methods() {
  2111. ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
  2112. ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
  2113. ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
  2114. ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
  2115. ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
  2116. ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
  2117. ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
  2118. ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
  2119. ClassDB::bind_method(D_METHOD("reimport_files", "files"), &EditorFileSystem::reimport_files);
  2120. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  2121. ADD_SIGNAL(MethodInfo("script_classes_updated"));
  2122. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  2123. ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2124. ADD_SIGNAL(MethodInfo("resources_reload", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  2125. }
  2126. void EditorFileSystem::_update_extensions() {
  2127. valid_extensions.clear();
  2128. import_extensions.clear();
  2129. textfile_extensions.clear();
  2130. List<String> extensionsl;
  2131. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  2132. for (const String &E : extensionsl) {
  2133. valid_extensions.insert(E);
  2134. }
  2135. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  2136. for (const String &E : textfile_ext) {
  2137. if (valid_extensions.has(E)) {
  2138. continue;
  2139. }
  2140. valid_extensions.insert(E);
  2141. textfile_extensions.insert(E);
  2142. }
  2143. extensionsl.clear();
  2144. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  2145. for (const String &E : extensionsl) {
  2146. import_extensions.insert(E);
  2147. }
  2148. }
  2149. void EditorFileSystem::add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2150. ERR_FAIL_COND(import_support_queries.find(p_query) != -1);
  2151. import_support_queries.push_back(p_query);
  2152. }
  2153. void EditorFileSystem::remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  2154. import_support_queries.erase(p_query);
  2155. }
  2156. EditorFileSystem::EditorFileSystem() {
  2157. ResourceLoader::import = _resource_import;
  2158. reimport_on_missing_imported_files = GLOBAL_GET("editor/import/reimport_missing_imported_files");
  2159. singleton = this;
  2160. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  2161. filesystem->parent = nullptr;
  2162. new_filesystem = nullptr;
  2163. // This should probably also work on Unix and use the string it returns for FAT32 or exFAT
  2164. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2165. using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "exFAT");
  2166. scan_total = 0;
  2167. MessageQueue::get_singleton()->push_callable(callable_mp(ResourceUID::get_singleton(), &ResourceUID::clear)); // Will be updated on scan.
  2168. ResourceSaver::set_get_resource_id_for_path(_resource_saver_get_resource_id_for_path);
  2169. }
  2170. EditorFileSystem::~EditorFileSystem() {
  2171. ResourceSaver::set_get_resource_id_for_path(nullptr);
  2172. }