marshalls.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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/io/resource_loader.h"
  32. #include "core/object/ref_counted.h"
  33. #include "core/object/script_language.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/string/print_string.h"
  36. #include <limits.h>
  37. #include <stdio.h>
  38. void EncodedObjectAsID::_bind_methods() {
  39. ClassDB::bind_method(D_METHOD("set_object_id", "id"), &EncodedObjectAsID::set_object_id);
  40. ClassDB::bind_method(D_METHOD("get_object_id"), &EncodedObjectAsID::get_object_id);
  41. ADD_PROPERTY(PropertyInfo(Variant::INT, "object_id"), "set_object_id", "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. #define ERR_FAIL_ADD_OF(a, b, err) ERR_FAIL_COND_V(((int32_t)(b)) < 0 || ((int32_t)(a)) < 0 || ((int32_t)(a)) > INT_MAX - ((int32_t)(b)), err)
  50. #define ERR_FAIL_MUL_OF(a, b, err) ERR_FAIL_COND_V(((int32_t)(a)) < 0 || ((int32_t)(b)) <= 0 || ((int32_t)(a)) > INT_MAX / ((int32_t)(b)), err)
  51. // Byte 0: `Variant::Type`, byte 1: unused, bytes 2 and 3: additional data.
  52. #define HEADER_TYPE_MASK 0xFF
  53. // For `Variant::INT`, `Variant::FLOAT` and other math types.
  54. #define HEADER_DATA_FLAG_64 (1 << 16)
  55. // For `Variant::OBJECT`.
  56. #define HEADER_DATA_FLAG_OBJECT_AS_ID (1 << 16)
  57. // For `Variant::ARRAY`.
  58. // Occupies bits 16 and 17.
  59. #define HEADER_DATA_FIELD_TYPED_ARRAY_MASK (0b11 << 16)
  60. #define HEADER_DATA_FIELD_TYPED_ARRAY_NONE (0b00 << 16)
  61. #define HEADER_DATA_FIELD_TYPED_ARRAY_BUILTIN (0b01 << 16)
  62. #define HEADER_DATA_FIELD_TYPED_ARRAY_CLASS_NAME (0b10 << 16)
  63. #define HEADER_DATA_FIELD_TYPED_ARRAY_SCRIPT (0b11 << 16)
  64. static Error _decode_string(const uint8_t *&buf, int &len, int *r_len, String &r_string) {
  65. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  66. int32_t strlen = decode_uint32(buf);
  67. int32_t pad = 0;
  68. // Handle padding
  69. if (strlen % 4) {
  70. pad = 4 - strlen % 4;
  71. }
  72. buf += 4;
  73. len -= 4;
  74. // Ensure buffer is big enough
  75. ERR_FAIL_ADD_OF(strlen, pad, ERR_FILE_EOF);
  76. ERR_FAIL_COND_V(strlen < 0 || strlen + pad > len, ERR_FILE_EOF);
  77. String str;
  78. ERR_FAIL_COND_V(str.parse_utf8((const char *)buf, strlen) != OK, ERR_INVALID_DATA);
  79. r_string = str;
  80. // Add padding
  81. strlen += pad;
  82. // Update buffer pos, left data count, and return size
  83. buf += strlen;
  84. len -= strlen;
  85. if (r_len) {
  86. (*r_len) += 4 + strlen;
  87. }
  88. return OK;
  89. }
  90. Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_objects, int p_depth) {
  91. ERR_FAIL_COND_V_MSG(p_depth > Variant::MAX_RECURSION_DEPTH, ERR_OUT_OF_MEMORY, "Variant is too deep. Bailing.");
  92. const uint8_t *buf = p_buffer;
  93. int len = p_len;
  94. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  95. uint32_t header = decode_uint32(buf);
  96. ERR_FAIL_COND_V((header & HEADER_TYPE_MASK) >= Variant::VARIANT_MAX, ERR_INVALID_DATA);
  97. buf += 4;
  98. len -= 4;
  99. if (r_len) {
  100. *r_len = 4;
  101. }
  102. // Note: We cannot use sizeof(real_t) for decoding, in case a different size is encoded.
  103. // Decoding math types always checks for the encoded size, while encoding always uses compilation setting.
  104. // This does lead to some code duplication for decoding, but compatibility is the priority.
  105. switch (header & HEADER_TYPE_MASK) {
  106. case Variant::NIL: {
  107. r_variant = Variant();
  108. } break;
  109. case Variant::BOOL: {
  110. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  111. bool val = decode_uint32(buf);
  112. r_variant = val;
  113. if (r_len) {
  114. (*r_len) += 4;
  115. }
  116. } break;
  117. case Variant::INT: {
  118. if (header & HEADER_DATA_FLAG_64) {
  119. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  120. int64_t val = decode_uint64(buf);
  121. r_variant = val;
  122. if (r_len) {
  123. (*r_len) += 8;
  124. }
  125. } else {
  126. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  127. int32_t val = decode_uint32(buf);
  128. r_variant = val;
  129. if (r_len) {
  130. (*r_len) += 4;
  131. }
  132. }
  133. } break;
  134. case Variant::FLOAT: {
  135. if (header & HEADER_DATA_FLAG_64) {
  136. ERR_FAIL_COND_V((size_t)len < sizeof(double), ERR_INVALID_DATA);
  137. double val = decode_double(buf);
  138. r_variant = val;
  139. if (r_len) {
  140. (*r_len) += sizeof(double);
  141. }
  142. } else {
  143. ERR_FAIL_COND_V((size_t)len < sizeof(float), ERR_INVALID_DATA);
  144. float val = decode_float(buf);
  145. r_variant = val;
  146. if (r_len) {
  147. (*r_len) += sizeof(float);
  148. }
  149. }
  150. } break;
  151. case Variant::STRING: {
  152. String str;
  153. Error err = _decode_string(buf, len, r_len, str);
  154. if (err) {
  155. return err;
  156. }
  157. r_variant = str;
  158. } break;
  159. // math types
  160. case Variant::VECTOR2: {
  161. Vector2 val;
  162. if (header & HEADER_DATA_FLAG_64) {
  163. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 2, ERR_INVALID_DATA);
  164. val.x = decode_double(&buf[0]);
  165. val.y = decode_double(&buf[sizeof(double)]);
  166. if (r_len) {
  167. (*r_len) += sizeof(double) * 2;
  168. }
  169. } else {
  170. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 2, ERR_INVALID_DATA);
  171. val.x = decode_float(&buf[0]);
  172. val.y = decode_float(&buf[sizeof(float)]);
  173. if (r_len) {
  174. (*r_len) += sizeof(float) * 2;
  175. }
  176. }
  177. r_variant = val;
  178. } break;
  179. case Variant::VECTOR2I: {
  180. ERR_FAIL_COND_V(len < 4 * 2, ERR_INVALID_DATA);
  181. Vector2i val;
  182. val.x = decode_uint32(&buf[0]);
  183. val.y = decode_uint32(&buf[4]);
  184. r_variant = val;
  185. if (r_len) {
  186. (*r_len) += 4 * 2;
  187. }
  188. } break;
  189. case Variant::RECT2: {
  190. Rect2 val;
  191. if (header & HEADER_DATA_FLAG_64) {
  192. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  193. val.position.x = decode_double(&buf[0]);
  194. val.position.y = decode_double(&buf[sizeof(double)]);
  195. val.size.x = decode_double(&buf[sizeof(double) * 2]);
  196. val.size.y = decode_double(&buf[sizeof(double) * 3]);
  197. if (r_len) {
  198. (*r_len) += sizeof(double) * 4;
  199. }
  200. } else {
  201. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  202. val.position.x = decode_float(&buf[0]);
  203. val.position.y = decode_float(&buf[sizeof(float)]);
  204. val.size.x = decode_float(&buf[sizeof(float) * 2]);
  205. val.size.y = decode_float(&buf[sizeof(float) * 3]);
  206. if (r_len) {
  207. (*r_len) += sizeof(float) * 4;
  208. }
  209. }
  210. r_variant = val;
  211. } break;
  212. case Variant::RECT2I: {
  213. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  214. Rect2i val;
  215. val.position.x = decode_uint32(&buf[0]);
  216. val.position.y = decode_uint32(&buf[4]);
  217. val.size.x = decode_uint32(&buf[8]);
  218. val.size.y = decode_uint32(&buf[12]);
  219. r_variant = val;
  220. if (r_len) {
  221. (*r_len) += 4 * 4;
  222. }
  223. } break;
  224. case Variant::VECTOR3: {
  225. Vector3 val;
  226. if (header & HEADER_DATA_FLAG_64) {
  227. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 3, ERR_INVALID_DATA);
  228. val.x = decode_double(&buf[0]);
  229. val.y = decode_double(&buf[sizeof(double)]);
  230. val.z = decode_double(&buf[sizeof(double) * 2]);
  231. if (r_len) {
  232. (*r_len) += sizeof(double) * 3;
  233. }
  234. } else {
  235. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 3, ERR_INVALID_DATA);
  236. val.x = decode_float(&buf[0]);
  237. val.y = decode_float(&buf[sizeof(float)]);
  238. val.z = decode_float(&buf[sizeof(float) * 2]);
  239. if (r_len) {
  240. (*r_len) += sizeof(float) * 3;
  241. }
  242. }
  243. r_variant = val;
  244. } break;
  245. case Variant::VECTOR3I: {
  246. ERR_FAIL_COND_V(len < 4 * 3, ERR_INVALID_DATA);
  247. Vector3i val;
  248. val.x = decode_uint32(&buf[0]);
  249. val.y = decode_uint32(&buf[4]);
  250. val.z = decode_uint32(&buf[8]);
  251. r_variant = val;
  252. if (r_len) {
  253. (*r_len) += 4 * 3;
  254. }
  255. } break;
  256. case Variant::VECTOR4: {
  257. Vector4 val;
  258. if (header & HEADER_DATA_FLAG_64) {
  259. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  260. val.x = decode_double(&buf[0]);
  261. val.y = decode_double(&buf[sizeof(double)]);
  262. val.z = decode_double(&buf[sizeof(double) * 2]);
  263. val.w = decode_double(&buf[sizeof(double) * 3]);
  264. if (r_len) {
  265. (*r_len) += sizeof(double) * 4;
  266. }
  267. } else {
  268. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  269. val.x = decode_float(&buf[0]);
  270. val.y = decode_float(&buf[sizeof(float)]);
  271. val.z = decode_float(&buf[sizeof(float) * 2]);
  272. val.w = decode_float(&buf[sizeof(float) * 3]);
  273. if (r_len) {
  274. (*r_len) += sizeof(float) * 4;
  275. }
  276. }
  277. r_variant = val;
  278. } break;
  279. case Variant::VECTOR4I: {
  280. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  281. Vector4i val;
  282. val.x = decode_uint32(&buf[0]);
  283. val.y = decode_uint32(&buf[4]);
  284. val.z = decode_uint32(&buf[8]);
  285. val.w = decode_uint32(&buf[12]);
  286. r_variant = val;
  287. if (r_len) {
  288. (*r_len) += 4 * 4;
  289. }
  290. } break;
  291. case Variant::TRANSFORM2D: {
  292. Transform2D val;
  293. if (header & HEADER_DATA_FLAG_64) {
  294. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 6, ERR_INVALID_DATA);
  295. for (int i = 0; i < 3; i++) {
  296. for (int j = 0; j < 2; j++) {
  297. val.columns[i][j] = decode_double(&buf[(i * 2 + j) * sizeof(double)]);
  298. }
  299. }
  300. if (r_len) {
  301. (*r_len) += sizeof(double) * 6;
  302. }
  303. } else {
  304. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 6, ERR_INVALID_DATA);
  305. for (int i = 0; i < 3; i++) {
  306. for (int j = 0; j < 2; j++) {
  307. val.columns[i][j] = decode_float(&buf[(i * 2 + j) * sizeof(float)]);
  308. }
  309. }
  310. if (r_len) {
  311. (*r_len) += sizeof(float) * 6;
  312. }
  313. }
  314. r_variant = val;
  315. } break;
  316. case Variant::PLANE: {
  317. Plane val;
  318. if (header & HEADER_DATA_FLAG_64) {
  319. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  320. val.normal.x = decode_double(&buf[0]);
  321. val.normal.y = decode_double(&buf[sizeof(double)]);
  322. val.normal.z = decode_double(&buf[sizeof(double) * 2]);
  323. val.d = decode_double(&buf[sizeof(double) * 3]);
  324. if (r_len) {
  325. (*r_len) += sizeof(double) * 4;
  326. }
  327. } else {
  328. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  329. val.normal.x = decode_float(&buf[0]);
  330. val.normal.y = decode_float(&buf[sizeof(float)]);
  331. val.normal.z = decode_float(&buf[sizeof(float) * 2]);
  332. val.d = decode_float(&buf[sizeof(float) * 3]);
  333. if (r_len) {
  334. (*r_len) += sizeof(float) * 4;
  335. }
  336. }
  337. r_variant = val;
  338. } break;
  339. case Variant::QUATERNION: {
  340. Quaternion val;
  341. if (header & HEADER_DATA_FLAG_64) {
  342. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
  343. val.x = decode_double(&buf[0]);
  344. val.y = decode_double(&buf[sizeof(double)]);
  345. val.z = decode_double(&buf[sizeof(double) * 2]);
  346. val.w = decode_double(&buf[sizeof(double) * 3]);
  347. if (r_len) {
  348. (*r_len) += sizeof(double) * 4;
  349. }
  350. } else {
  351. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
  352. val.x = decode_float(&buf[0]);
  353. val.y = decode_float(&buf[sizeof(float)]);
  354. val.z = decode_float(&buf[sizeof(float) * 2]);
  355. val.w = decode_float(&buf[sizeof(float) * 3]);
  356. if (r_len) {
  357. (*r_len) += sizeof(float) * 4;
  358. }
  359. }
  360. r_variant = val;
  361. } break;
  362. case Variant::AABB: {
  363. AABB val;
  364. if (header & HEADER_DATA_FLAG_64) {
  365. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 6, ERR_INVALID_DATA);
  366. val.position.x = decode_double(&buf[0]);
  367. val.position.y = decode_double(&buf[sizeof(double)]);
  368. val.position.z = decode_double(&buf[sizeof(double) * 2]);
  369. val.size.x = decode_double(&buf[sizeof(double) * 3]);
  370. val.size.y = decode_double(&buf[sizeof(double) * 4]);
  371. val.size.z = decode_double(&buf[sizeof(double) * 5]);
  372. if (r_len) {
  373. (*r_len) += sizeof(double) * 6;
  374. }
  375. } else {
  376. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 6, ERR_INVALID_DATA);
  377. val.position.x = decode_float(&buf[0]);
  378. val.position.y = decode_float(&buf[sizeof(float)]);
  379. val.position.z = decode_float(&buf[sizeof(float) * 2]);
  380. val.size.x = decode_float(&buf[sizeof(float) * 3]);
  381. val.size.y = decode_float(&buf[sizeof(float) * 4]);
  382. val.size.z = decode_float(&buf[sizeof(float) * 5]);
  383. if (r_len) {
  384. (*r_len) += sizeof(float) * 6;
  385. }
  386. }
  387. r_variant = val;
  388. } break;
  389. case Variant::BASIS: {
  390. Basis val;
  391. if (header & HEADER_DATA_FLAG_64) {
  392. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 9, ERR_INVALID_DATA);
  393. for (int i = 0; i < 3; i++) {
  394. for (int j = 0; j < 3; j++) {
  395. val.rows[i][j] = decode_double(&buf[(i * 3 + j) * sizeof(double)]);
  396. }
  397. }
  398. if (r_len) {
  399. (*r_len) += sizeof(double) * 9;
  400. }
  401. } else {
  402. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 9, ERR_INVALID_DATA);
  403. for (int i = 0; i < 3; i++) {
  404. for (int j = 0; j < 3; j++) {
  405. val.rows[i][j] = decode_float(&buf[(i * 3 + j) * sizeof(float)]);
  406. }
  407. }
  408. if (r_len) {
  409. (*r_len) += sizeof(float) * 9;
  410. }
  411. }
  412. r_variant = val;
  413. } break;
  414. case Variant::TRANSFORM3D: {
  415. Transform3D val;
  416. if (header & HEADER_DATA_FLAG_64) {
  417. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 12, ERR_INVALID_DATA);
  418. for (int i = 0; i < 3; i++) {
  419. for (int j = 0; j < 3; j++) {
  420. val.basis.rows[i][j] = decode_double(&buf[(i * 3 + j) * sizeof(double)]);
  421. }
  422. }
  423. val.origin[0] = decode_double(&buf[sizeof(double) * 9]);
  424. val.origin[1] = decode_double(&buf[sizeof(double) * 10]);
  425. val.origin[2] = decode_double(&buf[sizeof(double) * 11]);
  426. if (r_len) {
  427. (*r_len) += sizeof(double) * 12;
  428. }
  429. } else {
  430. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 12, ERR_INVALID_DATA);
  431. for (int i = 0; i < 3; i++) {
  432. for (int j = 0; j < 3; j++) {
  433. val.basis.rows[i][j] = decode_float(&buf[(i * 3 + j) * sizeof(float)]);
  434. }
  435. }
  436. val.origin[0] = decode_float(&buf[sizeof(float) * 9]);
  437. val.origin[1] = decode_float(&buf[sizeof(float) * 10]);
  438. val.origin[2] = decode_float(&buf[sizeof(float) * 11]);
  439. if (r_len) {
  440. (*r_len) += sizeof(float) * 12;
  441. }
  442. }
  443. r_variant = val;
  444. } break;
  445. case Variant::PROJECTION: {
  446. Projection val;
  447. if (header & HEADER_DATA_FLAG_64) {
  448. ERR_FAIL_COND_V((size_t)len < sizeof(double) * 16, ERR_INVALID_DATA);
  449. for (int i = 0; i < 4; i++) {
  450. for (int j = 0; j < 4; j++) {
  451. val.columns[i][j] = decode_double(&buf[(i * 4 + j) * sizeof(double)]);
  452. }
  453. }
  454. if (r_len) {
  455. (*r_len) += sizeof(double) * 16;
  456. }
  457. } else {
  458. ERR_FAIL_COND_V((size_t)len < sizeof(float) * 16, ERR_INVALID_DATA);
  459. for (int i = 0; i < 4; i++) {
  460. for (int j = 0; j < 4; j++) {
  461. val.columns[i][j] = decode_float(&buf[(i * 4 + j) * sizeof(float)]);
  462. }
  463. }
  464. if (r_len) {
  465. (*r_len) += sizeof(float) * 16;
  466. }
  467. }
  468. r_variant = val;
  469. } break;
  470. // misc types
  471. case Variant::COLOR: {
  472. ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
  473. Color val;
  474. val.r = decode_float(&buf[0]);
  475. val.g = decode_float(&buf[4]);
  476. val.b = decode_float(&buf[8]);
  477. val.a = decode_float(&buf[12]);
  478. r_variant = val;
  479. if (r_len) {
  480. (*r_len) += 4 * 4; // Colors should always be in single-precision.
  481. }
  482. } break;
  483. case Variant::STRING_NAME: {
  484. String str;
  485. Error err = _decode_string(buf, len, r_len, str);
  486. if (err) {
  487. return err;
  488. }
  489. r_variant = StringName(str);
  490. } break;
  491. case Variant::NODE_PATH: {
  492. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  493. int32_t strlen = decode_uint32(buf);
  494. if (strlen & 0x80000000) {
  495. //new format
  496. ERR_FAIL_COND_V(len < 12, ERR_INVALID_DATA);
  497. Vector<StringName> names;
  498. Vector<StringName> subnames;
  499. uint32_t namecount = strlen &= 0x7FFFFFFF;
  500. uint32_t subnamecount = decode_uint32(buf + 4);
  501. uint32_t np_flags = decode_uint32(buf + 8);
  502. len -= 12;
  503. buf += 12;
  504. if (np_flags & 2) { // Obsolete format with property separate from subpath.
  505. subnamecount++;
  506. }
  507. uint32_t total = namecount + subnamecount;
  508. if (r_len) {
  509. (*r_len) += 12;
  510. }
  511. for (uint32_t i = 0; i < total; i++) {
  512. String str;
  513. Error err = _decode_string(buf, len, r_len, str);
  514. if (err) {
  515. return err;
  516. }
  517. if (i < namecount) {
  518. names.push_back(str);
  519. } else {
  520. subnames.push_back(str);
  521. }
  522. }
  523. r_variant = NodePath(names, subnames, np_flags & 1);
  524. } else {
  525. //old format, just a string
  526. ERR_FAIL_V(ERR_INVALID_DATA);
  527. }
  528. } break;
  529. case Variant::RID: {
  530. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  531. uint64_t id = decode_uint64(buf);
  532. if (r_len) {
  533. (*r_len) += 8;
  534. }
  535. r_variant = RID::from_uint64(id);
  536. } break;
  537. case Variant::OBJECT: {
  538. if (header & HEADER_DATA_FLAG_OBJECT_AS_ID) {
  539. // This _is_ allowed.
  540. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  541. ObjectID val = ObjectID(decode_uint64(buf));
  542. if (r_len) {
  543. (*r_len) += 8;
  544. }
  545. if (val.is_null()) {
  546. r_variant = (Object *)nullptr;
  547. } else {
  548. Ref<EncodedObjectAsID> obj_as_id;
  549. obj_as_id.instantiate();
  550. obj_as_id->set_object_id(val);
  551. r_variant = obj_as_id;
  552. }
  553. } else {
  554. ERR_FAIL_COND_V(!p_allow_objects, ERR_UNAUTHORIZED);
  555. String str;
  556. Error err = _decode_string(buf, len, r_len, str);
  557. if (err) {
  558. return err;
  559. }
  560. if (str.is_empty()) {
  561. r_variant = (Object *)nullptr;
  562. } else {
  563. ERR_FAIL_COND_V(!ClassDB::can_instantiate(str), ERR_INVALID_DATA);
  564. Object *obj = ClassDB::instantiate(str);
  565. ERR_FAIL_NULL_V(obj, ERR_UNAVAILABLE);
  566. // Avoid premature free `RefCounted`. This must be done before properties are initialized,
  567. // since script functions (setters, implicit initializer) may be called. See GH-68666.
  568. Variant variant;
  569. if (Object::cast_to<RefCounted>(obj)) {
  570. Ref<RefCounted> ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
  571. variant = ref;
  572. } else {
  573. variant = obj;
  574. }
  575. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  576. int32_t count = decode_uint32(buf);
  577. buf += 4;
  578. len -= 4;
  579. if (r_len) {
  580. (*r_len) += 4; // Size of count number.
  581. }
  582. for (int i = 0; i < count; i++) {
  583. str = String();
  584. err = _decode_string(buf, len, r_len, str);
  585. if (err) {
  586. return err;
  587. }
  588. Variant value;
  589. int used;
  590. err = decode_variant(value, buf, len, &used, p_allow_objects, p_depth + 1);
  591. if (err) {
  592. return err;
  593. }
  594. buf += used;
  595. len -= used;
  596. if (r_len) {
  597. (*r_len) += used;
  598. }
  599. if (str == "script" && value.get_type() != Variant::NIL) {
  600. ERR_FAIL_COND_V_MSG(value.get_type() != Variant::STRING, ERR_INVALID_DATA, "Invalid value for \"script\" property, expected script path as String.");
  601. String path = value;
  602. ERR_FAIL_COND_V_MSG(path.is_empty() || !path.begins_with("res://") || !ResourceLoader::exists(path, "Script"), ERR_INVALID_DATA, "Invalid script path: '" + path + "'.");
  603. Ref<Script> script = ResourceLoader::load(path, "Script");
  604. ERR_FAIL_COND_V_MSG(script.is_null(), ERR_INVALID_DATA, "Can't load script at path: '" + path + "'.");
  605. obj->set_script(script);
  606. } else {
  607. obj->set(str, value);
  608. }
  609. }
  610. r_variant = variant;
  611. }
  612. }
  613. } break;
  614. case Variant::CALLABLE: {
  615. r_variant = Callable();
  616. } break;
  617. case Variant::SIGNAL: {
  618. String name;
  619. Error err = _decode_string(buf, len, r_len, name);
  620. if (err) {
  621. return err;
  622. }
  623. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  624. ObjectID id = ObjectID(decode_uint64(buf));
  625. if (r_len) {
  626. (*r_len) += 8;
  627. }
  628. r_variant = Signal(id, StringName(name));
  629. } break;
  630. case Variant::DICTIONARY: {
  631. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  632. int32_t count = decode_uint32(buf);
  633. // bool shared = count&0x80000000;
  634. count &= 0x7FFFFFFF;
  635. buf += 4;
  636. len -= 4;
  637. if (r_len) {
  638. (*r_len) += 4; // Size of count number.
  639. }
  640. Dictionary d;
  641. for (int i = 0; i < count; i++) {
  642. Variant key, value;
  643. int used;
  644. Error err = decode_variant(key, buf, len, &used, p_allow_objects, p_depth + 1);
  645. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant.");
  646. buf += used;
  647. len -= used;
  648. if (r_len) {
  649. (*r_len) += used;
  650. }
  651. err = decode_variant(value, buf, len, &used, p_allow_objects, p_depth + 1);
  652. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant.");
  653. buf += used;
  654. len -= used;
  655. if (r_len) {
  656. (*r_len) += used;
  657. }
  658. d[key] = value;
  659. }
  660. r_variant = d;
  661. } break;
  662. case Variant::ARRAY: {
  663. Variant::Type builtin_type = Variant::VARIANT_MAX;
  664. StringName class_name;
  665. Ref<Script> script;
  666. switch (header & HEADER_DATA_FIELD_TYPED_ARRAY_MASK) {
  667. case HEADER_DATA_FIELD_TYPED_ARRAY_NONE:
  668. break; // Untyped array.
  669. case HEADER_DATA_FIELD_TYPED_ARRAY_BUILTIN: {
  670. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  671. int32_t bt = decode_uint32(buf);
  672. buf += 4;
  673. len -= 4;
  674. if (r_len) {
  675. (*r_len) += 4;
  676. }
  677. ERR_FAIL_INDEX_V(bt, Variant::VARIANT_MAX, ERR_INVALID_DATA);
  678. builtin_type = (Variant::Type)bt;
  679. if (!p_allow_objects && builtin_type == Variant::OBJECT) {
  680. class_name = EncodedObjectAsID::get_class_static();
  681. }
  682. } break;
  683. case HEADER_DATA_FIELD_TYPED_ARRAY_CLASS_NAME: {
  684. String str;
  685. Error err = _decode_string(buf, len, r_len, str);
  686. if (err) {
  687. return err;
  688. }
  689. builtin_type = Variant::OBJECT;
  690. if (p_allow_objects) {
  691. class_name = str;
  692. } else {
  693. class_name = EncodedObjectAsID::get_class_static();
  694. }
  695. } break;
  696. case HEADER_DATA_FIELD_TYPED_ARRAY_SCRIPT: {
  697. String path;
  698. Error err = _decode_string(buf, len, r_len, path);
  699. if (err) {
  700. return err;
  701. }
  702. builtin_type = Variant::OBJECT;
  703. if (p_allow_objects) {
  704. ERR_FAIL_COND_V_MSG(path.is_empty() || !path.begins_with("res://") || !ResourceLoader::exists(path, "Script"), ERR_INVALID_DATA, "Invalid script path: '" + path + "'.");
  705. script = ResourceLoader::load(path, "Script");
  706. ERR_FAIL_COND_V_MSG(script.is_null(), ERR_INVALID_DATA, "Can't load script at path: '" + path + "'.");
  707. class_name = script->get_instance_base_type();
  708. } else {
  709. class_name = EncodedObjectAsID::get_class_static();
  710. }
  711. } break;
  712. default:
  713. ERR_FAIL_V(ERR_INVALID_DATA); // Future proofing.
  714. }
  715. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  716. int32_t count = decode_uint32(buf);
  717. // bool shared = count&0x80000000;
  718. count &= 0x7FFFFFFF;
  719. buf += 4;
  720. len -= 4;
  721. if (r_len) {
  722. (*r_len) += 4; // Size of count number.
  723. }
  724. Array varr;
  725. if (builtin_type != Variant::VARIANT_MAX) {
  726. varr.set_typed(builtin_type, class_name, script);
  727. }
  728. for (int i = 0; i < count; i++) {
  729. int used = 0;
  730. Variant v;
  731. Error err = decode_variant(v, buf, len, &used, p_allow_objects, p_depth + 1);
  732. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant.");
  733. buf += used;
  734. len -= used;
  735. varr.push_back(v);
  736. if (r_len) {
  737. (*r_len) += used;
  738. }
  739. }
  740. r_variant = varr;
  741. } break;
  742. // arrays
  743. case Variant::PACKED_BYTE_ARRAY: {
  744. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  745. int32_t count = decode_uint32(buf);
  746. buf += 4;
  747. len -= 4;
  748. ERR_FAIL_COND_V(count < 0 || count > len, ERR_INVALID_DATA);
  749. Vector<uint8_t> data;
  750. if (count) {
  751. data.resize(count);
  752. uint8_t *w = data.ptrw();
  753. for (int32_t i = 0; i < count; i++) {
  754. w[i] = buf[i];
  755. }
  756. }
  757. r_variant = data;
  758. if (r_len) {
  759. if (count % 4) {
  760. (*r_len) += 4 - count % 4;
  761. }
  762. (*r_len) += 4 + count;
  763. }
  764. } break;
  765. case Variant::PACKED_INT32_ARRAY: {
  766. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  767. int32_t count = decode_uint32(buf);
  768. buf += 4;
  769. len -= 4;
  770. ERR_FAIL_MUL_OF(count, 4, ERR_INVALID_DATA);
  771. ERR_FAIL_COND_V(count < 0 || count * 4 > len, ERR_INVALID_DATA);
  772. Vector<int32_t> data;
  773. if (count) {
  774. //const int*rbuf=(const int*)buf;
  775. data.resize(count);
  776. int32_t *w = data.ptrw();
  777. for (int32_t i = 0; i < count; i++) {
  778. w[i] = decode_uint32(&buf[i * 4]);
  779. }
  780. }
  781. r_variant = Variant(data);
  782. if (r_len) {
  783. (*r_len) += 4 + count * sizeof(int32_t);
  784. }
  785. } break;
  786. case Variant::PACKED_INT64_ARRAY: {
  787. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  788. int32_t count = decode_uint32(buf);
  789. buf += 4;
  790. len -= 4;
  791. ERR_FAIL_MUL_OF(count, 8, ERR_INVALID_DATA);
  792. ERR_FAIL_COND_V(count < 0 || count * 8 > len, ERR_INVALID_DATA);
  793. Vector<int64_t> data;
  794. if (count) {
  795. //const int*rbuf=(const int*)buf;
  796. data.resize(count);
  797. int64_t *w = data.ptrw();
  798. for (int64_t i = 0; i < count; i++) {
  799. w[i] = decode_uint64(&buf[i * 8]);
  800. }
  801. }
  802. r_variant = Variant(data);
  803. if (r_len) {
  804. (*r_len) += 4 + count * sizeof(int64_t);
  805. }
  806. } break;
  807. case Variant::PACKED_FLOAT32_ARRAY: {
  808. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  809. int32_t count = decode_uint32(buf);
  810. buf += 4;
  811. len -= 4;
  812. ERR_FAIL_MUL_OF(count, 4, ERR_INVALID_DATA);
  813. ERR_FAIL_COND_V(count < 0 || count * 4 > len, ERR_INVALID_DATA);
  814. Vector<float> data;
  815. if (count) {
  816. //const float*rbuf=(const float*)buf;
  817. data.resize(count);
  818. float *w = data.ptrw();
  819. for (int32_t i = 0; i < count; i++) {
  820. w[i] = decode_float(&buf[i * 4]);
  821. }
  822. }
  823. r_variant = data;
  824. if (r_len) {
  825. (*r_len) += 4 + count * sizeof(float);
  826. }
  827. } break;
  828. case Variant::PACKED_FLOAT64_ARRAY: {
  829. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  830. int32_t count = decode_uint32(buf);
  831. buf += 4;
  832. len -= 4;
  833. ERR_FAIL_MUL_OF(count, 8, ERR_INVALID_DATA);
  834. ERR_FAIL_COND_V(count < 0 || count * 8 > len, ERR_INVALID_DATA);
  835. Vector<double> data;
  836. if (count) {
  837. data.resize(count);
  838. double *w = data.ptrw();
  839. for (int64_t i = 0; i < count; i++) {
  840. w[i] = decode_double(&buf[i * 8]);
  841. }
  842. }
  843. r_variant = data;
  844. if (r_len) {
  845. (*r_len) += 4 + count * sizeof(double);
  846. }
  847. } break;
  848. case Variant::PACKED_STRING_ARRAY: {
  849. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  850. int32_t count = decode_uint32(buf);
  851. Vector<String> strings;
  852. buf += 4;
  853. len -= 4;
  854. if (r_len) {
  855. (*r_len) += 4; // Size of count number.
  856. }
  857. for (int32_t i = 0; i < count; i++) {
  858. String str;
  859. Error err = _decode_string(buf, len, r_len, str);
  860. if (err) {
  861. return err;
  862. }
  863. strings.push_back(str);
  864. }
  865. r_variant = strings;
  866. } break;
  867. case Variant::PACKED_VECTOR2_ARRAY: {
  868. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  869. int32_t count = decode_uint32(buf);
  870. buf += 4;
  871. len -= 4;
  872. Vector<Vector2> varray;
  873. if (header & HEADER_DATA_FLAG_64) {
  874. ERR_FAIL_MUL_OF(count, sizeof(double) * 2, ERR_INVALID_DATA);
  875. ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 2 > (size_t)len, ERR_INVALID_DATA);
  876. if (r_len) {
  877. (*r_len) += 4; // Size of count number.
  878. }
  879. if (count) {
  880. varray.resize(count);
  881. Vector2 *w = varray.ptrw();
  882. for (int32_t i = 0; i < count; i++) {
  883. w[i].x = decode_double(buf + i * sizeof(double) * 2 + sizeof(double) * 0);
  884. w[i].y = decode_double(buf + i * sizeof(double) * 2 + sizeof(double) * 1);
  885. }
  886. int adv = sizeof(double) * 2 * count;
  887. if (r_len) {
  888. (*r_len) += adv;
  889. }
  890. len -= adv;
  891. buf += adv;
  892. }
  893. } else {
  894. ERR_FAIL_MUL_OF(count, sizeof(float) * 2, ERR_INVALID_DATA);
  895. ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 2 > (size_t)len, ERR_INVALID_DATA);
  896. if (r_len) {
  897. (*r_len) += 4; // Size of count number.
  898. }
  899. if (count) {
  900. varray.resize(count);
  901. Vector2 *w = varray.ptrw();
  902. for (int32_t i = 0; i < count; i++) {
  903. w[i].x = decode_float(buf + i * sizeof(float) * 2 + sizeof(float) * 0);
  904. w[i].y = decode_float(buf + i * sizeof(float) * 2 + sizeof(float) * 1);
  905. }
  906. int adv = sizeof(float) * 2 * count;
  907. if (r_len) {
  908. (*r_len) += adv;
  909. }
  910. }
  911. }
  912. r_variant = varray;
  913. } break;
  914. case Variant::PACKED_VECTOR3_ARRAY: {
  915. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  916. int32_t count = decode_uint32(buf);
  917. buf += 4;
  918. len -= 4;
  919. Vector<Vector3> varray;
  920. if (header & HEADER_DATA_FLAG_64) {
  921. ERR_FAIL_MUL_OF(count, sizeof(double) * 3, ERR_INVALID_DATA);
  922. ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 3 > (size_t)len, ERR_INVALID_DATA);
  923. if (r_len) {
  924. (*r_len) += 4; // Size of count number.
  925. }
  926. if (count) {
  927. varray.resize(count);
  928. Vector3 *w = varray.ptrw();
  929. for (int32_t i = 0; i < count; i++) {
  930. w[i].x = decode_double(buf + i * sizeof(double) * 3 + sizeof(double) * 0);
  931. w[i].y = decode_double(buf + i * sizeof(double) * 3 + sizeof(double) * 1);
  932. w[i].z = decode_double(buf + i * sizeof(double) * 3 + sizeof(double) * 2);
  933. }
  934. int adv = sizeof(double) * 3 * count;
  935. if (r_len) {
  936. (*r_len) += adv;
  937. }
  938. len -= adv;
  939. buf += adv;
  940. }
  941. } else {
  942. ERR_FAIL_MUL_OF(count, sizeof(float) * 3, ERR_INVALID_DATA);
  943. ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 3 > (size_t)len, ERR_INVALID_DATA);
  944. if (r_len) {
  945. (*r_len) += 4; // Size of count number.
  946. }
  947. if (count) {
  948. varray.resize(count);
  949. Vector3 *w = varray.ptrw();
  950. for (int32_t i = 0; i < count; i++) {
  951. w[i].x = decode_float(buf + i * sizeof(float) * 3 + sizeof(float) * 0);
  952. w[i].y = decode_float(buf + i * sizeof(float) * 3 + sizeof(float) * 1);
  953. w[i].z = decode_float(buf + i * sizeof(float) * 3 + sizeof(float) * 2);
  954. }
  955. int adv = sizeof(float) * 3 * count;
  956. if (r_len) {
  957. (*r_len) += adv;
  958. }
  959. len -= adv;
  960. buf += adv;
  961. }
  962. }
  963. r_variant = varray;
  964. } break;
  965. case Variant::PACKED_COLOR_ARRAY: {
  966. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  967. int32_t count = decode_uint32(buf);
  968. buf += 4;
  969. len -= 4;
  970. ERR_FAIL_MUL_OF(count, 4 * 4, ERR_INVALID_DATA);
  971. ERR_FAIL_COND_V(count < 0 || count * 4 * 4 > len, ERR_INVALID_DATA);
  972. Vector<Color> carray;
  973. if (r_len) {
  974. (*r_len) += 4; // Size of count number.
  975. }
  976. if (count) {
  977. carray.resize(count);
  978. Color *w = carray.ptrw();
  979. for (int32_t i = 0; i < count; i++) {
  980. // Colors should always be in single-precision.
  981. w[i].r = decode_float(buf + i * 4 * 4 + 4 * 0);
  982. w[i].g = decode_float(buf + i * 4 * 4 + 4 * 1);
  983. w[i].b = decode_float(buf + i * 4 * 4 + 4 * 2);
  984. w[i].a = decode_float(buf + i * 4 * 4 + 4 * 3);
  985. }
  986. int adv = 4 * 4 * count;
  987. if (r_len) {
  988. (*r_len) += adv;
  989. }
  990. }
  991. r_variant = carray;
  992. } break;
  993. case Variant::PACKED_VECTOR4_ARRAY: {
  994. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  995. int32_t count = decode_uint32(buf);
  996. buf += 4;
  997. len -= 4;
  998. Vector<Vector4> varray;
  999. if (header & HEADER_DATA_FLAG_64) {
  1000. ERR_FAIL_MUL_OF(count, sizeof(double) * 4, ERR_INVALID_DATA);
  1001. ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 4 > (size_t)len, ERR_INVALID_DATA);
  1002. if (r_len) {
  1003. (*r_len) += 4; // Size of count number.
  1004. }
  1005. if (count) {
  1006. varray.resize(count);
  1007. Vector4 *w = varray.ptrw();
  1008. for (int32_t i = 0; i < count; i++) {
  1009. w[i].x = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 0);
  1010. w[i].y = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 1);
  1011. w[i].z = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 2);
  1012. w[i].w = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 3);
  1013. }
  1014. int adv = sizeof(double) * 4 * count;
  1015. if (r_len) {
  1016. (*r_len) += adv;
  1017. }
  1018. len -= adv;
  1019. buf += adv;
  1020. }
  1021. } else {
  1022. ERR_FAIL_MUL_OF(count, sizeof(float) * 4, ERR_INVALID_DATA);
  1023. ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 4 > (size_t)len, ERR_INVALID_DATA);
  1024. if (r_len) {
  1025. (*r_len) += 4; // Size of count number.
  1026. }
  1027. if (count) {
  1028. varray.resize(count);
  1029. Vector4 *w = varray.ptrw();
  1030. for (int32_t i = 0; i < count; i++) {
  1031. w[i].x = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 0);
  1032. w[i].y = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 1);
  1033. w[i].z = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 2);
  1034. w[i].w = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 3);
  1035. }
  1036. int adv = sizeof(float) * 4 * count;
  1037. if (r_len) {
  1038. (*r_len) += adv;
  1039. }
  1040. len -= adv;
  1041. buf += adv;
  1042. }
  1043. }
  1044. r_variant = varray;
  1045. } break;
  1046. default: {
  1047. ERR_FAIL_V(ERR_BUG);
  1048. }
  1049. }
  1050. return OK;
  1051. }
  1052. static void _encode_string(const String &p_string, uint8_t *&buf, int &r_len) {
  1053. CharString utf8 = p_string.utf8();
  1054. if (buf) {
  1055. encode_uint32(utf8.length(), buf);
  1056. buf += 4;
  1057. memcpy(buf, utf8.get_data(), utf8.length());
  1058. buf += utf8.length();
  1059. }
  1060. r_len += 4 + utf8.length();
  1061. while (r_len % 4) {
  1062. r_len++; //pad
  1063. if (buf) {
  1064. *(buf++) = 0;
  1065. }
  1066. }
  1067. }
  1068. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects, int p_depth) {
  1069. ERR_FAIL_COND_V_MSG(p_depth > Variant::MAX_RECURSION_DEPTH, ERR_OUT_OF_MEMORY, "Potential infinite recursion detected. Bailing.");
  1070. uint8_t *buf = r_buffer;
  1071. r_len = 0;
  1072. uint32_t header = p_variant.get_type();
  1073. switch (p_variant.get_type()) {
  1074. case Variant::INT: {
  1075. int64_t val = p_variant;
  1076. if (val > (int64_t)INT_MAX || val < (int64_t)INT_MIN) {
  1077. header |= HEADER_DATA_FLAG_64;
  1078. }
  1079. } break;
  1080. case Variant::FLOAT: {
  1081. double d = p_variant;
  1082. float f = d;
  1083. if (double(f) != d) {
  1084. header |= HEADER_DATA_FLAG_64;
  1085. }
  1086. } break;
  1087. case Variant::OBJECT: {
  1088. // Test for potential wrong values sent by the debugger when it breaks.
  1089. Object *obj = p_variant.get_validated_object();
  1090. if (!obj) {
  1091. // Object is invalid, send a nullptr instead.
  1092. if (buf) {
  1093. encode_uint32(Variant::NIL, buf);
  1094. }
  1095. r_len += 4;
  1096. return OK;
  1097. }
  1098. if (!p_full_objects) {
  1099. header |= HEADER_DATA_FLAG_OBJECT_AS_ID;
  1100. }
  1101. } break;
  1102. case Variant::ARRAY: {
  1103. Array array = p_variant;
  1104. if (array.is_typed()) {
  1105. Ref<Script> script = array.get_typed_script();
  1106. if (script.is_valid()) {
  1107. header |= HEADER_DATA_FIELD_TYPED_ARRAY_SCRIPT;
  1108. } else if (array.get_typed_class_name() != StringName()) {
  1109. header |= HEADER_DATA_FIELD_TYPED_ARRAY_CLASS_NAME;
  1110. } else {
  1111. header |= HEADER_DATA_FIELD_TYPED_ARRAY_BUILTIN;
  1112. }
  1113. }
  1114. } break;
  1115. #ifdef REAL_T_IS_DOUBLE
  1116. case Variant::VECTOR2:
  1117. case Variant::VECTOR3:
  1118. case Variant::VECTOR4:
  1119. case Variant::PACKED_VECTOR2_ARRAY:
  1120. case Variant::PACKED_VECTOR3_ARRAY:
  1121. case Variant::PACKED_VECTOR4_ARRAY:
  1122. case Variant::TRANSFORM2D:
  1123. case Variant::TRANSFORM3D:
  1124. case Variant::PROJECTION:
  1125. case Variant::QUATERNION:
  1126. case Variant::PLANE:
  1127. case Variant::BASIS:
  1128. case Variant::RECT2:
  1129. case Variant::AABB: {
  1130. header |= HEADER_DATA_FLAG_64;
  1131. } break;
  1132. #endif // REAL_T_IS_DOUBLE
  1133. default: {
  1134. } // nothing to do at this stage
  1135. }
  1136. if (buf) {
  1137. encode_uint32(header, buf);
  1138. buf += 4;
  1139. }
  1140. r_len += 4;
  1141. switch (p_variant.get_type()) {
  1142. case Variant::NIL: {
  1143. //nothing to do
  1144. } break;
  1145. case Variant::BOOL: {
  1146. if (buf) {
  1147. encode_uint32(p_variant.operator bool(), buf);
  1148. }
  1149. r_len += 4;
  1150. } break;
  1151. case Variant::INT: {
  1152. if (header & HEADER_DATA_FLAG_64) {
  1153. //64 bits
  1154. if (buf) {
  1155. encode_uint64(p_variant.operator int64_t(), buf);
  1156. }
  1157. r_len += 8;
  1158. } else {
  1159. if (buf) {
  1160. encode_uint32(p_variant.operator int32_t(), buf);
  1161. }
  1162. r_len += 4;
  1163. }
  1164. } break;
  1165. case Variant::FLOAT: {
  1166. if (header & HEADER_DATA_FLAG_64) {
  1167. if (buf) {
  1168. encode_double(p_variant.operator double(), buf);
  1169. }
  1170. r_len += 8;
  1171. } else {
  1172. if (buf) {
  1173. encode_float(p_variant.operator float(), buf);
  1174. }
  1175. r_len += 4;
  1176. }
  1177. } break;
  1178. case Variant::NODE_PATH: {
  1179. NodePath np = p_variant;
  1180. if (buf) {
  1181. encode_uint32(uint32_t(np.get_name_count()) | 0x80000000, buf); //for compatibility with the old format
  1182. encode_uint32(np.get_subname_count(), buf + 4);
  1183. uint32_t np_flags = 0;
  1184. if (np.is_absolute()) {
  1185. np_flags |= 1;
  1186. }
  1187. encode_uint32(np_flags, buf + 8);
  1188. buf += 12;
  1189. }
  1190. r_len += 12;
  1191. int total = np.get_name_count() + np.get_subname_count();
  1192. for (int i = 0; i < total; i++) {
  1193. String str;
  1194. if (i < np.get_name_count()) {
  1195. str = np.get_name(i);
  1196. } else {
  1197. str = np.get_subname(i - np.get_name_count());
  1198. }
  1199. CharString utf8 = str.utf8();
  1200. int pad = 0;
  1201. if (utf8.length() % 4) {
  1202. pad = 4 - utf8.length() % 4;
  1203. }
  1204. if (buf) {
  1205. encode_uint32(utf8.length(), buf);
  1206. buf += 4;
  1207. memcpy(buf, utf8.get_data(), utf8.length());
  1208. buf += pad + utf8.length();
  1209. }
  1210. r_len += 4 + utf8.length() + pad;
  1211. }
  1212. } break;
  1213. case Variant::STRING:
  1214. case Variant::STRING_NAME: {
  1215. _encode_string(p_variant, buf, r_len);
  1216. } break;
  1217. // math types
  1218. case Variant::VECTOR2: {
  1219. if (buf) {
  1220. Vector2 v2 = p_variant;
  1221. encode_real(v2.x, &buf[0]);
  1222. encode_real(v2.y, &buf[sizeof(real_t)]);
  1223. }
  1224. r_len += 2 * sizeof(real_t);
  1225. } break;
  1226. case Variant::VECTOR2I: {
  1227. if (buf) {
  1228. Vector2i v2 = p_variant;
  1229. encode_uint32(v2.x, &buf[0]);
  1230. encode_uint32(v2.y, &buf[4]);
  1231. }
  1232. r_len += 2 * 4;
  1233. } break;
  1234. case Variant::RECT2: {
  1235. if (buf) {
  1236. Rect2 r2 = p_variant;
  1237. encode_real(r2.position.x, &buf[0]);
  1238. encode_real(r2.position.y, &buf[sizeof(real_t)]);
  1239. encode_real(r2.size.x, &buf[sizeof(real_t) * 2]);
  1240. encode_real(r2.size.y, &buf[sizeof(real_t) * 3]);
  1241. }
  1242. r_len += 4 * sizeof(real_t);
  1243. } break;
  1244. case Variant::RECT2I: {
  1245. if (buf) {
  1246. Rect2i r2 = p_variant;
  1247. encode_uint32(r2.position.x, &buf[0]);
  1248. encode_uint32(r2.position.y, &buf[4]);
  1249. encode_uint32(r2.size.x, &buf[8]);
  1250. encode_uint32(r2.size.y, &buf[12]);
  1251. }
  1252. r_len += 4 * 4;
  1253. } break;
  1254. case Variant::VECTOR3: {
  1255. if (buf) {
  1256. Vector3 v3 = p_variant;
  1257. encode_real(v3.x, &buf[0]);
  1258. encode_real(v3.y, &buf[sizeof(real_t)]);
  1259. encode_real(v3.z, &buf[sizeof(real_t) * 2]);
  1260. }
  1261. r_len += 3 * sizeof(real_t);
  1262. } break;
  1263. case Variant::VECTOR3I: {
  1264. if (buf) {
  1265. Vector3i v3 = p_variant;
  1266. encode_uint32(v3.x, &buf[0]);
  1267. encode_uint32(v3.y, &buf[4]);
  1268. encode_uint32(v3.z, &buf[8]);
  1269. }
  1270. r_len += 3 * 4;
  1271. } break;
  1272. case Variant::TRANSFORM2D: {
  1273. if (buf) {
  1274. Transform2D val = p_variant;
  1275. for (int i = 0; i < 3; i++) {
  1276. for (int j = 0; j < 2; j++) {
  1277. memcpy(&buf[(i * 2 + j) * sizeof(real_t)], &val.columns[i][j], sizeof(real_t));
  1278. }
  1279. }
  1280. }
  1281. r_len += 6 * sizeof(real_t);
  1282. } break;
  1283. case Variant::VECTOR4: {
  1284. if (buf) {
  1285. Vector4 v4 = p_variant;
  1286. encode_real(v4.x, &buf[0]);
  1287. encode_real(v4.y, &buf[sizeof(real_t)]);
  1288. encode_real(v4.z, &buf[sizeof(real_t) * 2]);
  1289. encode_real(v4.w, &buf[sizeof(real_t) * 3]);
  1290. }
  1291. r_len += 4 * sizeof(real_t);
  1292. } break;
  1293. case Variant::VECTOR4I: {
  1294. if (buf) {
  1295. Vector4i v4 = p_variant;
  1296. encode_uint32(v4.x, &buf[0]);
  1297. encode_uint32(v4.y, &buf[4]);
  1298. encode_uint32(v4.z, &buf[8]);
  1299. encode_uint32(v4.w, &buf[12]);
  1300. }
  1301. r_len += 4 * 4;
  1302. } break;
  1303. case Variant::PLANE: {
  1304. if (buf) {
  1305. Plane p = p_variant;
  1306. encode_real(p.normal.x, &buf[0]);
  1307. encode_real(p.normal.y, &buf[sizeof(real_t)]);
  1308. encode_real(p.normal.z, &buf[sizeof(real_t) * 2]);
  1309. encode_real(p.d, &buf[sizeof(real_t) * 3]);
  1310. }
  1311. r_len += 4 * sizeof(real_t);
  1312. } break;
  1313. case Variant::QUATERNION: {
  1314. if (buf) {
  1315. Quaternion q = p_variant;
  1316. encode_real(q.x, &buf[0]);
  1317. encode_real(q.y, &buf[sizeof(real_t)]);
  1318. encode_real(q.z, &buf[sizeof(real_t) * 2]);
  1319. encode_real(q.w, &buf[sizeof(real_t) * 3]);
  1320. }
  1321. r_len += 4 * sizeof(real_t);
  1322. } break;
  1323. case Variant::AABB: {
  1324. if (buf) {
  1325. AABB aabb = p_variant;
  1326. encode_real(aabb.position.x, &buf[0]);
  1327. encode_real(aabb.position.y, &buf[sizeof(real_t)]);
  1328. encode_real(aabb.position.z, &buf[sizeof(real_t) * 2]);
  1329. encode_real(aabb.size.x, &buf[sizeof(real_t) * 3]);
  1330. encode_real(aabb.size.y, &buf[sizeof(real_t) * 4]);
  1331. encode_real(aabb.size.z, &buf[sizeof(real_t) * 5]);
  1332. }
  1333. r_len += 6 * sizeof(real_t);
  1334. } break;
  1335. case Variant::BASIS: {
  1336. if (buf) {
  1337. Basis val = p_variant;
  1338. for (int i = 0; i < 3; i++) {
  1339. for (int j = 0; j < 3; j++) {
  1340. memcpy(&buf[(i * 3 + j) * sizeof(real_t)], &val.rows[i][j], sizeof(real_t));
  1341. }
  1342. }
  1343. }
  1344. r_len += 9 * sizeof(real_t);
  1345. } break;
  1346. case Variant::TRANSFORM3D: {
  1347. if (buf) {
  1348. Transform3D val = p_variant;
  1349. for (int i = 0; i < 3; i++) {
  1350. for (int j = 0; j < 3; j++) {
  1351. memcpy(&buf[(i * 3 + j) * sizeof(real_t)], &val.basis.rows[i][j], sizeof(real_t));
  1352. }
  1353. }
  1354. encode_real(val.origin.x, &buf[sizeof(real_t) * 9]);
  1355. encode_real(val.origin.y, &buf[sizeof(real_t) * 10]);
  1356. encode_real(val.origin.z, &buf[sizeof(real_t) * 11]);
  1357. }
  1358. r_len += 12 * sizeof(real_t);
  1359. } break;
  1360. case Variant::PROJECTION: {
  1361. if (buf) {
  1362. Projection val = p_variant;
  1363. for (int i = 0; i < 4; i++) {
  1364. for (int j = 0; j < 4; j++) {
  1365. memcpy(&buf[(i * 4 + j) * sizeof(real_t)], &val.columns[i][j], sizeof(real_t));
  1366. }
  1367. }
  1368. }
  1369. r_len += 16 * sizeof(real_t);
  1370. } break;
  1371. // misc types
  1372. case Variant::COLOR: {
  1373. if (buf) {
  1374. Color c = p_variant;
  1375. encode_float(c.r, &buf[0]);
  1376. encode_float(c.g, &buf[4]);
  1377. encode_float(c.b, &buf[8]);
  1378. encode_float(c.a, &buf[12]);
  1379. }
  1380. r_len += 4 * 4; // Colors should always be in single-precision.
  1381. } break;
  1382. case Variant::RID: {
  1383. RID rid = p_variant;
  1384. if (buf) {
  1385. encode_uint64(rid.get_id(), buf);
  1386. }
  1387. r_len += 8;
  1388. } break;
  1389. case Variant::OBJECT: {
  1390. if (p_full_objects) {
  1391. Object *obj = p_variant;
  1392. if (!obj) {
  1393. if (buf) {
  1394. encode_uint32(0, buf);
  1395. }
  1396. r_len += 4;
  1397. } else {
  1398. ERR_FAIL_COND_V(!ClassDB::can_instantiate(obj->get_class()), ERR_INVALID_PARAMETER);
  1399. _encode_string(obj->get_class(), buf, r_len);
  1400. List<PropertyInfo> props;
  1401. obj->get_property_list(&props);
  1402. int pc = 0;
  1403. for (const PropertyInfo &E : props) {
  1404. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1405. continue;
  1406. }
  1407. pc++;
  1408. }
  1409. if (buf) {
  1410. encode_uint32(pc, buf);
  1411. buf += 4;
  1412. }
  1413. r_len += 4;
  1414. for (const PropertyInfo &E : props) {
  1415. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1416. continue;
  1417. }
  1418. _encode_string(E.name, buf, r_len);
  1419. Variant value;
  1420. if (E.name == CoreStringName(script)) {
  1421. Ref<Script> script = obj->get_script();
  1422. if (script.is_valid()) {
  1423. String path = script->get_path();
  1424. ERR_FAIL_COND_V_MSG(path.is_empty() || !path.begins_with("res://"), ERR_UNAVAILABLE, "Failed to encode a path to a custom script.");
  1425. value = path;
  1426. }
  1427. } else {
  1428. value = obj->get(E.name);
  1429. }
  1430. int len;
  1431. Error err = encode_variant(value, buf, len, p_full_objects, p_depth + 1);
  1432. ERR_FAIL_COND_V(err, err);
  1433. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1434. r_len += len;
  1435. if (buf) {
  1436. buf += len;
  1437. }
  1438. }
  1439. }
  1440. } else {
  1441. if (buf) {
  1442. Object *obj = p_variant.get_validated_object();
  1443. ObjectID id;
  1444. if (obj) {
  1445. id = obj->get_instance_id();
  1446. }
  1447. encode_uint64(id, buf);
  1448. }
  1449. r_len += 8;
  1450. }
  1451. } break;
  1452. case Variant::CALLABLE: {
  1453. } break;
  1454. case Variant::SIGNAL: {
  1455. Signal signal = p_variant;
  1456. _encode_string(signal.get_name(), buf, r_len);
  1457. if (buf) {
  1458. encode_uint64(signal.get_object_id(), buf);
  1459. }
  1460. r_len += 8;
  1461. } break;
  1462. case Variant::DICTIONARY: {
  1463. Dictionary d = p_variant;
  1464. if (buf) {
  1465. encode_uint32(uint32_t(d.size()), buf);
  1466. buf += 4;
  1467. }
  1468. r_len += 4;
  1469. List<Variant> keys;
  1470. d.get_key_list(&keys);
  1471. for (const Variant &E : keys) {
  1472. int len;
  1473. Error err = encode_variant(E, buf, len, p_full_objects, p_depth + 1);
  1474. ERR_FAIL_COND_V(err, err);
  1475. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1476. r_len += len;
  1477. if (buf) {
  1478. buf += len;
  1479. }
  1480. Variant *v = d.getptr(E);
  1481. ERR_FAIL_NULL_V(v, ERR_BUG);
  1482. err = encode_variant(*v, buf, len, p_full_objects, p_depth + 1);
  1483. ERR_FAIL_COND_V(err, err);
  1484. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1485. r_len += len;
  1486. if (buf) {
  1487. buf += len;
  1488. }
  1489. }
  1490. } break;
  1491. case Variant::ARRAY: {
  1492. Array array = p_variant;
  1493. if (array.is_typed()) {
  1494. Variant variant = array.get_typed_script();
  1495. Ref<Script> script = variant;
  1496. if (script.is_valid()) {
  1497. String path = script->get_path();
  1498. ERR_FAIL_COND_V_MSG(path.is_empty() || !path.begins_with("res://"), ERR_UNAVAILABLE, "Failed to encode a path to a custom script for an array type.");
  1499. _encode_string(path, buf, r_len);
  1500. } else if (array.get_typed_class_name() != StringName()) {
  1501. _encode_string(array.get_typed_class_name(), buf, r_len);
  1502. } else {
  1503. if (buf) {
  1504. encode_uint32(array.get_typed_builtin(), buf);
  1505. buf += 4;
  1506. }
  1507. r_len += 4;
  1508. }
  1509. }
  1510. if (buf) {
  1511. encode_uint32(uint32_t(array.size()), buf);
  1512. buf += 4;
  1513. }
  1514. r_len += 4;
  1515. for (const Variant &var : array) {
  1516. int len;
  1517. Error err = encode_variant(var, buf, len, p_full_objects, p_depth + 1);
  1518. ERR_FAIL_COND_V(err, err);
  1519. ERR_FAIL_COND_V(len % 4, ERR_BUG);
  1520. if (buf) {
  1521. buf += len;
  1522. }
  1523. r_len += len;
  1524. }
  1525. } break;
  1526. // arrays
  1527. case Variant::PACKED_BYTE_ARRAY: {
  1528. Vector<uint8_t> data = p_variant;
  1529. int datalen = data.size();
  1530. int datasize = sizeof(uint8_t);
  1531. if (buf) {
  1532. encode_uint32(datalen, buf);
  1533. buf += 4;
  1534. const uint8_t *r = data.ptr();
  1535. if (r) {
  1536. memcpy(buf, &r[0], datalen * datasize);
  1537. buf += datalen * datasize;
  1538. }
  1539. }
  1540. r_len += 4 + datalen * datasize;
  1541. while (r_len % 4) {
  1542. r_len++;
  1543. if (buf) {
  1544. *(buf++) = 0;
  1545. }
  1546. }
  1547. } break;
  1548. case Variant::PACKED_INT32_ARRAY: {
  1549. Vector<int32_t> data = p_variant;
  1550. int datalen = data.size();
  1551. int datasize = sizeof(int32_t);
  1552. if (buf) {
  1553. encode_uint32(datalen, buf);
  1554. buf += 4;
  1555. const int32_t *r = data.ptr();
  1556. for (int32_t i = 0; i < datalen; i++) {
  1557. encode_uint32(r[i], &buf[i * datasize]);
  1558. }
  1559. }
  1560. r_len += 4 + datalen * datasize;
  1561. } break;
  1562. case Variant::PACKED_INT64_ARRAY: {
  1563. Vector<int64_t> data = p_variant;
  1564. int datalen = data.size();
  1565. int datasize = sizeof(int64_t);
  1566. if (buf) {
  1567. encode_uint32(datalen, buf);
  1568. buf += 4;
  1569. const int64_t *r = data.ptr();
  1570. for (int64_t i = 0; i < datalen; i++) {
  1571. encode_uint64(r[i], &buf[i * datasize]);
  1572. }
  1573. }
  1574. r_len += 4 + datalen * datasize;
  1575. } break;
  1576. case Variant::PACKED_FLOAT32_ARRAY: {
  1577. Vector<float> data = p_variant;
  1578. int datalen = data.size();
  1579. int datasize = sizeof(float);
  1580. if (buf) {
  1581. encode_uint32(datalen, buf);
  1582. buf += 4;
  1583. const float *r = data.ptr();
  1584. for (int i = 0; i < datalen; i++) {
  1585. encode_float(r[i], &buf[i * datasize]);
  1586. }
  1587. }
  1588. r_len += 4 + datalen * datasize;
  1589. } break;
  1590. case Variant::PACKED_FLOAT64_ARRAY: {
  1591. Vector<double> data = p_variant;
  1592. int datalen = data.size();
  1593. int datasize = sizeof(double);
  1594. if (buf) {
  1595. encode_uint32(datalen, buf);
  1596. buf += 4;
  1597. const double *r = data.ptr();
  1598. for (int i = 0; i < datalen; i++) {
  1599. encode_double(r[i], &buf[i * datasize]);
  1600. }
  1601. }
  1602. r_len += 4 + datalen * datasize;
  1603. } break;
  1604. case Variant::PACKED_STRING_ARRAY: {
  1605. Vector<String> data = p_variant;
  1606. int len = data.size();
  1607. if (buf) {
  1608. encode_uint32(len, buf);
  1609. buf += 4;
  1610. }
  1611. r_len += 4;
  1612. for (int i = 0; i < len; i++) {
  1613. CharString utf8 = data.get(i).utf8();
  1614. if (buf) {
  1615. encode_uint32(utf8.length() + 1, buf);
  1616. buf += 4;
  1617. memcpy(buf, utf8.get_data(), utf8.length() + 1);
  1618. buf += utf8.length() + 1;
  1619. }
  1620. r_len += 4 + utf8.length() + 1;
  1621. while (r_len % 4) {
  1622. r_len++; //pad
  1623. if (buf) {
  1624. *(buf++) = 0;
  1625. }
  1626. }
  1627. }
  1628. } break;
  1629. case Variant::PACKED_VECTOR2_ARRAY: {
  1630. Vector<Vector2> data = p_variant;
  1631. int len = data.size();
  1632. if (buf) {
  1633. encode_uint32(len, buf);
  1634. buf += 4;
  1635. }
  1636. r_len += 4;
  1637. if (buf) {
  1638. for (int i = 0; i < len; i++) {
  1639. Vector2 v = data.get(i);
  1640. encode_real(v.x, &buf[0]);
  1641. encode_real(v.y, &buf[sizeof(real_t)]);
  1642. buf += sizeof(real_t) * 2;
  1643. }
  1644. }
  1645. r_len += sizeof(real_t) * 2 * len;
  1646. } break;
  1647. case Variant::PACKED_VECTOR3_ARRAY: {
  1648. Vector<Vector3> data = p_variant;
  1649. int len = data.size();
  1650. if (buf) {
  1651. encode_uint32(len, buf);
  1652. buf += 4;
  1653. }
  1654. r_len += 4;
  1655. if (buf) {
  1656. for (int i = 0; i < len; i++) {
  1657. Vector3 v = data.get(i);
  1658. encode_real(v.x, &buf[0]);
  1659. encode_real(v.y, &buf[sizeof(real_t)]);
  1660. encode_real(v.z, &buf[sizeof(real_t) * 2]);
  1661. buf += sizeof(real_t) * 3;
  1662. }
  1663. }
  1664. r_len += sizeof(real_t) * 3 * len;
  1665. } break;
  1666. case Variant::PACKED_COLOR_ARRAY: {
  1667. Vector<Color> data = p_variant;
  1668. int len = data.size();
  1669. if (buf) {
  1670. encode_uint32(len, buf);
  1671. buf += 4;
  1672. }
  1673. r_len += 4;
  1674. if (buf) {
  1675. for (int i = 0; i < len; i++) {
  1676. Color c = data.get(i);
  1677. encode_float(c.r, &buf[0]);
  1678. encode_float(c.g, &buf[4]);
  1679. encode_float(c.b, &buf[8]);
  1680. encode_float(c.a, &buf[12]);
  1681. buf += 4 * 4; // Colors should always be in single-precision.
  1682. }
  1683. }
  1684. r_len += 4 * 4 * len;
  1685. } break;
  1686. case Variant::PACKED_VECTOR4_ARRAY: {
  1687. Vector<Vector4> data = p_variant;
  1688. int len = data.size();
  1689. if (buf) {
  1690. encode_uint32(len, buf);
  1691. buf += 4;
  1692. }
  1693. r_len += 4;
  1694. if (buf) {
  1695. for (int i = 0; i < len; i++) {
  1696. Vector4 v = data.get(i);
  1697. encode_real(v.x, &buf[0]);
  1698. encode_real(v.y, &buf[sizeof(real_t)]);
  1699. encode_real(v.z, &buf[sizeof(real_t) * 2]);
  1700. encode_real(v.w, &buf[sizeof(real_t) * 3]);
  1701. buf += sizeof(real_t) * 4;
  1702. }
  1703. }
  1704. r_len += sizeof(real_t) * 4 * len;
  1705. } break;
  1706. default: {
  1707. ERR_FAIL_V(ERR_BUG);
  1708. }
  1709. }
  1710. return OK;
  1711. }
  1712. Vector<float> vector3_to_float32_array(const Vector3 *vecs, size_t count) {
  1713. // We always allocate a new array, and we don't memcpy.
  1714. // We also don't consider returning a pointer to the passed vectors when sizeof(real_t) == 4.
  1715. // One reason is that we could decide to put a 4th component in Vector3 for SIMD/mobile performance,
  1716. // which would cause trouble with these optimizations.
  1717. Vector<float> floats;
  1718. if (count == 0) {
  1719. return floats;
  1720. }
  1721. floats.resize(count * 3);
  1722. float *floats_w = floats.ptrw();
  1723. for (size_t i = 0; i < count; ++i) {
  1724. const Vector3 v = vecs[i];
  1725. floats_w[0] = v.x;
  1726. floats_w[1] = v.y;
  1727. floats_w[2] = v.z;
  1728. floats_w += 3;
  1729. }
  1730. return floats;
  1731. }