10std::unique_ptr<PackFile>
ORE::create(
const std::string& path) {
12 FileStream stream{path, FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
14 .write<uint32_t>(
sizeof(uint32_t) * 3)
22 if (!std::filesystem::exists(path)) {
27 auto* ore =
new ORE{path};
28 auto packFile = std::unique_ptr<PackFile>(ore);
30 FileStream reader{ore->fullFilePath};
33 auto headerSize = reader.read<uint32_t>();
34 auto dirCount = reader.read<uint32_t>();
35 std::vector<std::pair<std::string, uint32_t>> dirInfos;
36 for (uint32_t i = 0; i < dirCount; i++) {
37 std::pair<std::string, uint32_t> dirInfo;
38 dirInfo.first = reader.read_string();
39 dirInfo.second = reader.read<uint32_t>();
40 dirInfos.push_back(dirInfo);
42 if (reader.tell_in() != headerSize) {
46 auto looseFileCount = reader.read<uint32_t>();
47 const auto addOreFiles = [&callback, ore, &reader](
const std::string& dirName, uint32_t dirOffset, uint32_t fileCount) {
48 for (uint32_t i = 0; i < fileCount; i++) {
51 auto entryPath = ore->cleanEntryPath(dirName + (dirName.empty() ?
"" :
"/") + reader.read_string());
53 entry.
offset = reader.read<uint32_t>() + dirOffset;
54 entry.
length = reader.read<uint32_t>();
56 ore->entries.emplace(entryPath, entry);
59 callback(entryPath, entry);
63 addOreFiles(
"", headerSize, looseFileCount);
65 for (
const auto& [dirName, dirOffset] : dirInfos) {
66 reader.seek_in(dirOffset);
67 if (reader.read<uint64_t>() != 8) {
70 addOreFiles(dirName, dirOffset +
sizeof(uint64_t), reader.read<uint32_t>());
76std::optional<std::vector<std::byte>>
ORE::readEntry(
const std::string& path_)
const {
91 stream.seek_in_u(entry->offset);
92 return stream.read_bytes(entry->length);
This class represents the metadata that a file has inside a PackFile.
uint64_t offset
Offset, format-specific meaning - 0 if unused, or if the offset genuinely is 0.
uint64_t length
Length in bytes (in formats with compression, this is the uncompressed length)
std::optional< std::vector< std::byte > > readEntry(const std::string &path_) const override
Try to read the entry's data to a bytebuffer.
static std::unique_ptr< PackFile > create(const std::string &path)
Create an ORE file.
Attribute getSupportedEntryAttributes() const override
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their meta...
static std::unique_ptr< PackFile > open(const std::string &path, const EntryCallback &callback=nullptr)
Open an ORE file.
EntryCallbackBase< void > EntryCallback
std::optional< Entry > findEntry(const std::string &path_, bool includeUnbaked=true) const
Try to find an entry given the file path.
std::string cleanEntryPath(const std::string &path) const
static Entry createNewEntry()
static std::optional< std::vector< std::byte > > readUnbakedEntry(const Entry &entry)