mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-05-09 22:14:56 +00:00
feat: write iwi data to specified iwds
This commit is contained in:
parent
fe5d0f79ff
commit
351b6bb1ea
@ -2,8 +2,11 @@
|
|||||||
|
|
||||||
#include "Utils/FileToZlibWrapper.h"
|
#include "Utils/FileToZlibWrapper.h"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <zip.h>
|
||||||
|
|
||||||
IwdToCreate::IwdToCreate(std::string name)
|
IwdToCreate::IwdToCreate(std::string name)
|
||||||
: m_name(std::move(name))
|
: m_name(std::move(name))
|
||||||
@ -17,10 +20,63 @@ void IwdToCreate::AddFile(std::string filePath)
|
|||||||
|
|
||||||
void IwdToCreate::Build(ISearchPath& searchPath, const std::filesystem::path& outPath)
|
void IwdToCreate::Build(ISearchPath& searchPath, const std::filesystem::path& outPath)
|
||||||
{
|
{
|
||||||
std::cout << std::format("Creating iwd {} with {} entries:\n", m_name, m_file_paths.size());
|
auto filePath = outPath / std::format("{}.iwd", m_name);
|
||||||
|
std::ofstream file(filePath, std::ios::out | std::ios::binary);
|
||||||
|
if (!file.is_open())
|
||||||
|
{
|
||||||
|
std::cerr << std::format("Failed to open file for iwd {}\n", m_name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto functions = FileToZlibWrapper::CreateFunctions32ForFile(&file);
|
||||||
|
|
||||||
|
auto zipFile = zipOpen2(filePath.c_str(), APPEND_STATUS_CREATE, nullptr, &functions);
|
||||||
|
if (!zipFile)
|
||||||
|
{
|
||||||
|
std::cerr << std::format("Failed to open file as zip for iwd {}\n", m_name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (const auto& filePath : m_file_paths)
|
for (const auto& filePath : m_file_paths)
|
||||||
std::cout << std::format(" {}\n", filePath);
|
{
|
||||||
|
auto readFile = searchPath.Open(filePath);
|
||||||
|
if (!readFile.IsOpen())
|
||||||
|
{
|
||||||
|
std::cerr << std::format("Failed to open file for iwd: {}\n", filePath);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto localNow = std::chrono::zoned_time{std::chrono::current_zone(), std::chrono::system_clock::now()}.get_local_time();
|
||||||
|
auto nowDays = std::chrono::floor<std::chrono::days>(localNow);
|
||||||
|
std::chrono::year_month_day ymd(std::chrono::floor<std::chrono::days>(localNow));
|
||||||
|
std::chrono::hh_mm_ss hms(std::chrono::floor<std::chrono::milliseconds>(localNow - nowDays));
|
||||||
|
|
||||||
|
zip_fileinfo fileInfo{};
|
||||||
|
fileInfo.dosDate = 0u;
|
||||||
|
fileInfo.tmz_date.tm_year = static_cast<int>(ymd.year());
|
||||||
|
fileInfo.tmz_date.tm_mon = static_cast<int>(static_cast<unsigned>(ymd.month()) - static_cast<unsigned>(std::chrono::January));
|
||||||
|
fileInfo.tmz_date.tm_mday = static_cast<int>(static_cast<unsigned>(ymd.day()));
|
||||||
|
fileInfo.tmz_date.tm_hour = static_cast<int>(hms.hours().count());
|
||||||
|
fileInfo.tmz_date.tm_min = static_cast<int>(hms.minutes().count());
|
||||||
|
fileInfo.tmz_date.tm_sec = static_cast<int>(hms.seconds().count());
|
||||||
|
zipOpenNewFileInZip(zipFile, filePath.c_str(), &fileInfo, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
|
||||||
|
|
||||||
|
char tempBuffer[0x1000];
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
readFile.m_stream->read(tempBuffer, sizeof(tempBuffer));
|
||||||
|
const auto readCount = readFile.m_stream->gcount();
|
||||||
|
if (readCount > 0)
|
||||||
|
zipWriteInFileInZip(zipFile, tempBuffer, readCount);
|
||||||
|
} while (!readFile.m_stream->eof());
|
||||||
|
|
||||||
|
zipCloseFileInZip(zipFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
zipClose(zipFile, nullptr);
|
||||||
|
|
||||||
|
std::cout << std::format("Created iwd {} with {} entries\n", m_name, m_file_paths.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
IwdToCreate* IwdCreator::GetOrAddIwd(const std::string& iwdName)
|
IwdToCreate* IwdCreator::GetOrAddIwd(const std::string& iwdName)
|
||||||
@ -38,6 +94,10 @@ IwdToCreate* IwdCreator::GetOrAddIwd(const std::string& iwdName)
|
|||||||
|
|
||||||
void IwdCreator::Finalize(ISearchPath& searchPath, const std::filesystem::path& outPath)
|
void IwdCreator::Finalize(ISearchPath& searchPath, const std::filesystem::path& outPath)
|
||||||
{
|
{
|
||||||
|
std::cout << std::format("Writing {} iwd files to disk\n", m_iwds.size());
|
||||||
for (const auto& iwdToCreate : m_iwds)
|
for (const auto& iwdToCreate : m_iwds)
|
||||||
iwdToCreate->Build(searchPath, outPath);
|
iwdToCreate->Build(searchPath, outPath);
|
||||||
|
|
||||||
|
m_iwds.clear();
|
||||||
|
m_iwd_lookup.clear();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user