variant_parser.cpp 67 KB

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