some code changes

This commit is contained in:
DubbleClick
2023-11-15 13:53:50 +01:00
parent fa8f6f0896
commit 74417a89e8
3 changed files with 56 additions and 55 deletions
+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
}
}
}