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

@ -156,21 +156,40 @@ std::optional<remorse::u16> queueUsecodeTask(AppState& state, remorse::u16 class
state.diagnosticKernel.activeGroup(), state.diagnosticGroupCounts);
const remorse::u16 processId = static_cast<remorse::u16>(process->base.bytes[2] |
(static_cast<remorse::u16>(process->base.bytes[3]) << 8));
const auto terminateProcess = [&state, processId]() {
state.diagnosticKernel.terminateProcess(processId, state.diagnosticGroupCounts);
state.diagnosticKernel.clearTerminatedProcesses();
};
auto eventContext = remorse::createUsecodeEventContext(state.usecodeTasks.runtimeMemory(),
state.usecodeOwnerRows, classId, eventSlot, sourceItem, code, itemArgumentSize);
if (!eventContext) {
terminateProcess();
return std::nullopt;
}
const std::vector<remorse::u8> processIdBytes{
static_cast<remorse::u8>(processId), static_cast<remorse::u8>(processId >> 8)};
if (!state.usecodeTasks.runtimeMemory().write(eventContext->processIdOutput, processIdBytes)) {
remorse::destroyUsecodeEventContext(state.usecodeTasks.runtimeMemory(), *eventContext);
terminateProcess();
return std::nullopt;
}
auto task = remorse::UsecodeProcessTask::create(processId, classId, eventSlot,
sourceItem, std::move(code), itemArgumentSize, std::move(extraArguments));
if (!task) {
state.diagnosticKernel.terminateProcess(processId, state.diagnosticGroupCounts);
state.diagnosticKernel.clearTerminatedProcesses();
if (!task || !state.usecodeTasks.addWithContext(std::move(*task), std::move(*eventContext))) {
terminateProcess();
return std::nullopt;
}
const remorse::UsecodeContextFactoryFields factoryFields{{0, 0}, itemArgumentSize,
classId, eventSlot, task->interpreter.contextParameterOffset.value_or(0)};
if (!remorse::writeUsecodeContextFactoryFields(reinterpret_cast<remorse::u8*>(process),
sizeof(*process), factoryFields) || !state.usecodeTasks.add(std::move(*task))) {
state.diagnosticKernel.terminateProcess(processId, state.diagnosticGroupCounts);
state.diagnosticKernel.clearTerminatedProcesses();
const auto* registeredContext = state.usecodeTasks.context(processId);
if (registeredContext == nullptr) {
state.usecodeTasks.remove(processId);
terminateProcess();
return std::nullopt;
}
std::copy(registeredContext->context.processBytes.begin() + remorse::usecodeContextSlotIndexOffset,
registeredContext->context.processBytes.end(),
reinterpret_cast<remorse::u8*>(process) + remorse::usecodeContextSlotIndexOffset);
return processId;
}
@ -501,12 +520,32 @@ void loadSceneAsync(HWND window, AppState& state)
options.viewCenter = spawn;
remorse::LiveFixedMapItems liveItems(std::move(*stagedItems), std::move(containedItems));
liveItems.initializeAvatar();
remorse::StagedFixedMapItem diagnosticStart;
diagnosticStart.data.x = static_cast<remorse::u16>(spawn.x / 2);
diagnosticStart.data.y = static_cast<remorse::u16>(spawn.y / 2);
diagnosticStart.data.z = static_cast<remorse::u8>(spawn.z);
liveItems.teleportAvatar(startingEgg ? *startingEgg : diagnosticStart,
static_cast<remorse::u8>(state.mapIndex));
if (startingEgg) {
remorse::ProcessKernel teleporterKernel;
remorse::u16 teleporterGroupCounts[32]{};
for (int reservedSlot = 0; reservedSlot < 3; ++reservedSlot) {
teleporterKernel.createProcess(1, teleporterGroupCounts);
}
auto* teleporter = teleporterKernel.createTeleporterProcess(1,
static_cast<remorse::u16>(state.mapIndex), 0x1e, teleporterGroupCounts);
const remorse::u16 processId = static_cast<remorse::u16>(teleporter->base.bytes[2] |
(static_cast<remorse::u16>(teleporter->base.bytes[3]) << 8));
const bool teleported = remorse::TeleporterProcess::run(*teleporter, liveItems,
static_cast<remorse::u16>(state.mapIndex), *shapeTypes);
teleporter->base.bytes[0x18] |= 4;
teleporterKernel.terminateProcess(processId, teleporterGroupCounts);
if (!teleported) {
setLoadFailure(state, L"The teleporter process could not resolve its map and egg destination.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
}
} else {
remorse::StagedFixedMapItem diagnosticStart;
diagnosticStart.data.x = static_cast<remorse::u16>(spawn.x / 2);
diagnosticStart.data.y = static_cast<remorse::u16>(spawn.y / 2);
diagnosticStart.data.z = static_cast<remorse::u8>(spawn.z);
liveItems.teleportAvatar(diagnosticStart, static_cast<remorse::u8>(state.mapIndex));
}
auto renderer = remorse::loadMapSceneRenderer(state.root, state.mapIndex);
if (renderer) {
hatchGlobsInFastArea(liveItems, *renderer, remorse::calculateFastAreaRange(spawn));