variant_parser.cpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  1. /**************************************************************************/
  2. /* variant_parser.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 "variant_parser.h"
  31. #include "core/crypto/crypto_core.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/object/script_language.h"
  34. #include "core/string/string_buffer.h"
  35. char32_t VariantParser::Stream::get_char() {
  36. // is within buffer?
  37. if (readahead_pointer < readahead_filled) {
  38. return readahead_buffer[readahead_pointer++];
  39. }
  40. // attempt to readahead
  41. readahead_filled = _read_buffer(readahead_buffer, readahead_enabled ? READAHEAD_SIZE : 1);
  42. if (readahead_filled) {
  43. readahead_pointer = 0;
  44. } else {
  45. // EOF
  46. readahead_pointer = 1;
  47. eof = true;
  48. return 0;
  49. }
  50. return get_char();
  51. }
  52. bool VariantParser::Stream::is_eof() const {
  53. if (readahead_enabled) {
  54. return eof;
  55. }
  56. return _is_eof();
  57. }
  58. bool VariantParser::StreamFile::is_utf8() const {
  59. return true;
  60. }
  61. bool VariantParser::StreamFile::_is_eof() const {
  62. return f->eof_reached();
  63. }
  64. uint32_t VariantParser::StreamFile::_read_buffer(char32_t *p_buffer, uint32_t p_num_chars) {
  65. // The buffer is assumed to include at least one character (for null terminator)
  66. ERR_FAIL_COND_V(!p_num_chars, 0);
  67. uint8_t *temp = (uint8_t *)alloca(p_num_chars);
  68. uint64_t num_read = f->get_buffer(temp, p_num_chars);
  69. ERR_FAIL_COND_V(num_read == UINT64_MAX, 0);
  70. // translate to wchar
  71. for (uint32_t n = 0; n < num_read; n++) {
  72. p_buffer[n] = temp[n];
  73. }
  74. // could be less than p_num_chars, or zero
  75. return num_read;
  76. }
  77. bool VariantParser::StreamString::is_utf8() const {
  78. return false;
  79. }
  80. bool VariantParser::StreamString::_is_eof() const {
  81. return pos > s.length();
  82. }
  83. uint32_t VariantParser::StreamString::_read_buffer(char32_t *p_buffer, uint32_t p_num_chars) {
  84. // The buffer is assumed to include at least one character (for null terminator)
  85. ERR_FAIL_COND_V(!p_num_chars, 0);
  86. ERR_FAIL_NULL_V(p_buffer, 0);
  87. int available = MAX(s.length() - pos, 0);
  88. if (available >= (int)p_num_chars) {
  89. const char32_t *src = s.ptr();
  90. src += pos;
  91. memcpy(p_buffer, src, p_num_chars * sizeof(char32_t));
  92. pos += p_num_chars;
  93. return p_num_chars;
  94. }
  95. // going to reach EOF
  96. if (available) {
  97. const char32_t *src = s.ptr();
  98. src += pos;
  99. memcpy(p_buffer, src, available * sizeof(char32_t));
  100. pos += available;
  101. }
  102. // add a zero
  103. p_buffer[available] = 0;
  104. return available;
  105. }
  106. /////////////////////////////////////////////////////////////////////////////////////////////////
  107. const char *VariantParser::tk_name[TK_MAX] = {
  108. "'{'",
  109. "'}'",
  110. "'['",
  111. "']'",
  112. "'('",
  113. "')'",
  114. "identifier",
  115. "string",
  116. "string_name",
  117. "number",
  118. "color",
  119. "':'",
  120. "','",
  121. "'.'",
  122. "'='",
  123. "EOF",
  124. "ERROR"
  125. };
  126. static double stor_fix(const String &p_str) {
  127. if (p_str == "inf") {
  128. return Math::INF;
  129. } else if (p_str == "-inf" || p_str == "inf_neg") {
  130. // inf_neg kept for compatibility.
  131. return -Math::INF;
  132. } else if (p_str == "nan") {
  133. return Math::NaN;
  134. }
  135. return -1;
  136. }
  137. Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str) {
  138. bool string_name = false;
  139. while (true) {
  140. char32_t cchar;
  141. if (p_stream->saved) {
  142. cchar = p_stream->saved;
  143. p_stream->saved = 0;
  144. } else {
  145. cchar = p_stream->get_char();
  146. if (p_stream->is_eof()) {
  147. r_token.type = TK_EOF;
  148. return OK;
  149. }
  150. }
  151. switch (cchar) {
  152. case '\n': {
  153. line++;
  154. break;
  155. }
  156. case 0: {
  157. r_token.type = TK_EOF;
  158. return OK;
  159. } break;
  160. case '{': {
  161. r_token.type = TK_CURLY_BRACKET_OPEN;
  162. return OK;
  163. }
  164. case '}': {
  165. r_token.type = TK_CURLY_BRACKET_CLOSE;
  166. return OK;
  167. }
  168. case '[': {
  169. r_token.type = TK_BRACKET_OPEN;
  170. return OK;
  171. }
  172. case ']': {
  173. r_token.type = TK_BRACKET_CLOSE;
  174. return OK;
  175. }
  176. case '(': {
  177. r_token.type = TK_PARENTHESIS_OPEN;
  178. return OK;
  179. }
  180. case ')': {
  181. r_token.type = TK_PARENTHESIS_CLOSE;
  182. return OK;
  183. }
  184. case ':': {
  185. r_token.type = TK_COLON;
  186. return OK;
  187. }
  188. case ';': {
  189. while (true) {
  190. char32_t ch = p_stream->get_char();
  191. if (p_stream->is_eof()) {
  192. r_token.type = TK_EOF;
  193. return OK;
  194. }
  195. if (ch == '\n') {
  196. line++;
  197. break;
  198. }
  199. }
  200. break;
  201. }
  202. case ',': {
  203. r_token.type = TK_COMMA;
  204. return OK;
  205. }
  206. case '.': {
  207. r_token.type = TK_PERIOD;
  208. return OK;
  209. }
  210. case '=': {
  211. r_token.type = TK_EQUAL;
  212. return OK;
  213. }
  214. case '#': {
  215. StringBuffer<> color_str;
  216. color_str += '#';
  217. while (true) {
  218. char32_t ch = p_stream->get_char();
  219. if (p_stream->is_eof()) {
  220. r_token.type = TK_EOF;
  221. return OK;
  222. } else if (is_hex_digit(ch)) {
  223. color_str += ch;
  224. } else {
  225. p_stream->saved = ch;
  226. break;
  227. }
  228. }
  229. r_token.value = Color::html(color_str.as_string());
  230. r_token.type = TK_COLOR;
  231. return OK;
  232. }
  233. #ifndef DISABLE_DEPRECATED
  234. case '@': // Compatibility with 3.x StringNames.
  235. #endif
  236. case '&': { // StringName.
  237. cchar = p_stream->get_char();
  238. if (cchar != '"') {
  239. r_err_str = "Expected '\"' after '&'";
  240. r_token.type = TK_ERROR;
  241. return ERR_PARSE_ERROR;
  242. }
  243. string_name = true;
  244. [[fallthrough]];
  245. }
  246. case '"': {
  247. String str;
  248. char32_t prev = 0;
  249. while (true) {
  250. char32_t ch = p_stream->get_char();
  251. if (ch == 0) {
  252. r_err_str = "Unterminated string";
  253. r_token.type = TK_ERROR;
  254. return ERR_PARSE_ERROR;
  255. } else if (ch == '"') {
  256. break;
  257. } else if (ch == '\\') {
  258. //escaped characters...
  259. char32_t next = p_stream->get_char();
  260. if (next == 0) {
  261. r_err_str = "Unterminated string";
  262. r_token.type = TK_ERROR;
  263. return ERR_PARSE_ERROR;
  264. }
  265. char32_t res = 0;
  266. switch (next) {
  267. case 'b':
  268. res = 8;
  269. break;
  270. case 't':
  271. res = 9;
  272. break;
  273. case 'n':
  274. res = 10;
  275. break;
  276. case 'f':
  277. res = 12;
  278. break;
  279. case 'r':
  280. res = 13;
  281. break;
  282. case 'U':
  283. case 'u': {
  284. // Hexadecimal sequence.
  285. int hex_len = (next == 'U') ? 6 : 4;
  286. for (int j = 0; j < hex_len; j++) {
  287. char32_t c = p_stream->get_char();
  288. if (c == 0) {
  289. r_err_str = "Unterminated string";
  290. r_token.type = TK_ERROR;
  291. return ERR_PARSE_ERROR;
  292. }
  293. if (!is_hex_digit(c)) {
  294. r_err_str = "Malformed hex constant in string";
  295. r_token.type = TK_ERROR;
  296. return ERR_PARSE_ERROR;
  297. }
  298. char32_t v;
  299. if (is_digit(c)) {
  300. v = c - '0';
  301. } else if (c >= 'a' && c <= 'f') {
  302. v = c - 'a';
  303. v += 10;
  304. } else if (c >= 'A' && c <= 'F') {
  305. v = c - 'A';
  306. v += 10;
  307. } else {
  308. ERR_PRINT("Bug parsing hex constant.");
  309. v = 0;
  310. }
  311. res <<= 4;
  312. res |= v;
  313. }
  314. } break;
  315. default: {
  316. res = next;
  317. } break;
  318. }
  319. // Parse UTF-16 pair.
  320. if ((res & 0xfffffc00) == 0xd800) {
  321. if (prev == 0) {
  322. prev = res;
  323. continue;
  324. } else {
  325. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  326. r_token.type = TK_ERROR;
  327. return ERR_PARSE_ERROR;
  328. }
  329. } else if ((res & 0xfffffc00) == 0xdc00) {
  330. if (prev == 0) {
  331. r_err_str = "Invalid UTF-16 sequence in string, unpaired trail surrogate";
  332. r_token.type = TK_ERROR;
  333. return ERR_PARSE_ERROR;
  334. } else {
  335. res = (prev << 10UL) + res - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  336. prev = 0;
  337. }
  338. }
  339. if (prev != 0) {
  340. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  341. r_token.type = TK_ERROR;
  342. return ERR_PARSE_ERROR;
  343. }
  344. str += res;
  345. } else {
  346. if (prev != 0) {
  347. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  348. r_token.type = TK_ERROR;
  349. return ERR_PARSE_ERROR;
  350. }
  351. if (ch == '\n') {
  352. line++;
  353. }
  354. str += ch;
  355. }
  356. }
  357. if (prev != 0) {
  358. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  359. r_token.type = TK_ERROR;
  360. return ERR_PARSE_ERROR;
  361. }
  362. if (p_stream->is_utf8()) {
  363. // Re-interpret the string we built as ascii.
  364. CharString string_as_ascii = str.ascii(true);
  365. str.clear();
  366. str.append_utf8(string_as_ascii);
  367. }
  368. if (string_name) {
  369. r_token.type = TK_STRING_NAME;
  370. r_token.value = StringName(str);
  371. } else {
  372. r_token.type = TK_STRING;
  373. r_token.value = str;
  374. }
  375. return OK;
  376. } break;
  377. default: {
  378. if (cchar <= 32) {
  379. break;
  380. }
  381. StringBuffer<> token_text;
  382. if (cchar == '-') {
  383. token_text += '-';
  384. cchar = p_stream->get_char();
  385. }
  386. if (cchar >= '0' && cchar <= '9') {
  387. //a number
  388. #define READING_SIGN 0
  389. #define READING_INT 1
  390. #define READING_DEC 2
  391. #define READING_EXP 3
  392. #define READING_DONE 4
  393. int reading = READING_INT;
  394. char32_t c = cchar;
  395. bool exp_sign = false;
  396. bool exp_beg = false;
  397. bool is_float = false;
  398. while (true) {
  399. switch (reading) {
  400. case READING_INT: {
  401. if (is_digit(c)) {
  402. //pass
  403. } else if (c == '.') {
  404. reading = READING_DEC;
  405. is_float = true;
  406. } else if (c == 'e' || c == 'E') {
  407. reading = READING_EXP;
  408. is_float = true;
  409. } else {
  410. reading = READING_DONE;
  411. }
  412. } break;
  413. case READING_DEC: {
  414. if (is_digit(c)) {
  415. } else if (c == 'e' || c == 'E') {
  416. reading = READING_EXP;
  417. } else {
  418. reading = READING_DONE;
  419. }
  420. } break;
  421. case READING_EXP: {
  422. if (is_digit(c)) {
  423. exp_beg = true;
  424. } else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) {
  425. exp_sign = true;
  426. } else {
  427. reading = READING_DONE;
  428. }
  429. } break;
  430. }
  431. if (reading == READING_DONE) {
  432. break;
  433. }
  434. token_text += c;
  435. c = p_stream->get_char();
  436. }
  437. p_stream->saved = c;
  438. r_token.type = TK_NUMBER;
  439. if (is_float) {
  440. r_token.value = token_text.as_double();
  441. } else {
  442. r_token.value = token_text.as_int();
  443. }
  444. return OK;
  445. } else if (is_ascii_alphabet_char(cchar) || is_underscore(cchar)) {
  446. bool first = true;
  447. while (is_ascii_alphabet_char(cchar) || is_underscore(cchar) || (!first && is_digit(cchar))) {
  448. token_text += cchar;
  449. cchar = p_stream->get_char();
  450. first = false;
  451. }
  452. p_stream->saved = cchar;
  453. r_token.type = TK_IDENTIFIER;
  454. r_token.value = token_text.as_string();
  455. return OK;
  456. } else {
  457. r_err_str = "Unexpected character";
  458. r_token.type = TK_ERROR;
  459. return ERR_PARSE_ERROR;
  460. }
  461. }
  462. }
  463. }
  464. r_err_str = "Unknown error getting token";
  465. r_token.type = TK_ERROR;
  466. return ERR_PARSE_ERROR;
  467. }
  468. Error VariantParser::_parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str) {
  469. Token token;
  470. get_token(p_stream, token, line, r_err_str);
  471. if (token.type != TK_PARENTHESIS_OPEN) {
  472. r_err_str = "Expected '(' in old-style project.godot construct";
  473. return ERR_PARSE_ERROR;
  474. }
  475. String accum;
  476. while (true) {
  477. char32_t c = p_stream->get_char();
  478. if (p_stream->is_eof()) {
  479. r_err_str = "Unexpected EOF while parsing old-style project.godot construct";
  480. return ERR_PARSE_ERROR;
  481. }
  482. if (c == ',') {
  483. strings.push_back(accum.strip_edges());
  484. accum = String();
  485. } else if (c == ')') {
  486. strings.push_back(accum.strip_edges());
  487. return OK;
  488. } else if (c == '\n') {
  489. line++;
  490. }
  491. }
  492. }
  493. template <typename T>
  494. Error VariantParser::_parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str) {
  495. Token token;
  496. get_token(p_stream, token, line, r_err_str);
  497. if (token.type != TK_PARENTHESIS_OPEN) {
  498. r_err_str = "Expected '(' in constructor";
  499. return ERR_PARSE_ERROR;
  500. }
  501. bool first = true;
  502. while (true) {
  503. if (!first) {
  504. get_token(p_stream, token, line, r_err_str);
  505. if (token.type == TK_COMMA) {
  506. //do none
  507. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  508. break;
  509. } else {
  510. r_err_str = "Expected ',' or ')' in constructor";
  511. return ERR_PARSE_ERROR;
  512. }
  513. }
  514. get_token(p_stream, token, line, r_err_str);
  515. if (first && token.type == TK_PARENTHESIS_CLOSE) {
  516. break;
  517. } else if (token.type != TK_NUMBER) {
  518. bool valid = false;
  519. if (token.type == TK_IDENTIFIER) {
  520. double real = stor_fix(token.value);
  521. if (real != -1) {
  522. token.type = TK_NUMBER;
  523. token.value = real;
  524. valid = true;
  525. }
  526. }
  527. if (!valid) {
  528. r_err_str = "Expected float in constructor";
  529. return ERR_PARSE_ERROR;
  530. }
  531. }
  532. r_construct.push_back(token.value);
  533. first = false;
  534. }
  535. return OK;
  536. }
  537. Error VariantParser::_parse_byte_array(Stream *p_stream, Vector<uint8_t> &r_construct, int &line, String &r_err_str) {
  538. Token token;
  539. get_token(p_stream, token, line, r_err_str);
  540. if (token.type != TK_PARENTHESIS_OPEN) {
  541. r_err_str = "Expected '(' in constructor";
  542. return ERR_PARSE_ERROR;
  543. }
  544. get_token(p_stream, token, line, r_err_str);
  545. if (token.type == TK_STRING) {
  546. // Base64 encoded array.
  547. String base64_encoded_string = token.value;
  548. int strlen = base64_encoded_string.length();
  549. CharString cstr = base64_encoded_string.ascii();
  550. size_t arr_len = 0;
  551. r_construct.resize(strlen / 4 * 3 + 1);
  552. uint8_t *w = r_construct.ptrw();
  553. Error err = CryptoCore::b64_decode(&w[0], r_construct.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen);
  554. if (err) {
  555. r_err_str = "Invalid base64-encoded string";
  556. return ERR_PARSE_ERROR;
  557. }
  558. r_construct.resize(arr_len);
  559. get_token(p_stream, token, line, r_err_str);
  560. if (token.type != TK_PARENTHESIS_CLOSE) {
  561. r_err_str = "Expected ')' in constructor";
  562. return ERR_PARSE_ERROR;
  563. }
  564. } else if (token.type == TK_NUMBER || token.type == TK_IDENTIFIER) {
  565. // Individual elements.
  566. while (true) {
  567. if (token.type != TK_NUMBER) {
  568. bool valid = false;
  569. if (token.type == TK_IDENTIFIER) {
  570. double real = stor_fix(token.value);
  571. if (real != -1) {
  572. token.type = TK_NUMBER;
  573. token.value = real;
  574. valid = true;
  575. }
  576. }
  577. if (!valid) {
  578. r_err_str = "Expected number in constructor";
  579. return ERR_PARSE_ERROR;
  580. }
  581. }
  582. r_construct.push_back(token.value);
  583. get_token(p_stream, token, line, r_err_str);
  584. if (token.type == TK_COMMA) {
  585. //do none
  586. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  587. break;
  588. } else {
  589. r_err_str = "Expected ',' or ')' in constructor";
  590. return ERR_PARSE_ERROR;
  591. }
  592. get_token(p_stream, token, line, r_err_str);
  593. }
  594. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  595. // Empty array.
  596. return OK;
  597. } else {
  598. r_err_str = "Expected base64 string, or list of numbers in constructor";
  599. return ERR_PARSE_ERROR;
  600. }
  601. return OK;
  602. }
  603. Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  604. if (token.type == TK_CURLY_BRACKET_OPEN) {
  605. Dictionary d;
  606. Error err = _parse_dictionary(d, p_stream, line, r_err_str, p_res_parser);
  607. if (err) {
  608. return err;
  609. }
  610. value = d;
  611. return OK;
  612. } else if (token.type == TK_BRACKET_OPEN) {
  613. Array a;
  614. Error err = _parse_array(a, p_stream, line, r_err_str, p_res_parser);
  615. if (err) {
  616. return err;
  617. }
  618. value = a;
  619. return OK;
  620. } else if (token.type == TK_IDENTIFIER) {
  621. String id = token.value;
  622. if (id == "true") {
  623. value = true;
  624. } else if (id == "false") {
  625. value = false;
  626. } else if (id == "null" || id == "nil") {
  627. value = Variant();
  628. } else if (id == "inf") {
  629. value = Math::INF;
  630. } else if (id == "-inf" || id == "inf_neg") {
  631. // inf_neg kept for compatibility.
  632. value = -Math::INF;
  633. } else if (id == "nan") {
  634. value = Math::NaN;
  635. } else if (id == "Vector2") {
  636. Vector<real_t> args;
  637. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  638. if (err) {
  639. return err;
  640. }
  641. if (args.size() != 2) {
  642. r_err_str = "Expected 2 arguments for constructor";
  643. return ERR_PARSE_ERROR;
  644. }
  645. value = Vector2(args[0], args[1]);
  646. } else if (id == "Vector2i") {
  647. Vector<int32_t> args;
  648. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  649. if (err) {
  650. return err;
  651. }
  652. if (args.size() != 2) {
  653. r_err_str = "Expected 2 arguments for constructor";
  654. return ERR_PARSE_ERROR;
  655. }
  656. value = Vector2i(args[0], args[1]);
  657. } else if (id == "Rect2") {
  658. Vector<real_t> args;
  659. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  660. if (err) {
  661. return err;
  662. }
  663. if (args.size() != 4) {
  664. r_err_str = "Expected 4 arguments for constructor";
  665. return ERR_PARSE_ERROR;
  666. }
  667. value = Rect2(args[0], args[1], args[2], args[3]);
  668. } else if (id == "Rect2i") {
  669. Vector<int32_t> args;
  670. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  671. if (err) {
  672. return err;
  673. }
  674. if (args.size() != 4) {
  675. r_err_str = "Expected 4 arguments for constructor";
  676. return ERR_PARSE_ERROR;
  677. }
  678. value = Rect2i(args[0], args[1], args[2], args[3]);
  679. } else if (id == "Vector3") {
  680. Vector<real_t> args;
  681. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  682. if (err) {
  683. return err;
  684. }
  685. if (args.size() != 3) {
  686. r_err_str = "Expected 3 arguments for constructor";
  687. return ERR_PARSE_ERROR;
  688. }
  689. value = Vector3(args[0], args[1], args[2]);
  690. } else if (id == "Vector3i") {
  691. Vector<int32_t> args;
  692. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  693. if (err) {
  694. return err;
  695. }
  696. if (args.size() != 3) {
  697. r_err_str = "Expected 3 arguments for constructor";
  698. return ERR_PARSE_ERROR;
  699. }
  700. value = Vector3i(args[0], args[1], args[2]);
  701. } else if (id == "Vector4") {
  702. Vector<real_t> args;
  703. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  704. if (err) {
  705. return err;
  706. }
  707. if (args.size() != 4) {
  708. r_err_str = "Expected 4 arguments for constructor";
  709. return ERR_PARSE_ERROR;
  710. }
  711. value = Vector4(args[0], args[1], args[2], args[3]);
  712. } else if (id == "Vector4i") {
  713. Vector<int32_t> args;
  714. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  715. if (err) {
  716. return err;
  717. }
  718. if (args.size() != 4) {
  719. r_err_str = "Expected 4 arguments for constructor";
  720. return ERR_PARSE_ERROR;
  721. }
  722. value = Vector4i(args[0], args[1], args[2], args[3]);
  723. } else if (id == "Transform2D" || id == "Matrix32") { //compatibility
  724. Vector<real_t> args;
  725. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  726. if (err) {
  727. return err;
  728. }
  729. if (args.size() != 6) {
  730. r_err_str = "Expected 6 arguments for constructor";
  731. return ERR_PARSE_ERROR;
  732. }
  733. Transform2D m;
  734. m[0] = Vector2(args[0], args[1]);
  735. m[1] = Vector2(args[2], args[3]);
  736. m[2] = Vector2(args[4], args[5]);
  737. value = m;
  738. } else if (id == "Plane") {
  739. Vector<real_t> args;
  740. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  741. if (err) {
  742. return err;
  743. }
  744. if (args.size() != 4) {
  745. r_err_str = "Expected 4 arguments for constructor";
  746. return ERR_PARSE_ERROR;
  747. }
  748. value = Plane(args[0], args[1], args[2], args[3]);
  749. } else if (id == "Quaternion" || id == "Quat") { // "Quat" kept for compatibility
  750. Vector<real_t> args;
  751. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  752. if (err) {
  753. return err;
  754. }
  755. if (args.size() != 4) {
  756. r_err_str = "Expected 4 arguments for constructor";
  757. return ERR_PARSE_ERROR;
  758. }
  759. value = Quaternion(args[0], args[1], args[2], args[3]);
  760. } else if (id == "AABB" || id == "Rect3") {
  761. Vector<real_t> args;
  762. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  763. if (err) {
  764. return err;
  765. }
  766. if (args.size() != 6) {
  767. r_err_str = "Expected 6 arguments for constructor";
  768. return ERR_PARSE_ERROR;
  769. }
  770. value = AABB(Vector3(args[0], args[1], args[2]), Vector3(args[3], args[4], args[5]));
  771. } else if (id == "Basis" || id == "Matrix3") { //compatibility
  772. Vector<real_t> args;
  773. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  774. if (err) {
  775. return err;
  776. }
  777. if (args.size() != 9) {
  778. r_err_str = "Expected 9 arguments for constructor";
  779. return ERR_PARSE_ERROR;
  780. }
  781. value = Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
  782. } else if (id == "Transform3D" || id == "Transform") { // "Transform" kept for compatibility with Godot <4.
  783. Vector<real_t> args;
  784. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  785. if (err) {
  786. return err;
  787. }
  788. if (args.size() != 12) {
  789. r_err_str = "Expected 12 arguments for constructor";
  790. return ERR_PARSE_ERROR;
  791. }
  792. value = Transform3D(Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]), Vector3(args[9], args[10], args[11]));
  793. } else if (id == "Projection") { // "Transform" kept for compatibility with Godot <4.
  794. Vector<real_t> args;
  795. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  796. if (err) {
  797. return err;
  798. }
  799. if (args.size() != 16) {
  800. r_err_str = "Expected 16 arguments for constructor";
  801. return ERR_PARSE_ERROR;
  802. }
  803. value = Projection(Vector4(args[0], args[1], args[2], args[3]), Vector4(args[4], args[5], args[6], args[7]), Vector4(args[8], args[9], args[10], args[11]), Vector4(args[12], args[13], args[14], args[15]));
  804. } else if (id == "Color") {
  805. Vector<float> args;
  806. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  807. if (err) {
  808. return err;
  809. }
  810. if (args.size() != 4) {
  811. r_err_str = "Expected 4 arguments for constructor";
  812. return ERR_PARSE_ERROR;
  813. }
  814. value = Color(args[0], args[1], args[2], args[3]);
  815. } else if (id == "NodePath") {
  816. get_token(p_stream, token, line, r_err_str);
  817. if (token.type != TK_PARENTHESIS_OPEN) {
  818. r_err_str = "Expected '('";
  819. return ERR_PARSE_ERROR;
  820. }
  821. get_token(p_stream, token, line, r_err_str);
  822. if (token.type != TK_STRING) {
  823. r_err_str = "Expected string as argument for NodePath()";
  824. return ERR_PARSE_ERROR;
  825. }
  826. value = NodePath(String(token.value));
  827. get_token(p_stream, token, line, r_err_str);
  828. if (token.type != TK_PARENTHESIS_CLOSE) {
  829. r_err_str = "Expected ')'";
  830. return ERR_PARSE_ERROR;
  831. }
  832. } else if (id == "RID") {
  833. get_token(p_stream, token, line, r_err_str);
  834. if (token.type != TK_PARENTHESIS_OPEN) {
  835. r_err_str = "Expected '('";
  836. return ERR_PARSE_ERROR;
  837. }
  838. get_token(p_stream, token, line, r_err_str);
  839. // Permit empty RID.
  840. if (token.type == TK_PARENTHESIS_CLOSE) {
  841. value = RID();
  842. return OK;
  843. } else if (token.type != TK_NUMBER) {
  844. r_err_str = "Expected number as argument or ')'";
  845. return ERR_PARSE_ERROR;
  846. }
  847. value = RID::from_uint64(token.value);
  848. get_token(p_stream, token, line, r_err_str);
  849. if (token.type != TK_PARENTHESIS_CLOSE) {
  850. r_err_str = "Expected ')'";
  851. return ERR_PARSE_ERROR;
  852. }
  853. } else if (id == "Signal") {
  854. get_token(p_stream, token, line, r_err_str);
  855. if (token.type != TK_PARENTHESIS_OPEN) {
  856. r_err_str = "Expected '('";
  857. return ERR_PARSE_ERROR;
  858. }
  859. // Load as empty.
  860. value = Signal();
  861. get_token(p_stream, token, line, r_err_str);
  862. if (token.type != TK_PARENTHESIS_CLOSE) {
  863. r_err_str = "Expected ')'";
  864. return ERR_PARSE_ERROR;
  865. }
  866. } else if (id == "Callable") {
  867. get_token(p_stream, token, line, r_err_str);
  868. if (token.type != TK_PARENTHESIS_OPEN) {
  869. r_err_str = "Expected '('";
  870. return ERR_PARSE_ERROR;
  871. }
  872. // Load as empty.
  873. value = Callable();
  874. get_token(p_stream, token, line, r_err_str);
  875. if (token.type != TK_PARENTHESIS_CLOSE) {
  876. r_err_str = "Expected ')'";
  877. return ERR_PARSE_ERROR;
  878. }
  879. } else if (id == "Object") {
  880. get_token(p_stream, token, line, r_err_str);
  881. if (token.type != TK_PARENTHESIS_OPEN) {
  882. r_err_str = "Expected '('";
  883. return ERR_PARSE_ERROR;
  884. }
  885. get_token(p_stream, token, line, r_err_str);
  886. if (token.type != TK_IDENTIFIER) {
  887. r_err_str = "Expected identifier with type of object";
  888. return ERR_PARSE_ERROR;
  889. }
  890. String type = token.value;
  891. Object *obj = ClassDB::instantiate(type);
  892. if (!obj) {
  893. r_err_str = vformat("Can't instantiate Object() of type '%s'", type);
  894. return ERR_PARSE_ERROR;
  895. }
  896. Ref<RefCounted> ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
  897. get_token(p_stream, token, line, r_err_str);
  898. if (token.type != TK_COMMA) {
  899. r_err_str = "Expected ',' after object type";
  900. return ERR_PARSE_ERROR;
  901. }
  902. bool at_key = true;
  903. String key;
  904. bool need_comma = false;
  905. while (true) {
  906. if (p_stream->is_eof()) {
  907. r_err_str = "Unexpected EOF while parsing Object()";
  908. return ERR_FILE_CORRUPT;
  909. }
  910. if (at_key) {
  911. Error err = get_token(p_stream, token, line, r_err_str);
  912. if (err != OK) {
  913. return err;
  914. }
  915. if (token.type == TK_PARENTHESIS_CLOSE) {
  916. value = ref.is_valid() ? Variant(ref) : Variant(obj);
  917. return OK;
  918. }
  919. if (need_comma) {
  920. if (token.type != TK_COMMA) {
  921. r_err_str = "Expected '}' or ','";
  922. return ERR_PARSE_ERROR;
  923. } else {
  924. need_comma = false;
  925. continue;
  926. }
  927. }
  928. if (token.type != TK_STRING) {
  929. r_err_str = "Expected property name as string";
  930. return ERR_PARSE_ERROR;
  931. }
  932. key = token.value;
  933. err = get_token(p_stream, token, line, r_err_str);
  934. if (err != OK) {
  935. return err;
  936. }
  937. if (token.type != TK_COLON) {
  938. r_err_str = "Expected ':'";
  939. return ERR_PARSE_ERROR;
  940. }
  941. at_key = false;
  942. } else {
  943. Error err = get_token(p_stream, token, line, r_err_str);
  944. if (err != OK) {
  945. return err;
  946. }
  947. Variant v;
  948. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  949. if (err) {
  950. return err;
  951. }
  952. obj->set(key, v);
  953. need_comma = true;
  954. at_key = true;
  955. }
  956. }
  957. } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") {
  958. get_token(p_stream, token, line, r_err_str);
  959. if (token.type != TK_PARENTHESIS_OPEN) {
  960. r_err_str = "Expected '('";
  961. return ERR_PARSE_ERROR;
  962. }
  963. if (p_res_parser && id == "Resource" && p_res_parser->func) {
  964. Ref<Resource> res;
  965. Error err = p_res_parser->func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  966. if (err) {
  967. return err;
  968. }
  969. value = res;
  970. } else if (p_res_parser && id == "ExtResource" && p_res_parser->ext_func) {
  971. Ref<Resource> res;
  972. Error err = p_res_parser->ext_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  973. if (err) {
  974. // If the file is missing, the error can be ignored.
  975. if (err != ERR_FILE_NOT_FOUND && err != ERR_CANT_OPEN && err != ERR_FILE_CANT_OPEN) {
  976. return err;
  977. }
  978. }
  979. value = res;
  980. } else if (p_res_parser && id == "SubResource" && p_res_parser->sub_func) {
  981. Ref<Resource> res;
  982. Error err = p_res_parser->sub_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  983. if (err) {
  984. return err;
  985. }
  986. value = res;
  987. } else {
  988. get_token(p_stream, token, line, r_err_str);
  989. if (token.type == TK_STRING) {
  990. String path = token.value;
  991. Ref<Resource> res = ResourceLoader::load(path);
  992. if (res.is_null()) {
  993. r_err_str = "Can't load resource at path: " + path;
  994. return ERR_PARSE_ERROR;
  995. }
  996. get_token(p_stream, token, line, r_err_str);
  997. if (token.type != TK_PARENTHESIS_CLOSE) {
  998. r_err_str = "Expected ')'";
  999. return ERR_PARSE_ERROR;
  1000. }
  1001. value = res;
  1002. } else {
  1003. r_err_str = "Expected string as argument for Resource()";
  1004. return ERR_PARSE_ERROR;
  1005. }
  1006. }
  1007. } else if (id == "Dictionary") {
  1008. Error err = OK;
  1009. get_token(p_stream, token, line, r_err_str);
  1010. if (token.type != TK_BRACKET_OPEN) {
  1011. r_err_str = "Expected '['";
  1012. return ERR_PARSE_ERROR;
  1013. }
  1014. get_token(p_stream, token, line, r_err_str);
  1015. if (token.type != TK_IDENTIFIER) {
  1016. r_err_str = "Expected type identifier for key";
  1017. return ERR_PARSE_ERROR;
  1018. }
  1019. static HashMap<StringName, Variant::Type> builtin_types;
  1020. if (builtin_types.is_empty()) {
  1021. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  1022. builtin_types[Variant::get_type_name((Variant::Type)i)] = (Variant::Type)i;
  1023. }
  1024. }
  1025. Dictionary dict;
  1026. Variant::Type key_type = Variant::NIL;
  1027. StringName key_class_name;
  1028. Variant key_script;
  1029. bool got_comma_token = false;
  1030. if (builtin_types.has(token.value)) {
  1031. key_type = builtin_types.get(token.value);
  1032. } else if (token.value == "Resource" || token.value == "SubResource" || token.value == "ExtResource") {
  1033. Variant resource;
  1034. err = parse_value(token, resource, p_stream, line, r_err_str, p_res_parser);
  1035. if (err) {
  1036. if (token.value == "Resource" && err == ERR_PARSE_ERROR && r_err_str == "Expected '('" && token.type == TK_COMMA) {
  1037. err = OK;
  1038. r_err_str = String();
  1039. key_type = Variant::OBJECT;
  1040. key_class_name = token.value;
  1041. got_comma_token = true;
  1042. } else {
  1043. return err;
  1044. }
  1045. } else {
  1046. Ref<Script> script = resource;
  1047. if (script.is_valid() && script->is_valid()) {
  1048. key_type = Variant::OBJECT;
  1049. key_class_name = script->get_instance_base_type();
  1050. key_script = script;
  1051. }
  1052. }
  1053. } else if (ClassDB::class_exists(token.value)) {
  1054. key_type = Variant::OBJECT;
  1055. key_class_name = token.value;
  1056. }
  1057. if (!got_comma_token) {
  1058. get_token(p_stream, token, line, r_err_str);
  1059. if (token.type != TK_COMMA) {
  1060. r_err_str = "Expected ',' after key type";
  1061. return ERR_PARSE_ERROR;
  1062. }
  1063. }
  1064. get_token(p_stream, token, line, r_err_str);
  1065. if (token.type != TK_IDENTIFIER) {
  1066. r_err_str = "Expected type identifier for value";
  1067. return ERR_PARSE_ERROR;
  1068. }
  1069. Variant::Type value_type = Variant::NIL;
  1070. StringName value_class_name;
  1071. Variant value_script;
  1072. bool got_bracket_token = false;
  1073. if (builtin_types.has(token.value)) {
  1074. value_type = builtin_types.get(token.value);
  1075. } else if (token.value == "Resource" || token.value == "SubResource" || token.value == "ExtResource") {
  1076. Variant resource;
  1077. err = parse_value(token, resource, p_stream, line, r_err_str, p_res_parser);
  1078. if (err) {
  1079. if (token.value == "Resource" && err == ERR_PARSE_ERROR && r_err_str == "Expected '('" && token.type == TK_BRACKET_CLOSE) {
  1080. err = OK;
  1081. r_err_str = String();
  1082. value_type = Variant::OBJECT;
  1083. value_class_name = token.value;
  1084. got_bracket_token = true;
  1085. } else {
  1086. return err;
  1087. }
  1088. } else {
  1089. Ref<Script> script = resource;
  1090. if (script.is_valid() && script->is_valid()) {
  1091. value_type = Variant::OBJECT;
  1092. value_class_name = script->get_instance_base_type();
  1093. value_script = script;
  1094. }
  1095. }
  1096. } else if (ClassDB::class_exists(token.value)) {
  1097. value_type = Variant::OBJECT;
  1098. value_class_name = token.value;
  1099. }
  1100. if (key_type != Variant::NIL || value_type != Variant::NIL) {
  1101. dict.set_typed(key_type, key_class_name, key_script, value_type, value_class_name, value_script);
  1102. }
  1103. if (!got_bracket_token) {
  1104. get_token(p_stream, token, line, r_err_str);
  1105. if (token.type != TK_BRACKET_CLOSE) {
  1106. r_err_str = "Expected ']'";
  1107. return ERR_PARSE_ERROR;
  1108. }
  1109. }
  1110. get_token(p_stream, token, line, r_err_str);
  1111. if (token.type != TK_PARENTHESIS_OPEN) {
  1112. r_err_str = "Expected '('";
  1113. return ERR_PARSE_ERROR;
  1114. }
  1115. get_token(p_stream, token, line, r_err_str);
  1116. if (token.type != TK_CURLY_BRACKET_OPEN) {
  1117. r_err_str = "Expected '{'";
  1118. return ERR_PARSE_ERROR;
  1119. }
  1120. Dictionary values;
  1121. err = _parse_dictionary(values, p_stream, line, r_err_str, p_res_parser);
  1122. if (err) {
  1123. return err;
  1124. }
  1125. get_token(p_stream, token, line, r_err_str);
  1126. if (token.type != TK_PARENTHESIS_CLOSE) {
  1127. r_err_str = "Expected ')'";
  1128. return ERR_PARSE_ERROR;
  1129. }
  1130. dict.assign(values);
  1131. value = dict;
  1132. } else if (id == "Array") {
  1133. Error err = OK;
  1134. get_token(p_stream, token, line, r_err_str);
  1135. if (token.type != TK_BRACKET_OPEN) {
  1136. r_err_str = "Expected '['";
  1137. return ERR_PARSE_ERROR;
  1138. }
  1139. get_token(p_stream, token, line, r_err_str);
  1140. if (token.type != TK_IDENTIFIER) {
  1141. r_err_str = "Expected type identifier";
  1142. return ERR_PARSE_ERROR;
  1143. }
  1144. static HashMap<String, Variant::Type> builtin_types;
  1145. if (builtin_types.is_empty()) {
  1146. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  1147. builtin_types[Variant::get_type_name((Variant::Type)i)] = (Variant::Type)i;
  1148. }
  1149. }
  1150. Array array = Array();
  1151. bool got_bracket_token = false;
  1152. if (builtin_types.has(token.value)) {
  1153. array.set_typed(builtin_types.get(token.value), StringName(), Variant());
  1154. } else if (token.value == "Resource" || token.value == "SubResource" || token.value == "ExtResource") {
  1155. Variant resource;
  1156. err = parse_value(token, resource, p_stream, line, r_err_str, p_res_parser);
  1157. if (err) {
  1158. if (token.value == "Resource" && err == ERR_PARSE_ERROR && r_err_str == "Expected '('" && token.type == TK_BRACKET_CLOSE) {
  1159. err = OK;
  1160. r_err_str = String();
  1161. array.set_typed(Variant::OBJECT, token.value, Variant());
  1162. got_bracket_token = true;
  1163. } else {
  1164. return err;
  1165. }
  1166. } else {
  1167. Ref<Script> script = resource;
  1168. if (script.is_valid() && script->is_valid()) {
  1169. array.set_typed(Variant::OBJECT, script->get_instance_base_type(), script);
  1170. }
  1171. }
  1172. } else if (ClassDB::class_exists(token.value)) {
  1173. array.set_typed(Variant::OBJECT, token.value, Variant());
  1174. }
  1175. if (!got_bracket_token) {
  1176. get_token(p_stream, token, line, r_err_str);
  1177. if (token.type != TK_BRACKET_CLOSE) {
  1178. r_err_str = "Expected ']'";
  1179. return ERR_PARSE_ERROR;
  1180. }
  1181. }
  1182. get_token(p_stream, token, line, r_err_str);
  1183. if (token.type != TK_PARENTHESIS_OPEN) {
  1184. r_err_str = "Expected '('";
  1185. return ERR_PARSE_ERROR;
  1186. }
  1187. get_token(p_stream, token, line, r_err_str);
  1188. if (token.type != TK_BRACKET_OPEN) {
  1189. r_err_str = "Expected '['";
  1190. return ERR_PARSE_ERROR;
  1191. }
  1192. Array values;
  1193. err = _parse_array(values, p_stream, line, r_err_str, p_res_parser);
  1194. if (err) {
  1195. return err;
  1196. }
  1197. get_token(p_stream, token, line, r_err_str);
  1198. if (token.type != TK_PARENTHESIS_CLOSE) {
  1199. r_err_str = "Expected ')'";
  1200. return ERR_PARSE_ERROR;
  1201. }
  1202. array.assign(values);
  1203. value = array;
  1204. } else if (id == "PackedByteArray" || id == "PoolByteArray" || id == "ByteArray") {
  1205. Vector<uint8_t> args;
  1206. Error err = _parse_byte_array(p_stream, args, line, r_err_str);
  1207. if (err) {
  1208. return err;
  1209. }
  1210. Vector<uint8_t> arr;
  1211. {
  1212. int len = args.size();
  1213. arr.resize(len);
  1214. uint8_t *w = arr.ptrw();
  1215. for (int i = 0; i < len; i++) {
  1216. w[i] = args[i];
  1217. }
  1218. }
  1219. value = arr;
  1220. } else if (id == "PackedInt32Array" || id == "PackedIntArray" || id == "PoolIntArray" || id == "IntArray") {
  1221. Vector<int32_t> args;
  1222. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  1223. if (err) {
  1224. return err;
  1225. }
  1226. Vector<int32_t> arr;
  1227. {
  1228. int32_t len = args.size();
  1229. arr.resize(len);
  1230. int32_t *w = arr.ptrw();
  1231. for (int32_t i = 0; i < len; i++) {
  1232. w[i] = int32_t(args[i]);
  1233. }
  1234. }
  1235. value = arr;
  1236. } else if (id == "PackedInt64Array") {
  1237. Vector<int64_t> args;
  1238. Error err = _parse_construct<int64_t>(p_stream, args, line, r_err_str);
  1239. if (err) {
  1240. return err;
  1241. }
  1242. Vector<int64_t> arr;
  1243. {
  1244. int64_t len = args.size();
  1245. arr.resize(len);
  1246. int64_t *w = arr.ptrw();
  1247. for (int64_t i = 0; i < len; i++) {
  1248. w[i] = int64_t(args[i]);
  1249. }
  1250. }
  1251. value = arr;
  1252. } else if (id == "PackedFloat32Array" || id == "PackedRealArray" || id == "PoolRealArray" || id == "FloatArray") {
  1253. Vector<float> args;
  1254. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  1255. if (err) {
  1256. return err;
  1257. }
  1258. Vector<float> arr;
  1259. {
  1260. int len = args.size();
  1261. arr.resize(len);
  1262. float *w = arr.ptrw();
  1263. for (int i = 0; i < len; i++) {
  1264. w[i] = args[i];
  1265. }
  1266. }
  1267. value = arr;
  1268. } else if (id == "PackedFloat64Array") {
  1269. Vector<double> args;
  1270. Error err = _parse_construct<double>(p_stream, args, line, r_err_str);
  1271. if (err) {
  1272. return err;
  1273. }
  1274. Vector<double> arr;
  1275. {
  1276. int len = args.size();
  1277. arr.resize(len);
  1278. double *w = arr.ptrw();
  1279. for (int i = 0; i < len; i++) {
  1280. w[i] = args[i];
  1281. }
  1282. }
  1283. value = arr;
  1284. } else if (id == "PackedStringArray" || id == "PoolStringArray" || id == "StringArray") {
  1285. get_token(p_stream, token, line, r_err_str);
  1286. if (token.type != TK_PARENTHESIS_OPEN) {
  1287. r_err_str = "Expected '('";
  1288. return ERR_PARSE_ERROR;
  1289. }
  1290. Vector<String> cs;
  1291. bool first = true;
  1292. while (true) {
  1293. if (!first) {
  1294. get_token(p_stream, token, line, r_err_str);
  1295. if (token.type == TK_COMMA) {
  1296. //do none
  1297. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  1298. break;
  1299. } else {
  1300. r_err_str = "Expected ',' or ')'";
  1301. return ERR_PARSE_ERROR;
  1302. }
  1303. }
  1304. get_token(p_stream, token, line, r_err_str);
  1305. if (token.type == TK_PARENTHESIS_CLOSE) {
  1306. break;
  1307. } else if (token.type != TK_STRING) {
  1308. r_err_str = "Expected string";
  1309. return ERR_PARSE_ERROR;
  1310. }
  1311. first = false;
  1312. cs.push_back(token.value);
  1313. }
  1314. Vector<String> arr;
  1315. {
  1316. int len = cs.size();
  1317. arr.resize(len);
  1318. String *w = arr.ptrw();
  1319. for (int i = 0; i < len; i++) {
  1320. w[i] = cs[i];
  1321. }
  1322. }
  1323. value = arr;
  1324. } else if (id == "PackedVector2Array" || id == "PoolVector2Array" || id == "Vector2Array") {
  1325. Vector<real_t> args;
  1326. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  1327. if (err) {
  1328. return err;
  1329. }
  1330. Vector<Vector2> arr;
  1331. {
  1332. int len = args.size() / 2;
  1333. arr.resize(len);
  1334. Vector2 *w = arr.ptrw();
  1335. for (int i = 0; i < len; i++) {
  1336. w[i] = Vector2(args[i * 2 + 0], args[i * 2 + 1]);
  1337. }
  1338. }
  1339. value = arr;
  1340. } else if (id == "PackedVector3Array" || id == "PoolVector3Array" || id == "Vector3Array") {
  1341. Vector<real_t> args;
  1342. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  1343. if (err) {
  1344. return err;
  1345. }
  1346. Vector<Vector3> arr;
  1347. {
  1348. int len = args.size() / 3;
  1349. arr.resize(len);
  1350. Vector3 *w = arr.ptrw();
  1351. for (int i = 0; i < len; i++) {
  1352. w[i] = Vector3(args[i * 3 + 0], args[i * 3 + 1], args[i * 3 + 2]);
  1353. }
  1354. }
  1355. value = arr;
  1356. } else if (id == "PackedVector4Array" || id == "PoolVector4Array" || id == "Vector4Array") {
  1357. Vector<real_t> args;
  1358. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  1359. if (err) {
  1360. return err;
  1361. }
  1362. Vector<Vector4> arr;
  1363. {
  1364. int len = args.size() / 4;
  1365. arr.resize(len);
  1366. Vector4 *w = arr.ptrw();
  1367. for (int i = 0; i < len; i++) {
  1368. w[i] = Vector4(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  1369. }
  1370. }
  1371. value = arr;
  1372. } else if (id == "PackedColorArray" || id == "PoolColorArray" || id == "ColorArray") {
  1373. Vector<float> args;
  1374. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  1375. if (err) {
  1376. return err;
  1377. }
  1378. Vector<Color> arr;
  1379. {
  1380. int len = args.size() / 4;
  1381. arr.resize(len);
  1382. Color *w = arr.ptrw();
  1383. for (int i = 0; i < len; i++) {
  1384. w[i] = Color(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  1385. }
  1386. }
  1387. value = arr;
  1388. } else {
  1389. r_err_str = vformat("Unexpected identifier '%s'", id);
  1390. return ERR_PARSE_ERROR;
  1391. }
  1392. // All above branches end up here unless they had an early return.
  1393. return OK;
  1394. } else if (token.type == TK_NUMBER) {
  1395. value = token.value;
  1396. return OK;
  1397. } else if (token.type == TK_STRING) {
  1398. value = token.value;
  1399. return OK;
  1400. } else if (token.type == TK_STRING_NAME) {
  1401. value = token.value;
  1402. return OK;
  1403. } else if (token.type == TK_COLOR) {
  1404. value = token.value;
  1405. return OK;
  1406. } else {
  1407. r_err_str = vformat("Expected value, got '%s'", String(tk_name[token.type]));
  1408. return ERR_PARSE_ERROR;
  1409. }
  1410. }
  1411. Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1412. Token token;
  1413. bool need_comma = false;
  1414. while (true) {
  1415. if (p_stream->is_eof()) {
  1416. r_err_str = "Unexpected EOF while parsing array";
  1417. return ERR_FILE_CORRUPT;
  1418. }
  1419. Error err = get_token(p_stream, token, line, r_err_str);
  1420. if (err != OK) {
  1421. return err;
  1422. }
  1423. if (token.type == TK_BRACKET_CLOSE) {
  1424. return OK;
  1425. }
  1426. if (need_comma) {
  1427. if (token.type != TK_COMMA) {
  1428. r_err_str = "Expected ','";
  1429. return ERR_PARSE_ERROR;
  1430. } else {
  1431. need_comma = false;
  1432. continue;
  1433. }
  1434. }
  1435. Variant v;
  1436. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1437. if (err) {
  1438. return err;
  1439. }
  1440. array.push_back(v);
  1441. need_comma = true;
  1442. }
  1443. }
  1444. Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1445. bool at_key = true;
  1446. Variant key;
  1447. Token token;
  1448. bool need_comma = false;
  1449. while (true) {
  1450. if (p_stream->is_eof()) {
  1451. r_err_str = "Unexpected EOF while parsing dictionary";
  1452. return ERR_FILE_CORRUPT;
  1453. }
  1454. if (at_key) {
  1455. Error err = get_token(p_stream, token, line, r_err_str);
  1456. if (err != OK) {
  1457. return err;
  1458. }
  1459. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  1460. return OK;
  1461. }
  1462. if (need_comma) {
  1463. if (token.type != TK_COMMA) {
  1464. r_err_str = "Expected '}' or ','";
  1465. return ERR_PARSE_ERROR;
  1466. } else {
  1467. need_comma = false;
  1468. continue;
  1469. }
  1470. }
  1471. err = parse_value(token, key, p_stream, line, r_err_str, p_res_parser);
  1472. if (err) {
  1473. return err;
  1474. }
  1475. err = get_token(p_stream, token, line, r_err_str);
  1476. if (err != OK) {
  1477. return err;
  1478. }
  1479. if (token.type != TK_COLON) {
  1480. r_err_str = "Expected ':'";
  1481. return ERR_PARSE_ERROR;
  1482. }
  1483. at_key = false;
  1484. } else {
  1485. Error err = get_token(p_stream, token, line, r_err_str);
  1486. if (err != OK) {
  1487. return err;
  1488. }
  1489. Variant v;
  1490. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1491. if (err && err != ERR_FILE_MISSING_DEPENDENCIES) {
  1492. return err;
  1493. }
  1494. object[key] = v;
  1495. need_comma = true;
  1496. at_key = true;
  1497. }
  1498. }
  1499. }
  1500. Error VariantParser::_parse_tag(Token &token, Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1501. r_tag.fields.clear();
  1502. if (token.type != TK_BRACKET_OPEN) {
  1503. r_err_str = "Expected '['";
  1504. return ERR_PARSE_ERROR;
  1505. }
  1506. if (p_simple_tag) {
  1507. r_tag.name = "";
  1508. r_tag.fields.clear();
  1509. bool escaping = false;
  1510. if (p_stream->is_utf8()) {
  1511. CharString cs;
  1512. while (true) {
  1513. char c = p_stream->get_char();
  1514. if (p_stream->is_eof()) {
  1515. r_err_str = "Unexpected EOF while parsing simple tag";
  1516. return ERR_PARSE_ERROR;
  1517. }
  1518. if (c == ']') {
  1519. if (escaping) {
  1520. escaping = false;
  1521. } else {
  1522. break;
  1523. }
  1524. } else if (c == '\\') {
  1525. escaping = true;
  1526. } else {
  1527. escaping = false;
  1528. }
  1529. cs += c;
  1530. }
  1531. r_tag.name.clear();
  1532. r_tag.name.append_utf8(cs.get_data(), cs.length());
  1533. } else {
  1534. while (true) {
  1535. char32_t c = p_stream->get_char();
  1536. if (p_stream->is_eof()) {
  1537. r_err_str = "Unexpected EOF while parsing simple tag";
  1538. return ERR_PARSE_ERROR;
  1539. }
  1540. if (c == ']') {
  1541. if (escaping) {
  1542. escaping = false;
  1543. } else {
  1544. break;
  1545. }
  1546. } else if (c == '\\') {
  1547. escaping = true;
  1548. } else {
  1549. escaping = false;
  1550. }
  1551. r_tag.name += c;
  1552. }
  1553. }
  1554. r_tag.name = r_tag.name.strip_edges();
  1555. return OK;
  1556. }
  1557. get_token(p_stream, token, line, r_err_str);
  1558. if (token.type != TK_IDENTIFIER) {
  1559. r_err_str = "Expected identifier (tag name)";
  1560. return ERR_PARSE_ERROR;
  1561. }
  1562. r_tag.name = token.value;
  1563. bool parsing_tag = true;
  1564. while (true) {
  1565. if (p_stream->is_eof()) {
  1566. r_err_str = vformat("Unexpected EOF while parsing tag '%s'", r_tag.name);
  1567. return ERR_FILE_CORRUPT;
  1568. }
  1569. get_token(p_stream, token, line, r_err_str);
  1570. if (token.type == TK_BRACKET_CLOSE) {
  1571. break;
  1572. }
  1573. if (parsing_tag && token.type == TK_PERIOD) {
  1574. r_tag.name += "."; //support tags such as [someprop.Android] for specific platforms
  1575. get_token(p_stream, token, line, r_err_str);
  1576. } else if (parsing_tag && token.type == TK_COLON) {
  1577. r_tag.name += ":"; //support tags such as [someprop.Android] for specific platforms
  1578. get_token(p_stream, token, line, r_err_str);
  1579. } else {
  1580. parsing_tag = false;
  1581. }
  1582. if (token.type != TK_IDENTIFIER) {
  1583. r_err_str = "Expected identifier";
  1584. return ERR_PARSE_ERROR;
  1585. }
  1586. String id = token.value;
  1587. if (parsing_tag) {
  1588. r_tag.name += id;
  1589. continue;
  1590. }
  1591. get_token(p_stream, token, line, r_err_str);
  1592. if (token.type != TK_EQUAL) {
  1593. r_err_str = "Expected '=' after identifier";
  1594. return ERR_PARSE_ERROR;
  1595. }
  1596. get_token(p_stream, token, line, r_err_str);
  1597. Variant value;
  1598. Error err = parse_value(token, value, p_stream, line, r_err_str, p_res_parser);
  1599. if (err) {
  1600. return err;
  1601. }
  1602. r_tag.fields[id] = value;
  1603. }
  1604. return OK;
  1605. }
  1606. Error VariantParser::parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1607. Token token;
  1608. get_token(p_stream, token, line, r_err_str);
  1609. if (token.type == TK_EOF) {
  1610. return ERR_FILE_EOF;
  1611. }
  1612. if (token.type != TK_BRACKET_OPEN) {
  1613. r_err_str = "Expected '['";
  1614. return ERR_PARSE_ERROR;
  1615. }
  1616. return _parse_tag(token, p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1617. }
  1618. Error VariantParser::parse_tag_assign_eof(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, String &r_assign, Variant &r_value, ResourceParser *p_res_parser, bool p_simple_tag) {
  1619. //assign..
  1620. r_assign = "";
  1621. String what;
  1622. while (true) {
  1623. char32_t c;
  1624. if (p_stream->saved) {
  1625. c = p_stream->saved;
  1626. p_stream->saved = 0;
  1627. } else {
  1628. c = p_stream->get_char();
  1629. }
  1630. if (p_stream->is_eof()) {
  1631. return ERR_FILE_EOF;
  1632. }
  1633. if (c == ';') { //comment
  1634. while (true) {
  1635. char32_t ch = p_stream->get_char();
  1636. if (p_stream->is_eof()) {
  1637. return ERR_FILE_EOF;
  1638. }
  1639. if (ch == '\n') {
  1640. line++;
  1641. break;
  1642. }
  1643. }
  1644. continue;
  1645. }
  1646. if (c == '[' && what.length() == 0) {
  1647. //it's a tag!
  1648. p_stream->saved = '['; //go back one
  1649. Error err = parse_tag(p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1650. return err;
  1651. }
  1652. if (c > 32) {
  1653. if (c == '"') { //quoted
  1654. p_stream->saved = '"';
  1655. Token tk;
  1656. Error err = get_token(p_stream, tk, line, r_err_str);
  1657. if (err) {
  1658. return err;
  1659. }
  1660. if (tk.type != TK_STRING) {
  1661. r_err_str = "Error reading quoted string";
  1662. return ERR_INVALID_DATA;
  1663. }
  1664. what = tk.value;
  1665. } else if (c != '=') {
  1666. what += c;
  1667. } else {
  1668. r_assign = what;
  1669. Token token;
  1670. get_token(p_stream, token, line, r_err_str);
  1671. Error err = parse_value(token, r_value, p_stream, line, r_err_str, p_res_parser);
  1672. return err;
  1673. }
  1674. } else if (c == '\n') {
  1675. line++;
  1676. }
  1677. }
  1678. }
  1679. Error VariantParser::parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser) {
  1680. Token token;
  1681. Error err = get_token(p_stream, token, r_err_line, r_err_str);
  1682. if (err) {
  1683. return err;
  1684. }
  1685. if (token.type == TK_EOF) {
  1686. return ERR_FILE_EOF;
  1687. }
  1688. return parse_value(token, r_ret, p_stream, r_err_line, r_err_str, p_res_parser);
  1689. }
  1690. //////////////////////////////////////////////////////////////////////////////////
  1691. //////////////////////////////////////////////////////////////////////////////////
  1692. //////////////////////////////////////////////////////////////////////////////////
  1693. // These two functions serialize floats or doubles using num_scientific to ensure
  1694. // it can be read back in the same way (except collapsing -0 to 0, and NaN values).
  1695. static String rtos_fix(float p_value, bool p_compat) {
  1696. if (p_value == 0.0f) {
  1697. return "0"; // Avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
  1698. } else if (p_compat) {
  1699. // Write old inf_neg for compatibility.
  1700. if (std::isinf(p_value) && p_value < 0.0f) {
  1701. return "inf_neg";
  1702. }
  1703. }
  1704. return String::num_scientific(p_value);
  1705. }
  1706. static String rtos_fix(double p_value, bool p_compat) {
  1707. if (p_value == 0.0) {
  1708. return "0"; // Avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
  1709. } else if (p_compat) {
  1710. // Write old inf_neg for compatibility.
  1711. if (std::isinf(p_value) && p_value < 0.0) {
  1712. return "inf_neg";
  1713. }
  1714. }
  1715. return String::num_scientific(p_value);
  1716. }
  1717. Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_string_func, void *p_store_string_ud, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud, int p_recursion_count, bool p_compat) {
  1718. switch (p_variant.get_type()) {
  1719. case Variant::NIL: {
  1720. p_store_string_func(p_store_string_ud, "null");
  1721. } break;
  1722. case Variant::BOOL: {
  1723. p_store_string_func(p_store_string_ud, p_variant.operator bool() ? "true" : "false");
  1724. } break;
  1725. case Variant::INT: {
  1726. p_store_string_func(p_store_string_ud, itos(p_variant.operator int64_t()));
  1727. } break;
  1728. case Variant::FLOAT: {
  1729. const double value = p_variant.operator double();
  1730. String s;
  1731. // Hack to avoid garbage digits when the underlying float is 32-bit.
  1732. if ((double)(float)value == value) {
  1733. s = rtos_fix((float)value, p_compat);
  1734. } else {
  1735. s = rtos_fix(value, p_compat);
  1736. }
  1737. // Append ".0" to floats to ensure they are float literals.
  1738. if (s != "inf" && s != "-inf" && s != "nan" && !s.contains_char('.') && !s.contains_char('e') && !s.contains_char('E')) {
  1739. s += ".0";
  1740. }
  1741. p_store_string_func(p_store_string_ud, s);
  1742. } break;
  1743. case Variant::STRING: {
  1744. String str = p_variant;
  1745. str = "\"" + str.c_escape_multiline() + "\"";
  1746. p_store_string_func(p_store_string_ud, str);
  1747. } break;
  1748. // Math types.
  1749. case Variant::VECTOR2: {
  1750. Vector2 v = p_variant;
  1751. p_store_string_func(p_store_string_ud, "Vector2(" + rtos_fix(v.x, p_compat) + ", " + rtos_fix(v.y, p_compat) + ")");
  1752. } break;
  1753. case Variant::VECTOR2I: {
  1754. Vector2i v = p_variant;
  1755. p_store_string_func(p_store_string_ud, "Vector2i(" + itos(v.x) + ", " + itos(v.y) + ")");
  1756. } break;
  1757. case Variant::RECT2: {
  1758. Rect2 aabb = p_variant;
  1759. p_store_string_func(p_store_string_ud, "Rect2(" + rtos_fix(aabb.position.x, p_compat) + ", " + rtos_fix(aabb.position.y, p_compat) + ", " + rtos_fix(aabb.size.x, p_compat) + ", " + rtos_fix(aabb.size.y, p_compat) + ")");
  1760. } break;
  1761. case Variant::RECT2I: {
  1762. Rect2i aabb = p_variant;
  1763. p_store_string_func(p_store_string_ud, "Rect2i(" + itos(aabb.position.x) + ", " + itos(aabb.position.y) + ", " + itos(aabb.size.x) + ", " + itos(aabb.size.y) + ")");
  1764. } break;
  1765. case Variant::VECTOR3: {
  1766. Vector3 v = p_variant;
  1767. p_store_string_func(p_store_string_ud, "Vector3(" + rtos_fix(v.x, p_compat) + ", " + rtos_fix(v.y, p_compat) + ", " + rtos_fix(v.z, p_compat) + ")");
  1768. } break;
  1769. case Variant::VECTOR3I: {
  1770. Vector3i v = p_variant;
  1771. p_store_string_func(p_store_string_ud, "Vector3i(" + itos(v.x) + ", " + itos(v.y) + ", " + itos(v.z) + ")");
  1772. } break;
  1773. case Variant::VECTOR4: {
  1774. Vector4 v = p_variant;
  1775. p_store_string_func(p_store_string_ud, "Vector4(" + rtos_fix(v.x, p_compat) + ", " + rtos_fix(v.y, p_compat) + ", " + rtos_fix(v.z, p_compat) + ", " + rtos_fix(v.w, p_compat) + ")");
  1776. } break;
  1777. case Variant::VECTOR4I: {
  1778. Vector4i v = p_variant;
  1779. p_store_string_func(p_store_string_ud, "Vector4i(" + itos(v.x) + ", " + itos(v.y) + ", " + itos(v.z) + ", " + itos(v.w) + ")");
  1780. } break;
  1781. case Variant::PLANE: {
  1782. Plane p = p_variant;
  1783. p_store_string_func(p_store_string_ud, "Plane(" + rtos_fix(p.normal.x, p_compat) + ", " + rtos_fix(p.normal.y, p_compat) + ", " + rtos_fix(p.normal.z, p_compat) + ", " + rtos_fix(p.d, p_compat) + ")");
  1784. } break;
  1785. case Variant::AABB: {
  1786. AABB aabb = p_variant;
  1787. p_store_string_func(p_store_string_ud, "AABB(" + rtos_fix(aabb.position.x, p_compat) + ", " + rtos_fix(aabb.position.y, p_compat) + ", " + rtos_fix(aabb.position.z, p_compat) + ", " + rtos_fix(aabb.size.x, p_compat) + ", " + rtos_fix(aabb.size.y, p_compat) + ", " + rtos_fix(aabb.size.z, p_compat) + ")");
  1788. } break;
  1789. case Variant::QUATERNION: {
  1790. Quaternion quaternion = p_variant;
  1791. p_store_string_func(p_store_string_ud, "Quaternion(" + rtos_fix(quaternion.x, p_compat) + ", " + rtos_fix(quaternion.y, p_compat) + ", " + rtos_fix(quaternion.z, p_compat) + ", " + rtos_fix(quaternion.w, p_compat) + ")");
  1792. } break;
  1793. case Variant::TRANSFORM2D: {
  1794. String s = "Transform2D(";
  1795. Transform2D m3 = p_variant;
  1796. for (int i = 0; i < 3; i++) {
  1797. for (int j = 0; j < 2; j++) {
  1798. if (i != 0 || j != 0) {
  1799. s += ", ";
  1800. }
  1801. s += rtos_fix(m3.columns[i][j], p_compat);
  1802. }
  1803. }
  1804. p_store_string_func(p_store_string_ud, s + ")");
  1805. } break;
  1806. case Variant::BASIS: {
  1807. String s = "Basis(";
  1808. Basis m3 = p_variant;
  1809. for (int i = 0; i < 3; i++) {
  1810. for (int j = 0; j < 3; j++) {
  1811. if (i != 0 || j != 0) {
  1812. s += ", ";
  1813. }
  1814. s += rtos_fix(m3.rows[i][j], p_compat);
  1815. }
  1816. }
  1817. p_store_string_func(p_store_string_ud, s + ")");
  1818. } break;
  1819. case Variant::TRANSFORM3D: {
  1820. String s = "Transform3D(";
  1821. Transform3D t = p_variant;
  1822. Basis &m3 = t.basis;
  1823. for (int i = 0; i < 3; i++) {
  1824. for (int j = 0; j < 3; j++) {
  1825. if (i != 0 || j != 0) {
  1826. s += ", ";
  1827. }
  1828. s += rtos_fix(m3.rows[i][j], p_compat);
  1829. }
  1830. }
  1831. s = s + ", " + rtos_fix(t.origin.x, p_compat) + ", " + rtos_fix(t.origin.y, p_compat) + ", " + rtos_fix(t.origin.z, p_compat);
  1832. p_store_string_func(p_store_string_ud, s + ")");
  1833. } break;
  1834. case Variant::PROJECTION: {
  1835. String s = "Projection(";
  1836. Projection t = p_variant;
  1837. for (int i = 0; i < 4; i++) {
  1838. for (int j = 0; j < 4; j++) {
  1839. if (i != 0 || j != 0) {
  1840. s += ", ";
  1841. }
  1842. s += rtos_fix(t.columns[i][j], p_compat);
  1843. }
  1844. }
  1845. p_store_string_func(p_store_string_ud, s + ")");
  1846. } break;
  1847. // Misc types.
  1848. case Variant::COLOR: {
  1849. Color c = p_variant;
  1850. p_store_string_func(p_store_string_ud, "Color(" + rtos_fix(c.r, p_compat) + ", " + rtos_fix(c.g, p_compat) + ", " + rtos_fix(c.b, p_compat) + ", " + rtos_fix(c.a, p_compat) + ")");
  1851. } break;
  1852. case Variant::STRING_NAME: {
  1853. String str = p_variant;
  1854. str = "&\"" + str.c_escape() + "\"";
  1855. p_store_string_func(p_store_string_ud, str);
  1856. } break;
  1857. case Variant::NODE_PATH: {
  1858. String str = p_variant;
  1859. str = "NodePath(\"" + str.c_escape() + "\")";
  1860. p_store_string_func(p_store_string_ud, str);
  1861. } break;
  1862. case Variant::RID: {
  1863. RID rid = p_variant;
  1864. if (rid == RID()) {
  1865. p_store_string_func(p_store_string_ud, "RID()");
  1866. } else {
  1867. p_store_string_func(p_store_string_ud, "RID(" + itos(rid.get_id()) + ")");
  1868. }
  1869. } break;
  1870. // Do not really store these, but ensure that assignments are not empty.
  1871. case Variant::SIGNAL: {
  1872. p_store_string_func(p_store_string_ud, "Signal()");
  1873. } break;
  1874. case Variant::CALLABLE: {
  1875. p_store_string_func(p_store_string_ud, "Callable()");
  1876. } break;
  1877. case Variant::OBJECT: {
  1878. if (unlikely(p_recursion_count > MAX_RECURSION)) {
  1879. ERR_PRINT("Max recursion reached");
  1880. p_store_string_func(p_store_string_ud, "null");
  1881. return OK;
  1882. }
  1883. p_recursion_count++;
  1884. Object *obj = p_variant.get_validated_object();
  1885. if (!obj) {
  1886. p_store_string_func(p_store_string_ud, "null");
  1887. break; // don't save it
  1888. }
  1889. Ref<Resource> res = p_variant;
  1890. if (res.is_valid()) {
  1891. //is resource
  1892. String res_text;
  1893. //try external function
  1894. if (p_encode_res_func) {
  1895. res_text = p_encode_res_func(p_encode_res_ud, res);
  1896. }
  1897. //try path because it's a file
  1898. if (res_text.is_empty() && res->get_path().is_resource_file()) {
  1899. //external resource
  1900. String path = res->get_path();
  1901. res_text = "Resource(\"" + path + "\")";
  1902. }
  1903. //could come up with some sort of text
  1904. if (!res_text.is_empty()) {
  1905. p_store_string_func(p_store_string_ud, res_text);
  1906. break;
  1907. }
  1908. }
  1909. //store as generic object
  1910. p_store_string_func(p_store_string_ud, "Object(" + obj->get_class() + ",");
  1911. List<PropertyInfo> props;
  1912. obj->get_property_list(&props);
  1913. bool first = true;
  1914. for (const PropertyInfo &E : props) {
  1915. if (E.usage & PROPERTY_USAGE_STORAGE || E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
  1916. //must be serialized
  1917. if (first) {
  1918. first = false;
  1919. } else {
  1920. p_store_string_func(p_store_string_ud, ",");
  1921. }
  1922. p_store_string_func(p_store_string_ud, "\"" + E.name + "\":");
  1923. write(obj->get(E.name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat);
  1924. }
  1925. }
  1926. p_store_string_func(p_store_string_ud, ")\n");
  1927. } break;
  1928. case Variant::DICTIONARY: {
  1929. Dictionary dict = p_variant;
  1930. if (dict.is_typed()) {
  1931. p_store_string_func(p_store_string_ud, "Dictionary[");
  1932. Variant::Type key_builtin_type = (Variant::Type)dict.get_typed_key_builtin();
  1933. StringName key_class_name = dict.get_typed_key_class_name();
  1934. Ref<Script> key_script = dict.get_typed_key_script();
  1935. if (key_script.is_valid()) {
  1936. String resource_text;
  1937. if (p_encode_res_func) {
  1938. resource_text = p_encode_res_func(p_encode_res_ud, key_script);
  1939. }
  1940. if (resource_text.is_empty() && key_script->get_path().is_resource_file()) {
  1941. resource_text = "Resource(\"" + key_script->get_path() + "\")";
  1942. }
  1943. if (!resource_text.is_empty()) {
  1944. p_store_string_func(p_store_string_ud, resource_text);
  1945. } else {
  1946. ERR_PRINT("Failed to encode a path to a custom script for a dictionary key type.");
  1947. p_store_string_func(p_store_string_ud, key_class_name);
  1948. }
  1949. } else if (key_class_name != StringName()) {
  1950. p_store_string_func(p_store_string_ud, key_class_name);
  1951. } else if (key_builtin_type == Variant::NIL) {
  1952. p_store_string_func(p_store_string_ud, "Variant");
  1953. } else {
  1954. p_store_string_func(p_store_string_ud, Variant::get_type_name(key_builtin_type));
  1955. }
  1956. p_store_string_func(p_store_string_ud, ", ");
  1957. Variant::Type value_builtin_type = (Variant::Type)dict.get_typed_value_builtin();
  1958. StringName value_class_name = dict.get_typed_value_class_name();
  1959. Ref<Script> value_script = dict.get_typed_value_script();
  1960. if (value_script.is_valid()) {
  1961. String resource_text;
  1962. if (p_encode_res_func) {
  1963. resource_text = p_encode_res_func(p_encode_res_ud, value_script);
  1964. }
  1965. if (resource_text.is_empty() && value_script->get_path().is_resource_file()) {
  1966. resource_text = "Resource(\"" + value_script->get_path() + "\")";
  1967. }
  1968. if (!resource_text.is_empty()) {
  1969. p_store_string_func(p_store_string_ud, resource_text);
  1970. } else {
  1971. ERR_PRINT("Failed to encode a path to a custom script for a dictionary value type.");
  1972. p_store_string_func(p_store_string_ud, value_class_name);
  1973. }
  1974. } else if (value_class_name != StringName()) {
  1975. p_store_string_func(p_store_string_ud, value_class_name);
  1976. } else if (value_builtin_type == Variant::NIL) {
  1977. p_store_string_func(p_store_string_ud, "Variant");
  1978. } else {
  1979. p_store_string_func(p_store_string_ud, Variant::get_type_name(value_builtin_type));
  1980. }
  1981. p_store_string_func(p_store_string_ud, "](");
  1982. }
  1983. if (unlikely(p_recursion_count > MAX_RECURSION)) {
  1984. ERR_PRINT("Max recursion reached");
  1985. p_store_string_func(p_store_string_ud, "{}");
  1986. } else {
  1987. LocalVector<Variant> keys = dict.get_key_list();
  1988. keys.sort_custom<StringLikeVariantOrder>();
  1989. if (keys.is_empty()) {
  1990. // Avoid unnecessary line break.
  1991. p_store_string_func(p_store_string_ud, "{}");
  1992. } else {
  1993. p_recursion_count++;
  1994. p_store_string_func(p_store_string_ud, "{\n");
  1995. for (uint32_t i = 0; i < keys.size(); i++) {
  1996. const Variant &key = keys[i];
  1997. write(key, p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat);
  1998. p_store_string_func(p_store_string_ud, ": ");
  1999. write(dict[key], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat);
  2000. if (i + 1 < keys.size()) {
  2001. p_store_string_func(p_store_string_ud, ",\n");
  2002. } else {
  2003. p_store_string_func(p_store_string_ud, "\n");
  2004. }
  2005. }
  2006. p_store_string_func(p_store_string_ud, "}");
  2007. }
  2008. }
  2009. if (dict.is_typed()) {
  2010. p_store_string_func(p_store_string_ud, ")");
  2011. }
  2012. } break;
  2013. case Variant::ARRAY: {
  2014. Array array = p_variant;
  2015. if (array.is_typed()) {
  2016. p_store_string_func(p_store_string_ud, "Array[");
  2017. Variant::Type builtin_type = (Variant::Type)array.get_typed_builtin();
  2018. StringName class_name = array.get_typed_class_name();
  2019. Ref<Script> script = array.get_typed_script();
  2020. if (script.is_valid()) {
  2021. String resource_text = String();
  2022. if (p_encode_res_func) {
  2023. resource_text = p_encode_res_func(p_encode_res_ud, script);
  2024. }
  2025. if (resource_text.is_empty() && script->get_path().is_resource_file()) {
  2026. resource_text = "Resource(\"" + script->get_path() + "\")";
  2027. }
  2028. if (!resource_text.is_empty()) {
  2029. p_store_string_func(p_store_string_ud, resource_text);
  2030. } else {
  2031. ERR_PRINT("Failed to encode a path to a custom script for an array type.");
  2032. p_store_string_func(p_store_string_ud, class_name);
  2033. }
  2034. } else if (class_name != StringName()) {
  2035. p_store_string_func(p_store_string_ud, class_name);
  2036. } else {
  2037. p_store_string_func(p_store_string_ud, Variant::get_type_name(builtin_type));
  2038. }
  2039. p_store_string_func(p_store_string_ud, "](");
  2040. }
  2041. if (unlikely(p_recursion_count > MAX_RECURSION)) {
  2042. ERR_PRINT("Max recursion reached");
  2043. p_store_string_func(p_store_string_ud, "[]");
  2044. } else {
  2045. p_recursion_count++;
  2046. p_store_string_func(p_store_string_ud, "[");
  2047. bool first = true;
  2048. for (const Variant &var : array) {
  2049. if (first) {
  2050. first = false;
  2051. } else {
  2052. p_store_string_func(p_store_string_ud, ", ");
  2053. }
  2054. write(var, p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat);
  2055. }
  2056. p_store_string_func(p_store_string_ud, "]");
  2057. }
  2058. if (array.is_typed()) {
  2059. p_store_string_func(p_store_string_ud, ")");
  2060. }
  2061. } break;
  2062. case Variant::PACKED_BYTE_ARRAY: {
  2063. p_store_string_func(p_store_string_ud, "PackedByteArray(");
  2064. Vector<uint8_t> data = p_variant;
  2065. if (p_compat) {
  2066. int len = data.size();
  2067. const uint8_t *ptr = data.ptr();
  2068. for (int i = 0; i < len; i++) {
  2069. if (i > 0) {
  2070. p_store_string_func(p_store_string_ud, ", ");
  2071. }
  2072. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  2073. }
  2074. } else if (data.size() > 0) {
  2075. p_store_string_func(p_store_string_ud, "\"");
  2076. p_store_string_func(p_store_string_ud, CryptoCore::b64_encode_str(data.ptr(), data.size()));
  2077. p_store_string_func(p_store_string_ud, "\"");
  2078. }
  2079. p_store_string_func(p_store_string_ud, ")");
  2080. } break;
  2081. case Variant::PACKED_INT32_ARRAY: {
  2082. p_store_string_func(p_store_string_ud, "PackedInt32Array(");
  2083. Vector<int32_t> data = p_variant;
  2084. int32_t len = data.size();
  2085. const int32_t *ptr = data.ptr();
  2086. for (int32_t i = 0; i < len; i++) {
  2087. if (i > 0) {
  2088. p_store_string_func(p_store_string_ud, ", ");
  2089. }
  2090. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  2091. }
  2092. p_store_string_func(p_store_string_ud, ")");
  2093. } break;
  2094. case Variant::PACKED_INT64_ARRAY: {
  2095. p_store_string_func(p_store_string_ud, "PackedInt64Array(");
  2096. Vector<int64_t> data = p_variant;
  2097. int64_t len = data.size();
  2098. const int64_t *ptr = data.ptr();
  2099. for (int64_t i = 0; i < len; i++) {
  2100. if (i > 0) {
  2101. p_store_string_func(p_store_string_ud, ", ");
  2102. }
  2103. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  2104. }
  2105. p_store_string_func(p_store_string_ud, ")");
  2106. } break;
  2107. case Variant::PACKED_FLOAT32_ARRAY: {
  2108. p_store_string_func(p_store_string_ud, "PackedFloat32Array(");
  2109. Vector<float> data = p_variant;
  2110. int len = data.size();
  2111. const float *ptr = data.ptr();
  2112. for (int i = 0; i < len; i++) {
  2113. if (i > 0) {
  2114. p_store_string_func(p_store_string_ud, ", ");
  2115. }
  2116. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i], p_compat));
  2117. }
  2118. p_store_string_func(p_store_string_ud, ")");
  2119. } break;
  2120. case Variant::PACKED_FLOAT64_ARRAY: {
  2121. p_store_string_func(p_store_string_ud, "PackedFloat64Array(");
  2122. Vector<double> data = p_variant;
  2123. int len = data.size();
  2124. const double *ptr = data.ptr();
  2125. for (int i = 0; i < len; i++) {
  2126. if (i > 0) {
  2127. p_store_string_func(p_store_string_ud, ", ");
  2128. }
  2129. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i], p_compat));
  2130. }
  2131. p_store_string_func(p_store_string_ud, ")");
  2132. } break;
  2133. case Variant::PACKED_STRING_ARRAY: {
  2134. p_store_string_func(p_store_string_ud, "PackedStringArray(");
  2135. Vector<String> data = p_variant;
  2136. int len = data.size();
  2137. const String *ptr = data.ptr();
  2138. for (int i = 0; i < len; i++) {
  2139. if (i > 0) {
  2140. p_store_string_func(p_store_string_ud, ", ");
  2141. }
  2142. p_store_string_func(p_store_string_ud, "\"" + ptr[i].c_escape() + "\"");
  2143. }
  2144. p_store_string_func(p_store_string_ud, ")");
  2145. } break;
  2146. case Variant::PACKED_VECTOR2_ARRAY: {
  2147. p_store_string_func(p_store_string_ud, "PackedVector2Array(");
  2148. Vector<Vector2> data = p_variant;
  2149. int len = data.size();
  2150. const Vector2 *ptr = data.ptr();
  2151. for (int i = 0; i < len; i++) {
  2152. if (i > 0) {
  2153. p_store_string_func(p_store_string_ud, ", ");
  2154. }
  2155. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i].x, p_compat) + ", " + rtos_fix(ptr[i].y, p_compat));
  2156. }
  2157. p_store_string_func(p_store_string_ud, ")");
  2158. } break;
  2159. case Variant::PACKED_VECTOR3_ARRAY: {
  2160. p_store_string_func(p_store_string_ud, "PackedVector3Array(");
  2161. Vector<Vector3> data = p_variant;
  2162. int len = data.size();
  2163. const Vector3 *ptr = data.ptr();
  2164. for (int i = 0; i < len; i++) {
  2165. if (i > 0) {
  2166. p_store_string_func(p_store_string_ud, ", ");
  2167. }
  2168. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i].x, p_compat) + ", " + rtos_fix(ptr[i].y, p_compat) + ", " + rtos_fix(ptr[i].z, p_compat));
  2169. }
  2170. p_store_string_func(p_store_string_ud, ")");
  2171. } break;
  2172. case Variant::PACKED_COLOR_ARRAY: {
  2173. p_store_string_func(p_store_string_ud, "PackedColorArray(");
  2174. Vector<Color> data = p_variant;
  2175. int len = data.size();
  2176. const Color *ptr = data.ptr();
  2177. for (int i = 0; i < len; i++) {
  2178. if (i > 0) {
  2179. p_store_string_func(p_store_string_ud, ", ");
  2180. }
  2181. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i].r, p_compat) + ", " + rtos_fix(ptr[i].g, p_compat) + ", " + rtos_fix(ptr[i].b, p_compat) + ", " + rtos_fix(ptr[i].a, p_compat));
  2182. }
  2183. p_store_string_func(p_store_string_ud, ")");
  2184. } break;
  2185. case Variant::PACKED_VECTOR4_ARRAY: {
  2186. p_store_string_func(p_store_string_ud, "PackedVector4Array(");
  2187. Vector<Vector4> data = p_variant;
  2188. int len = data.size();
  2189. const Vector4 *ptr = data.ptr();
  2190. for (int i = 0; i < len; i++) {
  2191. if (i > 0) {
  2192. p_store_string_func(p_store_string_ud, ", ");
  2193. }
  2194. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i].x, p_compat) + ", " + rtos_fix(ptr[i].y, p_compat) + ", " + rtos_fix(ptr[i].z, p_compat) + ", " + rtos_fix(ptr[i].w, p_compat));
  2195. }
  2196. p_store_string_func(p_store_string_ud, ")");
  2197. } break;
  2198. default: {
  2199. ERR_PRINT("Unknown variant type");
  2200. return ERR_BUG;
  2201. }
  2202. }
  2203. return OK;
  2204. }
  2205. static Error _write_to_str(void *ud, const String &p_string) {
  2206. String *str = (String *)ud;
  2207. (*str) += p_string;
  2208. return OK;
  2209. }
  2210. Error VariantWriter::write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud, bool p_compat) {
  2211. r_string = String();
  2212. return write(p_variant, _write_to_str, &r_string, p_encode_res_func, p_encode_res_ud, 0, p_compat);
  2213. }