mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-11 07:18:11 -05:00
chore: add abstraction for opening output files to be able to mock it
This commit is contained in:
@ -22,7 +22,7 @@ namespace
|
||||
const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir)
|
||||
IOutputPath& outDir)
|
||||
{
|
||||
auto& memory = *zone.GetMemory();
|
||||
|
||||
@ -37,8 +37,8 @@ void ObjCompiler::ConfigureCreatorCollection(AssetCreatorCollection& collection,
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir,
|
||||
const fs::path& cacheDir) const
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const
|
||||
{
|
||||
ConfigurePostProcessors(collection, zone, zoneDefinition, searchPath, zoneStates, outDir);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ namespace IW3
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir,
|
||||
const std::filesystem::path& cacheDir) const override;
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const override;
|
||||
};
|
||||
} // namespace IW3
|
||||
|
@ -22,7 +22,7 @@ namespace
|
||||
const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir)
|
||||
IOutputPath& outDir)
|
||||
{
|
||||
auto& memory = *zone.GetMemory();
|
||||
|
||||
@ -37,8 +37,8 @@ void ObjCompiler::ConfigureCreatorCollection(AssetCreatorCollection& collection,
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir,
|
||||
const fs::path& cacheDir) const
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const
|
||||
{
|
||||
ConfigurePostProcessors(collection, zone, zoneDefinition, searchPath, zoneStates, outDir);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ namespace IW4
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir,
|
||||
const std::filesystem::path& cacheDir) const override;
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const override;
|
||||
};
|
||||
} // namespace IW4
|
||||
|
@ -22,7 +22,7 @@ namespace
|
||||
const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir)
|
||||
IOutputPath& outDir)
|
||||
{
|
||||
auto& memory = *zone.GetMemory();
|
||||
|
||||
@ -37,8 +37,8 @@ void ObjCompiler::ConfigureCreatorCollection(AssetCreatorCollection& collection,
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir,
|
||||
const fs::path& cacheDir) const
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const
|
||||
{
|
||||
ConfigurePostProcessors(collection, zone, zoneDefinition, searchPath, zoneStates, outDir);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ namespace IW5
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir,
|
||||
const std::filesystem::path& cacheDir) const override;
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const override;
|
||||
};
|
||||
} // namespace IW5
|
||||
|
@ -22,7 +22,7 @@ namespace
|
||||
const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir)
|
||||
IOutputPath& outDir)
|
||||
{
|
||||
auto& memory = *zone.GetMemory();
|
||||
|
||||
@ -37,8 +37,8 @@ void ObjCompiler::ConfigureCreatorCollection(AssetCreatorCollection& collection,
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir,
|
||||
const fs::path& cacheDir) const
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const
|
||||
{
|
||||
ConfigurePostProcessors(collection, zone, zoneDefinition, searchPath, zoneStates, outDir);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ namespace T5
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir,
|
||||
const std::filesystem::path& cacheDir) const override;
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const override;
|
||||
};
|
||||
} // namespace T5
|
||||
|
@ -28,7 +28,7 @@ namespace
|
||||
const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir)
|
||||
IOutputPath& outDir)
|
||||
{
|
||||
auto& memory = *zone.GetMemory();
|
||||
|
||||
@ -46,8 +46,8 @@ void ObjCompiler::ConfigureCreatorCollection(AssetCreatorCollection& collection,
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const fs::path& outDir,
|
||||
const fs::path& cacheDir) const
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const
|
||||
{
|
||||
ConfigureCompilers(collection, zone, zoneDefinition, searchPath, zoneStates);
|
||||
ConfigurePostProcessors(collection, zone, zoneDefinition, searchPath, zoneStates, outDir);
|
||||
|
@ -13,7 +13,7 @@ namespace T6
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir,
|
||||
const std::filesystem::path& cacheDir) const override;
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const override;
|
||||
};
|
||||
} // namespace T6
|
||||
|
@ -4,14 +4,10 @@
|
||||
#include "Asset/IZoneAssetCreationState.h"
|
||||
#include "Asset/ZoneDefinitionContext.h"
|
||||
#include "Gdt/IGdtQueryable.h"
|
||||
#include "SearchPath/IOutputPath.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
#include "Zone/Definition/ZoneDefinition.h"
|
||||
#include "Zone/Zone.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
class IObjCompiler
|
||||
{
|
||||
public:
|
||||
@ -28,8 +24,8 @@ public:
|
||||
ISearchPath& searchPath,
|
||||
IGdtQueryable& gdt,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir,
|
||||
const std::filesystem::path& cacheDir) const = 0;
|
||||
IOutputPath& outDir,
|
||||
IOutputPath& cacheDir) const = 0;
|
||||
|
||||
static const IObjCompiler* GetObjCompilerForGame(GameId game);
|
||||
};
|
||||
|
@ -379,17 +379,16 @@ void IPakToCreate::AddImage(std::string imageName)
|
||||
m_image_names.emplace_back(std::move(imageName));
|
||||
}
|
||||
|
||||
void IPakToCreate::Build(ISearchPath& searchPath, const std::filesystem::path& outPath)
|
||||
void IPakToCreate::Build(ISearchPath& searchPath, IOutputPath& outPath)
|
||||
{
|
||||
auto filePath = outPath / std::format("{}.ipak", m_name);
|
||||
std::ofstream file(filePath, std::ios::out | std::ios::binary);
|
||||
if (!file.is_open())
|
||||
const auto file = outPath.Open(std::format("{}.ipak", m_name));
|
||||
if (!file)
|
||||
{
|
||||
std::cerr << std::format("Failed to open file for ipak {}\n", m_name);
|
||||
return;
|
||||
}
|
||||
|
||||
IPakWriter writer(file, searchPath, m_image_names);
|
||||
IPakWriter writer(*file, searchPath, m_image_names);
|
||||
writer.Write();
|
||||
|
||||
std::cout << std::format("Created ipak {} with {} entries\n", m_name, m_image_names.size());
|
||||
@ -421,7 +420,7 @@ IPakToCreate* IPakCreator::GetOrAddIPak(const std::string& ipakName)
|
||||
return result;
|
||||
}
|
||||
|
||||
void IPakCreator::Finalize(ISearchPath& searchPath, const std::filesystem::path& outPath)
|
||||
void IPakCreator::Finalize(ISearchPath& searchPath, IOutputPath& outPath)
|
||||
{
|
||||
for (const auto& ipakToCreate : m_ipaks)
|
||||
ipakToCreate->Build(searchPath, outPath);
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
#include "Asset/IZoneAssetCreationState.h"
|
||||
#include "KeyValuePairs/KeyValuePairsCreator.h"
|
||||
#include "SearchPath/IOutputPath.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
@ -15,14 +15,14 @@ public:
|
||||
explicit IPakToCreate(std::string name);
|
||||
|
||||
void AddImage(std::string imageName);
|
||||
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_image_names;
|
||||
};
|
||||
|
||||
class IPakCreator : public IZoneAssetCreationState
|
||||
class IPakCreator final : public IZoneAssetCreationState
|
||||
{
|
||||
public:
|
||||
IPakCreator();
|
||||
@ -30,7 +30,7 @@ public:
|
||||
void Inject(ZoneAssetCreationInjection& inject) override;
|
||||
|
||||
IPakToCreate* GetOrAddIPak(const std::string& ipakName);
|
||||
void Finalize(ISearchPath& searchPath, const std::filesystem::path& outPath);
|
||||
void Finalize(ISearchPath& searchPath, IOutputPath& outPath);
|
||||
|
||||
private:
|
||||
KeyValuePairsCreator* m_kvp_creator;
|
||||
|
@ -8,7 +8,7 @@
|
||||
AbstractImageIPakPostProcessor::AbstractImageIPakPostProcessor(const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir)
|
||||
IOutputPath& outDir)
|
||||
: m_zone_definition(zoneDefinition),
|
||||
m_search_path(searchPath),
|
||||
m_ipak_creator(zoneStates.GetZoneAssetCreationState<IPakCreator>()),
|
||||
|
@ -3,8 +3,7 @@
|
||||
#include "Asset/IAssetPostProcessor.h"
|
||||
#include "Asset/ZoneDefinitionContext.h"
|
||||
#include "Image/IPak/IPakCreator.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include "SearchPath/IOutputPath.h"
|
||||
|
||||
class AbstractImageIPakPostProcessor : public IAssetPostProcessor
|
||||
{
|
||||
@ -12,7 +11,7 @@ public:
|
||||
AbstractImageIPakPostProcessor(const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir);
|
||||
IOutputPath& outDir);
|
||||
|
||||
static bool AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition);
|
||||
|
||||
@ -25,7 +24,7 @@ private:
|
||||
const ZoneDefinitionContext& m_zone_definition;
|
||||
ISearchPath& m_search_path;
|
||||
IPakCreator& m_ipak_creator;
|
||||
const std::filesystem::path& m_out_dir;
|
||||
IOutputPath& m_out_dir;
|
||||
|
||||
unsigned m_obj_container_index;
|
||||
IPakToCreate* m_current_ipak;
|
||||
@ -41,7 +40,7 @@ public:
|
||||
ImageIPakPostProcessor(const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir)
|
||||
IOutputPath& outDir)
|
||||
: AbstractImageIPakPostProcessor(zoneDefinition, searchPath, zoneStates, outDir)
|
||||
{
|
||||
}
|
||||
@ -49,5 +48,5 @@ public:
|
||||
[[nodiscard]] asset_type_t GetHandlingAssetType() const override
|
||||
{
|
||||
return AssetType::EnumEntry;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
@ -9,7 +9,7 @@
|
||||
AbstractImageIwdPostProcessor::AbstractImageIwdPostProcessor(const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir)
|
||||
IOutputPath& outDir)
|
||||
: m_zone_definition(zoneDefinition),
|
||||
m_search_path(searchPath),
|
||||
m_iwd_creator(zoneStates.GetZoneAssetCreationState<IwdCreator>()),
|
||||
|
@ -3,8 +3,7 @@
|
||||
#include "Asset/IAssetPostProcessor.h"
|
||||
#include "Asset/ZoneDefinitionContext.h"
|
||||
#include "Iwd/IwdCreator.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include "SearchPath/IOutputPath.h"
|
||||
|
||||
class AbstractImageIwdPostProcessor : public IAssetPostProcessor
|
||||
{
|
||||
@ -12,7 +11,7 @@ public:
|
||||
AbstractImageIwdPostProcessor(const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir);
|
||||
IOutputPath& outDir);
|
||||
|
||||
static bool AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition);
|
||||
|
||||
@ -25,7 +24,7 @@ private:
|
||||
const ZoneDefinitionContext& m_zone_definition;
|
||||
ISearchPath& m_search_path;
|
||||
IwdCreator& m_iwd_creator;
|
||||
const std::filesystem::path& m_out_dir;
|
||||
IOutputPath& m_out_dir;
|
||||
|
||||
unsigned m_obj_container_index;
|
||||
IwdToCreate* m_current_iwd;
|
||||
@ -41,7 +40,7 @@ public:
|
||||
ImageIwdPostProcessor(const ZoneDefinitionContext& zoneDefinition,
|
||||
ISearchPath& searchPath,
|
||||
ZoneAssetCreationStateContainer& zoneStates,
|
||||
const std::filesystem::path& outDir)
|
||||
IOutputPath& outDir)
|
||||
: AbstractImageIwdPostProcessor(zoneDefinition, searchPath, zoneStates, outDir)
|
||||
{
|
||||
}
|
||||
@ -49,5 +48,5 @@ public:
|
||||
[[nodiscard]] asset_type_t GetHandlingAssetType() const override
|
||||
{
|
||||
return AssetType::EnumEntry;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user