variant_parser.cpp 67 KB

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