mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-19 15:52:53 +00:00
test: add unit test for IPakCreator
This commit is contained in:
parent
54e240e98c
commit
fa249b0bd3
@ -165,13 +165,13 @@ namespace T6
|
||||
if (ObjLoading::Configuration.Verbose)
|
||||
std::cout << std::format("Trying to load ipak '{}' for zone '{}'\n", ipakName, zone.m_name);
|
||||
|
||||
auto* existingIPak = IPak::Repository.GetContainerByName(ipakName);
|
||||
auto* existingIPak = IIPak::Repository.GetContainerByName(ipakName);
|
||||
if (existingIPak != nullptr)
|
||||
{
|
||||
if (ObjLoading::Configuration.Verbose)
|
||||
std::cout << std::format("Referencing loaded ipak '{}'.\n", ipakName);
|
||||
|
||||
IPak::Repository.AddContainerReference(existingIPak, &zone);
|
||||
IIPak::Repository.AddContainerReference(existingIPak, &zone);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -180,11 +180,11 @@ namespace T6
|
||||
auto file = searchPath.Open(ipakFilename);
|
||||
if (file.IsOpen())
|
||||
{
|
||||
auto ipak = std::make_unique<IPak>(ipakFilename, std::move(file.m_stream));
|
||||
auto ipak = IIPak::Create(ipakFilename, std::move(file.m_stream));
|
||||
|
||||
if (ipak->Initialize())
|
||||
{
|
||||
IPak::Repository.AddContainer(std::move(ipak), &zone);
|
||||
IIPak::Repository.AddContainer(std::move(ipak), &zone);
|
||||
|
||||
if (ObjLoading::Configuration.Verbose)
|
||||
std::cout << std::format("Found and loaded ipak '{}'.\n", ipakFilename);
|
||||
@ -277,7 +277,7 @@ namespace T6
|
||||
|
||||
void ObjLoader::UnloadContainersOfZone(Zone& zone) const
|
||||
{
|
||||
IPak::Repository.RemoveContainerReferences(&zone);
|
||||
IIPak::Repository.RemoveContainerReferences(&zone);
|
||||
}
|
||||
|
||||
namespace
|
||||
|
@ -1,35 +1,22 @@
|
||||
#include "IPak.h"
|
||||
|
||||
#include "Exception/IPakLoadException.h"
|
||||
#include "IPakStreamManager.h"
|
||||
#include "ObjContainer/IPak/IPakTypes.h"
|
||||
#include "Utils/FileUtils.h"
|
||||
#include "zlib.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
ObjContainerRepository<IPak, Zone> IPak::Repository;
|
||||
ObjContainerRepository<IIPak, Zone> IIPak::Repository;
|
||||
|
||||
class IPak::Impl : public ObjContainerReferenceable
|
||||
namespace
|
||||
{
|
||||
std::string m_path;
|
||||
std::unique_ptr<std::istream> m_stream;
|
||||
|
||||
bool m_initialized;
|
||||
|
||||
std::unique_ptr<IPakSection> m_index_section;
|
||||
std::unique_ptr<IPakSection> m_data_section;
|
||||
|
||||
std::vector<IPakIndexEntry> m_index_entries;
|
||||
|
||||
IPakStreamManager m_stream_manager;
|
||||
|
||||
static uint32_t R_HashString(const char* str, uint32_t hash)
|
||||
std::uint32_t R_HashString(const char* str, std::uint32_t hash)
|
||||
{
|
||||
for (const auto* pos = str; *pos; pos++)
|
||||
{
|
||||
@ -38,7 +25,63 @@ class IPak::Impl : public ObjContainerReferenceable
|
||||
|
||||
return hash;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace
|
||||
{
|
||||
class IPak final : public IIPak
|
||||
{
|
||||
public:
|
||||
IPak(std::string path, std::unique_ptr<std::istream> stream)
|
||||
: m_path(std::move(path)),
|
||||
m_stream(std::move(stream)),
|
||||
m_initialized(false),
|
||||
m_index_section(nullptr),
|
||||
m_data_section(nullptr),
|
||||
m_stream_manager(*m_stream)
|
||||
{
|
||||
}
|
||||
|
||||
bool Initialize() override
|
||||
{
|
||||
if (m_initialized)
|
||||
return true;
|
||||
|
||||
if (!ReadHeader())
|
||||
return false;
|
||||
|
||||
m_initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::unique_ptr<iobjstream> GetEntryStream(const Hash nameHash, const Hash dataHash) const override
|
||||
{
|
||||
IPakIndexEntryKey wantedKey{};
|
||||
wantedKey.nameHash = nameHash;
|
||||
wantedKey.dataHash = dataHash;
|
||||
|
||||
for (auto& entry : m_index_entries)
|
||||
{
|
||||
if (entry.key.combinedKey == wantedKey.combinedKey)
|
||||
{
|
||||
return m_stream_manager.OpenStream(static_cast<int64_t>(m_data_section->offset) + entry.offset, entry.size);
|
||||
}
|
||||
else if (entry.key.combinedKey > wantedKey.combinedKey)
|
||||
{
|
||||
// The index entries are sorted so if the current entry is higher than the wanted entry we can cancel here
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string GetName() override
|
||||
{
|
||||
return fs::path(m_path).filename().replace_extension("").string();
|
||||
}
|
||||
|
||||
private:
|
||||
bool ReadIndexSection()
|
||||
{
|
||||
m_stream->seekg(m_index_section->offset);
|
||||
@ -49,7 +92,7 @@ class IPak::Impl : public ObjContainerReferenceable
|
||||
m_stream->read(reinterpret_cast<char*>(&indexEntry), sizeof(indexEntry));
|
||||
if (m_stream->gcount() != sizeof(indexEntry))
|
||||
{
|
||||
printf("Unexpected eof when trying to load index entry %u.\n", itemIndex);
|
||||
std::cerr << std::format("Unexpected eof when trying to load index entry {}.\n", itemIndex);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -72,7 +115,7 @@ class IPak::Impl : public ObjContainerReferenceable
|
||||
m_stream->read(reinterpret_cast<char*>(§ion), sizeof(section));
|
||||
if (m_stream->gcount() != sizeof(section))
|
||||
{
|
||||
printf("Unexpected eof when trying to load section.\n");
|
||||
std::cerr << "Unexpected eof when trying to load section.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -100,19 +143,19 @@ class IPak::Impl : public ObjContainerReferenceable
|
||||
m_stream->read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
if (m_stream->gcount() != sizeof(header))
|
||||
{
|
||||
printf("Unexpected eof when trying to load header.\n");
|
||||
std::cerr << "Unexpected eof when trying to load header.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (header.magic != ipak_consts::IPAK_MAGIC)
|
||||
{
|
||||
printf("Invalid ipak magic '0x%x'.\n", header.magic);
|
||||
std::cerr << std::format("Invalid ipak magic '{:#x}'.\n", header.magic);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (header.version != ipak_consts::IPAK_VERSION)
|
||||
{
|
||||
printf("Unsupported ipak version '%u'.\n", header.version);
|
||||
std::cerr << std::format("Unsupported ipak version '{}'.\n", header.version);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -124,13 +167,13 @@ class IPak::Impl : public ObjContainerReferenceable
|
||||
|
||||
if (m_index_section == nullptr)
|
||||
{
|
||||
printf("IPak does not contain an index section.\n");
|
||||
std::cerr << "IPak does not contain an index section.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_data_section == nullptr)
|
||||
{
|
||||
printf("IPak does not contain a data section.\n");
|
||||
std::cerr << "IPak does not contain a data section.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -140,101 +183,31 @@ class IPak::Impl : public ObjContainerReferenceable
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
Impl(std::string path, std::unique_ptr<std::istream> stream)
|
||||
: m_path(std::move(path)),
|
||||
m_stream(std::move(stream)),
|
||||
m_initialized(false),
|
||||
m_index_section(nullptr),
|
||||
m_data_section(nullptr),
|
||||
m_stream_manager(*m_stream)
|
||||
{
|
||||
}
|
||||
std::string m_path;
|
||||
std::unique_ptr<std::istream> m_stream;
|
||||
|
||||
~Impl() override = default;
|
||||
bool m_initialized;
|
||||
|
||||
std::string GetName() override
|
||||
{
|
||||
return fs::path(m_path).filename().replace_extension("").string();
|
||||
}
|
||||
std::unique_ptr<IPakSection> m_index_section;
|
||||
std::unique_ptr<IPakSection> m_data_section;
|
||||
|
||||
bool Initialize()
|
||||
{
|
||||
if (m_initialized)
|
||||
return true;
|
||||
std::vector<IPakIndexEntry> m_index_entries;
|
||||
|
||||
if (!ReadHeader())
|
||||
return false;
|
||||
IPakStreamManager m_stream_manager;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
m_initialized = true;
|
||||
return true;
|
||||
}
|
||||
std::unique_ptr<IIPak> IIPak::Create(std::string path, std::unique_ptr<std::istream> stream)
|
||||
{
|
||||
return std::make_unique<IPak>(std::move(path), std::move(stream));
|
||||
}
|
||||
|
||||
std::unique_ptr<iobjstream> GetEntryData(const Hash nameHash, const Hash dataHash)
|
||||
{
|
||||
IPakIndexEntryKey wantedKey{};
|
||||
wantedKey.nameHash = nameHash;
|
||||
wantedKey.dataHash = dataHash;
|
||||
|
||||
for (auto& entry : m_index_entries)
|
||||
{
|
||||
if (entry.key.combinedKey == wantedKey.combinedKey)
|
||||
{
|
||||
return m_stream_manager.OpenStream(static_cast<int64_t>(m_data_section->offset) + entry.offset, entry.size);
|
||||
}
|
||||
else if (entry.key.combinedKey > wantedKey.combinedKey)
|
||||
{
|
||||
// The index entries are sorted so if the current entry is higher than the wanted entry we can cancel here
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static Hash HashString(const std::string& str)
|
||||
{
|
||||
IIPak::Hash IIPak::HashString(const std::string& str)
|
||||
{
|
||||
return R_HashString(str.c_str(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
static Hash HashData(const void* data, const size_t dataSize)
|
||||
{
|
||||
IIPak::Hash IIPak::HashData(const void* data, const size_t dataSize)
|
||||
{
|
||||
return crc32(0, static_cast<const Bytef*>(data), dataSize);
|
||||
}
|
||||
};
|
||||
|
||||
IPak::IPak(std::string path, std::unique_ptr<std::istream> stream)
|
||||
{
|
||||
m_impl = new Impl(std::move(path), std::move(stream));
|
||||
}
|
||||
|
||||
IPak::~IPak()
|
||||
{
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
}
|
||||
|
||||
std::string IPak::GetName()
|
||||
{
|
||||
return m_impl->GetName();
|
||||
}
|
||||
|
||||
bool IPak::Initialize()
|
||||
{
|
||||
return m_impl->Initialize();
|
||||
}
|
||||
|
||||
std::unique_ptr<iobjstream> IPak::GetEntryStream(const Hash nameHash, const Hash dataHash) const
|
||||
{
|
||||
return m_impl->GetEntryData(nameHash, dataHash);
|
||||
}
|
||||
|
||||
IPak::Hash IPak::HashString(const std::string& str)
|
||||
{
|
||||
return Impl::HashString(str);
|
||||
}
|
||||
|
||||
IPak::Hash IPak::HashData(const void* data, const size_t dataSize)
|
||||
{
|
||||
return Impl::HashData(data, dataSize);
|
||||
}
|
||||
|
@ -2,30 +2,31 @@
|
||||
|
||||
#include "ObjContainer/ObjContainerReferenceable.h"
|
||||
#include "ObjContainer/ObjContainerRepository.h"
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "Utils/ObjStream.h"
|
||||
#include "Zone/Zone.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <istream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class IPak final : public ObjContainerReferenceable
|
||||
class IIPak : public ObjContainerReferenceable
|
||||
{
|
||||
class Impl;
|
||||
Impl* m_impl;
|
||||
|
||||
public:
|
||||
typedef uint32_t Hash;
|
||||
static ObjContainerRepository<IIPak, Zone> Repository;
|
||||
typedef std::uint32_t Hash;
|
||||
|
||||
static ObjContainerRepository<IPak, Zone> Repository;
|
||||
IIPak() = default;
|
||||
virtual ~IIPak() = default;
|
||||
IIPak(const IIPak& other) = default;
|
||||
IIPak(IIPak&& other) noexcept = default;
|
||||
IIPak& operator=(const IIPak& other) = default;
|
||||
IIPak& operator=(IIPak&& other) noexcept = default;
|
||||
|
||||
IPak(std::string path, std::unique_ptr<std::istream> stream);
|
||||
~IPak() override;
|
||||
|
||||
std::string GetName() override;
|
||||
|
||||
bool Initialize();
|
||||
_NODISCARD std::unique_ptr<iobjstream> GetEntryStream(Hash nameHash, Hash dataHash) const;
|
||||
virtual bool Initialize() = 0;
|
||||
[[nodiscard]] virtual std::unique_ptr<iobjstream> GetEntryStream(Hash nameHash, Hash dataHash) const = 0;
|
||||
|
||||
static std::unique_ptr<IIPak> Create(std::string path, std::unique_ptr<std::istream> stream);
|
||||
static Hash HashString(const std::string& str);
|
||||
static Hash HashData(const void* data, size_t dataSize);
|
||||
};
|
||||
|
@ -38,7 +38,7 @@ namespace
|
||||
{
|
||||
if (image->streamedPartCount > 0)
|
||||
{
|
||||
for (auto* ipak : IPak::Repository)
|
||||
for (auto* ipak : IIPak::Repository)
|
||||
{
|
||||
auto ipakStream = ipak->GetEntryStream(image->hash, image->streamedParts[0].hash);
|
||||
|
||||
|
@ -108,6 +108,11 @@ MockOutputFile::MockOutputFile(std::string name, std::vector<std::uint8_t> data)
|
||||
{
|
||||
}
|
||||
|
||||
std::string MockOutputFile::AsString() const
|
||||
{
|
||||
return std::string(reinterpret_cast<const char*>(m_data.data()), m_data.size());
|
||||
}
|
||||
|
||||
std::unique_ptr<std::ostream> MockOutputPath::Open(const std::string& fileName)
|
||||
{
|
||||
return std::make_unique<MockFileWrapper>(fileName, m_files);
|
||||
@ -123,3 +128,8 @@ const MockOutputFile* MockOutputPath::GetMockedFile(const std::string& name) con
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::vector<MockOutputFile>& MockOutputPath::GetMockedFileList() const
|
||||
{
|
||||
return m_files;
|
||||
}
|
||||
|
@ -15,6 +15,8 @@ public:
|
||||
|
||||
MockOutputFile();
|
||||
MockOutputFile(std::string name, std::vector<std::uint8_t> data);
|
||||
|
||||
[[nodiscard]] std::string AsString() const;
|
||||
};
|
||||
|
||||
class MockOutputPath final : public IOutputPath
|
||||
@ -23,6 +25,7 @@ public:
|
||||
std::unique_ptr<std::ostream> Open(const std::string& fileName) override;
|
||||
|
||||
[[nodiscard]] const MockOutputFile* GetMockedFile(const std::string& name) const;
|
||||
[[nodiscard]] const std::vector<MockOutputFile>& GetMockedFileList() const;
|
||||
|
||||
private:
|
||||
std::vector<MockOutputFile> m_files;
|
||||
|
86
test/ObjCompilingTests/Image/IPak/IPakCreatorTest.cpp
Normal file
86
test/ObjCompilingTests/Image/IPak/IPakCreatorTest.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "Image/IPak/IPakCreator.h"
|
||||
|
||||
#include "Asset/AssetCreatorCollection.h"
|
||||
#include "ObjContainer/IPak/IPak.h"
|
||||
#include "SearchPath/MockOutputPath.h"
|
||||
#include "SearchPath/MockSearchPath.h"
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/generators/catch_generators.hpp>
|
||||
#include <catch2/matchers/catch_matchers_floating_point.hpp>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
namespace
|
||||
{
|
||||
class TestContext
|
||||
{
|
||||
public:
|
||||
TestContext()
|
||||
: m_zone("test", 0, IGame::GetGameById(GameId::T6)),
|
||||
m_zone_states(m_zone),
|
||||
m_out_dir()
|
||||
{
|
||||
}
|
||||
|
||||
IPakCreator& CreateSut()
|
||||
{
|
||||
return m_zone_states.GetZoneAssetCreationState<IPakCreator>();
|
||||
}
|
||||
|
||||
Zone m_zone;
|
||||
MockSearchPath m_search_path;
|
||||
ZoneAssetCreationStateContainer m_zone_states;
|
||||
MockOutputPath m_out_dir;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace test::image::ipak
|
||||
{
|
||||
TEST_CASE("IPakCreator: Does nothing if no ipak was defined", "[image]")
|
||||
{
|
||||
TestContext testContext;
|
||||
auto& sut = testContext.CreateSut();
|
||||
|
||||
sut.Finalize(testContext.m_search_path, testContext.m_out_dir);
|
||||
REQUIRE(testContext.m_out_dir.GetMockedFileList().empty());
|
||||
}
|
||||
|
||||
TEST_CASE("IPakCreator: Writes IPak file", "[image]")
|
||||
{
|
||||
TestContext testContext;
|
||||
auto& sut = testContext.CreateSut();
|
||||
|
||||
auto* ipak = sut.GetOrAddIPak("amazing");
|
||||
ipak->AddImage("random");
|
||||
|
||||
constexpr auto iwiData = "hello world";
|
||||
testContext.m_search_path.AddFileData("images/random.iwi", iwiData);
|
||||
|
||||
sut.Finalize(testContext.m_search_path, testContext.m_out_dir);
|
||||
|
||||
const auto* file = testContext.m_out_dir.GetMockedFile("amazing.ipak");
|
||||
REQUIRE(file);
|
||||
|
||||
const auto* data = file->m_data.data();
|
||||
REQUIRE(data[0] == 'K');
|
||||
REQUIRE(data[1] == 'A');
|
||||
REQUIRE(data[2] == 'P');
|
||||
REQUIRE(data[3] == 'I');
|
||||
|
||||
auto readIpak = IIPak::Create("amazing.ipak", std::make_unique<std::istringstream>(file->AsString()));
|
||||
REQUIRE(readIpak->Initialize());
|
||||
|
||||
auto entry = readIpak->GetEntryStream(IIPak::HashString("random"), IIPak::HashData(iwiData, std::char_traits<char>::length(iwiData)));
|
||||
REQUIRE(entry);
|
||||
|
||||
char readBuffer[std::char_traits<char>::length(iwiData) + 10];
|
||||
entry->read(readBuffer, sizeof(readBuffer));
|
||||
|
||||
REQUIRE(entry->gcount() == std::char_traits<char>::length(iwiData));
|
||||
REQUIRE(std::strncmp(iwiData, readBuffer, std::char_traits<char>::length(iwiData)) == 0);
|
||||
}
|
||||
} // namespace test::image::ipak
|
Loading…
x
Reference in New Issue
Block a user