Skip to content

Commit

Permalink
Create a very basic filesystem implementation for zip, folder and wad
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Mar 1, 2024
1 parent 8e17874 commit 973512b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ set(ZDRAY_SOURCES
src/framework/file.h
src/framework/utf16.cpp
src/framework/utf16.h
src/framework/filesystem.cpp
src/framework/filesystem.h
src/blockmapbuilder/blockmapbuilder.cpp
src/blockmapbuilder/blockmapbuilder.h
src/level/level.cpp
Expand Down
1 change: 0 additions & 1 deletion src/framework/textureid.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

#include "textureid.h"

FFileSystem fileSystem;
FTextureManager TexMan;
19 changes: 1 addition & 18 deletions src/framework/textureid.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "framework/tarray.h"
#include "framework/templates.h"
#include "framework/zstring.h"
#include "framework/filesystem.h"
#include <map>
#include <memory>

Expand Down Expand Up @@ -71,24 +72,6 @@ class FSetTextureID : public FTextureID
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
{
public:
Expand Down
60 changes: 49 additions & 11 deletions src/framework/zipreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,54 @@ class ZipReaderImpl : public ZipReader
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
{
mz_uint32 fileIndex;
mz_bool result = mz_zip_reader_locate_file_v2(&zip, filename.c_str(), nullptr, 0, &fileIndex);
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
{
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 get_crc32(file_index);
}

uint32_t get_crc32(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 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_bool result = mz_zip_reader_file_stat(&zip, file_index, &stat);
if (result == MZ_FALSE)
Expand All @@ -66,12 +87,8 @@ class ZipReaderImpl : public ZipReader
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_bool result = mz_zip_reader_file_stat(&zip, file_index, &stat);
if (result == MZ_FALSE)
Expand All @@ -88,6 +105,27 @@ class ZipReaderImpl : public ZipReader
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)
{
ZipReaderImpl* impl = static_cast<ZipReaderImpl*>(pOpaque);
Expand Down
8 changes: 8 additions & 0 deletions src/framework/zipreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class ZipReader

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 uint32_t get_crc32(const std::string& filename) = 0;
virtual std::vector<uint8_t> read_all_bytes(const std::string& filename) = 0;
Expand Down

0 comments on commit 973512b

Please sign in to comment.