- Enhance `entity_dispatch_entry_tests.cpp` with additional assertions for event type checks, group ID increments, and error handling. - Refactor `file_system_tests.cpp` to streamline probe execution and enhance output validation. - Extend `fixed_map_tests.cpp` to cover retail map loading, item classification, and process creation scenarios. - Introduce new checks in `flex_archive_tests.cpp` for usecode loading and event handling, ensuring robust validation of game data. - Add tests in `isometric_controls_tests.cpp` for controller direction mapping based on scan codes. - Implement `test_crash_setup.cpp` to initialize crash diagnostics for improved error handling during tests.
109 lines
No EOL
4 KiB
C++
109 lines
No EOL
4 KiB
C++
#include "remorse/usecode_archive.h"
|
|
|
|
#include <utility>
|
|
|
|
namespace remorse {
|
|
namespace {
|
|
|
|
u16 readWord(const std::vector<u8>& bytes, std::size_t offset)
|
|
{
|
|
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
|
}
|
|
|
|
u32 readDword(const std::vector<u8>& bytes, std::size_t offset)
|
|
{
|
|
return static_cast<u32>(bytes[offset]) | (static_cast<u32>(bytes[offset + 1]) << 8) |
|
|
(static_cast<u32>(bytes[offset + 2]) << 16) | (static_cast<u32>(bytes[offset + 3]) << 24);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
u16 selectUsecodeClassId(const UsecodeItemIdentity& item, const UsecodeClassBases& bases)
|
|
{
|
|
if (item.isNpc && (item.status & 2) == 0) {
|
|
return static_cast<u16>(item.itemNumber + bases.npc);
|
|
}
|
|
if (item.family == 4) {
|
|
return static_cast<u16>((item.quality & 0xff) + bases.unknownEgg);
|
|
}
|
|
return static_cast<u16>(item.shape + bases.regularShape);
|
|
}
|
|
|
|
std::optional<std::vector<u8>> 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<u8>& ownerRows, u16 classId, u32 eventMask)
|
|
{
|
|
constexpr std::size_t rowSize = 13;
|
|
const std::size_t row = static_cast<std::size_t>(classId) * rowSize;
|
|
if (eventMask == 0 || row > ownerRows.size() || ownerRows.size() - row < rowSize) {
|
|
return false;
|
|
}
|
|
return (readDword(ownerRows, row) & eventMask) != 0;
|
|
}
|
|
|
|
std::optional<UsecodeClass> 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<std::size_t>(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<char>((*names)[nameOffset + index]));
|
|
}
|
|
return UsecodeClass{classId, std::move(name), codeBase, std::move(*bytes)};
|
|
}
|
|
|
|
std::optional<UsecodeEventBody> 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<std::size_t>(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<std::size_t>(offset) - 1;
|
|
if (length > usecodeClass.bytes.size() - start) {
|
|
return std::nullopt;
|
|
}
|
|
return UsecodeEventBody{length, offset,
|
|
std::vector<u8>(usecodeClass.bytes.begin() + start, usecodeClass.bytes.begin() + start + length)};
|
|
}
|
|
|
|
std::optional<EligibleUsecodeEvent> selectEligibleUsecodeEvent(const FlexArchive& archive,
|
|
const std::vector<u8>& 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
|