Create a very basic filesystem implementation for zip, folder and wad

This commit is contained in:
Magnus Norddahl 2024-03-01 13:28:07 +01:00
parent 8e1787450b
commit 973512ba06
5 changed files with 60 additions and 30 deletions

View file

@ -36,6 +36,8 @@ set(ZDRAY_SOURCES
src/framework/file.h src/framework/file.h
src/framework/utf16.cpp src/framework/utf16.cpp
src/framework/utf16.h src/framework/utf16.h
src/framework/filesystem.cpp
src/framework/filesystem.h
src/blockmapbuilder/blockmapbuilder.cpp src/blockmapbuilder/blockmapbuilder.cpp
src/blockmapbuilder/blockmapbuilder.h src/blockmapbuilder/blockmapbuilder.h
src/level/level.cpp src/level/level.cpp

View file

@ -1,5 +1,4 @@
#include "textureid.h" #include "textureid.h"
FFileSystem fileSystem;
FTextureManager TexMan; FTextureManager TexMan;

View file

@ -3,6 +3,7 @@
#include "framework/tarray.h" #include "framework/tarray.h"
#include "framework/templates.h" #include "framework/templates.h"
#include "framework/zstring.h" #include "framework/zstring.h"
#include "framework/filesystem.h"
#include <map> #include <map>
#include <memory> #include <memory>
@ -71,24 +72,6 @@ public:
constexpr FSetTextureID(int v) : FTextureID(v) {} constexpr FSetTextureID(int v) : FTextureID(v) {}
}; };
struct FileData
{
char* GetMem() { return nullptr; }
};
class FFileSystem
{
public:
int CheckNumForFullName(const FString& fullname) { return -1; }
int FileLength(int lump) { return 0; }
FileData ReadFile(int lump) { return {}; }
const char* GetFileFullName(int lump, bool returnshort = true) const { return ""; }
};
extern FFileSystem fileSystem;
class FGameTexture class FGameTexture
{ {
public: public:

View file

@ -24,6 +24,19 @@ public:
mz_zip_reader_end(&zip); mz_zip_reader_end(&zip);
} }
int get_num_files() override
{
return mz_zip_reader_get_num_files(&zip);
}
std::string get_filename(int file_index) override
{
std::string filename;
filename.resize((size_t)mz_zip_reader_get_filename(&zip, (mz_uint)file_index, nullptr, 0));
mz_zip_reader_get_filename(&zip, file_index, filename.data(), (mz_uint)filename.size());
return filename;
}
bool file_exists(const std::string& filename) override bool file_exists(const std::string& filename) override
{ {
mz_uint32 fileIndex; mz_uint32 fileIndex;
@ -31,26 +44,34 @@ public:
return result; return result;
} }
uint64_t get_uncompressed_size(int file_index) override
{
mz_zip_archive_file_stat stat;
mz_bool result = mz_zip_reader_file_stat(&zip, file_index, &stat);
if (result == MZ_FALSE)
throw std::runtime_error("mz_zip_reader_file_stat failed");
return (uint64_t)stat.m_uncomp_size;
}
uint32_t get_crc32(const std::string& filename) override uint32_t get_crc32(const std::string& filename) override
{ {
int file_index = mz_zip_reader_locate_file(&zip, filename.c_str(), nullptr, 0); int file_index = mz_zip_reader_locate_file(&zip, filename.c_str(), nullptr, 0);
if (file_index == -1) if (file_index == -1)
throw std::runtime_error("File " + filename + " not found in archive"); throw std::runtime_error("File " + filename + " not found in archive");
return get_crc32(file_index);
}
uint32_t get_crc32(int file_index) override
{
mz_zip_archive_file_stat stat; mz_zip_archive_file_stat stat;
mz_bool result = mz_zip_reader_file_stat(&zip, file_index, &stat); mz_bool result = mz_zip_reader_file_stat(&zip, file_index, &stat);
if (result == MZ_FALSE) if (result == MZ_FALSE)
throw std::runtime_error("mz_zip_reader_file_stat failed"); throw std::runtime_error("mz_zip_reader_file_stat failed");
return stat.m_crc32; return stat.m_crc32;
} }
std::vector<uint8_t> read_all_bytes(const std::string& filename) override std::vector<uint8_t> read_all_bytes(int file_index) override
{ {
int file_index = mz_zip_reader_locate_file(&zip, filename.c_str(), nullptr, 0);
if (file_index == -1)
throw std::runtime_error("File " + filename + " not found in archive");
mz_zip_archive_file_stat stat; mz_zip_archive_file_stat stat;
mz_bool result = mz_zip_reader_file_stat(&zip, file_index, &stat); mz_bool result = mz_zip_reader_file_stat(&zip, file_index, &stat);
if (result == MZ_FALSE) if (result == MZ_FALSE)
@ -66,12 +87,8 @@ public:
return buffer; return buffer;
} }
std::string read_all_text(const std::string& filename) override std::string read_all_text(int file_index) override
{ {
int file_index = mz_zip_reader_locate_file(&zip, filename.c_str(), nullptr, 0);
if (file_index == -1)
throw std::runtime_error("File " + filename + " not found in archive");
mz_zip_archive_file_stat stat; mz_zip_archive_file_stat stat;
mz_bool result = mz_zip_reader_file_stat(&zip, file_index, &stat); mz_bool result = mz_zip_reader_file_stat(&zip, file_index, &stat);
if (result == MZ_FALSE) if (result == MZ_FALSE)
@ -88,6 +105,27 @@ public:
return buffer; return buffer;
} }
int locate_file(const std::string& filename) override
{
return mz_zip_reader_locate_file(&zip, filename.c_str(), nullptr, 0);
}
std::vector<uint8_t> read_all_bytes(const std::string& filename) override
{
int file_index = mz_zip_reader_locate_file(&zip, filename.c_str(), nullptr, 0);
if (file_index == -1)
throw std::runtime_error("File " + filename + " not found in archive");
return read_all_bytes(file_index);
}
std::string read_all_text(const std::string& filename) override
{
int file_index = mz_zip_reader_locate_file(&zip, filename.c_str(), nullptr, 0);
if (file_index == -1)
throw std::runtime_error("File " + filename + " not found in archive");
return read_all_text(file_index);
}
static size_t read(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n) static size_t read(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n)
{ {
ZipReaderImpl* impl = static_cast<ZipReaderImpl*>(pOpaque); ZipReaderImpl* impl = static_cast<ZipReaderImpl*>(pOpaque);

View file

@ -12,6 +12,14 @@ public:
virtual ~ZipReader() = default; virtual ~ZipReader() = default;
virtual int get_num_files() = 0;
virtual std::string get_filename(int file_index) = 0;
virtual uint64_t get_uncompressed_size(int file_index) = 0;
virtual uint32_t get_crc32(int file_index) = 0;
virtual std::vector<uint8_t> read_all_bytes(int file_index) = 0;
virtual std::string read_all_text(int file_index) = 0;
virtual int locate_file(const std::string& filename) = 0;
virtual bool file_exists(const std::string& filename) = 0; virtual bool file_exists(const std::string& filename) = 0;
virtual uint32_t get_crc32(const std::string& filename) = 0; virtual uint32_t get_crc32(const std::string& filename) = 0;
virtual std::vector<uint8_t> read_all_bytes(const std::string& filename) = 0; virtual std::vector<uint8_t> read_all_bytes(const std::string& filename) = 0;