some code changes

This commit is contained in:
DubbleClick
2023-11-15 13:52:46 +01:00
parent fa8f6f0896
commit 74417a89e8
3 changed files with 56 additions and 55 deletions
+11 -12
View File
@@ -5,21 +5,20 @@
#include <vector>
#include <string>
#include <ranges>
#include <streambuf>
#include <libzippp.h>
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<uint8_t> _tpfPassword{
std::string file_name;
std::ifstream stream;
static constexpr std::vector<uint8_t> 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<TpfEntry> entryCache;
bool loaded;
static constexpr std::string ENCODED_PASSWORD = std::string(TPF_PASSWORD.begin(), TPF_PASSWORD.end());
std::vector<TpfEntry> entry_cache{};
bool loaded = false;
public:
gMod_FileLoader(const std::string& fileName);
+4 -4
View File
@@ -3,9 +3,9 @@
#include <iostream>
#include <fstream>
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<int>(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<int>(length);
for (int i = 0; i < bytesOverLength; i++) {
if (bytesRead - i - 1 >= 0) {
buffer[bytesRead - i - 1] = 0;
+41 -39
View File
@@ -1,71 +1,73 @@
#include "../header/gMod_FileLoader.h"
#include <filesystem>
#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<TpfEntry> gMod_FileLoader::Load() {
std::vector<TpfEntry> gMod_FileLoader::Load()
{
if (!loaded) {
entryCache = GetContents();
entry_cache = GetContents();
loaded = true;
}
return entryCache;
return entry_cache;
}
std::vector<TpfEntry> gMod_FileLoader::GetContents() {
if (_fileName.ends_with(".tpf")) {
return GetTpfContents();
}
else {
return GetFileContents();
}
std::vector<TpfEntry> gMod_FileLoader::GetContents()
{
return file_name.ends_with(".tpf") ? GetTpfContents() : GetFileContents();
}
std::vector<TpfEntry> gMod_FileLoader::GetTpfContents() {
std::vector<TpfEntry> gMod_FileLoader::GetTpfContents()
{
std::vector<TpfEntry> 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<TpfEntry> gMod_FileLoader::GetFileContents() {
std::vector<TpfEntry> 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<TpfEntry>& entries) {
std::vector<TpfEntry> gMod_FileLoader::GetFileContents()
{
std::vector<TpfEntry> 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<TpfEntry>& 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
}
}
}