#include "remorse/usecode_archive.h" #include namespace remorse { namespace { u16 readWord(const std::vector& bytes, std::size_t offset) { return static_cast(bytes[offset] | (static_cast(bytes[offset + 1]) << 8)); } u32 readDword(const std::vector& bytes, std::size_t offset) { return static_cast(bytes[offset]) | (static_cast(bytes[offset + 1]) << 8) | (static_cast(bytes[offset + 2]) << 16) | (static_cast(bytes[offset + 3]) << 24); } } // namespace u16 selectUsecodeClassId(const UsecodeItemIdentity& item, const UsecodeClassBases& bases) { if (item.isNpc && (item.status & 2) == 0) { return static_cast(item.itemNumber + bases.npc); } if (item.family == 4) { return static_cast((item.quality & 0xff) + bases.unknownEgg); } return static_cast(item.shape + bases.regularShape); } std::optional> loadUsecodeOwnerRows(const FlexArchive& archive) { auto rows = readFlexEntry(archive, 1); if (!rows || rows->size() % 13 != 0) { return std::nullopt; } return rows; } bool ownerRowAllowsEvent(const std::vector& ownerRows, u16 classId, u32 eventMask) { constexpr std::size_t rowSize = 13; const std::size_t row = static_cast(classId) * rowSize; if (eventMask == 0 || row > ownerRows.size() || ownerRows.size() - row < rowSize) { return false; } return (readDword(ownerRows, row) & eventMask) != 0; } std::optional loadUsecodeClass(const FlexArchive& archive, u32 classId) { if (classId >= archive.entryCount || archive.entryCount - classId <= 2) { return std::nullopt; } const auto names = loadUsecodeOwnerRows(archive); auto bytes = readFlexEntry(archive, classId + 2); if (!names || !bytes || bytes->size() < 20 || classId >= names->size() / 13) { return std::nullopt; } const std::size_t nameOffset = 4 + static_cast(classId) * 13; const u32 codeBase = readDword(*bytes, 8); if (codeBase < 20 || codeBase > bytes->size() || (codeBase - 20) % 6 != 0) { return std::nullopt; } std::string name; for (std::size_t index = 0; index < 9 && (*names)[nameOffset + index] != 0; ++index) { name.push_back(static_cast((*names)[nameOffset + index])); } return UsecodeClass{classId, std::move(name), codeBase, std::move(*bytes)}; } std::optional readUsecodeEventBody(const UsecodeClass& usecodeClass, u32 eventId) { if (usecodeClass.codeBase < 20 || usecodeClass.codeBase > usecodeClass.bytes.size() || eventId >= (usecodeClass.codeBase - 20) / 6) { return std::nullopt; } const std::size_t row = 20 + static_cast(eventId) * 6; const u16 length = readWord(usecodeClass.bytes, row); const u32 offset = readDword(usecodeClass.bytes, row + 2); if (length == 0 || offset == 0 || offset > usecodeClass.bytes.size() - usecodeClass.codeBase + 1) { return std::nullopt; } const std::size_t start = usecodeClass.codeBase + static_cast(offset) - 1; if (length > usecodeClass.bytes.size() - start) { return std::nullopt; } return UsecodeEventBody{length, offset, std::vector(usecodeClass.bytes.begin() + start, usecodeClass.bytes.begin() + start + length)}; } std::optional selectEligibleUsecodeEvent(const FlexArchive& archive, const std::vector& ownerRows, const UsecodeItemIdentity& item, const UsecodeClassBases& bases, u32 eventMask, u16 eventId) { const u16 classId = selectUsecodeClassId(item, bases); if (!ownerRowAllowsEvent(ownerRows, classId, eventMask)) { return std::nullopt; } const auto usecodeClass = loadUsecodeClass(archive, classId); const auto body = usecodeClass ? readUsecodeEventBody(*usecodeClass, eventId) : std::nullopt; if (!body) { return std::nullopt; } return EligibleUsecodeEvent{classId, usecodeClass->name, *body}; } } // namespace remorse