feat: godot-engine-source-4.3-stable
This commit is contained in:
parent
c59a7dcade
commit
7125d019b5
11149 changed files with 5070401 additions and 0 deletions
9
engine/modules/zip/SCsub
Normal file
9
engine/modules/zip/SCsub
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
Import("env")
|
||||
Import("env_modules")
|
||||
|
||||
env_zip = env_modules.Clone()
|
||||
|
||||
# Module files
|
||||
env_zip.add_source_files(env.modules_sources, "*.cpp")
|
||||
17
engine/modules/zip/config.py
Normal file
17
engine/modules/zip/config.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
def can_build(env, platform):
|
||||
return env["minizip"]
|
||||
|
||||
|
||||
def configure(env):
|
||||
pass
|
||||
|
||||
|
||||
def get_doc_classes():
|
||||
return [
|
||||
"ZIPReader",
|
||||
"ZIPPacker",
|
||||
]
|
||||
|
||||
|
||||
def get_doc_path():
|
||||
return "doc_classes"
|
||||
75
engine/modules/zip/doc_classes/ZIPPacker.xml
Normal file
75
engine/modules/zip/doc_classes/ZIPPacker.xml
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<class name="ZIPPacker" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
|
||||
<brief_description>
|
||||
Allows the creation of zip files.
|
||||
</brief_description>
|
||||
<description>
|
||||
This class implements a writer that allows storing the multiple blobs in a zip archive.
|
||||
[codeblock]
|
||||
func write_zip_file():
|
||||
var writer := ZIPPacker.new()
|
||||
var err := writer.open("user://archive.zip")
|
||||
if err != OK:
|
||||
return err
|
||||
writer.start_file("hello.txt")
|
||||
writer.write_file("Hello World".to_utf8_buffer())
|
||||
writer.close_file()
|
||||
|
||||
writer.close()
|
||||
return OK
|
||||
[/codeblock]
|
||||
</description>
|
||||
<tutorials>
|
||||
</tutorials>
|
||||
<methods>
|
||||
<method name="close">
|
||||
<return type="int" enum="Error" />
|
||||
<description>
|
||||
Closes the underlying resources used by this instance.
|
||||
</description>
|
||||
</method>
|
||||
<method name="close_file">
|
||||
<return type="int" enum="Error" />
|
||||
<description>
|
||||
Stops writing to a file within the archive.
|
||||
It will fail if there is no open file.
|
||||
</description>
|
||||
</method>
|
||||
<method name="open">
|
||||
<return type="int" enum="Error" />
|
||||
<param index="0" name="path" type="String" />
|
||||
<param index="1" name="append" type="int" enum="ZIPPacker.ZipAppend" default="0" />
|
||||
<description>
|
||||
Opens a zip file for writing at the given path using the specified write mode.
|
||||
This must be called before everything else.
|
||||
</description>
|
||||
</method>
|
||||
<method name="start_file">
|
||||
<return type="int" enum="Error" />
|
||||
<param index="0" name="path" type="String" />
|
||||
<description>
|
||||
Starts writing to a file within the archive. Only one file can be written at the same time.
|
||||
Must be called after [method open].
|
||||
</description>
|
||||
</method>
|
||||
<method name="write_file">
|
||||
<return type="int" enum="Error" />
|
||||
<param index="0" name="data" type="PackedByteArray" />
|
||||
<description>
|
||||
Write the given [param data] to the file.
|
||||
Needs to be called after [method start_file].
|
||||
</description>
|
||||
</method>
|
||||
</methods>
|
||||
<constants>
|
||||
<constant name="APPEND_CREATE" value="0" enum="ZipAppend">
|
||||
Create a new zip archive at the given path.
|
||||
</constant>
|
||||
<constant name="APPEND_CREATEAFTER" value="1" enum="ZipAppend">
|
||||
Append a new zip archive to the end of the already existing file at the given path.
|
||||
</constant>
|
||||
<constant name="APPEND_ADDINZIP" value="2" enum="ZipAppend">
|
||||
Add new files to the existing zip archive at the given path.
|
||||
</constant>
|
||||
</constants>
|
||||
</class>
|
||||
61
engine/modules/zip/doc_classes/ZIPReader.xml
Normal file
61
engine/modules/zip/doc_classes/ZIPReader.xml
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<class name="ZIPReader" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
|
||||
<brief_description>
|
||||
Allows reading the content of a zip file.
|
||||
</brief_description>
|
||||
<description>
|
||||
This class implements a reader that can extract the content of individual files inside a zip archive.
|
||||
[codeblock]
|
||||
func read_zip_file():
|
||||
var reader := ZIPReader.new()
|
||||
var err := reader.open("user://archive.zip")
|
||||
if err != OK:
|
||||
return PackedByteArray()
|
||||
var res := reader.read_file("hello.txt")
|
||||
reader.close()
|
||||
return res
|
||||
[/codeblock]
|
||||
</description>
|
||||
<tutorials>
|
||||
</tutorials>
|
||||
<methods>
|
||||
<method name="close">
|
||||
<return type="int" enum="Error" />
|
||||
<description>
|
||||
Closes the underlying resources used by this instance.
|
||||
</description>
|
||||
</method>
|
||||
<method name="file_exists">
|
||||
<return type="bool" />
|
||||
<param index="0" name="path" type="String" />
|
||||
<param index="1" name="case_sensitive" type="bool" default="true" />
|
||||
<description>
|
||||
Returns [code]true[/code] if the file exists in the loaded zip archive.
|
||||
Must be called after [method open].
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_files">
|
||||
<return type="PackedStringArray" />
|
||||
<description>
|
||||
Returns the list of names of all files in the loaded archive.
|
||||
Must be called after [method open].
|
||||
</description>
|
||||
</method>
|
||||
<method name="open">
|
||||
<return type="int" enum="Error" />
|
||||
<param index="0" name="path" type="String" />
|
||||
<description>
|
||||
Opens the zip archive at the given [param path] and reads its file index.
|
||||
</description>
|
||||
</method>
|
||||
<method name="read_file">
|
||||
<return type="PackedByteArray" />
|
||||
<param index="0" name="path" type="String" />
|
||||
<param index="1" name="case_sensitive" type="bool" default="true" />
|
||||
<description>
|
||||
Loads the whole content of a file in the loaded zip archive into memory and returns it.
|
||||
Must be called after [method open].
|
||||
</description>
|
||||
</method>
|
||||
</methods>
|
||||
</class>
|
||||
51
engine/modules/zip/register_types.cpp
Normal file
51
engine/modules/zip/register_types.cpp
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/**************************************************************************/
|
||||
/* register_types.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "register_types.h"
|
||||
|
||||
#include "zip_packer.h"
|
||||
#include "zip_reader.h"
|
||||
|
||||
#include "core/object/class_db.h"
|
||||
|
||||
void initialize_zip_module(ModuleInitializationLevel p_level) {
|
||||
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||
return;
|
||||
}
|
||||
|
||||
GDREGISTER_CLASS(ZIPPacker);
|
||||
GDREGISTER_CLASS(ZIPReader);
|
||||
}
|
||||
|
||||
void uninitialize_zip_module(ModuleInitializationLevel p_level) {
|
||||
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
39
engine/modules/zip/register_types.h
Normal file
39
engine/modules/zip/register_types.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/**************************************************************************/
|
||||
/* register_types.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef ZIP_REGISTER_TYPES_H
|
||||
#define ZIP_REGISTER_TYPES_H
|
||||
|
||||
#include "modules/register_module_types.h"
|
||||
|
||||
void initialize_zip_module(ModuleInitializationLevel p_level);
|
||||
void uninitialize_zip_module(ModuleInitializationLevel p_level);
|
||||
|
||||
#endif // ZIP_REGISTER_TYPES_H
|
||||
126
engine/modules/zip/zip_packer.cpp
Normal file
126
engine/modules/zip/zip_packer.cpp
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/**************************************************************************/
|
||||
/* zip_packer.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "zip_packer.h"
|
||||
|
||||
#include "core/io/zip_io.h"
|
||||
#include "core/os/os.h"
|
||||
|
||||
Error ZIPPacker::open(const String &p_path, ZipAppend p_append) {
|
||||
if (fa.is_valid()) {
|
||||
close();
|
||||
}
|
||||
|
||||
zlib_filefunc_def io = zipio_create_io(&fa);
|
||||
zf = zipOpen2(p_path.utf8().get_data(), p_append, nullptr, &io);
|
||||
return zf != nullptr ? OK : FAILED;
|
||||
}
|
||||
|
||||
Error ZIPPacker::close() {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker cannot be closed because it is not open.");
|
||||
|
||||
Error err = zipClose(zf, nullptr) == ZIP_OK ? OK : FAILED;
|
||||
if (err == OK) {
|
||||
DEV_ASSERT(fa == nullptr);
|
||||
zf = nullptr;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
Error ZIPPacker::start_file(const String &p_path) {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
||||
|
||||
zip_fileinfo zipfi;
|
||||
|
||||
OS::DateTime time = OS::get_singleton()->get_datetime();
|
||||
|
||||
zipfi.tmz_date.tm_sec = time.second;
|
||||
zipfi.tmz_date.tm_min = time.minute;
|
||||
zipfi.tmz_date.tm_hour = time.hour;
|
||||
zipfi.tmz_date.tm_mday = time.day;
|
||||
zipfi.tmz_date.tm_mon = time.month - 1;
|
||||
zipfi.tmz_date.tm_year = time.year;
|
||||
zipfi.dosDate = 0;
|
||||
zipfi.internal_fa = 0;
|
||||
zipfi.external_fa = 0;
|
||||
|
||||
int err = zipOpenNewFileInZip4(zf,
|
||||
p_path.utf8().get_data(),
|
||||
&zipfi,
|
||||
nullptr,
|
||||
0,
|
||||
nullptr,
|
||||
0,
|
||||
nullptr,
|
||||
Z_DEFLATED,
|
||||
Z_DEFAULT_COMPRESSION,
|
||||
0,
|
||||
-MAX_WBITS,
|
||||
DEF_MEM_LEVEL,
|
||||
Z_DEFAULT_STRATEGY,
|
||||
nullptr,
|
||||
0,
|
||||
0, // "version made by", indicates the compatibility of the file attribute information (the `external_fa` field above).
|
||||
1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
|
||||
return err == ZIP_OK ? OK : FAILED;
|
||||
}
|
||||
|
||||
Error ZIPPacker::write_file(const Vector<uint8_t> &p_data) {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
||||
|
||||
return zipWriteInFileInZip(zf, p_data.ptr(), p_data.size()) == ZIP_OK ? OK : FAILED;
|
||||
}
|
||||
|
||||
Error ZIPPacker::close_file() {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
||||
|
||||
return zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
|
||||
}
|
||||
|
||||
void ZIPPacker::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("open", "path", "append"), &ZIPPacker::open, DEFVAL(Variant(APPEND_CREATE)));
|
||||
ClassDB::bind_method(D_METHOD("start_file", "path"), &ZIPPacker::start_file);
|
||||
ClassDB::bind_method(D_METHOD("write_file", "data"), &ZIPPacker::write_file);
|
||||
ClassDB::bind_method(D_METHOD("close_file"), &ZIPPacker::close_file);
|
||||
ClassDB::bind_method(D_METHOD("close"), &ZIPPacker::close);
|
||||
|
||||
BIND_ENUM_CONSTANT(APPEND_CREATE);
|
||||
BIND_ENUM_CONSTANT(APPEND_CREATEAFTER);
|
||||
BIND_ENUM_CONSTANT(APPEND_ADDINZIP);
|
||||
}
|
||||
|
||||
ZIPPacker::ZIPPacker() {}
|
||||
|
||||
ZIPPacker::~ZIPPacker() {
|
||||
if (fa.is_valid()) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
68
engine/modules/zip/zip_packer.h
Normal file
68
engine/modules/zip/zip_packer.h
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/**************************************************************************/
|
||||
/* zip_packer.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef ZIP_PACKER_H
|
||||
#define ZIP_PACKER_H
|
||||
|
||||
#include "core/io/file_access.h"
|
||||
#include "core/object/ref_counted.h"
|
||||
|
||||
#include "thirdparty/minizip/zip.h"
|
||||
|
||||
class ZIPPacker : public RefCounted {
|
||||
GDCLASS(ZIPPacker, RefCounted);
|
||||
|
||||
Ref<FileAccess> fa;
|
||||
zipFile zf = nullptr;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
enum ZipAppend {
|
||||
APPEND_CREATE = 0,
|
||||
APPEND_CREATEAFTER = 1,
|
||||
APPEND_ADDINZIP = 2,
|
||||
};
|
||||
|
||||
Error open(const String &p_path, ZipAppend p_append);
|
||||
Error close();
|
||||
|
||||
Error start_file(const String &p_path);
|
||||
Error write_file(const Vector<uint8_t> &p_data);
|
||||
Error close_file();
|
||||
|
||||
ZIPPacker();
|
||||
~ZIPPacker();
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST(ZIPPacker::ZipAppend)
|
||||
|
||||
#endif // ZIP_PACKER_H
|
||||
157
engine/modules/zip/zip_reader.cpp
Normal file
157
engine/modules/zip/zip_reader.cpp
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
/**************************************************************************/
|
||||
/* zip_reader.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "zip_reader.h"
|
||||
|
||||
#include "core/error/error_macros.h"
|
||||
#include "core/io/zip_io.h"
|
||||
|
||||
Error ZIPReader::open(const String &p_path) {
|
||||
if (fa.is_valid()) {
|
||||
close();
|
||||
}
|
||||
|
||||
zlib_filefunc_def io = zipio_create_io(&fa);
|
||||
uzf = unzOpen2(p_path.utf8().get_data(), &io);
|
||||
return uzf != nullptr ? OK : FAILED;
|
||||
}
|
||||
|
||||
Error ZIPReader::close() {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPReader cannot be closed because it is not open.");
|
||||
|
||||
Error err = unzClose(uzf) == UNZ_OK ? OK : FAILED;
|
||||
if (err == OK) {
|
||||
DEV_ASSERT(fa == nullptr);
|
||||
uzf = nullptr;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
PackedStringArray ZIPReader::get_files() {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), PackedStringArray(), "ZIPReader must be opened before use.");
|
||||
|
||||
unz_global_info gi;
|
||||
int err = unzGetGlobalInfo(uzf, &gi);
|
||||
ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray());
|
||||
if (gi.number_entry == 0) {
|
||||
return PackedStringArray();
|
||||
}
|
||||
|
||||
err = unzGoToFirstFile(uzf);
|
||||
ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray());
|
||||
|
||||
List<String> s;
|
||||
do {
|
||||
unz_file_info64 file_info;
|
||||
String filepath;
|
||||
|
||||
err = godot_unzip_get_current_file_info(uzf, file_info, filepath);
|
||||
if (err == UNZ_OK) {
|
||||
s.push_back(filepath);
|
||||
}
|
||||
} while (unzGoToNextFile(uzf) == UNZ_OK);
|
||||
|
||||
PackedStringArray arr;
|
||||
arr.resize(s.size());
|
||||
int idx = 0;
|
||||
for (const List<String>::Element *E = s.front(); E; E = E->next()) {
|
||||
arr.set(idx++, E->get());
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
PackedByteArray ZIPReader::read_file(const String &p_path, bool p_case_sensitive) {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "ZIPReader must be opened before use.");
|
||||
|
||||
int err = UNZ_OK;
|
||||
|
||||
// Locate and open the file.
|
||||
err = godot_unzip_locate_file(uzf, p_path, p_case_sensitive);
|
||||
ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "File does not exist in zip archive: " + p_path);
|
||||
err = unzOpenCurrentFile(uzf);
|
||||
ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Could not open file within zip archive.");
|
||||
|
||||
// Read the file info.
|
||||
unz_file_info info;
|
||||
err = unzGetCurrentFileInfo(uzf, &info, nullptr, 0, nullptr, 0, nullptr, 0);
|
||||
ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Unable to read file information from zip archive.");
|
||||
ERR_FAIL_COND_V_MSG(info.uncompressed_size > INT_MAX, PackedByteArray(), "File contents too large to read from zip archive (>2 GB).");
|
||||
|
||||
// Read the file data.
|
||||
PackedByteArray data;
|
||||
data.resize(info.uncompressed_size);
|
||||
uint8_t *buffer = data.ptrw();
|
||||
int to_read = data.size();
|
||||
while (to_read > 0) {
|
||||
int bytes_read = unzReadCurrentFile(uzf, buffer, to_read);
|
||||
ERR_FAIL_COND_V_MSG(bytes_read < 0, PackedByteArray(), "IO/zlib error reading file from zip archive.");
|
||||
ERR_FAIL_COND_V_MSG(bytes_read == UNZ_EOF && to_read != 0, PackedByteArray(), "Incomplete file read from zip archive.");
|
||||
DEV_ASSERT(bytes_read <= to_read);
|
||||
buffer += bytes_read;
|
||||
to_read -= bytes_read;
|
||||
}
|
||||
|
||||
// Verify the data and return.
|
||||
err = unzCloseCurrentFile(uzf);
|
||||
ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "CRC error reading file from zip archive.");
|
||||
return data;
|
||||
}
|
||||
|
||||
bool ZIPReader::file_exists(const String &p_path, bool p_case_sensitive) {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), false, "ZIPReader must be opened before use.");
|
||||
|
||||
int cs = p_case_sensitive ? 1 : 2;
|
||||
if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) {
|
||||
return false;
|
||||
}
|
||||
if (unzOpenCurrentFile(uzf) != UNZ_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
unzCloseCurrentFile(uzf);
|
||||
return true;
|
||||
}
|
||||
|
||||
ZIPReader::ZIPReader() {}
|
||||
|
||||
ZIPReader::~ZIPReader() {
|
||||
if (fa.is_valid()) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
void ZIPReader::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("open", "path"), &ZIPReader::open);
|
||||
ClassDB::bind_method(D_METHOD("close"), &ZIPReader::close);
|
||||
ClassDB::bind_method(D_METHOD("get_files"), &ZIPReader::get_files);
|
||||
ClassDB::bind_method(D_METHOD("read_file", "path", "case_sensitive"), &ZIPReader::read_file, DEFVAL(Variant(true)));
|
||||
ClassDB::bind_method(D_METHOD("file_exists", "path", "case_sensitive"), &ZIPReader::file_exists, DEFVAL(Variant(true)));
|
||||
}
|
||||
60
engine/modules/zip/zip_reader.h
Normal file
60
engine/modules/zip/zip_reader.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/**************************************************************************/
|
||||
/* zip_reader.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef ZIP_READER_H
|
||||
#define ZIP_READER_H
|
||||
|
||||
#include "core/io/file_access.h"
|
||||
#include "core/object/ref_counted.h"
|
||||
|
||||
#include "thirdparty/minizip/unzip.h"
|
||||
|
||||
class ZIPReader : public RefCounted {
|
||||
GDCLASS(ZIPReader, RefCounted)
|
||||
|
||||
Ref<FileAccess> fa;
|
||||
unzFile uzf = nullptr;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
Error open(const String &p_path);
|
||||
Error close();
|
||||
|
||||
PackedStringArray get_files();
|
||||
PackedByteArray read_file(const String &p_path, bool p_case_sensitive);
|
||||
bool file_exists(const String &p_path, bool p_case_sensitive);
|
||||
|
||||
ZIPReader();
|
||||
~ZIPReader();
|
||||
};
|
||||
|
||||
#endif // ZIP_READER_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue