resource_format_text.cpp 67 KB

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