Crusader_Decomp/rebuild/remorse/src/glob_archive.cpp

56 lines
1.6 KiB
C++
Raw Normal View History

#include "remorse/glob_archive.h"
#include "remorse/flex_archive.h"
namespace remorse {
namespace {
constexpr u32 globCountSize = 2;
constexpr u32 globEntrySize = 6;
u16 readU16Le(const std::vector<u8>& bytes, u32 offset)
{
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
}
} // namespace
std::optional<std::vector<GlobObject>> loadGlobObjects(const std::filesystem::path& path)
{
const auto archive = loadFlexArchive(path);
if (!archive) {
return std::nullopt;
}
std::vector<GlobObject> objects;
for (const FlexEntry& flexEntry : archive->populatedEntries) {
const auto bytes = readFlexEntry(*archive, flexEntry.index);
if (!bytes || bytes->size() < globCountSize) {
return std::nullopt;
}
const u16 entryCount = readU16Le(*bytes, 0);
const u32 expectedSize = globCountSize + static_cast<u32>(entryCount) * globEntrySize;
if (expectedSize > bytes->size()) {
return std::nullopt;
}
GlobObject object;
object.index = flexEntry.index;
for (u32 index = 0; index < entryCount; ++index) {
const u32 offset = globCountSize + index * globEntrySize;
GlobObjectEntry entry;
entry.x = (*bytes)[offset];
entry.y = (*bytes)[offset + 1];
entry.z = (*bytes)[offset + 2];
entry.shape = readU16Le(*bytes, offset + 3);
entry.frame = (*bytes)[offset + 5];
object.entries.push_back(entry);
}
objects.push_back(object);
}
return objects;
}
} // namespace remorse