marshalls.cpp 29 KB

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