variant_parser.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. /**************************************************************************/
  2. /* variant_parser.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "variant_parser.h"
  31. #include "core/crypto/crypto_core.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/object/script_language.h"
  34. #include "core/string/string_buffer.h"
  35. char32_t VariantParser::Stream::get_char() {
  36. // is within buffer?
  37. if (readahead_pointer < readahead_filled) {
  38. return readahead_buffer[readahead_pointer++];
  39. }
  40. // attempt to readahead
  41. readahead_filled = _read_buffer(readahead_buffer, readahead_enabled ? READAHEAD_SIZE : 1);
  42. if (readahead_filled) {
  43. readahead_pointer = 0;
  44. } else {
  45. // EOF
  46. readahead_pointer = 1;
  47. eof = true;
  48. return 0;
  49. }
  50. return get_char();
  51. }
  52. bool VariantParser::Stream::is_eof() const {
  53. if (readahead_enabled) {
  54. return eof;
  55. }
  56. return _is_eof();
  57. }
  58. bool VariantParser::StreamFile::is_utf8() const {
  59. return true;
  60. }
  61. bool VariantParser::StreamFile::_is_eof() const {
  62. return f->eof_reached();
  63. }
  64. uint32_t VariantParser::StreamFile::_read_buffer(char32_t *p_buffer, uint32_t p_num_chars) {
  65. // The buffer is assumed to include at least one character (for null terminator)
  66. ERR_FAIL_COND_V(!p_num_chars, 0);
  67. uint8_t *temp = (uint8_t *)alloca(p_num_chars);
  68. uint64_t num_read = f->get_buffer(temp, p_num_chars);
  69. ERR_FAIL_COND_V(num_read == UINT64_MAX, 0);
  70. // translate to wchar
  71. for (uint32_t n = 0; n < num_read; n++) {
  72. p_buffer[n] = temp[n];
  73. }
  74. // could be less than p_num_chars, or zero
  75. return num_read;
  76. }
  77. bool VariantParser::StreamString::is_utf8() const {
  78. return false;
  79. }
  80. bool VariantParser::StreamString::_is_eof() const {
  81. return pos > s.length();
  82. }
  83. uint32_t VariantParser::StreamString::_read_buffer(char32_t *p_buffer, uint32_t p_num_chars) {
  84. // The buffer is assumed to include at least one character (for null terminator)
  85. ERR_FAIL_COND_V(!p_num_chars, 0);
  86. ERR_FAIL_NULL_V(p_buffer, 0);
  87. int available = MAX(s.length() - pos, 0);
  88. if (available >= (int)p_num_chars) {
  89. const char32_t *src = s.ptr();
  90. src += pos;
  91. memcpy(p_buffer, src, p_num_chars * sizeof(char32_t));
  92. pos += p_num_chars;
  93. return p_num_chars;
  94. }
  95. // going to reach EOF
  96. if (available) {
  97. const char32_t *src = s.ptr();
  98. src += pos;
  99. memcpy(p_buffer, src, available * sizeof(char32_t));
  100. pos += available;
  101. }
  102. // add a zero
  103. p_buffer[available] = 0;
  104. return available;
  105. }
  106. /////////////////////////////////////////////////////////////////////////////////////////////////
  107. const char *VariantParser::tk_name[TK_MAX] = {
  108. "'{'",
  109. "'}'",
  110. "'['",
  111. "']'",
  112. "'('",
  113. "')'",
  114. "identifier",
  115. "string",
  116. "string_name",
  117. "number",
  118. "color",
  119. "':'",
  120. "','",
  121. "'.'",
  122. "'='",
  123. "EOF",
  124. "ERROR"
  125. };
  126. static double stor_fix(const String &p_str) {
  127. if (p_str == "inf") {
  128. return INFINITY;
  129. } else if (p_str == "inf_neg") {
  130. return -INFINITY;
  131. } else if (p_str == "nan") {
  132. return NAN;
  133. }
  134. return -1;
  135. }
  136. Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str) {
  137. bool string_name = false;
  138. while (true) {
  139. char32_t cchar;
  140. if (p_stream->saved) {
  141. cchar = p_stream->saved;
  142. p_stream->saved = 0;
  143. } else {
  144. cchar = p_stream->get_char();
  145. if (p_stream->is_eof()) {
  146. r_token.type = TK_EOF;
  147. return OK;
  148. }
  149. }
  150. switch (cchar) {
  151. case '\n': {
  152. line++;
  153. break;
  154. }
  155. case 0: {
  156. r_token.type = TK_EOF;
  157. return OK;
  158. } break;
  159. case '{': {
  160. r_token.type = TK_CURLY_BRACKET_OPEN;
  161. return OK;
  162. }
  163. case '}': {
  164. r_token.type = TK_CURLY_BRACKET_CLOSE;
  165. return OK;
  166. }
  167. case '[': {
  168. r_token.type = TK_BRACKET_OPEN;
  169. return OK;
  170. }
  171. case ']': {
  172. r_token.type = TK_BRACKET_CLOSE;
  173. return OK;
  174. }
  175. case '(': {
  176. r_token.type = TK_PARENTHESIS_OPEN;
  177. return OK;
  178. }
  179. case ')': {
  180. r_token.type = TK_PARENTHESIS_CLOSE;
  181. return OK;
  182. }
  183. case ':': {
  184. r_token.type = TK_COLON;
  185. return OK;
  186. }
  187. case ';': {
  188. while (true) {
  189. char32_t ch = p_stream->get_char();
  190. if (p_stream->is_eof()) {
  191. r_token.type = TK_EOF;
  192. return OK;
  193. }
  194. if (ch == '\n') {
  195. line++;
  196. break;
  197. }
  198. }
  199. break;
  200. }
  201. case ',': {
  202. r_token.type = TK_COMMA;
  203. return OK;
  204. }
  205. case '.': {
  206. r_token.type = TK_PERIOD;
  207. return OK;
  208. }
  209. case '=': {
  210. r_token.type = TK_EQUAL;
  211. return OK;
  212. }
  213. case '#': {
  214. StringBuffer<> color_str;
  215. color_str += '#';
  216. while (true) {
  217. char32_t ch = p_stream->get_char();
  218. if (p_stream->is_eof()) {
  219. r_token.type = TK_EOF;
  220. return OK;
  221. } else if (is_hex_digit(ch)) {
  222. color_str += ch;
  223. } else {
  224. p_stream->saved = ch;
  225. break;
  226. }
  227. }
  228. r_token.value = Color::html(color_str.as_string());
  229. r_token.type = TK_COLOR;
  230. return OK;
  231. }
  232. #ifndef DISABLE_DEPRECATED
  233. case '@': // Compatibility with 3.x StringNames.
  234. #endif
  235. case '&': { // StringName.
  236. cchar = p_stream->get_char();
  237. if (cchar != '"') {
  238. r_err_str = "Expected '\"' after '&'";
  239. r_token.type = TK_ERROR;
  240. return ERR_PARSE_ERROR;
  241. }
  242. string_name = true;
  243. [[fallthrough]];
  244. }
  245. case '"': {
  246. String str;
  247. char32_t prev = 0;
  248. while (true) {
  249. char32_t ch = p_stream->get_char();
  250. if (ch == 0) {
  251. r_err_str = "Unterminated string";
  252. r_token.type = TK_ERROR;
  253. return ERR_PARSE_ERROR;
  254. } else if (ch == '"') {
  255. break;
  256. } else if (ch == '\\') {
  257. //escaped characters...
  258. char32_t next = p_stream->get_char();
  259. if (next == 0) {
  260. r_err_str = "Unterminated string";
  261. r_token.type = TK_ERROR;
  262. return ERR_PARSE_ERROR;
  263. }
  264. char32_t res = 0;
  265. switch (next) {
  266. case 'b':
  267. res = 8;
  268. break;
  269. case 't':
  270. res = 9;
  271. break;
  272. case 'n':
  273. res = 10;
  274. break;
  275. case 'f':
  276. res = 12;
  277. break;
  278. case 'r':
  279. res = 13;
  280. break;
  281. case 'U':
  282. case 'u': {
  283. // Hexadecimal sequence.
  284. int hex_len = (next == 'U') ? 6 : 4;
  285. for (int j = 0; j < hex_len; j++) {
  286. char32_t c = p_stream->get_char();
  287. if (c == 0) {
  288. r_err_str = "Unterminated string";
  289. r_token.type = TK_ERROR;
  290. return ERR_PARSE_ERROR;
  291. }
  292. if (!is_hex_digit(c)) {
  293. r_err_str = "Malformed hex constant in string";
  294. r_token.type = TK_ERROR;
  295. return ERR_PARSE_ERROR;
  296. }
  297. char32_t v;
  298. if (is_digit(c)) {
  299. v = c - '0';
  300. } else if (c >= 'a' && c <= 'f') {
  301. v = c - 'a';
  302. v += 10;
  303. } else if (c >= 'A' && c <= 'F') {
  304. v = c - 'A';
  305. v += 10;
  306. } else {
  307. ERR_PRINT("Bug parsing hex constant.");
  308. v = 0;
  309. }
  310. res <<= 4;
  311. res |= v;
  312. }
  313. } break;
  314. default: {
  315. res = next;
  316. } break;
  317. }
  318. // Parse UTF-16 pair.
  319. if ((res & 0xfffffc00) == 0xd800) {
  320. if (prev == 0) {
  321. prev = res;
  322. continue;
  323. } else {
  324. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  325. r_token.type = TK_ERROR;
  326. return ERR_PARSE_ERROR;
  327. }
  328. } else if ((res & 0xfffffc00) == 0xdc00) {
  329. if (prev == 0) {
  330. r_err_str = "Invalid UTF-16 sequence in string, unpaired trail surrogate";
  331. r_token.type = TK_ERROR;
  332. return ERR_PARSE_ERROR;
  333. } else {
  334. res = (prev << 10UL) + res - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  335. prev = 0;
  336. }
  337. }
  338. if (prev != 0) {
  339. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  340. r_token.type = TK_ERROR;
  341. return ERR_PARSE_ERROR;
  342. }
  343. str += res;
  344. } else {
  345. if (prev != 0) {
  346. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  347. r_token.type = TK_ERROR;
  348. return ERR_PARSE_ERROR;
  349. }
  350. if (ch == '\n') {
  351. line++;
  352. }
  353. str += ch;
  354. }
  355. }
  356. if (prev != 0) {
  357. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  358. r_token.type = TK_ERROR;
  359. return ERR_PARSE_ERROR;
  360. }
  361. if (p_stream->is_utf8()) {
  362. str.parse_utf8(str.ascii(true).get_data());
  363. }
  364. if (string_name) {
  365. r_token.type = TK_STRING_NAME;
  366. r_token.value = StringName(str);
  367. } else {
  368. r_token.type = TK_STRING;
  369. r_token.value = str;
  370. }
  371. return OK;
  372. } break;
  373. default: {
  374. if (cchar <= 32) {
  375. break;
  376. }
  377. if (cchar == '-' || (cchar >= '0' && cchar <= '9')) {
  378. //a number
  379. StringBuffer<> num;
  380. #define READING_SIGN 0
  381. #define READING_INT 1
  382. #define READING_DEC 2
  383. #define READING_EXP 3
  384. #define READING_DONE 4
  385. int reading = READING_INT;
  386. if (cchar == '-') {
  387. num += '-';
  388. cchar = p_stream->get_char();
  389. }
  390. char32_t c = cchar;
  391. bool exp_sign = false;
  392. bool exp_beg = false;
  393. bool is_float = false;
  394. while (true) {
  395. switch (reading) {
  396. case READING_INT: {
  397. if (is_digit(c)) {
  398. //pass
  399. } else if (c == '.') {
  400. reading = READING_DEC;
  401. is_float = true;
  402. } else if (c == 'e' || c == 'E') {
  403. reading = READING_EXP;
  404. is_float = true;
  405. } else {
  406. reading = READING_DONE;
  407. }
  408. } break;
  409. case READING_DEC: {
  410. if (is_digit(c)) {
  411. } else if (c == 'e' || c == 'E') {
  412. reading = READING_EXP;
  413. } else {
  414. reading = READING_DONE;
  415. }
  416. } break;
  417. case READING_EXP: {
  418. if (is_digit(c)) {
  419. exp_beg = true;
  420. } else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) {
  421. exp_sign = true;
  422. } else {
  423. reading = READING_DONE;
  424. }
  425. } break;
  426. }
  427. if (reading == READING_DONE) {
  428. break;
  429. }
  430. num += c;
  431. c = p_stream->get_char();
  432. }
  433. p_stream->saved = c;
  434. r_token.type = TK_NUMBER;
  435. if (is_float) {
  436. r_token.value = num.as_double();
  437. } else {
  438. r_token.value = num.as_int();
  439. }
  440. return OK;
  441. } else if (is_ascii_alphabet_char(cchar) || is_underscore(cchar)) {
  442. StringBuffer<> id;
  443. bool first = true;
  444. while (is_ascii_alphabet_char(cchar) || is_underscore(cchar) || (!first && is_digit(cchar))) {
  445. id += cchar;
  446. cchar = p_stream->get_char();
  447. first = false;
  448. }
  449. p_stream->saved = cchar;
  450. r_token.type = TK_IDENTIFIER;
  451. r_token.value = id.as_string();
  452. return OK;
  453. } else {
  454. r_err_str = "Unexpected character";
  455. r_token.type = TK_ERROR;
  456. return ERR_PARSE_ERROR;
  457. }
  458. }
  459. }
  460. }
  461. r_err_str = "Unknown error getting token";
  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 = vformat("Can't instantiate Object() of type '%s'", 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 EOF 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_bracket_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 = vformat("Unexpected identifier '%s'", 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 = vformat("Expected value, got '%s'", 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 EOF 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 EOF 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 = vformat("Unexpected EOF while parsing tag '%s'", 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. r_err_str = "Expected '=' after identifier";
  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') && !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. }