Refactor usecode task management and enhance teleportation logic

- Improved the queueUsecodeTask function to streamline process termination and context management.
- Added robust error handling for event context creation and memory writing.
- Enhanced the loadSceneAsync function to better handle avatar teleportation, including a new teleporter process.
- Expanded entity dispatch tests to cover movement and teleporter processes, ensuring correct behavior under various conditions.
- Introduced new tests for usecode runtime memory allocation and context creation, validating edge cases and error handling.
- Updated flex archive tests to ensure proper loading and unloading of usecode slot chunk caches.
This commit is contained in:
Maddo 2026-10-05 21:50:41 +02:00
commit 0771c86043
12 changed files with 1091 additions and 253 deletions

View file

@ -23,6 +23,26 @@ int main()
std::equal(prefix.begin(), prefix.end(), body.begin()) &&
body[remorse::usecodeEventBytecodeOffset] == 0x0b;
};
const auto readDescriptorPointer = [](const std::vector<remorse::u8>& descriptors,
std::size_t chunkIndex) {
const std::size_t offset = chunkIndex * 6 + 2;
return static_cast<remorse::u32>(descriptors[offset]) |
(static_cast<remorse::u32>(descriptors[offset + 1]) << 8) |
(static_cast<remorse::u32>(descriptors[offset + 2]) << 16) |
(static_cast<remorse::u32>(descriptors[offset + 3]) << 24);
};
remorse::UsecodeRuntimeMemory boundedMemory;
assert(!boundedMemory.allocate(0));
assert(!boundedMemory.allocate(0x10001));
const auto fullSegment = boundedMemory.allocate(0x10000, 0x5a);
assert(fullSegment);
const auto finalSegmentByte = boundedMemory.read({0xffff, fullSegment->segment}, 1);
assert(finalSegmentByte && finalSegmentByte->size() == 1 && finalSegmentByte->front() == 0x5a);
assert(!boundedMemory.read({0xffff, fullSegment->segment}, 2));
assert(!boundedMemory.read({0, 0xffff}, 1));
assert(boundedMemory.release(*fullSegment) && boundedMemory.allocationCount() == 0);
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
assert(root.has_value());
@ -66,6 +86,36 @@ int main()
[](remorse::u16 state) { return state == 0xffff; }));
assert(std::equal(npcTriggerOwnerBuffers->descriptors.begin(), npcTriggerOwnerBuffers->descriptors.end(),
npcTriggerClass->bytes.begin() + 20));
remorse::UsecodeSlotChunkCache npcTriggerChunkCache(*npcTriggerClass);
const auto originalNpcTriggerDescriptors = npcTriggerChunkCache.ownerBuffers().descriptors;
std::optional<remorse::u16> npcTriggerChunkIndex;
for (remorse::u16 chunkIndex = 0; chunkIndex < npcTriggerChunkCache.chunkCount(); ++chunkIndex) {
if (npcTriggerChunkCache.ensureLoaded(chunkIndex)) {
npcTriggerChunkIndex = chunkIndex;
break;
}
}
assert(npcTriggerChunkIndex);
const auto npcTriggerSourcePointer = readDescriptorPointer(
originalNpcTriggerDescriptors, *npcTriggerChunkIndex);
const auto* loadedNpcTriggerProcess = npcTriggerChunkCache.loadedProcess(*npcTriggerChunkIndex);
assert(loadedNpcTriggerProcess && loadedNpcTriggerProcess->layout.slotIndex == 0x363 &&
loadedNpcTriggerProcess->layout.chunkIndex == *npcTriggerChunkIndex &&
loadedNpcTriggerProcess->layout.savedSource.offset == static_cast<remorse::u16>(npcTriggerSourcePointer) &&
loadedNpcTriggerProcess->layout.savedSource.segment == static_cast<remorse::u16>(npcTriggerSourcePointer >> 16));
const auto loadedNpcTriggerBytes = npcTriggerChunkCache.runtimeMemory().read(
loadedNpcTriggerProcess->layout.chunkBuffer, loadedNpcTriggerProcess->body.length);
assert(loadedNpcTriggerBytes && *loadedNpcTriggerBytes == loadedNpcTriggerProcess->body.bytes &&
readDescriptorPointer(npcTriggerChunkCache.ownerBuffers().descriptors, *npcTriggerChunkIndex) ==
(static_cast<remorse::u32>(loadedNpcTriggerProcess->layout.chunkBuffer.offset) |
(static_cast<remorse::u32>(loadedNpcTriggerProcess->layout.chunkBuffer.segment) << 16)));
assert(npcTriggerChunkCache.release(*npcTriggerChunkIndex));
assert(npcTriggerChunkCache.tryUnload(*npcTriggerChunkIndex));
assert(npcTriggerChunkCache.loadedProcessCount() == 0 &&
npcTriggerChunkCache.runtimeMemory().allocationCount() == 0 &&
readDescriptorPointer(npcTriggerChunkCache.ownerBuffers().descriptors, *npcTriggerChunkIndex) ==
npcTriggerSourcePointer);
auto inconsistentOwnerHeader = *eventClass;
inconsistentOwnerHeader.extraSlotCount = 1;
assert(!remorse::initializeUsecodeSlotOwnerBuffers(inconsistentOwnerHeader));
@ -74,9 +124,24 @@ int main()
assert(eventBody->bytes.size() == 0x1fd6 && eventBody->bytes.front() == eventClass->bytes[0xd4]);
remorse::UsecodeSlotChunkCache eventChunkCache(*eventClass);
assert(eventChunkCache.chunkCount() == 32);
const auto sourceEventPointer = readDescriptorPointer(eventChunkCache.ownerBuffers().descriptors, 0x0a);
const auto* loadedEventChunk = eventChunkCache.ensureLoaded(0x0a);
assert(loadedEventChunk && loadedEventChunk->bytes == eventBody->bytes &&
eventChunkCache.inUseCount(0x0a) == 1);
const auto* loadedEventProcess = eventChunkCache.loadedProcess(0x0a);
assert(loadedEventProcess && loadedEventProcess->layout.vtableOffset == 0x673c &&
loadedEventProcess->layout.slotIndex == 0x361 && loadedEventProcess->layout.chunkIndex == 0x0a &&
loadedEventProcess->layout.savedSource.offset == static_cast<remorse::u16>(sourceEventPointer) &&
loadedEventProcess->layout.savedSource.segment == static_cast<remorse::u16>(sourceEventPointer >> 16));
const auto loadedEventBytes = eventChunkCache.runtimeMemory().read(
loadedEventProcess->layout.chunkBuffer, loadedEventProcess->body.length);
assert(loadedEventBytes && *loadedEventBytes == loadedEventProcess->body.bytes &&
eventChunkCache.loadedListHead().offset == loadedEventProcess->processAddress.offset &&
eventChunkCache.loadedListHead().segment == loadedEventProcess->processAddress.segment &&
eventChunkCache.loadedProcessCount() == 1 && eventChunkCache.runtimeMemory().allocationCount() == 2);
assert(readDescriptorPointer(eventChunkCache.ownerBuffers().descriptors, 0x0a) ==
(static_cast<remorse::u32>(loadedEventProcess->layout.chunkBuffer.offset) |
(static_cast<remorse::u32>(loadedEventProcess->layout.chunkBuffer.segment) << 16)));
const auto* reusedEventChunk = eventChunkCache.ensureLoaded(0x0a);
assert(reusedEventChunk == loadedEventChunk && eventChunkCache.inUseCount(0x0a) == 2);
assert(eventChunkCache.release(0x0a) && eventChunkCache.inUseCount(0x0a) == 1);
@ -85,6 +150,9 @@ int main()
assert(!eventChunkCache.release(0x0a));
assert(eventChunkCache.tryUnload(0x0a));
assert(eventChunkCache.ownerBuffers().chunkStates[0x0a] == 0xffff);
assert(eventChunkCache.loadedProcessCount() == 0 && eventChunkCache.runtimeMemory().allocationCount() == 0 &&
eventChunkCache.loadedListHead().offset == 0 && eventChunkCache.loadedListHead().segment == 0 &&
readDescriptorPointer(eventChunkCache.ownerBuffers().descriptors, 0x0a) == sourceEventPointer);
const auto* reloadedEventChunk = eventChunkCache.ensureLoaded(0x0a);
assert(reloadedEventChunk && reloadedEventChunk->bytes == eventBody->bytes &&
eventChunkCache.inUseCount(0x0a) == 1);
@ -95,6 +163,49 @@ int main()
assert(eventChunkCache.inUseCount(0x0a) == 29999);
assert(eventChunkCache.ensureLoaded(0x0a) == nullptr && eventChunkCache.inUseCount(0x0a) == 29999);
assert(eventChunkCache.ensureLoaded(0x20) == nullptr && eventChunkCache.inUseCount(0x20) == 0);
remorse::UsecodeClass syntheticOwnerClass;
syntheticOwnerClass.classId = 0x123;
syntheticOwnerClass.name = "SYNTHETIC";
syntheticOwnerClass.codeBase = 0xd4;
syntheticOwnerClass.bytes.resize(syntheticOwnerClass.codeBase + 2);
syntheticOwnerClass.bytes[20] = 1;
syntheticOwnerClass.bytes[22] = 1;
syntheticOwnerClass.bytes[26] = 1;
syntheticOwnerClass.bytes[28] = 2;
syntheticOwnerClass.bytes[syntheticOwnerClass.codeBase] = 0xaa;
syntheticOwnerClass.bytes[syntheticOwnerClass.codeBase + 1] = 0xbb;
remorse::UsecodeSlotChunkCache syntheticChunkCache(std::move(syntheticOwnerClass));
const auto syntheticSourceDescriptors = syntheticChunkCache.ownerBuffers().descriptors;
const auto* firstSyntheticBody = syntheticChunkCache.ensureLoaded(0);
const auto* secondSyntheticBody = syntheticChunkCache.ensureLoaded(1);
assert(firstSyntheticBody && firstSyntheticBody->bytes == std::vector<remorse::u8>{0xaa} &&
secondSyntheticBody && secondSyntheticBody->bytes == std::vector<remorse::u8>{0xbb});
const auto* firstSyntheticProcess = syntheticChunkCache.loadedProcess(0);
const auto* secondSyntheticProcess = syntheticChunkCache.loadedProcess(1);
assert(firstSyntheticProcess && secondSyntheticProcess &&
secondSyntheticProcess->layout.previous.offset == firstSyntheticProcess->processAddress.offset &&
secondSyntheticProcess->layout.previous.segment == firstSyntheticProcess->processAddress.segment &&
firstSyntheticProcess->layout.next.offset == secondSyntheticProcess->processAddress.offset &&
firstSyntheticProcess->layout.next.segment == secondSyntheticProcess->processAddress.segment &&
syntheticChunkCache.loadedListHead().offset == secondSyntheticProcess->processAddress.offset &&
syntheticChunkCache.loadedListHead().segment == secondSyntheticProcess->processAddress.segment);
const auto firstSyntheticSourcePointer = readDescriptorPointer(
syntheticSourceDescriptors, 0);
const auto secondSyntheticSourcePointer = readDescriptorPointer(
syntheticSourceDescriptors, 1);
assert(syntheticChunkCache.release(0) && syntheticChunkCache.tryUnload(0));
assert(syntheticChunkCache.loadedProcessCount() == 1 &&
syntheticChunkCache.loadedListHead().offset == secondSyntheticProcess->processAddress.offset &&
syntheticChunkCache.loadedListHead().segment == secondSyntheticProcess->processAddress.segment &&
syntheticChunkCache.loadedProcess(1)->layout.previous.offset == 0 &&
syntheticChunkCache.loadedProcess(1)->layout.previous.segment == 0 &&
readDescriptorPointer(syntheticChunkCache.ownerBuffers().descriptors, 0) == firstSyntheticSourcePointer);
assert(syntheticChunkCache.release(1) && syntheticChunkCache.tryUnload(1));
assert(syntheticChunkCache.loadedProcessCount() == 0 && syntheticChunkCache.runtimeMemory().allocationCount() == 0 &&
syntheticChunkCache.loadedListHead().offset == 0 && syntheticChunkCache.loadedListHead().segment == 0 &&
readDescriptorPointer(syntheticChunkCache.ownerBuffers().descriptors, 1) == secondSyntheticSourcePointer);
assert(!remorse::readUsecodeEventBody(*eventClass, 0x20));
assert(!remorse::loadUsecodeClass(*usecode, usecode->entryCount));
auto truncated = *eventClass;
@ -132,6 +243,48 @@ int main()
assert(stagedItems && !stagedItems->empty());
const auto spawnEgg = remorse::findTeleportEgg(*stagedItems, *typeFlags, 0x1e);
assert(spawnEgg);
remorse::LiveFixedMapItems spawnSearchWorld(*stagedItems);
assert(spawnSearchWorld.initializeAvatar());
remorse::ProcessKernel teleporterKernel;
remorse::u16 teleporterGroups[32]{};
for (int reservedSlot = 0; reservedSlot < 3; ++reservedSlot) {
teleporterKernel.createProcess(1, teleporterGroups);
}
auto* teleporterProcess = teleporterKernel.createTeleporterProcess(1, 1, 0x1e, teleporterGroups);
const remorse::u16 teleporterProcessId = static_cast<remorse::u16>(
teleporterProcess->base.bytes[2] | (static_cast<remorse::u16>(teleporterProcess->base.bytes[3]) << 8));
assert(!remorse::TeleporterProcess::run(*teleporterProcess, spawnSearchWorld, 2, *typeFlags));
assert(spawnSearchWorld.findItem(1)->data.x == 0 && spawnSearchWorld.findItem(1)->data.y == 0);
assert(remorse::TeleporterProcess::run(*teleporterProcess, spawnSearchWorld, 1, *typeFlags));
teleporterProcess->base.bytes[0x18] |= 4;
teleporterKernel.terminateProcess(teleporterProcessId, teleporterGroups);
assert(teleporterKernel.processAt(teleporterProcessId) == nullptr &&
teleporterKernel.allocationSizeAt(teleporterProcessId) == 0 && teleporterGroups[1] == 3);
assert(spawnEgg->recordIndex < fixedMap->entries.size());
const auto& startingEggRecord = fixedMap->entries[spawnEgg->recordIndex];
const auto* spawnedAvatar = spawnSearchWorld.findItem(1);
assert(spawnEgg->data.x == startingEggRecord.x / 2 &&
spawnEgg->data.y == startingEggRecord.y / 2 && spawnEgg->data.z == startingEggRecord.z);
assert(spawnedAvatar && spawnedAvatar->itemNumber == 1 &&
spawnedAvatar->data.x == spawnEgg->data.x && spawnedAvatar->data.y == spawnEgg->data.y &&
spawnedAvatar->data.z == spawnEgg->data.z && spawnedAvatar->data.mapNumber == 1);
const auto spawnUseCandidates = remorse::itemGrabSearchCandidates(
*spawnedAvatar, spawnSearchWorld.mapItems(), *typeFlags);
assert(spawnUseCandidates);
unsigned eligibleSpawnUses = 0;
for (const auto& candidate : *spawnUseCandidates) {
const auto* item = spawnSearchWorld.findItem(candidate.itemNumber);
if (!item || item->data.shape >= typeFlags->size()) continue;
const auto& shape = (*typeFlags)[item->data.shape];
const remorse::UsecodeItemIdentity spawnIdentity{item->itemNumber, item->data.shape,
item->data.flags, item->data.quality, shape.family, false};
const auto eligible = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
spawnIdentity, remorse::UsecodeClassBases{0, 0x800, 0x900}, 0x02, 1);
if (eligible) {
++eligibleSpawnUses;
}
}
assert(eligibleSpawnUses == 0);
unsigned eligibleMapUseItems = 0;
unsigned keycardUseItems = 0;
std::optional<remorse::StagedFixedMapItem> boxItem;
@ -448,9 +601,46 @@ int main()
0x5a, 0x02, 0x5c, 0xd6, 0x01, 'B', 'O', 'X', '_', 'N', 'S', 0x00, 0x00, 0x00};
assert(hasRetailEventEntry(boxUse->bytes, boxPrefix));
remorse::LiveFixedMapItems boxWorld(*stagedItems);
auto boxTask = remorse::UsecodeProcessTask::create(0x0340, 0x0081, 1,
boxItem->itemNumber, boxUse->bytes);
assert(boxTask);
const auto& boxShape = (*typeFlags)[boxItem->data.shape];
const remorse::u8 boxHeight = boxShape.z == 0 && (boxShape.flags & 1) == 0
? 1 : static_cast<remorse::u8>(boxShape.z * 8);
assert(static_cast<unsigned>(boxItem->data.z) + boxHeight <= 0xff);
remorse::StagedFixedMapItem boxSearchOrigin;
boxSearchOrigin.itemNumber = 1;
boxSearchOrigin.data.shape = 1;
boxSearchOrigin.data.x = boxItem->data.x;
boxSearchOrigin.data.y = boxItem->data.y;
boxSearchOrigin.data.z = static_cast<remorse::u8>(boxItem->data.z + boxHeight);
const auto boxCandidates = remorse::itemGrabSearchCandidates(boxSearchOrigin,
boxWorld.mapItems(), *typeFlags);
assert(boxCandidates);
const auto boxCandidate = std::find_if(boxCandidates->begin(), boxCandidates->end(),
[boxItem](const auto& candidate) { return candidate.itemNumber == boxItem->itemNumber; });
assert(boxCandidate != boxCandidates->end());
const auto* selectedBoxItem = boxWorld.findItem(boxCandidate->itemNumber);
assert(selectedBoxItem && selectedBoxItem->data.shape < typeFlags->size());
const remorse::UsecodeItemIdentity boxIdentity{selectedBoxItem->itemNumber, selectedBoxItem->data.shape,
selectedBoxItem->data.flags, selectedBoxItem->data.quality,
(*typeFlags)[selectedBoxItem->data.shape].family, false};
const auto selectedBoxEvent = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
boxIdentity, retailBases, 0x02, 1);
assert(selectedBoxEvent && selectedBoxEvent->classId == 0x0081 &&
selectedBoxEvent->className == "BOX_NS" && selectedBoxEvent->body.bytes == boxUse->bytes);
remorse::UsecodeTaskRegistry boxTasks;
auto boxContext = remorse::createUsecodeEventContext(boxTasks.runtimeMemory(), *retailOwnerRows,
selectedBoxEvent->classId, 1, boxItem->itemNumber, selectedBoxEvent->body.bytes, 2);
assert(boxContext && boxContext->context.interpreter.callFrameCount == 1 &&
boxContext->ownedRegions.size() == 3 && boxTasks.runtimeMemory().allocationCount() == 5);
const std::vector<remorse::u8> boxProcessIdBytes{0x40, 0x03};
assert(boxTasks.runtimeMemory().write(boxContext->processIdOutput, boxProcessIdBytes));
auto boxTaskValue = remorse::UsecodeProcessTask::create(0x0340, selectedBoxEvent->classId, 1,
boxItem->itemNumber, selectedBoxEvent->body.bytes, 2);
assert(boxTaskValue && boxTasks.addWithContext(std::move(*boxTaskValue), std::move(*boxContext)));
auto* boxTask = boxTasks.find(0x0340);
const auto* registeredBoxContext = boxTasks.context(0x0340);
assert(boxTask && registeredBoxContext && boxTask->sourceItem == boxItem->itemNumber &&
boxTask->interpreter.callFrameCount == 1 && registeredBoxContext->context.processBytes[
remorse::usecodeContextSlotIndexOffset] == 0x81);
const auto freeClass = remorse::loadUsecodeClass(*usecode, 0x0a0c);
const auto freeTimerBody = freeClass ? remorse::readUsecodeEventBody(*freeClass, 0x32) : std::nullopt;
auto boxRenderer = remorse::loadMapSceneRenderer(*root, 1);
@ -540,6 +730,12 @@ int main()
[](const auto& left, const auto& right) {
return left.red == right.red && left.green == right.green && left.blue == right.blue;
}));
const auto boxProcessIdOutput = boxTasks.runtimeMemory().read(registeredBoxContext->processIdOutput,
sizeof(remorse::u16));
assert(boxProcessIdOutput && (*boxProcessIdOutput)[0] == 0x40 &&
(*boxProcessIdOutput)[1] == 0x03 && boxTasks.runtimeMemory().allocationCount() == 5);
assert(boxTasks.remove(0x0340) && boxTasks.size() == 0 && boxTasks.context(0x0340) == nullptr &&
boxTasks.runtimeMemory().allocationCount() == 0);
assert(!remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
rechargeItem, retailBases, 0, 9));
std::vector<remorse::u8> ownerRows(26);