diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ba9a35..5946e9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/framework/textureid.cpp b/src/framework/textureid.cpp index d3ba79a..499dabf 100644 --- a/src/framework/textureid.cpp +++ b/src/framework/textureid.cpp @@ -1,5 +1,4 @@ #include "textureid.h" -FFileSystem fileSystem; FTextureManager TexMan; diff --git a/src/framework/textureid.h b/src/framework/textureid.h index d3c9cd1..585bb40 100644 --- a/src/framework/textureid.h +++ b/src/framework/textureid.h @@ -3,6 +3,7 @@ #include "framework/tarray.h" #include "framework/templates.h" #include "framework/zstring.h" +#include "framework/filesystem.h" #include #include @@ -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: diff --git a/src/framework/zipreader.cpp b/src/framework/zipreader.cpp index 626a2cf..4b44d44 100644 --- a/src/framework/zipreader.cpp +++ b/src/framework/zipreader.cpp @@ -24,6 +24,19 @@ 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; @@ -31,26 +44,34 @@ class ZipReaderImpl : public ZipReader 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 read_all_bytes(const std::string& filename) override + std::vector 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) @@ -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) @@ -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 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(pOpaque); diff --git a/src/framework/zipreader.h b/src/framework/zipreader.h index fc4fd91..071a826 100644 --- a/src/framework/zipreader.h +++ b/src/framework/zipreader.h @@ -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 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 read_all_bytes(const std::string& filename) = 0;