Add comprehensive tests for entity dispatch, file system, fixed map, flex archive, isometric controls, and crash setup
- 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.
This commit is contained in:
parent
202679f857
commit
c346b0e1a7
17 changed files with 1207 additions and 92 deletions
|
|
@ -61,6 +61,9 @@ EntityDispatchEntryBase* ProcessKernel::createProcess(u8 activeGroup, u16 (&grou
|
|||
|
||||
StandProcessEntry* ProcessKernel::createStandProcess(u8 activeGroup, u16 npcNumber, u16 (&groupCounts)[32])
|
||||
{
|
||||
if (npcNumber > maxTrackedItemNumber) {
|
||||
throw std::out_of_range("NPC item number exceeds process lookup tables");
|
||||
}
|
||||
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
||||
throw std::overflow_error("process table exhausted");
|
||||
}
|
||||
|
|
@ -102,6 +105,28 @@ StandProcessEntry* ProcessKernel::createStandProcess(u8 activeGroup, u16 npcNumb
|
|||
return result;
|
||||
}
|
||||
|
||||
EntityDispatchEntryBase* ProcessKernel::createItemProcess(u8 activeGroup, u16 itemNumber, u16 (&groupCounts)[32])
|
||||
{
|
||||
if (itemNumber == 0) {
|
||||
throw std::invalid_argument("item process needs an item number");
|
||||
}
|
||||
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
||||
throw std::overflow_error("process table exhausted");
|
||||
}
|
||||
ownedProcesses_.reserve(ownedProcesses_.size() + 1);
|
||||
auto process = std::make_unique<EntityDispatchEntryBase>();
|
||||
EntityDispatchEntry::initBase(process.get(), activeGroup, groupCounts);
|
||||
writeLe16(*process, 0x00, ItemProcess::vtableOffset);
|
||||
setProcessSourceType(*process, itemNumber);
|
||||
setProcessEventTypeChecked(*process, 0xf1);
|
||||
setLastAllocationSize(sizeof(EntityDispatchEntryBase));
|
||||
const u16 id = addProcess(*process);
|
||||
processNames_[id] = "ItemProcess";
|
||||
EntityDispatchEntryBase* result = process.get();
|
||||
ownedProcesses_.push_back(std::move(process));
|
||||
return result;
|
||||
}
|
||||
|
||||
SystemTimerProcessEntry* ProcessKernel::createSystemTimerProcess(u8 activeGroup, u16 (&groupCounts)[32])
|
||||
{
|
||||
if (systemTimerProcess_ != nullptr) {
|
||||
|
|
@ -195,6 +220,45 @@ void ProcessKernel::setMarkNewProcesses(bool enabled)
|
|||
markNewProcesses_ = enabled;
|
||||
}
|
||||
|
||||
void ProcessKernel::setActiveGroup(u8 groupId)
|
||||
{
|
||||
if (groupId == 0 || groupId > 31) {
|
||||
throw std::invalid_argument("active process group must be in 1..31");
|
||||
}
|
||||
if (activeGroup_ != groupId) {
|
||||
activeGroup_ = groupId;
|
||||
groupSelectionPending_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
u8 ProcessKernel::activeGroup() const
|
||||
{
|
||||
return activeGroup_;
|
||||
}
|
||||
|
||||
bool ProcessKernel::groupSelectionPending() const
|
||||
{
|
||||
return groupSelectionPending_;
|
||||
}
|
||||
|
||||
void ProcessKernel::refreshActiveGroup(const u16 (&groupCounts)[32])
|
||||
{
|
||||
if (groupCounts[activeGroup_] != 0) {
|
||||
return;
|
||||
}
|
||||
u8 highestGroup = 1;
|
||||
for (const EntityDispatchEntryBase* entry : processes_) {
|
||||
if (entry != nullptr) {
|
||||
const u8 groupId = entry->bytes[0x08] & 0x1f;
|
||||
if (groupId > highestGroup && groupId != 30 && groupId != 31) {
|
||||
highestGroup = groupId;
|
||||
}
|
||||
}
|
||||
}
|
||||
setActiveGroup(highestGroup);
|
||||
groupSelectionPending_ = false;
|
||||
}
|
||||
|
||||
void ProcessKernel::setLastAllocationSize(u16 size)
|
||||
{
|
||||
lastAllocationSize_ = size;
|
||||
|
|
@ -260,6 +324,7 @@ void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
|
|||
throw std::out_of_range("process ID is not addressable");
|
||||
}
|
||||
const u16 itemNumber = readLe16(*entry, 0x04);
|
||||
const u16 eventType = readLe16(*entry, 0x06);
|
||||
if (systemTimerProcess_ != nullptr && entry == &systemTimerProcess_->base) {
|
||||
throw std::logic_error("system timer teardown is not ported");
|
||||
}
|
||||
|
|
@ -271,7 +336,10 @@ void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
|
|||
}
|
||||
const auto stand = std::find_if(ownedStandProcesses_.begin(), ownedStandProcesses_.end(),
|
||||
[entry](const auto& process) { return &process->base == entry; });
|
||||
if (itemNumber != 0 && (stand == ownedStandProcesses_.end() || id < 3 ||
|
||||
const bool ownedItemProcess = readLe16(*entry, 0x00) == ItemProcess::vtableOffset &&
|
||||
std::any_of(ownedProcesses_.begin(), ownedProcesses_.end(),
|
||||
[entry](const auto& process) { return process.get() == entry; });
|
||||
if (itemNumber != 0 && (stand == ownedStandProcesses_.end() && !ownedItemProcess || id < 3 ||
|
||||
readLe16(*entry, 0x24) != 0 || readLe16(*entry, 0x26) != 0 ||
|
||||
readLe16(*entry, 0x2e) != 0 || readLe16(*entry, 0x30) != 0)) {
|
||||
throw std::logic_error("item process child cleanup is not ported");
|
||||
|
|
@ -292,15 +360,92 @@ void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
|
|||
++terminatedProcessCount_;
|
||||
}
|
||||
--processCount_;
|
||||
refreshItemPresence(itemNumber);
|
||||
refreshItemProcessType(itemNumber, eventType);
|
||||
}
|
||||
|
||||
void ProcessKernel::refreshItemPresence(u16 itemNumber)
|
||||
{
|
||||
if (itemNumber == 0) {
|
||||
return;
|
||||
}
|
||||
const bool present = processes_.size() > 3 && std::any_of(processes_.begin() + 3, processes_.end(),
|
||||
[itemNumber](const EntityDispatchEntryBase* process) {
|
||||
return process != nullptr && readLe16(*process, 0x04) == itemNumber &&
|
||||
(readLe16(*process, 0x16) & 0x08) == 0;
|
||||
});
|
||||
const std::size_t itemByte = itemNumber >> 3;
|
||||
if (itemProcessPresence_.size() <= itemByte) {
|
||||
itemProcessPresence_.resize(itemByte + 1, 0);
|
||||
}
|
||||
if (present) {
|
||||
itemProcessPresence_[itemByte] |= static_cast<u8>(1u << (itemNumber & 7));
|
||||
} else {
|
||||
itemProcessPresence_[itemByte] &= static_cast<u8>(~(1u << (itemNumber & 7)));
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessKernel::refreshItemProcessType(u16 itemNumber, u16 eventType)
|
||||
{
|
||||
if (itemNumber == 0 || eventType < 0xf0 || eventType > 0xf7) {
|
||||
return;
|
||||
}
|
||||
const bool present = processes_.size() > 3 && std::any_of(processes_.begin() + 3, processes_.end(),
|
||||
[itemNumber, eventType](const EntityDispatchEntryBase* process) {
|
||||
return process != nullptr && readLe16(*process, 0x04) == itemNumber &&
|
||||
readLe16(*process, 0x06) == eventType && (readLe16(*process, 0x16) & 0x08) == 0;
|
||||
});
|
||||
if (itemProcessTypes_.size() <= itemNumber) {
|
||||
itemProcessTypes_.resize(static_cast<std::size_t>(itemNumber) + 1, 0);
|
||||
}
|
||||
const u8 bit = static_cast<u8>(1u << (eventType - 0xf0));
|
||||
if (present) {
|
||||
itemProcessTypes_[itemNumber] |= bit;
|
||||
} else {
|
||||
itemProcessTypes_[itemNumber] &= static_cast<u8>(~bit);
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessKernel::setProcessSourceType(EntityDispatchEntryBase& entry, u16 itemNumber)
|
||||
{
|
||||
if (itemNumber > maxTrackedItemNumber) {
|
||||
throw std::out_of_range("item number exceeds process lookup tables");
|
||||
}
|
||||
const u16 previousItem = readLe16(entry, 0x04);
|
||||
const u16 eventType = readLe16(entry, 0x06);
|
||||
EntityDispatchEntry::setSourceType(entry, itemNumber);
|
||||
if (itemNumber != 0) {
|
||||
const bool stillPresent = std::any_of(processes_.begin() + 3, processes_.end(),
|
||||
[itemNumber](const EntityDispatchEntryBase* process) {
|
||||
return process != nullptr && readLe16(*process, 0x04) == itemNumber &&
|
||||
(readLe16(*process, 0x16) & 0x08) == 0;
|
||||
});
|
||||
if (!stillPresent) {
|
||||
itemProcessPresence_[itemNumber >> 3] &= static_cast<u8>(~(1u << (itemNumber & 7)));
|
||||
const std::size_t itemByte = itemNumber >> 3;
|
||||
if (itemProcessPresence_.size() <= itemByte) {
|
||||
itemProcessPresence_.resize(itemByte + 1, 0);
|
||||
}
|
||||
itemProcessPresence_[itemByte] |= static_cast<u8>(1u << (itemNumber & 7));
|
||||
if (eventType >= 0xf0 && eventType <= 0xf7) {
|
||||
if (itemProcessTypes_.size() <= itemNumber) {
|
||||
itemProcessTypes_.resize(static_cast<std::size_t>(itemNumber) + 1, 0);
|
||||
}
|
||||
itemProcessTypes_[itemNumber] |= static_cast<u8>(1u << (eventType - 0xf0));
|
||||
}
|
||||
}
|
||||
if (previousItem != 0 && previousItem != itemNumber) {
|
||||
refreshItemPresence(previousItem);
|
||||
refreshItemProcessType(previousItem, eventType);
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessKernel::setProcessEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType)
|
||||
{
|
||||
const u16 previousType = readLe16(entry, 0x06);
|
||||
const u16 itemNumber = readLe16(entry, 0x04);
|
||||
EntityDispatchEntry::setEventTypeChecked(entry, eventType);
|
||||
if (itemNumber != 0 && eventType >= 0xf0 && eventType <= 0xf7) {
|
||||
if (itemProcessTypes_.size() <= itemNumber) {
|
||||
itemProcessTypes_.resize(static_cast<std::size_t>(itemNumber) + 1, 0);
|
||||
}
|
||||
itemProcessTypes_[itemNumber] |= static_cast<u8>(1u << (eventType - 0xf0));
|
||||
}
|
||||
if (previousType != eventType) {
|
||||
refreshItemProcessType(itemNumber, previousType);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -324,6 +469,7 @@ void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDisp
|
|||
if (run == nullptr) {
|
||||
throw std::invalid_argument("process run slot is required");
|
||||
}
|
||||
setActiveGroup(activeGroup);
|
||||
const bool priorSyncEnabled = syncEnabled_;
|
||||
const u16 priorProcessId = currentProcessId_;
|
||||
syncEnabled_ = true;
|
||||
|
|
@ -353,7 +499,7 @@ void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDisp
|
|||
const u16 flags = readLe16(*entry, 0x16);
|
||||
if ((readLe16(*entry, 0x18) & 0x1000) == 0 && readLe16(*entry, 0x24) == 0 &&
|
||||
readLe16(*entry, 0x26) == 0 && (flags & 1) == 0 &&
|
||||
((flags & 2) != 0 || groupId == activeGroup || groupId == 30 || groupId == 31)) {
|
||||
((flags & 2) != 0 || groupId == activeGroup_ || groupId == 30 || groupId == 31)) {
|
||||
currentProcessId_ = static_cast<u16>(id);
|
||||
run(*entry, context);
|
||||
currentProcessId_ = 0;
|
||||
|
|
@ -371,6 +517,17 @@ void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDisp
|
|||
currentProcessId_ = priorProcessId;
|
||||
}
|
||||
|
||||
u32 ProcessKernel::runTimedSchedulerPass(u8 activeGroup, void (*run)(EntityDispatchEntryBase&, void*), void* context,
|
||||
void (*afterScan)(void*), void* afterContext)
|
||||
{
|
||||
const u32 startTick = systemTimerTick();
|
||||
scanRunnableProcesses(activeGroup, run, context, startTick);
|
||||
if (afterScan != nullptr) {
|
||||
afterScan(afterContext);
|
||||
}
|
||||
return systemTimerTick() - startTick;
|
||||
}
|
||||
|
||||
EntityDispatchEntryBase* ProcessKernel::processAt(u16 id) const
|
||||
{
|
||||
return id < processes_.size() ? processes_[id] : nullptr;
|
||||
|
|
@ -393,6 +550,11 @@ bool ProcessKernel::itemHasProcess(u16 itemNumber) const
|
|||
(itemProcessPresence_[itemByte] & (1u << (itemNumber & 7))) != 0;
|
||||
}
|
||||
|
||||
u8 ProcessKernel::itemProcessTypes(u16 itemNumber) const
|
||||
{
|
||||
return itemNumber < itemProcessTypes_.size() ? itemProcessTypes_[itemNumber] : 0;
|
||||
}
|
||||
|
||||
u16 ProcessKernel::processCount() const
|
||||
{
|
||||
return processCount_;
|
||||
|
|
@ -449,9 +611,15 @@ void EntityDispatchEntry::setGroupId(EntityDispatchEntryBase& entry, u8 groupId,
|
|||
++groupCounts[groupId];
|
||||
}
|
||||
|
||||
void EntityDispatchEntry::incrementGroupId(EntityDispatchEntryBase& entry)
|
||||
void EntityDispatchEntry::incrementGroupId(EntityDispatchEntryBase& entry, ProcessKernel& kernel, u16 (&groupCounts)[32])
|
||||
{
|
||||
++entry.bytes[groupIdOffset];
|
||||
const u8 previousGroup = entry.bytes[groupIdOffset] & 0x1f;
|
||||
const u8 nextGroup = static_cast<u8>(previousGroup + 1);
|
||||
if (nextGroup >= 30 || kernel.activeGroup() != previousGroup) {
|
||||
throw std::invalid_argument("process group cannot advance from the current active group");
|
||||
}
|
||||
setGroupId(entry, nextGroup, groupCounts);
|
||||
kernel.setActiveGroup(nextGroup);
|
||||
}
|
||||
|
||||
void EntityDispatchEntry::setSourceType(EntityDispatchEntryBase& entry, u16 sourceType)
|
||||
|
|
@ -462,6 +630,9 @@ void EntityDispatchEntry::setSourceType(EntityDispatchEntryBase& entry, u16 sour
|
|||
void EntityDispatchEntry::setEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType)
|
||||
{
|
||||
writeLe16(entry, eventTypeOffset, eventType);
|
||||
if (eventType > 0x0fff) {
|
||||
throw std::invalid_argument("process event type exceeds 0x0fff");
|
||||
}
|
||||
}
|
||||
|
||||
void EntityDispatchEntry::sleep(EntityDispatchEntryBase& entry, u32 timerTick, u32 duration)
|
||||
|
|
@ -511,6 +682,17 @@ void StandProcess::run(StandProcessEntry& entry, const NpcBehaviorServices& serv
|
|||
}, &started, timerTick);
|
||||
}
|
||||
|
||||
void ItemProcess::run(EntityDispatchEntryBase& entry, ProcessKernel& kernel, u16 (&groupCounts)[32],
|
||||
void (*release)(u16 itemNumber, u32 eventMask, u16 eventId, void*), void* context)
|
||||
{
|
||||
if (release == nullptr) {
|
||||
throw std::invalid_argument("item release boundary is required");
|
||||
}
|
||||
const u16 itemNumber = readLe16(entry, 0x04);
|
||||
release(itemNumber, 0x200, 9, context);
|
||||
kernel.terminateProcess(readLe16(entry, 0x02), groupCounts);
|
||||
}
|
||||
|
||||
u32 SystemTimerProcess::tick(const SystemTimerProcessEntry& entry)
|
||||
{
|
||||
return static_cast<u32>(entry.extra[0x0e] | (static_cast<u16>(entry.extra[0x0f]) << 8)) |
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue