Browse Source

Merge pull request #9274 from vnen/compress-functions

Expose compression functions to GDScript
Thomas Herzog 8 years ago
parent
commit
9d67be447a
4 changed files with 115 additions and 0 deletions
  1. 24 0
      core/bind/core_bind.cpp
  2. 8 0
      core/bind/core_bind.h
  3. 41 0
      core/variant_call.cpp
  4. 42 0
      doc/base/classes.xml

+ 24 - 0
core/bind/core_bind.cpp

@@ -31,6 +31,7 @@
 
 
 #include "core/global_config.h"
 #include "core/global_config.h"
 #include "geometry.h"
 #include "geometry.h"
+#include "io/file_access_compressed.h"
 #include "io/file_access_encrypted.h"
 #include "io/file_access_encrypted.h"
 #include "io/marshalls.h"
 #include "io/marshalls.h"
 #include "os/keyboard.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;
 	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) {
 Error _File::open(const String &p_path, int p_mode_flags) {
 
 
 	close();
 	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", "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_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("open", "path", "flags"), &_File::open);
 	ClassDB::bind_method(D_METHOD("close"), &_File::close);
 	ClassDB::bind_method(D_METHOD("close"), &_File::close);
@@ -1749,6 +1769,10 @@ void _File::_bind_methods() {
 	BIND_CONSTANT(WRITE);
 	BIND_CONSTANT(WRITE);
 	BIND_CONSTANT(READ_WRITE);
 	BIND_CONSTANT(READ_WRITE);
 	BIND_CONSTANT(WRITE_READ);
 	BIND_CONSTANT(WRITE_READ);
+
+	BIND_CONSTANT(COMPRESSION_FASTLZ);
+	BIND_CONSTANT(COMPRESSION_DEFLATE);
+	BIND_CONSTANT(COMPRESSION_ZSTD);
 }
 }
 
 
 _File::_File() {
 _File::_File() {

+ 8 - 0
core/bind/core_bind.h

@@ -31,6 +31,7 @@
 #define CORE_BIND_H
 #define CORE_BIND_H
 
 
 #include "image.h"
 #include "image.h"
+#include "io/compression.h"
 #include "io/resource_loader.h"
 #include "io/resource_loader.h"
 #include "io/resource_saver.h"
 #include "io/resource_saver.h"
 #include "os/dir_access.h"
 #include "os/dir_access.h"
@@ -366,8 +367,15 @@ public:
 		WRITE_READ = 7,
 		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(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_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
 	Error open(const String &p_path, int p_mode_flags); ///< open a file
 	void close(); ///< close a file
 	void close(); ///< close a file

+ 41 - 0
core/variant_call.cpp

@@ -30,6 +30,7 @@
 #include "variant.h"
 #include "variant.h"
 
 
 #include "core_string_names.h"
 #include "core_string_names.h"
+#include "io/compression.h"
 #include "object.h"
 #include "object.h"
 #include "os/os.h"
 #include "os/os.h"
 #include "script_language.h"
 #include "script_language.h"
@@ -509,6 +510,44 @@ struct _VariantCall {
 		r_ret = s;
 		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_LOCALMEM0R(PoolByteArray, size);
 	VCALL_LOCALMEM2(PoolByteArray, set);
 	VCALL_LOCALMEM2(PoolByteArray, set);
 	VCALL_LOCALMEM1R(PoolByteArray, get);
 	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_ascii, varray());
 	ADDFUNC0(POOL_BYTE_ARRAY, STRING, PoolByteArray, get_string_from_utf8, 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());
 	ADDFUNC0(POOL_INT_ARRAY, INT, PoolIntArray, size, varray());
 	ADDFUNC2(POOL_INT_ARRAY, NIL, PoolIntArray, set, INT, "idx", INT, "integer", varray());
 	ADDFUNC2(POOL_INT_ARRAY, NIL, PoolIntArray, set, INT, "idx", INT, "integer", varray());

+ 42 - 0
doc/base/classes.xml

@@ -15299,6 +15299,19 @@
 				Open the file for writing or reading, depending on the flags.
 				Open the file for writing or reading, depending on the flags.
 			</description>
 			</description>
 		</method>
 		</method>
+		<method name="open_compressed">
+			<return type="int">
+			</return>
+			<argument index="0" name="path" type="String">
+			</argument>
+			<argument index="1" name="mode_flags" type="int">
+			</argument>
+			<argument index="2" name="compression_mode" type="int" default="0">
+			</argument>
+			<description>
+				Open a compressed file for reading or writing. The compression_mode can be set as one of the COMPRESSION_* constants.
+			</description>
+		</method>
 		<method name="open_encrypted">
 		<method name="open_encrypted">
 			<return type="int">
 			<return type="int">
 			</return>
 			</return>
@@ -15445,6 +15458,15 @@
 		<constant name="WRITE_READ" value="7">
 		<constant name="WRITE_READ" value="7">
 			Open the file for reading and writing. Create it if the file not exists and truncate if it exists.
 			Open the file for reading and writing. Create it if the file not exists and truncate if it exists.
 		</constant>
 		</constant>
+		<constant name="COMPRESSION_FASTLZ" value="0">
+			Use the FastLZ compression method.
+		</constant>
+		<constant name="COMPRESSION_DEFLATE" value="1">
+			Use the Deflate compression method.
+		</constant>
+		<constant name="COMPRESSION_ZSTD" value="2">
+			Use the Zstd compression method.
+		</constant>
 	</constants>
 	</constants>
 </class>
 </class>
 <class name="FileDialog" inherits="ConfirmationDialog" category="Core">
 <class name="FileDialog" inherits="ConfirmationDialog" category="Core">
@@ -33764,6 +33786,26 @@
 				Append an [PoolByteArray] at the end of this array.
 				Append an [PoolByteArray] at the end of this array.
 			</description>
 			</description>
 		</method>
 		</method>
+		<method name="compress">
+			<return type="PoolByteArray">
+			</return>
+			<argument index="0" name="compression_mode" type="int" default="0">
+			</argument>
+			<description>
+			Returns a new [PoolByteArray] with the data compressed. The compression mode can be set using one of the COMPRESS_* constants of [File].
+			</description>
+		</method>
+		<method name="decompress">
+			<return type="PoolByteArray">
+			</return>
+			<argument index="0" name="buffer_size" type="int">
+			</argument>
+			<argument index="1" name="compression_mode" type="int" default="0">
+			</argument>
+			<description>
+			Returns a new [PoolByteArray] with the data decompressed. The buffer_size should be set as the size of the uncompressed data. The compression mode can be set using one of the COMPRESS_* constants of [File].
+			</description>
+		</method>
 		<method name="get_string_from_ascii">
 		<method name="get_string_from_ascii">
 			<return type="String">
 			<return type="String">
 			</return>
 			</return>