editor_file_system.cpp 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680
  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/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/resource_saver.h"
  36. #include "core/object/worker_thread_pool.h"
  37. #include "core/os/os.h"
  38. #include "core/variant/variant_parser.h"
  39. #include "editor/editor_help.h"
  40. #include "editor/editor_node.h"
  41. #include "editor/editor_paths.h"
  42. #include "editor/editor_resource_preview.h"
  43. #include "editor/editor_settings.h"
  44. #include "editor/plugins/script_editor_plugin.h"
  45. #include "editor/project_settings_editor.h"
  46. #include "scene/resources/packed_scene.h"
  47. EditorFileSystem *EditorFileSystem::singleton = nullptr;
  48. int EditorFileSystem::nb_files_total = 0;
  49. EditorFileSystem::ScannedDirectory *EditorFileSystem::first_scan_root_dir = nullptr;
  50. //the name is the version, to keep compatibility with different versions of Godot
  51. #define CACHE_FILE_NAME "filesystem_cache10"
  52. int EditorFileSystemDirectory::find_file_index(const String &p_file) const {
  53. for (int i = 0; i < files.size(); i++) {
  54. if (files[i]->file == p_file) {
  55. return i;
  56. }
  57. }
  58. return -1;
  59. }
  60. int EditorFileSystemDirectory::find_dir_index(const String &p_dir) const {
  61. for (int i = 0; i < subdirs.size(); i++) {
  62. if (subdirs[i]->name == p_dir) {
  63. return i;
  64. }
  65. }
  66. return -1;
  67. }
  68. void EditorFileSystemDirectory::force_update() {
  69. // We set modified_time to 0 to force `EditorFileSystem::_scan_fs_changes` to search changes in the directory
  70. modified_time = 0;
  71. }
  72. int EditorFileSystemDirectory::get_subdir_count() const {
  73. return subdirs.size();
  74. }
  75. EditorFileSystemDirectory *EditorFileSystemDirectory::get_subdir(int p_idx) {
  76. ERR_FAIL_INDEX_V(p_idx, subdirs.size(), nullptr);
  77. return subdirs[p_idx];
  78. }
  79. int EditorFileSystemDirectory::get_file_count() const {
  80. return files.size();
  81. }
  82. String EditorFileSystemDirectory::get_file(int p_idx) const {
  83. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  84. return files[p_idx]->file;
  85. }
  86. String EditorFileSystemDirectory::get_path() const {
  87. int parents = 0;
  88. const EditorFileSystemDirectory *efd = this;
  89. // Determine the level of nesting.
  90. while (efd->parent) {
  91. parents++;
  92. efd = efd->parent;
  93. }
  94. if (parents == 0) {
  95. return "res://";
  96. }
  97. // Using PackedStringArray, because the path is built in reverse order.
  98. PackedStringArray path_bits;
  99. // Allocate an array based on nesting. It will store path bits.
  100. path_bits.resize(parents + 2); // Last String is empty, so paths end with /.
  101. String *path_write = path_bits.ptrw();
  102. path_write[0] = "res:/";
  103. efd = this;
  104. for (int i = parents; i > 0; i--) {
  105. path_write[i] = efd->name;
  106. efd = efd->parent;
  107. }
  108. return String("/").join(path_bits);
  109. }
  110. String EditorFileSystemDirectory::get_file_path(int p_idx) const {
  111. return get_path().path_join(get_file(p_idx));
  112. }
  113. Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {
  114. ERR_FAIL_INDEX_V(p_idx, files.size(), Vector<String>());
  115. Vector<String> deps;
  116. for (int i = 0; i < files[p_idx]->deps.size(); i++) {
  117. String dep = files[p_idx]->deps[i];
  118. int sep_idx = dep.find("::"); //may contain type information, unwanted
  119. if (sep_idx != -1) {
  120. dep = dep.substr(0, sep_idx);
  121. }
  122. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(dep);
  123. if (uid != ResourceUID::INVALID_ID) {
  124. //return proper dependency resource from uid
  125. if (ResourceUID::get_singleton()->has_id(uid)) {
  126. dep = ResourceUID::get_singleton()->get_id_path(uid);
  127. } else {
  128. continue;
  129. }
  130. }
  131. deps.push_back(dep);
  132. }
  133. return deps;
  134. }
  135. bool EditorFileSystemDirectory::get_file_import_is_valid(int p_idx) const {
  136. ERR_FAIL_INDEX_V(p_idx, files.size(), false);
  137. return files[p_idx]->import_valid;
  138. }
  139. uint64_t EditorFileSystemDirectory::get_file_modified_time(int p_idx) const {
  140. ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
  141. return files[p_idx]->modified_time;
  142. }
  143. uint64_t EditorFileSystemDirectory::get_file_import_modified_time(int p_idx) const {
  144. ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
  145. return files[p_idx]->import_modified_time;
  146. }
  147. String EditorFileSystemDirectory::get_file_script_class_name(int p_idx) const {
  148. return files[p_idx]->class_info.name;
  149. }
  150. String EditorFileSystemDirectory::get_file_script_class_extends(int p_idx) const {
  151. return files[p_idx]->class_info.extends;
  152. }
  153. String EditorFileSystemDirectory::get_file_script_class_icon_path(int p_idx) const {
  154. return files[p_idx]->class_info.icon_path;
  155. }
  156. String EditorFileSystemDirectory::get_file_icon_path(int p_idx) const {
  157. return files[p_idx]->class_info.icon_path;
  158. }
  159. StringName EditorFileSystemDirectory::get_file_type(int p_idx) const {
  160. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  161. return files[p_idx]->type;
  162. }
  163. StringName EditorFileSystemDirectory::get_file_resource_script_class(int p_idx) const {
  164. ERR_FAIL_INDEX_V(p_idx, files.size(), "");
  165. return files[p_idx]->resource_script_class;
  166. }
  167. String EditorFileSystemDirectory::get_name() {
  168. return name;
  169. }
  170. EditorFileSystemDirectory *EditorFileSystemDirectory::get_parent() {
  171. return parent;
  172. }
  173. void EditorFileSystemDirectory::_bind_methods() {
  174. ClassDB::bind_method(D_METHOD("get_subdir_count"), &EditorFileSystemDirectory::get_subdir_count);
  175. ClassDB::bind_method(D_METHOD("get_subdir", "idx"), &EditorFileSystemDirectory::get_subdir);
  176. ClassDB::bind_method(D_METHOD("get_file_count"), &EditorFileSystemDirectory::get_file_count);
  177. ClassDB::bind_method(D_METHOD("get_file", "idx"), &EditorFileSystemDirectory::get_file);
  178. ClassDB::bind_method(D_METHOD("get_file_path", "idx"), &EditorFileSystemDirectory::get_file_path);
  179. ClassDB::bind_method(D_METHOD("get_file_type", "idx"), &EditorFileSystemDirectory::get_file_type);
  180. ClassDB::bind_method(D_METHOD("get_file_script_class_name", "idx"), &EditorFileSystemDirectory::get_file_script_class_name);
  181. ClassDB::bind_method(D_METHOD("get_file_script_class_extends", "idx"), &EditorFileSystemDirectory::get_file_script_class_extends);
  182. ClassDB::bind_method(D_METHOD("get_file_import_is_valid", "idx"), &EditorFileSystemDirectory::get_file_import_is_valid);
  183. ClassDB::bind_method(D_METHOD("get_name"), &EditorFileSystemDirectory::get_name);
  184. ClassDB::bind_method(D_METHOD("get_path"), &EditorFileSystemDirectory::get_path);
  185. ClassDB::bind_method(D_METHOD("get_parent"), &EditorFileSystemDirectory::get_parent);
  186. ClassDB::bind_method(D_METHOD("find_file_index", "name"), &EditorFileSystemDirectory::find_file_index);
  187. ClassDB::bind_method(D_METHOD("find_dir_index", "name"), &EditorFileSystemDirectory::find_dir_index);
  188. }
  189. EditorFileSystemDirectory::EditorFileSystemDirectory() {
  190. modified_time = 0;
  191. parent = nullptr;
  192. }
  193. EditorFileSystemDirectory::~EditorFileSystemDirectory() {
  194. for (EditorFileSystemDirectory::FileInfo *fi : files) {
  195. memdelete(fi);
  196. }
  197. for (EditorFileSystemDirectory *dir : subdirs) {
  198. memdelete(dir);
  199. }
  200. }
  201. EditorFileSystem::ScannedDirectory::~ScannedDirectory() {
  202. for (ScannedDirectory *dir : subdirs) {
  203. memdelete(dir);
  204. }
  205. }
  206. void EditorFileSystem::_load_first_scan_root_dir() {
  207. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  208. first_scan_root_dir = memnew(ScannedDirectory);
  209. first_scan_root_dir->full_path = "res://";
  210. nb_files_total = _scan_new_dir(first_scan_root_dir, d);
  211. }
  212. void EditorFileSystem::scan_for_uid() {
  213. // Load file structure into memory.
  214. _load_first_scan_root_dir();
  215. // Load extensions for which an .import should exists.
  216. List<String> extensionsl;
  217. HashSet<String> import_extensions;
  218. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  219. for (const String &E : extensionsl) {
  220. import_extensions.insert(E);
  221. }
  222. // Scan the file system to load uid.
  223. _scan_for_uid_directory(first_scan_root_dir, import_extensions);
  224. // It's done, resetting the callback method to prevent a second scan.
  225. ResourceUID::scan_for_uid_on_startup = nullptr;
  226. }
  227. void EditorFileSystem::_scan_for_uid_directory(const ScannedDirectory *p_scan_dir, const HashSet<String> &p_import_extensions) {
  228. for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
  229. _scan_for_uid_directory(scan_sub_dir, p_import_extensions);
  230. }
  231. for (const String &scan_file : p_scan_dir->files) {
  232. const String ext = scan_file.get_extension().to_lower();
  233. if (ext == "uid" || ext == "import") {
  234. continue;
  235. }
  236. const String path = p_scan_dir->full_path.path_join(scan_file);
  237. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  238. if (p_import_extensions.has(ext)) {
  239. if (FileAccess::exists(path + ".import")) {
  240. uid = ResourceFormatImporter::get_singleton()->get_resource_uid(path);
  241. }
  242. } else {
  243. uid = ResourceLoader::get_resource_uid(path);
  244. }
  245. if (uid != ResourceUID::INVALID_ID) {
  246. if (!ResourceUID::get_singleton()->has_id(uid)) {
  247. ResourceUID::get_singleton()->add_id(uid, path);
  248. }
  249. }
  250. }
  251. }
  252. void EditorFileSystem::_first_scan_filesystem() {
  253. EditorProgress ep = EditorProgress("first_scan_filesystem", TTR("Project initialization"), 5);
  254. HashSet<String> existing_class_names;
  255. HashSet<String> extensions;
  256. if (!first_scan_root_dir) {
  257. ep.step(TTR("Scanning file structure..."), 0, true);
  258. _load_first_scan_root_dir();
  259. }
  260. // Preloading GDExtensions file extensions to prevent looping on all the resource loaders
  261. // for each files in _first_scan_process_scripts.
  262. List<String> gdextension_extensions;
  263. ResourceLoader::get_recognized_extensions_for_type("GDExtension", &gdextension_extensions);
  264. // This loads the global class names from the scripts and ensures that even if the
  265. // global_script_class_cache.cfg was missing or invalid, the global class names are valid in ScriptServer.
  266. // At the same time, to prevent looping multiple times in all files, it looks for extensions.
  267. ep.step(TTR("Loading global class names..."), 1, true);
  268. _first_scan_process_scripts(first_scan_root_dir, gdextension_extensions, existing_class_names, extensions);
  269. // Removing invalid global class to prevent having invalid paths in ScriptServer.
  270. bool save_scripts = _remove_invalid_global_class_names(existing_class_names);
  271. // If a global class is found or removed, we sync global_script_class_cache.cfg with the ScriptServer
  272. if (!existing_class_names.is_empty() || save_scripts) {
  273. EditorNode::get_editor_data().script_class_save_global_classes();
  274. }
  275. // Processing extensions to add new extensions or remove invalid ones.
  276. // Important to do it in the first scan so custom types, new class names, custom importers, etc...
  277. // from extensions are ready to go before plugins, autoloads and resources validation/importation.
  278. // At this point, a restart of the editor should not be needed so we don't use the return value.
  279. ep.step(TTR("Verifying GDExtensions..."), 2, true);
  280. GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions);
  281. // Now that all the global class names should be loaded, create autoloads and plugins.
  282. // This is done after loading the global class names because autoloads and plugins can use
  283. // global class names.
  284. ep.step(TTR("Creating autoload scripts..."), 3, true);
  285. ProjectSettingsEditor::get_singleton()->init_autoloads();
  286. ep.step(TTR("Initializing plugins..."), 4, true);
  287. EditorNode::get_singleton()->init_plugins();
  288. ep.step(TTR("Starting file scan..."), 5, true);
  289. }
  290. void EditorFileSystem::_first_scan_process_scripts(const ScannedDirectory *p_scan_dir, List<String> &p_gdextension_extensions, HashSet<String> &p_existing_class_names, HashSet<String> &p_extensions) {
  291. for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
  292. _first_scan_process_scripts(scan_sub_dir, p_gdextension_extensions, p_existing_class_names, p_extensions);
  293. }
  294. for (const String &scan_file : p_scan_dir->files) {
  295. // Optimization to skip the ResourceLoader::get_resource_type for files
  296. // that are not scripts. Some loader get_resource_type methods read the file
  297. // which can be very slow on large projects.
  298. const String ext = scan_file.get_extension().to_lower();
  299. bool is_script = false;
  300. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  301. if (ScriptServer::get_language(i)->get_extension() == ext) {
  302. is_script = true;
  303. break;
  304. }
  305. }
  306. if (is_script) {
  307. const String path = p_scan_dir->full_path.path_join(scan_file);
  308. const String type = ResourceLoader::get_resource_type(path);
  309. if (ClassDB::is_parent_class(type, SNAME("Script"))) {
  310. const ScriptClassInfo &info = _get_global_script_class(type, path);
  311. ScriptClassInfoUpdate update(info);
  312. update.type = type;
  313. _register_global_class_script(path, path, update);
  314. if (!info.name.is_empty()) {
  315. p_existing_class_names.insert(info.name);
  316. }
  317. }
  318. }
  319. // Check for GDExtensions.
  320. if (p_gdextension_extensions.find(ext)) {
  321. const String path = p_scan_dir->full_path.path_join(scan_file);
  322. const String type = ResourceLoader::get_resource_type(path);
  323. if (type == SNAME("GDExtension")) {
  324. p_extensions.insert(path);
  325. }
  326. }
  327. }
  328. }
  329. void EditorFileSystem::_scan_filesystem() {
  330. // On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
  331. ERR_FAIL_COND(!scanning || new_filesystem || (first_scan && !first_scan_root_dir));
  332. //read .fscache
  333. String cpath;
  334. sources_changed.clear();
  335. file_cache.clear();
  336. String project = ProjectSettings::get_singleton()->get_resource_path();
  337. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
  338. {
  339. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::READ);
  340. bool first = true;
  341. if (f.is_valid()) {
  342. //read the disk cache
  343. while (!f->eof_reached()) {
  344. String l = f->get_line().strip_edges();
  345. if (first) {
  346. if (first_scan) {
  347. // only use this on first scan, afterwards it gets ignored
  348. // this is so on first reimport we synchronize versions, then
  349. // we don't care until editor restart. This is for usability mainly so
  350. // your workflow is not killed after changing a setting by forceful reimporting
  351. // everything there is.
  352. filesystem_settings_version_for_import = l.strip_edges();
  353. if (filesystem_settings_version_for_import != ResourceFormatImporter::get_singleton()->get_import_settings_hash()) {
  354. revalidate_import_files = true;
  355. }
  356. }
  357. first = false;
  358. continue;
  359. }
  360. if (l.is_empty()) {
  361. continue;
  362. }
  363. if (l.begins_with("::")) {
  364. Vector<String> split = l.split("::");
  365. ERR_CONTINUE(split.size() != 3);
  366. const String &name = split[1];
  367. cpath = name;
  368. } else {
  369. // The last section (deps) may contain the same splitter, so limit the maxsplit to 8 to get the complete deps.
  370. Vector<String> split = l.split("::", true, 8);
  371. ERR_CONTINUE(split.size() < 9);
  372. String name = split[0];
  373. String file;
  374. file = name;
  375. name = cpath.path_join(name);
  376. FileCache fc;
  377. fc.type = split[1].get_slicec('/', 0);
  378. fc.resource_script_class = split[1].get_slicec('/', 1);
  379. fc.uid = split[2].to_int();
  380. fc.modification_time = split[3].to_int();
  381. fc.import_modification_time = split[4].to_int();
  382. fc.import_valid = split[5].to_int() != 0;
  383. fc.import_group_file = split[6].strip_edges();
  384. {
  385. const Vector<String> &slices = split[7].split("<>");
  386. ERR_CONTINUE(slices.size() < 7);
  387. fc.class_info.name = slices[0];
  388. fc.class_info.extends = slices[1];
  389. fc.class_info.icon_path = slices[2];
  390. fc.class_info.is_abstract = slices[3].to_int();
  391. fc.class_info.is_tool = slices[4].to_int();
  392. fc.import_md5 = slices[5];
  393. fc.import_dest_paths = slices[6].split("<*>");
  394. }
  395. fc.deps = split[8].strip_edges().split("<>");
  396. file_cache[name] = fc;
  397. }
  398. }
  399. }
  400. }
  401. const String update_cache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  402. if (first_scan && FileAccess::exists(update_cache)) {
  403. {
  404. Ref<FileAccess> f2 = FileAccess::open(update_cache, FileAccess::READ);
  405. String l = f2->get_line().strip_edges();
  406. while (!l.is_empty()) {
  407. dep_update_list.insert(l);
  408. file_cache.erase(l); // Erase cache for this, so it gets updated.
  409. l = f2->get_line().strip_edges();
  410. }
  411. }
  412. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  413. d->remove(update_cache); // Bye bye update cache.
  414. }
  415. EditorProgressBG scan_progress("efs", "ScanFS", 1000);
  416. ScanProgress sp;
  417. sp.hi = nb_files_total;
  418. sp.progress = &scan_progress;
  419. new_filesystem = memnew(EditorFileSystemDirectory);
  420. new_filesystem->parent = nullptr;
  421. ScannedDirectory *sd;
  422. HashSet<String> *processed_files = nullptr;
  423. // On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
  424. if (first_scan) {
  425. sd = first_scan_root_dir;
  426. // Will be updated on scan.
  427. ResourceUID::get_singleton()->clear();
  428. ResourceUID::scan_for_uid_on_startup = nullptr;
  429. processed_files = memnew(HashSet<String>());
  430. } else {
  431. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  432. sd = memnew(ScannedDirectory);
  433. sd->full_path = "res://";
  434. nb_files_total = _scan_new_dir(sd, d);
  435. }
  436. _process_file_system(sd, new_filesystem, sp, processed_files);
  437. if (first_scan) {
  438. _process_removed_files(*processed_files);
  439. }
  440. dep_update_list.clear();
  441. file_cache.clear(); //clear caches, no longer needed
  442. if (first_scan) {
  443. memdelete(first_scan_root_dir);
  444. first_scan_root_dir = nullptr;
  445. memdelete(processed_files);
  446. } else {
  447. //on the first scan this is done from the main thread after re-importing
  448. _save_filesystem_cache();
  449. }
  450. scanning = false;
  451. }
  452. void EditorFileSystem::_save_filesystem_cache() {
  453. group_file_cache.clear();
  454. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
  455. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  456. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  457. f->store_line(filesystem_settings_version_for_import);
  458. _save_filesystem_cache(filesystem, f);
  459. }
  460. void EditorFileSystem::_thread_func(void *_userdata) {
  461. EditorFileSystem *sd = (EditorFileSystem *)_userdata;
  462. sd->_scan_filesystem();
  463. }
  464. bool EditorFileSystem::_is_test_for_reimport_needed(const String &p_path, uint64_t p_last_modification_time, uint64_t p_modification_time, uint64_t p_last_import_modification_time, uint64_t p_import_modification_time, const Vector<String> &p_import_dest_paths) {
  465. // The idea here is to trust the cache. If the last modification times in the cache correspond
  466. // to the last modification times of the files on disk, it means the files have not changed since
  467. // the last import, and the files in .godot/imported (p_import_dest_paths) should all be valid.
  468. if (p_last_modification_time != p_modification_time) {
  469. return true;
  470. }
  471. if (p_last_import_modification_time != p_import_modification_time) {
  472. return true;
  473. }
  474. if (reimport_on_missing_imported_files) {
  475. for (const String &path : p_import_dest_paths) {
  476. if (!FileAccess::exists(path)) {
  477. return true;
  478. }
  479. }
  480. }
  481. return false;
  482. }
  483. bool EditorFileSystem::_test_for_reimport(const String &p_path, const String &p_expected_import_md5) {
  484. if (p_expected_import_md5.is_empty()) {
  485. // Marked as reimportation needed.
  486. return true;
  487. }
  488. String new_md5 = FileAccess::get_md5(p_path + ".import");
  489. if (p_expected_import_md5 != new_md5) {
  490. return true;
  491. }
  492. Error err;
  493. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  494. if (f.is_null()) { // No import file, reimport.
  495. return true;
  496. }
  497. VariantParser::StreamFile stream;
  498. stream.f = f;
  499. String assign;
  500. Variant value;
  501. VariantParser::Tag next_tag;
  502. int lines = 0;
  503. String error_text;
  504. Vector<String> to_check;
  505. String importer_name;
  506. String source_file = "";
  507. String source_md5 = "";
  508. Vector<String> dest_files;
  509. String dest_md5 = "";
  510. int version = 0;
  511. bool found_uid = false;
  512. Variant meta;
  513. while (true) {
  514. assign = Variant();
  515. next_tag.fields.clear();
  516. next_tag.name = String();
  517. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  518. if (err == ERR_FILE_EOF) {
  519. break;
  520. } else if (err != OK) {
  521. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
  522. // Parse error, skip and let user attempt manual reimport to avoid reimport loop.
  523. return false;
  524. }
  525. if (!assign.is_empty()) {
  526. if (assign == "valid" && value.operator bool() == false) {
  527. // Invalid import (failed previous import), skip and let user attempt manual reimport to avoid reimport loop.
  528. return false;
  529. }
  530. if (assign.begins_with("path")) {
  531. to_check.push_back(value);
  532. } else if (assign == "files") {
  533. Array fa = value;
  534. for (const Variant &check_path : fa) {
  535. to_check.push_back(check_path);
  536. }
  537. } else if (assign == "importer_version") {
  538. version = value;
  539. } else if (assign == "importer") {
  540. importer_name = value;
  541. } else if (assign == "uid") {
  542. found_uid = true;
  543. } else if (assign == "source_file") {
  544. source_file = value;
  545. } else if (assign == "dest_files") {
  546. dest_files = value;
  547. } else if (assign == "metadata") {
  548. meta = value;
  549. }
  550. } else if (next_tag.name != "remap" && next_tag.name != "deps") {
  551. break;
  552. }
  553. }
  554. if (importer_name == "keep" || importer_name == "skip") {
  555. return false; // Keep mode, do not reimport.
  556. }
  557. if (!found_uid) {
  558. return true; // UID not found, old format, reimport.
  559. }
  560. // Imported files are gone, reimport.
  561. for (const String &E : to_check) {
  562. if (!FileAccess::exists(E)) {
  563. return true;
  564. }
  565. }
  566. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  567. if (importer.is_null()) {
  568. return true; // The importer has possibly changed, try to reimport.
  569. }
  570. if (importer->get_format_version() > version) {
  571. return true; // Version changed, reimport.
  572. }
  573. if (!importer->are_import_settings_valid(p_path, meta)) {
  574. // Reimport settings are out of sync with project settings, reimport.
  575. return true;
  576. }
  577. // Read the md5's from a separate file (so the import parameters aren't dependent on the file version).
  578. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path);
  579. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err);
  580. if (md5s.is_null()) { // No md5's stored for this resource.
  581. return true;
  582. }
  583. VariantParser::StreamFile md5_stream;
  584. md5_stream.f = md5s;
  585. while (true) {
  586. assign = Variant();
  587. next_tag.fields.clear();
  588. next_tag.name = String();
  589. err = VariantParser::parse_tag_assign_eof(&md5_stream, lines, error_text, next_tag, assign, value, nullptr, true);
  590. if (err == ERR_FILE_EOF) {
  591. break;
  592. } else if (err != OK) {
  593. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import.md5:" + itos(lines) + "' error '" + error_text + "'.");
  594. return false; // Parse error.
  595. }
  596. if (!assign.is_empty()) {
  597. if (assign == "source_md5") {
  598. source_md5 = value;
  599. } else if (assign == "dest_md5") {
  600. dest_md5 = value;
  601. }
  602. }
  603. }
  604. // Check source md5 matching.
  605. if (!source_file.is_empty() && source_file != p_path) {
  606. return true; // File was moved, reimport.
  607. }
  608. if (source_md5.is_empty()) {
  609. return true; // Lacks md5, so just reimport.
  610. }
  611. String md5 = FileAccess::get_md5(p_path);
  612. if (md5 != source_md5) {
  613. return true;
  614. }
  615. if (!dest_files.is_empty() && !dest_md5.is_empty()) {
  616. md5 = FileAccess::get_multiple_md5(dest_files);
  617. if (md5 != dest_md5) {
  618. return true;
  619. }
  620. }
  621. return false; // Nothing changed.
  622. }
  623. Vector<String> EditorFileSystem::_get_import_dest_paths(const String &p_path) {
  624. Error err;
  625. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  626. if (f.is_null()) { // No import file, reimport.
  627. return Vector<String>();
  628. }
  629. VariantParser::StreamFile stream;
  630. stream.f = f;
  631. String assign;
  632. Variant value;
  633. VariantParser::Tag next_tag;
  634. int lines = 0;
  635. String error_text;
  636. Vector<String> dest_paths;
  637. String importer_name;
  638. while (true) {
  639. assign = Variant();
  640. next_tag.fields.clear();
  641. next_tag.name = String();
  642. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  643. if (err == ERR_FILE_EOF) {
  644. break;
  645. } else if (err != OK) {
  646. ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
  647. // Parse error, skip and let user attempt manual reimport to avoid reimport loop.
  648. return Vector<String>();
  649. }
  650. if (!assign.is_empty()) {
  651. if (assign == "valid" && value.operator bool() == false) {
  652. // Invalid import (failed previous import), skip and let user attempt manual reimport to avoid reimport loop.
  653. return Vector<String>();
  654. }
  655. if (assign.begins_with("path")) {
  656. dest_paths.push_back(value);
  657. } else if (assign == "files") {
  658. Array fa = value;
  659. for (const Variant &dest_path : fa) {
  660. dest_paths.push_back(dest_path);
  661. }
  662. } else if (assign == "importer") {
  663. importer_name = value;
  664. }
  665. } else if (next_tag.name != "remap" && next_tag.name != "deps") {
  666. break;
  667. }
  668. }
  669. if (importer_name == "keep" || importer_name == "skip") {
  670. return Vector<String>();
  671. }
  672. return dest_paths;
  673. }
  674. bool EditorFileSystem::_scan_import_support(const Vector<String> &reimports) {
  675. if (import_support_queries.is_empty()) {
  676. return false;
  677. }
  678. HashMap<String, int> import_support_test;
  679. Vector<bool> import_support_tested;
  680. import_support_tested.resize(import_support_queries.size());
  681. for (int i = 0; i < import_support_queries.size(); i++) {
  682. import_support_tested.write[i] = false;
  683. if (import_support_queries[i]->is_active()) {
  684. Vector<String> extensions = import_support_queries[i]->get_file_extensions();
  685. for (int j = 0; j < extensions.size(); j++) {
  686. import_support_test.insert(extensions[j], i);
  687. }
  688. }
  689. }
  690. if (import_support_test.is_empty()) {
  691. return false; //well nothing to do
  692. }
  693. for (int i = 0; i < reimports.size(); i++) {
  694. HashMap<String, int>::Iterator E = import_support_test.find(reimports[i].get_extension().to_lower());
  695. if (E) {
  696. import_support_tested.write[E->value] = true;
  697. }
  698. }
  699. for (int i = 0; i < import_support_tested.size(); i++) {
  700. if (import_support_tested[i]) {
  701. if (import_support_queries.write[i]->query()) {
  702. return true;
  703. }
  704. }
  705. }
  706. return false;
  707. }
  708. bool EditorFileSystem::_update_scan_actions() {
  709. sources_changed.clear();
  710. // We need to update the script global class names before the reimports to be sure that
  711. // all the importer classes that depends on class names will work.
  712. _update_script_classes();
  713. bool fs_changed = false;
  714. Vector<String> reimports;
  715. Vector<String> reloads;
  716. EditorProgress *ep = nullptr;
  717. if (scan_actions.size() > 1) {
  718. ep = memnew(EditorProgress("_update_scan_actions", TTR("Scanning actions..."), scan_actions.size()));
  719. }
  720. int step_count = 0;
  721. for (const ItemAction &ia : scan_actions) {
  722. switch (ia.action) {
  723. case ItemAction::ACTION_NONE: {
  724. } break;
  725. case ItemAction::ACTION_DIR_ADD: {
  726. int idx = 0;
  727. for (int i = 0; i < ia.dir->subdirs.size(); i++) {
  728. if (ia.new_dir->name.filenocasecmp_to(ia.dir->subdirs[i]->name) < 0) {
  729. break;
  730. }
  731. idx++;
  732. }
  733. if (idx == ia.dir->subdirs.size()) {
  734. ia.dir->subdirs.push_back(ia.new_dir);
  735. } else {
  736. ia.dir->subdirs.insert(idx, ia.new_dir);
  737. }
  738. fs_changed = true;
  739. } break;
  740. case ItemAction::ACTION_DIR_REMOVE: {
  741. ERR_CONTINUE(!ia.dir->parent);
  742. ia.dir->parent->subdirs.erase(ia.dir);
  743. memdelete(ia.dir);
  744. fs_changed = true;
  745. } break;
  746. case ItemAction::ACTION_FILE_ADD: {
  747. int idx = 0;
  748. for (int i = 0; i < ia.dir->files.size(); i++) {
  749. if (ia.new_file->file.filenocasecmp_to(ia.dir->files[i]->file) < 0) {
  750. break;
  751. }
  752. idx++;
  753. }
  754. if (idx == ia.dir->files.size()) {
  755. ia.dir->files.push_back(ia.new_file);
  756. } else {
  757. ia.dir->files.insert(idx, ia.new_file);
  758. }
  759. fs_changed = true;
  760. const String new_file_path = ia.dir->get_file_path(idx);
  761. const ResourceUID::ID existing_id = ResourceLoader::get_resource_uid(new_file_path);
  762. if (existing_id != ResourceUID::INVALID_ID) {
  763. const String old_path = ResourceUID::get_singleton()->get_id_path(existing_id);
  764. if (old_path != new_file_path && FileAccess::exists(old_path)) {
  765. const ResourceUID::ID new_id = ResourceUID::get_singleton()->create_id_for_path(new_file_path);
  766. ResourceUID::get_singleton()->add_id(new_id, new_file_path);
  767. ResourceSaver::set_uid(new_file_path, new_id);
  768. WARN_PRINT(vformat("Duplicate UID detected for Resource at \"%s\".\nOld Resource path: \"%s\". The new file UID was changed automatically.", new_file_path, old_path));
  769. } else {
  770. // Re-assign the UID to file, just in case it was pulled from cache.
  771. ResourceSaver::set_uid(new_file_path, existing_id);
  772. }
  773. } else if (ResourceLoader::should_create_uid_file(new_file_path)) {
  774. Ref<FileAccess> f = FileAccess::open(new_file_path + ".uid", FileAccess::WRITE);
  775. if (f.is_valid()) {
  776. ia.new_file->uid = ResourceUID::get_singleton()->create_id_for_path(new_file_path);
  777. f->store_line(ResourceUID::get_singleton()->id_to_text(ia.new_file->uid));
  778. }
  779. }
  780. if (ClassDB::is_parent_class(ia.new_file->type, SNAME("Script"))) {
  781. _queue_update_script_class(new_file_path, ScriptClassInfoUpdate::from_file_info(ia.new_file));
  782. }
  783. if (ia.new_file->type == SNAME("PackedScene")) {
  784. _queue_update_scene_groups(new_file_path);
  785. }
  786. } break;
  787. case ItemAction::ACTION_FILE_REMOVE: {
  788. int idx = ia.dir->find_file_index(ia.file);
  789. ERR_CONTINUE(idx == -1);
  790. const String file_path = ia.dir->get_file_path(idx);
  791. const String class_name = ia.dir->files[idx]->class_info.name;
  792. if (ClassDB::is_parent_class(ia.dir->files[idx]->type, SNAME("Script"))) {
  793. _queue_update_script_class(file_path, ScriptClassInfoUpdate());
  794. }
  795. if (ia.dir->files[idx]->type == SNAME("PackedScene")) {
  796. _queue_update_scene_groups(file_path);
  797. }
  798. _delete_internal_files(file_path);
  799. memdelete(ia.dir->files[idx]);
  800. ia.dir->files.remove_at(idx);
  801. // Restore another script with the same global class name if it exists.
  802. if (!class_name.is_empty()) {
  803. EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
  804. String old_file = _get_file_by_class_name(filesystem, class_name, old_fi);
  805. if (!old_file.is_empty() && old_fi) {
  806. _queue_update_script_class(old_file, ScriptClassInfoUpdate::from_file_info(old_fi));
  807. }
  808. }
  809. fs_changed = true;
  810. } break;
  811. case ItemAction::ACTION_FILE_TEST_REIMPORT: {
  812. int idx = ia.dir->find_file_index(ia.file);
  813. ERR_CONTINUE(idx == -1);
  814. String full_path = ia.dir->get_file_path(idx);
  815. bool need_reimport = _test_for_reimport(full_path, ia.dir->files[idx]->import_md5);
  816. if (need_reimport) {
  817. // Must reimport.
  818. reimports.push_back(full_path);
  819. Vector<String> dependencies = _get_dependencies(full_path);
  820. for (const String &dep : dependencies) {
  821. const String &dependency_path = dep.contains("::") ? dep.get_slice("::", 0) : dep;
  822. if (_can_import_file(dep)) {
  823. reimports.push_back(dependency_path);
  824. }
  825. }
  826. } else {
  827. // Must not reimport, all was good.
  828. // Update modified times, md5 and destination paths, to avoid reimport.
  829. ia.dir->files[idx]->modified_time = FileAccess::get_modified_time(full_path);
  830. ia.dir->files[idx]->import_modified_time = FileAccess::get_modified_time(full_path + ".import");
  831. if (ia.dir->files[idx]->import_md5.is_empty()) {
  832. ia.dir->files[idx]->import_md5 = FileAccess::get_md5(full_path + ".import");
  833. }
  834. ia.dir->files[idx]->import_dest_paths = _get_import_dest_paths(full_path);
  835. }
  836. fs_changed = true;
  837. } break;
  838. case ItemAction::ACTION_FILE_RELOAD: {
  839. int idx = ia.dir->find_file_index(ia.file);
  840. ERR_CONTINUE(idx == -1);
  841. // Only reloads the resources that are already loaded.
  842. if (ResourceCache::has(ia.dir->get_file_path(idx))) {
  843. reloads.push_back(ia.dir->get_file_path(idx));
  844. }
  845. } break;
  846. }
  847. if (ep) {
  848. ep->step(ia.file, step_count++, false);
  849. }
  850. }
  851. memdelete_notnull(ep);
  852. if (_scan_extensions()) {
  853. //needs editor restart
  854. //extensions also may provide filetypes to be imported, so they must run before importing
  855. 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"))) {
  856. if (!first_scan) {
  857. EditorNode::get_singleton()->save_all_scenes();
  858. }
  859. EditorNode::get_singleton()->restart_editor();
  860. //do not import
  861. return true;
  862. }
  863. }
  864. if (!reimports.is_empty()) {
  865. if (_scan_import_support(reimports)) {
  866. return true;
  867. }
  868. reimport_files(reimports);
  869. } else {
  870. //reimport files will update the uid cache file so if nothing was reimported, update it manually
  871. ResourceUID::get_singleton()->update_cache();
  872. }
  873. if (!reloads.is_empty()) {
  874. // Update global class names, dependencies, etc...
  875. update_files(reloads);
  876. }
  877. if (first_scan) {
  878. //only on first scan this is valid and updated, then settings changed.
  879. revalidate_import_files = false;
  880. filesystem_settings_version_for_import = ResourceFormatImporter::get_singleton()->get_import_settings_hash();
  881. _save_filesystem_cache();
  882. }
  883. // Moving the processing of pending updates before the resources_reload event to be sure all global class names
  884. // are updated. Script.cpp listens on resources_reload and reloads updated scripts.
  885. _process_update_pending();
  886. if (reloads.size()) {
  887. emit_signal(SNAME("resources_reload"), reloads);
  888. }
  889. scan_actions.clear();
  890. return fs_changed;
  891. }
  892. void EditorFileSystem::scan() {
  893. if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/) {
  894. return;
  895. }
  896. if (scanning || scanning_changes || thread.is_started()) {
  897. return;
  898. }
  899. // The first scan must be on the main thread because, after the first scan and update
  900. // of global class names, we load the plugins and autoloads. These need to
  901. // be added on the main thread because they are nodes, and we need to wait for them
  902. // to be loaded to continue the scan and reimportations.
  903. if (first_scan) {
  904. _first_scan_filesystem();
  905. #ifdef ANDROID_ENABLED
  906. const String nomedia_file_path = ProjectSettings::get_singleton()->get_resource_path().path_join(".nomedia");
  907. if (!FileAccess::exists(nomedia_file_path)) {
  908. // Create a .nomedia file to hide assets from media apps on Android.
  909. Ref<FileAccess> f = FileAccess::open(nomedia_file_path, FileAccess::WRITE);
  910. if (f.is_null()) {
  911. // .nomedia isn't so critical.
  912. ERR_PRINT("Couldn't create .nomedia in project path.");
  913. } else {
  914. f->close();
  915. }
  916. }
  917. #endif
  918. }
  919. _update_extensions();
  920. if (!use_threads) {
  921. scanning = true;
  922. scan_total = 0;
  923. _scan_filesystem();
  924. if (filesystem) {
  925. memdelete(filesystem);
  926. }
  927. //file_type_cache.clear();
  928. filesystem = new_filesystem;
  929. new_filesystem = nullptr;
  930. _update_scan_actions();
  931. // Update all icons so they are loaded for the FileSystemDock.
  932. _update_files_icon_path();
  933. scanning = false;
  934. // Set first_scan to false before the signals so the function doing_first_scan can return false
  935. // in editor_node to start the export if needed.
  936. first_scan = false;
  937. ResourceImporter::load_on_startup = nullptr;
  938. emit_signal(SNAME("filesystem_changed"));
  939. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  940. } else {
  941. ERR_FAIL_COND(thread.is_started());
  942. set_process(true);
  943. Thread::Settings s;
  944. scanning = true;
  945. scan_total = 0;
  946. s.priority = Thread::PRIORITY_LOW;
  947. thread.start(_thread_func, this, s);
  948. }
  949. }
  950. void EditorFileSystem::ScanProgress::increment() {
  951. current++;
  952. float ratio = current / MAX(hi, 1.0f);
  953. if (progress) {
  954. progress->step(ratio * 1000.0f);
  955. }
  956. EditorFileSystem::singleton->scan_total = ratio;
  957. }
  958. int EditorFileSystem::_scan_new_dir(ScannedDirectory *p_dir, Ref<DirAccess> &da) {
  959. List<String> dirs;
  960. List<String> files;
  961. String cd = da->get_current_dir();
  962. da->list_dir_begin();
  963. while (true) {
  964. String f = da->get_next();
  965. if (f.is_empty()) {
  966. break;
  967. }
  968. if (da->current_is_hidden()) {
  969. continue;
  970. }
  971. if (da->current_is_dir()) {
  972. if (f.begins_with(".")) { // Ignore special and . / ..
  973. continue;
  974. }
  975. if (_should_skip_directory(cd.path_join(f))) {
  976. continue;
  977. }
  978. dirs.push_back(f);
  979. } else {
  980. files.push_back(f);
  981. }
  982. }
  983. da->list_dir_end();
  984. dirs.sort_custom<FileNoCaseComparator>();
  985. files.sort_custom<FileNoCaseComparator>();
  986. int nb_files_total_scan = 0;
  987. for (const String &dir : dirs) {
  988. if (da->change_dir(dir) == OK) {
  989. String d = da->get_current_dir();
  990. if (d == cd || !d.begins_with(cd)) {
  991. da->change_dir(cd); //avoid recursion
  992. } else {
  993. ScannedDirectory *sd = memnew(ScannedDirectory);
  994. sd->name = dir;
  995. sd->full_path = p_dir->full_path.path_join(sd->name);
  996. nb_files_total_scan += _scan_new_dir(sd, da);
  997. p_dir->subdirs.push_back(sd);
  998. da->change_dir("..");
  999. }
  1000. } else {
  1001. ERR_PRINT("Cannot go into subdir '" + dir + "'.");
  1002. }
  1003. }
  1004. p_dir->files = files;
  1005. nb_files_total_scan += files.size();
  1006. return nb_files_total_scan;
  1007. }
  1008. void EditorFileSystem::_process_file_system(const ScannedDirectory *p_scan_dir, EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, HashSet<String> *r_processed_files) {
  1009. p_dir->modified_time = FileAccess::get_modified_time(p_scan_dir->full_path);
  1010. for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
  1011. EditorFileSystemDirectory *sub_dir = memnew(EditorFileSystemDirectory);
  1012. sub_dir->parent = p_dir;
  1013. sub_dir->name = scan_sub_dir->name;
  1014. p_dir->subdirs.push_back(sub_dir);
  1015. _process_file_system(scan_sub_dir, sub_dir, p_progress, r_processed_files);
  1016. }
  1017. for (const String &scan_file : p_scan_dir->files) {
  1018. String ext = scan_file.get_extension().to_lower();
  1019. if (!valid_extensions.has(ext)) {
  1020. p_progress.increment();
  1021. continue; //invalid
  1022. }
  1023. String path = p_scan_dir->full_path.path_join(scan_file);
  1024. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1025. fi->file = scan_file;
  1026. p_dir->files.push_back(fi);
  1027. if (r_processed_files) {
  1028. r_processed_files->insert(path);
  1029. }
  1030. FileCache *fc = file_cache.getptr(path);
  1031. uint64_t mt = FileAccess::get_modified_time(path);
  1032. if (_can_import_file(scan_file)) {
  1033. //is imported
  1034. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  1035. if (fc) {
  1036. fi->type = fc->type;
  1037. fi->resource_script_class = fc->resource_script_class;
  1038. fi->uid = fc->uid;
  1039. fi->deps = fc->deps;
  1040. fi->modified_time = mt;
  1041. fi->import_modified_time = import_mt;
  1042. fi->import_md5 = fc->import_md5;
  1043. fi->import_dest_paths = fc->import_dest_paths;
  1044. fi->import_valid = fc->import_valid;
  1045. fi->import_group_file = fc->import_group_file;
  1046. fi->class_info = fc->class_info;
  1047. // Ensures backward compatibility when the project is loaded for the first time with the added import_md5
  1048. // and import_dest_paths properties in the file cache.
  1049. if (fc->import_md5.is_empty()) {
  1050. fi->import_md5 = FileAccess::get_md5(path + ".import");
  1051. fi->import_dest_paths = _get_import_dest_paths(path);
  1052. }
  1053. // The method _is_test_for_reimport_needed checks if the files were modified and ensures that
  1054. // all the destination files still exist without reading the .import file.
  1055. // If something is different, we will queue a test for reimportation that will check
  1056. // the md5 of all files and import settings and, if necessary, execute a reimportation.
  1057. if (_is_test_for_reimport_needed(path, fc->modification_time, mt, fc->import_modification_time, import_mt, fi->import_dest_paths) ||
  1058. (revalidate_import_files && !ResourceFormatImporter::get_singleton()->are_import_settings_valid(path))) {
  1059. ItemAction ia;
  1060. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1061. ia.dir = p_dir;
  1062. ia.file = fi->file;
  1063. scan_actions.push_back(ia);
  1064. }
  1065. if (fc->type.is_empty()) {
  1066. fi->type = ResourceLoader::get_resource_type(path);
  1067. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  1068. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  1069. //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?)
  1070. //note: I think this should not happen any longer..
  1071. }
  1072. if (fc->uid == ResourceUID::INVALID_ID) {
  1073. // imported files should always have a UID, so attempt to fetch it.
  1074. fi->uid = ResourceLoader::get_resource_uid(path);
  1075. }
  1076. } else {
  1077. // Using get_resource_import_info() to prevent calling 3 times ResourceFormatImporter::_get_path_and_type.
  1078. ResourceFormatImporter::get_singleton()->get_resource_import_info(path, fi->type, fi->uid, fi->import_group_file);
  1079. fi->class_info = _get_global_script_class(fi->type, path);
  1080. fi->modified_time = 0;
  1081. fi->import_modified_time = 0;
  1082. fi->import_md5 = "";
  1083. fi->import_dest_paths = Vector<String>();
  1084. fi->import_valid = (fi->type == "TextFile" || fi->type == "OtherFile") ? true : ResourceLoader::is_import_valid(path);
  1085. ItemAction ia;
  1086. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1087. ia.dir = p_dir;
  1088. ia.file = fi->file;
  1089. scan_actions.push_back(ia);
  1090. }
  1091. } else {
  1092. if (fc && fc->modification_time == mt) {
  1093. //not imported, so just update type if changed
  1094. fi->type = fc->type;
  1095. fi->resource_script_class = fc->resource_script_class;
  1096. fi->uid = fc->uid;
  1097. fi->modified_time = mt;
  1098. fi->deps = fc->deps;
  1099. fi->import_modified_time = 0;
  1100. fi->import_md5 = "";
  1101. fi->import_dest_paths = Vector<String>();
  1102. fi->import_valid = true;
  1103. fi->class_info = fc->class_info;
  1104. if (first_scan && ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  1105. bool update_script = false;
  1106. String old_class_name = fi->class_info.name;
  1107. fi->class_info = _get_global_script_class(fi->type, path);
  1108. if (old_class_name != fi->class_info.name) {
  1109. update_script = true;
  1110. } else if (!fi->class_info.name.is_empty() && (!ScriptServer::is_global_class(fi->class_info.name) || ScriptServer::get_global_class_path(fi->class_info.name) != path)) {
  1111. // This script has a class name but is not in the global class names or the path of the class has changed.
  1112. update_script = true;
  1113. }
  1114. if (update_script) {
  1115. _queue_update_script_class(path, ScriptClassInfoUpdate::from_file_info(fi));
  1116. }
  1117. }
  1118. } else {
  1119. //new or modified time
  1120. fi->type = ResourceLoader::get_resource_type(path);
  1121. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  1122. if (fi->type == "" && textfile_extensions.has(ext)) {
  1123. fi->type = "TextFile";
  1124. }
  1125. if (fi->type == "" && other_file_extensions.has(ext)) {
  1126. fi->type = "OtherFile";
  1127. }
  1128. fi->uid = ResourceLoader::get_resource_uid(path);
  1129. fi->class_info = _get_global_script_class(fi->type, path);
  1130. fi->deps = _get_dependencies(path);
  1131. fi->modified_time = mt;
  1132. fi->import_modified_time = 0;
  1133. fi->import_md5 = "";
  1134. fi->import_dest_paths = Vector<String>();
  1135. fi->import_valid = true;
  1136. // Files in dep_update_list are forced for rescan to update dependencies. They don't need other updates.
  1137. if (!dep_update_list.has(path)) {
  1138. if (ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  1139. _queue_update_script_class(path, ScriptClassInfoUpdate::from_file_info(fi));
  1140. }
  1141. if (fi->type == SNAME("PackedScene")) {
  1142. _queue_update_scene_groups(path);
  1143. }
  1144. }
  1145. }
  1146. if (ResourceLoader::should_create_uid_file(path)) {
  1147. // Create a UID file and new UID, if it's invalid.
  1148. Ref<FileAccess> f = FileAccess::open(path + ".uid", FileAccess::WRITE);
  1149. if (f.is_valid()) {
  1150. if (fi->uid == ResourceUID::INVALID_ID) {
  1151. fi->uid = ResourceUID::get_singleton()->create_id_for_path(path);
  1152. } else {
  1153. WARN_PRINT(vformat("Missing .uid file for path \"%s\". The file was re-created from cache.", path));
  1154. }
  1155. f->store_line(ResourceUID::get_singleton()->id_to_text(fi->uid));
  1156. }
  1157. }
  1158. }
  1159. if (fi->uid != ResourceUID::INVALID_ID) {
  1160. if (ResourceUID::get_singleton()->has_id(fi->uid)) {
  1161. // Restrict UID dupe warning to first-scan since we know there are no file moves going on yet.
  1162. if (first_scan) {
  1163. // Warn if we detect files with duplicate UIDs.
  1164. const String other_path = ResourceUID::get_singleton()->get_id_path(fi->uid);
  1165. if (other_path != path) {
  1166. WARN_PRINT(vformat("UID duplicate detected between %s and %s.", path, other_path));
  1167. }
  1168. }
  1169. ResourceUID::get_singleton()->set_id(fi->uid, path);
  1170. } else {
  1171. ResourceUID::get_singleton()->add_id(fi->uid, path);
  1172. }
  1173. }
  1174. p_progress.increment();
  1175. }
  1176. }
  1177. void EditorFileSystem::_process_removed_files(const HashSet<String> &p_processed_files) {
  1178. for (const KeyValue<String, EditorFileSystem::FileCache> &kv : file_cache) {
  1179. if (!p_processed_files.has(kv.key)) {
  1180. if (ClassDB::is_parent_class(kv.value.type, SNAME("Script"))) {
  1181. // A script has been removed from disk since the last startup. The documentation needs to be updated.
  1182. // There's no need to add the path in update_script_paths since that is exclusively for updating global class names,
  1183. // which is handled in _first_scan_filesystem before the full scan to ensure plugins and autoloads can be created.
  1184. MutexLock update_script_lock(update_script_mutex);
  1185. update_script_paths_documentation.insert(kv.key);
  1186. }
  1187. }
  1188. }
  1189. }
  1190. void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, bool p_recursive) {
  1191. uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
  1192. bool updated_dir = false;
  1193. String cd = p_dir->get_path();
  1194. int diff_nb_files = 0;
  1195. if (current_mtime != p_dir->modified_time || using_fat32_or_exfat) {
  1196. updated_dir = true;
  1197. p_dir->modified_time = current_mtime;
  1198. //ooooops, dir changed, see what's going on
  1199. //first mark everything as verified
  1200. for (int i = 0; i < p_dir->files.size(); i++) {
  1201. p_dir->files[i]->verified = false;
  1202. }
  1203. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1204. p_dir->get_subdir(i)->verified = false;
  1205. }
  1206. diff_nb_files -= p_dir->files.size();
  1207. //then scan files and directories and check what's different
  1208. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1209. Error ret = da->change_dir(cd);
  1210. ERR_FAIL_COND_MSG(ret != OK, "Cannot change to '" + cd + "' folder.");
  1211. da->list_dir_begin();
  1212. while (true) {
  1213. String f = da->get_next();
  1214. if (f.is_empty()) {
  1215. break;
  1216. }
  1217. if (da->current_is_hidden()) {
  1218. continue;
  1219. }
  1220. if (da->current_is_dir()) {
  1221. if (f.begins_with(".")) { // Ignore special and . / ..
  1222. continue;
  1223. }
  1224. int idx = p_dir->find_dir_index(f);
  1225. if (idx == -1) {
  1226. String dir_path = cd.path_join(f);
  1227. if (_should_skip_directory(dir_path)) {
  1228. continue;
  1229. }
  1230. ScannedDirectory sd;
  1231. sd.name = f;
  1232. sd.full_path = dir_path;
  1233. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  1234. efd->parent = p_dir;
  1235. efd->name = f;
  1236. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1237. d->change_dir(dir_path);
  1238. int nb_files_dir = _scan_new_dir(&sd, d);
  1239. p_progress.hi += nb_files_dir;
  1240. diff_nb_files += nb_files_dir;
  1241. _process_file_system(&sd, efd, p_progress, nullptr);
  1242. ItemAction ia;
  1243. ia.action = ItemAction::ACTION_DIR_ADD;
  1244. ia.dir = p_dir;
  1245. ia.file = f;
  1246. ia.new_dir = efd;
  1247. scan_actions.push_back(ia);
  1248. } else {
  1249. p_dir->subdirs[idx]->verified = true;
  1250. }
  1251. } else {
  1252. String ext = f.get_extension().to_lower();
  1253. if (!valid_extensions.has(ext)) {
  1254. continue; //invalid
  1255. }
  1256. int idx = p_dir->find_file_index(f);
  1257. if (idx == -1) {
  1258. //never seen this file, add actition to add it
  1259. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  1260. fi->file = f;
  1261. String path = cd.path_join(fi->file);
  1262. fi->modified_time = FileAccess::get_modified_time(path);
  1263. fi->import_modified_time = 0;
  1264. fi->import_md5 = "";
  1265. fi->import_dest_paths = Vector<String>();
  1266. fi->type = ResourceLoader::get_resource_type(path);
  1267. fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
  1268. if (fi->type == "" && textfile_extensions.has(ext)) {
  1269. fi->type = "TextFile";
  1270. }
  1271. if (fi->type == "" && other_file_extensions.has(ext)) {
  1272. fi->type = "OtherFile";
  1273. }
  1274. fi->class_info = _get_global_script_class(fi->type, path);
  1275. fi->import_valid = (fi->type == "TextFile" || fi->type == "OtherFile") ? true : ResourceLoader::is_import_valid(path);
  1276. fi->import_group_file = ResourceLoader::get_import_group_file(path);
  1277. {
  1278. ItemAction ia;
  1279. ia.action = ItemAction::ACTION_FILE_ADD;
  1280. ia.dir = p_dir;
  1281. ia.file = f;
  1282. ia.new_file = fi;
  1283. scan_actions.push_back(ia);
  1284. }
  1285. if (_can_import_file(f)) {
  1286. //if it can be imported, and it was added, it needs to be reimported
  1287. ItemAction ia;
  1288. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1289. ia.dir = p_dir;
  1290. ia.file = f;
  1291. scan_actions.push_back(ia);
  1292. }
  1293. diff_nb_files++;
  1294. } else {
  1295. p_dir->files[idx]->verified = true;
  1296. }
  1297. }
  1298. }
  1299. da->list_dir_end();
  1300. }
  1301. for (int i = 0; i < p_dir->files.size(); i++) {
  1302. if (updated_dir && !p_dir->files[i]->verified) {
  1303. //this file was removed, add action to remove it
  1304. ItemAction ia;
  1305. ia.action = ItemAction::ACTION_FILE_REMOVE;
  1306. ia.dir = p_dir;
  1307. ia.file = p_dir->files[i]->file;
  1308. scan_actions.push_back(ia);
  1309. diff_nb_files--;
  1310. continue;
  1311. }
  1312. String path = cd.path_join(p_dir->files[i]->file);
  1313. if (_can_import_file(p_dir->files[i]->file)) {
  1314. // Check here if file must be imported or not.
  1315. // Same logic as in _process_file_system, the last modifications dates
  1316. // needs to be trusted to prevent reading all the .import files and the md5
  1317. // each time the user switch back to Godot.
  1318. uint64_t mt = FileAccess::get_modified_time(path);
  1319. uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
  1320. if (_is_test_for_reimport_needed(path, p_dir->files[i]->modified_time, mt, p_dir->files[i]->import_modified_time, import_mt, p_dir->files[i]->import_dest_paths)) {
  1321. ItemAction ia;
  1322. ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
  1323. ia.dir = p_dir;
  1324. ia.file = p_dir->files[i]->file;
  1325. scan_actions.push_back(ia);
  1326. }
  1327. } else {
  1328. uint64_t mt = FileAccess::get_modified_time(path);
  1329. if (mt != p_dir->files[i]->modified_time) {
  1330. p_dir->files[i]->modified_time = mt; //save new time, but test for reload
  1331. ItemAction ia;
  1332. ia.action = ItemAction::ACTION_FILE_RELOAD;
  1333. ia.dir = p_dir;
  1334. ia.file = p_dir->files[i]->file;
  1335. scan_actions.push_back(ia);
  1336. }
  1337. }
  1338. p_progress.increment();
  1339. }
  1340. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1341. if ((updated_dir && !p_dir->subdirs[i]->verified) || _should_skip_directory(p_dir->subdirs[i]->get_path())) {
  1342. // Add all the files of the folder to be sure _update_scan_actions process the removed files
  1343. // for global class names.
  1344. diff_nb_files += _insert_actions_delete_files_directory(p_dir->subdirs[i]);
  1345. //this directory was removed or ignored, add action to remove it
  1346. ItemAction ia;
  1347. ia.action = ItemAction::ACTION_DIR_REMOVE;
  1348. ia.dir = p_dir->subdirs[i];
  1349. scan_actions.push_back(ia);
  1350. continue;
  1351. }
  1352. if (p_recursive) {
  1353. _scan_fs_changes(p_dir->get_subdir(i), p_progress);
  1354. }
  1355. }
  1356. nb_files_total = MAX(nb_files_total + diff_nb_files, 0);
  1357. }
  1358. void EditorFileSystem::_delete_internal_files(const String &p_file) {
  1359. if (FileAccess::exists(p_file + ".import")) {
  1360. List<String> paths;
  1361. ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
  1362. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1363. for (const String &E : paths) {
  1364. da->remove(E);
  1365. }
  1366. da->remove(p_file + ".import");
  1367. }
  1368. if (FileAccess::exists(p_file + ".uid")) {
  1369. DirAccess::remove_absolute(p_file + ".uid");
  1370. }
  1371. }
  1372. int EditorFileSystem::_insert_actions_delete_files_directory(EditorFileSystemDirectory *p_dir) {
  1373. int nb_files = 0;
  1374. for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
  1375. ItemAction ia;
  1376. ia.action = ItemAction::ACTION_FILE_REMOVE;
  1377. ia.dir = p_dir;
  1378. ia.file = fi->file;
  1379. scan_actions.push_back(ia);
  1380. nb_files++;
  1381. }
  1382. for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
  1383. nb_files += _insert_actions_delete_files_directory(sub_dir);
  1384. }
  1385. return nb_files;
  1386. }
  1387. void EditorFileSystem::_thread_func_sources(void *_userdata) {
  1388. EditorFileSystem *efs = (EditorFileSystem *)_userdata;
  1389. if (efs->filesystem) {
  1390. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  1391. ScanProgress sp;
  1392. sp.progress = &pr;
  1393. sp.hi = efs->nb_files_total;
  1394. efs->_scan_fs_changes(efs->filesystem, sp);
  1395. }
  1396. efs->scanning_changes_done.set();
  1397. }
  1398. bool EditorFileSystem::_remove_invalid_global_class_names(const HashSet<String> &p_existing_class_names) {
  1399. List<StringName> global_classes;
  1400. bool must_save = false;
  1401. ScriptServer::get_global_class_list(&global_classes);
  1402. for (const StringName &class_name : global_classes) {
  1403. if (!p_existing_class_names.has(class_name)) {
  1404. ScriptServer::remove_global_class(class_name);
  1405. must_save = true;
  1406. }
  1407. }
  1408. return must_save;
  1409. }
  1410. String EditorFileSystem::_get_file_by_class_name(EditorFileSystemDirectory *p_dir, const String &p_class_name, EditorFileSystemDirectory::FileInfo *&r_file_info) {
  1411. for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
  1412. if (fi->class_info.name == p_class_name) {
  1413. r_file_info = fi;
  1414. return p_dir->get_path().path_join(fi->file);
  1415. }
  1416. }
  1417. for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
  1418. String file = _get_file_by_class_name(sub_dir, p_class_name, r_file_info);
  1419. if (!file.is_empty()) {
  1420. return file;
  1421. }
  1422. }
  1423. r_file_info = nullptr;
  1424. return "";
  1425. }
  1426. void EditorFileSystem::scan_changes() {
  1427. if (first_scan || // Prevent a premature changes scan from inhibiting the first full scan
  1428. scanning || scanning_changes || thread.is_started()) {
  1429. scan_changes_pending = true;
  1430. set_process(true);
  1431. return;
  1432. }
  1433. _update_extensions();
  1434. sources_changed.clear();
  1435. scanning_changes = true;
  1436. scanning_changes_done.clear();
  1437. if (!use_threads) {
  1438. if (filesystem) {
  1439. EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
  1440. ScanProgress sp;
  1441. sp.progress = &pr;
  1442. sp.hi = nb_files_total;
  1443. scan_total = 0;
  1444. _scan_fs_changes(filesystem, sp);
  1445. if (_update_scan_actions()) {
  1446. emit_signal(SNAME("filesystem_changed"));
  1447. }
  1448. }
  1449. scanning_changes = false;
  1450. scanning_changes_done.set();
  1451. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1452. } else {
  1453. ERR_FAIL_COND(thread_sources.is_started());
  1454. set_process(true);
  1455. scan_total = 0;
  1456. Thread::Settings s;
  1457. s.priority = Thread::PRIORITY_LOW;
  1458. thread_sources.start(_thread_func_sources, this, s);
  1459. }
  1460. }
  1461. void EditorFileSystem::_notification(int p_what) {
  1462. switch (p_what) {
  1463. case NOTIFICATION_EXIT_TREE: {
  1464. Thread &active_thread = thread.is_started() ? thread : thread_sources;
  1465. if (use_threads && active_thread.is_started()) {
  1466. while (scanning) {
  1467. OS::get_singleton()->delay_usec(1000);
  1468. }
  1469. active_thread.wait_to_finish();
  1470. WARN_PRINT("Scan thread aborted...");
  1471. set_process(false);
  1472. }
  1473. if (filesystem) {
  1474. memdelete(filesystem);
  1475. }
  1476. if (new_filesystem) {
  1477. memdelete(new_filesystem);
  1478. }
  1479. filesystem = nullptr;
  1480. new_filesystem = nullptr;
  1481. } break;
  1482. case NOTIFICATION_PROCESS: {
  1483. if (use_threads) {
  1484. /** This hack exists because of the EditorProgress nature
  1485. * of processing events recursively. This needs to be rewritten
  1486. * at some point entirely, but in the meantime the following
  1487. * hack prevents deadlock on import.
  1488. */
  1489. static bool prevent_recursive_process_hack = false;
  1490. if (prevent_recursive_process_hack) {
  1491. break;
  1492. }
  1493. prevent_recursive_process_hack = true;
  1494. bool done_importing = false;
  1495. if (scanning_changes) {
  1496. if (scanning_changes_done.is_set()) {
  1497. set_process(false);
  1498. if (thread_sources.is_started()) {
  1499. thread_sources.wait_to_finish();
  1500. }
  1501. bool changed = _update_scan_actions();
  1502. // Set first_scan to false before the signals so the function doing_first_scan can return false
  1503. // in editor_node to start the export if needed.
  1504. first_scan = false;
  1505. ResourceImporter::load_on_startup = nullptr;
  1506. if (changed) {
  1507. emit_signal(SNAME("filesystem_changed"));
  1508. }
  1509. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1510. scanning_changes = false; // Changed to false here to prevent recursive triggering of scan thread.
  1511. done_importing = true;
  1512. }
  1513. } else if (!scanning && thread.is_started()) {
  1514. set_process(false);
  1515. if (filesystem) {
  1516. memdelete(filesystem);
  1517. }
  1518. filesystem = new_filesystem;
  1519. new_filesystem = nullptr;
  1520. thread.wait_to_finish();
  1521. _update_scan_actions();
  1522. // Update all icons so they are loaded for the FileSystemDock.
  1523. _update_files_icon_path();
  1524. // Set first_scan to false before the signals so the function doing_first_scan can return false
  1525. // in editor_node to start the export if needed.
  1526. first_scan = false;
  1527. ResourceImporter::load_on_startup = nullptr;
  1528. emit_signal(SNAME("filesystem_changed"));
  1529. emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
  1530. }
  1531. if (done_importing && scan_changes_pending) {
  1532. scan_changes_pending = false;
  1533. scan_changes();
  1534. }
  1535. prevent_recursive_process_hack = false;
  1536. }
  1537. } break;
  1538. }
  1539. }
  1540. bool EditorFileSystem::is_scanning() const {
  1541. return scanning || scanning_changes || first_scan;
  1542. }
  1543. float EditorFileSystem::get_scanning_progress() const {
  1544. return scan_total;
  1545. }
  1546. EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
  1547. return filesystem;
  1548. }
  1549. void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file) {
  1550. if (!p_dir) {
  1551. return; //none
  1552. }
  1553. p_file->store_line("::" + p_dir->get_path() + "::" + String::num_int64(p_dir->modified_time));
  1554. for (int i = 0; i < p_dir->files.size(); i++) {
  1555. const EditorFileSystemDirectory::FileInfo *file_info = p_dir->files[i];
  1556. if (!file_info->import_group_file.is_empty()) {
  1557. group_file_cache.insert(file_info->import_group_file);
  1558. }
  1559. String type = file_info->type;
  1560. if (file_info->resource_script_class) {
  1561. type += "/" + String(file_info->resource_script_class);
  1562. }
  1563. PackedStringArray cache_string;
  1564. cache_string.append(file_info->file);
  1565. cache_string.append(type);
  1566. cache_string.append(itos(file_info->uid));
  1567. cache_string.append(itos(file_info->modified_time));
  1568. cache_string.append(itos(file_info->import_modified_time));
  1569. cache_string.append(itos(file_info->import_valid));
  1570. cache_string.append(file_info->import_group_file);
  1571. cache_string.append(String("<>").join({ file_info->class_info.name, file_info->class_info.extends, file_info->class_info.icon_path, itos(file_info->class_info.is_abstract), itos(file_info->class_info.is_tool), file_info->import_md5, String("<*>").join(file_info->import_dest_paths) }));
  1572. cache_string.append(String("<>").join(file_info->deps));
  1573. p_file->store_line(String("::").join(cache_string));
  1574. }
  1575. for (int i = 0; i < p_dir->subdirs.size(); i++) {
  1576. _save_filesystem_cache(p_dir->subdirs[i], p_file);
  1577. }
  1578. }
  1579. bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
  1580. //todo make faster
  1581. if (!filesystem || scanning) {
  1582. return false;
  1583. }
  1584. String f = ProjectSettings::get_singleton()->localize_path(p_file);
  1585. // Note: Only checks if base directory is case sensitive.
  1586. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1587. bool fs_case_sensitive = dir->is_case_sensitive("res://");
  1588. if (!f.begins_with("res://")) {
  1589. return false;
  1590. }
  1591. f = f.substr(6);
  1592. f = f.replace_char('\\', '/');
  1593. Vector<String> path = f.split("/");
  1594. if (path.is_empty()) {
  1595. return false;
  1596. }
  1597. String file = path[path.size() - 1];
  1598. path.resize(path.size() - 1);
  1599. EditorFileSystemDirectory *fs = filesystem;
  1600. for (int i = 0; i < path.size(); i++) {
  1601. if (path[i].begins_with(".")) {
  1602. return false;
  1603. }
  1604. int idx = -1;
  1605. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1606. if (fs_case_sensitive) {
  1607. if (fs->get_subdir(j)->get_name() == path[i]) {
  1608. idx = j;
  1609. break;
  1610. }
  1611. } else {
  1612. if (fs->get_subdir(j)->get_name().to_lower() == path[i].to_lower()) {
  1613. idx = j;
  1614. break;
  1615. }
  1616. }
  1617. }
  1618. if (idx == -1) {
  1619. // Only create a missing directory in memory when it exists on disk.
  1620. if (!dir->dir_exists(fs->get_path().path_join(path[i]))) {
  1621. return false;
  1622. }
  1623. EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
  1624. efsd->name = path[i];
  1625. efsd->parent = fs;
  1626. int idx2 = 0;
  1627. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1628. if (efsd->name.filenocasecmp_to(fs->get_subdir(j)->get_name()) < 0) {
  1629. break;
  1630. }
  1631. idx2++;
  1632. }
  1633. if (idx2 == fs->get_subdir_count()) {
  1634. fs->subdirs.push_back(efsd);
  1635. } else {
  1636. fs->subdirs.insert(idx2, efsd);
  1637. }
  1638. fs = efsd;
  1639. } else {
  1640. fs = fs->get_subdir(idx);
  1641. }
  1642. }
  1643. int cpos = -1;
  1644. for (int i = 0; i < fs->files.size(); i++) {
  1645. if (fs->files[i]->file == file) {
  1646. cpos = i;
  1647. break;
  1648. }
  1649. }
  1650. r_file_pos = cpos;
  1651. *r_d = fs;
  1652. return cpos != -1;
  1653. }
  1654. String EditorFileSystem::get_file_type(const String &p_file) const {
  1655. EditorFileSystemDirectory *fs = nullptr;
  1656. int cpos = -1;
  1657. if (!_find_file(p_file, &fs, cpos)) {
  1658. return "";
  1659. }
  1660. return fs->files[cpos]->type;
  1661. }
  1662. EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
  1663. if (!filesystem || scanning) {
  1664. return nullptr;
  1665. }
  1666. EditorFileSystemDirectory *fs = nullptr;
  1667. int cpos = -1;
  1668. if (!_find_file(p_file, &fs, cpos)) {
  1669. return nullptr;
  1670. }
  1671. if (r_index) {
  1672. *r_index = cpos;
  1673. }
  1674. return fs;
  1675. }
  1676. ResourceUID::ID EditorFileSystem::get_file_uid(const String &p_path) const {
  1677. int file_idx;
  1678. EditorFileSystemDirectory *directory = find_file(p_path, &file_idx);
  1679. if (!directory) {
  1680. return ResourceUID::INVALID_ID;
  1681. }
  1682. return directory->files[file_idx]->uid;
  1683. }
  1684. EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
  1685. if (!filesystem || scanning) {
  1686. return nullptr;
  1687. }
  1688. String f = ProjectSettings::get_singleton()->localize_path(p_path);
  1689. if (!f.begins_with("res://")) {
  1690. return nullptr;
  1691. }
  1692. f = f.substr(6);
  1693. f = f.replace_char('\\', '/');
  1694. if (f.is_empty()) {
  1695. return filesystem;
  1696. }
  1697. if (f.ends_with("/")) {
  1698. f = f.substr(0, f.length() - 1);
  1699. }
  1700. Vector<String> path = f.split("/");
  1701. if (path.is_empty()) {
  1702. return nullptr;
  1703. }
  1704. EditorFileSystemDirectory *fs = filesystem;
  1705. for (int i = 0; i < path.size(); i++) {
  1706. int idx = -1;
  1707. for (int j = 0; j < fs->get_subdir_count(); j++) {
  1708. if (fs->get_subdir(j)->get_name() == path[i]) {
  1709. idx = j;
  1710. break;
  1711. }
  1712. }
  1713. if (idx == -1) {
  1714. return nullptr;
  1715. } else {
  1716. fs = fs->get_subdir(idx);
  1717. }
  1718. }
  1719. return fs;
  1720. }
  1721. void EditorFileSystem::_save_late_updated_files() {
  1722. //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
  1723. String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
  1724. Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
  1725. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
  1726. for (const String &E : late_update_files) {
  1727. f->store_line(E);
  1728. }
  1729. }
  1730. Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
  1731. // Avoid error spam on first opening of a not yet imported project by treating the following situation
  1732. // as a benign one, not letting the file open error happen: the resource is of an importable type but
  1733. // it has not been imported yet.
  1734. if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
  1735. const String &internal_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
  1736. if (!internal_path.is_empty() && !FileAccess::exists(internal_path)) { // If path is empty (error), keep the code flow to the error.
  1737. return Vector<String>();
  1738. }
  1739. }
  1740. List<String> deps;
  1741. ResourceLoader::get_dependencies(p_path, &deps);
  1742. Vector<String> ret;
  1743. for (const String &E : deps) {
  1744. ret.push_back(E);
  1745. }
  1746. return ret;
  1747. }
  1748. EditorFileSystem::ScriptClassInfo EditorFileSystem::_get_global_script_class(const String &p_type, const String &p_path) const {
  1749. ScriptClassInfo info;
  1750. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1751. if (ScriptServer::get_language(i)->handles_global_class_type(p_type)) {
  1752. info.name = ScriptServer::get_language(i)->get_global_class_name(p_path, &info.extends, &info.icon_path, &info.is_abstract, &info.is_tool);
  1753. break;
  1754. }
  1755. }
  1756. return info;
  1757. }
  1758. void EditorFileSystem::_update_file_icon_path(EditorFileSystemDirectory::FileInfo *file_info) {
  1759. String icon_path;
  1760. if (file_info->resource_script_class != StringName()) {
  1761. icon_path = EditorNode::get_editor_data().script_class_get_icon_path(file_info->resource_script_class);
  1762. } else if (file_info->class_info.icon_path.is_empty() && !file_info->deps.is_empty()) {
  1763. const String &script_dep = file_info->deps[0]; // Assuming the first dependency is a script.
  1764. const String &script_path = script_dep.contains("::") ? script_dep.get_slice("::", 2) : script_dep;
  1765. if (!script_path.is_empty()) {
  1766. String *cached = file_icon_cache.getptr(script_path);
  1767. if (cached) {
  1768. icon_path = *cached;
  1769. } else {
  1770. if (ClassDB::is_parent_class(ResourceLoader::get_resource_type(script_path), SNAME("Script"))) {
  1771. int script_file;
  1772. EditorFileSystemDirectory *efsd = find_file(script_path, &script_file);
  1773. if (efsd) {
  1774. icon_path = efsd->files[script_file]->class_info.icon_path;
  1775. }
  1776. }
  1777. file_icon_cache.insert(script_path, icon_path);
  1778. }
  1779. }
  1780. }
  1781. if (icon_path.is_empty() && !file_info->type.is_empty()) {
  1782. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(file_info->type);
  1783. if (icon.is_valid()) {
  1784. icon_path = icon->get_path();
  1785. }
  1786. }
  1787. file_info->class_info.icon_path = icon_path;
  1788. }
  1789. void EditorFileSystem::_update_files_icon_path(EditorFileSystemDirectory *edp) {
  1790. if (!edp) {
  1791. edp = filesystem;
  1792. file_icon_cache.clear();
  1793. }
  1794. for (EditorFileSystemDirectory *sub_dir : edp->subdirs) {
  1795. _update_files_icon_path(sub_dir);
  1796. }
  1797. for (EditorFileSystemDirectory::FileInfo *fi : edp->files) {
  1798. _update_file_icon_path(fi);
  1799. }
  1800. }
  1801. void EditorFileSystem::_update_script_classes() {
  1802. if (update_script_paths.is_empty()) {
  1803. // Ensure the global class file is always present; it's essential for exports to work.
  1804. if (!FileAccess::exists(ProjectSettings::get_singleton()->get_global_class_list_path())) {
  1805. ScriptServer::save_global_classes();
  1806. }
  1807. return;
  1808. }
  1809. {
  1810. MutexLock update_script_lock(update_script_mutex);
  1811. EditorProgress *ep = nullptr;
  1812. if (update_script_paths.size() > 1) {
  1813. if (MessageQueue::get_singleton()->is_flushing()) {
  1814. // Use background progress when message queue is flushing.
  1815. ep = memnew(EditorProgress("update_scripts_classes", TTR("Registering global classes..."), update_script_paths.size(), false, true));
  1816. } else {
  1817. ep = memnew(EditorProgress("update_scripts_classes", TTR("Registering global classes..."), update_script_paths.size()));
  1818. }
  1819. }
  1820. int step_count = 0;
  1821. for (const KeyValue<String, ScriptClassInfoUpdate> &E : update_script_paths) {
  1822. _register_global_class_script(E.key, E.key, E.value);
  1823. if (ep) {
  1824. ep->step(E.value.name, step_count++, false);
  1825. }
  1826. }
  1827. memdelete_notnull(ep);
  1828. update_script_paths.clear();
  1829. }
  1830. EditorNode::get_editor_data().script_class_save_global_classes();
  1831. emit_signal("script_classes_updated");
  1832. // Rescan custom loaders and savers.
  1833. // Doing the following here because the `filesystem_changed` signal fires multiple times and isn't always followed by script classes update.
  1834. // So I thought it's better to do this when script classes really get updated
  1835. ResourceLoader::remove_custom_loaders();
  1836. ResourceLoader::add_custom_loaders();
  1837. ResourceSaver::remove_custom_savers();
  1838. ResourceSaver::add_custom_savers();
  1839. }
  1840. void EditorFileSystem::_update_script_documentation() {
  1841. if (update_script_paths_documentation.is_empty()) {
  1842. return;
  1843. }
  1844. MutexLock update_script_lock(update_script_mutex);
  1845. EditorProgress *ep = nullptr;
  1846. if (update_script_paths_documentation.size() > 1) {
  1847. if (MessageQueue::get_singleton()->is_flushing()) {
  1848. // Use background progress when message queue is flushing.
  1849. ep = memnew(EditorProgress("update_script_paths_documentation", TTR("Updating scripts documentation"), update_script_paths_documentation.size(), false, true));
  1850. } else {
  1851. ep = memnew(EditorProgress("update_script_paths_documentation", TTR("Updating scripts documentation"), update_script_paths_documentation.size()));
  1852. }
  1853. }
  1854. int step_count = 0;
  1855. for (const String &path : update_script_paths_documentation) {
  1856. int index = -1;
  1857. EditorFileSystemDirectory *efd = find_file(path, &index);
  1858. if (!efd || index < 0) {
  1859. // The file was removed
  1860. EditorHelp::remove_script_doc_by_path(path);
  1861. continue;
  1862. }
  1863. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1864. ScriptLanguage *lang = ScriptServer::get_language(i);
  1865. if (lang->supports_documentation() && efd->files[index]->type == lang->get_type()) {
  1866. bool should_reload_script = _should_reload_script(path);
  1867. Ref<Script> scr = ResourceLoader::load(path);
  1868. if (scr.is_null()) {
  1869. continue;
  1870. }
  1871. if (should_reload_script) {
  1872. // Reloading the script from disk. Otherwise, the ResourceLoader::load will
  1873. // return the last loaded version of the script (without the modifications).
  1874. scr->reload_from_file();
  1875. }
  1876. for (const DocData::ClassDoc &cd : scr->get_documentation()) {
  1877. EditorHelp::add_doc(cd);
  1878. if (!first_scan) {
  1879. // Update the documentation in the Script Editor if it is open.
  1880. ScriptEditor::get_singleton()->update_doc(cd.name);
  1881. }
  1882. }
  1883. }
  1884. }
  1885. if (ep) {
  1886. ep->step(efd->files[index]->file, step_count++, false);
  1887. }
  1888. }
  1889. memdelete_notnull(ep);
  1890. update_script_paths_documentation.clear();
  1891. }
  1892. bool EditorFileSystem::_should_reload_script(const String &p_path) {
  1893. if (first_scan) {
  1894. return false;
  1895. }
  1896. Ref<Script> scr = ResourceCache::get_ref(p_path);
  1897. if (scr.is_null()) {
  1898. // Not a script or not already loaded.
  1899. return false;
  1900. }
  1901. // Scripts are reloaded via the script editor if they are currently opened.
  1902. if (ScriptEditor::get_singleton()->get_open_scripts().has(scr)) {
  1903. return false;
  1904. }
  1905. return true;
  1906. }
  1907. void EditorFileSystem::_process_update_pending() {
  1908. _update_script_classes();
  1909. // Parse documentation second, as it requires the class names to be loaded
  1910. // because _update_script_documentation loads the scripts completely.
  1911. _update_script_documentation();
  1912. _update_pending_scene_groups();
  1913. }
  1914. void EditorFileSystem::_queue_update_script_class(const String &p_path, const ScriptClassInfoUpdate &p_script_update) {
  1915. MutexLock update_script_lock(update_script_mutex);
  1916. update_script_paths.insert(p_path, p_script_update);
  1917. update_script_paths_documentation.insert(p_path);
  1918. }
  1919. void EditorFileSystem::_update_scene_groups() {
  1920. if (update_scene_paths.is_empty()) {
  1921. return;
  1922. }
  1923. EditorProgress *ep = nullptr;
  1924. if (update_scene_paths.size() > 20) {
  1925. ep = memnew(EditorProgress("update_scene_groups", TTR("Updating Scene Groups"), update_scene_paths.size()));
  1926. }
  1927. int step_count = 0;
  1928. {
  1929. MutexLock update_scene_lock(update_scene_mutex);
  1930. for (const String &path : update_scene_paths) {
  1931. ProjectSettings::get_singleton()->remove_scene_groups_cache(path);
  1932. int index = -1;
  1933. EditorFileSystemDirectory *efd = find_file(path, &index);
  1934. if (!efd || index < 0) {
  1935. // The file was removed.
  1936. continue;
  1937. }
  1938. const HashSet<StringName> scene_groups = PackedScene::get_scene_groups(path);
  1939. if (!scene_groups.is_empty()) {
  1940. ProjectSettings::get_singleton()->add_scene_groups_cache(path, scene_groups);
  1941. }
  1942. if (ep) {
  1943. ep->step(efd->files[index]->file, step_count++, false);
  1944. }
  1945. }
  1946. memdelete_notnull(ep);
  1947. update_scene_paths.clear();
  1948. }
  1949. ProjectSettings::get_singleton()->save_scene_groups_cache();
  1950. }
  1951. void EditorFileSystem::_update_pending_scene_groups() {
  1952. if (!FileAccess::exists(ProjectSettings::get_singleton()->get_scene_groups_cache_path())) {
  1953. _get_all_scenes(get_filesystem(), update_scene_paths);
  1954. _update_scene_groups();
  1955. } else if (!update_scene_paths.is_empty()) {
  1956. _update_scene_groups();
  1957. }
  1958. }
  1959. void EditorFileSystem::_queue_update_scene_groups(const String &p_path) {
  1960. MutexLock update_scene_lock(update_scene_mutex);
  1961. update_scene_paths.insert(p_path);
  1962. }
  1963. void EditorFileSystem::_get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet<String> &r_list) {
  1964. for (int i = 0; i < p_dir->get_file_count(); i++) {
  1965. if (p_dir->get_file_type(i) == SNAME("PackedScene")) {
  1966. r_list.insert(p_dir->get_file_path(i));
  1967. }
  1968. }
  1969. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1970. _get_all_scenes(p_dir->get_subdir(i), r_list);
  1971. }
  1972. }
  1973. void EditorFileSystem::update_file(const String &p_file) {
  1974. ERR_FAIL_COND(p_file.is_empty());
  1975. update_files({ p_file });
  1976. }
  1977. void EditorFileSystem::update_files(const Vector<String> &p_script_paths) {
  1978. bool updated = false;
  1979. bool update_files_icon_cache = false;
  1980. Vector<EditorFileSystemDirectory::FileInfo *> files_to_update_icon_path;
  1981. for (const String &file : p_script_paths) {
  1982. ERR_CONTINUE(file.is_empty());
  1983. EditorFileSystemDirectory *fs = nullptr;
  1984. int cpos = -1;
  1985. if (!_find_file(file, &fs, cpos)) {
  1986. if (!fs) {
  1987. continue;
  1988. }
  1989. }
  1990. if (!FileAccess::exists(file)) {
  1991. //was removed
  1992. _delete_internal_files(file);
  1993. if (cpos != -1) { // Might've never been part of the editor file system (*.* files deleted in Open dialog).
  1994. if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  1995. if (ResourceUID::get_singleton()->has_id(fs->files[cpos]->uid)) {
  1996. ResourceUID::get_singleton()->remove_id(fs->files[cpos]->uid);
  1997. }
  1998. }
  1999. if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
  2000. ScriptClassInfoUpdate update;
  2001. update.type = fs->files[cpos]->type;
  2002. _queue_update_script_class(file, update);
  2003. if (!fs->files[cpos]->class_info.icon_path.is_empty()) {
  2004. update_files_icon_cache = true;
  2005. }
  2006. }
  2007. if (fs->files[cpos]->type == SNAME("PackedScene")) {
  2008. _queue_update_scene_groups(file);
  2009. }
  2010. memdelete(fs->files[cpos]);
  2011. fs->files.remove_at(cpos);
  2012. updated = true;
  2013. }
  2014. } else {
  2015. String type = ResourceLoader::get_resource_type(file);
  2016. if (type.is_empty() && textfile_extensions.has(file.get_extension())) {
  2017. type = "TextFile";
  2018. }
  2019. if (type.is_empty() && other_file_extensions.has(file.get_extension())) {
  2020. type = "OtherFile";
  2021. }
  2022. String script_class = ResourceLoader::get_resource_script_class(file);
  2023. ResourceUID::ID uid = ResourceLoader::get_resource_uid(file);
  2024. if (cpos == -1) {
  2025. // The file did not exist, it was added.
  2026. int idx = 0;
  2027. String file_name = file.get_file();
  2028. for (int i = 0; i < fs->files.size(); i++) {
  2029. if (file.filenocasecmp_to(fs->files[i]->file) < 0) {
  2030. break;
  2031. }
  2032. idx++;
  2033. }
  2034. EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
  2035. fi->file = file_name;
  2036. fi->import_modified_time = 0;
  2037. fi->import_valid = (type == "TextFile" || type == "OtherFile") ? true : ResourceLoader::is_import_valid(file);
  2038. fi->import_md5 = "";
  2039. fi->import_dest_paths = Vector<String>();
  2040. if (idx == fs->files.size()) {
  2041. fs->files.push_back(fi);
  2042. } else {
  2043. fs->files.insert(idx, fi);
  2044. }
  2045. cpos = idx;
  2046. } else {
  2047. //the file exists and it was updated, and was not added in this step.
  2048. //this means we must force upon next restart to scan it again, to get proper type and dependencies
  2049. late_update_files.insert(file);
  2050. _save_late_updated_files(); //files need to be updated in the re-scan
  2051. }
  2052. EditorFileSystemDirectory::FileInfo *fi = fs->files[cpos];
  2053. const String old_script_class_icon_path = fi->class_info.icon_path;
  2054. const String old_class_name = fi->class_info.name;
  2055. fi->type = type;
  2056. fi->resource_script_class = script_class;
  2057. fi->uid = uid;
  2058. fi->class_info = _get_global_script_class(type, file);
  2059. fi->import_group_file = ResourceLoader::get_import_group_file(file);
  2060. fi->modified_time = FileAccess::get_modified_time(file);
  2061. fi->deps = _get_dependencies(file);
  2062. fi->import_valid = (type == "TextFile" || type == "OtherFile") ? true : ResourceLoader::is_import_valid(file);
  2063. if (uid != ResourceUID::INVALID_ID) {
  2064. if (ResourceUID::get_singleton()->has_id(uid)) {
  2065. ResourceUID::get_singleton()->set_id(uid, file);
  2066. } else {
  2067. ResourceUID::get_singleton()->add_id(uid, file);
  2068. }
  2069. ResourceUID::get_singleton()->update_cache();
  2070. } else {
  2071. if (ResourceLoader::should_create_uid_file(file)) {
  2072. Ref<FileAccess> f = FileAccess::open(file + ".uid", FileAccess::WRITE);
  2073. if (f.is_valid()) {
  2074. const ResourceUID::ID id = ResourceUID::get_singleton()->create_id_for_path(file);
  2075. ResourceUID::get_singleton()->add_id(id, file);
  2076. f->store_line(ResourceUID::get_singleton()->id_to_text(id));
  2077. fi->uid = id;
  2078. }
  2079. }
  2080. }
  2081. // Update preview
  2082. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  2083. if (ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
  2084. _queue_update_script_class(file, ScriptClassInfoUpdate::from_file_info(fi));
  2085. }
  2086. if (fi->type == SNAME("PackedScene")) {
  2087. _queue_update_scene_groups(file);
  2088. }
  2089. if (ClassDB::is_parent_class(fi->type, SNAME("Resource"))) {
  2090. files_to_update_icon_path.push_back(fi);
  2091. } else if (old_script_class_icon_path != fi->class_info.icon_path) {
  2092. update_files_icon_cache = true;
  2093. }
  2094. // Restore another script as the global class name if multiple scripts had the same old class name.
  2095. if (!old_class_name.is_empty() && fi->class_info.name != old_class_name && ClassDB::is_parent_class(type, SNAME("Script"))) {
  2096. EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
  2097. String old_file = _get_file_by_class_name(filesystem, old_class_name, old_fi);
  2098. if (!old_file.is_empty() && old_fi) {
  2099. _queue_update_script_class(old_file, ScriptClassInfoUpdate::from_file_info(old_fi));
  2100. }
  2101. }
  2102. updated = true;
  2103. }
  2104. }
  2105. if (updated) {
  2106. if (update_files_icon_cache) {
  2107. _update_files_icon_path();
  2108. } else {
  2109. for (EditorFileSystemDirectory::FileInfo *fi : files_to_update_icon_path) {
  2110. _update_file_icon_path(fi);
  2111. }
  2112. }
  2113. if (!is_scanning()) {
  2114. _process_update_pending();
  2115. }
  2116. if (!filesystem_changed_queued) {
  2117. filesystem_changed_queued = true;
  2118. callable_mp(this, &EditorFileSystem::_notify_filesystem_changed).call_deferred();
  2119. }
  2120. }
  2121. }
  2122. void EditorFileSystem::_notify_filesystem_changed() {
  2123. emit_signal("filesystem_changed");
  2124. filesystem_changed_queued = false;
  2125. }
  2126. HashSet<String> EditorFileSystem::get_valid_extensions() const {
  2127. return valid_extensions;
  2128. }
  2129. void EditorFileSystem::_register_global_class_script(const String &p_search_path, const String &p_target_path, const ScriptClassInfoUpdate &p_script_update) {
  2130. ScriptServer::remove_global_class_by_path(p_search_path); // First remove, just in case it changed
  2131. if (p_script_update.name.is_empty()) {
  2132. return;
  2133. }
  2134. String lang;
  2135. for (int j = 0; j < ScriptServer::get_language_count(); j++) {
  2136. if (ScriptServer::get_language(j)->handles_global_class_type(p_script_update.type)) {
  2137. lang = ScriptServer::get_language(j)->get_name();
  2138. break;
  2139. }
  2140. }
  2141. if (lang.is_empty()) {
  2142. return; // No lang found that can handle this global class
  2143. }
  2144. ScriptServer::add_global_class(p_script_update.name, p_script_update.extends, lang, p_target_path, p_script_update.is_abstract, p_script_update.is_tool);
  2145. EditorNode::get_editor_data().script_class_set_icon_path(p_script_update.name, p_script_update.icon_path);
  2146. EditorNode::get_editor_data().script_class_set_name(p_target_path, p_script_update.name);
  2147. }
  2148. void EditorFileSystem::register_global_class_script(const String &p_search_path, const String &p_target_path) {
  2149. int index_file;
  2150. EditorFileSystemDirectory *efsd = find_file(p_search_path, &index_file);
  2151. if (efsd) {
  2152. const EditorFileSystemDirectory::FileInfo *fi = efsd->files[index_file];
  2153. EditorFileSystem::get_singleton()->_register_global_class_script(p_search_path, p_target_path, ScriptClassInfoUpdate::from_file_info(fi));
  2154. } else {
  2155. ScriptServer::remove_global_class_by_path(p_search_path);
  2156. }
  2157. }
  2158. Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) {
  2159. String importer_name;
  2160. HashMap<String, HashMap<StringName, Variant>> source_file_options;
  2161. HashMap<String, ResourceUID::ID> uids;
  2162. HashMap<String, String> base_paths;
  2163. for (int i = 0; i < p_files.size(); i++) {
  2164. Ref<ConfigFile> config;
  2165. config.instantiate();
  2166. Error err = config->load(p_files[i] + ".import");
  2167. ERR_CONTINUE(err != OK);
  2168. ERR_CONTINUE(!config->has_section_key("remap", "importer"));
  2169. String file_importer_name = config->get_value("remap", "importer");
  2170. ERR_CONTINUE(file_importer_name.is_empty());
  2171. if (!importer_name.is_empty() && importer_name != file_importer_name) {
  2172. EditorNode::get_singleton()->show_warning(vformat(TTR("There are multiple importers for different types pointing to file %s, import aborted"), p_group_file));
  2173. ERR_FAIL_V(ERR_FILE_CORRUPT);
  2174. }
  2175. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  2176. if (config->has_section_key("remap", "uid")) {
  2177. String uidt = config->get_value("remap", "uid");
  2178. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  2179. }
  2180. uids[p_files[i]] = uid;
  2181. source_file_options[p_files[i]] = HashMap<StringName, Variant>();
  2182. importer_name = file_importer_name;
  2183. if (importer_name == "keep" || importer_name == "skip") {
  2184. continue; //do nothing
  2185. }
  2186. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2187. ERR_FAIL_COND_V(importer.is_null(), ERR_FILE_CORRUPT);
  2188. List<ResourceImporter::ImportOption> options;
  2189. importer->get_import_options(p_files[i], &options);
  2190. //set default values
  2191. for (const ResourceImporter::ImportOption &E : options) {
  2192. source_file_options[p_files[i]][E.option.name] = E.default_value;
  2193. }
  2194. if (config->has_section("params")) {
  2195. Vector<String> sk = config->get_section_keys("params");
  2196. for (const String &param : sk) {
  2197. Variant value = config->get_value("params", param);
  2198. //override with whatever is in file
  2199. source_file_options[p_files[i]][param] = value;
  2200. }
  2201. }
  2202. base_paths[p_files[i]] = ResourceFormatImporter::get_singleton()->get_import_base_path(p_files[i]);
  2203. }
  2204. if (importer_name == "keep" || importer_name == "skip") {
  2205. return OK; // (do nothing)
  2206. }
  2207. ERR_FAIL_COND_V(importer_name.is_empty(), ERR_UNCONFIGURED);
  2208. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2209. Error err = importer->import_group_file(p_group_file, source_file_options, base_paths);
  2210. //all went well, overwrite config files with proper remaps and md5s
  2211. for (const KeyValue<String, HashMap<StringName, Variant>> &E : source_file_options) {
  2212. const String &file = E.key;
  2213. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(file);
  2214. Vector<String> dest_paths;
  2215. ResourceUID::ID uid = uids[file];
  2216. {
  2217. Ref<FileAccess> f = FileAccess::open(file + ".import", FileAccess::WRITE);
  2218. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'.");
  2219. //write manually, as order matters ([remap] has to go first for performance).
  2220. f->store_line("[remap]");
  2221. f->store_line("");
  2222. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  2223. int version = importer->get_format_version();
  2224. if (version > 0) {
  2225. f->store_line("importer_version=" + itos(version));
  2226. }
  2227. if (!importer->get_resource_type().is_empty()) {
  2228. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  2229. }
  2230. if (uid == ResourceUID::INVALID_ID) {
  2231. uid = ResourceUID::get_singleton()->create_id_for_path(file);
  2232. }
  2233. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
  2234. if (err == OK) {
  2235. String path = base_path + "." + importer->get_save_extension();
  2236. f->store_line("path=\"" + path + "\"");
  2237. dest_paths.push_back(path);
  2238. }
  2239. f->store_line("group_file=" + Variant(p_group_file).get_construct_string());
  2240. if (err == OK) {
  2241. f->store_line("valid=true");
  2242. } else {
  2243. f->store_line("valid=false");
  2244. }
  2245. f->store_line("[deps]\n");
  2246. f->store_line("");
  2247. f->store_line("source_file=" + Variant(file).get_construct_string());
  2248. if (dest_paths.size()) {
  2249. Array dp;
  2250. for (int i = 0; i < dest_paths.size(); i++) {
  2251. dp.push_back(dest_paths[i]);
  2252. }
  2253. f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
  2254. }
  2255. f->store_line("[params]");
  2256. f->store_line("");
  2257. //store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  2258. List<ResourceImporter::ImportOption> options;
  2259. importer->get_import_options(file, &options);
  2260. //set default values
  2261. for (const ResourceImporter::ImportOption &F : options) {
  2262. String base = F.option.name;
  2263. Variant v = F.default_value;
  2264. if (source_file_options[file].has(base)) {
  2265. v = source_file_options[file][base];
  2266. }
  2267. String value;
  2268. VariantWriter::write_to_string(v, value);
  2269. f->store_line(base + "=" + value);
  2270. }
  2271. }
  2272. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  2273. {
  2274. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  2275. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  2276. md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\"");
  2277. if (dest_paths.size()) {
  2278. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  2279. }
  2280. }
  2281. EditorFileSystemDirectory *fs = nullptr;
  2282. int cpos = -1;
  2283. bool found = _find_file(file, &fs, cpos);
  2284. ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, vformat("Can't find file '%s' during group reimport.", file));
  2285. //update modified times, to avoid reimport
  2286. fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
  2287. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(file + ".import");
  2288. fs->files[cpos]->import_md5 = FileAccess::get_md5(file + ".import");
  2289. fs->files[cpos]->import_dest_paths = dest_paths;
  2290. fs->files[cpos]->deps = _get_dependencies(file);
  2291. fs->files[cpos]->uid = uid;
  2292. fs->files[cpos]->type = importer->get_resource_type();
  2293. if (fs->files[cpos]->type == "" && textfile_extensions.has(file.get_extension())) {
  2294. fs->files[cpos]->type = "TextFile";
  2295. }
  2296. if (fs->files[cpos]->type == "" && other_file_extensions.has(file.get_extension())) {
  2297. fs->files[cpos]->type = "OtherFile";
  2298. }
  2299. fs->files[cpos]->import_valid = err == OK;
  2300. if (ResourceUID::get_singleton()->has_id(uid)) {
  2301. ResourceUID::get_singleton()->set_id(uid, file);
  2302. } else {
  2303. ResourceUID::get_singleton()->add_id(uid, file);
  2304. }
  2305. //if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  2306. //to reload properly
  2307. Ref<Resource> r = ResourceCache::get_ref(file);
  2308. if (r.is_valid()) {
  2309. if (!r->get_import_path().is_empty()) {
  2310. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(file);
  2311. r->set_import_path(dst_path);
  2312. r->set_import_last_modified_time(0);
  2313. }
  2314. }
  2315. EditorResourcePreview::get_singleton()->check_for_invalidation(file);
  2316. }
  2317. return err;
  2318. }
  2319. Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant *p_generator_parameters, bool p_update_file_system) {
  2320. print_verbose(vformat("EditorFileSystem: Importing file: %s", p_file));
  2321. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  2322. EditorFileSystemDirectory *fs = nullptr;
  2323. int cpos = -1;
  2324. if (p_update_file_system) {
  2325. bool found = _find_file(p_file, &fs, cpos);
  2326. ERR_FAIL_COND_V_MSG(!found, ERR_FILE_NOT_FOUND, vformat("Can't find file '%s' during file reimport.", p_file));
  2327. }
  2328. //try to obtain existing params
  2329. HashMap<StringName, Variant> params = p_custom_options;
  2330. String importer_name; //empty by default though
  2331. if (!p_custom_importer.is_empty()) {
  2332. importer_name = p_custom_importer;
  2333. }
  2334. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  2335. Variant generator_parameters;
  2336. if (p_generator_parameters) {
  2337. generator_parameters = *p_generator_parameters;
  2338. }
  2339. if (FileAccess::exists(p_file + ".import")) {
  2340. //use existing
  2341. Ref<ConfigFile> cf;
  2342. cf.instantiate();
  2343. Error err = cf->load(p_file + ".import");
  2344. if (err == OK) {
  2345. if (cf->has_section("params")) {
  2346. Vector<String> sk = cf->get_section_keys("params");
  2347. for (const String &E : sk) {
  2348. if (!params.has(E)) {
  2349. params[E] = cf->get_value("params", E);
  2350. }
  2351. }
  2352. }
  2353. if (cf->has_section("remap")) {
  2354. if (p_custom_importer.is_empty()) {
  2355. importer_name = cf->get_value("remap", "importer");
  2356. }
  2357. if (cf->has_section_key("remap", "uid")) {
  2358. String uidt = cf->get_value("remap", "uid");
  2359. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  2360. }
  2361. if (!p_generator_parameters) {
  2362. if (cf->has_section_key("remap", "generator_parameters")) {
  2363. generator_parameters = cf->get_value("remap", "generator_parameters");
  2364. }
  2365. }
  2366. }
  2367. }
  2368. }
  2369. if (importer_name == "keep" || importer_name == "skip") {
  2370. //keep files, do nothing.
  2371. if (p_update_file_system) {
  2372. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  2373. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  2374. fs->files[cpos]->import_md5 = FileAccess::get_md5(p_file + ".import");
  2375. fs->files[cpos]->import_dest_paths = Vector<String>();
  2376. fs->files[cpos]->deps.clear();
  2377. fs->files[cpos]->type = "";
  2378. fs->files[cpos]->import_valid = false;
  2379. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  2380. }
  2381. return OK;
  2382. }
  2383. Ref<ResourceImporter> importer;
  2384. bool load_default = false;
  2385. //find the importer
  2386. if (!importer_name.is_empty()) {
  2387. importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  2388. }
  2389. if (importer.is_null()) {
  2390. //not found by name, find by extension
  2391. importer = ResourceFormatImporter::get_singleton()->get_importer_by_file(p_file);
  2392. load_default = true;
  2393. if (importer.is_null()) {
  2394. 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.");
  2395. }
  2396. }
  2397. if (FileAccess::exists(p_file + ".import")) {
  2398. // We only want to handle compat for existing files, not new ones.
  2399. importer->handle_compatibility_options(params);
  2400. }
  2401. //mix with default params, in case a parameter is missing
  2402. List<ResourceImporter::ImportOption> opts;
  2403. importer->get_import_options(p_file, &opts);
  2404. for (const ResourceImporter::ImportOption &E : opts) {
  2405. if (!params.has(E.option.name)) { //this one is not present
  2406. params[E.option.name] = E.default_value;
  2407. }
  2408. }
  2409. if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  2410. //use defaults if exist
  2411. Dictionary d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
  2412. for (const KeyValue<Variant, Variant> &kv : d) {
  2413. params[kv.key] = kv.value;
  2414. }
  2415. }
  2416. if (uid == ResourceUID::INVALID_ID) {
  2417. uid = ResourceUID::get_singleton()->create_id_for_path(p_file);
  2418. }
  2419. //finally, perform import!!
  2420. String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
  2421. List<String> import_variants;
  2422. List<String> gen_files;
  2423. Variant meta;
  2424. Error err = importer->import(uid, p_file, base_path, params, &import_variants, &gen_files, &meta);
  2425. // As import is complete, save the .import file.
  2426. Vector<String> dest_paths;
  2427. {
  2428. Ref<FileAccess> f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
  2429. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open file from path '" + p_file + ".import'.");
  2430. // Write manually, as order matters ([remap] has to go first for performance).
  2431. f->store_line("[remap]");
  2432. f->store_line("");
  2433. f->store_line("importer=\"" + importer->get_importer_name() + "\"");
  2434. int version = importer->get_format_version();
  2435. if (version > 0) {
  2436. f->store_line("importer_version=" + itos(version));
  2437. }
  2438. if (!importer->get_resource_type().is_empty()) {
  2439. f->store_line("type=\"" + importer->get_resource_type() + "\"");
  2440. }
  2441. f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
  2442. if (err == OK) {
  2443. if (importer->get_save_extension().is_empty()) {
  2444. //no path
  2445. } else if (import_variants.size()) {
  2446. //import with variants
  2447. for (const String &E : import_variants) {
  2448. String path = base_path.c_escape() + "." + E + "." + importer->get_save_extension();
  2449. f->store_line("path." + E + "=\"" + path + "\"");
  2450. dest_paths.push_back(path);
  2451. }
  2452. } else {
  2453. String path = base_path + "." + importer->get_save_extension();
  2454. f->store_line("path=\"" + path + "\"");
  2455. dest_paths.push_back(path);
  2456. }
  2457. } else {
  2458. f->store_line("valid=false");
  2459. }
  2460. if (meta != Variant()) {
  2461. f->store_line("metadata=" + meta.get_construct_string());
  2462. }
  2463. if (generator_parameters != Variant()) {
  2464. f->store_line("generator_parameters=" + generator_parameters.get_construct_string());
  2465. }
  2466. f->store_line("");
  2467. f->store_line("[deps]\n");
  2468. if (gen_files.size()) {
  2469. Array genf;
  2470. for (const String &E : gen_files) {
  2471. genf.push_back(E);
  2472. dest_paths.push_back(E);
  2473. }
  2474. String value;
  2475. VariantWriter::write_to_string(genf, value);
  2476. f->store_line("files=" + value);
  2477. f->store_line("");
  2478. }
  2479. f->store_line("source_file=" + Variant(p_file).get_construct_string());
  2480. if (dest_paths.size()) {
  2481. Array dp;
  2482. for (int i = 0; i < dest_paths.size(); i++) {
  2483. dp.push_back(dest_paths[i]);
  2484. }
  2485. f->store_line("dest_files=" + Variant(dp).get_construct_string());
  2486. }
  2487. f->store_line("");
  2488. f->store_line("[params]");
  2489. f->store_line("");
  2490. // Store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
  2491. for (const ResourceImporter::ImportOption &E : opts) {
  2492. String base = E.option.name;
  2493. String value;
  2494. VariantWriter::write_to_string(params[base], value);
  2495. f->store_line(base + "=" + value);
  2496. }
  2497. }
  2498. // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
  2499. {
  2500. Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
  2501. ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
  2502. md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"");
  2503. if (dest_paths.size()) {
  2504. md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
  2505. }
  2506. }
  2507. if (p_update_file_system) {
  2508. // Update cpos, newly created files could've changed the index of the reimported p_file.
  2509. _find_file(p_file, &fs, cpos);
  2510. // Update modified times, to avoid reimport.
  2511. fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
  2512. fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
  2513. fs->files[cpos]->import_md5 = FileAccess::get_md5(p_file + ".import");
  2514. fs->files[cpos]->import_dest_paths = dest_paths;
  2515. fs->files[cpos]->deps = _get_dependencies(p_file);
  2516. fs->files[cpos]->type = importer->get_resource_type();
  2517. fs->files[cpos]->uid = uid;
  2518. fs->files[cpos]->import_valid = fs->files[cpos]->type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
  2519. }
  2520. if (ResourceUID::get_singleton()->has_id(uid)) {
  2521. ResourceUID::get_singleton()->set_id(uid, p_file);
  2522. } else {
  2523. ResourceUID::get_singleton()->add_id(uid, p_file);
  2524. }
  2525. // If file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
  2526. // to reload properly.
  2527. Ref<Resource> r = ResourceCache::get_ref(p_file);
  2528. if (r.is_valid()) {
  2529. if (!r->get_import_path().is_empty()) {
  2530. String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
  2531. r->set_import_path(dst_path);
  2532. r->set_import_last_modified_time(0);
  2533. }
  2534. }
  2535. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  2536. print_verbose(vformat("EditorFileSystem: \"%s\" import took %d ms.", p_file, OS::get_singleton()->get_ticks_msec() - start_time));
  2537. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_UNRECOGNIZED, "Error importing '" + p_file + "'.");
  2538. return OK;
  2539. }
  2540. void EditorFileSystem::_find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport) {
  2541. int fc = efd->files.size();
  2542. const EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptr();
  2543. for (int i = 0; i < fc; i++) {
  2544. if (groups_to_reimport.has(files[i]->import_group_file)) {
  2545. if (!group_files.has(files[i]->import_group_file)) {
  2546. group_files[files[i]->import_group_file] = Vector<String>();
  2547. }
  2548. group_files[files[i]->import_group_file].push_back(efd->get_file_path(i));
  2549. }
  2550. }
  2551. for (int i = 0; i < efd->get_subdir_count(); i++) {
  2552. _find_group_files(efd->get_subdir(i), group_files, groups_to_reimport);
  2553. }
  2554. }
  2555. void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params) {
  2556. Vector<String> reloads;
  2557. reloads.append(p_file);
  2558. // Emit the resource_reimporting signal for the single file before the actual importation.
  2559. emit_signal(SNAME("resources_reimporting"), reloads);
  2560. _reimport_file(p_file, p_custom_params, p_importer);
  2561. // Emit the resource_reimported signal for the single file we just reimported.
  2562. emit_signal(SNAME("resources_reimported"), reloads);
  2563. }
  2564. Error EditorFileSystem::_copy_file(const String &p_from, const String &p_to) {
  2565. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2566. if (FileAccess::exists(p_from + ".import")) {
  2567. Error err = da->copy(p_from, p_to);
  2568. if (err != OK) {
  2569. return err;
  2570. }
  2571. // Remove uid from .import file to avoid conflict.
  2572. Ref<ConfigFile> cfg;
  2573. cfg.instantiate();
  2574. cfg->load(p_from + ".import");
  2575. cfg->erase_section_key("remap", "uid");
  2576. err = cfg->save(p_to + ".import");
  2577. if (err != OK) {
  2578. return err;
  2579. }
  2580. } else if (ResourceLoader::get_resource_uid(p_from) == ResourceUID::INVALID_ID) {
  2581. // Files which do not use an uid can just be copied.
  2582. Error err = da->copy(p_from, p_to);
  2583. if (err != OK) {
  2584. return err;
  2585. }
  2586. } else {
  2587. // Load the resource and save it again in the new location (this generates a new UID).
  2588. Error err;
  2589. Ref<Resource> res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
  2590. if (err == OK && res.is_valid()) {
  2591. err = ResourceSaver::save(res, p_to, ResourceSaver::FLAG_COMPRESS);
  2592. if (err != OK) {
  2593. return err;
  2594. }
  2595. } else if (err != OK) {
  2596. // When loading files like text files the error is OK but the resource is still null.
  2597. // We can ignore such files.
  2598. return err;
  2599. }
  2600. }
  2601. return OK;
  2602. }
  2603. bool EditorFileSystem::_copy_directory(const String &p_from, const String &p_to, List<CopiedFile> *p_files) {
  2604. Ref<DirAccess> old_dir = DirAccess::open(p_from);
  2605. ERR_FAIL_COND_V(old_dir.is_null(), false);
  2606. Error err = make_dir_recursive(p_to);
  2607. if (err != OK && err != ERR_ALREADY_EXISTS) {
  2608. return false;
  2609. }
  2610. bool success = true;
  2611. old_dir->set_include_navigational(false);
  2612. old_dir->list_dir_begin();
  2613. for (String F = old_dir->_get_next(); !F.is_empty(); F = old_dir->_get_next()) {
  2614. if (old_dir->current_is_dir()) {
  2615. success = _copy_directory(p_from.path_join(F), p_to.path_join(F), p_files) && success;
  2616. } else if (F.get_extension() != "import" && F.get_extension() != "uid") {
  2617. CopiedFile copy;
  2618. copy.from = p_from.path_join(F);
  2619. copy.to = p_to.path_join(F);
  2620. p_files->push_back(copy);
  2621. }
  2622. }
  2623. return success;
  2624. }
  2625. void EditorFileSystem::_queue_refresh_filesystem() {
  2626. if (refresh_queued) {
  2627. return;
  2628. }
  2629. refresh_queued = true;
  2630. get_tree()->connect(SNAME("process_frame"), callable_mp(this, &EditorFileSystem::_refresh_filesystem), CONNECT_ONE_SHOT);
  2631. }
  2632. void EditorFileSystem::_refresh_filesystem() {
  2633. for (const ObjectID &id : folders_to_sort) {
  2634. EditorFileSystemDirectory *dir = ObjectDB::get_instance<EditorFileSystemDirectory>(id);
  2635. if (dir) {
  2636. dir->subdirs.sort_custom<DirectoryComparator>();
  2637. }
  2638. }
  2639. folders_to_sort.clear();
  2640. _update_scan_actions();
  2641. emit_signal(SNAME("filesystem_changed"));
  2642. refresh_queued = false;
  2643. }
  2644. void EditorFileSystem::_reimport_thread(uint32_t p_index, ImportThreadData *p_import_data) {
  2645. ResourceLoader::set_is_import_thread(true);
  2646. int file_idx = p_import_data->reimport_from + int(p_index);
  2647. _reimport_file(p_import_data->reimport_files[file_idx].path);
  2648. ResourceLoader::set_is_import_thread(false);
  2649. p_import_data->imported_sem->post();
  2650. }
  2651. void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
  2652. ERR_FAIL_COND_MSG(importing, "Attempted to call reimport_files() recursively, this is not allowed.");
  2653. importing = true;
  2654. Vector<String> reloads;
  2655. EditorProgress *ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
  2656. // The method reimport_files runs on the main thread, and if VSync is enabled
  2657. // or Update Continuously is disabled, Main::Iteration takes longer each frame.
  2658. // Each EditorProgress::step can trigger a redraw, and when there are many files to import,
  2659. // this could lead to a slow import process, especially when the editor is unfocused.
  2660. // Temporarily disabling VSync and low_processor_usage_mode while reimporting fixes this.
  2661. const bool old_low_processor_usage_mode = OS::get_singleton()->is_in_low_processor_usage_mode();
  2662. const DisplayServer::VSyncMode old_vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
  2663. OS::get_singleton()->set_low_processor_usage_mode(false);
  2664. DisplayServer::get_singleton()->window_set_vsync_mode(DisplayServer::VSyncMode::VSYNC_DISABLED);
  2665. Vector<ImportFile> reimport_files;
  2666. HashSet<String> groups_to_reimport;
  2667. for (int i = 0; i < p_files.size(); i++) {
  2668. ep->step(TTR("Preparing files to reimport..."), i, false);
  2669. String file = p_files[i];
  2670. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(file);
  2671. if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
  2672. file = ResourceUID::get_singleton()->get_id_path(uid);
  2673. }
  2674. String group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(file);
  2675. if (group_file_cache.has(file)) {
  2676. // Maybe the file itself is a group!
  2677. groups_to_reimport.insert(file);
  2678. // Groups do not belong to groups.
  2679. group_file = String();
  2680. } else if (groups_to_reimport.has(file)) {
  2681. // Groups do not belong to groups.
  2682. group_file = String();
  2683. } else if (!group_file.is_empty()) {
  2684. // It's a group file, add group to import and skip this file.
  2685. groups_to_reimport.insert(group_file);
  2686. } else {
  2687. // It's a regular file.
  2688. ImportFile ifile;
  2689. ifile.path = file;
  2690. ResourceFormatImporter::get_singleton()->get_import_order_threads_and_importer(file, ifile.order, ifile.threaded, ifile.importer);
  2691. reloads.push_back(file);
  2692. reimport_files.push_back(ifile);
  2693. }
  2694. // Group may have changed, so also update group reference.
  2695. EditorFileSystemDirectory *fs = nullptr;
  2696. int cpos = -1;
  2697. if (_find_file(file, &fs, cpos)) {
  2698. fs->files.write[cpos]->import_group_file = group_file;
  2699. }
  2700. }
  2701. reimport_files.sort();
  2702. ep->step(TTR("Executing pre-reimport operations..."), 0, true);
  2703. // Emit the resource_reimporting signal for the single file before the actual importation.
  2704. emit_signal(SNAME("resources_reimporting"), reloads);
  2705. #ifdef THREADS_ENABLED
  2706. bool use_multiple_threads = GLOBAL_GET("editor/import/use_multiple_threads");
  2707. #else
  2708. bool use_multiple_threads = false;
  2709. #endif
  2710. int from = 0;
  2711. Semaphore imported_sem;
  2712. for (int i = 0; i < reimport_files.size(); i++) {
  2713. if (groups_to_reimport.has(reimport_files[i].path)) {
  2714. from = i + 1;
  2715. continue;
  2716. }
  2717. if (use_multiple_threads && reimport_files[i].threaded) {
  2718. if (i + 1 == reimport_files.size() || reimport_files[i + 1].importer != reimport_files[from].importer || groups_to_reimport.has(reimport_files[i + 1].path)) {
  2719. if (from - i == 0) {
  2720. // Single file, do not use threads.
  2721. ep->step(reimport_files[i].path.get_file(), i, false);
  2722. _reimport_file(reimport_files[i].path);
  2723. } else {
  2724. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(reimport_files[from].importer);
  2725. if (importer.is_null()) {
  2726. ERR_PRINT(vformat("Invalid importer for \"%s\".", reimport_files[from].importer));
  2727. from = i + 1;
  2728. continue;
  2729. }
  2730. importer->import_threaded_begin();
  2731. ImportThreadData tdata;
  2732. tdata.reimport_from = from;
  2733. tdata.reimport_files = reimport_files.ptr();
  2734. tdata.imported_sem = &imported_sem;
  2735. int item_count = i - from + 1;
  2736. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &EditorFileSystem::_reimport_thread, &tdata, item_count, -1, false, vformat(TTR("Import resources of type: %s"), reimport_files[from].importer));
  2737. int imported_count = 0;
  2738. while (true) {
  2739. ep->step(reimport_files[imported_count].path.get_file(), from + imported_count, false);
  2740. imported_sem.wait();
  2741. do {
  2742. imported_count++;
  2743. } while (imported_sem.try_wait());
  2744. if (imported_count == item_count) {
  2745. break;
  2746. }
  2747. }
  2748. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  2749. DEV_ASSERT(!imported_sem.try_wait());
  2750. importer->import_threaded_end();
  2751. }
  2752. from = i + 1;
  2753. }
  2754. } else {
  2755. ep->step(reimport_files[i].path.get_file(), i, false);
  2756. _reimport_file(reimport_files[i].path);
  2757. // We need to increment the counter, maybe the next file is multithreaded
  2758. // and doesn't have the same importer.
  2759. from = i + 1;
  2760. }
  2761. }
  2762. // Reimport groups.
  2763. from = reimport_files.size();
  2764. if (groups_to_reimport.size()) {
  2765. HashMap<String, Vector<String>> group_files;
  2766. _find_group_files(filesystem, group_files, groups_to_reimport);
  2767. for (const KeyValue<String, Vector<String>> &E : group_files) {
  2768. ep->step(E.key.get_file(), from++, false);
  2769. Error err = _reimport_group(E.key, E.value);
  2770. reloads.push_back(E.key);
  2771. reloads.append_array(E.value);
  2772. if (err == OK) {
  2773. _reimport_file(E.key);
  2774. }
  2775. }
  2776. }
  2777. ep->step(TTR("Finalizing Asset Import..."), p_files.size());
  2778. ResourceUID::get_singleton()->update_cache(); // After reimporting, update the cache.
  2779. _save_filesystem_cache();
  2780. memdelete_notnull(ep);
  2781. _process_update_pending();
  2782. // Revert to previous values to restore editor settings for VSync and Update Continuously.
  2783. OS::get_singleton()->set_low_processor_usage_mode(old_low_processor_usage_mode);
  2784. DisplayServer::get_singleton()->window_set_vsync_mode(old_vsync_mode);
  2785. importing = false;
  2786. ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
  2787. ep->step(TTR("Executing post-reimport operations..."), 0, true);
  2788. if (!is_scanning()) {
  2789. emit_signal(SNAME("filesystem_changed"));
  2790. }
  2791. emit_signal(SNAME("resources_reimported"), reloads);
  2792. memdelete_notnull(ep);
  2793. }
  2794. Error EditorFileSystem::reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
  2795. Vector<String> reloads;
  2796. reloads.append(p_file);
  2797. // Emit the resource_reimporting signal for the single file before the actual importation.
  2798. emit_signal(SNAME("resources_reimporting"), reloads);
  2799. Error ret = _reimport_file(p_file, p_custom_options, p_custom_importer, &p_generator_parameters);
  2800. // Emit the resource_reimported signal for the single file we just reimported.
  2801. emit_signal(SNAME("resources_reimported"), reloads);
  2802. return ret;
  2803. }
  2804. Error EditorFileSystem::_resource_import(const String &p_path) {
  2805. Vector<String> files;
  2806. files.push_back(p_path);
  2807. singleton->update_file(p_path);
  2808. singleton->reimport_files(files);
  2809. return OK;
  2810. }
  2811. Ref<Resource> EditorFileSystem::_load_resource_on_startup(ResourceFormatImporter *p_importer, const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, ResourceFormatLoader::CacheMode p_cache_mode) {
  2812. ERR_FAIL_NULL_V(p_importer, Ref<Resource>());
  2813. if (!FileAccess::exists(p_path)) {
  2814. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Failed loading resource: %s. The file doesn't seem to exist.", p_path));
  2815. }
  2816. Ref<Resource> res;
  2817. bool can_retry = true;
  2818. bool retry = true;
  2819. while (retry) {
  2820. retry = false;
  2821. res = p_importer->load_internal(p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode, can_retry);
  2822. if (res.is_null() && can_retry) {
  2823. can_retry = false;
  2824. Error err = singleton->_reimport_file(p_path, HashMap<StringName, Variant>(), "", nullptr, false);
  2825. if (err == OK) {
  2826. retry = true;
  2827. }
  2828. }
  2829. }
  2830. return res;
  2831. }
  2832. bool EditorFileSystem::_should_skip_directory(const String &p_path) {
  2833. String project_data_path = ProjectSettings::get_singleton()->get_project_data_path();
  2834. if (p_path == project_data_path || p_path.begins_with(project_data_path + "/")) {
  2835. return true;
  2836. }
  2837. if (FileAccess::exists(p_path.path_join("project.godot"))) {
  2838. // Skip if another project inside this.
  2839. if (EditorFileSystem::get_singleton()->first_scan) {
  2840. WARN_PRINT_ONCE(vformat("Detected another project.godot at %s. The folder will be ignored.", p_path));
  2841. }
  2842. return true;
  2843. }
  2844. if (FileAccess::exists(p_path.path_join(".gdignore"))) {
  2845. // Skip if a `.gdignore` file is inside this.
  2846. return true;
  2847. }
  2848. return false;
  2849. }
  2850. bool EditorFileSystem::is_group_file(const String &p_path) const {
  2851. return group_file_cache.has(p_path);
  2852. }
  2853. void EditorFileSystem::_move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location) {
  2854. int fc = efd->files.size();
  2855. EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptrw();
  2856. for (int i = 0; i < fc; i++) {
  2857. if (files[i]->import_group_file == p_group_file) {
  2858. files[i]->import_group_file = p_new_location;
  2859. Ref<ConfigFile> config;
  2860. config.instantiate();
  2861. String path = efd->get_file_path(i) + ".import";
  2862. Error err = config->load(path);
  2863. if (err != OK) {
  2864. continue;
  2865. }
  2866. if (config->has_section_key("remap", "group_file")) {
  2867. config->set_value("remap", "group_file", p_new_location);
  2868. }
  2869. Vector<String> sk = config->get_section_keys("params");
  2870. for (const String &param : sk) {
  2871. //not very clean, but should work
  2872. String value = config->get_value("params", param);
  2873. if (value == p_group_file) {
  2874. config->set_value("params", param, p_new_location);
  2875. }
  2876. }
  2877. config->save(path);
  2878. }
  2879. }
  2880. for (int i = 0; i < efd->get_subdir_count(); i++) {
  2881. _move_group_files(efd->get_subdir(i), p_group_file, p_new_location);
  2882. }
  2883. }
  2884. void EditorFileSystem::move_group_file(const String &p_path, const String &p_new_path) {
  2885. if (get_filesystem()) {
  2886. _move_group_files(get_filesystem(), p_path, p_new_path);
  2887. if (group_file_cache.has(p_path)) {
  2888. group_file_cache.erase(p_path);
  2889. group_file_cache.insert(p_new_path);
  2890. }
  2891. }
  2892. }
  2893. Error EditorFileSystem::make_dir_recursive(const String &p_path, const String &p_base_path) {
  2894. Error err;
  2895. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2896. if (!p_base_path.is_empty()) {
  2897. err = da->change_dir(p_base_path);
  2898. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open base directory '" + p_base_path + "'.");
  2899. }
  2900. if (da->dir_exists(p_path)) {
  2901. return ERR_ALREADY_EXISTS;
  2902. }
  2903. err = da->make_dir_recursive(p_path);
  2904. if (err != OK) {
  2905. return err;
  2906. }
  2907. const String path = da->get_current_dir();
  2908. EditorFileSystemDirectory *parent = get_filesystem_path(path);
  2909. ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
  2910. folders_to_sort.insert(parent->get_instance_id());
  2911. const PackedStringArray folders = p_path.trim_prefix(path).split("/", false);
  2912. for (const String &folder : folders) {
  2913. const int current = parent->find_dir_index(folder);
  2914. if (current > -1) {
  2915. parent = parent->get_subdir(current);
  2916. continue;
  2917. }
  2918. EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
  2919. efd->parent = parent;
  2920. efd->name = folder;
  2921. parent->subdirs.push_back(efd);
  2922. parent = efd;
  2923. }
  2924. _queue_refresh_filesystem();
  2925. return OK;
  2926. }
  2927. Error EditorFileSystem::copy_file(const String &p_from, const String &p_to) {
  2928. _copy_file(p_from, p_to);
  2929. EditorFileSystemDirectory *parent = get_filesystem_path(p_to.get_base_dir());
  2930. ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
  2931. ScanProgress sp;
  2932. _scan_fs_changes(parent, sp, false);
  2933. _queue_refresh_filesystem();
  2934. return OK;
  2935. }
  2936. Error EditorFileSystem::copy_directory(const String &p_from, const String &p_to) {
  2937. List<CopiedFile> files;
  2938. bool success = _copy_directory(p_from, p_to, &files);
  2939. EditorProgress *ep = nullptr;
  2940. if (files.size() > 10) {
  2941. ep = memnew(EditorProgress("_copy_files", TTR("Copying files..."), files.size()));
  2942. }
  2943. int i = 0;
  2944. for (const CopiedFile &F : files) {
  2945. if (_copy_file(F.from, F.to) != OK) {
  2946. success = false;
  2947. }
  2948. if (ep) {
  2949. ep->step(F.from.get_file(), i++, false);
  2950. }
  2951. }
  2952. memdelete_notnull(ep);
  2953. EditorFileSystemDirectory *efd = get_filesystem_path(p_to);
  2954. ERR_FAIL_NULL_V(efd, FAILED);
  2955. ERR_FAIL_NULL_V(efd->get_parent(), FAILED);
  2956. folders_to_sort.insert(efd->get_parent()->get_instance_id());
  2957. ScanProgress sp;
  2958. _scan_fs_changes(efd, sp);
  2959. _queue_refresh_filesystem();
  2960. return success ? OK : FAILED;
  2961. }
  2962. ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate) {
  2963. if (!p_path.is_resource_file() || p_path.begins_with(ProjectSettings::get_singleton()->get_project_data_path())) {
  2964. // Saved externally (configuration file) or internal file, do not assign an ID.
  2965. return ResourceUID::INVALID_ID;
  2966. }
  2967. EditorFileSystemDirectory *fs = nullptr;
  2968. int cpos = -1;
  2969. if (!singleton->_find_file(p_path, &fs, cpos)) {
  2970. // Fallback to ResourceLoader if filesystem cache fails (can happen during scanning etc.).
  2971. ResourceUID::ID fallback = ResourceLoader::get_resource_uid(p_path);
  2972. if (fallback != ResourceUID::INVALID_ID) {
  2973. return fallback;
  2974. }
  2975. if (p_generate) {
  2976. return ResourceUID::get_singleton()->create_id_for_path(p_path); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
  2977. } else {
  2978. return ResourceUID::INVALID_ID;
  2979. }
  2980. } else if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
  2981. return fs->files[cpos]->uid;
  2982. } else if (p_generate) {
  2983. return ResourceUID::get_singleton()->create_id_for_path(p_path); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
  2984. } else {
  2985. return ResourceUID::INVALID_ID;
  2986. }
  2987. }
  2988. static void _scan_extensions_dir(EditorFileSystemDirectory *d, HashSet<String> &extensions) {
  2989. int fc = d->get_file_count();
  2990. for (int i = 0; i < fc; i++) {
  2991. if (d->get_file_type(i) == SNAME("GDExtension")) {
  2992. extensions.insert(d->get_file_path(i));
  2993. }
  2994. }
  2995. int dc = d->get_subdir_count();
  2996. for (int i = 0; i < dc; i++) {
  2997. _scan_extensions_dir(d->get_subdir(i), extensions);
  2998. }
  2999. }
  3000. bool EditorFileSystem::_scan_extensions() {
  3001. EditorFileSystemDirectory *d = get_filesystem();
  3002. HashSet<String> extensions;
  3003. _scan_extensions_dir(d, extensions);
  3004. return GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions);
  3005. }
  3006. void EditorFileSystem::_bind_methods() {
  3007. ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
  3008. ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
  3009. ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
  3010. ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
  3011. ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
  3012. ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
  3013. ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
  3014. ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
  3015. ClassDB::bind_method(D_METHOD("reimport_files", "files"), &EditorFileSystem::reimport_files);
  3016. ADD_SIGNAL(MethodInfo("filesystem_changed"));
  3017. ADD_SIGNAL(MethodInfo("script_classes_updated"));
  3018. ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
  3019. ADD_SIGNAL(MethodInfo("resources_reimporting", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  3020. ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  3021. ADD_SIGNAL(MethodInfo("resources_reload", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
  3022. }
  3023. void EditorFileSystem::_update_extensions() {
  3024. valid_extensions.clear();
  3025. import_extensions.clear();
  3026. textfile_extensions.clear();
  3027. other_file_extensions.clear();
  3028. List<String> extensionsl;
  3029. ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
  3030. for (const String &E : extensionsl) {
  3031. valid_extensions.insert(E);
  3032. }
  3033. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  3034. for (const String &E : textfile_ext) {
  3035. if (valid_extensions.has(E)) {
  3036. continue;
  3037. }
  3038. valid_extensions.insert(E);
  3039. textfile_extensions.insert(E);
  3040. }
  3041. const Vector<String> other_file_ext = ((String)(EDITOR_GET("docks/filesystem/other_file_extensions"))).split(",", false);
  3042. for (const String &E : other_file_ext) {
  3043. if (valid_extensions.has(E)) {
  3044. continue;
  3045. }
  3046. valid_extensions.insert(E);
  3047. other_file_extensions.insert(E);
  3048. }
  3049. extensionsl.clear();
  3050. ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
  3051. for (const String &E : extensionsl) {
  3052. import_extensions.insert(!E.begins_with(".") ? "." + E : E);
  3053. }
  3054. }
  3055. bool EditorFileSystem::_can_import_file(const String &p_file) {
  3056. for (const String &F : import_extensions) {
  3057. if (p_file.right(F.length()).nocasecmp_to(F) == 0) {
  3058. return true;
  3059. }
  3060. }
  3061. return false;
  3062. }
  3063. void EditorFileSystem::add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  3064. ERR_FAIL_COND(import_support_queries.has(p_query));
  3065. import_support_queries.push_back(p_query);
  3066. }
  3067. void EditorFileSystem::remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
  3068. import_support_queries.erase(p_query);
  3069. }
  3070. EditorFileSystem::EditorFileSystem() {
  3071. #ifdef THREADS_ENABLED
  3072. use_threads = true;
  3073. #endif
  3074. ResourceLoader::import = _resource_import;
  3075. reimport_on_missing_imported_files = GLOBAL_GET("editor/import/reimport_missing_imported_files");
  3076. singleton = this;
  3077. filesystem = memnew(EditorFileSystemDirectory); //like, empty
  3078. filesystem->parent = nullptr;
  3079. new_filesystem = nullptr;
  3080. // This should probably also work on Unix and use the string it returns for FAT32 or exFAT
  3081. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  3082. using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "exFAT");
  3083. scan_total = 0;
  3084. ResourceSaver::set_get_resource_id_for_path(_resource_saver_get_resource_id_for_path);
  3085. // Set the callback method that the ResourceFormatImporter will use
  3086. // if resources are loaded during the first scan.
  3087. ResourceImporter::load_on_startup = _load_resource_on_startup;
  3088. }
  3089. EditorFileSystem::~EditorFileSystem() {
  3090. if (filesystem) {
  3091. memdelete(filesystem);
  3092. }
  3093. filesystem = nullptr;
  3094. ResourceSaver::set_get_resource_id_for_path(nullptr);
  3095. }