marshalls.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. /*************************************************************************/
  2. /* marshalls.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/object/ref_counted.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/string/print_string.h"
  34. #include <limits.h>
  35. #include <stdio.h>
  36. void EncodedObjectAsID::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("set_object_id", "id"), &EncodedObjectAsID::set_object_id);
  38. ClassDB::bind_method(D_METHOD("get_object_id"), &EncodedObjectAsID::get_object_id);
  39. ADD_PROPERTY(PropertyInfo(Variant::INT, "object_id"), "set_object_id", "get_object_id");
  40. }
  41. void EncodedObjectAsID::set_object_id(ObjectID p_id) {
  42. id = p_id;
  43. }
  44. ObjectID EncodedObjectAsID::get_object_id() const {
  45. return id;
  46. }
  47. #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)
  48. #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)
  49. #define ENCODE_MASK 0xFF
  50. #define ENCODE_FLAG_64 1 << 16
  51. #define ENCODE_FLAG_OBJECT_AS_ID 1 << 16
  52. static Error _decode_string(const uint8_t *&buf, int &len, int *r_len, String &r_string) {
  53. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  54. int32_t strlen = decode_uint32(buf);
  55. int32_t pad = 0;
  56. // Handle padding
  57. if (strlen % 4) {
  58. pad = 4 - strlen % 4;
  59. }
  60. buf += 4;
  61. len -= 4;
  62. // Ensure buffer is big enough
  63. ERR_FAIL_ADD_OF(strlen, pad, ERR_FILE_EOF);
  64. ERR_FAIL_COND_V(strlen < 0 || strlen + pad > len, ERR_FILE_EOF);
  65. String str;
  66. ERR_FAIL_COND_V(str.parse_utf8((const char *)buf, strlen), ERR_INVALID_DATA);
  67. r_string = str;
  68. // Add padding
  69. strlen += pad;
  70. // Update buffer pos, left data count, and return size
  71. buf += strlen;
  72. len -= strlen;
  73. if (r_len) {
  74. (*r_len) += 4 + strlen;
  75. }
  76. return OK;
  77. }
  78. Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_objects) {
  79. const uint8_t *buf = p_buffer;
  80. int len = p_len;
  81. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  82. uint32_t type = decode_uint32(buf);
  83. ERR_FAIL_COND_V((type & ENCODE_MASK) >= Variant::VARIANT_MAX, ERR_INVALID_DATA);
  84. buf += 4;
  85. len -= 4;
  86. if (r_len) {
  87. *r_len = 4;
  88. }
  89. // Note: We cannot use sizeof(real_t) for decoding, in case a different size is encoded.
  90. // Decoding math types always checks for the encoded size, while encoding always uses compilation setting.
  91. // This does lead to some code duplication for decoding, but compatibility is the priority.
  92. switch (type & ENCODE_MASK) {
  93. case Variant::NIL: {
  94. r_variant = Variant();
  95. } break;
  96. case Variant::BOOL: {
  97. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  98. bool val = decode_uint32(buf);
  99. r_variant = val;
  100. if (r_len) {
  101. (*r_len) += 4;
  102. }
  103. } break;
  104. case Variant::INT: {
  105. if (type & ENCODE_FLAG_64) {
  106. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  107. int64_t val = decode_uint64(buf);
  108. r_variant = val;
  109. if (r_len) {
  110. (*r_len) += 8;
  111. }
  112. } else {
  113. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  114. int32_t val = decode_uint32(buf);
  115. r_variant = val;
  116. if (r_len) {
  117. (*r_len) += 4;
  118. }
  119. }
  120. } break;
  121. case Variant::FLOAT: {
  122. if (type & ENCODE_FLAG_64) {
  123. ERR_FAIL_COND_V((size_t)len < sizeof(double), ERR_INVALID_DATA);
  124. double val = decode_double(buf);
  125. r_variant = val;
  126. if (r_len) {
  127. (*r_len) += sizeof(double);
  128. }
  129. } else {
  130. ERR_FAIL_COND_V((size_t)len < sizeof(float), ERR_INVALID_DATA);
  131. float val = decode_float(buf);
  132. r_variant = val;
  133. if (r_len) {
  134. (*r_len) += sizeof(float);
  135. }
  136. }
  137. } break;
  138. case Variant::STRING: {
  139. String str;
  140. Error err = _decode_string(buf, len, r_len, str);
  141. if (err) {
  142. return err;
  143. }
  144. r_variant = str;
  145. } break;
  146. // math types
  147. case Variant::VECTOR2: {
  148. Vector2 val;
  149. if (type & ENCODE_FLAG_64) {
  150. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 2, ERR_INVALID_DATA);
  151. val.x = decode_double(&buf[0]);
  152. val.y = decode_double(&buf[sizeof(double)]);
  153. if (r_len) {
  154. (*r_len) += sizeof(double) * 2;
  155. }
  156. } else {
  157. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 2, ERR_INVALID_DATA);
  158. val.x = decode_float(&buf[0]);
  159. val.y = decode_float(&buf[sizeof(float)]);
  160. if (r_len) {
  161. (*r_len) += sizeof(float) * 2;
  162. }
  163. }
  164. r_variant = val;
  165. } break;
  166. case Variant::VECTOR2I: {
  167. ERR_FAIL_COND_V(len < 4 * 2, ERR_INVALID_DATA);
  168. Vector2i val;
  169. val.x = decode_uint32(&buf[0]);
  170. val.y = decode_uint32(&buf[4]);
  171. r_variant = val;
  172. if (r_len) {
  173. (*r_len) += 4 * 2;
  174. }
  175. } break;
  176. case Variant::RECT2: {
  177. Rect2 val;
  178. if (type & ENCODE_FLAG_64) {
  179. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  180. val.position.x = decode_double(&buf[0]);
  181. val.position.y = decode_double(&buf[sizeof(double)]);
  182. val.size.x = decode_double(&buf[sizeof(double) * 2]);
  183. val.size.y = decode_double(&buf[sizeof(double) * 3]);
  184. if (r_len) {
  185. (*r_len) += sizeof(double) * 4;
  186. }
  187. } else {
  188. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  189. val.position.x = decode_float(&buf[0]);
  190. val.position.y = decode_float(&buf[sizeof(float)]);
  191. val.size.x = decode_float(&buf[sizeof(float) * 2]);
  192. val.size.y = decode_float(&buf[sizeof(float) * 3]);
  193. if (r_len) {
  194. (*r_len) += sizeof(float) * 4;
  195. }
  196. }
  197. r_variant = val;
  198. } break;
  199. case Variant::RECT2I: {
  200. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  201. Rect2i val;
  202. val.position.x = decode_uint32(&buf[0]);
  203. val.position.y = decode_uint32(&buf[4]);
  204. val.size.x = decode_uint32(&buf[8]);
  205. val.size.y = decode_uint32(&buf[12]);
  206. r_variant = val;
  207. if (r_len) {
  208. (*r_len) += 4 * 4;
  209. }
  210. } break;
  211. case Variant::VECTOR3: {
  212. Vector3 val;
  213. if (type & ENCODE_FLAG_64) {
  214. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 3, ERR_INVALID_DATA);
  215. val.x = decode_double(&buf[0]);
  216. val.y = decode_double(&buf[sizeof(double)]);
  217. val.z = decode_double(&buf[sizeof(double) * 2]);
  218. if (r_len) {
  219. (*r_len) += sizeof(double) * 3;
  220. }
  221. } else {
  222. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 3, ERR_INVALID_DATA);
  223. val.x = decode_float(&buf[0]);
  224. val.y = decode_float(&buf[sizeof(float)]);
  225. val.z = decode_float(&buf[sizeof(float) * 2]);
  226. if (r_len) {
  227. (*r_len) += sizeof(float) * 3;
  228. }
  229. }
  230. r_variant = val;
  231. } break;
  232. case Variant::VECTOR3I: {
  233. ERR_FAIL_COND_V(len < 4 * 3, ERR_INVALID_DATA);
  234. Vector3i val;
  235. val.x = decode_uint32(&buf[0]);
  236. val.y = decode_uint32(&buf[4]);
  237. val.z = decode_uint32(&buf[8]);
  238. r_variant = val;
  239. if (r_len) {
  240. (*r_len) += 4 * 3;
  241. }
  242. } break;
  243. case Variant::TRANSFORM2D: {
  244. Transform2D val;
  245. if (type & ENCODE_FLAG_64) {
  246. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 6, ERR_INVALID_DATA);
  247. for (int i = 0; i < 3; i++) {
  248. for (int j = 0; j < 2; j++) {
  249. val.elements[i][j] = decode_double(&buf[(i * 2 + j) * sizeof(double)]);
  250. }
  251. }
  252. if (r_len) {
  253. (*r_len) += sizeof(double) * 6;
  254. }
  255. } else {
  256. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 6, ERR_INVALID_DATA);
  257. for (int i = 0; i < 3; i++) {
  258. for (int j = 0; j < 2; j++) {
  259. val.elements[i][j] = decode_float(&buf[(i * 2 + j) * sizeof(float)]);
  260. }
  261. }
  262. if (r_len) {
  263. (*r_len) += sizeof(float) * 6;
  264. }
  265. }
  266. r_variant = val;
  267. } break;
  268. case Variant::PLANE: {
  269. Plane val;
  270. if (type & ENCODE_FLAG_64) {
  271. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  272. val.normal.x = decode_double(&buf[0]);
  273. val.normal.y = decode_double(&buf[sizeof(double)]);
  274. val.normal.z = decode_double(&buf[sizeof(double) * 2]);
  275. val.d = decode_double(&buf[sizeof(double) * 3]);
  276. if (r_len) {
  277. (*r_len) += sizeof(double) * 4;
  278. }
  279. } else {
  280. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  281. val.normal.x = decode_float(&buf[0]);
  282. val.normal.y = decode_float(&buf[sizeof(float)]);
  283. val.normal.z = decode_float(&buf[sizeof(float) * 2]);
  284. val.d = decode_float(&buf[sizeof(float) * 3]);
  285. if (r_len) {
  286. (*r_len) += sizeof(float) * 4;
  287. }
  288. }
  289. r_variant = val;
  290. } break;
  291. case Variant::QUATERNION: {
  292. Quaternion val;
  293. if (type & ENCODE_FLAG_64) {
  294. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  295. val.x = decode_double(&buf[0]);
  296. val.y = decode_double(&buf[sizeof(double)]);
  297. val.z = decode_double(&buf[sizeof(double) * 2]);
  298. val.w = decode_double(&buf[sizeof(double) * 3]);
  299. if (r_len) {
  300. (*r_len) += sizeof(double) * 4;
  301. }
  302. } else {
  303. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  304. val.x = decode_float(&buf[0]);
  305. val.y = decode_float(&buf[sizeof(float)]);
  306. val.z = decode_float(&buf[sizeof(float) * 2]);
  307. val.w = decode_float(&buf[sizeof(float) * 3]);
  308. if (r_len) {
  309. (*r_len) += sizeof(float) * 4;
  310. }
  311. }
  312. r_variant = val;
  313. } break;
  314. case Variant::AABB: {
  315. AABB val;
  316. if (type & ENCODE_FLAG_64) {
  317. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 6, ERR_INVALID_DATA);
  318. val.position.x = decode_double(&buf[0]);
  319. val.position.y = decode_double(&buf[sizeof(double)]);
  320. val.position.z = decode_double(&buf[sizeof(double) * 2]);
  321. val.size.x = decode_double(&buf[sizeof(double) * 3]);
  322. val.size.y = decode_double(&buf[sizeof(double) * 4]);
  323. val.size.z = decode_double(&buf[sizeof(double) * 5]);
  324. if (r_len) {
  325. (*r_len) += sizeof(double) * 6;
  326. }
  327. } else {
  328. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 6, ERR_INVALID_DATA);
  329. val.position.x = decode_float(&buf[0]);
  330. val.position.y = decode_float(&buf[sizeof(float)]);
  331. val.position.z = decode_float(&buf[sizeof(float) * 2]);
  332. val.size.x = decode_float(&buf[sizeof(float) * 3]);
  333. val.size.y = decode_float(&buf[sizeof(float) * 4]);
  334. val.size.z = decode_float(&buf[sizeof(float) * 5]);
  335. if (r_len) {
  336. (*r_len) += sizeof(float) * 6;
  337. }
  338. }
  339. r_variant = val;
  340. } break;
  341. case Variant::BASIS: {
  342. Basis val;
  343. if (type & ENCODE_FLAG_64) {
  344. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 9, ERR_INVALID_DATA);
  345. for (int i = 0; i < 3; i++) {
  346. for (int j = 0; j < 3; j++) {
  347. val.elements[i][j] = decode_double(&buf[(i * 3 + j) * sizeof(double)]);
  348. }
  349. }
  350. if (r_len) {
  351. (*r_len) += sizeof(double) * 9;
  352. }
  353. } else {
  354. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 9, ERR_INVALID_DATA);
  355. for (int i = 0; i < 3; i++) {
  356. for (int j = 0; j < 3; j++) {
  357. val.elements[i][j] = decode_float(&buf[(i * 3 + j) * sizeof(float)]);
  358. }
  359. }
  360. if (r_len) {
  361. (*r_len) += sizeof(float) * 9;
  362. }
  363. }
  364. r_variant = val;
  365. } break;
  366. case Variant::TRANSFORM3D: {
  367. Transform3D val;
  368. if (type & ENCODE_FLAG_64) {
  369. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 12, ERR_INVALID_DATA);
  370. for (int i = 0; i < 3; i++) {
  371. for (int j = 0; j < 3; j++) {
  372. val.basis.elements[i][j] = decode_double(&buf[(i * 3 + j) * sizeof(double)]);
  373. }
  374. }
  375. val.origin[0] = decode_double(&buf[sizeof(double) * 9]);
  376. val.origin[1] = decode_double(&buf[sizeof(double) * 10]);
  377. val.origin[2] = decode_double(&buf[sizeof(double) * 11]);
  378. if (r_len) {
  379. (*r_len) += sizeof(double) * 12;
  380. }
  381. } else {
  382. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 12, ERR_INVALID_DATA);
  383. for (int i = 0; i < 3; i++) {
  384. for (int j = 0; j < 3; j++) {
  385. val.basis.elements[i][j] = decode_float(&buf[(i * 3 + j) * sizeof(float)]);
  386. }
  387. }
  388. val.origin[0] = decode_float(&buf[sizeof(float) * 9]);
  389. val.origin[1] = decode_float(&buf[sizeof(float) * 10]);
  390. val.origin[2] = decode_float(&buf[sizeof(float) * 11]);
  391. if (r_len) {
  392. (*r_len) += sizeof(float) * 12;
  393. }
  394. }
  395. r_variant = val;
  396. } break;
  397. // misc types
  398. case Variant::COLOR: {
  399. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  400. Color val;
  401. val.r = decode_float(&buf[0]);
  402. val.g = decode_float(&buf[4]);
  403. val.b = decode_float(&buf[8]);
  404. val.a = decode_float(&buf[12]);
  405. r_variant = val;
  406. if (r_len) {
  407. (*r_len) += 4 * 4; // Colors should always be in single-precision.
  408. }
  409. } break;
  410. case Variant::STRING_NAME: {
  411. String str;
  412. Error err = _decode_string(buf, len, r_len, str);
  413. if (err) {
  414. return err;
  415. }
  416. r_variant = StringName(str);
  417. } break;
  418. case Variant::NODE_PATH: {
  419. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  420. int32_t strlen = decode_uint32(buf);
  421. if (strlen & 0x80000000) {
  422. //new format
  423. ERR_FAIL_COND_V(len < 12, ERR_INVALID_DATA);
  424. Vector<StringName> names;
  425. Vector<StringName> subnames;
  426. uint32_t namecount = strlen &= 0x7FFFFFFF;
  427. uint32_t subnamecount = decode_uint32(buf + 4);
  428. uint32_t flags = decode_uint32(buf + 8);
  429. len -= 12;
  430. buf += 12;
  431. if (flags & 2) { // Obsolete format with property separate from subpath
  432. subnamecount++;
  433. }
  434. uint32_t total = namecount + subnamecount;
  435. if (r_len) {
  436. (*r_len) += 12;
  437. }
  438. for (uint32_t i = 0; i < total; i++) {
  439. String str;
  440. Error err = _decode_string(buf, len, r_len, str);
  441. if (err) {
  442. return err;
  443. }
  444. if (i < namecount) {
  445. names.push_back(str);
  446. } else {
  447. subnames.push_back(str);
  448. }
  449. }
  450. r_variant = NodePath(names, subnames, flags & 1);
  451. } else {
  452. //old format, just a string
  453. ERR_FAIL_V(ERR_INVALID_DATA);
  454. }
  455. } break;
  456. case Variant::RID: {
  457. r_variant = RID();
  458. } break;
  459. case Variant::OBJECT: {
  460. if (type & ENCODE_FLAG_OBJECT_AS_ID) {
  461. //this _is_ allowed
  462. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  463. ObjectID val = ObjectID(decode_uint64(buf));
  464. if (r_len) {
  465. (*r_len) += 8;
  466. }
  467. if (val.is_null()) {
  468. r_variant = (Object *)nullptr;
  469. } else {
  470. Ref<EncodedObjectAsID> obj_as_id;
  471. obj_as_id.instantiate();
  472. obj_as_id->set_object_id(val);
  473. r_variant = obj_as_id;
  474. }
  475. } else {
  476. ERR_FAIL_COND_V(!p_allow_objects, ERR_UNAUTHORIZED);
  477. String str;
  478. Error err = _decode_string(buf, len, r_len, str);
  479. if (err) {
  480. return err;
  481. }
  482. if (str.is_empty()) {
  483. r_variant = (Object *)nullptr;
  484. } else {
  485. Object *obj = ClassDB::instantiate(str);
  486. ERR_FAIL_COND_V(!obj, ERR_UNAVAILABLE);
  487. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  488. int32_t count = decode_uint32(buf);
  489. buf += 4;
  490. len -= 4;
  491. if (r_len) {
  492. (*r_len) += 4; // Size of count number.
  493. }
  494. for (int i = 0; i < count; i++) {
  495. str = String();
  496. err = _decode_string(buf, len, r_len, str);
  497. if (err) {
  498. return err;
  499. }
  500. Variant value;
  501. int used;
  502. err = decode_variant(value, buf, len, &used, p_allow_objects);
  503. if (err) {
  504. return err;
  505. }
  506. buf += used;
  507. len -= used;
  508. if (r_len) {
  509. (*r_len) += used;
  510. }
  511. obj->set(str, value);
  512. }
  513. if (Object::cast_to<RefCounted>(obj)) {
  514. REF ref = REF(Object::cast_to<RefCounted>(obj));
  515. r_variant = ref;
  516. } else {
  517. r_variant = obj;
  518. }
  519. }
  520. }
  521. } break;
  522. case Variant::CALLABLE: {
  523. r_variant = Callable();
  524. } break;
  525. case Variant::SIGNAL: {
  526. r_variant = Signal();
  527. } break;
  528. case Variant::DICTIONARY: {
  529. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  530. int32_t count = decode_uint32(buf);
  531. // bool shared = count&0x80000000;
  532. count &= 0x7FFFFFFF;
  533. buf += 4;
  534. len -= 4;
  535. if (r_len) {
  536. (*r_len) += 4; // Size of count number.
  537. }
  538. Dictionary d;
  539. for (int i = 0; i < count; i++) {
  540. Variant key, value;
  541. int used;
  542. Error err = decode_variant(key, buf, len, &used, p_allow_objects);
  543. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant.");
  544. buf += used;
  545. len -= used;
  546. if (r_len) {
  547. (*r_len) += used;
  548. }
  549. err = decode_variant(value, buf, len, &used, p_allow_objects);
  550. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant.");
  551. buf += used;
  552. len -= used;
  553. if (r_len) {
  554. (*r_len) += used;
  555. }
  556. d[key] = value;
  557. }
  558. r_variant = d;
  559. } break;
  560. case Variant::ARRAY: {
  561. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  562. int32_t count = decode_uint32(buf);
  563. // bool shared = count&0x80000000;
  564. count &= 0x7FFFFFFF;
  565. buf += 4;
  566. len -= 4;
  567. if (r_len) {
  568. (*r_len) += 4; // Size of count number.
  569. }
  570. Array varr;
  571. for (int i = 0; i < count; i++) {
  572. int used = 0;
  573. Variant v;
  574. Error err = decode_variant(v, buf, len, &used, p_allow_objects);
  575. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant.");
  576. buf += used;
  577. len -= used;
  578. varr.push_back(v);
  579. if (r_len) {
  580. (*r_len) += used;
  581. }
  582. }
  583. r_variant = varr;
  584. } break;
  585. // arrays
  586. case Variant::PACKED_BYTE_ARRAY: {
  587. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  588. int32_t count = decode_uint32(buf);
  589. buf += 4;
  590. len -= 4;
  591. ERR_FAIL_COND_V(count < 0 || count > len, ERR_INVALID_DATA);
  592. Vector<uint8_t> data;
  593. if (count) {
  594. data.resize(count);
  595. uint8_t *w = data.ptrw();
  596. for (int32_t i = 0; i < count; i++) {
  597. w[i] = buf[i];
  598. }
  599. }
  600. r_variant = data;
  601. if (r_len) {
  602. if (count % 4) {
  603. (*r_len) += 4 - count % 4;
  604. }
  605. (*r_len) += 4 + count;
  606. }
  607. } break;
  608. case Variant::PACKED_INT32_ARRAY: {
  609. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  610. int32_t count = decode_uint32(buf);
  611. buf += 4;
  612. len -= 4;
  613. ERR_FAIL_MUL_OF(count, 4, ERR_INVALID_DATA);
  614. ERR_FAIL_COND_V(count < 0 || count * 4 > len, ERR_INVALID_DATA);
  615. Vector<int32_t> data;
  616. if (count) {
  617. //const int*rbuf=(const int*)buf;
  618. data.resize(count);
  619. int32_t *w = data.ptrw();
  620. for (int32_t i = 0; i < count; i++) {
  621. w[i] = decode_uint32(&buf[i * 4]);
  622. }
  623. }
  624. r_variant = Variant(data);
  625. if (r_len) {
  626. (*r_len) += 4 + count * sizeof(int32_t);
  627. }
  628. } break;
  629. case Variant::PACKED_INT64_ARRAY: {
  630. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  631. int32_t count = decode_uint32(buf);
  632. buf += 4;
  633. len -= 4;
  634. ERR_FAIL_MUL_OF(count, 8, ERR_INVALID_DATA);
  635. ERR_FAIL_COND_V(count < 0 || count * 8 > len, ERR_INVALID_DATA);
  636. Vector<int64_t> data;
  637. if (count) {
  638. //const int*rbuf=(const int*)buf;
  639. data.resize(count);
  640. int64_t *w = data.ptrw();
  641. for (int64_t i = 0; i < count; i++) {
  642. w[i] = decode_uint64(&buf[i * 8]);
  643. }
  644. }
  645. r_variant = Variant(data);
  646. if (r_len) {
  647. (*r_len) += 4 + count * sizeof(int64_t);
  648. }
  649. } break;
  650. case Variant::PACKED_FLOAT32_ARRAY: {
  651. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  652. int32_t count = decode_uint32(buf);
  653. buf += 4;
  654. len -= 4;
  655. ERR_FAIL_MUL_OF(count, 4, ERR_INVALID_DATA);
  656. ERR_FAIL_COND_V(count < 0 || count * 4 > len, ERR_INVALID_DATA);
  657. Vector<float> data;
  658. if (count) {
  659. //const float*rbuf=(const float*)buf;
  660. data.resize(count);
  661. float *w = data.ptrw();
  662. for (int32_t i = 0; i < count; i++) {
  663. w[i] = decode_float(&buf[i * 4]);
  664. }
  665. }
  666. r_variant = data;
  667. if (r_len) {
  668. (*r_len) += 4 + count * sizeof(float);
  669. }
  670. } break;
  671. case Variant::PACKED_FLOAT64_ARRAY: {
  672. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  673. int32_t count = decode_uint32(buf);
  674. buf += 4;
  675. len -= 4;
  676. ERR_FAIL_MUL_OF(count, 8, ERR_INVALID_DATA);
  677. ERR_FAIL_COND_V(count < 0 || count * 8 > len, ERR_INVALID_DATA);
  678. Vector<double> data;
  679. if (count) {
  680. data.resize(count);
  681. double *w = data.ptrw();
  682. for (int64_t i = 0; i < count; i++) {
  683. w[i] = decode_double(&buf[i * 8]);
  684. }
  685. }
  686. r_variant = data;
  687. if (r_len) {
  688. (*r_len) += 4 + count * sizeof(double);
  689. }
  690. } break;
  691. case Variant::PACKED_STRING_ARRAY: {
  692. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  693. int32_t count = decode_uint32(buf);
  694. Vector<String> strings;
  695. buf += 4;
  696. len -= 4;
  697. if (r_len) {
  698. (*r_len) += 4; // Size of count number.
  699. }
  700. for (int32_t i = 0; i < count; i++) {
  701. String str;
  702. Error err = _decode_string(buf, len, r_len, str);
  703. if (err) {
  704. return err;
  705. }
  706. strings.push_back(str);
  707. }
  708. r_variant = strings;
  709. } break;
  710. case Variant::PACKED_VECTOR2_ARRAY: {
  711. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  712. int32_t count = decode_uint32(buf);
  713. buf += 4;
  714. len -= 4;
  715. Vector<Vector2> varray;
  716. if (type & ENCODE_FLAG_64) {
  717. ERR_FAIL_MUL_OF(count, sizeof(double) * 2, ERR_INVALID_DATA);
  718. ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 2 > (size_t)len, ERR_INVALID_DATA);
  719. if (r_len) {
  720. (*r_len) += 4; // Size of count number.
  721. }
  722. if (count) {
  723. varray.resize(count);
  724. Vector2 *w = varray.ptrw();
  725. for (int32_t i = 0; i < count; i++) {
  726. w[i].x = decode_double(buf + i * sizeof(double) * 2 + sizeof(double) * 0);
  727. w[i].y = decode_double(buf + i * sizeof(double) * 2 + sizeof(double) * 1);
  728. }
  729. int adv = sizeof(double) * 2 * count;
  730. if (r_len) {
  731. (*r_len) += adv;
  732. }
  733. len -= adv;
  734. buf += adv;
  735. }
  736. } else {
  737. ERR_FAIL_MUL_OF(count, sizeof(float) * 2, ERR_INVALID_DATA);
  738. ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 2 > (size_t)len, ERR_INVALID_DATA);
  739. if (r_len) {
  740. (*r_len) += 4; // Size of count number.
  741. }
  742. if (count) {
  743. varray.resize(count);
  744. Vector2 *w = varray.ptrw();
  745. for (int32_t i = 0; i < count; i++) {
  746. w[i].x = decode_float(buf + i * sizeof(float) * 2 + sizeof(float) * 0);
  747. w[i].y = decode_float(buf + i * sizeof(float) * 2 + sizeof(float) * 1);
  748. }
  749. int adv = sizeof(float) * 2 * count;
  750. if (r_len) {
  751. (*r_len) += adv;
  752. }
  753. }
  754. }
  755. r_variant = varray;
  756. } break;
  757. case Variant::PACKED_VECTOR3_ARRAY: {
  758. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  759. int32_t count = decode_uint32(buf);
  760. buf += 4;
  761. len -= 4;
  762. Vector<Vector3> varray;
  763. if (type & ENCODE_FLAG_64) {
  764. ERR_FAIL_MUL_OF(count, sizeof(double) * 3, ERR_INVALID_DATA);
  765. ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 3 > (size_t)len, ERR_INVALID_DATA);
  766. if (r_len) {
  767. (*r_len) += 4; // Size of count number.
  768. }
  769. if (count) {
  770. varray.resize(count);
  771. Vector3 *w = varray.ptrw();
  772. for (int32_t i = 0; i < count; i++) {
  773. w[i].x = decode_double(buf + i * sizeof(double) * 3 + sizeof(double) * 0);
  774. w[i].y = decode_double(buf + i * sizeof(double) * 3 + sizeof(double) * 1);
  775. w[i].z = decode_double(buf + i * sizeof(double) * 3 + sizeof(double) * 2);
  776. }
  777. int adv = sizeof(double) * 3 * count;
  778. if (r_len) {
  779. (*r_len) += adv;
  780. }
  781. len -= adv;
  782. buf += adv;
  783. }
  784. } else {
  785. ERR_FAIL_MUL_OF(count, sizeof(float) * 3, ERR_INVALID_DATA);
  786. ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 3 > (size_t)len, ERR_INVALID_DATA);
  787. if (r_len) {
  788. (*r_len) += 4; // Size of count number.
  789. }
  790. if (count) {
  791. varray.resize(count);
  792. Vector3 *w = varray.ptrw();
  793. for (int32_t i = 0; i < count; i++) {
  794. w[i].x = decode_float(buf + i * sizeof(float) * 3 + sizeof(float) * 0);
  795. w[i].y = decode_float(buf + i * sizeof(float) * 3 + sizeof(float) * 1);
  796. w[i].z = decode_float(buf + i * sizeof(float) * 3 + sizeof(float) * 2);
  797. }
  798. int adv = sizeof(float) * 3 * count;
  799. if (r_len) {
  800. (*r_len) += adv;
  801. }
  802. len -= adv;
  803. buf += adv;
  804. }
  805. }
  806. r_variant = varray;
  807. } break;
  808. case Variant::PACKED_COLOR_ARRAY: {
  809. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  810. int32_t count = decode_uint32(buf);
  811. buf += 4;
  812. len -= 4;
  813. ERR_FAIL_MUL_OF(count, 4 * 4, ERR_INVALID_DATA);
  814. ERR_FAIL_COND_V(count < 0 || count * 4 * 4 > len, ERR_INVALID_DATA);
  815. Vector<Color> carray;
  816. if (r_len) {
  817. (*r_len) += 4; // Size of count number.
  818. }
  819. if (count) {
  820. carray.resize(count);
  821. Color *w = carray.ptrw();
  822. for (int32_t i = 0; i < count; i++) {
  823. // Colors should always be in single-precision.
  824. w[i].r = decode_float(buf + i * 4 * 4 + 4 * 0);
  825. w[i].g = decode_float(buf + i * 4 * 4 + 4 * 1);
  826. w[i].b = decode_float(buf + i * 4 * 4 + 4 * 2);
  827. w[i].a = decode_float(buf + i * 4 * 4 + 4 * 3);
  828. }
  829. int adv = 4 * 4 * count;
  830. if (r_len) {
  831. (*r_len) += adv;
  832. }
  833. }
  834. r_variant = carray;
  835. } break;
  836. default: {
  837. ERR_FAIL_V(ERR_BUG);
  838. }
  839. }
  840. return OK;
  841. }
  842. static void _encode_string(const String &p_string, uint8_t *&buf, int &r_len) {
  843. CharString utf8 = p_string.utf8();
  844. if (buf) {
  845. encode_uint32(utf8.length(), buf);
  846. buf += 4;
  847. memcpy(buf, utf8.get_data(), utf8.length());
  848. buf += utf8.length();
  849. }
  850. r_len += 4 + utf8.length();
  851. while (r_len % 4) {
  852. r_len++; //pad
  853. if (buf) {
  854. *(buf++) = 0;
  855. }
  856. }
  857. }
  858. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects, int p_depth) {
  859. ERR_FAIL_COND_V_MSG(p_depth > Variant::MAX_RECURSION_DEPTH, ERR_OUT_OF_MEMORY, "Potential inifite recursion detected. Bailing.");
  860. uint8_t *buf = r_buffer;
  861. r_len = 0;
  862. uint32_t flags = 0;
  863. switch (p_variant.get_type()) {
  864. case Variant::INT: {
  865. int64_t val = p_variant;
  866. if (val > (int64_t)INT_MAX || val < (int64_t)INT_MIN) {
  867. flags |= ENCODE_FLAG_64;
  868. }
  869. } break;
  870. case Variant::FLOAT: {
  871. double d = p_variant;
  872. float f = d;
  873. if (double(f) != d) {
  874. flags |= ENCODE_FLAG_64;
  875. }
  876. } break;
  877. case Variant::OBJECT: {
  878. // Test for potential wrong values sent by the debugger when it breaks.
  879. Object *obj = p_variant.get_validated_object();
  880. if (!obj) {
  881. // Object is invalid, send a nullptr instead.
  882. if (buf) {
  883. encode_uint32(Variant::NIL, buf);
  884. }
  885. r_len += 4;
  886. return OK;
  887. }
  888. if (!p_full_objects) {
  889. flags |= ENCODE_FLAG_OBJECT_AS_ID;
  890. }
  891. } break;
  892. default: {
  893. } // nothing to do at this stage
  894. }
  895. if (buf) {
  896. encode_uint32(p_variant.get_type() | flags, buf);
  897. buf += 4;
  898. }
  899. r_len += 4;
  900. switch (p_variant.get_type()) {
  901. case Variant::NIL: {
  902. //nothing to do
  903. } break;
  904. case Variant::BOOL: {
  905. if (buf) {
  906. encode_uint32(p_variant.operator bool(), buf);
  907. }
  908. r_len += 4;
  909. } break;
  910. case Variant::INT: {
  911. if (flags & ENCODE_FLAG_64) {
  912. //64 bits
  913. if (buf) {
  914. encode_uint64(p_variant.operator int64_t(), buf);
  915. }
  916. r_len += 8;
  917. } else {
  918. if (buf) {
  919. encode_uint32(p_variant.operator int32_t(), buf);
  920. }
  921. r_len += 4;
  922. }
  923. } break;
  924. case Variant::FLOAT: {
  925. if (flags & ENCODE_FLAG_64) {
  926. if (buf) {
  927. encode_double(p_variant.operator double(), buf);
  928. }
  929. r_len += 8;
  930. } else {
  931. if (buf) {
  932. encode_float(p_variant.operator float(), buf);
  933. }
  934. r_len += 4;
  935. }
  936. } break;
  937. case Variant::NODE_PATH: {
  938. NodePath np = p_variant;
  939. if (buf) {
  940. encode_uint32(uint32_t(np.get_name_count()) | 0x80000000, buf); //for compatibility with the old format
  941. encode_uint32(np.get_subname_count(), buf + 4);
  942. uint32_t np_flags = 0;
  943. if (np.is_absolute()) {
  944. np_flags |= 1;
  945. }
  946. encode_uint32(np_flags, buf + 8);
  947. buf += 12;
  948. }
  949. r_len += 12;
  950. int total = np.get_name_count() + np.get_subname_count();
  951. for (int i = 0; i < total; i++) {
  952. String str;
  953. if (i < np.get_name_count()) {
  954. str = np.get_name(i);
  955. } else {
  956. str = np.get_subname(i - np.get_name_count());
  957. }
  958. CharString utf8 = str.utf8();
  959. int pad = 0;
  960. if (utf8.length() % 4) {
  961. pad = 4 - utf8.length() % 4;
  962. }
  963. if (buf) {
  964. encode_uint32(utf8.length(), buf);
  965. buf += 4;
  966. memcpy(buf, utf8.get_data(), utf8.length());
  967. buf += pad + utf8.length();
  968. }
  969. r_len += 4 + utf8.length() + pad;
  970. }
  971. } break;
  972. case Variant::STRING:
  973. case Variant::STRING_NAME: {
  974. _encode_string(p_variant, buf, r_len);
  975. } break;
  976. // math types
  977. case Variant::VECTOR2: {
  978. if (buf) {
  979. Vector2 v2 = p_variant;
  980. encode_real(v2.x, &buf[0]);
  981. encode_real(v2.y, &buf[sizeof(real_t)]);
  982. }
  983. r_len += 2 * sizeof(real_t);
  984. } break;
  985. case Variant::VECTOR2I: {
  986. if (buf) {
  987. Vector2i v2 = p_variant;
  988. encode_uint32(v2.x, &buf[0]);
  989. encode_uint32(v2.y, &buf[4]);
  990. }
  991. r_len += 2 * 4;
  992. } break;
  993. case Variant::RECT2: {
  994. if (buf) {
  995. Rect2 r2 = p_variant;
  996. encode_real(r2.position.x, &buf[0]);
  997. encode_real(r2.position.y, &buf[sizeof(real_t)]);
  998. encode_real(r2.size.x, &buf[sizeof(real_t) * 2]);
  999. encode_real(r2.size.y, &buf[sizeof(real_t) * 3]);
  1000. }
  1001. r_len += 4 * sizeof(real_t);
  1002. } break;
  1003. case Variant::RECT2I: {
  1004. if (buf) {
  1005. Rect2i r2 = p_variant;
  1006. encode_uint32(r2.position.x, &buf[0]);
  1007. encode_uint32(r2.position.y, &buf[4]);
  1008. encode_uint32(r2.size.x, &buf[8]);
  1009. encode_uint32(r2.size.y, &buf[12]);
  1010. }
  1011. r_len += 4 * 4;
  1012. } break;
  1013. case Variant::VECTOR3: {
  1014. if (buf) {
  1015. Vector3 v3 = p_variant;
  1016. encode_real(v3.x, &buf[0]);
  1017. encode_real(v3.y, &buf[sizeof(real_t)]);
  1018. encode_real(v3.z, &buf[sizeof(real_t) * 2]);
  1019. }
  1020. r_len += 3 * sizeof(real_t);
  1021. } break;
  1022. case Variant::VECTOR3I: {
  1023. if (buf) {
  1024. Vector3i v3 = p_variant;
  1025. encode_uint32(v3.x, &buf[0]);
  1026. encode_uint32(v3.y, &buf[4]);
  1027. encode_uint32(v3.z, &buf[8]);
  1028. }
  1029. r_len += 3 * 4;
  1030. } break;
  1031. case Variant::TRANSFORM2D: {
  1032. if (buf) {
  1033. Transform2D val = p_variant;
  1034. for (int i = 0; i < 3; i++) {
  1035. for (int j = 0; j < 2; j++) {
  1036. memcpy(&buf[(i * 2 + j) * sizeof(real_t)], &val.elements[i][j], sizeof(real_t));
  1037. }
  1038. }
  1039. }
  1040. r_len += 6 * sizeof(real_t);
  1041. } break;
  1042. case Variant::PLANE: {
  1043. if (buf) {
  1044. Plane p = p_variant;
  1045. encode_real(p.normal.x, &buf[0]);
  1046. encode_real(p.normal.y, &buf[sizeof(real_t)]);
  1047. encode_real(p.normal.z, &buf[sizeof(real_t) * 2]);
  1048. encode_real(p.d, &buf[sizeof(real_t) * 3]);
  1049. }
  1050. r_len += 4 * sizeof(real_t);
  1051. } break;
  1052. case Variant::QUATERNION: {
  1053. if (buf) {
  1054. Quaternion q = p_variant;
  1055. encode_real(q.x, &buf[0]);
  1056. encode_real(q.y, &buf[sizeof(real_t)]);
  1057. encode_real(q.z, &buf[sizeof(real_t) * 2]);
  1058. encode_real(q.w, &buf[sizeof(real_t) * 3]);
  1059. }
  1060. r_len += 4 * sizeof(real_t);
  1061. } break;
  1062. case Variant::AABB: {
  1063. if (buf) {
  1064. AABB aabb = p_variant;
  1065. encode_real(aabb.position.x, &buf[0]);
  1066. encode_real(aabb.position.y, &buf[sizeof(real_t)]);
  1067. encode_real(aabb.position.z, &buf[sizeof(real_t) * 2]);
  1068. encode_real(aabb.size.x, &buf[sizeof(real_t) * 3]);
  1069. encode_real(aabb.size.y, &buf[sizeof(real_t) * 4]);
  1070. encode_real(aabb.size.z, &buf[sizeof(real_t) * 5]);
  1071. }
  1072. r_len += 6 * sizeof(real_t);
  1073. } break;
  1074. case Variant::BASIS: {
  1075. if (buf) {
  1076. Basis val = p_variant;
  1077. for (int i = 0; i < 3; i++) {
  1078. for (int j = 0; j < 3; j++) {
  1079. memcpy(&buf[(i * 3 + j) * sizeof(real_t)], &val.elements[i][j], sizeof(real_t));
  1080. }
  1081. }
  1082. }
  1083. r_len += 9 * sizeof(real_t);
  1084. } break;
  1085. case Variant::TRANSFORM3D: {
  1086. if (buf) {
  1087. Transform3D val = p_variant;
  1088. for (int i = 0; i < 3; i++) {
  1089. for (int j = 0; j < 3; j++) {
  1090. memcpy(&buf[(i * 3 + j) * sizeof(real_t)], &val.basis.elements[i][j], sizeof(real_t));
  1091. }
  1092. }
  1093. encode_real(val.origin.x, &buf[sizeof(real_t) * 9]);
  1094. encode_real(val.origin.y, &buf[sizeof(real_t) * 10]);
  1095. encode_real(val.origin.z, &buf[sizeof(real_t) * 11]);
  1096. }
  1097. r_len += 12 * sizeof(real_t);
  1098. } break;
  1099. // misc types
  1100. case Variant::COLOR: {
  1101. if (buf) {
  1102. Color c = p_variant;
  1103. encode_float(c.r, &buf[0]);
  1104. encode_float(c.g, &buf[4]);
  1105. encode_float(c.b, &buf[8]);
  1106. encode_float(c.a, &buf[12]);
  1107. }
  1108. r_len += 4 * 4; // Colors should always be in single-precision.
  1109. } break;
  1110. case Variant::RID: {
  1111. } break;
  1112. case Variant::CALLABLE: {
  1113. } break;
  1114. case Variant::SIGNAL: {
  1115. } break;
  1116. case Variant::OBJECT: {
  1117. if (p_full_objects) {
  1118. Object *obj = p_variant;
  1119. if (!obj) {
  1120. if (buf) {
  1121. encode_uint32(0, buf);
  1122. }
  1123. r_len += 4;
  1124. } else {
  1125. _encode_string(obj->get_class(), buf, r_len);
  1126. List<PropertyInfo> props;
  1127. obj->get_property_list(&props);
  1128. int pc = 0;
  1129. for (const PropertyInfo &E : props) {
  1130. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1131. continue;
  1132. }
  1133. pc++;
  1134. }
  1135. if (buf) {
  1136. encode_uint32(pc, buf);
  1137. buf += 4;
  1138. }
  1139. r_len += 4;
  1140. for (const PropertyInfo &E : props) {
  1141. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1142. continue;
  1143. }
  1144. _encode_string(E.name, buf, r_len);
  1145. int len;
  1146. Error err = encode_variant(obj->get(E.name), buf, len, p_full_objects, p_depth + 1);
  1147. ERR_FAIL_COND_V(err, err);
  1148. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1149. r_len += len;
  1150. if (buf) {
  1151. buf += len;
  1152. }
  1153. }
  1154. }
  1155. } else {
  1156. if (buf) {
  1157. Object *obj = p_variant.get_validated_object();
  1158. ObjectID id;
  1159. if (obj) {
  1160. id = obj->get_instance_id();
  1161. }
  1162. encode_uint64(id, buf);
  1163. }
  1164. r_len += 8;
  1165. }
  1166. } break;
  1167. case Variant::DICTIONARY: {
  1168. Dictionary d = p_variant;
  1169. if (buf) {
  1170. encode_uint32(uint32_t(d.size()), buf);
  1171. buf += 4;
  1172. }
  1173. r_len += 4;
  1174. List<Variant> keys;
  1175. d.get_key_list(&keys);
  1176. for (const Variant &E : keys) {
  1177. /*
  1178. CharString utf8 = E->->utf8();
  1179. if (buf) {
  1180. encode_uint32(utf8.length()+1,buf);
  1181. buf+=4;
  1182. memcpy(buf,utf8.get_data(),utf8.length()+1);
  1183. }
  1184. r_len+=4+utf8.length()+1;
  1185. while (r_len%4)
  1186. r_len++; //pad
  1187. */
  1188. int len;
  1189. Error err = encode_variant(E, buf, len, p_full_objects, p_depth + 1);
  1190. ERR_FAIL_COND_V(err, err);
  1191. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1192. r_len += len;
  1193. if (buf) {
  1194. buf += len;
  1195. }
  1196. Variant *v = d.getptr(E);
  1197. ERR_FAIL_COND_V(!v, ERR_BUG);
  1198. err = encode_variant(*v, buf, len, p_full_objects, p_depth + 1);
  1199. ERR_FAIL_COND_V(err, err);
  1200. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1201. r_len += len;
  1202. if (buf) {
  1203. buf += len;
  1204. }
  1205. }
  1206. } break;
  1207. case Variant::ARRAY: {
  1208. Array v = p_variant;
  1209. if (buf) {
  1210. encode_uint32(uint32_t(v.size()), buf);
  1211. buf += 4;
  1212. }
  1213. r_len += 4;
  1214. for (int i = 0; i < v.size(); i++) {
  1215. int len;
  1216. Error err = encode_variant(v.get(i), buf, len, p_full_objects, p_depth + 1);
  1217. ERR_FAIL_COND_V(err, err);
  1218. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1219. r_len += len;
  1220. if (buf) {
  1221. buf += len;
  1222. }
  1223. }
  1224. } break;
  1225. // arrays
  1226. case Variant::PACKED_BYTE_ARRAY: {
  1227. Vector<uint8_t> data = p_variant;
  1228. int datalen = data.size();
  1229. int datasize = sizeof(uint8_t);
  1230. if (buf) {
  1231. encode_uint32(datalen, buf);
  1232. buf += 4;
  1233. const uint8_t *r = data.ptr();
  1234. memcpy(buf, &r[0], datalen * datasize);
  1235. buf += datalen * datasize;
  1236. }
  1237. r_len += 4 + datalen * datasize;
  1238. while (r_len % 4) {
  1239. r_len++;
  1240. if (buf) {
  1241. *(buf++) = 0;
  1242. }
  1243. }
  1244. } break;
  1245. case Variant::PACKED_INT32_ARRAY: {
  1246. Vector<int32_t> data = p_variant;
  1247. int datalen = data.size();
  1248. int datasize = sizeof(int32_t);
  1249. if (buf) {
  1250. encode_uint32(datalen, buf);
  1251. buf += 4;
  1252. const int32_t *r = data.ptr();
  1253. for (int32_t i = 0; i < datalen; i++) {
  1254. encode_uint32(r[i], &buf[i * datasize]);
  1255. }
  1256. }
  1257. r_len += 4 + datalen * datasize;
  1258. } break;
  1259. case Variant::PACKED_INT64_ARRAY: {
  1260. Vector<int64_t> data = p_variant;
  1261. int datalen = data.size();
  1262. int datasize = sizeof(int64_t);
  1263. if (buf) {
  1264. encode_uint32(datalen, buf);
  1265. buf += 4;
  1266. const int64_t *r = data.ptr();
  1267. for (int64_t i = 0; i < datalen; i++) {
  1268. encode_uint64(r[i], &buf[i * datasize]);
  1269. }
  1270. }
  1271. r_len += 4 + datalen * datasize;
  1272. } break;
  1273. case Variant::PACKED_FLOAT32_ARRAY: {
  1274. Vector<float> data = p_variant;
  1275. int datalen = data.size();
  1276. int datasize = sizeof(float);
  1277. if (buf) {
  1278. encode_uint32(datalen, buf);
  1279. buf += 4;
  1280. const float *r = data.ptr();
  1281. for (int i = 0; i < datalen; i++) {
  1282. encode_float(r[i], &buf[i * datasize]);
  1283. }
  1284. }
  1285. r_len += 4 + datalen * datasize;
  1286. } break;
  1287. case Variant::PACKED_FLOAT64_ARRAY: {
  1288. Vector<double> data = p_variant;
  1289. int datalen = data.size();
  1290. int datasize = sizeof(double);
  1291. if (buf) {
  1292. encode_uint32(datalen, buf);
  1293. buf += 4;
  1294. const double *r = data.ptr();
  1295. for (int i = 0; i < datalen; i++) {
  1296. encode_double(r[i], &buf[i * datasize]);
  1297. }
  1298. }
  1299. r_len += 4 + datalen * datasize;
  1300. } break;
  1301. case Variant::PACKED_STRING_ARRAY: {
  1302. Vector<String> data = p_variant;
  1303. int len = data.size();
  1304. if (buf) {
  1305. encode_uint32(len, buf);
  1306. buf += 4;
  1307. }
  1308. r_len += 4;
  1309. for (int i = 0; i < len; i++) {
  1310. CharString utf8 = data.get(i).utf8();
  1311. if (buf) {
  1312. encode_uint32(utf8.length() + 1, buf);
  1313. buf += 4;
  1314. memcpy(buf, utf8.get_data(), utf8.length() + 1);
  1315. buf += utf8.length() + 1;
  1316. }
  1317. r_len += 4 + utf8.length() + 1;
  1318. while (r_len % 4) {
  1319. r_len++; //pad
  1320. if (buf) {
  1321. *(buf++) = 0;
  1322. }
  1323. }
  1324. }
  1325. } break;
  1326. case Variant::PACKED_VECTOR2_ARRAY: {
  1327. Vector<Vector2> data = p_variant;
  1328. int len = data.size();
  1329. if (buf) {
  1330. encode_uint32(len, buf);
  1331. buf += 4;
  1332. }
  1333. r_len += 4;
  1334. if (buf) {
  1335. for (int i = 0; i < len; i++) {
  1336. Vector2 v = data.get(i);
  1337. encode_real(v.x, &buf[0]);
  1338. encode_real(v.y, &buf[sizeof(real_t)]);
  1339. buf += sizeof(real_t) * 2;
  1340. }
  1341. }
  1342. r_len += sizeof(real_t) * 2 * len;
  1343. } break;
  1344. case Variant::PACKED_VECTOR3_ARRAY: {
  1345. Vector<Vector3> data = p_variant;
  1346. int len = data.size();
  1347. if (buf) {
  1348. encode_uint32(len, buf);
  1349. buf += 4;
  1350. }
  1351. r_len += 4;
  1352. if (buf) {
  1353. for (int i = 0; i < len; i++) {
  1354. Vector3 v = data.get(i);
  1355. encode_real(v.x, &buf[0]);
  1356. encode_real(v.y, &buf[sizeof(real_t)]);
  1357. encode_real(v.z, &buf[sizeof(real_t) * 2]);
  1358. buf += sizeof(real_t) * 3;
  1359. }
  1360. }
  1361. r_len += sizeof(real_t) * 3 * len;
  1362. } break;
  1363. case Variant::PACKED_COLOR_ARRAY: {
  1364. Vector<Color> data = p_variant;
  1365. int len = data.size();
  1366. if (buf) {
  1367. encode_uint32(len, buf);
  1368. buf += 4;
  1369. }
  1370. r_len += 4;
  1371. if (buf) {
  1372. for (int i = 0; i < len; i++) {
  1373. Color c = data.get(i);
  1374. encode_float(c.r, &buf[0]);
  1375. encode_float(c.g, &buf[4]);
  1376. encode_float(c.b, &buf[8]);
  1377. encode_float(c.a, &buf[12]);
  1378. buf += 4 * 4; // Colors should always be in single-precision.
  1379. }
  1380. }
  1381. r_len += 4 * 4 * len;
  1382. } break;
  1383. default: {
  1384. ERR_FAIL_V(ERR_BUG);
  1385. }
  1386. }
  1387. return OK;
  1388. }