marshalls.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  1. /**************************************************************************/
  2. /* marshalls.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 "marshalls.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/object/ref_counted.h"
  33. #include "core/object/script_language.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/string/print_string.h"
  36. #include <limits.h>
  37. #include <stdio.h>
  38. void EncodedObjectAsID::_bind_methods() {
  39. ClassDB::bind_method(D_METHOD("set_object_id", "id"), &EncodedObjectAsID::set_object_id);
  40. ClassDB::bind_method(D_METHOD("get_object_id"), &EncodedObjectAsID::get_object_id);
  41. ADD_PROPERTY(PropertyInfo(Variant::INT, "object_id"), "set_object_id", "get_object_id");
  42. }
  43. void EncodedObjectAsID::set_object_id(ObjectID p_id) {
  44. id = p_id;
  45. }
  46. ObjectID EncodedObjectAsID::get_object_id() const {
  47. return id;
  48. }
  49. #define ERR_FAIL_ADD_OF(a, b, err) ERR_FAIL_COND_V(((int32_t)(b)) < 0 || ((int32_t)(a)) < 0 || ((int32_t)(a)) > INT_MAX - ((int32_t)(b)), err)
  50. #define ERR_FAIL_MUL_OF(a, b, err) ERR_FAIL_COND_V(((int32_t)(a)) < 0 || ((int32_t)(b)) <= 0 || ((int32_t)(a)) > INT_MAX / ((int32_t)(b)), err)
  51. // Byte 0: `Variant::Type`, byte 1: unused, bytes 2 and 3: additional data.
  52. #define HEADER_TYPE_MASK 0xFF
  53. // For `Variant::INT`, `Variant::FLOAT` and other math types.
  54. #define HEADER_DATA_FLAG_64 (1 << 16)
  55. // For `Variant::OBJECT`.
  56. #define HEADER_DATA_FLAG_OBJECT_AS_ID (1 << 16)
  57. // For `Variant::ARRAY`.
  58. // Occupies bits 16 and 17.
  59. #define HEADER_DATA_FIELD_TYPED_ARRAY_MASK (0b11 << 16)
  60. #define HEADER_DATA_FIELD_TYPED_ARRAY_NONE (0b00 << 16)
  61. #define HEADER_DATA_FIELD_TYPED_ARRAY_BUILTIN (0b01 << 16)
  62. #define HEADER_DATA_FIELD_TYPED_ARRAY_CLASS_NAME (0b10 << 16)
  63. #define HEADER_DATA_FIELD_TYPED_ARRAY_SCRIPT (0b11 << 16)
  64. static Error _decode_string(const uint8_t *&buf, int &len, int *r_len, String &r_string) {
  65. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  66. int32_t strlen = decode_uint32(buf);
  67. int32_t pad = 0;
  68. // Handle padding
  69. if (strlen % 4) {
  70. pad = 4 - strlen % 4;
  71. }
  72. buf += 4;
  73. len -= 4;
  74. // Ensure buffer is big enough
  75. ERR_FAIL_ADD_OF(strlen, pad, ERR_FILE_EOF);
  76. ERR_FAIL_COND_V(strlen < 0 || strlen + pad > len, ERR_FILE_EOF);
  77. String str;
  78. ERR_FAIL_COND_V(str.parse_utf8((const char *)buf, strlen) != OK, ERR_INVALID_DATA);
  79. r_string = str;
  80. // Add padding
  81. strlen += pad;
  82. // Update buffer pos, left data count, and return size
  83. buf += strlen;
  84. len -= strlen;
  85. if (r_len) {
  86. (*r_len) += 4 + strlen;
  87. }
  88. return OK;
  89. }
  90. Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_objects, int p_depth) {
  91. ERR_FAIL_COND_V_MSG(p_depth > Variant::MAX_RECURSION_DEPTH, ERR_OUT_OF_MEMORY, "Variant is too deep. Bailing.");
  92. const uint8_t *buf = p_buffer;
  93. int len = p_len;
  94. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  95. uint32_t header = decode_uint32(buf);
  96. ERR_FAIL_COND_V((header & HEADER_TYPE_MASK) >= Variant::VARIANT_MAX, ERR_INVALID_DATA);
  97. buf += 4;
  98. len -= 4;
  99. if (r_len) {
  100. *r_len = 4;
  101. }
  102. // Note: We cannot use sizeof(real_t) for decoding, in case a different size is encoded.
  103. // Decoding math types always checks for the encoded size, while encoding always uses compilation setting.
  104. // This does lead to some code duplication for decoding, but compatibility is the priority.
  105. switch (header & HEADER_TYPE_MASK) {
  106. case Variant::NIL: {
  107. r_variant = Variant();
  108. } break;
  109. case Variant::BOOL: {
  110. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  111. bool val = decode_uint32(buf);
  112. r_variant = val;
  113. if (r_len) {
  114. (*r_len) += 4;
  115. }
  116. } break;
  117. case Variant::INT: {
  118. if (header & HEADER_DATA_FLAG_64) {
  119. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  120. int64_t val = decode_uint64(buf);
  121. r_variant = val;
  122. if (r_len) {
  123. (*r_len) += 8;
  124. }
  125. } else {
  126. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  127. int32_t val = decode_uint32(buf);
  128. r_variant = val;
  129. if (r_len) {
  130. (*r_len) += 4;
  131. }
  132. }
  133. } break;
  134. case Variant::FLOAT: {
  135. if (header & HEADER_DATA_FLAG_64) {
  136. ERR_FAIL_COND_V((size_t)len < sizeof(double), ERR_INVALID_DATA);
  137. double val = decode_double(buf);
  138. r_variant = val;
  139. if (r_len) {
  140. (*r_len) += sizeof(double);
  141. }
  142. } else {
  143. ERR_FAIL_COND_V((size_t)len < sizeof(float), ERR_INVALID_DATA);
  144. float val = decode_float(buf);
  145. r_variant = val;
  146. if (r_len) {
  147. (*r_len) += sizeof(float);
  148. }
  149. }
  150. } break;
  151. case Variant::STRING: {
  152. String str;
  153. Error err = _decode_string(buf, len, r_len, str);
  154. if (err) {
  155. return err;
  156. }
  157. r_variant = str;
  158. } break;
  159. // math types
  160. case Variant::VECTOR2: {
  161. Vector2 val;
  162. if (header & HEADER_DATA_FLAG_64) {
  163. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 2, ERR_INVALID_DATA);
  164. val.x = decode_double(&buf[0]);
  165. val.y = decode_double(&buf[sizeof(double)]);
  166. if (r_len) {
  167. (*r_len) += sizeof(double) * 2;
  168. }
  169. } else {
  170. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 2, ERR_INVALID_DATA);
  171. val.x = decode_float(&buf[0]);
  172. val.y = decode_float(&buf[sizeof(float)]);
  173. if (r_len) {
  174. (*r_len) += sizeof(float) * 2;
  175. }
  176. }
  177. r_variant = val;
  178. } break;
  179. case Variant::VECTOR2I: {
  180. ERR_FAIL_COND_V(len < 4 * 2, ERR_INVALID_DATA);
  181. Vector2i val;
  182. val.x = decode_uint32(&buf[0]);
  183. val.y = decode_uint32(&buf[4]);
  184. r_variant = val;
  185. if (r_len) {
  186. (*r_len) += 4 * 2;
  187. }
  188. } break;
  189. case Variant::RECT2: {
  190. Rect2 val;
  191. if (header & HEADER_DATA_FLAG_64) {
  192. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  193. val.position.x = decode_double(&buf[0]);
  194. val.position.y = decode_double(&buf[sizeof(double)]);
  195. val.size.x = decode_double(&buf[sizeof(double) * 2]);
  196. val.size.y = decode_double(&buf[sizeof(double) * 3]);
  197. if (r_len) {
  198. (*r_len) += sizeof(double) * 4;
  199. }
  200. } else {
  201. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  202. val.position.x = decode_float(&buf[0]);
  203. val.position.y = decode_float(&buf[sizeof(float)]);
  204. val.size.x = decode_float(&buf[sizeof(float) * 2]);
  205. val.size.y = decode_float(&buf[sizeof(float) * 3]);
  206. if (r_len) {
  207. (*r_len) += sizeof(float) * 4;
  208. }
  209. }
  210. r_variant = val;
  211. } break;
  212. case Variant::RECT2I: {
  213. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  214. Rect2i val;
  215. val.position.x = decode_uint32(&buf[0]);
  216. val.position.y = decode_uint32(&buf[4]);
  217. val.size.x = decode_uint32(&buf[8]);
  218. val.size.y = decode_uint32(&buf[12]);
  219. r_variant = val;
  220. if (r_len) {
  221. (*r_len) += 4 * 4;
  222. }
  223. } break;
  224. case Variant::VECTOR3: {
  225. Vector3 val;
  226. if (header & HEADER_DATA_FLAG_64) {
  227. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 3, ERR_INVALID_DATA);
  228. val.x = decode_double(&buf[0]);
  229. val.y = decode_double(&buf[sizeof(double)]);
  230. val.z = decode_double(&buf[sizeof(double) * 2]);
  231. if (r_len) {
  232. (*r_len) += sizeof(double) * 3;
  233. }
  234. } else {
  235. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 3, ERR_INVALID_DATA);
  236. val.x = decode_float(&buf[0]);
  237. val.y = decode_float(&buf[sizeof(float)]);
  238. val.z = decode_float(&buf[sizeof(float) * 2]);
  239. if (r_len) {
  240. (*r_len) += sizeof(float) * 3;
  241. }
  242. }
  243. r_variant = val;
  244. } break;
  245. case Variant::VECTOR3I: {
  246. ERR_FAIL_COND_V(len < 4 * 3, ERR_INVALID_DATA);
  247. Vector3i val;
  248. val.x = decode_uint32(&buf[0]);
  249. val.y = decode_uint32(&buf[4]);
  250. val.z = decode_uint32(&buf[8]);
  251. r_variant = val;
  252. if (r_len) {
  253. (*r_len) += 4 * 3;
  254. }
  255. } break;
  256. case Variant::VECTOR4: {
  257. Vector4 val;
  258. if (header & HEADER_DATA_FLAG_64) {
  259. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  260. val.x = decode_double(&buf[0]);
  261. val.y = decode_double(&buf[sizeof(double)]);
  262. val.z = decode_double(&buf[sizeof(double) * 2]);
  263. val.w = decode_double(&buf[sizeof(double) * 3]);
  264. if (r_len) {
  265. (*r_len) += sizeof(double) * 4;
  266. }
  267. } else {
  268. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  269. val.x = decode_float(&buf[0]);
  270. val.y = decode_float(&buf[sizeof(float)]);
  271. val.z = decode_float(&buf[sizeof(float) * 2]);
  272. val.w = decode_float(&buf[sizeof(float) * 3]);
  273. if (r_len) {
  274. (*r_len) += sizeof(float) * 4;
  275. }
  276. }
  277. r_variant = val;
  278. } break;
  279. case Variant::VECTOR4I: {
  280. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  281. Vector4i val;
  282. val.x = decode_uint32(&buf[0]);
  283. val.y = decode_uint32(&buf[4]);
  284. val.z = decode_uint32(&buf[8]);
  285. val.w = decode_uint32(&buf[12]);
  286. r_variant = val;
  287. if (r_len) {
  288. (*r_len) += 4 * 4;
  289. }
  290. } break;
  291. case Variant::TRANSFORM2D: {
  292. Transform2D val;
  293. if (header & HEADER_DATA_FLAG_64) {
  294. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 6, ERR_INVALID_DATA);
  295. for (int i = 0; i < 3; i++) {
  296. for (int j = 0; j < 2; j++) {
  297. val.columns[i][j] = decode_double(&buf[(i * 2 + j) * sizeof(double)]);
  298. }
  299. }
  300. if (r_len) {
  301. (*r_len) += sizeof(double) * 6;
  302. }
  303. } else {
  304. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 6, ERR_INVALID_DATA);
  305. for (int i = 0; i < 3; i++) {
  306. for (int j = 0; j < 2; j++) {
  307. val.columns[i][j] = decode_float(&buf[(i * 2 + j) * sizeof(float)]);
  308. }
  309. }
  310. if (r_len) {
  311. (*r_len) += sizeof(float) * 6;
  312. }
  313. }
  314. r_variant = val;
  315. } break;
  316. case Variant::PLANE: {
  317. Plane val;
  318. if (header & HEADER_DATA_FLAG_64) {
  319. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  320. val.normal.x = decode_double(&buf[0]);
  321. val.normal.y = decode_double(&buf[sizeof(double)]);
  322. val.normal.z = decode_double(&buf[sizeof(double) * 2]);
  323. val.d = decode_double(&buf[sizeof(double) * 3]);
  324. if (r_len) {
  325. (*r_len) += sizeof(double) * 4;
  326. }
  327. } else {
  328. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  329. val.normal.x = decode_float(&buf[0]);
  330. val.normal.y = decode_float(&buf[sizeof(float)]);
  331. val.normal.z = decode_float(&buf[sizeof(float) * 2]);
  332. val.d = decode_float(&buf[sizeof(float) * 3]);
  333. if (r_len) {
  334. (*r_len) += sizeof(float) * 4;
  335. }
  336. }
  337. r_variant = val;
  338. } break;
  339. case Variant::QUATERNION: {
  340. Quaternion val;
  341. if (header & HEADER_DATA_FLAG_64) {
  342. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  343. val.x = decode_double(&buf[0]);
  344. val.y = decode_double(&buf[sizeof(double)]);
  345. val.z = decode_double(&buf[sizeof(double) * 2]);
  346. val.w = decode_double(&buf[sizeof(double) * 3]);
  347. if (r_len) {
  348. (*r_len) += sizeof(double) * 4;
  349. }
  350. } else {
  351. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  352. val.x = decode_float(&buf[0]);
  353. val.y = decode_float(&buf[sizeof(float)]);
  354. val.z = decode_float(&buf[sizeof(float) * 2]);
  355. val.w = decode_float(&buf[sizeof(float) * 3]);
  356. if (r_len) {
  357. (*r_len) += sizeof(float) * 4;
  358. }
  359. }
  360. r_variant = val;
  361. } break;
  362. case Variant::AABB: {
  363. AABB val;
  364. if (header & HEADER_DATA_FLAG_64) {
  365. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 6, ERR_INVALID_DATA);
  366. val.position.x = decode_double(&buf[0]);
  367. val.position.y = decode_double(&buf[sizeof(double)]);
  368. val.position.z = decode_double(&buf[sizeof(double) * 2]);
  369. val.size.x = decode_double(&buf[sizeof(double) * 3]);
  370. val.size.y = decode_double(&buf[sizeof(double) * 4]);
  371. val.size.z = decode_double(&buf[sizeof(double) * 5]);
  372. if (r_len) {
  373. (*r_len) += sizeof(double) * 6;
  374. }
  375. } else {
  376. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 6, ERR_INVALID_DATA);
  377. val.position.x = decode_float(&buf[0]);
  378. val.position.y = decode_float(&buf[sizeof(float)]);
  379. val.position.z = decode_float(&buf[sizeof(float) * 2]);
  380. val.size.x = decode_float(&buf[sizeof(float) * 3]);
  381. val.size.y = decode_float(&buf[sizeof(float) * 4]);
  382. val.size.z = decode_float(&buf[sizeof(float) * 5]);
  383. if (r_len) {
  384. (*r_len) += sizeof(float) * 6;
  385. }
  386. }
  387. r_variant = val;
  388. } break;
  389. case Variant::BASIS: {
  390. Basis val;
  391. if (header & HEADER_DATA_FLAG_64) {
  392. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 9, ERR_INVALID_DATA);
  393. for (int i = 0; i < 3; i++) {
  394. for (int j = 0; j < 3; j++) {
  395. val.rows[i][j] = decode_double(&buf[(i * 3 + j) * sizeof(double)]);
  396. }
  397. }
  398. if (r_len) {
  399. (*r_len) += sizeof(double) * 9;
  400. }
  401. } else {
  402. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 9, ERR_INVALID_DATA);
  403. for (int i = 0; i < 3; i++) {
  404. for (int j = 0; j < 3; j++) {
  405. val.rows[i][j] = decode_float(&buf[(i * 3 + j) * sizeof(float)]);
  406. }
  407. }
  408. if (r_len) {
  409. (*r_len) += sizeof(float) * 9;
  410. }
  411. }
  412. r_variant = val;
  413. } break;
  414. case Variant::TRANSFORM3D: {
  415. Transform3D val;
  416. if (header & HEADER_DATA_FLAG_64) {
  417. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 12, ERR_INVALID_DATA);
  418. for (int i = 0; i < 3; i++) {
  419. for (int j = 0; j < 3; j++) {
  420. val.basis.rows[i][j] = decode_double(&buf[(i * 3 + j) * sizeof(double)]);
  421. }
  422. }
  423. val.origin[0] = decode_double(&buf[sizeof(double) * 9]);
  424. val.origin[1] = decode_double(&buf[sizeof(double) * 10]);
  425. val.origin[2] = decode_double(&buf[sizeof(double) * 11]);
  426. if (r_len) {
  427. (*r_len) += sizeof(double) * 12;
  428. }
  429. } else {
  430. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 12, ERR_INVALID_DATA);
  431. for (int i = 0; i < 3; i++) {
  432. for (int j = 0; j < 3; j++) {
  433. val.basis.rows[i][j] = decode_float(&buf[(i * 3 + j) * sizeof(float)]);
  434. }
  435. }
  436. val.origin[0] = decode_float(&buf[sizeof(float) * 9]);
  437. val.origin[1] = decode_float(&buf[sizeof(float) * 10]);
  438. val.origin[2] = decode_float(&buf[sizeof(float) * 11]);
  439. if (r_len) {
  440. (*r_len) += sizeof(float) * 12;
  441. }
  442. }
  443. r_variant = val;
  444. } break;
  445. case Variant::PROJECTION: {
  446. Projection val;
  447. if (header & HEADER_DATA_FLAG_64) {
  448. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 16, ERR_INVALID_DATA);
  449. for (int i = 0; i < 4; i++) {
  450. for (int j = 0; j < 4; j++) {
  451. val.columns[i][j] = decode_double(&buf[(i * 4 + j) * sizeof(double)]);
  452. }
  453. }
  454. if (r_len) {
  455. (*r_len) += sizeof(double) * 16;
  456. }
  457. } else {
  458. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 16, ERR_INVALID_DATA);
  459. for (int i = 0; i < 4; i++) {
  460. for (int j = 0; j < 4; j++) {
  461. val.columns[i][j] = decode_float(&buf[(i * 4 + j) * sizeof(float)]);
  462. }
  463. }
  464. if (r_len) {
  465. (*r_len) += sizeof(float) * 16;
  466. }
  467. }
  468. r_variant = val;
  469. } break;
  470. // misc types
  471. case Variant::COLOR: {
  472. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  473. Color val;
  474. val.r = decode_float(&buf[0]);
  475. val.g = decode_float(&buf[4]);
  476. val.b = decode_float(&buf[8]);
  477. val.a = decode_float(&buf[12]);
  478. r_variant = val;
  479. if (r_len) {
  480. (*r_len) += 4 * 4; // Colors should always be in single-precision.
  481. }
  482. } break;
  483. case Variant::STRING_NAME: {
  484. String str;
  485. Error err = _decode_string(buf, len, r_len, str);
  486. if (err) {
  487. return err;
  488. }
  489. r_variant = StringName(str);
  490. } break;
  491. case Variant::NODE_PATH: {
  492. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  493. int32_t strlen = decode_uint32(buf);
  494. if (strlen & 0x80000000) {
  495. //new format
  496. ERR_FAIL_COND_V(len < 12, ERR_INVALID_DATA);
  497. Vector<StringName> names;
  498. Vector<StringName> subnames;
  499. uint32_t namecount = strlen &= 0x7FFFFFFF;
  500. uint32_t subnamecount = decode_uint32(buf + 4);
  501. uint32_t np_flags = decode_uint32(buf + 8);
  502. len -= 12;
  503. buf += 12;
  504. if (np_flags & 2) { // Obsolete format with property separate from subpath.
  505. subnamecount++;
  506. }
  507. uint32_t total = namecount + subnamecount;
  508. if (r_len) {
  509. (*r_len) += 12;
  510. }
  511. for (uint32_t i = 0; i < total; i++) {
  512. String str;
  513. Error err = _decode_string(buf, len, r_len, str);
  514. if (err) {
  515. return err;
  516. }
  517. if (i < namecount) {
  518. names.push_back(str);
  519. } else {
  520. subnames.push_back(str);
  521. }
  522. }
  523. r_variant = NodePath(names, subnames, np_flags & 1);
  524. } else {
  525. //old format, just a string
  526. ERR_FAIL_V(ERR_INVALID_DATA);
  527. }
  528. } break;
  529. case Variant::RID: {
  530. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  531. uint64_t id = decode_uint64(buf);
  532. if (r_len) {
  533. (*r_len) += 8;
  534. }
  535. r_variant = RID::from_uint64(id);
  536. } break;
  537. case Variant::OBJECT: {
  538. if (header & HEADER_DATA_FLAG_OBJECT_AS_ID) {
  539. // This _is_ allowed.
  540. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  541. ObjectID val = ObjectID(decode_uint64(buf));
  542. if (r_len) {
  543. (*r_len) += 8;
  544. }
  545. if (val.is_null()) {
  546. r_variant = (Object *)nullptr;
  547. } else {
  548. Ref<EncodedObjectAsID> obj_as_id;
  549. obj_as_id.instantiate();
  550. obj_as_id->set_object_id(val);
  551. r_variant = obj_as_id;
  552. }
  553. } else {
  554. ERR_FAIL_COND_V(!p_allow_objects, ERR_UNAUTHORIZED);
  555. String str;
  556. Error err = _decode_string(buf, len, r_len, str);
  557. if (err) {
  558. return err;
  559. }
  560. if (str.is_empty()) {
  561. r_variant = (Object *)nullptr;
  562. } else {
  563. ERR_FAIL_COND_V(!ClassDB::can_instantiate(str), ERR_INVALID_DATA);
  564. Object *obj = ClassDB::instantiate(str);
  565. ERR_FAIL_NULL_V(obj, ERR_UNAVAILABLE);
  566. // Avoid premature free `RefCounted`. This must be done before properties are initialized,
  567. // since script functions (setters, implicit initializer) may be called. See GH-68666.
  568. Variant variant;
  569. if (Object::cast_to<RefCounted>(obj)) {
  570. Ref<RefCounted> ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
  571. variant = ref;
  572. } else {
  573. variant = obj;
  574. }
  575. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  576. int32_t count = decode_uint32(buf);
  577. buf += 4;
  578. len -= 4;
  579. if (r_len) {
  580. (*r_len) += 4; // Size of count number.
  581. }
  582. for (int i = 0; i < count; i++) {
  583. str = String();
  584. err = _decode_string(buf, len, r_len, str);
  585. if (err) {
  586. return err;
  587. }
  588. Variant value;
  589. int used;
  590. err = decode_variant(value, buf, len, &used, p_allow_objects, p_depth + 1);
  591. if (err) {
  592. return err;
  593. }
  594. buf += used;
  595. len -= used;
  596. if (r_len) {
  597. (*r_len) += used;
  598. }
  599. if (str == "script" && value.get_type() != Variant::NIL) {
  600. ERR_FAIL_COND_V_MSG(value.get_type() != Variant::STRING, ERR_INVALID_DATA, "Invalid value for \"script\" property, expected script path as String.");
  601. String path = value;
  602. ERR_FAIL_COND_V_MSG(path.is_empty() || !path.begins_with("res://") || !ResourceLoader::exists(path, "Script"), ERR_INVALID_DATA, "Invalid script path: '" + path + "'.");
  603. Ref<Script> script = ResourceLoader::load(path, "Script");
  604. ERR_FAIL_COND_V_MSG(script.is_null(), ERR_INVALID_DATA, "Can't load script at path: '" + path + "'.");
  605. obj->set_script(script);
  606. } else {
  607. obj->set(str, value);
  608. }
  609. }
  610. r_variant = variant;
  611. }
  612. }
  613. } break;
  614. case Variant::CALLABLE: {
  615. r_variant = Callable();
  616. } break;
  617. case Variant::SIGNAL: {
  618. String name;
  619. Error err = _decode_string(buf, len, r_len, name);
  620. if (err) {
  621. return err;
  622. }
  623. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  624. ObjectID id = ObjectID(decode_uint64(buf));
  625. if (r_len) {
  626. (*r_len) += 8;
  627. }
  628. r_variant = Signal(id, StringName(name));
  629. } break;
  630. case Variant::DICTIONARY: {
  631. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  632. int32_t count = decode_uint32(buf);
  633. // bool shared = count&0x80000000;
  634. count &= 0x7FFFFFFF;
  635. buf += 4;
  636. len -= 4;
  637. if (r_len) {
  638. (*r_len) += 4; // Size of count number.
  639. }
  640. Dictionary d;
  641. for (int i = 0; i < count; i++) {
  642. Variant key, value;
  643. int used;
  644. Error err = decode_variant(key, buf, len, &used, p_allow_objects, p_depth + 1);
  645. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant.");
  646. buf += used;
  647. len -= used;
  648. if (r_len) {
  649. (*r_len) += used;
  650. }
  651. err = decode_variant(value, buf, len, &used, p_allow_objects, p_depth + 1);
  652. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant.");
  653. buf += used;
  654. len -= used;
  655. if (r_len) {
  656. (*r_len) += used;
  657. }
  658. d[key] = value;
  659. }
  660. r_variant = d;
  661. } break;
  662. case Variant::ARRAY: {
  663. Variant::Type builtin_type = Variant::VARIANT_MAX;
  664. StringName class_name;
  665. Ref<Script> script;
  666. switch (header & HEADER_DATA_FIELD_TYPED_ARRAY_MASK) {
  667. case HEADER_DATA_FIELD_TYPED_ARRAY_NONE:
  668. break; // Untyped array.
  669. case HEADER_DATA_FIELD_TYPED_ARRAY_BUILTIN: {
  670. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  671. int32_t bt = decode_uint32(buf);
  672. buf += 4;
  673. len -= 4;
  674. if (r_len) {
  675. (*r_len) += 4;
  676. }
  677. ERR_FAIL_INDEX_V(bt, Variant::VARIANT_MAX, ERR_INVALID_DATA);
  678. builtin_type = (Variant::Type)bt;
  679. if (!p_allow_objects && builtin_type == Variant::OBJECT) {
  680. class_name = EncodedObjectAsID::get_class_static();
  681. }
  682. } break;
  683. case HEADER_DATA_FIELD_TYPED_ARRAY_CLASS_NAME: {
  684. String str;
  685. Error err = _decode_string(buf, len, r_len, str);
  686. if (err) {
  687. return err;
  688. }
  689. builtin_type = Variant::OBJECT;
  690. if (p_allow_objects) {
  691. class_name = str;
  692. } else {
  693. class_name = EncodedObjectAsID::get_class_static();
  694. }
  695. } break;
  696. case HEADER_DATA_FIELD_TYPED_ARRAY_SCRIPT: {
  697. String path;
  698. Error err = _decode_string(buf, len, r_len, path);
  699. if (err) {
  700. return err;
  701. }
  702. builtin_type = Variant::OBJECT;
  703. if (p_allow_objects) {
  704. ERR_FAIL_COND_V_MSG(path.is_empty() || !path.begins_with("res://") || !ResourceLoader::exists(path, "Script"), ERR_INVALID_DATA, "Invalid script path: '" + path + "'.");
  705. script = ResourceLoader::load(path, "Script");
  706. ERR_FAIL_COND_V_MSG(script.is_null(), ERR_INVALID_DATA, "Can't load script at path: '" + path + "'.");
  707. class_name = script->get_instance_base_type();
  708. } else {
  709. class_name = EncodedObjectAsID::get_class_static();
  710. }
  711. } break;
  712. default:
  713. ERR_FAIL_V(ERR_INVALID_DATA); // Future proofing.
  714. }
  715. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  716. int32_t count = decode_uint32(buf);
  717. // bool shared = count&0x80000000;
  718. count &= 0x7FFFFFFF;
  719. buf += 4;
  720. len -= 4;
  721. if (r_len) {
  722. (*r_len) += 4; // Size of count number.
  723. }
  724. Array varr;
  725. if (builtin_type != Variant::VARIANT_MAX) {
  726. varr.set_typed(builtin_type, class_name, script);
  727. }
  728. for (int i = 0; i < count; i++) {
  729. int used = 0;
  730. Variant v;
  731. Error err = decode_variant(v, buf, len, &used, p_allow_objects, p_depth + 1);
  732. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant.");
  733. buf += used;
  734. len -= used;
  735. varr.push_back(v);
  736. if (r_len) {
  737. (*r_len) += used;
  738. }
  739. }
  740. r_variant = varr;
  741. } break;
  742. // arrays
  743. case Variant::PACKED_BYTE_ARRAY: {
  744. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  745. int32_t count = decode_uint32(buf);
  746. buf += 4;
  747. len -= 4;
  748. ERR_FAIL_COND_V(count < 0 || count > len, ERR_INVALID_DATA);
  749. Vector<uint8_t> data;
  750. if (count) {
  751. data.resize(count);
  752. uint8_t *w = data.ptrw();
  753. for (int32_t i = 0; i < count; i++) {
  754. w[i] = buf[i];
  755. }
  756. }
  757. r_variant = data;
  758. if (r_len) {
  759. if (count % 4) {
  760. (*r_len) += 4 - count % 4;
  761. }
  762. (*r_len) += 4 + count;
  763. }
  764. } break;
  765. case Variant::PACKED_INT32_ARRAY: {
  766. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  767. int32_t count = decode_uint32(buf);
  768. buf += 4;
  769. len -= 4;
  770. ERR_FAIL_MUL_OF(count, 4, ERR_INVALID_DATA);
  771. ERR_FAIL_COND_V(count < 0 || count * 4 > len, ERR_INVALID_DATA);
  772. Vector<int32_t> data;
  773. if (count) {
  774. //const int*rbuf=(const int*)buf;
  775. data.resize(count);
  776. int32_t *w = data.ptrw();
  777. for (int32_t i = 0; i < count; i++) {
  778. w[i] = decode_uint32(&buf[i * 4]);
  779. }
  780. }
  781. r_variant = Variant(data);
  782. if (r_len) {
  783. (*r_len) += 4 + count * sizeof(int32_t);
  784. }
  785. } break;
  786. case Variant::PACKED_INT64_ARRAY: {
  787. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  788. int32_t count = decode_uint32(buf);
  789. buf += 4;
  790. len -= 4;
  791. ERR_FAIL_MUL_OF(count, 8, ERR_INVALID_DATA);
  792. ERR_FAIL_COND_V(count < 0 || count * 8 > len, ERR_INVALID_DATA);
  793. Vector<int64_t> data;
  794. if (count) {
  795. //const int*rbuf=(const int*)buf;
  796. data.resize(count);
  797. int64_t *w = data.ptrw();
  798. for (int64_t i = 0; i < count; i++) {
  799. w[i] = decode_uint64(&buf[i * 8]);
  800. }
  801. }
  802. r_variant = Variant(data);
  803. if (r_len) {
  804. (*r_len) += 4 + count * sizeof(int64_t);
  805. }
  806. } break;
  807. case Variant::PACKED_FLOAT32_ARRAY: {
  808. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  809. int32_t count = decode_uint32(buf);
  810. buf += 4;
  811. len -= 4;
  812. ERR_FAIL_MUL_OF(count, 4, ERR_INVALID_DATA);
  813. ERR_FAIL_COND_V(count < 0 || count * 4 > len, ERR_INVALID_DATA);
  814. Vector<float> data;
  815. if (count) {
  816. //const float*rbuf=(const float*)buf;
  817. data.resize(count);
  818. float *w = data.ptrw();
  819. for (int32_t i = 0; i < count; i++) {
  820. w[i] = decode_float(&buf[i * 4]);
  821. }
  822. }
  823. r_variant = data;
  824. if (r_len) {
  825. (*r_len) += 4 + count * sizeof(float);
  826. }
  827. } break;
  828. case Variant::PACKED_FLOAT64_ARRAY: {
  829. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  830. int32_t count = decode_uint32(buf);
  831. buf += 4;
  832. len -= 4;
  833. ERR_FAIL_MUL_OF(count, 8, ERR_INVALID_DATA);
  834. ERR_FAIL_COND_V(count < 0 || count * 8 > len, ERR_INVALID_DATA);
  835. Vector<double> data;
  836. if (count) {
  837. data.resize(count);
  838. double *w = data.ptrw();
  839. for (int64_t i = 0; i < count; i++) {
  840. w[i] = decode_double(&buf[i * 8]);
  841. }
  842. }
  843. r_variant = data;
  844. if (r_len) {
  845. (*r_len) += 4 + count * sizeof(double);
  846. }
  847. } break;
  848. case Variant::PACKED_STRING_ARRAY: {
  849. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  850. int32_t count = decode_uint32(buf);
  851. Vector<String> strings;
  852. buf += 4;
  853. len -= 4;
  854. if (r_len) {
  855. (*r_len) += 4; // Size of count number.
  856. }
  857. for (int32_t i = 0; i < count; i++) {
  858. String str;
  859. Error err = _decode_string(buf, len, r_len, str);
  860. if (err) {
  861. return err;
  862. }
  863. strings.push_back(str);
  864. }
  865. r_variant = strings;
  866. } break;
  867. case Variant::PACKED_VECTOR2_ARRAY: {
  868. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  869. int32_t count = decode_uint32(buf);
  870. buf += 4;
  871. len -= 4;
  872. Vector<Vector2> varray;
  873. if (header & HEADER_DATA_FLAG_64) {
  874. ERR_FAIL_MUL_OF(count, sizeof(double) * 2, ERR_INVALID_DATA);
  875. ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 2 > (size_t)len, ERR_INVALID_DATA);
  876. if (r_len) {
  877. (*r_len) += 4; // Size of count number.
  878. }
  879. if (count) {
  880. varray.resize(count);
  881. Vector2 *w = varray.ptrw();
  882. for (int32_t i = 0; i < count; i++) {
  883. w[i].x = decode_double(buf + i * sizeof(double) * 2 + sizeof(double) * 0);
  884. w[i].y = decode_double(buf + i * sizeof(double) * 2 + sizeof(double) * 1);
  885. }
  886. int adv = sizeof(double) * 2 * count;
  887. if (r_len) {
  888. (*r_len) += adv;
  889. }
  890. len -= adv;
  891. buf += adv;
  892. }
  893. } else {
  894. ERR_FAIL_MUL_OF(count, sizeof(float) * 2, ERR_INVALID_DATA);
  895. ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 2 > (size_t)len, ERR_INVALID_DATA);
  896. if (r_len) {
  897. (*r_len) += 4; // Size of count number.
  898. }
  899. if (count) {
  900. varray.resize(count);
  901. Vector2 *w = varray.ptrw();
  902. for (int32_t i = 0; i < count; i++) {
  903. w[i].x = decode_float(buf + i * sizeof(float) * 2 + sizeof(float) * 0);
  904. w[i].y = decode_float(buf + i * sizeof(float) * 2 + sizeof(float) * 1);
  905. }
  906. int adv = sizeof(float) * 2 * count;
  907. if (r_len) {
  908. (*r_len) += adv;
  909. }
  910. }
  911. }
  912. r_variant = varray;
  913. } break;
  914. case Variant::PACKED_VECTOR3_ARRAY: {
  915. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  916. int32_t count = decode_uint32(buf);
  917. buf += 4;
  918. len -= 4;
  919. Vector<Vector3> varray;
  920. if (header & HEADER_DATA_FLAG_64) {
  921. ERR_FAIL_MUL_OF(count, sizeof(double) * 3, ERR_INVALID_DATA);
  922. ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 3 > (size_t)len, ERR_INVALID_DATA);
  923. if (r_len) {
  924. (*r_len) += 4; // Size of count number.
  925. }
  926. if (count) {
  927. varray.resize(count);
  928. Vector3 *w = varray.ptrw();
  929. for (int32_t i = 0; i < count; i++) {
  930. w[i].x = decode_double(buf + i * sizeof(double) * 3 + sizeof(double) * 0);
  931. w[i].y = decode_double(buf + i * sizeof(double) * 3 + sizeof(double) * 1);
  932. w[i].z = decode_double(buf + i * sizeof(double) * 3 + sizeof(double) * 2);
  933. }
  934. int adv = sizeof(double) * 3 * count;
  935. if (r_len) {
  936. (*r_len) += adv;
  937. }
  938. len -= adv;
  939. buf += adv;
  940. }
  941. } else {
  942. ERR_FAIL_MUL_OF(count, sizeof(float) * 3, ERR_INVALID_DATA);
  943. ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 3 > (size_t)len, ERR_INVALID_DATA);
  944. if (r_len) {
  945. (*r_len) += 4; // Size of count number.
  946. }
  947. if (count) {
  948. varray.resize(count);
  949. Vector3 *w = varray.ptrw();
  950. for (int32_t i = 0; i < count; i++) {
  951. w[i].x = decode_float(buf + i * sizeof(float) * 3 + sizeof(float) * 0);
  952. w[i].y = decode_float(buf + i * sizeof(float) * 3 + sizeof(float) * 1);
  953. w[i].z = decode_float(buf + i * sizeof(float) * 3 + sizeof(float) * 2);
  954. }
  955. int adv = sizeof(float) * 3 * count;
  956. if (r_len) {
  957. (*r_len) += adv;
  958. }
  959. len -= adv;
  960. buf += adv;
  961. }
  962. }
  963. r_variant = varray;
  964. } break;
  965. case Variant::PACKED_COLOR_ARRAY: {
  966. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  967. int32_t count = decode_uint32(buf);
  968. buf += 4;
  969. len -= 4;
  970. ERR_FAIL_MUL_OF(count, 4 * 4, ERR_INVALID_DATA);
  971. ERR_FAIL_COND_V(count < 0 || count * 4 * 4 > len, ERR_INVALID_DATA);
  972. Vector<Color> carray;
  973. if (r_len) {
  974. (*r_len) += 4; // Size of count number.
  975. }
  976. if (count) {
  977. carray.resize(count);
  978. Color *w = carray.ptrw();
  979. for (int32_t i = 0; i < count; i++) {
  980. // Colors should always be in single-precision.
  981. w[i].r = decode_float(buf + i * 4 * 4 + 4 * 0);
  982. w[i].g = decode_float(buf + i * 4 * 4 + 4 * 1);
  983. w[i].b = decode_float(buf + i * 4 * 4 + 4 * 2);
  984. w[i].a = decode_float(buf + i * 4 * 4 + 4 * 3);
  985. }
  986. int adv = 4 * 4 * count;
  987. if (r_len) {
  988. (*r_len) += adv;
  989. }
  990. }
  991. r_variant = carray;
  992. } break;
  993. case Variant::PACKED_VECTOR4_ARRAY: {
  994. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  995. int32_t count = decode_uint32(buf);
  996. buf += 4;
  997. len -= 4;
  998. Vector<Vector4> varray;
  999. if (header & HEADER_DATA_FLAG_64) {
  1000. ERR_FAIL_MUL_OF(count, sizeof(double) * 4, ERR_INVALID_DATA);
  1001. ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 4 > (size_t)len, ERR_INVALID_DATA);
  1002. if (r_len) {
  1003. (*r_len) += 4; // Size of count number.
  1004. }
  1005. if (count) {
  1006. varray.resize(count);
  1007. Vector4 *w = varray.ptrw();
  1008. for (int32_t i = 0; i < count; i++) {
  1009. w[i].x = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 0);
  1010. w[i].y = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 1);
  1011. w[i].z = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 2);
  1012. w[i].w = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 3);
  1013. }
  1014. int adv = sizeof(double) * 4 * count;
  1015. if (r_len) {
  1016. (*r_len) += adv;
  1017. }
  1018. len -= adv;
  1019. buf += adv;
  1020. }
  1021. } else {
  1022. ERR_FAIL_MUL_OF(count, sizeof(float) * 4, ERR_INVALID_DATA);
  1023. ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 4 > (size_t)len, ERR_INVALID_DATA);
  1024. if (r_len) {
  1025. (*r_len) += 4; // Size of count number.
  1026. }
  1027. if (count) {
  1028. varray.resize(count);
  1029. Vector4 *w = varray.ptrw();
  1030. for (int32_t i = 0; i < count; i++) {
  1031. w[i].x = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 0);
  1032. w[i].y = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 1);
  1033. w[i].z = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 2);
  1034. w[i].w = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 3);
  1035. }
  1036. int adv = sizeof(float) * 4 * count;
  1037. if (r_len) {
  1038. (*r_len) += adv;
  1039. }
  1040. len -= adv;
  1041. buf += adv;
  1042. }
  1043. }
  1044. r_variant = varray;
  1045. } break;
  1046. default: {
  1047. ERR_FAIL_V(ERR_BUG);
  1048. }
  1049. }
  1050. return OK;
  1051. }
  1052. static void _encode_string(const String &p_string, uint8_t *&buf, int &r_len) {
  1053. CharString utf8 = p_string.utf8();
  1054. if (buf) {
  1055. encode_uint32(utf8.length(), buf);
  1056. buf += 4;
  1057. memcpy(buf, utf8.get_data(), utf8.length());
  1058. buf += utf8.length();
  1059. }
  1060. r_len += 4 + utf8.length();
  1061. while (r_len % 4) {
  1062. r_len++; //pad
  1063. if (buf) {
  1064. *(buf++) = 0;
  1065. }
  1066. }
  1067. }
  1068. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects, int p_depth) {
  1069. ERR_FAIL_COND_V_MSG(p_depth > Variant::MAX_RECURSION_DEPTH, ERR_OUT_OF_MEMORY, "Potential infinite recursion detected. Bailing.");
  1070. uint8_t *buf = r_buffer;
  1071. r_len = 0;
  1072. uint32_t header = p_variant.get_type();
  1073. switch (p_variant.get_type()) {
  1074. case Variant::INT: {
  1075. int64_t val = p_variant;
  1076. if (val > (int64_t)INT_MAX || val < (int64_t)INT_MIN) {
  1077. header |= HEADER_DATA_FLAG_64;
  1078. }
  1079. } break;
  1080. case Variant::FLOAT: {
  1081. double d = p_variant;
  1082. float f = d;
  1083. if (double(f) != d) {
  1084. header |= HEADER_DATA_FLAG_64;
  1085. }
  1086. } break;
  1087. case Variant::OBJECT: {
  1088. // Test for potential wrong values sent by the debugger when it breaks.
  1089. Object *obj = p_variant.get_validated_object();
  1090. if (!obj) {
  1091. // Object is invalid, send a nullptr instead.
  1092. if (buf) {
  1093. encode_uint32(Variant::NIL, buf);
  1094. }
  1095. r_len += 4;
  1096. return OK;
  1097. }
  1098. if (!p_full_objects) {
  1099. header |= HEADER_DATA_FLAG_OBJECT_AS_ID;
  1100. }
  1101. } break;
  1102. case Variant::ARRAY: {
  1103. Array array = p_variant;
  1104. if (array.is_typed()) {
  1105. Ref<Script> script = array.get_typed_script();
  1106. if (script.is_valid()) {
  1107. header |= p_full_objects ? HEADER_DATA_FIELD_TYPED_ARRAY_SCRIPT : HEADER_DATA_FIELD_TYPED_ARRAY_CLASS_NAME;
  1108. } else if (array.get_typed_class_name() != StringName()) {
  1109. header |= HEADER_DATA_FIELD_TYPED_ARRAY_CLASS_NAME;
  1110. } else {
  1111. // No need to check `p_full_objects` since for `Variant::OBJECT`
  1112. // `array.get_typed_class_name()` should be non-empty.
  1113. header |= HEADER_DATA_FIELD_TYPED_ARRAY_BUILTIN;
  1114. }
  1115. }
  1116. } break;
  1117. #ifdef REAL_T_IS_DOUBLE
  1118. case Variant::VECTOR2:
  1119. case Variant::VECTOR3:
  1120. case Variant::VECTOR4:
  1121. case Variant::PACKED_VECTOR2_ARRAY:
  1122. case Variant::PACKED_VECTOR3_ARRAY:
  1123. case Variant::PACKED_VECTOR4_ARRAY:
  1124. case Variant::TRANSFORM2D:
  1125. case Variant::TRANSFORM3D:
  1126. case Variant::PROJECTION:
  1127. case Variant::QUATERNION:
  1128. case Variant::PLANE:
  1129. case Variant::BASIS:
  1130. case Variant::RECT2:
  1131. case Variant::AABB: {
  1132. header |= HEADER_DATA_FLAG_64;
  1133. } break;
  1134. #endif // REAL_T_IS_DOUBLE
  1135. default: {
  1136. } // nothing to do at this stage
  1137. }
  1138. if (buf) {
  1139. encode_uint32(header, buf);
  1140. buf += 4;
  1141. }
  1142. r_len += 4;
  1143. switch (p_variant.get_type()) {
  1144. case Variant::NIL: {
  1145. //nothing to do
  1146. } break;
  1147. case Variant::BOOL: {
  1148. if (buf) {
  1149. encode_uint32(p_variant.operator bool(), buf);
  1150. }
  1151. r_len += 4;
  1152. } break;
  1153. case Variant::INT: {
  1154. if (header & HEADER_DATA_FLAG_64) {
  1155. //64 bits
  1156. if (buf) {
  1157. encode_uint64(p_variant.operator int64_t(), buf);
  1158. }
  1159. r_len += 8;
  1160. } else {
  1161. if (buf) {
  1162. encode_uint32(p_variant.operator int32_t(), buf);
  1163. }
  1164. r_len += 4;
  1165. }
  1166. } break;
  1167. case Variant::FLOAT: {
  1168. if (header & HEADER_DATA_FLAG_64) {
  1169. if (buf) {
  1170. encode_double(p_variant.operator double(), buf);
  1171. }
  1172. r_len += 8;
  1173. } else {
  1174. if (buf) {
  1175. encode_float(p_variant.operator float(), buf);
  1176. }
  1177. r_len += 4;
  1178. }
  1179. } break;
  1180. case Variant::NODE_PATH: {
  1181. NodePath np = p_variant;
  1182. if (buf) {
  1183. encode_uint32(uint32_t(np.get_name_count()) | 0x80000000, buf); //for compatibility with the old format
  1184. encode_uint32(np.get_subname_count(), buf + 4);
  1185. uint32_t np_flags = 0;
  1186. if (np.is_absolute()) {
  1187. np_flags |= 1;
  1188. }
  1189. encode_uint32(np_flags, buf + 8);
  1190. buf += 12;
  1191. }
  1192. r_len += 12;
  1193. int total = np.get_name_count() + np.get_subname_count();
  1194. for (int i = 0; i < total; i++) {
  1195. String str;
  1196. if (i < np.get_name_count()) {
  1197. str = np.get_name(i);
  1198. } else {
  1199. str = np.get_subname(i - np.get_name_count());
  1200. }
  1201. CharString utf8 = str.utf8();
  1202. int pad = 0;
  1203. if (utf8.length() % 4) {
  1204. pad = 4 - utf8.length() % 4;
  1205. }
  1206. if (buf) {
  1207. encode_uint32(utf8.length(), buf);
  1208. buf += 4;
  1209. memcpy(buf, utf8.get_data(), utf8.length());
  1210. buf += pad + utf8.length();
  1211. }
  1212. r_len += 4 + utf8.length() + pad;
  1213. }
  1214. } break;
  1215. case Variant::STRING:
  1216. case Variant::STRING_NAME: {
  1217. _encode_string(p_variant, buf, r_len);
  1218. } break;
  1219. // math types
  1220. case Variant::VECTOR2: {
  1221. if (buf) {
  1222. Vector2 v2 = p_variant;
  1223. encode_real(v2.x, &buf[0]);
  1224. encode_real(v2.y, &buf[sizeof(real_t)]);
  1225. }
  1226. r_len += 2 * sizeof(real_t);
  1227. } break;
  1228. case Variant::VECTOR2I: {
  1229. if (buf) {
  1230. Vector2i v2 = p_variant;
  1231. encode_uint32(v2.x, &buf[0]);
  1232. encode_uint32(v2.y, &buf[4]);
  1233. }
  1234. r_len += 2 * 4;
  1235. } break;
  1236. case Variant::RECT2: {
  1237. if (buf) {
  1238. Rect2 r2 = p_variant;
  1239. encode_real(r2.position.x, &buf[0]);
  1240. encode_real(r2.position.y, &buf[sizeof(real_t)]);
  1241. encode_real(r2.size.x, &buf[sizeof(real_t) * 2]);
  1242. encode_real(r2.size.y, &buf[sizeof(real_t) * 3]);
  1243. }
  1244. r_len += 4 * sizeof(real_t);
  1245. } break;
  1246. case Variant::RECT2I: {
  1247. if (buf) {
  1248. Rect2i r2 = p_variant;
  1249. encode_uint32(r2.position.x, &buf[0]);
  1250. encode_uint32(r2.position.y, &buf[4]);
  1251. encode_uint32(r2.size.x, &buf[8]);
  1252. encode_uint32(r2.size.y, &buf[12]);
  1253. }
  1254. r_len += 4 * 4;
  1255. } break;
  1256. case Variant::VECTOR3: {
  1257. if (buf) {
  1258. Vector3 v3 = p_variant;
  1259. encode_real(v3.x, &buf[0]);
  1260. encode_real(v3.y, &buf[sizeof(real_t)]);
  1261. encode_real(v3.z, &buf[sizeof(real_t) * 2]);
  1262. }
  1263. r_len += 3 * sizeof(real_t);
  1264. } break;
  1265. case Variant::VECTOR3I: {
  1266. if (buf) {
  1267. Vector3i v3 = p_variant;
  1268. encode_uint32(v3.x, &buf[0]);
  1269. encode_uint32(v3.y, &buf[4]);
  1270. encode_uint32(v3.z, &buf[8]);
  1271. }
  1272. r_len += 3 * 4;
  1273. } break;
  1274. case Variant::TRANSFORM2D: {
  1275. if (buf) {
  1276. Transform2D val = p_variant;
  1277. for (int i = 0; i < 3; i++) {
  1278. for (int j = 0; j < 2; j++) {
  1279. memcpy(&buf[(i * 2 + j) * sizeof(real_t)], &val.columns[i][j], sizeof(real_t));
  1280. }
  1281. }
  1282. }
  1283. r_len += 6 * sizeof(real_t);
  1284. } break;
  1285. case Variant::VECTOR4: {
  1286. if (buf) {
  1287. Vector4 v4 = p_variant;
  1288. encode_real(v4.x, &buf[0]);
  1289. encode_real(v4.y, &buf[sizeof(real_t)]);
  1290. encode_real(v4.z, &buf[sizeof(real_t) * 2]);
  1291. encode_real(v4.w, &buf[sizeof(real_t) * 3]);
  1292. }
  1293. r_len += 4 * sizeof(real_t);
  1294. } break;
  1295. case Variant::VECTOR4I: {
  1296. if (buf) {
  1297. Vector4i v4 = p_variant;
  1298. encode_uint32(v4.x, &buf[0]);
  1299. encode_uint32(v4.y, &buf[4]);
  1300. encode_uint32(v4.z, &buf[8]);
  1301. encode_uint32(v4.w, &buf[12]);
  1302. }
  1303. r_len += 4 * 4;
  1304. } break;
  1305. case Variant::PLANE: {
  1306. if (buf) {
  1307. Plane p = p_variant;
  1308. encode_real(p.normal.x, &buf[0]);
  1309. encode_real(p.normal.y, &buf[sizeof(real_t)]);
  1310. encode_real(p.normal.z, &buf[sizeof(real_t) * 2]);
  1311. encode_real(p.d, &buf[sizeof(real_t) * 3]);
  1312. }
  1313. r_len += 4 * sizeof(real_t);
  1314. } break;
  1315. case Variant::QUATERNION: {
  1316. if (buf) {
  1317. Quaternion q = p_variant;
  1318. encode_real(q.x, &buf[0]);
  1319. encode_real(q.y, &buf[sizeof(real_t)]);
  1320. encode_real(q.z, &buf[sizeof(real_t) * 2]);
  1321. encode_real(q.w, &buf[sizeof(real_t) * 3]);
  1322. }
  1323. r_len += 4 * sizeof(real_t);
  1324. } break;
  1325. case Variant::AABB: {
  1326. if (buf) {
  1327. AABB aabb = p_variant;
  1328. encode_real(aabb.position.x, &buf[0]);
  1329. encode_real(aabb.position.y, &buf[sizeof(real_t)]);
  1330. encode_real(aabb.position.z, &buf[sizeof(real_t) * 2]);
  1331. encode_real(aabb.size.x, &buf[sizeof(real_t) * 3]);
  1332. encode_real(aabb.size.y, &buf[sizeof(real_t) * 4]);
  1333. encode_real(aabb.size.z, &buf[sizeof(real_t) * 5]);
  1334. }
  1335. r_len += 6 * sizeof(real_t);
  1336. } break;
  1337. case Variant::BASIS: {
  1338. if (buf) {
  1339. Basis val = p_variant;
  1340. for (int i = 0; i < 3; i++) {
  1341. for (int j = 0; j < 3; j++) {
  1342. memcpy(&buf[(i * 3 + j) * sizeof(real_t)], &val.rows[i][j], sizeof(real_t));
  1343. }
  1344. }
  1345. }
  1346. r_len += 9 * sizeof(real_t);
  1347. } break;
  1348. case Variant::TRANSFORM3D: {
  1349. if (buf) {
  1350. Transform3D val = p_variant;
  1351. for (int i = 0; i < 3; i++) {
  1352. for (int j = 0; j < 3; j++) {
  1353. memcpy(&buf[(i * 3 + j) * sizeof(real_t)], &val.basis.rows[i][j], sizeof(real_t));
  1354. }
  1355. }
  1356. encode_real(val.origin.x, &buf[sizeof(real_t) * 9]);
  1357. encode_real(val.origin.y, &buf[sizeof(real_t) * 10]);
  1358. encode_real(val.origin.z, &buf[sizeof(real_t) * 11]);
  1359. }
  1360. r_len += 12 * sizeof(real_t);
  1361. } break;
  1362. case Variant::PROJECTION: {
  1363. if (buf) {
  1364. Projection val = p_variant;
  1365. for (int i = 0; i < 4; i++) {
  1366. for (int j = 0; j < 4; j++) {
  1367. memcpy(&buf[(i * 4 + j) * sizeof(real_t)], &val.columns[i][j], sizeof(real_t));
  1368. }
  1369. }
  1370. }
  1371. r_len += 16 * sizeof(real_t);
  1372. } break;
  1373. // misc types
  1374. case Variant::COLOR: {
  1375. if (buf) {
  1376. Color c = p_variant;
  1377. encode_float(c.r, &buf[0]);
  1378. encode_float(c.g, &buf[4]);
  1379. encode_float(c.b, &buf[8]);
  1380. encode_float(c.a, &buf[12]);
  1381. }
  1382. r_len += 4 * 4; // Colors should always be in single-precision.
  1383. } break;
  1384. case Variant::RID: {
  1385. RID rid = p_variant;
  1386. if (buf) {
  1387. encode_uint64(rid.get_id(), buf);
  1388. }
  1389. r_len += 8;
  1390. } break;
  1391. case Variant::OBJECT: {
  1392. if (p_full_objects) {
  1393. Object *obj = p_variant;
  1394. if (!obj) {
  1395. if (buf) {
  1396. encode_uint32(0, buf);
  1397. }
  1398. r_len += 4;
  1399. } else {
  1400. ERR_FAIL_COND_V(!ClassDB::can_instantiate(obj->get_class()), ERR_INVALID_PARAMETER);
  1401. _encode_string(obj->get_class(), buf, r_len);
  1402. List<PropertyInfo> props;
  1403. obj->get_property_list(&props);
  1404. int pc = 0;
  1405. for (const PropertyInfo &E : props) {
  1406. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1407. continue;
  1408. }
  1409. pc++;
  1410. }
  1411. if (buf) {
  1412. encode_uint32(pc, buf);
  1413. buf += 4;
  1414. }
  1415. r_len += 4;
  1416. for (const PropertyInfo &E : props) {
  1417. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1418. continue;
  1419. }
  1420. _encode_string(E.name, buf, r_len);
  1421. Variant value;
  1422. if (E.name == CoreStringName(script)) {
  1423. Ref<Script> script = obj->get_script();
  1424. if (script.is_valid()) {
  1425. String path = script->get_path();
  1426. ERR_FAIL_COND_V_MSG(path.is_empty() || !path.begins_with("res://"), ERR_UNAVAILABLE, "Failed to encode a path to a custom script.");
  1427. value = path;
  1428. }
  1429. } else {
  1430. value = obj->get(E.name);
  1431. }
  1432. int len;
  1433. Error err = encode_variant(value, buf, len, p_full_objects, p_depth + 1);
  1434. ERR_FAIL_COND_V(err, err);
  1435. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1436. r_len += len;
  1437. if (buf) {
  1438. buf += len;
  1439. }
  1440. }
  1441. }
  1442. } else {
  1443. if (buf) {
  1444. Object *obj = p_variant.get_validated_object();
  1445. ObjectID id;
  1446. if (obj) {
  1447. id = obj->get_instance_id();
  1448. }
  1449. encode_uint64(id, buf);
  1450. }
  1451. r_len += 8;
  1452. }
  1453. } break;
  1454. case Variant::CALLABLE: {
  1455. } break;
  1456. case Variant::SIGNAL: {
  1457. Signal signal = p_variant;
  1458. _encode_string(signal.get_name(), buf, r_len);
  1459. if (buf) {
  1460. encode_uint64(signal.get_object_id(), buf);
  1461. }
  1462. r_len += 8;
  1463. } break;
  1464. case Variant::DICTIONARY: {
  1465. Dictionary d = p_variant;
  1466. if (buf) {
  1467. encode_uint32(uint32_t(d.size()), buf);
  1468. buf += 4;
  1469. }
  1470. r_len += 4;
  1471. List<Variant> keys;
  1472. d.get_key_list(&keys);
  1473. for (const Variant &E : keys) {
  1474. int len;
  1475. Error err = encode_variant(E, buf, len, p_full_objects, p_depth + 1);
  1476. ERR_FAIL_COND_V(err, err);
  1477. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1478. r_len += len;
  1479. if (buf) {
  1480. buf += len;
  1481. }
  1482. Variant *v = d.getptr(E);
  1483. ERR_FAIL_NULL_V(v, ERR_BUG);
  1484. err = encode_variant(*v, buf, len, p_full_objects, p_depth + 1);
  1485. ERR_FAIL_COND_V(err, err);
  1486. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1487. r_len += len;
  1488. if (buf) {
  1489. buf += len;
  1490. }
  1491. }
  1492. } break;
  1493. case Variant::ARRAY: {
  1494. Array array = p_variant;
  1495. if (array.is_typed()) {
  1496. Variant variant = array.get_typed_script();
  1497. Ref<Script> script = variant;
  1498. if (script.is_valid()) {
  1499. if (p_full_objects) {
  1500. String path = script->get_path();
  1501. ERR_FAIL_COND_V_MSG(path.is_empty() || !path.begins_with("res://"), ERR_UNAVAILABLE, "Failed to encode a path to a custom script for an array type.");
  1502. _encode_string(path, buf, r_len);
  1503. } else {
  1504. _encode_string(EncodedObjectAsID::get_class_static(), buf, r_len);
  1505. }
  1506. } else if (array.get_typed_class_name() != StringName()) {
  1507. _encode_string(p_full_objects ? array.get_typed_class_name().operator String() : EncodedObjectAsID::get_class_static(), buf, r_len);
  1508. } else {
  1509. // No need to check `p_full_objects` since for `Variant::OBJECT`
  1510. // `array.get_typed_class_name()` should be non-empty.
  1511. if (buf) {
  1512. encode_uint32(array.get_typed_builtin(), buf);
  1513. buf += 4;
  1514. }
  1515. r_len += 4;
  1516. }
  1517. }
  1518. if (buf) {
  1519. encode_uint32(uint32_t(array.size()), buf);
  1520. buf += 4;
  1521. }
  1522. r_len += 4;
  1523. for (const Variant &var : array) {
  1524. int len;
  1525. Error err = encode_variant(var, buf, len, p_full_objects, p_depth + 1);
  1526. ERR_FAIL_COND_V(err, err);
  1527. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1528. if (buf) {
  1529. buf += len;
  1530. }
  1531. r_len += len;
  1532. }
  1533. } break;
  1534. // arrays
  1535. case Variant::PACKED_BYTE_ARRAY: {
  1536. Vector<uint8_t> data = p_variant;
  1537. int datalen = data.size();
  1538. int datasize = sizeof(uint8_t);
  1539. if (buf) {
  1540. encode_uint32(datalen, buf);
  1541. buf += 4;
  1542. const uint8_t *r = data.ptr();
  1543. if (r) {
  1544. memcpy(buf, &r[0], datalen * datasize);
  1545. buf += datalen * datasize;
  1546. }
  1547. }
  1548. r_len += 4 + datalen * datasize;
  1549. while (r_len % 4) {
  1550. r_len++;
  1551. if (buf) {
  1552. *(buf++) = 0;
  1553. }
  1554. }
  1555. } break;
  1556. case Variant::PACKED_INT32_ARRAY: {
  1557. Vector<int32_t> data = p_variant;
  1558. int datalen = data.size();
  1559. int datasize = sizeof(int32_t);
  1560. if (buf) {
  1561. encode_uint32(datalen, buf);
  1562. buf += 4;
  1563. const int32_t *r = data.ptr();
  1564. for (int32_t i = 0; i < datalen; i++) {
  1565. encode_uint32(r[i], &buf[i * datasize]);
  1566. }
  1567. }
  1568. r_len += 4 + datalen * datasize;
  1569. } break;
  1570. case Variant::PACKED_INT64_ARRAY: {
  1571. Vector<int64_t> data = p_variant;
  1572. int datalen = data.size();
  1573. int datasize = sizeof(int64_t);
  1574. if (buf) {
  1575. encode_uint32(datalen, buf);
  1576. buf += 4;
  1577. const int64_t *r = data.ptr();
  1578. for (int64_t i = 0; i < datalen; i++) {
  1579. encode_uint64(r[i], &buf[i * datasize]);
  1580. }
  1581. }
  1582. r_len += 4 + datalen * datasize;
  1583. } break;
  1584. case Variant::PACKED_FLOAT32_ARRAY: {
  1585. Vector<float> data = p_variant;
  1586. int datalen = data.size();
  1587. int datasize = sizeof(float);
  1588. if (buf) {
  1589. encode_uint32(datalen, buf);
  1590. buf += 4;
  1591. const float *r = data.ptr();
  1592. for (int i = 0; i < datalen; i++) {
  1593. encode_float(r[i], &buf[i * datasize]);
  1594. }
  1595. }
  1596. r_len += 4 + datalen * datasize;
  1597. } break;
  1598. case Variant::PACKED_FLOAT64_ARRAY: {
  1599. Vector<double> data = p_variant;
  1600. int datalen = data.size();
  1601. int datasize = sizeof(double);
  1602. if (buf) {
  1603. encode_uint32(datalen, buf);
  1604. buf += 4;
  1605. const double *r = data.ptr();
  1606. for (int i = 0; i < datalen; i++) {
  1607. encode_double(r[i], &buf[i * datasize]);
  1608. }
  1609. }
  1610. r_len += 4 + datalen * datasize;
  1611. } break;
  1612. case Variant::PACKED_STRING_ARRAY: {
  1613. Vector<String> data = p_variant;
  1614. int len = data.size();
  1615. if (buf) {
  1616. encode_uint32(len, buf);
  1617. buf += 4;
  1618. }
  1619. r_len += 4;
  1620. for (int i = 0; i < len; i++) {
  1621. CharString utf8 = data.get(i).utf8();
  1622. if (buf) {
  1623. encode_uint32(utf8.length() + 1, buf);
  1624. buf += 4;
  1625. memcpy(buf, utf8.get_data(), utf8.length() + 1);
  1626. buf += utf8.length() + 1;
  1627. }
  1628. r_len += 4 + utf8.length() + 1;
  1629. while (r_len % 4) {
  1630. r_len++; //pad
  1631. if (buf) {
  1632. *(buf++) = 0;
  1633. }
  1634. }
  1635. }
  1636. } break;
  1637. case Variant::PACKED_VECTOR2_ARRAY: {
  1638. Vector<Vector2> data = p_variant;
  1639. int len = data.size();
  1640. if (buf) {
  1641. encode_uint32(len, buf);
  1642. buf += 4;
  1643. }
  1644. r_len += 4;
  1645. if (buf) {
  1646. for (int i = 0; i < len; i++) {
  1647. Vector2 v = data.get(i);
  1648. encode_real(v.x, &buf[0]);
  1649. encode_real(v.y, &buf[sizeof(real_t)]);
  1650. buf += sizeof(real_t) * 2;
  1651. }
  1652. }
  1653. r_len += sizeof(real_t) * 2 * len;
  1654. } break;
  1655. case Variant::PACKED_VECTOR3_ARRAY: {
  1656. Vector<Vector3> data = p_variant;
  1657. int len = data.size();
  1658. if (buf) {
  1659. encode_uint32(len, buf);
  1660. buf += 4;
  1661. }
  1662. r_len += 4;
  1663. if (buf) {
  1664. for (int i = 0; i < len; i++) {
  1665. Vector3 v = data.get(i);
  1666. encode_real(v.x, &buf[0]);
  1667. encode_real(v.y, &buf[sizeof(real_t)]);
  1668. encode_real(v.z, &buf[sizeof(real_t) * 2]);
  1669. buf += sizeof(real_t) * 3;
  1670. }
  1671. }
  1672. r_len += sizeof(real_t) * 3 * len;
  1673. } break;
  1674. case Variant::PACKED_COLOR_ARRAY: {
  1675. Vector<Color> data = p_variant;
  1676. int len = data.size();
  1677. if (buf) {
  1678. encode_uint32(len, buf);
  1679. buf += 4;
  1680. }
  1681. r_len += 4;
  1682. if (buf) {
  1683. for (int i = 0; i < len; i++) {
  1684. Color c = data.get(i);
  1685. encode_float(c.r, &buf[0]);
  1686. encode_float(c.g, &buf[4]);
  1687. encode_float(c.b, &buf[8]);
  1688. encode_float(c.a, &buf[12]);
  1689. buf += 4 * 4; // Colors should always be in single-precision.
  1690. }
  1691. }
  1692. r_len += 4 * 4 * len;
  1693. } break;
  1694. case Variant::PACKED_VECTOR4_ARRAY: {
  1695. Vector<Vector4> data = p_variant;
  1696. int len = data.size();
  1697. if (buf) {
  1698. encode_uint32(len, buf);
  1699. buf += 4;
  1700. }
  1701. r_len += 4;
  1702. if (buf) {
  1703. for (int i = 0; i < len; i++) {
  1704. Vector4 v = data.get(i);
  1705. encode_real(v.x, &buf[0]);
  1706. encode_real(v.y, &buf[sizeof(real_t)]);
  1707. encode_real(v.z, &buf[sizeof(real_t) * 2]);
  1708. encode_real(v.w, &buf[sizeof(real_t) * 3]);
  1709. buf += sizeof(real_t) * 4;
  1710. }
  1711. }
  1712. r_len += sizeof(real_t) * 4 * len;
  1713. } break;
  1714. default: {
  1715. ERR_FAIL_V(ERR_BUG);
  1716. }
  1717. }
  1718. return OK;
  1719. }
  1720. Vector<float> vector3_to_float32_array(const Vector3 *vecs, size_t count) {
  1721. // We always allocate a new array, and we don't memcpy.
  1722. // We also don't consider returning a pointer to the passed vectors when sizeof(real_t) == 4.
  1723. // One reason is that we could decide to put a 4th component in Vector3 for SIMD/mobile performance,
  1724. // which would cause trouble with these optimizations.
  1725. Vector<float> floats;
  1726. if (count == 0) {
  1727. return floats;
  1728. }
  1729. floats.resize(count * 3);
  1730. float *floats_w = floats.ptrw();
  1731. for (size_t i = 0; i < count; ++i) {
  1732. const Vector3 v = vecs[i];
  1733. floats_w[0] = v.x;
  1734. floats_w[1] = v.y;
  1735. floats_w[2] = v.z;
  1736. floats_w += 3;
  1737. }
  1738. return floats;
  1739. }