editor_file_system.cpp 85 KB

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