Add compression support for File object

This commit is contained in:
George Marques 2017-06-19 13:28:00 -03:00
parent af7d590206
commit 5c779c574a
No known key found for this signature in database
GPG key ID: 046BD46A3201E43D
2 changed files with 32 additions and 0 deletions

View file

@ -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() {

View file

@ -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