marshalls.cpp 28 KB

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