variant_parser.cpp 68 KB

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