2012-03-24 22:39:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <zip.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "file/zip_read.h"
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
uint8_t *ReadFromZip(zip *archive, const char* filename, size_t *size) {
|
2012-10-31 12:23:16 +00:00
|
|
|
// Figure out the file size first.
|
|
|
|
struct zip_stat zstat;
|
|
|
|
zip_stat(archive, filename, ZIP_FL_NOCASE, &zstat);
|
|
|
|
|
|
|
|
uint8_t *contents = new uint8_t[zstat.size + 1];
|
|
|
|
|
|
|
|
zip_file *file = zip_fopen(archive, filename, 0);
|
|
|
|
if (!file) {
|
|
|
|
ELOG("Error opening %s from ZIP", filename);
|
|
|
|
delete [] contents;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
zip_fread(file, contents, zstat.size);
|
|
|
|
zip_fclose(file);
|
|
|
|
contents[zstat.size] = 0;
|
|
|
|
|
|
|
|
*size = zstat.size;
|
|
|
|
return contents;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// The return is non-const because - why not?
|
|
|
|
uint8_t *ReadLocalFile(const char *filename, size_t *size) {
|
2012-10-31 12:23:16 +00:00
|
|
|
FILE *file = fopen(filename, "rb");
|
|
|
|
if (!file) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
size_t f_size = ftell(file);
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
uint8_t *contents = new uint8_t[f_size+1];
|
|
|
|
fread(contents, 1, f_size, file);
|
|
|
|
fclose(file);
|
|
|
|
contents[f_size] = 0;
|
|
|
|
*size = f_size;
|
|
|
|
return contents;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
ZipAssetReader::ZipAssetReader(const char *zip_file, const char *in_zip_path) {
|
2012-10-31 12:23:16 +00:00
|
|
|
zip_file_ = zip_open(zip_file, 0, NULL);
|
|
|
|
strcpy(in_zip_path_, in_zip_path);
|
|
|
|
if (!zip_file_) {
|
|
|
|
ELOG("Failed to open %s as a zip file", zip_file);
|
|
|
|
}
|
|
|
|
// This is not really necessary.
|
|
|
|
int numFiles = zip_get_num_files(zip_file_);
|
|
|
|
for (int i = 0; i < numFiles; i++) {
|
|
|
|
const char* name = zip_get_name(zip_file_, i, 0);
|
|
|
|
if (name == NULL) {
|
|
|
|
ELOG("Error reading zip file name at index %i : %s", i, zip_strerror(zip_file_));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// ILOG("File %i : %s\n", i, name);
|
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ZipAssetReader::~ZipAssetReader() {
|
2012-10-31 12:23:16 +00:00
|
|
|
zip_close(zip_file_);
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *ZipAssetReader::ReadAsset(const char *path, size_t *size) {
|
2012-10-31 12:23:16 +00:00
|
|
|
char temp_path[256];
|
|
|
|
strcpy(temp_path, in_zip_path_);
|
|
|
|
strcat(temp_path, path);
|
|
|
|
return ReadFromZip(zip_file_, temp_path, size);
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint8_t *DirectoryAssetReader::ReadAsset(const char *path, size_t *size) {
|
2012-10-31 12:23:16 +00:00
|
|
|
char new_path[256] = {0};
|
|
|
|
// Check if it already contains the path
|
|
|
|
if (strlen(path) > strlen(path_) && 0 == memcmp(path, path_, strlen(path_))) {
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcpy(new_path, path_);
|
|
|
|
}
|
|
|
|
strcat(new_path, path);
|
|
|
|
// ILOG("New path: %s", new_path);
|
|
|
|
return ReadLocalFile(new_path, size);
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct VFSEntry {
|
2012-10-31 12:23:16 +00:00
|
|
|
const char *prefix;
|
|
|
|
AssetReader *reader;
|
2012-03-24 22:39:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static VFSEntry entries[16];
|
|
|
|
static int num_entries = 0;
|
|
|
|
|
|
|
|
void VFSRegister(const char *prefix, AssetReader *reader) {
|
2012-10-31 12:23:16 +00:00
|
|
|
entries[num_entries].prefix = prefix;
|
|
|
|
entries[num_entries].reader = reader;
|
|
|
|
ILOG("Registered VFS for prefix %s: %s", prefix, reader->toString().c_str());
|
|
|
|
num_entries++;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VFSShutdown() {
|
2012-10-31 12:23:16 +00:00
|
|
|
for (int i = 0; i < num_entries; i++) {
|
|
|
|
delete entries[i].reader;
|
|
|
|
}
|
|
|
|
num_entries = 0;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *VFSReadFile(const char *filename, size_t *size) {
|
2012-10-31 12:23:16 +00:00
|
|
|
int fn_len = strlen(filename);
|
|
|
|
for (int i = 0; i < num_entries; i++) {
|
|
|
|
int prefix_len = strlen(entries[i].prefix);
|
|
|
|
if (prefix_len >= fn_len) continue;
|
|
|
|
if (0 == memcmp(filename, entries[i].prefix, prefix_len)) {
|
|
|
|
// ILOG("Prefix match: %s (%s) -> %s", entries[i].prefix, filename, filename + prefix_len);
|
|
|
|
uint8_t *data = entries[i].reader->ReadAsset(filename + prefix_len, size);
|
|
|
|
if (data)
|
|
|
|
return data;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
// Else try the other registered file systems.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ELOG("Missing filesystem for %s", filename);
|
|
|
|
return 0;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|