1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-23 15:16:17 +00:00

Make FileAccess and DirAccess classes reference counted.

This commit is contained in:
bruvzg
2022-03-23 11:08:58 +02:00
parent ca9372622f
commit 9381acb6a4
193 changed files with 1122 additions and 1776 deletions

View File

@@ -38,20 +38,26 @@ ZipArchive *ZipArchive::instance = nullptr;
extern "C" {
static void *godot_open(void *data, const char *p_fname, int mode) {
struct ZipData {
Ref<FileAccess> f;
};
static void *godot_open(voidpf opaque, const char *p_fname, int mode) {
if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
return nullptr;
}
FileAccess *f = FileAccess::open(p_fname, FileAccess::READ);
ERR_FAIL_COND_V(!f, nullptr);
Ref<FileAccess> f = FileAccess::open(p_fname, FileAccess::READ);
ERR_FAIL_COND_V(f.is_null(), nullptr);
return f;
ZipData *zd = memnew(ZipData);
zd->f = f;
return zd;
}
static uLong godot_read(void *data, void *fdata, void *buf, uLong size) {
FileAccess *f = (FileAccess *)fdata;
f->get_buffer((uint8_t *)buf, size);
static uLong godot_read(voidpf opaque, voidpf stream, void *buf, uLong size) {
ZipData *zd = (ZipData *)stream;
zd->f->get_buffer((uint8_t *)buf, size);
return size;
}
@@ -60,42 +66,38 @@ static uLong godot_write(voidpf opaque, voidpf stream, const void *buf, uLong si
}
static long godot_tell(voidpf opaque, voidpf stream) {
FileAccess *f = (FileAccess *)stream;
return f->get_position();
ZipData *zd = (ZipData *)stream;
return zd->f->get_position();
}
static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
FileAccess *f = (FileAccess *)stream;
ZipData *zd = (ZipData *)stream;
uint64_t pos = offset;
switch (origin) {
case ZLIB_FILEFUNC_SEEK_CUR:
pos = f->get_position() + offset;
pos = zd->f->get_position() + offset;
break;
case ZLIB_FILEFUNC_SEEK_END:
pos = f->get_length() + offset;
pos = zd->f->get_length() + offset;
break;
default:
break;
}
f->seek(pos);
zd->f->seek(pos);
return 0;
}
static int godot_close(voidpf opaque, voidpf stream) {
FileAccess *f = (FileAccess *)stream;
if (f) {
f->close();
memdelete(f);
f = nullptr;
}
ZipData *zd = (ZipData *)stream;
memdelete(zd);
return 0;
}
static int godot_testerror(voidpf opaque, voidpf stream) {
FileAccess *f = (FileAccess *)stream;
return f->get_error() != OK ? 1 : 0;
ZipData *zd = (ZipData *)stream;
return zd->f->get_error() != OK ? 1 : 0;
}
static voidpf godot_alloc(voidpf opaque, uInt items, uInt size) {
@@ -208,7 +210,7 @@ bool ZipArchive::file_exists(String p_name) const {
return files.has(p_name);
}
FileAccess *ZipArchive::get_file(const String &p_path, PackedData::PackedFile *p_file) {
Ref<FileAccess> ZipArchive::get_file(const String &p_path, PackedData::PackedFile *p_file) {
return memnew(FileAccessZip(p_path, *p_file));
}