-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
AssetReader.cpp
195 lines (168 loc) · 5.3 KB
/
AssetReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <algorithm>
#include <ctype.h>
#include <set>
#include <stdio.h>
#ifdef __ANDROID__
#include <zip.h>
#endif
#include "Common/Common.h"
#include "Common/Log.h"
#include "Common/File/VFS/AssetReader.h"
#ifdef __ANDROID__
uint8_t *ReadFromZip(zip *archive, const char* filename, size_t *size) {
// Figure out the file size first.
struct zip_stat zstat;
zip_file *file = zip_fopen(archive, filename, ZIP_FL_NOCASE|ZIP_FL_UNCHANGED);
if (!file) {
ERROR_LOG(IO, "Error opening %s from ZIP", filename);
return 0;
}
zip_stat(archive, filename, ZIP_FL_NOCASE|ZIP_FL_UNCHANGED, &zstat);
uint8_t *contents = new uint8_t[zstat.size + 1];
zip_fread(file, contents, zstat.size);
zip_fclose(file);
contents[zstat.size] = 0;
*size = zstat.size;
return contents;
}
#endif
#ifdef __ANDROID__
ZipAssetReader::ZipAssetReader(const char *zip_file, const char *in_zip_path) {
zip_file_ = zip_open(zip_file, 0, NULL);
strcpy(in_zip_path_, in_zip_path);
if (!zip_file_) {
ERROR_LOG(IO, "Failed to open %s as a zip file", zip_file);
}
std::vector<File::FileInfo> info;
GetFileListing("assets", &info, 0);
for (size_t i = 0; i < info.size(); i++) {
if (info[i].isDirectory) {
DEBUG_LOG(IO, "Directory: %s", info[i].name.c_str());
} else {
DEBUG_LOG(IO, "File: %s", info[i].name.c_str());
}
}
}
ZipAssetReader::~ZipAssetReader() {
zip_close(zip_file_);
}
uint8_t *ZipAssetReader::ReadAsset(const char *path, size_t *size) {
char temp_path[1024];
strcpy(temp_path, in_zip_path_);
strcat(temp_path, path);
return ReadFromZip(zip_file_, temp_path, size);
}
bool ZipAssetReader::GetFileListing(const char *orig_path, std::vector<File::FileInfo> *listing, const char *filter = 0) {
char path[1024];
strcpy(path, in_zip_path_);
strcat(path, orig_path);
std::set<std::string> filters;
std::string tmp;
if (filter) {
while (*filter) {
if (*filter == ':') {
filters.insert("." + tmp);
tmp.clear();
} else {
tmp.push_back(*filter);
}
filter++;
}
}
if (tmp.size())
filters.insert("." + tmp);
// We just loop through the whole ZIP file and deduce what files are in this directory, and what subdirectories there are.
std::set<std::string> files;
std::set<std::string> directories;
int numFiles = zip_get_num_files(zip_file_);
size_t pathlen = strlen(path);
if (path[pathlen-1] == '/')
pathlen--;
for (int i = 0; i < numFiles; i++) {
const char* name = zip_get_name(zip_file_, i, 0);
if (!name)
continue;
if (!memcmp(name, path, pathlen)) {
// The prefix is right. Let's see if this is a file or path.
const char *slashPos = strchr(name + pathlen + 1, '/');
if (slashPos != 0) {
// A directory.
std::string dirName = std::string(name + pathlen + 1, slashPos - (name + pathlen + 1));
directories.insert(dirName);
} else if (name[pathlen] == '/') {
const char *fn = name + pathlen + 1;
files.insert(std::string(fn));
} // else, it was a file with the same prefix as the path. like langregion.ini next to lang/.
}
}
for (auto diter = directories.begin(); diter != directories.end(); ++diter) {
File::FileInfo info;
info.name = *diter;
// Remove the "inzip" part of the fullname.
info.fullName = Path(std::string(path).substr(strlen(in_zip_path_))) / *diter;
info.exists = true;
info.isWritable = false;
info.isDirectory = true;
listing->push_back(info);
}
for (auto fiter = files.begin(); fiter != files.end(); ++fiter) {
std::string fpath = path;
File::FileInfo info;
info.name = *fiter;
info.fullName = Path(std::string(path).substr(strlen(in_zip_path_))) / *fiter;
info.exists = true;
info.isWritable = false;
info.isDirectory = false;
std::string ext = info.fullName.GetFileExtension();
if (filter) {
if (filters.find(ext) == filters.end()) {
continue;
}
}
listing->push_back(info);
}
std::sort(listing->begin(), listing->end());
return true;
}
bool ZipAssetReader::GetFileInfo(const char *path, File::FileInfo *info) {
struct zip_stat zstat;
char temp_path[1024];
strcpy(temp_path, in_zip_path_);
strcat(temp_path, path);
if (0 != zip_stat(zip_file_, temp_path, ZIP_FL_NOCASE|ZIP_FL_UNCHANGED, &zstat)) {
// ZIP files do not have real directories, so we'll end up here if we
// try to stat one. For now that's fine.
info->exists = false;
info->size = 0;
return false;
}
info->fullName = Path(path);
info->exists = true; // TODO
info->isWritable = false;
info->isDirectory = false; // TODO
info->size = zstat.size;
return true;
}
#endif
DirectoryAssetReader::DirectoryAssetReader(const Path &path) {
path_ = path;
}
uint8_t *DirectoryAssetReader::ReadAsset(const char *path, size_t *size) {
Path new_path = Path(path).StartsWith(path_) ? Path(path) : path_ / path;
return File::ReadLocalFile(new_path, size);
}
bool DirectoryAssetReader::GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) {
Path new_path = Path(path).StartsWith(path_) ? Path(path) : path_ / path;
File::FileInfo info;
if (!File::GetFileInfo(new_path, &info))
return false;
if (info.isDirectory) {
File::GetFilesInDir(new_path, listing, filter);
return true;
}
return false;
}
bool DirectoryAssetReader::GetFileInfo(const char *path, File::FileInfo *info) {
Path new_path = Path(path).StartsWith(path_) ? Path(path) : path_ / path;
return File::GetFileInfo(new_path, info);
}