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