marshalls.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. /*************************************************************************/
  2. /* marshalls.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "os/keyboard.h"
  32. #include "print_string.h"
  33. #include <limits.h>
  34. #include <stdio.h>
  35. #define _S(a) ((int32_t)a)
  36. #define ERR_FAIL_ADD_OF(a, b, err) ERR_FAIL_COND_V(_S(b) < 0 || _S(a) < 0 || _S(a) > INT_MAX - _S(b), err)
  37. #define ERR_FAIL_MUL_OF(a, b, err) ERR_FAIL_COND_V(_S(a) < 0 || _S(b) <= 0 || _S(a) > INT_MAX / _S(b), err)
  38. static Error _decode_string(const uint8_t *&buf, int &len, int *r_len, String &r_string) {
  39. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  40. int32_t strlen = decode_uint32(buf);
  41. int32_t pad = 0;
  42. // Handle padding
  43. if (strlen % 4) {
  44. pad = 4 - strlen % 4;
  45. }
  46. buf += 4;
  47. len -= 4;
  48. // Ensure buffer is big enough
  49. ERR_FAIL_ADD_OF(strlen, pad, ERR_FILE_EOF);
  50. ERR_FAIL_COND_V(strlen < 0 || strlen + pad > len, ERR_FILE_EOF);
  51. String str;
  52. ERR_FAIL_COND_V(str.parse_utf8((const char *)buf, strlen), ERR_INVALID_DATA);
  53. r_string = str;
  54. // Add padding
  55. strlen += pad;
  56. // Update buffer pos, left data count, and return size
  57. buf += strlen;
  58. len -= strlen;
  59. if (r_len) {
  60. (*r_len) += 4 + strlen;
  61. }
  62. return OK;
  63. }
  64. Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len) {
  65. const uint8_t *buf = p_buffer;
  66. int len = p_len;
  67. if (len < 4) {
  68. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  69. }
  70. uint32_t type = decode_uint32(buf);
  71. ERR_FAIL_COND_V(type >= Variant::VARIANT_MAX, ERR_INVALID_DATA);
  72. buf += 4;
  73. len -= 4;
  74. if (r_len)
  75. *r_len = 4;
  76. switch (type) {
  77. case Variant::NIL: {
  78. r_variant = Variant();
  79. } break;
  80. case Variant::BOOL: {
  81. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  82. bool val = decode_uint32(buf);
  83. r_variant = val;
  84. if (r_len)
  85. (*r_len) += 4;
  86. } break;
  87. case Variant::INT: {
  88. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  89. int32_t val = decode_uint32(buf);
  90. r_variant = val;
  91. if (r_len)
  92. (*r_len) += 4;
  93. } break;
  94. case Variant::REAL: {
  95. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  96. float val = decode_float(buf);
  97. r_variant = val;
  98. if (r_len)
  99. (*r_len) += 4;
  100. } break;
  101. case Variant::STRING: {
  102. String str;
  103. Error err = _decode_string(buf, len, r_len, str);
  104. if (err)
  105. return err;
  106. r_variant = str;
  107. } break;
  108. // math types
  109. case Variant::VECTOR2: {
  110. ERR_FAIL_COND_V(len < 4 * 2, ERR_INVALID_DATA);
  111. Vector2 val;
  112. val.x = decode_float(&buf[0]);
  113. val.y = decode_float(&buf[4]);
  114. r_variant = val;
  115. if (r_len)
  116. (*r_len) += 4 * 2;
  117. } break; // 5
  118. case Variant::RECT2: {
  119. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  120. Rect2 val;
  121. val.pos.x = decode_float(&buf[0]);
  122. val.pos.y = decode_float(&buf[4]);
  123. val.size.x = decode_float(&buf[8]);
  124. val.size.y = decode_float(&buf[12]);
  125. r_variant = val;
  126. if (r_len)
  127. (*r_len) += 4 * 4;
  128. } break;
  129. case Variant::VECTOR3: {
  130. ERR_FAIL_COND_V(len < 4 * 3, ERR_INVALID_DATA);
  131. Vector3 val;
  132. val.x = decode_float(&buf[0]);
  133. val.y = decode_float(&buf[4]);
  134. val.z = decode_float(&buf[8]);
  135. r_variant = val;
  136. if (r_len)
  137. (*r_len) += 4 * 3;
  138. } break;
  139. case Variant::MATRIX32: {
  140. ERR_FAIL_COND_V(len < 4 * 6, ERR_INVALID_DATA);
  141. Matrix32 val;
  142. for (int i = 0; i < 3; i++) {
  143. for (int j = 0; j < 2; j++) {
  144. val.elements[i][j] = decode_float(&buf[(i * 2 + j) * 4]);
  145. }
  146. }
  147. r_variant = val;
  148. if (r_len)
  149. (*r_len) += 4 * 6;
  150. } break;
  151. case Variant::PLANE: {
  152. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  153. Plane val;
  154. val.normal.x = decode_float(&buf[0]);
  155. val.normal.y = decode_float(&buf[4]);
  156. val.normal.z = decode_float(&buf[8]);
  157. val.d = decode_float(&buf[12]);
  158. r_variant = val;
  159. if (r_len)
  160. (*r_len) += 4 * 4;
  161. } break;
  162. case Variant::QUAT: {
  163. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  164. Quat val;
  165. val.x = decode_float(&buf[0]);
  166. val.y = decode_float(&buf[4]);
  167. val.z = decode_float(&buf[8]);
  168. val.w = decode_float(&buf[12]);
  169. r_variant = val;
  170. if (r_len)
  171. (*r_len) += 4 * 4;
  172. } break;
  173. case Variant::_AABB: {
  174. ERR_FAIL_COND_V(len < 4 * 6, ERR_INVALID_DATA);
  175. AABB val;
  176. val.pos.x = decode_float(&buf[0]);
  177. val.pos.y = decode_float(&buf[4]);
  178. val.pos.z = decode_float(&buf[8]);
  179. val.size.x = decode_float(&buf[12]);
  180. val.size.y = decode_float(&buf[16]);
  181. val.size.z = decode_float(&buf[20]);
  182. r_variant = val;
  183. if (r_len)
  184. (*r_len) += 4 * 6;
  185. } break;
  186. case Variant::MATRIX3: {
  187. ERR_FAIL_COND_V(len < 4 * 9, ERR_INVALID_DATA);
  188. Matrix3 val;
  189. for (int i = 0; i < 3; i++) {
  190. for (int j = 0; j < 3; j++) {
  191. val.elements[i][j] = decode_float(&buf[(i * 3 + j) * 4]);
  192. }
  193. }
  194. r_variant = val;
  195. if (r_len)
  196. (*r_len) += 4 * 9;
  197. } break;
  198. case Variant::TRANSFORM: {
  199. ERR_FAIL_COND_V(len < 4 * 12, ERR_INVALID_DATA);
  200. Transform val;
  201. for (int i = 0; i < 3; i++) {
  202. for (int j = 0; j < 3; j++) {
  203. val.basis.elements[i][j] = decode_float(&buf[(i * 3 + j) * 4]);
  204. }
  205. }
  206. val.origin[0] = decode_float(&buf[36]);
  207. val.origin[1] = decode_float(&buf[40]);
  208. val.origin[2] = decode_float(&buf[44]);
  209. r_variant = val;
  210. if (r_len)
  211. (*r_len) += 4 * 12;
  212. } break;
  213. // misc types
  214. case Variant::COLOR: {
  215. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  216. Color val;
  217. val.r = decode_float(&buf[0]);
  218. val.g = decode_float(&buf[4]);
  219. val.b = decode_float(&buf[8]);
  220. val.a = decode_float(&buf[12]);
  221. r_variant = val;
  222. if (r_len)
  223. (*r_len) += 4 * 4;
  224. } break;
  225. case Variant::IMAGE: {
  226. ERR_FAIL_COND_V(len < 5 * 4, ERR_INVALID_DATA);
  227. Image::Format fmt = (Image::Format)decode_uint32(&buf[0]);
  228. ERR_FAIL_INDEX_V(fmt, Image::FORMAT_MAX, ERR_INVALID_DATA);
  229. uint32_t mipmaps = decode_uint32(&buf[4]);
  230. uint32_t w = decode_uint32(&buf[8]);
  231. uint32_t h = decode_uint32(&buf[12]);
  232. int32_t datalen = decode_uint32(&buf[16]);
  233. Image img;
  234. if (datalen > 0) {
  235. len -= 5 * 4;
  236. ERR_FAIL_COND_V(len < datalen, ERR_INVALID_DATA);
  237. DVector<uint8_t> data;
  238. data.resize(datalen);
  239. DVector<uint8_t>::Write wr = data.write();
  240. copymem(&wr[0], &buf[20], datalen);
  241. wr = DVector<uint8_t>::Write();
  242. img = Image(w, h, mipmaps, fmt, data);
  243. }
  244. r_variant = img;
  245. if (r_len) {
  246. if (datalen % 4)
  247. (*r_len) += 4 - datalen % 4;
  248. (*r_len) += 4 * 5 + datalen;
  249. }
  250. } break;
  251. case Variant::NODE_PATH: {
  252. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  253. int32_t strlen = decode_uint32(buf);
  254. if (strlen & 0x80000000) {
  255. //new format
  256. ERR_FAIL_COND_V(len < 12, ERR_INVALID_DATA);
  257. Vector<StringName> names;
  258. Vector<StringName> subnames;
  259. StringName prop;
  260. uint32_t namecount = strlen &= 0x7FFFFFFF;
  261. uint32_t subnamecount = decode_uint32(buf + 4);
  262. uint32_t flags = decode_uint32(buf + 8);
  263. len -= 12;
  264. buf += 12;
  265. int total = namecount + subnamecount;
  266. if (flags & 2)
  267. total++;
  268. if (r_len)
  269. (*r_len) += 12;
  270. for (uint32_t i = 0; i < total; i++) {
  271. String str;
  272. Error err = _decode_string(buf, len, r_len, str);
  273. if (err)
  274. return err;
  275. if (i < namecount)
  276. names.push_back(str);
  277. else if (i < namecount + subnamecount)
  278. subnames.push_back(str);
  279. else
  280. prop = str;
  281. }
  282. r_variant = NodePath(names, subnames, flags & 1, prop);
  283. } else {
  284. //old format, just a string
  285. ERR_FAIL_V(ERR_INVALID_DATA);
  286. }
  287. } break;
  288. /*case Variant::RESOURCE: {
  289. ERR_EXPLAIN("Can't marshallize resources");
  290. ERR_FAIL_V(ERR_INVALID_DATA); //no, i'm sorry, no go
  291. } break;*/
  292. case Variant::_RID: {
  293. r_variant = RID();
  294. } break;
  295. case Variant::OBJECT: {
  296. r_variant = (Object *)NULL;
  297. } break;
  298. case Variant::INPUT_EVENT: {
  299. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  300. InputEvent ie;
  301. ie.type = decode_uint32(&buf[0]);
  302. ie.device = decode_uint32(&buf[4]);
  303. if (r_len)
  304. (*r_len) += 12;
  305. switch (ie.type) {
  306. case InputEvent::KEY: {
  307. ERR_FAIL_COND_V(len < 20, ERR_INVALID_DATA);
  308. uint32_t mods = decode_uint32(&buf[12]);
  309. if (mods & KEY_MASK_SHIFT)
  310. ie.key.mod.shift = true;
  311. if (mods & KEY_MASK_CTRL)
  312. ie.key.mod.control = true;
  313. if (mods & KEY_MASK_ALT)
  314. ie.key.mod.alt = true;
  315. if (mods & KEY_MASK_META)
  316. ie.key.mod.meta = true;
  317. ie.key.scancode = decode_uint32(&buf[16]);
  318. if (r_len)
  319. (*r_len) += 8;
  320. } break;
  321. case InputEvent::MOUSE_BUTTON: {
  322. ERR_FAIL_COND_V(len < 16, ERR_INVALID_DATA);
  323. ie.mouse_button.button_index = decode_uint32(&buf[12]);
  324. if (r_len)
  325. (*r_len) += 4;
  326. } break;
  327. case InputEvent::JOYSTICK_BUTTON: {
  328. ERR_FAIL_COND_V(len < 16, ERR_INVALID_DATA);
  329. ie.joy_button.button_index = decode_uint32(&buf[12]);
  330. if (r_len)
  331. (*r_len) += 4;
  332. } break;
  333. case InputEvent::SCREEN_TOUCH: {
  334. ERR_FAIL_COND_V(len < 16, ERR_INVALID_DATA);
  335. ie.screen_touch.index = decode_uint32(&buf[12]);
  336. if (r_len)
  337. (*r_len) += 4;
  338. } break;
  339. case InputEvent::JOYSTICK_MOTION: {
  340. ERR_FAIL_COND_V(len < 20, ERR_INVALID_DATA);
  341. ie.joy_motion.axis = decode_uint32(&buf[12]);
  342. ie.joy_motion.axis_value = decode_float(&buf[16]);
  343. if (r_len)
  344. (*r_len) += 8;
  345. } break;
  346. }
  347. r_variant = ie;
  348. } break;
  349. case Variant::DICTIONARY: {
  350. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  351. int32_t count = decode_uint32(buf);
  352. bool shared = count & 0x80000000;
  353. count &= 0x7FFFFFFF;
  354. buf += 4;
  355. len -= 4;
  356. if (r_len) {
  357. (*r_len) += 4;
  358. }
  359. Dictionary d(shared);
  360. for (int i = 0; i < count; i++) {
  361. Variant key, value;
  362. int used;
  363. Error err = decode_variant(key, buf, len, &used);
  364. ERR_FAIL_COND_V(err, err);
  365. buf += used;
  366. len -= used;
  367. if (r_len) {
  368. (*r_len) += used;
  369. }
  370. err = decode_variant(value, buf, len, &used);
  371. ERR_FAIL_COND_V(err, err);
  372. buf += used;
  373. len -= used;
  374. if (r_len) {
  375. (*r_len) += used;
  376. }
  377. d[key] = value;
  378. }
  379. r_variant = d;
  380. } break;
  381. case Variant::ARRAY: {
  382. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  383. int32_t count = decode_uint32(buf);
  384. bool shared = count & 0x80000000;
  385. count &= 0x7FFFFFFF;
  386. buf += 4;
  387. len -= 4;
  388. if (r_len) {
  389. (*r_len) += 4;
  390. }
  391. Array varr(shared);
  392. for (int i = 0; i < count; i++) {
  393. int used = 0;
  394. Variant v;
  395. Error err = decode_variant(v, buf, len, &used);
  396. ERR_FAIL_COND_V(err, err);
  397. buf += used;
  398. len -= used;
  399. varr.push_back(v);
  400. if (r_len) {
  401. (*r_len) += used;
  402. }
  403. }
  404. r_variant = varr;
  405. } break;
  406. // arrays
  407. case Variant::RAW_ARRAY: {
  408. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  409. int32_t count = decode_uint32(buf);
  410. buf += 4;
  411. len -= 4;
  412. ERR_FAIL_COND_V(count < 0 || count > len, ERR_INVALID_DATA);
  413. DVector<uint8_t> data;
  414. if (count) {
  415. data.resize(count);
  416. DVector<uint8_t>::Write w = data.write();
  417. for (int32_t i = 0; i < count; i++) {
  418. w[i] = buf[i];
  419. }
  420. w = DVector<uint8_t>::Write();
  421. }
  422. r_variant = data;
  423. if (r_len) {
  424. if (count % 4)
  425. (*r_len) += 4 - count % 4;
  426. (*r_len) += 4 + count;
  427. }
  428. } break;
  429. case Variant::INT_ARRAY: {
  430. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  431. int32_t count = decode_uint32(buf);
  432. buf += 4;
  433. len -= 4;
  434. ERR_FAIL_MUL_OF(count, 4, ERR_INVALID_DATA);
  435. ERR_FAIL_COND_V(count < 0 || count * 4 > len, ERR_INVALID_DATA);
  436. DVector<int> data;
  437. if (count) {
  438. //const int*rbuf=(const int*)buf;
  439. data.resize(count);
  440. DVector<int>::Write w = data.write();
  441. for (int32_t i = 0; i < count; i++) {
  442. w[i] = decode_uint32(&buf[i * 4]);
  443. }
  444. w = DVector<int>::Write();
  445. }
  446. r_variant = Variant(data);
  447. if (r_len) {
  448. (*r_len) += 4 + count * sizeof(int);
  449. }
  450. } break;
  451. case Variant::REAL_ARRAY: {
  452. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  453. int32_t count = decode_uint32(buf);
  454. buf += 4;
  455. len -= 4;
  456. ERR_FAIL_MUL_OF(count, 4, ERR_INVALID_DATA);
  457. ERR_FAIL_COND_V(count < 0 || count * 4 > len, ERR_INVALID_DATA);
  458. DVector<float> data;
  459. if (count) {
  460. //const float*rbuf=(const float*)buf;
  461. data.resize(count);
  462. DVector<float>::Write w = data.write();
  463. for (int32_t i = 0; i < count; i++) {
  464. w[i] = decode_float(&buf[i * 4]);
  465. }
  466. w = DVector<float>::Write();
  467. }
  468. r_variant = data;
  469. if (r_len) {
  470. (*r_len) += 4 + count * sizeof(float);
  471. }
  472. } break;
  473. case Variant::STRING_ARRAY: {
  474. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  475. int32_t count = decode_uint32(buf);
  476. DVector<String> strings;
  477. buf += 4;
  478. len -= 4;
  479. if (r_len)
  480. (*r_len) += 4;
  481. //printf("string count: %i\n",count);
  482. for (int32_t i = 0; i < count; i++) {
  483. String str;
  484. Error err = _decode_string(buf, len, r_len, str);
  485. if (err)
  486. return err;
  487. strings.push_back(str);
  488. }
  489. r_variant = strings;
  490. } break;
  491. case Variant::VECTOR2_ARRAY: {
  492. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  493. int32_t count = decode_uint32(buf);
  494. buf += 4;
  495. len -= 4;
  496. ERR_FAIL_MUL_OF(count, 4 * 2, ERR_INVALID_DATA);
  497. ERR_FAIL_COND_V(count < 0 || count * 4 * 2 > len, ERR_INVALID_DATA);
  498. DVector<Vector2> varray;
  499. if (r_len) {
  500. (*r_len) += 4;
  501. }
  502. if (count) {
  503. varray.resize(count);
  504. DVector<Vector2>::Write w = varray.write();
  505. for (int32_t i = 0; i < count; i++) {
  506. w[i].x = decode_float(buf + i * 4 * 2 + 4 * 0);
  507. w[i].y = decode_float(buf + i * 4 * 2 + 4 * 1);
  508. }
  509. int adv = 4 * 2 * count;
  510. if (r_len)
  511. (*r_len) += adv;
  512. len -= adv;
  513. buf += adv;
  514. }
  515. r_variant = varray;
  516. } break;
  517. case Variant::VECTOR3_ARRAY: {
  518. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  519. int32_t count = decode_uint32(buf);
  520. buf += 4;
  521. len -= 4;
  522. ERR_FAIL_MUL_OF(count, 4 * 3, ERR_INVALID_DATA);
  523. ERR_FAIL_COND_V(count < 0 || count * 4 * 3 > len, ERR_INVALID_DATA);
  524. DVector<Vector3> varray;
  525. if (r_len) {
  526. (*r_len) += 4;
  527. }
  528. if (count) {
  529. varray.resize(count);
  530. DVector<Vector3>::Write w = varray.write();
  531. for (int32_t i = 0; i < count; i++) {
  532. w[i].x = decode_float(buf + i * 4 * 3 + 4 * 0);
  533. w[i].y = decode_float(buf + i * 4 * 3 + 4 * 1);
  534. w[i].z = decode_float(buf + i * 4 * 3 + 4 * 2);
  535. }
  536. int adv = 4 * 3 * count;
  537. if (r_len)
  538. (*r_len) += adv;
  539. len -= adv;
  540. buf += adv;
  541. }
  542. r_variant = varray;
  543. } break;
  544. case Variant::COLOR_ARRAY: {
  545. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  546. int32_t count = decode_uint32(buf);
  547. buf += 4;
  548. len -= 4;
  549. ERR_FAIL_MUL_OF(count, 4 * 4, ERR_INVALID_DATA);
  550. ERR_FAIL_COND_V(count < 0 || count * 4 * 4 > len, ERR_INVALID_DATA);
  551. DVector<Color> carray;
  552. if (r_len) {
  553. (*r_len) += 4;
  554. }
  555. if (count) {
  556. carray.resize(count);
  557. DVector<Color>::Write w = carray.write();
  558. for (int32_t i = 0; i < count; i++) {
  559. w[i].r = decode_float(buf + i * 4 * 4 + 4 * 0);
  560. w[i].g = decode_float(buf + i * 4 * 4 + 4 * 1);
  561. w[i].b = decode_float(buf + i * 4 * 4 + 4 * 2);
  562. w[i].a = decode_float(buf + i * 4 * 4 + 4 * 3);
  563. }
  564. int adv = 4 * 4 * count;
  565. if (r_len)
  566. (*r_len) += adv;
  567. len -= adv;
  568. buf += adv;
  569. }
  570. r_variant = carray;
  571. } break;
  572. default: {
  573. ERR_FAIL_V(ERR_BUG);
  574. }
  575. }
  576. return OK;
  577. }
  578. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len) {
  579. uint8_t *buf = r_buffer;
  580. r_len = 0;
  581. if (buf) {
  582. encode_uint32(p_variant.get_type(), buf);
  583. buf += 4;
  584. }
  585. r_len += 4;
  586. switch (p_variant.get_type()) {
  587. case Variant::NIL: {
  588. //nothing to do
  589. } break;
  590. case Variant::BOOL: {
  591. if (buf) {
  592. encode_uint32(p_variant.operator bool(), buf);
  593. }
  594. r_len += 4;
  595. } break;
  596. case Variant::INT: {
  597. if (buf) {
  598. encode_uint32(p_variant.operator int(), buf);
  599. }
  600. r_len += 4;
  601. } break;
  602. case Variant::REAL: {
  603. if (buf) {
  604. encode_float(p_variant.operator float(), buf);
  605. }
  606. r_len += 4;
  607. } break;
  608. case Variant::NODE_PATH: {
  609. NodePath np = p_variant;
  610. if (buf) {
  611. encode_uint32(uint32_t(np.get_name_count()) | 0x80000000, buf); //for compatibility with the old format
  612. encode_uint32(np.get_subname_count(), buf + 4);
  613. uint32_t flags = 0;
  614. if (np.is_absolute())
  615. flags |= 1;
  616. if (np.get_property() != StringName())
  617. flags |= 2;
  618. encode_uint32(flags, buf + 8);
  619. buf += 12;
  620. }
  621. r_len += 12;
  622. int total = np.get_name_count() + np.get_subname_count();
  623. if (np.get_property() != StringName())
  624. total++;
  625. for (int i = 0; i < total; i++) {
  626. String str;
  627. if (i < np.get_name_count())
  628. str = np.get_name(i);
  629. else if (i < np.get_name_count() + np.get_subname_count())
  630. str = np.get_subname(i - np.get_subname_count());
  631. else
  632. str = np.get_property();
  633. CharString utf8 = str.utf8();
  634. int pad = 0;
  635. if (utf8.length() % 4)
  636. pad = 4 - utf8.length() % 4;
  637. if (buf) {
  638. encode_uint32(utf8.length(), buf);
  639. buf += 4;
  640. copymem(buf, utf8.get_data(), utf8.length());
  641. buf += pad + utf8.length();
  642. }
  643. r_len += 4 + utf8.length() + pad;
  644. }
  645. } break;
  646. case Variant::STRING: {
  647. CharString utf8 = p_variant.operator String().utf8();
  648. if (buf) {
  649. encode_uint32(utf8.length(), buf);
  650. buf += 4;
  651. copymem(buf, utf8.get_data(), utf8.length());
  652. }
  653. r_len += 4 + utf8.length();
  654. while (r_len % 4)
  655. r_len++; //pad
  656. } break;
  657. // math types
  658. case Variant::VECTOR2: {
  659. if (buf) {
  660. Vector2 v2 = p_variant;
  661. encode_float(v2.x, &buf[0]);
  662. encode_float(v2.y, &buf[4]);
  663. }
  664. r_len += 2 * 4;
  665. } break; // 5
  666. case Variant::RECT2: {
  667. if (buf) {
  668. Rect2 r2 = p_variant;
  669. encode_float(r2.pos.x, &buf[0]);
  670. encode_float(r2.pos.y, &buf[4]);
  671. encode_float(r2.size.x, &buf[8]);
  672. encode_float(r2.size.y, &buf[12]);
  673. }
  674. r_len += 4 * 4;
  675. } break;
  676. case Variant::VECTOR3: {
  677. if (buf) {
  678. Vector3 v3 = p_variant;
  679. encode_float(v3.x, &buf[0]);
  680. encode_float(v3.y, &buf[4]);
  681. encode_float(v3.z, &buf[8]);
  682. }
  683. r_len += 3 * 4;
  684. } break;
  685. case Variant::MATRIX32: {
  686. if (buf) {
  687. Matrix32 val = p_variant;
  688. for (int i = 0; i < 3; i++) {
  689. for (int j = 0; j < 2; j++) {
  690. copymem(&buf[(i * 2 + j) * 4], &val.elements[i][j], sizeof(float));
  691. }
  692. }
  693. }
  694. r_len += 6 * 4;
  695. } break;
  696. case Variant::PLANE: {
  697. if (buf) {
  698. Plane p = p_variant;
  699. encode_float(p.normal.x, &buf[0]);
  700. encode_float(p.normal.y, &buf[4]);
  701. encode_float(p.normal.z, &buf[8]);
  702. encode_float(p.d, &buf[12]);
  703. }
  704. r_len += 4 * 4;
  705. } break;
  706. case Variant::QUAT: {
  707. if (buf) {
  708. Quat q = p_variant;
  709. encode_float(q.x, &buf[0]);
  710. encode_float(q.y, &buf[4]);
  711. encode_float(q.z, &buf[8]);
  712. encode_float(q.w, &buf[12]);
  713. }
  714. r_len += 4 * 4;
  715. } break;
  716. case Variant::_AABB: {
  717. if (buf) {
  718. AABB aabb = p_variant;
  719. encode_float(aabb.pos.x, &buf[0]);
  720. encode_float(aabb.pos.y, &buf[4]);
  721. encode_float(aabb.pos.z, &buf[8]);
  722. encode_float(aabb.size.x, &buf[12]);
  723. encode_float(aabb.size.y, &buf[16]);
  724. encode_float(aabb.size.z, &buf[20]);
  725. }
  726. r_len += 6 * 4;
  727. } break;
  728. case Variant::MATRIX3: {
  729. if (buf) {
  730. Matrix3 val = p_variant;
  731. for (int i = 0; i < 3; i++) {
  732. for (int j = 0; j < 3; j++) {
  733. copymem(&buf[(i * 3 + j) * 4], &val.elements[i][j], sizeof(float));
  734. }
  735. }
  736. }
  737. r_len += 9 * 4;
  738. } break;
  739. case Variant::TRANSFORM: {
  740. if (buf) {
  741. Transform val = p_variant;
  742. for (int i = 0; i < 3; i++) {
  743. for (int j = 0; j < 3; j++) {
  744. copymem(&buf[(i * 3 + j) * 4], &val.basis.elements[i][j], sizeof(float));
  745. }
  746. }
  747. encode_float(val.origin.x, &buf[36]);
  748. encode_float(val.origin.y, &buf[40]);
  749. encode_float(val.origin.z, &buf[44]);
  750. }
  751. r_len += 12 * 4;
  752. } break;
  753. // misc types
  754. case Variant::COLOR: {
  755. if (buf) {
  756. Color c = p_variant;
  757. encode_float(c.r, &buf[0]);
  758. encode_float(c.g, &buf[4]);
  759. encode_float(c.b, &buf[8]);
  760. encode_float(c.a, &buf[12]);
  761. }
  762. r_len += 4 * 4;
  763. } break;
  764. case Variant::IMAGE: {
  765. Image image = p_variant;
  766. DVector<uint8_t> data = image.get_data();
  767. if (buf) {
  768. encode_uint32(image.get_format(), &buf[0]);
  769. encode_uint32(image.get_mipmaps(), &buf[4]);
  770. encode_uint32(image.get_width(), &buf[8]);
  771. encode_uint32(image.get_height(), &buf[12]);
  772. int ds = data.size();
  773. encode_uint32(ds, &buf[16]);
  774. DVector<uint8_t>::Read r = data.read();
  775. copymem(&buf[20], &r[0], ds);
  776. }
  777. int pad = 0;
  778. if (data.size() % 4)
  779. pad = 4 - data.size() % 4;
  780. r_len += data.size() + 5 * 4 + pad;
  781. } break;
  782. /*case Variant::RESOURCE: {
  783. ERR_EXPLAIN("Can't marshallize resources");
  784. ERR_FAIL_V(ERR_INVALID_DATA); //no, i'm sorry, no go
  785. } break;*/
  786. case Variant::_RID:
  787. case Variant::OBJECT: {
  788. } break;
  789. case Variant::INPUT_EVENT: {
  790. InputEvent ie = p_variant;
  791. if (buf) {
  792. encode_uint32(ie.type, &buf[0]);
  793. encode_uint32(ie.device, &buf[4]);
  794. encode_uint32(0, &buf[8]);
  795. }
  796. int llen = 12;
  797. switch (ie.type) {
  798. case InputEvent::KEY: {
  799. if (buf) {
  800. uint32_t mods = 0;
  801. if (ie.key.mod.shift)
  802. mods |= KEY_MASK_SHIFT;
  803. if (ie.key.mod.control)
  804. mods |= KEY_MASK_CTRL;
  805. if (ie.key.mod.alt)
  806. mods |= KEY_MASK_ALT;
  807. if (ie.key.mod.meta)
  808. mods |= KEY_MASK_META;
  809. encode_uint32(mods, &buf[llen]);
  810. encode_uint32(ie.key.scancode, &buf[llen + 4]);
  811. }
  812. llen += 8;
  813. } break;
  814. case InputEvent::MOUSE_BUTTON: {
  815. if (buf) {
  816. encode_uint32(ie.mouse_button.button_index, &buf[llen]);
  817. }
  818. llen += 4;
  819. } break;
  820. case InputEvent::JOYSTICK_BUTTON: {
  821. if (buf) {
  822. encode_uint32(ie.joy_button.button_index, &buf[llen]);
  823. }
  824. llen += 4;
  825. } break;
  826. case InputEvent::SCREEN_TOUCH: {
  827. if (buf) {
  828. encode_uint32(ie.screen_touch.index, &buf[llen]);
  829. }
  830. llen += 4;
  831. } break;
  832. case InputEvent::JOYSTICK_MOTION: {
  833. if (buf) {
  834. int axis = ie.joy_motion.axis;
  835. encode_uint32(axis, &buf[llen]);
  836. encode_float(ie.joy_motion.axis_value, &buf[llen + 4]);
  837. }
  838. llen += 8;
  839. } break;
  840. }
  841. if (buf)
  842. encode_uint32(llen, &buf[8]);
  843. r_len += llen;
  844. // not supported
  845. } break;
  846. case Variant::DICTIONARY: {
  847. Dictionary d = p_variant;
  848. if (buf) {
  849. encode_uint32(uint32_t(d.size()) | (d.is_shared() ? 0x80000000 : 0), buf);
  850. buf += 4;
  851. }
  852. r_len += 4;
  853. List<Variant> keys;
  854. d.get_key_list(&keys);
  855. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  856. /*
  857. CharString utf8 = E->->utf8();
  858. if (buf) {
  859. encode_uint32(utf8.length()+1,buf);
  860. buf+=4;
  861. copymem(buf,utf8.get_data(),utf8.length()+1);
  862. }
  863. r_len+=4+utf8.length()+1;
  864. while (r_len%4)
  865. r_len++; //pad
  866. */
  867. int len;
  868. encode_variant(E->get(), buf, len);
  869. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  870. r_len += len;
  871. if (buf)
  872. buf += len;
  873. encode_variant(d[E->get()], buf, len);
  874. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  875. r_len += len;
  876. if (buf)
  877. buf += len;
  878. }
  879. } break;
  880. case Variant::ARRAY: {
  881. Array v = p_variant;
  882. if (buf) {
  883. encode_uint32(uint32_t(v.size()) | (v.is_shared() ? 0x80000000 : 0), buf);
  884. buf += 4;
  885. }
  886. r_len += 4;
  887. for (int i = 0; i < v.size(); i++) {
  888. int len;
  889. encode_variant(v.get(i), buf, len);
  890. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  891. r_len += len;
  892. if (buf)
  893. buf += len;
  894. }
  895. } break;
  896. // arrays
  897. case Variant::RAW_ARRAY: {
  898. DVector<uint8_t> data = p_variant;
  899. int datalen = data.size();
  900. int datasize = sizeof(uint8_t);
  901. if (buf) {
  902. encode_uint32(datalen, buf);
  903. buf += 4;
  904. DVector<uint8_t>::Read r = data.read();
  905. copymem(buf, &r[0], datalen * datasize);
  906. }
  907. r_len += 4 + datalen * datasize;
  908. while (r_len % 4)
  909. r_len++;
  910. } break;
  911. case Variant::INT_ARRAY: {
  912. DVector<int> data = p_variant;
  913. int datalen = data.size();
  914. int datasize = sizeof(int32_t);
  915. if (buf) {
  916. encode_uint32(datalen, buf);
  917. buf += 4;
  918. DVector<int>::Read r = data.read();
  919. for (int i = 0; i < datalen; i++)
  920. encode_uint32(r[i], &buf[i * datasize]);
  921. }
  922. r_len += 4 + datalen * datasize;
  923. } break;
  924. case Variant::REAL_ARRAY: {
  925. DVector<real_t> data = p_variant;
  926. int datalen = data.size();
  927. int datasize = sizeof(real_t);
  928. if (buf) {
  929. encode_uint32(datalen, buf);
  930. buf += 4;
  931. DVector<real_t>::Read r = data.read();
  932. for (int i = 0; i < datalen; i++)
  933. encode_float(r[i], &buf[i * datasize]);
  934. }
  935. r_len += 4 + datalen * datasize;
  936. } break;
  937. case Variant::STRING_ARRAY: {
  938. DVector<String> data = p_variant;
  939. int len = data.size();
  940. if (buf) {
  941. encode_uint32(len, buf);
  942. buf += 4;
  943. }
  944. r_len += 4;
  945. for (int i = 0; i < len; i++) {
  946. CharString utf8 = data.get(i).utf8();
  947. if (buf) {
  948. encode_uint32(utf8.length() + 1, buf);
  949. buf += 4;
  950. copymem(buf, utf8.get_data(), utf8.length() + 1);
  951. buf += utf8.length() + 1;
  952. }
  953. r_len += 4 + utf8.length() + 1;
  954. while (r_len % 4) {
  955. r_len++; //pad
  956. if (buf)
  957. *(buf++) = 0;
  958. }
  959. }
  960. } break;
  961. case Variant::VECTOR2_ARRAY: {
  962. DVector<Vector2> data = p_variant;
  963. int len = data.size();
  964. if (buf) {
  965. encode_uint32(len, buf);
  966. buf += 4;
  967. }
  968. r_len += 4;
  969. if (buf) {
  970. for (int i = 0; i < len; i++) {
  971. Vector2 v = data.get(i);
  972. encode_float(v.x, &buf[0]);
  973. encode_float(v.y, &buf[4]);
  974. buf += 4 * 2;
  975. }
  976. }
  977. r_len += 4 * 2 * len;
  978. } break;
  979. case Variant::VECTOR3_ARRAY: {
  980. DVector<Vector3> data = p_variant;
  981. int len = data.size();
  982. if (buf) {
  983. encode_uint32(len, buf);
  984. buf += 4;
  985. }
  986. r_len += 4;
  987. if (buf) {
  988. for (int i = 0; i < len; i++) {
  989. Vector3 v = data.get(i);
  990. encode_float(v.x, &buf[0]);
  991. encode_float(v.y, &buf[4]);
  992. encode_float(v.z, &buf[8]);
  993. buf += 4 * 3;
  994. }
  995. }
  996. r_len += 4 * 3 * len;
  997. } break;
  998. case Variant::COLOR_ARRAY: {
  999. DVector<Color> data = p_variant;
  1000. int len = data.size();
  1001. if (buf) {
  1002. encode_uint32(len, buf);
  1003. buf += 4;
  1004. }
  1005. r_len += 4;
  1006. if (buf) {
  1007. for (int i = 0; i < len; i++) {
  1008. Color c = data.get(i);
  1009. encode_float(c.r, &buf[0]);
  1010. encode_float(c.g, &buf[4]);
  1011. encode_float(c.b, &buf[8]);
  1012. encode_float(c.a, &buf[12]);
  1013. buf += 4 * 4;
  1014. }
  1015. }
  1016. r_len += 4 * 4 * len;
  1017. } break;
  1018. default: {
  1019. ERR_FAIL_V(ERR_BUG);
  1020. }
  1021. }
  1022. return OK;
  1023. }