1 | /**************************************************************************/ |
2 | /* file_access_zip.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef FILE_ACCESS_ZIP_H |
32 | #define FILE_ACCESS_ZIP_H |
33 | |
34 | #ifdef MINIZIP_ENABLED |
35 | |
36 | #include "core/io/file_access_pack.h" |
37 | #include "core/templates/rb_map.h" |
38 | |
39 | #include "thirdparty/minizip/unzip.h" |
40 | |
41 | #include <stdlib.h> |
42 | |
43 | class ZipArchive : public PackSource { |
44 | public: |
45 | struct File { |
46 | int package = -1; |
47 | unz_file_pos file_pos; |
48 | File() {} |
49 | }; |
50 | |
51 | private: |
52 | struct Package { |
53 | String filename; |
54 | unzFile zfile = nullptr; |
55 | }; |
56 | Vector<Package> packages; |
57 | |
58 | HashMap<String, File> files; |
59 | |
60 | static ZipArchive *instance; |
61 | |
62 | public: |
63 | void close_handle(unzFile p_file) const; |
64 | unzFile get_file_handle(String p_file) const; |
65 | |
66 | Error add_package(String p_name); |
67 | |
68 | bool file_exists(String p_name) const; |
69 | |
70 | virtual bool try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) override; |
71 | Ref<FileAccess> get_file(const String &p_path, PackedData::PackedFile *p_file) override; |
72 | |
73 | static ZipArchive *get_singleton(); |
74 | |
75 | ZipArchive(); |
76 | ~ZipArchive(); |
77 | }; |
78 | |
79 | class FileAccessZip : public FileAccess { |
80 | unzFile zfile = nullptr; |
81 | unz_file_info64 file_info; |
82 | |
83 | mutable bool at_eof = false; |
84 | |
85 | void _close(); |
86 | |
87 | public: |
88 | virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file |
89 | virtual bool is_open() const override; ///< true when file is open |
90 | |
91 | virtual void seek(uint64_t p_position) override; ///< seek to a given position |
92 | virtual void seek_end(int64_t p_position = 0) override; ///< seek from the end of file |
93 | virtual uint64_t get_position() const override; ///< get position in the file |
94 | virtual uint64_t get_length() const override; ///< get size of the file |
95 | |
96 | virtual bool eof_reached() const override; ///< reading passed EOF |
97 | |
98 | virtual uint8_t get_8() const override; ///< get a byte |
99 | virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override; |
100 | |
101 | virtual Error get_error() const override; ///< get last error |
102 | |
103 | virtual void flush() override; |
104 | virtual void store_8(uint8_t p_dest) override; ///< store a byte |
105 | |
106 | virtual bool file_exists(const String &p_name) override; ///< return true if a file exists |
107 | |
108 | virtual uint64_t _get_modified_time(const String &p_file) override { return 0; } // todo |
109 | virtual BitField<FileAccess::UnixPermissionFlags> _get_unix_permissions(const String &p_file) override { return 0; } |
110 | virtual Error _set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) override { return FAILED; } |
111 | |
112 | virtual bool _get_hidden_attribute(const String &p_file) override { return false; } |
113 | virtual Error _set_hidden_attribute(const String &p_file, bool p_hidden) override { return ERR_UNAVAILABLE; } |
114 | virtual bool _get_read_only_attribute(const String &p_file) override { return false; } |
115 | virtual Error _set_read_only_attribute(const String &p_file, bool p_ro) override { return ERR_UNAVAILABLE; } |
116 | |
117 | virtual void close() override; |
118 | |
119 | FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file); |
120 | ~FileAccessZip(); |
121 | }; |
122 | |
123 | #endif // MINIZIP_ENABLED |
124 | |
125 | #endif // FILE_ACCESS_ZIP_H |
126 | |