marshalls.cpp 28 KB

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