2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* packed_data_container.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "packed_data_container.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/core_string_names.h"
|
|
|
|
#include "core/io/marshalls.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainer::getvar(const Variant &p_key, bool *r_valid) const {
|
|
|
|
bool err = false;
|
|
|
|
Variant ret = _key_at_ofs(0, p_key, err);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r_valid) {
|
2017-03-05 16:44:50 +01:00
|
|
|
*r_valid = !err;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-11-07 02:29:22 +01:00
|
|
|
if (err) {
|
|
|
|
return Object::getvar(p_key, r_valid);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PackedDataContainer::size() const {
|
|
|
|
return _size(0);
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainer::_iter_init_ofs(const Array &p_iter, uint32_t p_offset) {
|
|
|
|
Array ref = p_iter;
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t size = _size(p_offset);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (size == 0 || ref.size() != 1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
ref[0] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainer::_iter_next_ofs(const Array &p_iter, uint32_t p_offset) {
|
|
|
|
Array ref = p_iter;
|
2017-08-31 23:30:35 +02:00
|
|
|
int size = _size(p_offset);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (ref.size() != 1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
int pos = ref[0];
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pos < 0 || pos >= size) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
pos += 1;
|
|
|
|
ref[0] = pos;
|
|
|
|
return pos != size;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainer::_iter_get_ofs(const Variant &p_iter, uint32_t p_offset) {
|
2017-08-31 23:30:35 +02:00
|
|
|
int size = _size(p_offset);
|
2017-03-05 16:44:50 +01:00
|
|
|
int pos = p_iter;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pos < 0 || pos >= size) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return Variant();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
const uint8_t *rd = data.ptr();
|
2017-03-05 16:44:50 +01:00
|
|
|
const uint8_t *r = &rd[p_offset];
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t type = decode_uint32(r);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool err = false;
|
|
|
|
if (type == TYPE_ARRAY) {
|
2020-02-17 22:06:54 +01:00
|
|
|
uint32_t vpos = decode_uint32(rd + p_offset + 8 + pos * 4);
|
|
|
|
return _get_at_ofs(vpos, rd, err);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (type == TYPE_DICT) {
|
2020-02-17 22:06:54 +01:00
|
|
|
uint32_t vpos = decode_uint32(rd + p_offset + 8 + pos * 12 + 4);
|
|
|
|
return _get_at_ofs(vpos, rd, err);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
ERR_FAIL_V(Variant());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainer::_get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, bool &err) const {
|
2021-11-21 13:14:59 +01:00
|
|
|
ERR_FAIL_COND_V(p_ofs + 4 > (uint32_t)data.size(), Variant());
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t type = decode_uint32(p_buf + p_ofs);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (type == TYPE_ARRAY || type == TYPE_DICT) {
|
|
|
|
Ref<PackedDataContainerRef> pdcr = memnew(PackedDataContainerRef);
|
|
|
|
Ref<PackedDataContainer> pdc = Ref<PackedDataContainer>((PackedDataContainer *)this);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pdcr->from = pdc;
|
|
|
|
pdcr->offset = p_ofs;
|
2014-02-10 02:10:30 +01:00
|
|
|
return pdcr;
|
|
|
|
} else {
|
|
|
|
Variant v;
|
2020-04-02 01:20:12 +02:00
|
|
|
Error rerr = decode_variant(v, p_buf + p_ofs, datalen - p_ofs, nullptr, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (rerr != OK) {
|
|
|
|
err = true;
|
2019-09-25 10:28:50 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 15:41:03 +02:00
|
|
|
uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const {
|
2021-11-21 13:14:59 +01:00
|
|
|
ERR_FAIL_COND_V(p_ofs + 4 > (uint32_t)data.size(), 0);
|
2020-02-17 22:06:54 +01:00
|
|
|
const uint8_t *rd = data.ptr();
|
2021-05-05 18:17:49 +02:00
|
|
|
ERR_FAIL_COND_V(!rd, 0);
|
2017-03-05 16:44:50 +01:00
|
|
|
const uint8_t *r = &rd[p_ofs];
|
2014-06-11 15:41:03 +02:00
|
|
|
uint32_t type = decode_uint32(r);
|
|
|
|
|
|
|
|
return type;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-06-11 15:41:03 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int PackedDataContainer::_size(uint32_t p_ofs) const {
|
2021-11-21 13:14:59 +01:00
|
|
|
ERR_FAIL_COND_V(p_ofs + 4 > (uint32_t)data.size(), 0);
|
2020-02-17 22:06:54 +01:00
|
|
|
const uint8_t *rd = data.ptr();
|
|
|
|
ERR_FAIL_COND_V(!rd, 0);
|
2017-03-05 16:44:50 +01:00
|
|
|
const uint8_t *r = &rd[p_ofs];
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t type = decode_uint32(r);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (type == TYPE_ARRAY) {
|
|
|
|
uint32_t len = decode_uint32(r + 4);
|
2014-02-10 02:10:30 +01:00
|
|
|
return len;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (type == TYPE_DICT) {
|
|
|
|
uint32_t len = decode_uint32(r + 4);
|
2014-02-10 02:10:30 +01:00
|
|
|
return len;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return -1;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const {
|
2021-11-21 13:14:59 +01:00
|
|
|
ERR_FAIL_COND_V(p_ofs + 4 > (uint32_t)data.size(), Variant());
|
2020-02-17 22:06:54 +01:00
|
|
|
const uint8_t *rd = data.ptr();
|
2021-05-05 18:17:49 +02:00
|
|
|
if (!rd) {
|
|
|
|
err = true;
|
|
|
|
ERR_FAIL_COND_V(!rd, Variant());
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
const uint8_t *r = &rd[p_ofs];
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t type = decode_uint32(r);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (type == TYPE_ARRAY) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_key.is_num()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = p_key;
|
2017-08-31 23:30:35 +02:00
|
|
|
int len = decode_uint32(r + 4);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (idx < 0 || idx >= len) {
|
|
|
|
err = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
return Variant();
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t ofs = decode_uint32(r + 8 + 4 * idx);
|
2020-02-17 22:06:54 +01:00
|
|
|
return _get_at_ofs(ofs, rd, err);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
err = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
return Variant();
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (type == TYPE_DICT) {
|
|
|
|
uint32_t hash = p_key.hash();
|
|
|
|
uint32_t len = decode_uint32(r + 4);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool found = false;
|
2017-08-31 23:30:35 +02:00
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t khash = decode_uint32(r + 8 + i * 12 + 0);
|
|
|
|
if (khash == hash) {
|
2020-02-17 22:06:54 +01:00
|
|
|
Variant key = _get_at_ofs(decode_uint32(r + 8 + i * 12 + 4), rd, err);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (err) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return Variant();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (key == p_key) {
|
2014-02-10 02:10:30 +01:00
|
|
|
//key matches, return value
|
2020-02-17 22:06:54 +01:00
|
|
|
return _get_at_ofs(decode_uint32(r + 8 + i * 12 + 8), rd, err);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
found = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (found) {
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
err = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
return Variant();
|
|
|
|
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
err = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache) {
|
|
|
|
switch (p_data.get_type()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case Variant::STRING: {
|
|
|
|
String s = p_data;
|
|
|
|
if (string_cache.has(s)) {
|
|
|
|
return string_cache[s];
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
string_cache[s] = tmpdata.size();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-22 20:47:50 +01:00
|
|
|
[[fallthrough]];
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
case Variant::NIL:
|
|
|
|
case Variant::BOOL:
|
|
|
|
case Variant::INT:
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
case Variant::FLOAT:
|
2014-02-10 02:10:30 +01:00
|
|
|
case Variant::VECTOR2:
|
|
|
|
case Variant::RECT2:
|
|
|
|
case Variant::VECTOR3:
|
2017-01-11 04:52:51 +01:00
|
|
|
case Variant::TRANSFORM2D:
|
2014-02-10 02:10:30 +01:00
|
|
|
case Variant::PLANE:
|
2021-01-20 08:02:02 +01:00
|
|
|
case Variant::QUATERNION:
|
2017-11-17 03:09:00 +01:00
|
|
|
case Variant::AABB:
|
2017-01-11 04:52:51 +01:00
|
|
|
case Variant::BASIS:
|
2021-04-28 09:36:08 +02:00
|
|
|
case Variant::TRANSFORM3D:
|
2020-02-17 22:06:54 +01:00
|
|
|
case Variant::PACKED_BYTE_ARRAY:
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
case Variant::PACKED_INT32_ARRAY:
|
|
|
|
case Variant::PACKED_INT64_ARRAY:
|
|
|
|
case Variant::PACKED_FLOAT32_ARRAY:
|
|
|
|
case Variant::PACKED_FLOAT64_ARRAY:
|
2020-02-17 22:06:54 +01:00
|
|
|
case Variant::PACKED_STRING_ARRAY:
|
|
|
|
case Variant::PACKED_VECTOR2_ARRAY:
|
|
|
|
case Variant::PACKED_VECTOR3_ARRAY:
|
|
|
|
case Variant::PACKED_COLOR_ARRAY:
|
2020-02-20 22:58:05 +01:00
|
|
|
case Variant::STRING_NAME:
|
2014-02-10 02:10:30 +01:00
|
|
|
case Variant::NODE_PATH: {
|
|
|
|
uint32_t pos = tmpdata.size();
|
|
|
|
int len;
|
2020-04-02 01:20:12 +02:00
|
|
|
encode_variant(p_data, nullptr, len, false);
|
2017-03-05 16:44:50 +01:00
|
|
|
tmpdata.resize(tmpdata.size() + len);
|
2019-03-26 16:52:42 +01:00
|
|
|
encode_variant(p_data, &tmpdata.write[pos], len, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
return pos;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
// misc types
|
2020-11-09 14:53:05 +01:00
|
|
|
case Variant::RID:
|
2014-02-10 02:10:30 +01:00
|
|
|
case Variant::OBJECT: {
|
2017-03-05 16:44:50 +01:00
|
|
|
return _pack(Variant(), tmpdata, string_cache);
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case Variant::DICTIONARY: {
|
2017-03-05 16:44:50 +01:00
|
|
|
Dictionary d = p_data;
|
2014-02-10 02:10:30 +01:00
|
|
|
//size is known, use sort
|
|
|
|
uint32_t pos = tmpdata.size();
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = d.size();
|
|
|
|
tmpdata.resize(tmpdata.size() + len * 12 + 8);
|
2018-07-25 03:11:03 +02:00
|
|
|
encode_uint32(TYPE_DICT, &tmpdata.write[pos + 0]);
|
|
|
|
encode_uint32(len, &tmpdata.write[pos + 4]);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
List<Variant> keys;
|
|
|
|
d.get_key_list(&keys);
|
|
|
|
List<DictKey> sortk;
|
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const Variant &key : keys) {
|
2014-02-10 02:10:30 +01:00
|
|
|
DictKey dk;
|
2021-07-16 05:45:57 +02:00
|
|
|
dk.hash = key.hash();
|
|
|
|
dk.key = key;
|
2014-02-10 02:10:30 +01:00
|
|
|
sortk.push_back(dk);
|
|
|
|
}
|
|
|
|
|
|
|
|
sortk.sort();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = 0;
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const DictKey &E : sortk) {
|
2021-07-16 05:45:57 +02:00
|
|
|
encode_uint32(E.hash, &tmpdata.write[pos + 8 + idx * 12 + 0]);
|
|
|
|
uint32_t ofs = _pack(E.key, tmpdata, string_cache);
|
2018-07-25 03:11:03 +02:00
|
|
|
encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 4]);
|
2021-07-16 05:45:57 +02:00
|
|
|
ofs = _pack(d[E.key], tmpdata, string_cache);
|
2018-07-25 03:11:03 +02:00
|
|
|
encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 8]);
|
2014-02-10 02:10:30 +01:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case Variant::ARRAY: {
|
2017-03-05 16:44:50 +01:00
|
|
|
Array a = p_data;
|
2014-02-10 02:10:30 +01:00
|
|
|
//size is known, use sort
|
|
|
|
uint32_t pos = tmpdata.size();
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = a.size();
|
|
|
|
tmpdata.resize(tmpdata.size() + len * 4 + 8);
|
2018-07-25 03:11:03 +02:00
|
|
|
encode_uint32(TYPE_ARRAY, &tmpdata.write[pos + 0]);
|
|
|
|
encode_uint32(len, &tmpdata.write[pos + 4]);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
uint32_t ofs = _pack(a[i], tmpdata, string_cache);
|
2018-07-25 03:11:03 +02:00
|
|
|
encode_uint32(ofs, &tmpdata.write[pos + 8 + i * 4]);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2019-04-09 17:08:36 +02:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error PackedDataContainer::pack(const Variant &p_data) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<uint8_t> tmpdata;
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<String, uint32_t> string_cache;
|
|
|
|
_pack(p_data, tmpdata, string_cache);
|
|
|
|
datalen = tmpdata.size();
|
2014-02-10 02:10:30 +01:00
|
|
|
data.resize(tmpdata.size());
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *w = data.ptrw();
|
2021-04-27 16:19:21 +02:00
|
|
|
memcpy(w, tmpdata.ptr(), tmpdata.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
void PackedDataContainer::_set_data(const Vector<uint8_t> &p_data) {
|
2017-03-05 16:44:50 +01:00
|
|
|
data = p_data;
|
|
|
|
datalen = data.size();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> PackedDataContainer::_get_data() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainer::_iter_init(const Array &p_iter) {
|
|
|
|
return _iter_init_ofs(p_iter, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainer::_iter_next(const Array &p_iter) {
|
|
|
|
return _iter_next_ofs(p_iter, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainer::_iter_get(const Variant &p_iter) {
|
|
|
|
return _iter_get_ofs(p_iter, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PackedDataContainer::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_data"), &PackedDataContainer::_set_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_data"), &PackedDataContainer::_get_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainer::_iter_init);
|
|
|
|
ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainer::_iter_get);
|
|
|
|
ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainer::_iter_next);
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("pack", "value"), &PackedDataContainer::pack);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("size"), &PackedDataContainer::size);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "__data__"), "_set_data", "_get_data");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainerRef::_iter_init(const Array &p_iter) {
|
|
|
|
return from->_iter_init_ofs(p_iter, offset);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainerRef::_iter_next(const Array &p_iter) {
|
|
|
|
return from->_iter_next_ofs(p_iter, offset);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainerRef::_iter_get(const Variant &p_iter) {
|
|
|
|
return from->_iter_get_ofs(p_iter, offset);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-06-11 15:41:03 +02:00
|
|
|
bool PackedDataContainerRef::_is_dictionary() const {
|
|
|
|
return from->_type_at_ofs(offset) == PackedDataContainer::TYPE_DICT;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void PackedDataContainerRef::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("size"), &PackedDataContainerRef::size);
|
|
|
|
ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainerRef::_iter_init);
|
|
|
|
ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainerRef::_iter_get);
|
|
|
|
ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainerRef::_iter_next);
|
|
|
|
ClassDB::bind_method(D_METHOD("_is_dictionary"), &PackedDataContainerRef::_is_dictionary);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant PackedDataContainerRef::getvar(const Variant &p_key, bool *r_valid) const {
|
|
|
|
bool err = false;
|
|
|
|
Variant ret = from->_key_at_ofs(offset, p_key, err);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r_valid) {
|
2017-03-05 16:44:50 +01:00
|
|
|
*r_valid = !err;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PackedDataContainerRef::size() const {
|
|
|
|
return from->_size(offset);
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|