chore: add abstraction for opening output files to be able to mock it

This commit is contained in:
Jan
2025-01-07 00:02:38 +01:00
parent cacccf64e1
commit e0f8b3d3ca
25 changed files with 202 additions and 68 deletions

View File

@ -18,19 +18,19 @@ void IwdToCreate::AddFile(std::string filePath)
m_file_paths.emplace_back(std::move(filePath));
}
void IwdToCreate::Build(ISearchPath& searchPath, const std::filesystem::path& outPath)
void IwdToCreate::Build(ISearchPath& searchPath, IOutputPath& outPath)
{
auto filePath = outPath / std::format("{}.iwd", m_name);
std::ofstream file(filePath, std::ios::out | std::ios::binary);
if (!file.is_open())
const auto fileName = std::format("{}.iwd", m_name);
const auto file = outPath.Open(fileName);
if (!file)
{
std::cerr << std::format("Failed to open file for iwd {}\n", m_name);
return;
}
auto functions = FileToZlibWrapper::CreateFunctions32ForFile(&file);
auto functions = FileToZlibWrapper::CreateFunctions32ForFile(file.get());
auto zipFile = zipOpen2(filePath.string().c_str(), APPEND_STATUS_CREATE, nullptr, &functions);
const auto zipFile = zipOpen2(fileName.c_str(), APPEND_STATUS_CREATE, nullptr, &functions);
if (!zipFile)
{
std::cerr << std::format("Failed to open file as zip for iwd {}\n", m_name);
@ -92,7 +92,7 @@ IwdToCreate* IwdCreator::GetOrAddIwd(const std::string& iwdName)
return result;
}
void IwdCreator::Finalize(ISearchPath& searchPath, const std::filesystem::path& outPath)
void IwdCreator::Finalize(ISearchPath& searchPath, IOutputPath& outPath)
{
std::cout << std::format("Writing {} iwd files to disk\n", m_iwds.size());
for (const auto& iwdToCreate : m_iwds)

View File

@ -1,9 +1,9 @@
#pragma once
#include "Asset/IZoneAssetCreationState.h"
#include "SearchPath/IOutputPath.h"
#include "SearchPath/ISearchPath.h"
#include <filesystem>
#include <memory>
#include <string>
#include <unordered_map>
@ -14,18 +14,18 @@ public:
explicit IwdToCreate(std::string name);
void AddFile(std::string filePath);
void Build(ISearchPath& searchPath, const std::filesystem::path& outPath);
void Build(ISearchPath& searchPath, IOutputPath& outPath);
private:
std::string m_name;
std::vector<std::string> m_file_paths;
};
class IwdCreator : public IZoneAssetCreationState
class IwdCreator final : public IZoneAssetCreationState
{
public:
IwdToCreate* GetOrAddIwd(const std::string& iwdName);
void Finalize(ISearchPath& searchPath, const std::filesystem::path& outPath);
void Finalize(ISearchPath& searchPath, IOutputPath& outPath);
private:
std::unordered_map<std::string, IwdToCreate*> m_iwd_lookup;