diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/io/compression.cpp | 23 | ||||
-rw-r--r-- | core/io/compression.h | 2 | ||||
-rw-r--r-- | core/io/stream_peer.cpp | 143 | ||||
-rw-r--r-- | core/io/stream_peer.h | 36 | ||||
-rw-r--r-- | core/path_db.cpp | 4 | ||||
-rw-r--r-- | core/register_core_types.cpp | 1 | ||||
-rw-r--r-- | core/string_db.cpp | 6 |
7 files changed, 199 insertions, 16 deletions
diff --git a/core/io/compression.cpp b/core/io/compression.cpp index a17e358cbb..ca44d24911 100644 --- a/core/io/compression.cpp +++ b/core/io/compression.cpp @@ -60,6 +60,10 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size,M strm.avail_in=p_src_size; int aout = deflateBound(&strm,p_src_size);; + /*if (aout>p_src_size) { + deflateEnd(&strm); + return -1; + }*/ strm.avail_out=aout; strm.next_in=(Bytef*)p_src; strm.next_out=p_dst; @@ -107,19 +111,21 @@ int Compression::get_max_compressed_buffer_size(int p_src_size,Mode p_mode){ -void Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode){ +int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode){ switch(p_mode) { case MODE_FASTLZ: { + int ret_size=0; + if (p_dst_max_size<16) { uint8_t dst[16]; - fastlz_decompress(p_src,p_src_size,dst,16); + ret_size = fastlz_decompress(p_src,p_src_size,dst,16); copymem(p_dst,dst,p_dst_max_size); } else { - fastlz_decompress(p_src,p_src_size,p_dst,p_dst_max_size); + ret_size = fastlz_decompress(p_src,p_src_size,p_dst,p_dst_max_size); } - return; + return ret_size; } break; case MODE_DEFLATE: { @@ -130,7 +136,7 @@ void Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t * strm.avail_in= 0; strm.next_in=Z_NULL; int err = inflateInit(&strm); - ERR_FAIL_COND(err!=Z_OK); + ERR_FAIL_COND_V(err!=Z_OK,-1); strm.avail_in=p_src_size; strm.avail_out=p_dst_max_size; @@ -138,11 +144,12 @@ void Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t * strm.next_out=p_dst; err = inflate(&strm,Z_FINISH); + int total = strm.total_out; inflateEnd(&strm); - ERR_FAIL_COND(err!=Z_STREAM_END); - return; + ERR_FAIL_COND_V(err!=Z_STREAM_END,-1); + return total; } break; } - ERR_FAIL(); + ERR_FAIL_V(-1); } diff --git a/core/io/compression.h b/core/io/compression.h index 07a293c940..e0a4d31a51 100644 --- a/core/io/compression.h +++ b/core/io/compression.h @@ -43,7 +43,7 @@ public: static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size,Mode p_mode=MODE_FASTLZ); static int get_max_compressed_buffer_size(int p_src_size,Mode p_mode=MODE_FASTLZ); - static void decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode=MODE_FASTLZ); + static int decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode=MODE_FASTLZ); Compression(); }; diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index 306e7d8c9d..176d55ffdd 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -222,6 +222,7 @@ void StreamPeer::put_double(double p_val){ void StreamPeer::put_utf8_string(const String& p_string) { CharString cs=p_string.utf8(); + put_u32(p_string.length()); put_data((const uint8_t*)cs.get_data(),cs.length()); } @@ -348,8 +349,10 @@ String StreamPeer::get_string(int p_bytes){ ERR_FAIL_COND_V(p_bytes<0,String()); Vector<char> buf; - buf.resize(p_bytes+1); - get_data((uint8_t*)&buf[0],p_bytes); + Error err = buf.resize(p_bytes+1); + ERR_FAIL_COND_V(err!=OK,String()); + err = get_data((uint8_t*)&buf[0],p_bytes); + ERR_FAIL_COND_V(err!=OK,String()); buf[p_bytes]=0; return buf.ptr(); @@ -359,8 +362,10 @@ String StreamPeer::get_utf8_string(int p_bytes){ ERR_FAIL_COND_V(p_bytes<0,String()); Vector<uint8_t> buf; - buf.resize(p_bytes); - get_data(buf.ptr(),p_bytes); + Error err = buf.resize(p_bytes); + ERR_FAIL_COND_V(err!=OK,String()); + err = get_data(buf.ptr(),p_bytes); + ERR_FAIL_COND_V(err!=OK,String()); String ret; ret.parse_utf8((const char*)buf.ptr(),buf.size()); @@ -371,8 +376,10 @@ Variant StreamPeer::get_var(){ int len = get_32(); Vector<uint8_t> var; - var.resize(len); - get_data(var.ptr(),len); + Error err = var.resize(len); + ERR_FAIL_COND_V(err!=OK,Variant()); + err = get_data(var.ptr(),len); + ERR_FAIL_COND_V(err!=OK,Variant()); Variant ret; decode_variant(ret,var.ptr(),len); @@ -420,3 +427,127 @@ void StreamPeer::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_utf8_string","bytes"),&StreamPeer::get_utf8_string); ObjectTypeDB::bind_method(_MD("get_var:Variant"),&StreamPeer::get_var); } +//////////////////////////////// + + +void StreamPeerBuffer::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("seek","pos"),&StreamPeerBuffer::seek); + ObjectTypeDB::bind_method(_MD("get_size"),&StreamPeerBuffer::get_size); + ObjectTypeDB::bind_method(_MD("get_pos"),&StreamPeerBuffer::get_pos); + ObjectTypeDB::bind_method(_MD("resize","size"),&StreamPeerBuffer::resize); + ObjectTypeDB::bind_method(_MD("set_data_array","data"),&StreamPeerBuffer::set_data_array); + ObjectTypeDB::bind_method(_MD("get_data_array"),&StreamPeerBuffer::get_data_array); + ObjectTypeDB::bind_method(_MD("clear"),&StreamPeerBuffer::clear); + ObjectTypeDB::bind_method(_MD("duplicate:StreamPeerBuffer"),&StreamPeerBuffer::duplicate); + +} + + +Error StreamPeerBuffer::put_data(const uint8_t* p_data,int p_bytes) { + + if (p_bytes<=0) + return OK; + + if (pointer+p_bytes > data.size()) { + data.resize(pointer+p_bytes); + + } + + DVector<uint8_t>::Write w = data.write(); + copymem(&w[pointer],p_data,p_bytes); + + pointer+=p_bytes; + return OK; +} + +Error StreamPeerBuffer::put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent){ + + r_sent=p_bytes; + return put_data(p_data,p_bytes); +} + +Error StreamPeerBuffer::get_data(uint8_t* p_buffer, int p_bytes){ + + int recv; + get_partial_data(p_buffer,p_bytes,recv); + if (recv!=p_bytes) + return ERR_INVALID_PARAMETER; + + return OK; + +} +Error StreamPeerBuffer::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received){ + + + if (pointer+p_bytes > data.size()) { + r_received=data.size()-pointer; + if (r_received<=0) { + r_received=0; + return OK; //you got 0 + } + } else { + r_received=p_bytes; + } + + DVector<uint8_t>::Read r = data.read(); + copymem(p_buffer,r.ptr(),r_received); +} + +int StreamPeerBuffer::get_available_bytes() const { + + return data.size()-pointer; +} + +void StreamPeerBuffer::seek(int p_pos){ + + ERR_FAIL_COND(p_pos < 0); + ERR_FAIL_COND(p_pos > data.size()); + pointer=p_pos; +} +int StreamPeerBuffer::get_size() const{ + + return data.size(); +} + +int StreamPeerBuffer::get_pos() const { + + return pointer; +} + +void StreamPeerBuffer::resize(int p_size){ + + data.resize(p_size); +} + +void StreamPeerBuffer::set_data_array(const DVector<uint8_t> & p_data){ + + data=p_data; + pointer=0; +} + +DVector<uint8_t> StreamPeerBuffer::get_data_array() const{ + + return data; +} + + +void StreamPeerBuffer::clear() { + + data.resize(0); + pointer=0; +} + + +Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const { + + Ref<StreamPeerBuffer> spb; + spb.instance(); + spb->data=data; +} + + +StreamPeerBuffer::StreamPeerBuffer() { + + pointer=0; +} diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h index 970e6695a5..f28e6f594d 100644 --- a/core/io/stream_peer.h +++ b/core/io/stream_peer.h @@ -91,4 +91,40 @@ public: StreamPeer() { big_endian=false; } }; + +class StreamPeerBuffer : public StreamPeer { + + OBJ_TYPE(StreamPeerBuffer,StreamPeer); + + DVector<uint8_t> data; + int pointer; +protected: + + static void _bind_methods(); +public: + Error put_data(const uint8_t* p_data,int p_bytes); + Error put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent); + + Error get_data(uint8_t* p_buffer, int p_bytes); + Error get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received); + + virtual int get_available_bytes() const; + + void seek(int p_pos); + int get_size() const; + int get_pos() const; + void resize(int p_size); + + + void set_data_array(const DVector<uint8_t> & p_data); + DVector<uint8_t> get_data_array() const; + + void clear(); + + Ref<StreamPeerBuffer> duplicate() const; + + StreamPeerBuffer(); +}; + + #endif // STREAM_PEER_H diff --git a/core/path_db.cpp b/core/path_db.cpp index d0feda5c82..132cc83a35 100644 --- a/core/path_db.cpp +++ b/core/path_db.cpp @@ -363,6 +363,7 @@ NodePath::NodePath(const String& p_path) { from=i+1; } + } path=path.substr(0,subpath_pos); @@ -380,6 +381,8 @@ NodePath::NodePath(const String& p_path) { last_is_slash=false; } + + } if (slices==0 && !absolute && !property) @@ -413,6 +416,7 @@ NodePath::NodePath(const String& p_path) { } else { last_is_slash=false; } + } diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 97bd5f2a32..3de26573f4 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -118,6 +118,7 @@ void register_core_types() { ObjectTypeDB::register_type<Resource>(); ObjectTypeDB::register_type<FuncRef>(); ObjectTypeDB::register_virtual_type<StreamPeer>(); + ObjectTypeDB::register_type<StreamPeerBuffer>(); ObjectTypeDB::register_create_type<StreamPeerTCP>(); ObjectTypeDB::register_create_type<TCP_Server>(); ObjectTypeDB::register_create_type<PacketPeerUDP>(); diff --git a/core/string_db.cpp b/core/string_db.cpp index 9a693f88e9..bf92c4eac4 100644 --- a/core/string_db.cpp +++ b/core/string_db.cpp @@ -183,7 +183,8 @@ StringName::StringName(const char *p_name) { ERR_FAIL_COND(!configured); - ERR_FAIL_COND( !p_name || !p_name[0]); + if (!p_name || p_name[0]==0) + return; //empty, ignore _global_lock(); @@ -288,6 +289,9 @@ StringName::StringName(const String& p_name) { ERR_FAIL_COND(!configured); + if (p_name==String()) + return; + _global_lock(); uint32_t hash = p_name.hash(); |