summaryrefslogtreecommitdiff
path: root/core/packed_data_container.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/packed_data_container.cpp')
-rw-r--r--core/packed_data_container.cpp301
1 files changed, 138 insertions, 163 deletions
diff --git a/core/packed_data_container.cpp b/core/packed_data_container.cpp
index 803a217fca..b2922a6173 100644
--- a/core/packed_data_container.cpp
+++ b/core/packed_data_container.cpp
@@ -28,16 +28,15 @@
/*************************************************************************/
#include "packed_data_container.h"
-#include "io/marshalls.h"
#include "core_string_names.h"
+#include "io/marshalls.h"
+Variant PackedDataContainer::getvar(const Variant &p_key, bool *r_valid) const {
-Variant PackedDataContainer::getvar(const Variant& p_key, bool *r_valid) const {
-
- bool err=false;
- Variant ret = _key_at_ofs(0,p_key,err);
+ bool err = false;
+ Variant ret = _key_at_ofs(0, p_key, err);
if (r_valid)
- *r_valid=!err;
+ *r_valid = !err;
return ret;
}
@@ -46,93 +45,88 @@ int PackedDataContainer::size() const {
return _size(0);
};
-Variant PackedDataContainer::_iter_init_ofs(const Array& p_iter,uint32_t p_offset) {
+Variant PackedDataContainer::_iter_init_ofs(const Array &p_iter, uint32_t p_offset) {
- Array ref=p_iter;
+ Array ref = p_iter;
uint32_t size = _size(p_offset);
- if (size==0 || ref.size()!=1)
+ if (size == 0 || ref.size() != 1)
return false;
else {
- ref[0]=0;
+ ref[0] = 0;
return true;
}
-
-
}
-Variant PackedDataContainer::_iter_next_ofs(const Array& p_iter,uint32_t p_offset){
+Variant PackedDataContainer::_iter_next_ofs(const Array &p_iter, uint32_t p_offset) {
- Array ref=p_iter;
+ Array ref = p_iter;
uint32_t size = _size(p_offset);
- if (ref.size()!=1)
+ if (ref.size() != 1)
return false;
int pos = ref[0];
- if (pos<0 || pos>=size)
+ if (pos < 0 || pos >= size)
return false;
- pos+=1;
- ref[0]=pos;
- return pos!=size;
-
+ pos += 1;
+ ref[0] = pos;
+ return pos != size;
}
-Variant PackedDataContainer::_iter_get_ofs(const Variant& p_iter,uint32_t p_offset){
+Variant PackedDataContainer::_iter_get_ofs(const Variant &p_iter, uint32_t p_offset) {
uint32_t size = _size(p_offset);
- int pos=p_iter;
- if (pos<0 || pos>=size)
+ int pos = p_iter;
+ if (pos < 0 || pos >= size)
return Variant();
- PoolVector<uint8_t>::Read rd=data.read();
- const uint8_t *r=&rd[p_offset];
+ PoolVector<uint8_t>::Read rd = data.read();
+ const uint8_t *r = &rd[p_offset];
uint32_t type = decode_uint32(r);
- bool err=false;
- if (type==TYPE_ARRAY) {
+ bool err = false;
+ if (type == TYPE_ARRAY) {
- uint32_t vpos = decode_uint32(rd.ptr() + p_offset+8+pos*4);
- return _get_at_ofs(vpos,rd.ptr(),err);
+ uint32_t vpos = decode_uint32(rd.ptr() + p_offset + 8 + pos * 4);
+ return _get_at_ofs(vpos, rd.ptr(), err);
- } else if (type==TYPE_DICT) {
+ } else if (type == TYPE_DICT) {
- uint32_t vpos = decode_uint32(rd.ptr() + p_offset+8+pos*12+4);
- return _get_at_ofs(vpos,rd.ptr(),err);
+ uint32_t vpos = decode_uint32(rd.ptr() + p_offset + 8 + pos * 12 + 4);
+ return _get_at_ofs(vpos, rd.ptr(), err);
} else {
ERR_FAIL_V(Variant());
}
}
-
-Variant PackedDataContainer::_get_at_ofs(uint32_t p_ofs,const uint8_t *p_buf,bool &err) const {
+Variant PackedDataContainer::_get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, bool &err) const {
uint32_t type = decode_uint32(p_buf + p_ofs);
- if (type==TYPE_ARRAY || type==TYPE_DICT) {
+ if (type == TYPE_ARRAY || type == TYPE_DICT) {
- Ref<PackedDataContainerRef> pdcr = memnew( PackedDataContainerRef );
- Ref<PackedDataContainer> pdc = Ref<PackedDataContainer>((PackedDataContainer*)this);
+ Ref<PackedDataContainerRef> pdcr = memnew(PackedDataContainerRef);
+ Ref<PackedDataContainer> pdc = Ref<PackedDataContainer>((PackedDataContainer *)this);
- pdcr->from=pdc;
- pdcr->offset=p_ofs;
+ pdcr->from = pdc;
+ pdcr->offset = p_ofs;
return pdcr;
} else {
Variant v;
- Error rerr = decode_variant(v,p_buf + p_ofs,datalen-p_ofs,NULL);
+ Error rerr = decode_variant(v, p_buf + p_ofs, datalen - p_ofs, NULL);
- if (rerr!=OK) {
+ if (rerr != OK) {
- err=true;
- ERR_FAIL_COND_V(err!=OK,Variant());
+ err = true;
+ ERR_FAIL_COND_V(err != OK, Variant());
}
return v;
}
-
}
uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const {
- PoolVector<uint8_t>::Read rd=data.read();
- const uint8_t *r=&rd[p_ofs];
+ PoolVector<uint8_t>::Read rd = data.read();
+ const uint8_t *r = &rd[p_ofs];
uint32_t type = decode_uint32(r);
return type;
@@ -140,87 +134,84 @@ uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const {
int PackedDataContainer::_size(uint32_t p_ofs) const {
- PoolVector<uint8_t>::Read rd=data.read();
- const uint8_t *r=&rd[p_ofs];
+ PoolVector<uint8_t>::Read rd = data.read();
+ const uint8_t *r = &rd[p_ofs];
uint32_t type = decode_uint32(r);
- if (type==TYPE_ARRAY) {
+ if (type == TYPE_ARRAY) {
- uint32_t len = decode_uint32(r+4);
+ uint32_t len = decode_uint32(r + 4);
return len;
- } else if (type==TYPE_DICT) {
+ } else if (type == TYPE_DICT) {
- uint32_t len = decode_uint32(r+4);
+ uint32_t len = decode_uint32(r + 4);
return len;
};
return -1;
};
-Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs,const Variant& p_key,bool &err) const {
+Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const {
- PoolVector<uint8_t>::Read rd=data.read();
- const uint8_t *r=&rd[p_ofs];
+ PoolVector<uint8_t>::Read rd = data.read();
+ const uint8_t *r = &rd[p_ofs];
uint32_t type = decode_uint32(r);
-
- if (type==TYPE_ARRAY) {
+ if (type == TYPE_ARRAY) {
if (p_key.is_num()) {
- int idx=p_key;
- uint32_t len = decode_uint32(r+4);
- if (idx<0 || idx>=len) {
- err=true;
+ int idx = p_key;
+ uint32_t len = decode_uint32(r + 4);
+ if (idx < 0 || idx >= len) {
+ err = true;
return Variant();
}
- uint32_t ofs = decode_uint32(r+8+4*idx);
- return _get_at_ofs(ofs,rd.ptr(),err);
+ uint32_t ofs = decode_uint32(r + 8 + 4 * idx);
+ return _get_at_ofs(ofs, rd.ptr(), err);
} else {
- err=true;
+ err = true;
return Variant();
}
- } else if (type==TYPE_DICT) {
+ } else if (type == TYPE_DICT) {
- uint32_t hash=p_key.hash();
- uint32_t len = decode_uint32(r+4);
+ uint32_t hash = p_key.hash();
+ uint32_t len = decode_uint32(r + 4);
- bool found=false;
- for(int i=0;i<len;i++) {
- uint32_t khash=decode_uint32(r+8+i*12+0);
- if (khash==hash) {
- Variant key = _get_at_ofs(decode_uint32(r+8+i*12+4),rd.ptr(),err);
+ bool found = false;
+ for (int i = 0; i < len; i++) {
+ uint32_t khash = decode_uint32(r + 8 + i * 12 + 0);
+ if (khash == hash) {
+ Variant key = _get_at_ofs(decode_uint32(r + 8 + i * 12 + 4), rd.ptr(), err);
if (err)
return Variant();
- if (key==p_key) {
+ if (key == p_key) {
//key matches, return value
- return _get_at_ofs(decode_uint32(r+8+i*12+8),rd.ptr(),err);
+ return _get_at_ofs(decode_uint32(r + 8 + i * 12 + 8), rd.ptr(), err);
}
- found=true;
+ found = true;
} else {
if (found)
break;
}
}
- err=true;
+ err = true;
return Variant();
} else {
- err=true;
+ err = true;
return Variant();
}
-
-
}
-uint32_t PackedDataContainer::_pack(const Variant& p_data, Vector<uint8_t>& tmpdata, Map<String,uint32_t>& string_cache) {
+uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache) {
- switch(p_data.get_type()) {
+ switch (p_data.get_type()) {
case Variant::STRING: {
@@ -229,7 +220,7 @@ uint32_t PackedDataContainer::_pack(const Variant& p_data, Vector<uint8_t>& tmpd
return string_cache[s];
}
- string_cache[s]=tmpdata.size();
+ string_cache[s] = tmpdata.size();
}; //fallthrough
case Variant::NIL:
@@ -258,9 +249,9 @@ uint32_t PackedDataContainer::_pack(const Variant& p_data, Vector<uint8_t>& tmpd
uint32_t pos = tmpdata.size();
int len;
- encode_variant(p_data,NULL,len);
- tmpdata.resize(tmpdata.size()+len);
- encode_variant(p_data,&tmpdata[pos],len);
+ encode_variant(p_data, NULL, len);
+ tmpdata.resize(tmpdata.size() + len);
+ encode_variant(p_data, &tmpdata[pos], len);
return pos;
} break;
@@ -268,67 +259,64 @@ uint32_t PackedDataContainer::_pack(const Variant& p_data, Vector<uint8_t>& tmpd
case Variant::_RID:
case Variant::OBJECT: {
- return _pack(Variant(),tmpdata,string_cache);
+ return _pack(Variant(), tmpdata, string_cache);
} break;
case Variant::DICTIONARY: {
- Dictionary d=p_data;
+ Dictionary d = p_data;
//size is known, use sort
uint32_t pos = tmpdata.size();
- int len=d.size();
- tmpdata.resize(tmpdata.size()+len*12+8);
- encode_uint32(TYPE_DICT,&tmpdata[pos+0]);
- encode_uint32(len,&tmpdata[pos+4]);
+ int len = d.size();
+ tmpdata.resize(tmpdata.size() + len * 12 + 8);
+ encode_uint32(TYPE_DICT, &tmpdata[pos + 0]);
+ encode_uint32(len, &tmpdata[pos + 4]);
List<Variant> keys;
d.get_key_list(&keys);
List<DictKey> sortk;
- for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
+ for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
DictKey dk;
- dk.hash=E->get().hash();
- dk.key=E->get();
+ dk.hash = E->get().hash();
+ dk.key = E->get();
sortk.push_back(dk);
}
sortk.sort();
- int idx=0;
- for(List<DictKey>::Element *E=sortk.front();E;E=E->next()) {
+ int idx = 0;
+ for (List<DictKey>::Element *E = sortk.front(); E; E = E->next()) {
- encode_uint32(E->get().hash,&tmpdata[pos+8+idx*12+0]);
- uint32_t ofs = _pack(E->get().key,tmpdata,string_cache);
- encode_uint32(ofs,&tmpdata[pos+8+idx*12+4]);
- ofs = _pack(d[E->get().key],tmpdata,string_cache);
- encode_uint32(ofs,&tmpdata[pos+8+idx*12+8]);
+ encode_uint32(E->get().hash, &tmpdata[pos + 8 + idx * 12 + 0]);
+ uint32_t ofs = _pack(E->get().key, tmpdata, string_cache);
+ encode_uint32(ofs, &tmpdata[pos + 8 + idx * 12 + 4]);
+ ofs = _pack(d[E->get().key], tmpdata, string_cache);
+ encode_uint32(ofs, &tmpdata[pos + 8 + idx * 12 + 8]);
idx++;
}
return pos;
-
-
} break;
case Variant::ARRAY: {
- Array a=p_data;
+ Array a = p_data;
//size is known, use sort
uint32_t pos = tmpdata.size();
- int len=a.size();
- tmpdata.resize(tmpdata.size()+len*4+8);
- encode_uint32(TYPE_ARRAY,&tmpdata[pos+0]);
- encode_uint32(len,&tmpdata[pos+4]);
+ int len = a.size();
+ tmpdata.resize(tmpdata.size() + len * 4 + 8);
+ encode_uint32(TYPE_ARRAY, &tmpdata[pos + 0]);
+ encode_uint32(len, &tmpdata[pos + 4]);
- for(int i=0;i<len;i++) {
+ for (int i = 0; i < len; i++) {
- uint32_t ofs = _pack(a[i],tmpdata,string_cache);
- encode_uint32(ofs,&tmpdata[pos+8+i*4]);
+ uint32_t ofs = _pack(a[i], tmpdata, string_cache);
+ encode_uint32(ofs, &tmpdata[pos + 8 + i * 4]);
}
return pos;
-
} break;
default: {}
@@ -337,86 +325,75 @@ uint32_t PackedDataContainer::_pack(const Variant& p_data, Vector<uint8_t>& tmpd
return OK;
}
-Error PackedDataContainer::pack(const Variant& p_data) {
+Error PackedDataContainer::pack(const Variant &p_data) {
Vector<uint8_t> tmpdata;
- Map<String,uint32_t> string_cache;
- _pack(p_data,tmpdata,string_cache);
- datalen=tmpdata.size();
+ Map<String, uint32_t> string_cache;
+ _pack(p_data, tmpdata, string_cache);
+ datalen = tmpdata.size();
data.resize(tmpdata.size());
PoolVector<uint8_t>::Write w = data.write();
- copymem(w.ptr(),tmpdata.ptr(),tmpdata.size());
+ copymem(w.ptr(), tmpdata.ptr(), tmpdata.size());
return OK;
}
+void PackedDataContainer::_set_data(const PoolVector<uint8_t> &p_data) {
-void PackedDataContainer::_set_data(const PoolVector<uint8_t>& p_data) {
-
- data=p_data;
- datalen=data.size();
-
+ data = p_data;
+ datalen = data.size();
}
PoolVector<uint8_t> PackedDataContainer::_get_data() const {
return data;
}
+Variant PackedDataContainer::_iter_init(const Array &p_iter) {
-Variant PackedDataContainer::_iter_init(const Array& p_iter) {
-
-
- return _iter_init_ofs(p_iter,0);
+ return _iter_init_ofs(p_iter, 0);
}
-Variant PackedDataContainer::_iter_next(const Array& p_iter){
-
- return _iter_next_ofs(p_iter,0);
+Variant PackedDataContainer::_iter_next(const Array &p_iter) {
+ return _iter_next_ofs(p_iter, 0);
}
-Variant PackedDataContainer::_iter_get(const Variant& p_iter){
+Variant PackedDataContainer::_iter_get(const Variant &p_iter) {
- return _iter_get_ofs(p_iter,0);
+ return _iter_get_ofs(p_iter, 0);
}
-
void PackedDataContainer::_bind_methods() {
- 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);
- ClassDB::bind_method(D_METHOD("pack:Error","value"),&PackedDataContainer::pack);
- ClassDB::bind_method(D_METHOD("size"),&PackedDataContainer::size);
+ 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);
+ ClassDB::bind_method(D_METHOD("pack:Error", "value"), &PackedDataContainer::pack);
+ ClassDB::bind_method(D_METHOD("size"), &PackedDataContainer::size);
- ADD_PROPERTY( PropertyInfo(Variant::POOL_BYTE_ARRAY,"__data__"),"_set_data","_get_data");
+ ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "__data__"), "_set_data", "_get_data");
}
-
PackedDataContainer::PackedDataContainer() {
- datalen=0;
+ datalen = 0;
}
-
//////////////////
+Variant PackedDataContainerRef::_iter_init(const Array &p_iter) {
-Variant PackedDataContainerRef::_iter_init(const Array& p_iter) {
-
-
- return from->_iter_init_ofs(p_iter,offset);
+ return from->_iter_init_ofs(p_iter, offset);
}
-Variant PackedDataContainerRef::_iter_next(const Array& p_iter){
-
- return from->_iter_next_ofs(p_iter,offset);
+Variant PackedDataContainerRef::_iter_next(const Array &p_iter) {
+ return from->_iter_next_ofs(p_iter, offset);
}
-Variant PackedDataContainerRef::_iter_get(const Variant& p_iter){
+Variant PackedDataContainerRef::_iter_get(const Variant &p_iter) {
- return from->_iter_get_ofs(p_iter,offset);
+ return from->_iter_get_ofs(p_iter, offset);
}
bool PackedDataContainerRef::_is_dictionary() const {
@@ -426,20 +403,19 @@ bool PackedDataContainerRef::_is_dictionary() const {
void PackedDataContainerRef::_bind_methods() {
- 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);
+ 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);
}
+Variant PackedDataContainerRef::getvar(const Variant &p_key, bool *r_valid) const {
-Variant PackedDataContainerRef::getvar(const Variant& p_key, bool *r_valid) const {
-
- bool err=false;
- Variant ret = from->_key_at_ofs(offset,p_key,err);
+ bool err = false;
+ Variant ret = from->_key_at_ofs(offset, p_key, err);
if (r_valid)
- *r_valid=!err;
+ *r_valid = !err;
return ret;
}
@@ -449,5 +425,4 @@ int PackedDataContainerRef::size() const {
};
PackedDataContainerRef::PackedDataContainerRef() {
-
}