Fixed o2r generation and moved from libzip to miniz

This commit is contained in:
KiritoDv
2025-01-15 10:01:49 -06:00
parent 94be71101c
commit 82c4397160
5 changed files with 5752 additions and 79 deletions
+4 -4
View File
@@ -7,11 +7,11 @@
class SWrapper : public BinaryWrapper {
public:
SWrapper(const std::string& path);
explicit SWrapper(const std::string& path);
int32_t CreateArchive(void);
bool CreateFile(const std::string& path, std::vector<char> data);
int32_t Close(void);
int32_t CreateArchive(void) override;
bool CreateFile(const std::string& path, std::vector<char> data) override;
int32_t Close(void) override;
private:
HANDLE hMpq{};
};
+14 -49
View File
@@ -5,34 +5,16 @@
#include "spdlog/spdlog.h"
#include <Companion.h>
#include <miniz/zip_file.hpp>
namespace fs = std::filesystem;
miniz_cpp::zip_file mZip;
ZWrapper::ZWrapper(const std::string& path) {
mPath = path;
}
int32_t ZWrapper::CreateArchive(void) {
int openErr;
zip_t* zip;
if(fs::exists(mPath)) {
fs::remove(mPath);
}
{
const std::lock_guard<std::mutex> lock(mMutex);
zip = zip_open(mPath.c_str(), ZIP_CREATE, &openErr);
}
if (zip == nullptr) {
zip_error_t error;
zip_error_init_with_code(&error, openErr);
SPDLOG_ERROR("Failed to create ZIP (O2R) file. Error: {}", zip_error_strerror(&error));
zip_error_fini(&error);
return -1;
}
mZip = zip;
int32_t ZWrapper::CreateArchive() {
SPDLOG_INFO("Loaded ZIP (O2R) archive: {}", mPath.c_str());
return 0;
}
@@ -40,40 +22,23 @@ int32_t ZWrapper::CreateArchive(void) {
bool ZWrapper::CreateFile(const std::string& path, std::vector<char> data) {
char* fileData = data.data();
size_t fileSize = data.size();
zip_source_t* source = zip_source_buffer(mZip, fileData, fileSize, 0);
if (source == nullptr) {
zip_error_t* zipError = zip_get_error(mZip);
SPDLOG_ERROR("Failed to create ZIP source. Error: {}", zip_error_strerror(zipError));
zip_source_free(source);
zip_error_fini(zipError);
return false;
}
if (zip_file_add(mZip, path.c_str(), source, ZIP_FL_OVERWRITE | ZIP_FL_ENC_UTF_8) < 0) {
zip_error_t* zipError = zip_get_error(mZip);
SPDLOG_ERROR("Failed to add file to ZIP. Error: {}", zip_error_strerror(zipError));
zip_source_free(source);
zip_error_fini(zipError);
return false;
if(Companion::Instance != nullptr && Companion::Instance->IsDebug()){
SPDLOG_INFO("Creating debug file: debug/{}", path);
std::string dpath = "debug/" + path;
if(!fs::exists(fs::path(dpath).parent_path())){
fs::create_directories(fs::path(dpath).parent_path());
}
std::ofstream stream(dpath, std::ios::binary);
stream.write(fileData, fileSize);
stream.close();
}
mZip.writebytes(path, data);
return true;
}
int32_t ZWrapper::Close(void) {
int err;
{
const std::lock_guard<std::mutex> lock(mMutex);
err = zip_close(mZip);
}
if (err < 0) {
zip_error_t* zipError = zip_get_error(mZip);
SPDLOG_ERROR("Failed to close ZIP (O2R) file. Error: {}", zip_error_strerror(zipError));
printf("fail\n");
zip_error_fini(zipError);
return -1;
}
mZip.save(this->mPath);
return 0;
}
+4 -7
View File
@@ -2,16 +2,13 @@
#include <vector>
#include <string>
#include <zip.h>
#include "BinaryWrapper.h"
class ZWrapper : public BinaryWrapper {
public:
ZWrapper(const std::string& path);
explicit ZWrapper(const std::string& path);
int32_t CreateArchive(void);
bool CreateFile(const std::string& path, std::vector<char> data);
int32_t Close(void);
private:
zip_t* mZip;
int32_t CreateArchive(void) override;
bool CreateFile(const std::string& path, std::vector<char> data) override;
int32_t Close(void) override;
};