diff options
Diffstat (limited to 'core/io')
-rw-r--r-- | core/io/compression.cpp | 34 | ||||
-rw-r--r-- | core/io/compression.h | 8 | ||||
-rw-r--r-- | core/io/packet_peer.cpp | 2 | ||||
-rw-r--r-- | core/io/resource_format_binary.cpp | 73 |
4 files changed, 99 insertions, 18 deletions
diff --git a/core/io/compression.cpp b/core/io/compression.cpp index f806c4da6d..b0f5448b6c 100644 --- a/core/io/compression.cpp +++ b/core/io/compression.cpp @@ -52,23 +52,22 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, } } break; - case MODE_DEFLATE: { + case MODE_DEFLATE: + case MODE_GZIP: { + + int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16; z_stream strm; strm.zalloc = zipio_alloc; strm.zfree = zipio_free; strm.opaque = Z_NULL; - int level = GLOBAL_GET("compression/zlib/compression_level"); - int err = deflateInit(&strm, level); + int level = p_mode == MODE_DEFLATE ? zlib_level : gzip_level; + int err = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY); if (err != Z_OK) return -1; 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; @@ -81,8 +80,7 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, case MODE_ZSTD: { int max_dst_size = get_max_compressed_buffer_size(p_src_size, MODE_ZSTD); - int level = GLOBAL_GET("compression/zstd/compression_level"); - return ZSTD_compress(p_dst, max_dst_size, p_src, p_src_size, level); + return ZSTD_compress(p_dst, max_dst_size, p_src, p_src_size, zstd_level); } break; } @@ -100,13 +98,16 @@ int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) { return ss; } break; - case MODE_DEFLATE: { + case MODE_DEFLATE: + case MODE_GZIP: { + + int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16; z_stream strm; strm.zalloc = zipio_alloc; strm.zfree = zipio_free; strm.opaque = Z_NULL; - int err = deflateInit(&strm, Z_DEFAULT_COMPRESSION); + int err = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY); if (err != Z_OK) return -1; int aout = deflateBound(&strm, p_src_size); @@ -138,7 +139,10 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p } return ret_size; } break; - case MODE_DEFLATE: { + case MODE_DEFLATE: + case MODE_GZIP: { + + int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16; z_stream strm; strm.zalloc = zipio_alloc; @@ -146,7 +150,7 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p strm.opaque = Z_NULL; strm.avail_in = 0; strm.next_in = Z_NULL; - int err = inflateInit(&strm); + int err = inflateInit2(&strm, window_bits); ERR_FAIL_COND_V(err != Z_OK, -1); strm.avail_in = p_src_size; @@ -168,3 +172,7 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p ERR_FAIL_V(-1); } + +int Compression::zlib_level = Z_DEFAULT_COMPRESSION; +int Compression::gzip_level = Z_DEFAULT_COMPRESSION; +int Compression::zstd_level = 3; diff --git a/core/io/compression.h b/core/io/compression.h index 742f0f4d68..5eb7806d7b 100644 --- a/core/io/compression.h +++ b/core/io/compression.h @@ -33,11 +33,17 @@ #include "typedefs.h" class Compression { + public: + static int zlib_level; + static int gzip_level; + static int zstd_level; + enum Mode { MODE_FASTLZ, MODE_DEFLATE, - MODE_ZSTD + MODE_ZSTD, + MODE_GZIP }; static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD); diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index ac68d5240c..93682e6b8a 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -254,7 +254,7 @@ void PacketPeerStream::set_input_buffer_max_size(int p_max_size) { PacketPeerStream::PacketPeerStream() { - int rbsize = GLOBAL_GET("network/packets/packet_stream_peer_max_buffer_po2"); + int rbsize = GLOBAL_GET("network/limits/packet_peer_stream/max_buffer_po2"); ring_buffer.resize(rbsize); temp_buffer.resize(1 << rbsize); diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index b474c2e078..728cd5d4ff 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "resource_format_binary.h" #include "global_config.h" +#include "image.h" #include "io/file_access_compressed.h" #include "io/marshalls.h" #include "os/dir_access.h" @@ -54,7 +55,6 @@ enum { VARIANT_TRANSFORM = 17, VARIANT_MATRIX32 = 18, VARIANT_COLOR = 20, - //VARIANT_IMAGE = 21, - no longer variant type VARIANT_NODE_PATH = 22, VARIANT_RID = 23, VARIANT_OBJECT = 24, @@ -70,7 +70,13 @@ enum { VARIANT_VECTOR2_ARRAY = 37, VARIANT_INT64 = 40, VARIANT_DOUBLE = 41, - +#ifndef DISABLE_DEPRECATED + VARIANT_IMAGE = 21, // - no longer variant type + IMAGE_ENCODING_EMPTY = 0, + IMAGE_ENCODING_RAW = 1, + IMAGE_ENCODING_LOSSLESS = 2, + IMAGE_ENCODING_LOSSY = 3, +#endif OBJECT_EMPTY = 0, OBJECT_EXTERNAL_RESOURCE = 1, OBJECT_INTERNAL_RESOURCE = 2, @@ -541,7 +547,69 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { w = PoolVector<Color>::Write(); r_v = array; } break; +#ifndef DISABLE_DEPRECATED + case VARIANT_IMAGE: { + uint32_t encoding = f->get_32(); + if (encoding == IMAGE_ENCODING_EMPTY) { + r_v = Ref<Image>(); + break; + } else if (encoding == IMAGE_ENCODING_RAW) { + uint32_t width = f->get_32(); + uint32_t height = f->get_32(); + uint32_t mipmaps = f->get_32(); + uint32_t format = f->get_32(); + const uint32_t format_version_shift = 24; + const uint32_t format_version_mask = format_version_shift - 1; + + uint32_t format_version = format >> format_version_shift; + + const uint32_t current_version = 0; + if (format_version > current_version) { + + ERR_PRINT("Format version for encoded binary image is too new"); + return ERR_PARSE_ERROR; + } + + Image::Format fmt = Image::Format(format & format_version_mask); //if format changes, we can add a compatibility bit on top + + uint32_t datalen = f->get_32(); + + PoolVector<uint8_t> imgdata; + imgdata.resize(datalen); + PoolVector<uint8_t>::Write w = imgdata.write(); + f->get_buffer(w.ptr(), datalen); + _advance_padding(datalen); + w = PoolVector<uint8_t>::Write(); + + Ref<Image> image; + image.instance(); + image->create(width, height, mipmaps, fmt, imgdata); + r_v = image; + + } else { + //compressed + PoolVector<uint8_t> data; + data.resize(f->get_32()); + PoolVector<uint8_t>::Write w = data.write(); + f->get_buffer(w.ptr(), data.size()); + w = PoolVector<uint8_t>::Write(); + + Ref<Image> image; + if (encoding == IMAGE_ENCODING_LOSSY && Image::lossy_unpacker) { + + image = Image::lossy_unpacker(data); + } else if (encoding == IMAGE_ENCODING_LOSSLESS && Image::lossless_unpacker) { + + image = Image::lossless_unpacker(data); + } + _advance_padding(data.size()); + + r_v = image; + } + + } break; +#endif default: { ERR_FAIL_V(ERR_FILE_CORRUPT); } break; @@ -1644,7 +1712,6 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant get_string_index(np.get_property()); } break; - default: {} } } |