diff --git a/header/gMod_FileLoader.h b/header/gMod_FileLoader.h index 34afcd4..38526be 100644 --- a/header/gMod_FileLoader.h +++ b/header/gMod_FileLoader.h @@ -5,21 +5,20 @@ #include #include #include -#include #include struct TpfEntry { - std::string Name; - std::string Entry; // Assuming ZipEntry is a string in your original code - uint32_t CrcHash; - void* Data; - unsigned long long Size; + std::string name; + std::string entry; // Assuming ZipEntry is a string in your original code + uint32_t crc_hash; + void* data; + unsigned long long size; }; class gMod_FileLoader { - std::string _fileName; - std::ifstream _stream; - const std::vector _tpfPassword{ + std::string file_name; + std::ifstream stream; + static constexpr std::vector TPF_PASSWORD{ 0x73, 0x2A, 0x63, 0x7D, 0x5F, 0x0A, 0xA6, 0xBD, 0x7D, 0x65, 0x7E, 0x67, 0x61, 0x2A, 0x7F, 0x7F, 0x74, 0x61, 0x67, 0x5B, 0x60, 0x70, 0x45, 0x74, @@ -27,9 +26,9 @@ class gMod_FileLoader { 0x77, 0x6E, 0x46, 0x47, 0x77, 0x49, 0x0C, 0x4B, 0x46, 0x6F }; - const std::string encodedPassword = std::string(_tpfPassword.begin(), _tpfPassword.end()); - std::vector entryCache; - bool loaded; + static constexpr std::string ENCODED_PASSWORD = std::string(TPF_PASSWORD.begin(), TPF_PASSWORD.end()); + std::vector entry_cache{}; + bool loaded = false; public: gMod_FileLoader(const std::string& fileName); diff --git a/header/gMod_XorStream.h b/header/gMod_XorStream.h index 7f09987..03600ec 100644 --- a/header/gMod_XorStream.h +++ b/header/gMod_XorStream.h @@ -3,9 +3,9 @@ #include #include -class XORStream : public std::streambuf { +class gMod_XorStream : public std::streambuf { public: - XORStream(std::istream& stream) : innerStream(stream) { + gMod_XorStream(std::istream& stream) : innerStream(stream) { if (!innerStream.seekg(0, std::ios::end).good() || !innerStream.seekg(0, std::ios::beg).good()) { throw std::invalid_argument("Provided stream needs to have SEEK set to True"); } @@ -22,7 +22,7 @@ public: innerStream.seekg(-2, std::ios::cur); } - auto lastZeroPosition = innerStream.tellg() - 1; + const auto lastZeroPosition = innerStream.tellg() - std::fpos(1); innerStream.seekg(0); length = lastZeroPosition; setg(nullptr, nullptr, nullptr); @@ -37,7 +37,7 @@ public: buffer[i] = XOR(buffer[i], i + startPosition); } - auto bytesOverLength = startPosition + bytesRead - length; + auto bytesOverLength = startPosition + bytesRead - std::fpos(length); for (int i = 0; i < bytesOverLength; i++) { if (bytesRead - i - 1 >= 0) { buffer[bytesRead - i - 1] = 0; diff --git a/source/gMod_FileLoader.cpp b/source/gMod_FileLoader.cpp index 5e2dc4f..f94d843 100644 --- a/source/gMod_FileLoader.cpp +++ b/source/gMod_FileLoader.cpp @@ -1,71 +1,73 @@ -#include "../header/gMod_FileLoader.h" #include +#include "gMod_FileLoader.h" +#include "gMod_XorStream.h" -gMod_FileLoader::gMod_FileLoader(const std::string& fileName) { - _fileName = std::filesystem::absolute(fileName).string(); - _stream = std::ifstream(_fileName, std::ios::binary); - loaded = false; - if (!_stream) { - throw std::runtime_error("Failed to open file: " + _fileName); +gMod_FileLoader::gMod_FileLoader(const std::string& fileName) +{ + file_name = std::filesystem::absolute(fileName).string(); + stream = std::ifstream(file_name, std::ios::binary); + if (!stream) { + throw std::runtime_error("Failed to open file: " + file_name); } } -std::vector gMod_FileLoader::Load() { +std::vector gMod_FileLoader::Load() +{ if (!loaded) { - entryCache = GetContents(); + entry_cache = GetContents(); loaded = true; } - return entryCache; + return entry_cache; } -std::vector gMod_FileLoader::GetContents() { - if (_fileName.ends_with(".tpf")) { - return GetTpfContents(); - } - else { - return GetFileContents(); - } +std::vector gMod_FileLoader::GetContents() +{ + return file_name.ends_with(".tpf") ? GetTpfContents() : GetFileContents(); } -std::vector gMod_FileLoader::GetTpfContents() { +std::vector gMod_FileLoader::GetTpfContents() +{ std::vector entries; try { - // Open the zip file using libzippp - libzippp::ZipArchive zipArchive(_fileName, encodedPassword); - zipArchive.open(); //ReadOnly and no consistency checks - LoadEntries(zipArchive, entries); - } - catch (const std::exception& e) { - throw std::runtime_error("Failed to open zip file: " + _fileName); - } - - return entries; -} - -std::vector gMod_FileLoader::GetFileContents() { - std::vector entries; - - try { - // Open the zip file using libzippp - libzippp::ZipArchive zipArchive(_fileName); + auto istream = std::ifstream(file_name, std::ios::binary); + const auto xorstream = gMod_XorStream(istream); + // todo from xorstream + libzippp::ZipArchive zipArchive(file_name, ENCODED_PASSWORD); zipArchive.open(); LoadEntries(zipArchive, entries); } catch (const std::exception& e) { - throw std::runtime_error("Failed to open zip file: " + _fileName); + throw std::runtime_error("Failed to open zip file: " + file_name + "\n" + e.what()); } return entries; } -void gMod_FileLoader::LoadEntries(libzippp::ZipArchive& archive, std::vector& entries) { +std::vector gMod_FileLoader::GetFileContents() +{ + std::vector entries; + + try { + libzippp::ZipArchive zipArchive(file_name); + zipArchive.open(); + LoadEntries(zipArchive, entries); + } + catch (const std::exception& e) { + throw std::runtime_error("Failed to open zip file: " + file_name); + } + + return entries; +} + +void gMod_FileLoader::LoadEntries(libzippp::ZipArchive& archive, std::vector& entries) +{ // Iterate over the files in the zip archive for (const auto& entry : archive.getEntries()) { if (entry.isFile()) { const auto dataPtr = entry.readAsBinary(); const auto size = entry.getSize(); - entries.push_back({ entry.getName(), entry.getName(), 0, dataPtr, size }); // Assuming ZipEntry is a string + entries.push_back({entry.getName(), entry.getName(), 0, dataPtr, size}); // Assuming ZipEntry is a string } } }