test: add unit test for IPakCreator

This commit is contained in:
Jan
2025-01-08 21:58:46 +01:00
parent 54e240e98c
commit fa249b0bd3
7 changed files with 292 additions and 219 deletions

View File

@ -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

View File

@ -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,203 +25,189 @@ class IPak::Impl : public ObjContainerReferenceable
return hash;
}
} // namespace
bool ReadIndexSection()
namespace
{
class IPak final : public IIPak
{
m_stream->seekg(m_index_section->offset);
IPakIndexEntry indexEntry{};
for (unsigned itemIndex = 0; itemIndex < m_index_section->itemCount; itemIndex++)
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)
{
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);
}
bool Initialize() override
{
if (m_initialized)
return true;
if (!ReadHeader())
return false;
}
m_index_entries.push_back(indexEntry);
}
std::ranges::sort(m_index_entries,
[](const IPakIndexEntry& entry1, const IPakIndexEntry& entry2)
{
return entry1.key.combinedKey < entry2.key.combinedKey;
});
return true;
}
bool ReadSection()
{
IPakSection section{};
m_stream->read(reinterpret_cast<char*>(&section), sizeof(section));
if (m_stream->gcount() != sizeof(section))
{
printf("Unexpected eof when trying to load section.\n");
return false;
}
switch (section.type)
{
case ipak_consts::IPAK_INDEX_SECTION:
m_index_section = std::make_unique<IPakSection>(section);
break;
case ipak_consts::IPAK_DATA_SECTION:
m_data_section = std::make_unique<IPakSection>(section);
break;
default:
break;
}
return true;
}
bool ReadHeader()
{
IPakHeader header{};
m_stream->read(reinterpret_cast<char*>(&header), sizeof(header));
if (m_stream->gcount() != sizeof(header))
{
printf("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);
return false;
}
if (header.version != ipak_consts::IPAK_VERSION)
{
printf("Unsupported ipak version '%u'.\n", header.version);
return false;
}
for (unsigned section = 0; section < header.sectionCount; section++)
{
if (!ReadSection())
return false;
}
if (m_index_section == nullptr)
{
printf("IPak does not contain an index section.\n");
return false;
}
if (m_data_section == nullptr)
{
printf("IPak does not contain a data section.\n");
return false;
}
if (!ReadIndexSection())
return false;
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)
{
}
~Impl() override = default;
std::string GetName() override
{
return fs::path(m_path).filename().replace_extension("").string();
}
bool Initialize()
{
if (m_initialized)
m_initialized = true;
return true;
if (!ReadHeader())
return false;
m_initialized = true;
return true;
}
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;
}
[[nodiscard]] std::unique_ptr<iobjstream> GetEntryStream(const Hash nameHash, const Hash dataHash) const override
{
IPakIndexEntryKey wantedKey{};
wantedKey.nameHash = nameHash;
wantedKey.dataHash = dataHash;
static Hash HashString(const std::string& str)
{
return R_HashString(str.c_str(), 0);
}
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;
}
}
static Hash HashData(const void* data, const size_t dataSize)
{
return crc32(0, static_cast<const Bytef*>(data), dataSize);
}
};
return nullptr;
}
IPak::IPak(std::string path, std::unique_ptr<std::istream> stream)
std::string GetName() override
{
return fs::path(m_path).filename().replace_extension("").string();
}
private:
bool ReadIndexSection()
{
m_stream->seekg(m_index_section->offset);
IPakIndexEntry indexEntry{};
for (unsigned itemIndex = 0; itemIndex < m_index_section->itemCount; itemIndex++)
{
m_stream->read(reinterpret_cast<char*>(&indexEntry), sizeof(indexEntry));
if (m_stream->gcount() != sizeof(indexEntry))
{
std::cerr << std::format("Unexpected eof when trying to load index entry {}.\n", itemIndex);
return false;
}
m_index_entries.push_back(indexEntry);
}
std::ranges::sort(m_index_entries,
[](const IPakIndexEntry& entry1, const IPakIndexEntry& entry2)
{
return entry1.key.combinedKey < entry2.key.combinedKey;
});
return true;
}
bool ReadSection()
{
IPakSection section{};
m_stream->read(reinterpret_cast<char*>(&section), sizeof(section));
if (m_stream->gcount() != sizeof(section))
{
std::cerr << "Unexpected eof when trying to load section.\n";
return false;
}
switch (section.type)
{
case ipak_consts::IPAK_INDEX_SECTION:
m_index_section = std::make_unique<IPakSection>(section);
break;
case ipak_consts::IPAK_DATA_SECTION:
m_data_section = std::make_unique<IPakSection>(section);
break;
default:
break;
}
return true;
}
bool ReadHeader()
{
IPakHeader header{};
m_stream->read(reinterpret_cast<char*>(&header), sizeof(header));
if (m_stream->gcount() != sizeof(header))
{
std::cerr << "Unexpected eof when trying to load header.\n";
return false;
}
if (header.magic != ipak_consts::IPAK_MAGIC)
{
std::cerr << std::format("Invalid ipak magic '{:#x}'.\n", header.magic);
return false;
}
if (header.version != ipak_consts::IPAK_VERSION)
{
std::cerr << std::format("Unsupported ipak version '{}'.\n", header.version);
return false;
}
for (unsigned section = 0; section < header.sectionCount; section++)
{
if (!ReadSection())
return false;
}
if (m_index_section == nullptr)
{
std::cerr << "IPak does not contain an index section.\n";
return false;
}
if (m_data_section == nullptr)
{
std::cerr << "IPak does not contain a data section.\n";
return false;
}
if (!ReadIndexSection())
return false;
return true;
}
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;
};
} // namespace
std::unique_ptr<IIPak> IIPak::Create(std::string path, std::unique_ptr<std::istream> stream)
{
m_impl = new Impl(std::move(path), std::move(stream));
return std::make_unique<IPak>(std::move(path), std::move(stream));
}
IPak::~IPak()
IIPak::Hash IIPak::HashString(const std::string& str)
{
delete m_impl;
m_impl = nullptr;
return R_HashString(str.c_str(), 0);
}
std::string IPak::GetName()
IIPak::Hash IIPak::HashData(const void* data, const size_t dataSize)
{
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);
return crc32(0, static_cast<const Bytef*>(data), dataSize);
}

View File

@ -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);
};