2022-09-05 20:59:46 -04:00
|
|
|
//
|
|
|
|
// EuropaTools
|
|
|
|
//
|
|
|
|
// (C) 2021-2022 modeco80 <lily.modeco80@protonmail.ch>
|
|
|
|
//
|
2022-09-21 03:49:57 -04:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2022-09-05 20:59:46 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef EUROPA_IO_PAKFILE_H
|
|
|
|
#define EUROPA_IO_PAKFILE_H
|
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
#include <cstdint>
|
2022-09-21 03:59:16 -04:00
|
|
|
#include <europa/structs/Pak.hpp>
|
2022-09-07 05:07:40 -04:00
|
|
|
#include <vector>
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
namespace europa::io {
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
struct PakReader;
|
|
|
|
struct PakWriter;
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
struct PakFile {
|
|
|
|
using DataType = std::vector<std::uint8_t>;
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2023-08-01 18:18:40 -04:00
|
|
|
template<class T>
|
|
|
|
void InitAs(const T& value) {
|
|
|
|
toc = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitAs(structs::PakVersion version) {
|
|
|
|
switch(version) {
|
|
|
|
case structs::PakVersion::Ver3:
|
|
|
|
toc = structs::PakHeader_V3::TocEntry{};
|
|
|
|
break;
|
|
|
|
case structs::PakVersion::Ver4:
|
|
|
|
toc = structs::PakHeader_V4::TocEntry{};
|
|
|
|
break;
|
|
|
|
case structs::PakVersion::Ver5:
|
|
|
|
toc = structs::PakHeader_V5::TocEntry{};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
/**
|
|
|
|
* Get the file data.
|
|
|
|
*/
|
2023-08-01 18:18:40 -04:00
|
|
|
[[nodiscard]] const DataType& GetData() const {
|
|
|
|
return data;
|
|
|
|
}
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
/**
|
|
|
|
* Get the TOC entry responsible.
|
|
|
|
*/
|
2023-08-01 18:18:40 -04:00
|
|
|
template<class T>
|
|
|
|
[[nodiscard]] const T& GetTOCEntry() const {
|
|
|
|
return std::get<T>(toc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetData(DataType&& data) {
|
|
|
|
this->data = std::move(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::uint32_t GetOffset() const {
|
|
|
|
std::uint32_t size{};
|
|
|
|
|
|
|
|
std::visit([&](auto& entry) {
|
|
|
|
size = entry.offset;
|
|
|
|
}, toc);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::uint32_t GetSize() const {
|
|
|
|
std::uint32_t size{};
|
|
|
|
|
|
|
|
std::visit([&](auto& entry) {
|
|
|
|
size = entry.size;
|
|
|
|
}, toc);
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2023-08-01 18:18:40 -04:00
|
|
|
return size;
|
|
|
|
}
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2023-08-01 18:18:40 -04:00
|
|
|
void FillTOCEntry() {
|
|
|
|
std::visit([&](auto& entry) {
|
|
|
|
entry.size = static_cast<std::uint32_t>(data.size());
|
|
|
|
}, toc);
|
|
|
|
}
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2023-08-01 18:18:40 -04:00
|
|
|
template<class Cb>
|
|
|
|
void Visit(const Cb& cb) {
|
|
|
|
std::visit(cb, toc);
|
|
|
|
}
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
private:
|
|
|
|
friend PakReader;
|
|
|
|
friend PakWriter;
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
std::vector<std::uint8_t> data;
|
2023-08-01 18:18:40 -04:00
|
|
|
structs::PakTocEntryVariant toc;
|
2022-09-07 05:07:40 -04:00
|
|
|
};
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
} // namespace europa::io
|
2022-09-05 20:59:46 -04:00
|
|
|
|
2022-09-07 05:07:40 -04:00
|
|
|
#endif // EUROPA_IO_PAKFILE_H
|