diff options
author | Thomas Herzog <karroffel@users.noreply.github.com> | 2017-06-19 22:59:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-19 22:59:24 +0200 |
commit | 9d67be447a80a742a9d5c5f12363da6d77f7284d (patch) | |
tree | 6a50b1bb75b239159fdc4ae1d32652b07b85a06b /core | |
parent | 6b93455b6b8c3c684a8e4b4b09dc54ca30f15c94 (diff) | |
parent | 34b6922caafe0138668859864adda19c4b803946 (diff) |
Merge pull request #9274 from vnen/compress-functions
Expose compression functions to GDScript
Diffstat (limited to 'core')
-rw-r--r-- | core/bind/core_bind.cpp | 24 | ||||
-rw-r--r-- | core/bind/core_bind.h | 8 | ||||
-rw-r--r-- | core/variant_call.cpp | 41 |
3 files changed, 73 insertions, 0 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index d81ccf0265..095f058ed9 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -31,6 +31,7 @@ #include "core/global_config.h" #include "geometry.h" +#include "io/file_access_compressed.h" #include "io/file_access_encrypted.h" #include "io/marshalls.h" #include "os/keyboard.h" @@ -1395,6 +1396,24 @@ Error _File::open_encrypted_pass(const String &p_path, int p_mode_flags, const S return OK; } +Error _File::open_compressed(const String &p_path, int p_mode_flags, int p_compress_mode) { + + FileAccessCompressed *fac = memnew(FileAccessCompressed); + Error err = OK; + + fac->configure("GCPF", (Compression::Mode)p_compress_mode); + + err = fac->_open(p_path, p_mode_flags); + + if (err) { + memdelete(fac); + return err; + } + + f = fac; + return OK; +} + Error _File::open(const String &p_path, int p_mode_flags) { close(); @@ -1700,6 +1719,7 @@ void _File::_bind_methods() { ClassDB::bind_method(D_METHOD("open_encrypted", "path", "mode_flags", "key"), &_File::open_encrypted); ClassDB::bind_method(D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &_File::open_encrypted_pass); + ClassDB::bind_method(D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &_File::open_compressed, DEFVAL(0)); ClassDB::bind_method(D_METHOD("open", "path", "flags"), &_File::open); ClassDB::bind_method(D_METHOD("close"), &_File::close); @@ -1749,6 +1769,10 @@ void _File::_bind_methods() { BIND_CONSTANT(WRITE); BIND_CONSTANT(READ_WRITE); BIND_CONSTANT(WRITE_READ); + + BIND_CONSTANT(COMPRESSION_FASTLZ); + BIND_CONSTANT(COMPRESSION_DEFLATE); + BIND_CONSTANT(COMPRESSION_ZSTD); } _File::_File() { diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index a2fb6c966c..87d84c0732 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -31,6 +31,7 @@ #define CORE_BIND_H #include "image.h" +#include "io/compression.h" #include "io/resource_loader.h" #include "io/resource_saver.h" #include "os/dir_access.h" @@ -366,8 +367,15 @@ public: WRITE_READ = 7, }; + enum CompressionMode { + COMPRESSION_FASTLZ = Compression::MODE_FASTLZ, + COMPRESSION_DEFLATE = Compression::MODE_DEFLATE, + COMPRESSION_ZSTD = Compression::MODE_ZSTD + }; + Error open_encrypted(const String &p_path, int p_mode_flags, const Vector<uint8_t> &p_key); Error open_encrypted_pass(const String &p_path, int p_mode_flags, const String &p_pass); + Error open_compressed(const String &p_path, int p_mode_flags, int p_compress_mode = 0); Error open(const String &p_path, int p_mode_flags); ///< open a file void close(); ///< close a file diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 9ead727a80..6936a362e1 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -30,6 +30,7 @@ #include "variant.h" #include "core_string_names.h" +#include "io/compression.h" #include "object.h" #include "os/os.h" #include "script_language.h" @@ -509,6 +510,44 @@ struct _VariantCall { r_ret = s; } + static void _call_PoolByteArray_compress(Variant &r_ret, Variant &p_self, const Variant **p_args) { + + PoolByteArray *ba = reinterpret_cast<PoolByteArray *>(p_self._data._mem); + PoolByteArray compressed; + Compression::Mode mode = (Compression::Mode)(int)(*p_args[0]); + + compressed.resize(Compression::get_max_compressed_buffer_size(ba->size())); + int result = Compression::compress(compressed.write().ptr(), ba->read().ptr(), ba->size(), mode); + + result = result >= 0 ? result : 0; + compressed.resize(result); + + r_ret = compressed; + } + + static void _call_PoolByteArray_decompress(Variant &r_ret, Variant &p_self, const Variant **p_args) { + + PoolByteArray *ba = reinterpret_cast<PoolByteArray *>(p_self._data._mem); + PoolByteArray decompressed; + Compression::Mode mode = (Compression::Mode)(int)(*p_args[1]); + + int buffer_size = (int)(*p_args[0]); + + if (buffer_size < 0) { + r_ret = decompressed; + ERR_EXPLAIN("Decompression buffer size is less than zero"); + ERR_FAIL(); + } + + decompressed.resize(buffer_size); + int result = Compression::decompress(decompressed.write().ptr(), buffer_size, ba->read().ptr(), ba->size(), mode); + + result = result >= 0 ? result : 0; + decompressed.resize(result); + + r_ret = decompressed; + } + VCALL_LOCALMEM0R(PoolByteArray, size); VCALL_LOCALMEM2(PoolByteArray, set); VCALL_LOCALMEM1R(PoolByteArray, get); @@ -1552,6 +1591,8 @@ void register_variant_methods() { ADDFUNC0(POOL_BYTE_ARRAY, STRING, PoolByteArray, get_string_from_ascii, varray()); ADDFUNC0(POOL_BYTE_ARRAY, STRING, PoolByteArray, get_string_from_utf8, varray()); + ADDFUNC1(POOL_BYTE_ARRAY, POOL_BYTE_ARRAY, PoolByteArray, compress, INT, "compression_mode", varray(0)); + ADDFUNC2(POOL_BYTE_ARRAY, POOL_BYTE_ARRAY, PoolByteArray, decompress, INT, "buffer_size", INT, "compression_mode", varray(0)); ADDFUNC0(POOL_INT_ARRAY, INT, PoolIntArray, size, varray()); ADDFUNC2(POOL_INT_ARRAY, NIL, PoolIntArray, set, INT, "idx", INT, "integer", varray()); |