resource_format_text.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  1. /**************************************************************************/
  2. /* resource_format_text.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 "resource_format_text.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/missing_resource.h"
  34. #include "core/io/resource_format_binary.h"
  35. #include "core/object/script_language.h"
  36. #include "core/version.h"
  37. // Version 2: Changed names for Basis, AABB, Vectors, etc.
  38. // Version 3: New string ID for ext/subresources, breaks forward compat.
  39. // Version 4: PackedByteArray can be base64 encoded, and PackedVector4Array was added.
  40. #define FORMAT_VERSION 4
  41. // For compat, save as version 3 if not using PackedVector4Array or no big PackedByteArray.
  42. #define FORMAT_VERSION_COMPAT 3
  43. #define BINARY_FORMAT_VERSION 4
  44. #include "core/io/dir_access.h"
  45. #include "core/version.h"
  46. #define _printerr() ERR_PRINT(String(res_path + ":" + itos(lines) + " - Parse Error: " + error_text).utf8().get_data());
  47. ///
  48. Ref<Resource> ResourceLoaderText::get_resource() {
  49. return resource;
  50. }
  51. Error ResourceLoaderText::_parse_sub_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) {
  52. VariantParser::Token token;
  53. VariantParser::get_token(p_stream, token, line, r_err_str);
  54. if (token.type != VariantParser::TK_NUMBER && token.type != VariantParser::TK_STRING) {
  55. r_err_str = "Expected number (old style) or string (sub-resource index)";
  56. return ERR_PARSE_ERROR;
  57. }
  58. if (p_data->no_placeholders) {
  59. r_res.unref();
  60. } else {
  61. String unique_id = token.value;
  62. if (!p_data->resource_map.has(unique_id)) {
  63. r_err_str = "Found unique_id reference before mapping, sub-resources stored out of order in resource file";
  64. return ERR_PARSE_ERROR;
  65. }
  66. r_res = p_data->resource_map[unique_id];
  67. }
  68. VariantParser::get_token(p_stream, token, line, r_err_str);
  69. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  70. r_err_str = "Expected ')'";
  71. return ERR_PARSE_ERROR;
  72. }
  73. return OK;
  74. }
  75. Error ResourceLoaderText::_parse_ext_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) {
  76. VariantParser::Token token;
  77. VariantParser::get_token(p_stream, token, line, r_err_str);
  78. if (token.type != VariantParser::TK_NUMBER && token.type != VariantParser::TK_STRING) {
  79. r_err_str = "Expected number (old style sub-resource index) or String (ext-resource ID)";
  80. return ERR_PARSE_ERROR;
  81. }
  82. if (p_data->no_placeholders) {
  83. r_res.unref();
  84. } else {
  85. String id = token.value;
  86. ERR_FAIL_COND_V(!p_data->rev_external_resources.has(id), ERR_PARSE_ERROR);
  87. r_res = p_data->rev_external_resources[id];
  88. }
  89. VariantParser::get_token(p_stream, token, line, r_err_str);
  90. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  91. r_err_str = "Expected ')'";
  92. return ERR_PARSE_ERROR;
  93. }
  94. return OK;
  95. }
  96. Error ResourceLoaderText::_parse_sub_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) {
  97. VariantParser::Token token;
  98. VariantParser::get_token(p_stream, token, line, r_err_str);
  99. if (token.type != VariantParser::TK_NUMBER && token.type != VariantParser::TK_STRING) {
  100. r_err_str = "Expected number (old style sub-resource index) or string";
  101. return ERR_PARSE_ERROR;
  102. }
  103. String id = token.value;
  104. ERR_FAIL_COND_V(!int_resources.has(id), ERR_INVALID_PARAMETER);
  105. r_res = int_resources[id];
  106. VariantParser::get_token(p_stream, token, line, r_err_str);
  107. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  108. r_err_str = "Expected ')'";
  109. return ERR_PARSE_ERROR;
  110. }
  111. return OK;
  112. }
  113. Error ResourceLoaderText::_parse_ext_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) {
  114. VariantParser::Token token;
  115. VariantParser::get_token(p_stream, token, line, r_err_str);
  116. if (token.type != VariantParser::TK_NUMBER && token.type != VariantParser::TK_STRING) {
  117. r_err_str = "Expected number (old style sub-resource index) or String (ext-resource ID)";
  118. return ERR_PARSE_ERROR;
  119. }
  120. String id = token.value;
  121. Error err = OK;
  122. if (!ignore_resource_parsing) {
  123. if (!ext_resources.has(id)) {
  124. r_err_str = "Can't load cached ext-resource id: " + id;
  125. return ERR_PARSE_ERROR;
  126. }
  127. String path = ext_resources[id].path;
  128. String type = ext_resources[id].type;
  129. Ref<ResourceLoader::LoadToken> &load_token = ext_resources[id].load_token;
  130. if (load_token.is_valid()) { // If not valid, it's OK since then we know this load accepts broken dependencies.
  131. Ref<Resource> res = ResourceLoader::_load_complete(*load_token.ptr(), &err);
  132. if (res.is_null()) {
  133. if (!ResourceLoader::is_cleaning_tasks()) {
  134. if (ResourceLoader::get_abort_on_missing_resources()) {
  135. error = ERR_FILE_MISSING_DEPENDENCIES;
  136. error_text = "[ext_resource] referenced non-existent resource at: " + path;
  137. _printerr();
  138. err = error;
  139. } else {
  140. ResourceLoader::notify_dependency_error(local_path, path, type);
  141. }
  142. }
  143. } else {
  144. #ifdef TOOLS_ENABLED
  145. //remember ID for saving
  146. res->set_id_for_path(local_path, id);
  147. #endif
  148. r_res = res;
  149. }
  150. } else {
  151. r_res = Ref<Resource>();
  152. }
  153. #ifdef TOOLS_ENABLED
  154. if (r_res.is_null()) {
  155. // Hack to allow checking original path.
  156. r_res.instantiate();
  157. r_res->set_meta("__load_path__", ext_resources[id].path);
  158. }
  159. #endif
  160. }
  161. VariantParser::get_token(p_stream, token, line, r_err_str);
  162. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  163. r_err_str = "Expected ')'";
  164. return ERR_PARSE_ERROR;
  165. }
  166. return err;
  167. }
  168. Ref<PackedScene> ResourceLoaderText::_parse_node_tag(VariantParser::ResourceParser &parser) {
  169. Ref<PackedScene> packed_scene;
  170. packed_scene.instantiate();
  171. while (true) {
  172. if (next_tag.name == "node") {
  173. int parent = -1;
  174. int owner = -1;
  175. int type = -1;
  176. int name = -1;
  177. int instance = -1;
  178. int index = -1;
  179. //int base_scene=-1;
  180. if (next_tag.fields.has("name")) {
  181. name = packed_scene->get_state()->add_name(next_tag.fields["name"]);
  182. }
  183. if (next_tag.fields.has("parent")) {
  184. NodePath np = next_tag.fields["parent"];
  185. np.prepend_period(); //compatible to how it manages paths internally
  186. parent = packed_scene->get_state()->add_node_path(np);
  187. }
  188. if (next_tag.fields.has("type")) {
  189. type = packed_scene->get_state()->add_name(next_tag.fields["type"]);
  190. } else {
  191. type = SceneState::TYPE_INSTANTIATED; //no type? assume this was instantiated
  192. }
  193. HashSet<StringName> path_properties;
  194. if (next_tag.fields.has("node_paths")) {
  195. Vector<String> paths = next_tag.fields["node_paths"];
  196. for (int i = 0; i < paths.size(); i++) {
  197. path_properties.insert(paths[i]);
  198. }
  199. }
  200. if (next_tag.fields.has("instance")) {
  201. instance = packed_scene->get_state()->add_value(next_tag.fields["instance"]);
  202. if (packed_scene->get_state()->get_node_count() == 0 && parent == -1) {
  203. packed_scene->get_state()->set_base_scene(instance);
  204. instance = -1;
  205. }
  206. }
  207. if (next_tag.fields.has("instance_placeholder")) {
  208. String path = next_tag.fields["instance_placeholder"];
  209. int path_v = packed_scene->get_state()->add_value(path);
  210. if (packed_scene->get_state()->get_node_count() == 0) {
  211. error = ERR_FILE_CORRUPT;
  212. error_text = "Instance Placeholder can't be used for inheritance.";
  213. _printerr();
  214. return Ref<PackedScene>();
  215. }
  216. instance = path_v | SceneState::FLAG_INSTANCE_IS_PLACEHOLDER;
  217. }
  218. if (next_tag.fields.has("owner")) {
  219. owner = packed_scene->get_state()->add_node_path(next_tag.fields["owner"]);
  220. } else {
  221. if (parent != -1 && !(type == SceneState::TYPE_INSTANTIATED && instance == -1)) {
  222. owner = 0; //if no owner, owner is root
  223. }
  224. }
  225. if (next_tag.fields.has("index")) {
  226. index = next_tag.fields["index"];
  227. }
  228. int node_id = packed_scene->get_state()->add_node(parent, owner, type, name, instance, index);
  229. if (next_tag.fields.has("groups")) {
  230. Array groups = next_tag.fields["groups"];
  231. for (const Variant &group : groups) {
  232. packed_scene->get_state()->add_node_group(node_id, packed_scene->get_state()->add_name(group));
  233. }
  234. }
  235. while (true) {
  236. String assign;
  237. Variant value;
  238. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &parser);
  239. if (error) {
  240. if (error == ERR_FILE_MISSING_DEPENDENCIES) {
  241. // Resource loading error, just skip it.
  242. } else if (error != ERR_FILE_EOF) {
  243. ERR_PRINT(vformat("Parse Error: %s. [Resource file %s:%d]", error_names[error], res_path, lines));
  244. return Ref<PackedScene>();
  245. } else {
  246. error = OK;
  247. return packed_scene;
  248. }
  249. }
  250. if (!assign.is_empty()) {
  251. StringName assign_name = assign;
  252. int nameidx = packed_scene->get_state()->add_name(assign_name);
  253. int valueidx = packed_scene->get_state()->add_value(value);
  254. packed_scene->get_state()->add_node_property(node_id, nameidx, valueidx, path_properties.has(assign_name));
  255. //it's assignment
  256. } else if (!next_tag.name.is_empty()) {
  257. break;
  258. }
  259. }
  260. } else if (next_tag.name == "connection") {
  261. if (!next_tag.fields.has("from")) {
  262. error = ERR_FILE_CORRUPT;
  263. error_text = "missing 'from' field from connection tag";
  264. return Ref<PackedScene>();
  265. }
  266. if (!next_tag.fields.has("to")) {
  267. error = ERR_FILE_CORRUPT;
  268. error_text = "missing 'to' field from connection tag";
  269. return Ref<PackedScene>();
  270. }
  271. if (!next_tag.fields.has("signal")) {
  272. error = ERR_FILE_CORRUPT;
  273. error_text = "missing 'signal' field from connection tag";
  274. return Ref<PackedScene>();
  275. }
  276. if (!next_tag.fields.has("method")) {
  277. error = ERR_FILE_CORRUPT;
  278. error_text = "missing 'method' field from connection tag";
  279. return Ref<PackedScene>();
  280. }
  281. NodePath from = next_tag.fields["from"];
  282. NodePath to = next_tag.fields["to"];
  283. StringName method = next_tag.fields["method"];
  284. StringName signal = next_tag.fields["signal"];
  285. int flags = Object::CONNECT_PERSIST;
  286. int unbinds = 0;
  287. Array binds;
  288. if (next_tag.fields.has("flags")) {
  289. flags = next_tag.fields["flags"];
  290. }
  291. if (next_tag.fields.has("binds")) {
  292. binds = next_tag.fields["binds"];
  293. }
  294. if (next_tag.fields.has("unbinds")) {
  295. unbinds = next_tag.fields["unbinds"];
  296. }
  297. Vector<int> bind_ints;
  298. for (const Variant &bind : binds) {
  299. bind_ints.push_back(packed_scene->get_state()->add_value(bind));
  300. }
  301. packed_scene->get_state()->add_connection(
  302. packed_scene->get_state()->add_node_path(from.simplified()),
  303. packed_scene->get_state()->add_node_path(to.simplified()),
  304. packed_scene->get_state()->add_name(signal),
  305. packed_scene->get_state()->add_name(method),
  306. flags,
  307. unbinds,
  308. bind_ints);
  309. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &parser);
  310. if (error) {
  311. if (error != ERR_FILE_EOF) {
  312. _printerr();
  313. return Ref<PackedScene>();
  314. } else {
  315. error = OK;
  316. return packed_scene;
  317. }
  318. }
  319. } else if (next_tag.name == "editable") {
  320. if (!next_tag.fields.has("path")) {
  321. error = ERR_FILE_CORRUPT;
  322. error_text = "missing 'path' field from editable tag";
  323. _printerr();
  324. return Ref<PackedScene>();
  325. }
  326. NodePath path = next_tag.fields["path"];
  327. packed_scene->get_state()->add_editable_instance(path.simplified());
  328. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &parser);
  329. if (error) {
  330. if (error != ERR_FILE_EOF) {
  331. _printerr();
  332. return Ref<PackedScene>();
  333. } else {
  334. error = OK;
  335. return packed_scene;
  336. }
  337. }
  338. } else {
  339. error = ERR_FILE_CORRUPT;
  340. _printerr();
  341. return Ref<PackedScene>();
  342. }
  343. }
  344. }
  345. Error ResourceLoaderText::load() {
  346. if (error != OK) {
  347. return error;
  348. }
  349. while (true) {
  350. if (next_tag.name != "ext_resource") {
  351. break;
  352. }
  353. if (!next_tag.fields.has("path")) {
  354. error = ERR_FILE_CORRUPT;
  355. error_text = "Missing 'path' in external resource tag";
  356. _printerr();
  357. return error;
  358. }
  359. if (!next_tag.fields.has("type")) {
  360. error = ERR_FILE_CORRUPT;
  361. error_text = "Missing 'type' in external resource tag";
  362. _printerr();
  363. return error;
  364. }
  365. if (!next_tag.fields.has("id")) {
  366. error = ERR_FILE_CORRUPT;
  367. error_text = "Missing 'id' in external resource tag";
  368. _printerr();
  369. return error;
  370. }
  371. String path = next_tag.fields["path"];
  372. String type = next_tag.fields["type"];
  373. String id = next_tag.fields["id"];
  374. if (next_tag.fields.has("uid")) {
  375. String uidt = next_tag.fields["uid"];
  376. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(uidt);
  377. if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
  378. // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
  379. path = ResourceUID::get_singleton()->get_id_path(uid);
  380. } else {
  381. #ifdef TOOLS_ENABLED
  382. // Silence a warning that can happen during the initial filesystem scan due to cache being regenerated.
  383. if (ResourceLoader::get_resource_uid(path) != uid) {
  384. WARN_PRINT(String(res_path + ":" + itos(lines) + " - ext_resource, invalid UID: " + uidt + " - using text path instead: " + path).utf8().get_data());
  385. }
  386. #else
  387. WARN_PRINT(String(res_path + ":" + itos(lines) + " - ext_resource, invalid UID: " + uidt + " - using text path instead: " + path).utf8().get_data());
  388. #endif
  389. }
  390. }
  391. if (!path.contains("://") && path.is_relative_path()) {
  392. // path is relative to file being loaded, so convert to a resource path
  393. path = ProjectSettings::get_singleton()->localize_path(local_path.get_base_dir().path_join(path));
  394. }
  395. if (remaps.has(path)) {
  396. path = remaps[path];
  397. }
  398. ext_resources[id].path = path;
  399. ext_resources[id].type = type;
  400. ext_resources[id].load_token = ResourceLoader::_load_start(path, type, use_sub_threads ? ResourceLoader::LOAD_THREAD_DISTRIBUTE : ResourceLoader::LOAD_THREAD_FROM_CURRENT, cache_mode_for_external);
  401. if (!ext_resources[id].load_token.is_valid()) {
  402. if (ResourceLoader::get_abort_on_missing_resources()) {
  403. error = ERR_FILE_CORRUPT;
  404. error_text = "[ext_resource] referenced non-existent resource at: " + path;
  405. _printerr();
  406. return error;
  407. } else {
  408. ResourceLoader::notify_dependency_error(local_path, path, type);
  409. }
  410. }
  411. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  412. if (error) {
  413. _printerr();
  414. return error;
  415. }
  416. resource_current++;
  417. }
  418. //these are the ones that count
  419. resources_total -= resource_current;
  420. resource_current = 0;
  421. while (true) {
  422. if (next_tag.name != "sub_resource") {
  423. break;
  424. }
  425. if (!next_tag.fields.has("type")) {
  426. error = ERR_FILE_CORRUPT;
  427. error_text = "Missing 'type' in external resource tag";
  428. _printerr();
  429. return error;
  430. }
  431. if (!next_tag.fields.has("id")) {
  432. error = ERR_FILE_CORRUPT;
  433. error_text = "Missing 'id' in external resource tag";
  434. _printerr();
  435. return error;
  436. }
  437. String type = next_tag.fields["type"];
  438. String id = next_tag.fields["id"];
  439. String path = local_path + "::" + id;
  440. //bool exists=ResourceCache::has(path);
  441. Ref<Resource> res;
  442. bool do_assign = false;
  443. if (cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE && ResourceCache::has(path)) {
  444. //reuse existing
  445. Ref<Resource> cache = ResourceCache::get_ref(path);
  446. if (cache.is_valid() && cache->get_class() == type) {
  447. res = cache;
  448. res->reset_state();
  449. do_assign = true;
  450. }
  451. }
  452. MissingResource *missing_resource = nullptr;
  453. if (res.is_null()) { //not reuse
  454. Ref<Resource> cache = ResourceCache::get_ref(path);
  455. if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE && cache.is_valid()) { //only if it doesn't exist
  456. //cached, do not assign
  457. res = cache;
  458. } else {
  459. //create
  460. Object *obj = ClassDB::instantiate(type);
  461. if (!obj) {
  462. if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) {
  463. missing_resource = memnew(MissingResource);
  464. missing_resource->set_original_class(type);
  465. missing_resource->set_recording_properties(true);
  466. obj = missing_resource;
  467. } else {
  468. error_text += "Can't create sub resource of type: " + type;
  469. _printerr();
  470. error = ERR_FILE_CORRUPT;
  471. return error;
  472. }
  473. }
  474. Resource *r = Object::cast_to<Resource>(obj);
  475. if (!r) {
  476. error_text += "Can't create sub resource of type, because not a resource: " + type;
  477. _printerr();
  478. error = ERR_FILE_CORRUPT;
  479. return error;
  480. }
  481. res = Ref<Resource>(r);
  482. do_assign = true;
  483. }
  484. }
  485. resource_current++;
  486. if (progress && resources_total > 0) {
  487. *progress = resource_current / float(resources_total);
  488. }
  489. int_resources[id] = res; // Always assign int resources.
  490. if (do_assign) {
  491. if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) {
  492. res->set_path(path, cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE);
  493. } else {
  494. res->set_path_cache(path);
  495. }
  496. res->set_scene_unique_id(id);
  497. }
  498. Dictionary missing_resource_properties;
  499. while (true) {
  500. String assign;
  501. Variant value;
  502. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp);
  503. if (error) {
  504. _printerr();
  505. return error;
  506. }
  507. if (!assign.is_empty()) {
  508. if (do_assign) {
  509. bool set_valid = true;
  510. if (value.get_type() == Variant::OBJECT && missing_resource != nullptr) {
  511. // If the property being set is a missing resource (and the parent is not),
  512. // then setting it will most likely not work.
  513. // Instead, save it as metadata.
  514. Ref<MissingResource> mr = value;
  515. if (mr.is_valid()) {
  516. missing_resource_properties[assign] = mr;
  517. set_valid = false;
  518. }
  519. }
  520. if (value.get_type() == Variant::ARRAY) {
  521. Array set_array = value;
  522. bool is_get_valid = false;
  523. Variant get_value = res->get(assign, &is_get_valid);
  524. if (is_get_valid && get_value.get_type() == Variant::ARRAY) {
  525. Array get_array = get_value;
  526. if (!set_array.is_same_typed(get_array)) {
  527. value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());
  528. }
  529. }
  530. }
  531. if (set_valid) {
  532. res->set(assign, value);
  533. }
  534. }
  535. //it's assignment
  536. } else if (!next_tag.name.is_empty()) {
  537. error = OK;
  538. break;
  539. } else {
  540. error = ERR_FILE_CORRUPT;
  541. error_text = "Premature end of file while parsing [sub_resource]";
  542. _printerr();
  543. return error;
  544. }
  545. }
  546. if (missing_resource) {
  547. missing_resource->set_recording_properties(false);
  548. }
  549. if (!missing_resource_properties.is_empty()) {
  550. res->set_meta(META_MISSING_RESOURCES, missing_resource_properties);
  551. }
  552. }
  553. while (true) {
  554. if (next_tag.name != "resource") {
  555. break;
  556. }
  557. if (is_scene) {
  558. error_text += "found the 'resource' tag on a scene file!";
  559. _printerr();
  560. error = ERR_FILE_CORRUPT;
  561. return error;
  562. }
  563. Ref<Resource> cache = ResourceCache::get_ref(local_path);
  564. if (cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE && cache.is_valid() && cache->get_class() == res_type) {
  565. cache->reset_state();
  566. resource = cache;
  567. }
  568. MissingResource *missing_resource = nullptr;
  569. if (!resource.is_valid()) {
  570. Object *obj = ClassDB::instantiate(res_type);
  571. if (!obj) {
  572. if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) {
  573. missing_resource = memnew(MissingResource);
  574. missing_resource->set_original_class(res_type);
  575. missing_resource->set_recording_properties(true);
  576. obj = missing_resource;
  577. } else {
  578. error_text += "Can't create sub resource of type: " + res_type;
  579. _printerr();
  580. error = ERR_FILE_CORRUPT;
  581. return error;
  582. }
  583. }
  584. Resource *r = Object::cast_to<Resource>(obj);
  585. if (!r) {
  586. error_text += "Can't create sub resource of type, because not a resource: " + res_type;
  587. _printerr();
  588. error = ERR_FILE_CORRUPT;
  589. return error;
  590. }
  591. resource = Ref<Resource>(r);
  592. }
  593. Dictionary missing_resource_properties;
  594. while (true) {
  595. String assign;
  596. Variant value;
  597. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp);
  598. if (error) {
  599. if (error != ERR_FILE_EOF) {
  600. _printerr();
  601. } else {
  602. error = OK;
  603. if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) {
  604. if (!ResourceCache::has(res_path)) {
  605. resource->set_path(res_path);
  606. }
  607. resource->set_as_translation_remapped(translation_remapped);
  608. } else {
  609. resource->set_path_cache(res_path);
  610. }
  611. }
  612. return error;
  613. }
  614. if (!assign.is_empty()) {
  615. bool set_valid = true;
  616. if (value.get_type() == Variant::OBJECT && missing_resource != nullptr) {
  617. // If the property being set is a missing resource (and the parent is not),
  618. // then setting it will most likely not work.
  619. // Instead, save it as metadata.
  620. Ref<MissingResource> mr = value;
  621. if (mr.is_valid()) {
  622. missing_resource_properties[assign] = mr;
  623. set_valid = false;
  624. }
  625. }
  626. if (value.get_type() == Variant::ARRAY) {
  627. Array set_array = value;
  628. bool is_get_valid = false;
  629. Variant get_value = resource->get(assign, &is_get_valid);
  630. if (is_get_valid && get_value.get_type() == Variant::ARRAY) {
  631. Array get_array = get_value;
  632. if (!set_array.is_same_typed(get_array)) {
  633. value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());
  634. }
  635. }
  636. }
  637. if (set_valid) {
  638. resource->set(assign, value);
  639. }
  640. //it's assignment
  641. } else if (!next_tag.name.is_empty()) {
  642. error = ERR_FILE_CORRUPT;
  643. error_text = "Extra tag found when parsing main resource file";
  644. _printerr();
  645. return error;
  646. } else {
  647. break;
  648. }
  649. }
  650. resource_current++;
  651. if (progress && resources_total > 0) {
  652. *progress = resource_current / float(resources_total);
  653. }
  654. if (missing_resource) {
  655. missing_resource->set_recording_properties(false);
  656. }
  657. if (!missing_resource_properties.is_empty()) {
  658. resource->set_meta(META_MISSING_RESOURCES, missing_resource_properties);
  659. }
  660. error = OK;
  661. return error;
  662. }
  663. //for scene files
  664. if (next_tag.name == "node") {
  665. if (!is_scene) {
  666. error_text += "found the 'node' tag on a resource file!";
  667. _printerr();
  668. error = ERR_FILE_CORRUPT;
  669. return error;
  670. }
  671. Ref<PackedScene> packed_scene = _parse_node_tag(rp);
  672. if (!packed_scene.is_valid()) {
  673. return error;
  674. }
  675. error = OK;
  676. //get it here
  677. resource = packed_scene;
  678. if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) {
  679. if (!ResourceCache::has(res_path)) {
  680. packed_scene->set_path(res_path);
  681. }
  682. } else {
  683. packed_scene->get_state()->set_path(res_path);
  684. packed_scene->set_path_cache(res_path);
  685. }
  686. resource_current++;
  687. if (progress && resources_total > 0) {
  688. *progress = resource_current / float(resources_total);
  689. }
  690. return error;
  691. } else {
  692. error_text += "Unknown tag in file: " + next_tag.name;
  693. _printerr();
  694. error = ERR_FILE_CORRUPT;
  695. return error;
  696. }
  697. }
  698. int ResourceLoaderText::get_stage() const {
  699. return resource_current;
  700. }
  701. int ResourceLoaderText::get_stage_count() const {
  702. return resources_total; //+ext_resources;
  703. }
  704. void ResourceLoaderText::set_translation_remapped(bool p_remapped) {
  705. translation_remapped = p_remapped;
  706. }
  707. ResourceLoaderText::ResourceLoaderText() :
  708. stream(false), format_version(FORMAT_VERSION) {}
  709. void ResourceLoaderText::get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types) {
  710. open(p_f);
  711. ignore_resource_parsing = true;
  712. ERR_FAIL_COND(error != OK);
  713. while (next_tag.name == "ext_resource") {
  714. if (!next_tag.fields.has("type")) {
  715. error = ERR_FILE_CORRUPT;
  716. error_text = "Missing 'type' in external resource tag";
  717. _printerr();
  718. return;
  719. }
  720. if (!next_tag.fields.has("id")) {
  721. error = ERR_FILE_CORRUPT;
  722. error_text = "Missing 'id' in external resource tag";
  723. _printerr();
  724. return;
  725. }
  726. String path = next_tag.fields["path"];
  727. String type = next_tag.fields["type"];
  728. String fallback_path;
  729. bool using_uid = false;
  730. if (next_tag.fields.has("uid")) {
  731. // If uid exists, return uid in text format, not the path.
  732. String uidt = next_tag.fields["uid"];
  733. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(uidt);
  734. if (uid != ResourceUID::INVALID_ID) {
  735. fallback_path = path; // Used by Dependency Editor, in case uid path fails.
  736. path = ResourceUID::get_singleton()->id_to_text(uid);
  737. using_uid = true;
  738. }
  739. }
  740. if (!using_uid && !path.contains("://") && path.is_relative_path()) {
  741. // Path is relative to file being loaded, so convert to a resource path.
  742. path = ProjectSettings::get_singleton()->localize_path(local_path.get_base_dir().path_join(path));
  743. }
  744. if (p_add_types) {
  745. path += "::" + type;
  746. }
  747. if (!fallback_path.is_empty()) {
  748. if (!p_add_types) {
  749. path += "::"; // Ensure that path comes third, even if there is no type.
  750. }
  751. path += "::" + fallback_path;
  752. }
  753. p_dependencies->push_back(path);
  754. Error err = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  755. if (err) {
  756. print_line(error_text + " - " + itos(lines));
  757. error_text = "Unexpected end of file";
  758. _printerr();
  759. error = ERR_FILE_CORRUPT;
  760. return;
  761. }
  762. }
  763. }
  764. Error ResourceLoaderText::rename_dependencies(Ref<FileAccess> p_f, const String &p_path, const HashMap<String, String> &p_map) {
  765. open(p_f, true);
  766. ERR_FAIL_COND_V(error != OK, error);
  767. ignore_resource_parsing = true;
  768. //FileAccess
  769. Ref<FileAccess> fw;
  770. String base_path = local_path.get_base_dir();
  771. uint64_t tag_end = f->get_position();
  772. while (true) {
  773. Error err = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  774. if (err != OK) {
  775. error = ERR_FILE_CORRUPT;
  776. ERR_FAIL_V(error);
  777. }
  778. if (next_tag.name != "ext_resource") {
  779. //nothing was done
  780. if (fw.is_null()) {
  781. return OK;
  782. }
  783. break;
  784. } else {
  785. if (fw.is_null()) {
  786. fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);
  787. if (res_uid == ResourceUID::INVALID_ID) {
  788. res_uid = ResourceSaver::get_resource_id_for_path(p_path);
  789. }
  790. String uid_text = "";
  791. if (res_uid != ResourceUID::INVALID_ID) {
  792. uid_text = " uid=\"" + ResourceUID::get_singleton()->id_to_text(res_uid) + "\"";
  793. }
  794. if (is_scene) {
  795. fw->store_line("[gd_scene load_steps=" + itos(resources_total) + " format=" + itos(format_version) + uid_text + "]\n");
  796. } else {
  797. String script_res_text;
  798. if (!script_class.is_empty()) {
  799. script_res_text = "script_class=\"" + script_class + "\" ";
  800. }
  801. fw->store_line("[gd_resource type=\"" + res_type + "\" " + script_res_text + "load_steps=" + itos(resources_total) + " format=" + itos(format_version) + uid_text + "]\n");
  802. }
  803. }
  804. if (!next_tag.fields.has("path") || !next_tag.fields.has("id") || !next_tag.fields.has("type")) {
  805. error = ERR_FILE_CORRUPT;
  806. ERR_FAIL_V(error);
  807. }
  808. String path = next_tag.fields["path"];
  809. String id = next_tag.fields["id"];
  810. String type = next_tag.fields["type"];
  811. if (next_tag.fields.has("uid")) {
  812. String uidt = next_tag.fields["uid"];
  813. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(uidt);
  814. if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
  815. // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
  816. path = ResourceUID::get_singleton()->get_id_path(uid);
  817. }
  818. }
  819. bool relative = false;
  820. if (!path.begins_with("res://")) {
  821. path = base_path.path_join(path).simplify_path();
  822. relative = true;
  823. }
  824. if (p_map.has(path)) {
  825. path = p_map[path];
  826. }
  827. if (relative) {
  828. //restore relative
  829. path = base_path.path_to_file(path);
  830. }
  831. String s = "[ext_resource type=\"" + type + "\"";
  832. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(path);
  833. if (uid != ResourceUID::INVALID_ID) {
  834. s += " uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\"";
  835. }
  836. s += " path=\"" + path + "\" id=\"" + id + "\"]";
  837. fw->store_line(s); // Bundled.
  838. tag_end = f->get_position();
  839. }
  840. }
  841. f->seek(tag_end);
  842. const uint32_t buffer_size = 2048;
  843. uint8_t *buffer = (uint8_t *)alloca(buffer_size);
  844. uint32_t num_read;
  845. num_read = f->get_buffer(buffer, buffer_size);
  846. ERR_FAIL_COND_V_MSG(num_read == UINT32_MAX, ERR_CANT_CREATE, "Failed to allocate memory for buffer.");
  847. ERR_FAIL_COND_V(num_read == 0, ERR_FILE_CORRUPT);
  848. if (*buffer == '\n') {
  849. // Skip first newline character since we added one.
  850. if (num_read > 1) {
  851. fw->store_buffer(buffer + 1, num_read - 1);
  852. }
  853. } else {
  854. fw->store_buffer(buffer, num_read);
  855. }
  856. while (!f->eof_reached()) {
  857. num_read = f->get_buffer(buffer, buffer_size);
  858. fw->store_buffer(buffer, num_read);
  859. }
  860. bool all_ok = fw->get_error() == OK;
  861. if (!all_ok) {
  862. return ERR_CANT_CREATE;
  863. }
  864. return OK;
  865. }
  866. void ResourceLoaderText::open(Ref<FileAccess> p_f, bool p_skip_first_tag) {
  867. error = OK;
  868. lines = 1;
  869. f = p_f;
  870. stream.f = f;
  871. is_scene = false;
  872. ignore_resource_parsing = false;
  873. resource_current = 0;
  874. VariantParser::Tag tag;
  875. Error err = VariantParser::parse_tag(&stream, lines, error_text, tag);
  876. if (err) {
  877. error = err;
  878. _printerr();
  879. return;
  880. }
  881. if (tag.fields.has("format")) {
  882. format_version = tag.fields["format"];
  883. if (format_version > FORMAT_VERSION) {
  884. error_text = "Saved with newer format version";
  885. _printerr();
  886. error = ERR_FILE_UNRECOGNIZED;
  887. return;
  888. }
  889. } else {
  890. format_version = FORMAT_VERSION;
  891. }
  892. if (tag.name == "gd_scene") {
  893. is_scene = true;
  894. } else if (tag.name == "gd_resource") {
  895. if (!tag.fields.has("type")) {
  896. error_text = "Missing 'type' field in 'gd_resource' tag";
  897. _printerr();
  898. error = ERR_PARSE_ERROR;
  899. return;
  900. }
  901. if (tag.fields.has("script_class")) {
  902. script_class = tag.fields["script_class"];
  903. }
  904. res_type = tag.fields["type"];
  905. } else {
  906. error_text = "Unrecognized file type: " + tag.name;
  907. _printerr();
  908. error = ERR_PARSE_ERROR;
  909. return;
  910. }
  911. if (tag.fields.has("uid")) {
  912. res_uid = ResourceUID::get_singleton()->text_to_id(tag.fields["uid"]);
  913. } else {
  914. res_uid = ResourceUID::INVALID_ID;
  915. }
  916. if (tag.fields.has("load_steps")) {
  917. resources_total = tag.fields["load_steps"];
  918. } else {
  919. resources_total = 0;
  920. }
  921. if (!p_skip_first_tag) {
  922. err = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp);
  923. if (err) {
  924. error_text = "Unexpected end of file";
  925. _printerr();
  926. error = ERR_FILE_CORRUPT;
  927. }
  928. }
  929. rp.ext_func = _parse_ext_resources;
  930. rp.sub_func = _parse_sub_resources;
  931. rp.userdata = this;
  932. }
  933. static void bs_save_unicode_string(Ref<FileAccess> p_f, const String &p_string, bool p_bit_on_len = false) {
  934. CharString utf8 = p_string.utf8();
  935. if (p_bit_on_len) {
  936. p_f->store_32((utf8.length() + 1) | 0x80000000);
  937. } else {
  938. p_f->store_32(utf8.length() + 1);
  939. }
  940. p_f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
  941. }
  942. Error ResourceLoaderText::save_as_binary(const String &p_path) {
  943. if (error) {
  944. return error;
  945. }
  946. Ref<FileAccess> wf = FileAccess::open(p_path, FileAccess::WRITE);
  947. if (wf.is_null()) {
  948. return ERR_CANT_OPEN;
  949. }
  950. //save header compressed
  951. static const uint8_t header[4] = { 'R', 'S', 'R', 'C' };
  952. wf->store_buffer(header, 4);
  953. wf->store_32(0); //endianness, little endian
  954. wf->store_32(0); //64 bits file, false for now
  955. wf->store_32(VERSION_MAJOR);
  956. wf->store_32(VERSION_MINOR);
  957. static const int save_format_version = BINARY_FORMAT_VERSION;
  958. wf->store_32(save_format_version);
  959. bs_save_unicode_string(wf, is_scene ? "PackedScene" : resource_type);
  960. wf->store_64(0); //offset to import metadata, this is no longer used
  961. wf->store_32(ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS | ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS);
  962. wf->store_64(res_uid);
  963. for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
  964. wf->store_32(0); // reserved
  965. }
  966. wf->store_32(0); //string table size, will not be in use
  967. uint64_t ext_res_count_pos = wf->get_position();
  968. wf->store_32(0); //zero ext resources, still parsing them
  969. //go with external resources
  970. DummyReadData dummy_read;
  971. VariantParser::ResourceParser rp_new;
  972. rp_new.ext_func = _parse_ext_resource_dummys;
  973. rp_new.sub_func = _parse_sub_resource_dummys;
  974. rp_new.userdata = &dummy_read;
  975. while (next_tag.name == "ext_resource") {
  976. if (!next_tag.fields.has("path")) {
  977. error = ERR_FILE_CORRUPT;
  978. error_text = "Missing 'path' in external resource tag";
  979. _printerr();
  980. return error;
  981. }
  982. if (!next_tag.fields.has("type")) {
  983. error = ERR_FILE_CORRUPT;
  984. error_text = "Missing 'type' in external resource tag";
  985. _printerr();
  986. return error;
  987. }
  988. if (!next_tag.fields.has("id")) {
  989. error = ERR_FILE_CORRUPT;
  990. error_text = "Missing 'id' in external resource tag";
  991. _printerr();
  992. return error;
  993. }
  994. String path = next_tag.fields["path"];
  995. String type = next_tag.fields["type"];
  996. String id = next_tag.fields["id"];
  997. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  998. if (next_tag.fields.has("uid")) {
  999. String uidt = next_tag.fields["uid"];
  1000. uid = ResourceUID::get_singleton()->text_to_id(uidt);
  1001. }
  1002. bs_save_unicode_string(wf, type);
  1003. bs_save_unicode_string(wf, path);
  1004. wf->store_64(uid);
  1005. int lindex = dummy_read.external_resources.size();
  1006. Ref<DummyResource> dr;
  1007. dr.instantiate();
  1008. dr->set_path("res://dummy" + itos(lindex)); //anything is good to detect it for saving as external
  1009. dummy_read.external_resources[dr] = lindex;
  1010. dummy_read.rev_external_resources[id] = dr;
  1011. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp_new);
  1012. if (error) {
  1013. _printerr();
  1014. return error;
  1015. }
  1016. }
  1017. // save external resource table
  1018. wf->seek(ext_res_count_pos);
  1019. wf->store_32(dummy_read.external_resources.size());
  1020. wf->seek_end();
  1021. //now, save resources to a separate file, for now
  1022. uint64_t sub_res_count_pos = wf->get_position();
  1023. wf->store_32(0); //zero sub resources, still parsing them
  1024. String temp_file = p_path + ".temp";
  1025. Vector<uint64_t> local_offsets;
  1026. Vector<uint64_t> local_pointers_pos;
  1027. {
  1028. Ref<FileAccess> wf2 = FileAccess::open(temp_file, FileAccess::WRITE);
  1029. if (wf2.is_null()) {
  1030. return ERR_CANT_OPEN;
  1031. }
  1032. while (next_tag.name == "sub_resource" || next_tag.name == "resource") {
  1033. String type;
  1034. String id;
  1035. bool main_res;
  1036. if (next_tag.name == "sub_resource") {
  1037. if (!next_tag.fields.has("type")) {
  1038. error = ERR_FILE_CORRUPT;
  1039. error_text = "Missing 'type' in external resource tag";
  1040. _printerr();
  1041. return error;
  1042. }
  1043. if (!next_tag.fields.has("id")) {
  1044. error = ERR_FILE_CORRUPT;
  1045. error_text = "Missing 'id' in external resource tag";
  1046. _printerr();
  1047. return error;
  1048. }
  1049. type = next_tag.fields["type"];
  1050. id = next_tag.fields["id"];
  1051. main_res = false;
  1052. if (!dummy_read.resource_map.has(id)) {
  1053. Ref<DummyResource> dr;
  1054. dr.instantiate();
  1055. dr->set_scene_unique_id(id);
  1056. dummy_read.resource_map[id] = dr;
  1057. uint32_t im_size = dummy_read.resource_index_map.size();
  1058. dummy_read.resource_index_map.insert(dr, im_size);
  1059. }
  1060. } else {
  1061. type = res_type;
  1062. String uid_text = ResourceUID::get_singleton()->id_to_text(res_uid);
  1063. id = type + "_" + uid_text.replace("uid://", "").replace("<invalid>", "0");
  1064. main_res = true;
  1065. }
  1066. local_offsets.push_back(wf2->get_position());
  1067. bs_save_unicode_string(wf, "local://" + id);
  1068. local_pointers_pos.push_back(wf->get_position());
  1069. wf->store_64(0); //temp local offset
  1070. bs_save_unicode_string(wf2, type);
  1071. uint64_t propcount_ofs = wf2->get_position();
  1072. wf2->store_32(0);
  1073. int prop_count = 0;
  1074. while (true) {
  1075. String assign;
  1076. Variant value;
  1077. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp_new);
  1078. if (error) {
  1079. if (main_res && error == ERR_FILE_EOF) {
  1080. next_tag.name = ""; //exit
  1081. break;
  1082. }
  1083. _printerr();
  1084. return error;
  1085. }
  1086. if (!assign.is_empty()) {
  1087. HashMap<StringName, int> empty_string_map; //unused
  1088. bs_save_unicode_string(wf2, assign, true);
  1089. ResourceFormatSaverBinaryInstance::write_variant(wf2, value, dummy_read.resource_index_map, dummy_read.external_resources, empty_string_map);
  1090. prop_count++;
  1091. } else if (!next_tag.name.is_empty()) {
  1092. error = OK;
  1093. break;
  1094. } else {
  1095. error = ERR_FILE_CORRUPT;
  1096. error_text = "Premature end of file while parsing [sub_resource]";
  1097. _printerr();
  1098. return error;
  1099. }
  1100. }
  1101. wf2->seek(propcount_ofs);
  1102. wf2->store_32(prop_count);
  1103. wf2->seek_end();
  1104. }
  1105. if (next_tag.name == "node") {
  1106. // This is a node, must save one more!
  1107. if (!is_scene) {
  1108. error_text += "found the 'node' tag on a resource file!";
  1109. _printerr();
  1110. error = ERR_FILE_CORRUPT;
  1111. return error;
  1112. }
  1113. Ref<PackedScene> packed_scene = _parse_node_tag(rp_new);
  1114. if (!packed_scene.is_valid()) {
  1115. return error;
  1116. }
  1117. error = OK;
  1118. //get it here
  1119. List<PropertyInfo> props;
  1120. packed_scene->get_property_list(&props);
  1121. String id = "PackedScene_" + ResourceUID::get_singleton()->id_to_text(res_uid).replace("uid://", "").replace("<invalid>", "0");
  1122. bs_save_unicode_string(wf, "local://" + id);
  1123. local_pointers_pos.push_back(wf->get_position());
  1124. wf->store_64(0); //temp local offset
  1125. local_offsets.push_back(wf2->get_position());
  1126. bs_save_unicode_string(wf2, "PackedScene");
  1127. uint64_t propcount_ofs = wf2->get_position();
  1128. wf2->store_32(0);
  1129. int prop_count = 0;
  1130. for (const PropertyInfo &E : props) {
  1131. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1132. continue;
  1133. }
  1134. String name = E.name;
  1135. Variant value = packed_scene->get(name);
  1136. HashMap<StringName, int> empty_string_map; //unused
  1137. bs_save_unicode_string(wf2, name, true);
  1138. ResourceFormatSaverBinaryInstance::write_variant(wf2, value, dummy_read.resource_index_map, dummy_read.external_resources, empty_string_map);
  1139. prop_count++;
  1140. }
  1141. wf2->seek(propcount_ofs);
  1142. wf2->store_32(prop_count);
  1143. wf2->seek_end();
  1144. }
  1145. }
  1146. uint64_t offset_from = wf->get_position();
  1147. wf->seek(sub_res_count_pos); //plus one because the saved one
  1148. wf->store_32(local_offsets.size());
  1149. for (int i = 0; i < local_offsets.size(); i++) {
  1150. wf->seek(local_pointers_pos[i]);
  1151. wf->store_64(local_offsets[i] + offset_from);
  1152. }
  1153. wf->seek_end();
  1154. Vector<uint8_t> data = FileAccess::get_file_as_bytes(temp_file);
  1155. wf->store_buffer(data.ptr(), data.size());
  1156. {
  1157. Ref<DirAccess> dar = DirAccess::open(temp_file.get_base_dir());
  1158. ERR_FAIL_COND_V(dar.is_null(), FAILED);
  1159. dar->remove(temp_file);
  1160. }
  1161. wf->store_buffer((const uint8_t *)"RSRC", 4); //magic at end
  1162. return OK;
  1163. }
  1164. Error ResourceLoaderText::get_classes_used(HashSet<StringName> *r_classes) {
  1165. if (error) {
  1166. return error;
  1167. }
  1168. ignore_resource_parsing = true;
  1169. DummyReadData dummy_read;
  1170. dummy_read.no_placeholders = true;
  1171. VariantParser::ResourceParser rp_new;
  1172. rp_new.ext_func = _parse_ext_resource_dummys;
  1173. rp_new.sub_func = _parse_sub_resource_dummys;
  1174. rp_new.userdata = &dummy_read;
  1175. while (next_tag.name == "ext_resource") {
  1176. error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp_new);
  1177. if (error) {
  1178. _printerr();
  1179. return error;
  1180. }
  1181. }
  1182. while (next_tag.name == "sub_resource" || next_tag.name == "resource") {
  1183. if (next_tag.name == "sub_resource") {
  1184. if (!next_tag.fields.has("type")) {
  1185. error = ERR_FILE_CORRUPT;
  1186. error_text = "Missing 'type' in external resource tag";
  1187. _printerr();
  1188. return error;
  1189. }
  1190. r_classes->insert(next_tag.fields["type"]);
  1191. } else {
  1192. r_classes->insert(next_tag.fields["res_type"]);
  1193. }
  1194. while (true) {
  1195. String assign;
  1196. Variant value;
  1197. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp_new);
  1198. if (error) {
  1199. if (error == ERR_FILE_EOF) {
  1200. return OK;
  1201. }
  1202. _printerr();
  1203. return error;
  1204. }
  1205. if (!assign.is_empty()) {
  1206. continue;
  1207. } else if (!next_tag.name.is_empty()) {
  1208. error = OK;
  1209. break;
  1210. } else {
  1211. error = ERR_FILE_CORRUPT;
  1212. error_text = "Premature end of file while parsing [sub_resource]";
  1213. _printerr();
  1214. return error;
  1215. }
  1216. }
  1217. }
  1218. while (next_tag.name == "node") {
  1219. // This is a node, must save one more!
  1220. if (!is_scene) {
  1221. error_text += "found the 'node' tag on a resource file!";
  1222. _printerr();
  1223. error = ERR_FILE_CORRUPT;
  1224. return error;
  1225. }
  1226. if (!next_tag.fields.has("type")) {
  1227. error = ERR_FILE_CORRUPT;
  1228. error_text = "Missing 'type' in external resource tag";
  1229. _printerr();
  1230. return error;
  1231. }
  1232. r_classes->insert(next_tag.fields["type"]);
  1233. while (true) {
  1234. String assign;
  1235. Variant value;
  1236. error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp_new);
  1237. if (error) {
  1238. if (error == ERR_FILE_MISSING_DEPENDENCIES) {
  1239. // Resource loading error, just skip it.
  1240. } else if (error != ERR_FILE_EOF) {
  1241. _printerr();
  1242. return error;
  1243. } else {
  1244. return OK;
  1245. }
  1246. }
  1247. if (!assign.is_empty()) {
  1248. continue;
  1249. } else if (!next_tag.name.is_empty()) {
  1250. error = OK;
  1251. break;
  1252. } else {
  1253. error = ERR_FILE_CORRUPT;
  1254. error_text = "Premature end of file while parsing [sub_resource]";
  1255. _printerr();
  1256. return error;
  1257. }
  1258. }
  1259. }
  1260. return OK;
  1261. }
  1262. String ResourceLoaderText::recognize_script_class(Ref<FileAccess> p_f) {
  1263. error = OK;
  1264. lines = 1;
  1265. f = p_f;
  1266. stream.f = f;
  1267. ignore_resource_parsing = true;
  1268. VariantParser::Tag tag;
  1269. Error err = VariantParser::parse_tag(&stream, lines, error_text, tag);
  1270. if (err) {
  1271. _printerr();
  1272. return "";
  1273. }
  1274. if (tag.fields.has("format")) {
  1275. int fmt = tag.fields["format"];
  1276. if (fmt > FORMAT_VERSION) {
  1277. error_text = "Saved with newer format version";
  1278. _printerr();
  1279. return "";
  1280. }
  1281. }
  1282. if (tag.name != "gd_resource") {
  1283. return "";
  1284. }
  1285. if (tag.fields.has("script_class")) {
  1286. return tag.fields["script_class"];
  1287. }
  1288. return "";
  1289. }
  1290. String ResourceLoaderText::recognize(Ref<FileAccess> p_f) {
  1291. error = OK;
  1292. lines = 1;
  1293. f = p_f;
  1294. stream.f = f;
  1295. ignore_resource_parsing = true;
  1296. VariantParser::Tag tag;
  1297. Error err = VariantParser::parse_tag(&stream, lines, error_text, tag);
  1298. if (err) {
  1299. _printerr();
  1300. return "";
  1301. }
  1302. if (tag.fields.has("format")) {
  1303. int fmt = tag.fields["format"];
  1304. if (fmt > FORMAT_VERSION) {
  1305. error_text = "Saved with newer format version";
  1306. _printerr();
  1307. return "";
  1308. }
  1309. }
  1310. if (tag.name == "gd_scene") {
  1311. return "PackedScene";
  1312. }
  1313. if (tag.name != "gd_resource") {
  1314. return "";
  1315. }
  1316. if (!tag.fields.has("type")) {
  1317. error_text = "Missing 'type' field in 'gd_resource' tag";
  1318. _printerr();
  1319. return "";
  1320. }
  1321. return tag.fields["type"];
  1322. }
  1323. ResourceUID::ID ResourceLoaderText::get_uid(Ref<FileAccess> p_f) {
  1324. error = OK;
  1325. lines = 1;
  1326. f = p_f;
  1327. stream.f = f;
  1328. ignore_resource_parsing = true;
  1329. VariantParser::Tag tag;
  1330. Error err = VariantParser::parse_tag(&stream, lines, error_text, tag);
  1331. if (err) {
  1332. _printerr();
  1333. return ResourceUID::INVALID_ID;
  1334. }
  1335. if (tag.fields.has("uid")) { //field is optional
  1336. String uidt = tag.fields["uid"];
  1337. return ResourceUID::get_singleton()->text_to_id(uidt);
  1338. }
  1339. return ResourceUID::INVALID_ID;
  1340. }
  1341. /////////////////////
  1342. Ref<Resource> ResourceFormatLoaderText::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  1343. if (r_error) {
  1344. *r_error = ERR_CANT_OPEN;
  1345. }
  1346. Error err;
  1347. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  1348. ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Cannot open file '" + p_path + "'.");
  1349. ResourceLoaderText loader;
  1350. String path = !p_original_path.is_empty() ? p_original_path : p_path;
  1351. switch (p_cache_mode) {
  1352. case CACHE_MODE_IGNORE:
  1353. case CACHE_MODE_REUSE:
  1354. case CACHE_MODE_REPLACE:
  1355. loader.cache_mode = p_cache_mode;
  1356. loader.cache_mode_for_external = CACHE_MODE_REUSE;
  1357. break;
  1358. case CACHE_MODE_IGNORE_DEEP:
  1359. loader.cache_mode = ResourceFormatLoader::CACHE_MODE_IGNORE;
  1360. loader.cache_mode_for_external = p_cache_mode;
  1361. break;
  1362. case CACHE_MODE_REPLACE_DEEP:
  1363. loader.cache_mode = ResourceFormatLoader::CACHE_MODE_REPLACE;
  1364. loader.cache_mode_for_external = p_cache_mode;
  1365. break;
  1366. }
  1367. loader.use_sub_threads = p_use_sub_threads;
  1368. loader.local_path = ProjectSettings::get_singleton()->localize_path(path);
  1369. loader.progress = r_progress;
  1370. loader.res_path = loader.local_path;
  1371. loader.open(f);
  1372. err = loader.load();
  1373. if (r_error) {
  1374. *r_error = err;
  1375. }
  1376. if (err == OK) {
  1377. return loader.get_resource();
  1378. } else {
  1379. return Ref<Resource>();
  1380. }
  1381. }
  1382. void ResourceFormatLoaderText::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  1383. if (p_type.is_empty()) {
  1384. get_recognized_extensions(p_extensions);
  1385. return;
  1386. }
  1387. if (ClassDB::is_parent_class("PackedScene", p_type)) {
  1388. p_extensions->push_back("tscn");
  1389. }
  1390. // Don't allow .tres for PackedScenes.
  1391. if (p_type != "PackedScene") {
  1392. p_extensions->push_back("tres");
  1393. }
  1394. }
  1395. void ResourceFormatLoaderText::get_recognized_extensions(List<String> *p_extensions) const {
  1396. p_extensions->push_back("tscn");
  1397. p_extensions->push_back("tres");
  1398. }
  1399. bool ResourceFormatLoaderText::handles_type(const String &p_type) const {
  1400. return true;
  1401. }
  1402. void ResourceFormatLoaderText::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
  1403. String ext = p_path.get_extension().to_lower();
  1404. if (ext == "tscn") {
  1405. r_classes->insert("PackedScene");
  1406. }
  1407. // ...for anything else must test...
  1408. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1409. if (f.is_null()) {
  1410. return; // Could not read.
  1411. }
  1412. ResourceLoaderText loader;
  1413. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1414. loader.res_path = loader.local_path;
  1415. loader.open(f);
  1416. loader.get_classes_used(r_classes);
  1417. }
  1418. String ResourceFormatLoaderText::get_resource_type(const String &p_path) const {
  1419. String ext = p_path.get_extension().to_lower();
  1420. if (ext == "tscn") {
  1421. return "PackedScene";
  1422. } else if (ext != "tres") {
  1423. return String();
  1424. }
  1425. // ...for anything else must test...
  1426. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1427. if (f.is_null()) {
  1428. return ""; //could not read
  1429. }
  1430. ResourceLoaderText loader;
  1431. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1432. loader.res_path = loader.local_path;
  1433. String r = loader.recognize(f);
  1434. return ClassDB::get_compatibility_remapped_class(r);
  1435. }
  1436. String ResourceFormatLoaderText::get_resource_script_class(const String &p_path) const {
  1437. String ext = p_path.get_extension().to_lower();
  1438. if (ext != "tres") {
  1439. return String();
  1440. }
  1441. // ...for anything else must test...
  1442. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1443. if (f.is_null()) {
  1444. return ""; //could not read
  1445. }
  1446. ResourceLoaderText loader;
  1447. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1448. loader.res_path = loader.local_path;
  1449. return loader.recognize_script_class(f);
  1450. }
  1451. ResourceUID::ID ResourceFormatLoaderText::get_resource_uid(const String &p_path) const {
  1452. String ext = p_path.get_extension().to_lower();
  1453. if (ext != "tscn" && ext != "tres") {
  1454. return ResourceUID::INVALID_ID;
  1455. }
  1456. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1457. if (f.is_null()) {
  1458. return ResourceUID::INVALID_ID; //could not read
  1459. }
  1460. ResourceLoaderText loader;
  1461. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1462. loader.res_path = loader.local_path;
  1463. return loader.get_uid(f);
  1464. }
  1465. void ResourceFormatLoaderText::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  1466. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1467. if (f.is_null()) {
  1468. ERR_FAIL();
  1469. }
  1470. ResourceLoaderText loader;
  1471. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1472. loader.res_path = loader.local_path;
  1473. loader.get_dependencies(f, p_dependencies, p_add_types);
  1474. }
  1475. Error ResourceFormatLoaderText::rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) {
  1476. Error err = OK;
  1477. {
  1478. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1479. if (f.is_null()) {
  1480. ERR_FAIL_V(ERR_CANT_OPEN);
  1481. }
  1482. ResourceLoaderText loader;
  1483. loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1484. loader.res_path = loader.local_path;
  1485. err = loader.rename_dependencies(f, p_path, p_map);
  1486. }
  1487. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1488. if (err == OK && da->file_exists(p_path + ".depren")) {
  1489. da->remove(p_path);
  1490. da->rename(p_path + ".depren", p_path);
  1491. }
  1492. return err;
  1493. }
  1494. ResourceFormatLoaderText *ResourceFormatLoaderText::singleton = nullptr;
  1495. Error ResourceFormatLoaderText::convert_file_to_binary(const String &p_src_path, const String &p_dst_path) {
  1496. Error err;
  1497. Ref<FileAccess> f = FileAccess::open(p_src_path, FileAccess::READ, &err);
  1498. ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_OPEN, "Cannot open file '" + p_src_path + "'.");
  1499. ResourceLoaderText loader;
  1500. const String &path = p_src_path;
  1501. loader.local_path = ProjectSettings::get_singleton()->localize_path(path);
  1502. loader.res_path = loader.local_path;
  1503. loader.open(f);
  1504. return loader.save_as_binary(p_dst_path);
  1505. }
  1506. /*****************************************************************************************************/
  1507. /*****************************************************************************************************/
  1508. /*****************************************************************************************************/
  1509. /*****************************************************************************************************/
  1510. /*****************************************************************************************************/
  1511. /*****************************************************************************************************/
  1512. /*****************************************************************************************************/
  1513. /*****************************************************************************************************/
  1514. /*****************************************************************************************************/
  1515. /*****************************************************************************************************/
  1516. String ResourceFormatSaverTextInstance::_write_resources(void *ud, const Ref<Resource> &p_resource) {
  1517. ResourceFormatSaverTextInstance *rsi = static_cast<ResourceFormatSaverTextInstance *>(ud);
  1518. return rsi->_write_resource(p_resource);
  1519. }
  1520. String ResourceFormatSaverTextInstance::_write_resource(const Ref<Resource> &res) {
  1521. if (res->get_meta(SNAME("_skip_save_"), false)) {
  1522. return "null";
  1523. }
  1524. if (external_resources.has(res)) {
  1525. return "ExtResource(\"" + external_resources[res] + "\")";
  1526. } else {
  1527. if (internal_resources.has(res)) {
  1528. return "SubResource(\"" + internal_resources[res] + "\")";
  1529. } else if (!res->is_built_in()) {
  1530. if (res->get_path() == local_path) { //circular reference attempt
  1531. return "null";
  1532. }
  1533. //external resource
  1534. String path = relative_paths ? local_path.path_to_file(res->get_path()) : res->get_path();
  1535. return "Resource(\"" + path + "\")";
  1536. } else {
  1537. ERR_FAIL_V_MSG("null", "Resource was not pre cached for the resource section, bug?");
  1538. //internal resource
  1539. }
  1540. }
  1541. }
  1542. void ResourceFormatSaverTextInstance::_find_resources(const Variant &p_variant, bool p_main) {
  1543. switch (p_variant.get_type()) {
  1544. case Variant::OBJECT: {
  1545. Ref<Resource> res = p_variant;
  1546. if (res.is_null() || external_resources.has(res) || res->get_meta(SNAME("_skip_save_"), false)) {
  1547. return;
  1548. }
  1549. if (!p_main && (!bundle_resources) && !res->is_built_in()) {
  1550. if (res->get_path() == local_path) {
  1551. ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded.");
  1552. return;
  1553. }
  1554. // Use a numeric ID as a base, because they are sorted in natural order before saving.
  1555. // This increases the chances of thread loading to fetch them first.
  1556. String id = itos(external_resources.size() + 1) + "_" + Resource::generate_scene_unique_id();
  1557. external_resources[res] = id;
  1558. return;
  1559. }
  1560. if (resource_set.has(res)) {
  1561. return;
  1562. }
  1563. resource_set.insert(res);
  1564. List<PropertyInfo> property_list;
  1565. res->get_property_list(&property_list);
  1566. property_list.sort();
  1567. List<PropertyInfo>::Element *I = property_list.front();
  1568. while (I) {
  1569. PropertyInfo pi = I->get();
  1570. if (pi.usage & PROPERTY_USAGE_STORAGE) {
  1571. Variant v = res->get(I->get().name);
  1572. if (pi.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1573. NonPersistentKey npk;
  1574. npk.base = res;
  1575. npk.property = pi.name;
  1576. non_persistent_map[npk] = v;
  1577. Ref<Resource> sres = v;
  1578. if (sres.is_valid()) {
  1579. resource_set.insert(sres);
  1580. saved_resources.push_back(sres);
  1581. } else {
  1582. _find_resources(v);
  1583. }
  1584. } else {
  1585. _find_resources(v);
  1586. }
  1587. }
  1588. I = I->next();
  1589. }
  1590. saved_resources.push_back(res); // Saved after, so the children it needs are available when loaded
  1591. } break;
  1592. case Variant::ARRAY: {
  1593. Array varray = p_variant;
  1594. _find_resources(varray.get_typed_script());
  1595. for (const Variant &var : varray) {
  1596. _find_resources(var);
  1597. }
  1598. } break;
  1599. case Variant::DICTIONARY: {
  1600. Dictionary d = p_variant;
  1601. List<Variant> keys;
  1602. d.get_key_list(&keys);
  1603. for (const Variant &E : keys) {
  1604. // Of course keys should also be cached, after all we can't prevent users from using resources as keys, right?
  1605. // See also ResourceFormatSaverBinaryInstance::_find_resources (when p_variant is of type Variant::DICTIONARY)
  1606. _find_resources(E);
  1607. Variant v = d[E];
  1608. _find_resources(v);
  1609. }
  1610. } break;
  1611. case Variant::PACKED_BYTE_ARRAY: {
  1612. // Balance between compatibility and performance.
  1613. if (use_compat && p_variant.operator PackedByteArray().size() > 64) {
  1614. use_compat = false;
  1615. }
  1616. } break;
  1617. case Variant::PACKED_VECTOR4_ARRAY: {
  1618. use_compat = false;
  1619. } break;
  1620. default: {
  1621. }
  1622. }
  1623. }
  1624. static String _resource_get_class(Ref<Resource> p_resource) {
  1625. Ref<MissingResource> missing_resource = p_resource;
  1626. if (missing_resource.is_valid()) {
  1627. return missing_resource->get_original_class();
  1628. } else {
  1629. return p_resource->get_class();
  1630. }
  1631. }
  1632. Error ResourceFormatSaverTextInstance::save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags) {
  1633. if (p_path.ends_with(".tscn")) {
  1634. packed_scene = p_resource;
  1635. }
  1636. Error err;
  1637. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1638. ERR_FAIL_COND_V_MSG(err, ERR_CANT_OPEN, "Cannot save file '" + p_path + "'.");
  1639. Ref<FileAccess> _fref(f);
  1640. local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1641. relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS;
  1642. skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  1643. bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES;
  1644. takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1645. if (!p_path.begins_with("res://")) {
  1646. takeover_paths = false;
  1647. }
  1648. // Save resources.
  1649. use_compat = true; // _find_resources() changes this.
  1650. _find_resources(p_resource, true);
  1651. if (packed_scene.is_valid()) {
  1652. // Add instances to external resources if saving a packed scene.
  1653. for (int i = 0; i < packed_scene->get_state()->get_node_count(); i++) {
  1654. if (packed_scene->get_state()->is_node_instance_placeholder(i)) {
  1655. continue;
  1656. }
  1657. Ref<PackedScene> instance = packed_scene->get_state()->get_node_instance(i);
  1658. if (instance.is_valid() && !external_resources.has(instance)) {
  1659. int index = external_resources.size() + 1;
  1660. external_resources[instance] = itos(index) + "_" + Resource::generate_scene_unique_id(); // Keep the order for improved thread loading performance.
  1661. }
  1662. }
  1663. }
  1664. {
  1665. String title = packed_scene.is_valid() ? "[gd_scene " : "[gd_resource ";
  1666. if (packed_scene.is_null()) {
  1667. title += "type=\"" + _resource_get_class(p_resource) + "\" ";
  1668. Ref<Script> script = p_resource->get_script();
  1669. if (script.is_valid() && script->get_global_name()) {
  1670. title += "script_class=\"" + String(script->get_global_name()) + "\" ";
  1671. }
  1672. }
  1673. int load_steps = saved_resources.size() + external_resources.size();
  1674. if (load_steps > 1) {
  1675. title += "load_steps=" + itos(load_steps) + " ";
  1676. }
  1677. title += "format=" + itos(use_compat ? FORMAT_VERSION_COMPAT : FORMAT_VERSION) + "";
  1678. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(local_path, true);
  1679. if (uid != ResourceUID::INVALID_ID) {
  1680. title += " uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\"";
  1681. }
  1682. f->store_string(title);
  1683. f->store_line("]\n"); // One empty line.
  1684. }
  1685. #ifdef TOOLS_ENABLED
  1686. // Keep order from cached ids.
  1687. HashSet<String> cached_ids_found;
  1688. for (KeyValue<Ref<Resource>, String> &E : external_resources) {
  1689. String cached_id = E.key->get_id_for_path(local_path);
  1690. if (cached_id.is_empty() || cached_ids_found.has(cached_id)) {
  1691. int sep_pos = E.value.find("_");
  1692. if (sep_pos != -1) {
  1693. E.value = E.value.substr(0, sep_pos + 1); // Keep the order found, for improved thread loading performance.
  1694. } else {
  1695. E.value = "";
  1696. }
  1697. } else {
  1698. E.value = cached_id;
  1699. cached_ids_found.insert(cached_id);
  1700. }
  1701. }
  1702. // Create IDs for non cached resources.
  1703. for (KeyValue<Ref<Resource>, String> &E : external_resources) {
  1704. if (cached_ids_found.has(E.value)) { // Already cached, go on.
  1705. continue;
  1706. }
  1707. String attempt;
  1708. while (true) {
  1709. attempt = E.value + Resource::generate_scene_unique_id();
  1710. if (!cached_ids_found.has(attempt)) {
  1711. break;
  1712. }
  1713. }
  1714. cached_ids_found.insert(attempt);
  1715. E.value = attempt;
  1716. // Update also in resource.
  1717. Ref<Resource> res = E.key;
  1718. res->set_id_for_path(local_path, attempt);
  1719. }
  1720. #else
  1721. // Make sure to start from one, as it makes format more readable.
  1722. int counter = 1;
  1723. for (KeyValue<Ref<Resource>, String> &E : external_resources) {
  1724. E.value = itos(counter++);
  1725. }
  1726. #endif
  1727. Vector<ResourceSort> sorted_er;
  1728. for (const KeyValue<Ref<Resource>, String> &E : external_resources) {
  1729. ResourceSort rs;
  1730. rs.resource = E.key;
  1731. rs.id = E.value;
  1732. sorted_er.push_back(rs);
  1733. }
  1734. sorted_er.sort();
  1735. for (int i = 0; i < sorted_er.size(); i++) {
  1736. String p = sorted_er[i].resource->get_path();
  1737. String s = "[ext_resource type=\"" + sorted_er[i].resource->get_save_class() + "\"";
  1738. ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p, false);
  1739. if (uid != ResourceUID::INVALID_ID) {
  1740. s += " uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\"";
  1741. }
  1742. s += " path=\"" + p + "\" id=\"" + sorted_er[i].id + "\"]\n";
  1743. f->store_string(s); // Bundled.
  1744. }
  1745. if (external_resources.size()) {
  1746. f->store_line(String()); // Separate.
  1747. }
  1748. HashSet<String> used_unique_ids;
  1749. for (List<Ref<Resource>>::Element *E = saved_resources.front(); E; E = E->next()) {
  1750. Ref<Resource> res = E->get();
  1751. if (E->next() && res->is_built_in()) {
  1752. if (!res->get_scene_unique_id().is_empty()) {
  1753. if (used_unique_ids.has(res->get_scene_unique_id())) {
  1754. res->set_scene_unique_id(""); // Repeated.
  1755. } else {
  1756. used_unique_ids.insert(res->get_scene_unique_id());
  1757. }
  1758. }
  1759. }
  1760. }
  1761. for (List<Ref<Resource>>::Element *E = saved_resources.front(); E; E = E->next()) {
  1762. Ref<Resource> res = E->get();
  1763. ERR_CONTINUE(!resource_set.has(res));
  1764. bool main = (E->next() == nullptr);
  1765. if (main && packed_scene.is_valid()) {
  1766. break; // Save as a scene.
  1767. }
  1768. if (main) {
  1769. f->store_line("[resource]");
  1770. } else {
  1771. String line = "[sub_resource ";
  1772. if (res->get_scene_unique_id().is_empty()) {
  1773. String new_id;
  1774. while (true) {
  1775. new_id = _resource_get_class(res) + "_" + Resource::generate_scene_unique_id();
  1776. if (!used_unique_ids.has(new_id)) {
  1777. break;
  1778. }
  1779. }
  1780. res->set_scene_unique_id(new_id);
  1781. used_unique_ids.insert(new_id);
  1782. }
  1783. String id = res->get_scene_unique_id();
  1784. line += "type=\"" + _resource_get_class(res) + "\" id=\"" + id;
  1785. f->store_line(line + "\"]");
  1786. if (takeover_paths) {
  1787. res->set_path(p_path + "::" + id, true);
  1788. }
  1789. internal_resources[res] = id;
  1790. #ifdef TOOLS_ENABLED
  1791. res->set_edited(false);
  1792. #endif
  1793. }
  1794. Dictionary missing_resource_properties = p_resource->get_meta(META_MISSING_RESOURCES, Dictionary());
  1795. List<PropertyInfo> property_list;
  1796. res->get_property_list(&property_list);
  1797. for (List<PropertyInfo>::Element *PE = property_list.front(); PE; PE = PE->next()) {
  1798. if (skip_editor && PE->get().name.begins_with("__editor")) {
  1799. continue;
  1800. }
  1801. if (PE->get().name == META_PROPERTY_MISSING_RESOURCES) {
  1802. continue;
  1803. }
  1804. if (PE->get().usage & PROPERTY_USAGE_STORAGE) {
  1805. String name = PE->get().name;
  1806. Variant value;
  1807. if (PE->get().usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
  1808. NonPersistentKey npk;
  1809. npk.base = res;
  1810. npk.property = name;
  1811. if (non_persistent_map.has(npk)) {
  1812. value = non_persistent_map[npk];
  1813. }
  1814. } else {
  1815. value = res->get(name);
  1816. }
  1817. if (PE->get().type == Variant::OBJECT && missing_resource_properties.has(PE->get().name)) {
  1818. // Was this missing resource overridden? If so do not save the old value.
  1819. Ref<Resource> ures = value;
  1820. if (ures.is_null()) {
  1821. value = missing_resource_properties[PE->get().name];
  1822. }
  1823. }
  1824. Variant default_value = ClassDB::class_get_default_property_value(res->get_class(), name);
  1825. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {
  1826. continue;
  1827. }
  1828. if (PE->get().type == Variant::OBJECT && value.is_zero() && !(PE->get().usage & PROPERTY_USAGE_STORE_IF_NULL)) {
  1829. continue;
  1830. }
  1831. String vars;
  1832. VariantWriter::write_to_string(value, vars, _write_resources, this, use_compat);
  1833. f->store_string(name.property_name_encode() + " = " + vars + "\n");
  1834. }
  1835. }
  1836. if (E->next()) {
  1837. f->store_line(String());
  1838. }
  1839. }
  1840. if (packed_scene.is_valid()) {
  1841. // If this is a scene, save nodes and connections!
  1842. Ref<SceneState> state = packed_scene->get_state();
  1843. for (int i = 0; i < state->get_node_count(); i++) {
  1844. StringName type = state->get_node_type(i);
  1845. StringName name = state->get_node_name(i);
  1846. int index = state->get_node_index(i);
  1847. NodePath path = state->get_node_path(i, true);
  1848. NodePath owner = state->get_node_owner_path(i);
  1849. Ref<PackedScene> instance = state->get_node_instance(i);
  1850. String instance_placeholder = state->get_node_instance_placeholder(i);
  1851. Vector<StringName> groups = state->get_node_groups(i);
  1852. Vector<String> deferred_node_paths = state->get_node_deferred_nodepath_properties(i);
  1853. String header = "[node";
  1854. header += " name=\"" + String(name).c_escape() + "\"";
  1855. if (type != StringName()) {
  1856. header += " type=\"" + String(type) + "\"";
  1857. }
  1858. if (path != NodePath()) {
  1859. header += " parent=\"" + String(path.simplified()).c_escape() + "\"";
  1860. }
  1861. if (owner != NodePath() && owner != NodePath(".")) {
  1862. header += " owner=\"" + String(owner.simplified()).c_escape() + "\"";
  1863. }
  1864. if (index >= 0) {
  1865. header += " index=\"" + itos(index) + "\"";
  1866. }
  1867. if (deferred_node_paths.size()) {
  1868. header += " node_paths=" + Variant(deferred_node_paths).get_construct_string();
  1869. }
  1870. if (groups.size()) {
  1871. // Write all groups on the same line as they're part of a section header.
  1872. // This improves readability while not impacting VCS friendliness too much,
  1873. // since it's rare to have more than 5 groups assigned to a single node.
  1874. groups.sort_custom<StringName::AlphCompare>();
  1875. String sgroups = " groups=[";
  1876. for (int j = 0; j < groups.size(); j++) {
  1877. sgroups += "\"" + String(groups[j]).c_escape() + "\"";
  1878. if (j < groups.size() - 1) {
  1879. sgroups += ", ";
  1880. }
  1881. }
  1882. sgroups += "]";
  1883. header += sgroups;
  1884. }
  1885. f->store_string(header);
  1886. if (!instance_placeholder.is_empty()) {
  1887. String vars;
  1888. f->store_string(" instance_placeholder=");
  1889. VariantWriter::write_to_string(instance_placeholder, vars, _write_resources, this, use_compat);
  1890. f->store_string(vars);
  1891. }
  1892. if (instance.is_valid()) {
  1893. String vars;
  1894. f->store_string(" instance=");
  1895. VariantWriter::write_to_string(instance, vars, _write_resources, this, use_compat);
  1896. f->store_string(vars);
  1897. }
  1898. f->store_line("]");
  1899. for (int j = 0; j < state->get_node_property_count(i); j++) {
  1900. String vars;
  1901. VariantWriter::write_to_string(state->get_node_property_value(i, j), vars, _write_resources, this, use_compat);
  1902. f->store_string(String(state->get_node_property_name(i, j)).property_name_encode() + " = " + vars + "\n");
  1903. }
  1904. if (i < state->get_node_count() - 1) {
  1905. f->store_line(String());
  1906. }
  1907. }
  1908. for (int i = 0; i < state->get_connection_count(); i++) {
  1909. if (i == 0) {
  1910. f->store_line("");
  1911. }
  1912. String connstr = "[connection";
  1913. connstr += " signal=\"" + String(state->get_connection_signal(i)).c_escape() + "\"";
  1914. connstr += " from=\"" + String(state->get_connection_source(i).simplified()).c_escape() + "\"";
  1915. connstr += " to=\"" + String(state->get_connection_target(i).simplified()).c_escape() + "\"";
  1916. connstr += " method=\"" + String(state->get_connection_method(i)).c_escape() + "\"";
  1917. int flags = state->get_connection_flags(i);
  1918. if (flags != Object::CONNECT_PERSIST) {
  1919. connstr += " flags=" + itos(flags);
  1920. }
  1921. int unbinds = state->get_connection_unbinds(i);
  1922. if (unbinds > 0) {
  1923. connstr += " unbinds=" + itos(unbinds);
  1924. }
  1925. Array binds = state->get_connection_binds(i);
  1926. f->store_string(connstr);
  1927. if (binds.size()) {
  1928. String vars;
  1929. VariantWriter::write_to_string(binds, vars, _write_resources, this, use_compat);
  1930. f->store_string(" binds= " + vars);
  1931. }
  1932. f->store_line("]");
  1933. }
  1934. Vector<NodePath> editable_instances = state->get_editable_instances();
  1935. for (int i = 0; i < editable_instances.size(); i++) {
  1936. if (i == 0) {
  1937. f->store_line("");
  1938. }
  1939. f->store_line("[editable path=\"" + editable_instances[i].operator String().c_escape() + "\"]");
  1940. }
  1941. }
  1942. if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) {
  1943. return ERR_CANT_CREATE;
  1944. }
  1945. return OK;
  1946. }
  1947. Error ResourceLoaderText::set_uid(Ref<FileAccess> p_f, ResourceUID::ID p_uid) {
  1948. open(p_f, true);
  1949. ERR_FAIL_COND_V(error != OK, error);
  1950. ignore_resource_parsing = true;
  1951. Ref<FileAccess> fw;
  1952. fw = FileAccess::open(local_path + ".uidren", FileAccess::WRITE);
  1953. if (is_scene) {
  1954. fw->store_string("[gd_scene load_steps=" + itos(resources_total) + " format=" + itos(format_version) + " uid=\"" + ResourceUID::get_singleton()->id_to_text(p_uid) + "\"]");
  1955. } else {
  1956. String script_res_text;
  1957. if (!script_class.is_empty()) {
  1958. script_res_text = "script_class=\"" + script_class + "\" ";
  1959. }
  1960. fw->store_string("[gd_resource type=\"" + res_type + "\" " + script_res_text + "load_steps=" + itos(resources_total) + " format=" + itos(format_version) + " uid=\"" + ResourceUID::get_singleton()->id_to_text(p_uid) + "\"]");
  1961. }
  1962. uint8_t c = f->get_8();
  1963. while (!f->eof_reached()) {
  1964. fw->store_8(c);
  1965. c = f->get_8();
  1966. }
  1967. bool all_ok = fw->get_error() == OK;
  1968. if (!all_ok) {
  1969. return ERR_CANT_CREATE;
  1970. }
  1971. return OK;
  1972. }
  1973. Error ResourceFormatSaverText::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
  1974. if (p_path.ends_with(".tscn") && !Ref<PackedScene>(p_resource).is_valid()) {
  1975. return ERR_FILE_UNRECOGNIZED;
  1976. }
  1977. ResourceFormatSaverTextInstance saver;
  1978. return saver.save(p_path, p_resource, p_flags);
  1979. }
  1980. Error ResourceFormatSaverText::set_uid(const String &p_path, ResourceUID::ID p_uid) {
  1981. String lc = p_path.to_lower();
  1982. if (!lc.ends_with(".tscn") && !lc.ends_with(".tres")) {
  1983. return ERR_FILE_UNRECOGNIZED;
  1984. }
  1985. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1986. Error err = OK;
  1987. {
  1988. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ);
  1989. if (file.is_null()) {
  1990. ERR_FAIL_V(ERR_CANT_OPEN);
  1991. }
  1992. ResourceLoaderText loader;
  1993. loader.local_path = local_path;
  1994. loader.res_path = loader.local_path;
  1995. err = loader.set_uid(file, p_uid);
  1996. }
  1997. if (err == OK) {
  1998. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1999. da->remove(local_path);
  2000. da->rename(local_path + ".uidren", local_path);
  2001. }
  2002. return err;
  2003. }
  2004. bool ResourceFormatSaverText::recognize(const Ref<Resource> &p_resource) const {
  2005. return true; // All resources recognized!
  2006. }
  2007. void ResourceFormatSaverText::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
  2008. if (Ref<PackedScene>(p_resource).is_valid()) {
  2009. p_extensions->push_back("tscn"); // Text scene.
  2010. } else {
  2011. p_extensions->push_back("tres"); // Text resource.
  2012. }
  2013. }
  2014. ResourceFormatSaverText *ResourceFormatSaverText::singleton = nullptr;
  2015. ResourceFormatSaverText::ResourceFormatSaverText() {
  2016. singleton = this;
  2017. }