Correct error handling for missing ini (only accept it in directories, not zip)

This commit is contained in:
Henrik Rydgård 2023-03-08 09:29:19 +01:00
parent 74cad6b521
commit 306fdde9d0
3 changed files with 29 additions and 20 deletions

View file

@ -15,25 +15,6 @@
#include "Common/File/VFS/ZipFileReader.h"
#include "Common/StringUtils.h"
static 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;
}
ZipFileReader *ZipFileReader::Create(const Path &zipFile, const char *inZipPath) {
int error = 0;
zip *zip_file;
@ -69,7 +50,21 @@ uint8_t *ZipFileReader::ReadFile(const char *path, size_t *size) {
snprintf(temp_path, sizeof(temp_path), "%s%s", inZipPath_, path);
std::lock_guard<std::mutex> guard(lock_);
return ReadFromZip(zip_file_, temp_path, size);
// Figure out the file size first.
struct zip_stat zstat;
zip_stat(zip_file_, temp_path, ZIP_FL_NOCASE | ZIP_FL_UNCHANGED, &zstat);
zip_file *file = zip_fopen(zip_file_, temp_path, ZIP_FL_NOCASE | ZIP_FL_UNCHANGED);
if (!file) {
ERROR_LOG(IO, "Error opening %s from ZIP", temp_path);
return 0;
}
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;
}
bool ZipFileReader::GetFileListing(const char *orig_path, std::vector<File::FileInfo> *listing, const char *filter = 0) {

View file

@ -116,7 +116,10 @@ bool TextureReplacer::LoadIni() {
// First, check for textures.zip, which is used to reduce IO.
VFSBackend *dir = ZipFileReader::Create(basePath_ / ZIP_FILENAME, "");
if (!dir) {
vfsIsZip_ = false;
dir = new DirectoryReader(basePath_);
} else {
vfsIsZip_ = true;
}
IniFile ini;
@ -149,9 +152,19 @@ bool TextureReplacer::LoadIni() {
}
}
}
} else {
if (vfsIsZip_) {
// We don't accept zip files without inis.
ERROR_LOG(G3D, "Texture pack lacking ini file: %s", basePath_.c_str());
delete dir;
return false;
} else {
WARN_LOG(G3D, "Texture pack lacking ini file: %s", basePath_.c_str());
}
}
vfs_ = dir;
INFO_LOG(G3D, "Texture pack activated from '%s'", basePath_.c_str());
// The ini doesn't have to exist for the texture directory or zip to be valid.
return true;

View file

@ -293,6 +293,7 @@ protected:
ReplacedTextureHash hash_ = ReplacedTextureHash::QUICK;
VFSBackend *vfs_ = nullptr;
bool vfsIsZip_ = false;
typedef std::pair<int, int> WidthHeightPair;
std::unordered_map<u64, WidthHeightPair> hashranges_;