diff options
author | A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> | 2023-08-16 14:01:58 +0200 |
---|---|---|
committer | A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> | 2023-08-16 15:10:32 +0200 |
commit | f7fa72c808fb5221213f45fef3a2977b9e74278a (patch) | |
tree | 1f6eb9ea60330586fe88166f5b54afcce8146e9b /modules | |
parent | 90f90cbcb0cf2c44a3114048accfd5b407c4ac98 (diff) | |
download | redot-engine-f7fa72c808fb5221213f45fef3a2977b9e74278a.tar.gz |
Add function `ZIPReader::file_exists`
Diffstat (limited to 'modules')
-rw-r--r-- | modules/zip/doc_classes/ZIPReader.xml | 9 | ||||
-rw-r--r-- | modules/zip/zip_reader.cpp | 16 | ||||
-rw-r--r-- | modules/zip/zip_reader.h | 1 |
3 files changed, 26 insertions, 0 deletions
diff --git a/modules/zip/doc_classes/ZIPReader.xml b/modules/zip/doc_classes/ZIPReader.xml index 0fa2672044..bf596806b2 100644 --- a/modules/zip/doc_classes/ZIPReader.xml +++ b/modules/zip/doc_classes/ZIPReader.xml @@ -25,6 +25,15 @@ 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> diff --git a/modules/zip/zip_reader.cpp b/modules/zip/zip_reader.cpp index 2684875e1c..5752b829ef 100644 --- a/modules/zip/zip_reader.cpp +++ b/modules/zip/zip_reader.cpp @@ -118,6 +118,21 @@ PackedByteArray ZIPReader::read_file(String p_path, bool p_case_sensitive) { return data; } +bool ZIPReader::file_exists(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() { @@ -131,4 +146,5 @@ void ZIPReader::_bind_methods() { 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))); } diff --git a/modules/zip/zip_reader.h b/modules/zip/zip_reader.h index c074197eb4..0f78352e3f 100644 --- a/modules/zip/zip_reader.h +++ b/modules/zip/zip_reader.h @@ -51,6 +51,7 @@ public: PackedStringArray get_files(); PackedByteArray read_file(String p_path, bool p_case_sensitive); + bool file_exists(String p_path, bool p_case_sensitive); ZIPReader(); ~ZIPReader(); |