variant_parser.cpp 70 KB

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