mirror of
https://github.com/izzy2lost/Torch.git
synced 2026-07-06 00:18:35 -07:00
Fixed o2r generation and moved from libzip to miniz
This commit is contained in:
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user