editor_file_system.cpp 84 KB

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