// SPDX-License-Identifier: MIT // Copyright (c) 2026 The Bozos (boubou666 and Arzaroth). MIT licence (see LICENSE at the repository root). // // Moved from the team's code in the PS2Recomp fork (native_mesh_pack.cpp; phase4-inventory.md section 2.1). // The mesh pack's loader and object store (runtime/native/native_mesh_pack.h, docs/native_packs.md). #include "bozo/render/mesh_pack.h" #include #include namespace bozo::render::packs { uint32_t crc32(const uint8_t *data, size_t size) { static uint32_t s_table[256]; static const bool s_made = [] { for (uint32_t i = 0; i < 256u; ++i) { uint32_t c = i; for (int k = 0; k < 8; ++k) c = (c & 1u) ? 0xedb88320u ^ (c >> 1) : c >> 1; s_table[i] = c; } return true; }(); (void)s_made; uint32_t c = 0xffffffffu; for (size_t i = 0; i < size; ++i) c = s_table[(c ^ data[i]) & 0xffu] ^ (c >> 8); return c ^ 0xffffffffu; } const MeshObject *MeshFile::object(uint32_t offset) const { const auto it = std::lower_bound(objects.begin(), objects.end(), offset, [](const MeshObject &o, uint32_t v) { return o.offset < v; }); return it != objects.end() && it->offset == offset ? &*it : nullptr; } const RegisterBlock *MeshFile::block(uint32_t offset) const { const auto it = std::lower_bound(blocks.begin(), blocks.end(), offset, [](const RegisterBlock &b, uint32_t v) { return b.offset < v; }); return it != blocks.end() && it->offset == offset ? &*it : nullptr; } namespace { // A bounds-checked reader over the pack's bytes. struct Reader { const uint8_t *p, *end; bool ok = true; bool has(size_t n) const { return ok && static_cast(end - p) >= n; } template T get() { T v{}; if (!has(sizeof(T))) { ok = false; return v; } std::memcpy(&v, p, sizeof(T)); p += sizeof(T); return v; } const uint8_t *skip(size_t n) { if (!has(n)) { ok = false; return nullptr; } const uint8_t *at = p; p += n; return at; } }; } bool MeshPack::load(const std::string &path) { m_blob.clear(); m_files.clear(); m_ok = false; std::ifstream in(path, std::ios::binary | std::ios::ate); if (!in) { m_error = "missing"; return false; } m_blob.resize(static_cast(in.tellg())); in.seekg(0); in.read(reinterpret_cast(m_blob.data()), static_cast(m_blob.size())); Reader r{m_blob.data(), m_blob.data() + m_blob.size()}; if (m_blob.size() < 12u || std::memcmp(m_blob.data(), kMagic, 8) != 0) { m_error = "not a mesh pack of this version"; m_blob.clear(); return false; } r.skip(8); const uint32_t fileCount = r.get(); for (uint32_t f = 0; f < fileCount && r.ok; ++f) { MeshFile file; const uint16_t length = r.get(); if (const uint8_t *name = r.skip(length)) file.name.assign(reinterpret_cast(name), length); file.size = r.get(); file.crc = r.get(); const uint32_t count = r.get(); if (!r.has(static_cast(count) * 24u)) break; file.objects.resize(count); for (MeshObject &o : file.objects) { o.offset = r.get(); o.kind = r.get(); o.attributeSize = r.get(); o.attributes = r.skip(o.attributeSize); o.positionCount = r.get(); o.positions = reinterpret_cast(r.skip(static_cast(o.positionCount) * 16u)); const uint32_t sections = r.get(); for (uint32_t s = 0; s < sections && r.ok; ++s) { MeshSection section; section.tag = r.get(); section.size = r.get(); section.data = r.skip(section.size); o.sections.push_back(section); } o.stripCount = r.get(); o.strips = r.p; for (uint32_t s = 0; s < o.stripCount && r.ok; ++s) { const uint8_t *header = r.skip(sizeof(StripHeader)); if (!header) break; StripHeader h; std::memcpy(&h, header, sizeof(h)); r.skip(static_cast(h.vertexCount) * sizeof(StripVertex)); } if (!r.ok) break; } const uint32_t blocks = r.get(); if (!r.has(static_cast(blocks) * 8u)) r.ok = false; else file.blocks.resize(blocks); for (RegisterBlock &b : file.blocks) { b.offset = r.get(); const uint32_t writes = r.get(); if (!r.has(static_cast(writes) * 12u)) { r.ok = false; break; } b.writes.resize(writes); for (auto &w : b.writes) { w.first = r.get(); w.second = r.get(); } } if (!r.ok) break; m_files.push_back(std::move(file)); } if (!r.ok) { m_error = "truncated"; m_files.clear(); m_blob.clear(); return false; } m_ok = true; return true; } const MeshFile *MeshPack::file(const std::string &name, uint32_t *index) const { for (size_t i = 0; i < m_files.size(); ++i) if (m_files[i].name == name) { if (index) *index = static_cast(i); return &m_files[i]; } return nullptr; } size_t MeshPack::objectCount() const { size_t n = 0; for (const MeshFile &f : m_files) n += f.objects.size(); return n; } LoadedImages::Bound LoadedImages::bind(const MeshPack &pack, const uint8_t *rdram, const std::string &name, uint32_t base, uint32_t size, bool replace) { Bound bound; bound.image.base = base; bound.image.size = size; uint32_t index = 0; if (const MeshFile *file = pack.file(name, &index)) { bound.named = file; bound.crc = size == file->size && (base & 0x1ffffffu) + size <= 0x2000000u ? crc32(rdram + (base & 0x1ffffffu), size) : 0u; if (size == file->size && bound.crc == file->crc) { bound.image.file = file; bound.image.index = index; } } std::lock_guard lock(m_mutex); if (replace) m_images.clear(); m_images.push_back(bound.image); return bound; } void LoadedImages::clear() { std::lock_guard lock(m_mutex); m_images.clear(); } std::vector LoadedImages::images() const { std::lock_guard lock(m_mutex); return m_images; } void LoadedImages::restore(std::vector images) { std::lock_guard lock(m_mutex); m_images = std::move(images); } bool LoadedImages::find(uint32_t address, Image &out) const { std::lock_guard lock(m_mutex); bool found = false; for (const Image &image : m_images) if (address >= image.base && address < image.base + image.size) { out = image; found = true; } return found; } }