editor_file_system.cpp 81 KB

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