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