summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorGeorge Marques <george@gmarqu.es>2017-06-19 02:46:29 -0300
committerGeorge Marques <george@gmarqu.es>2017-06-19 16:38:10 -0300
commitaf7d5902062546e961a24e133c09f8ea49ae42db (patch)
treec15e0117695621346264fc82e30eaf42c74f98a8 /core
parent9583ecf7e8042da67444802d94c57c797eb7e0a2 (diff)
Add basic compression functions to PoolBaseArray
Diffstat (limited to 'core')
-rw-r--r--core/variant_call.cpp41
1 files changed, 41 insertions, 0 deletions
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());