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

@ -133,13 +133,21 @@ Current layout claims that matter for class lifting:
- `+0x117/+0x119` cache the owner-linked source pair
- `+0x123` behaves as a busy or active flag in the sync/dispatch path
`CreateFromSlotIndex` initializes the embedded interpreter, copies the item parameter only when mode flags are nonzero, copies optional `ucparam` bytes into the backward-growing stack, then reserves four bytes for the prior stack pointer when both are present. The host VM models this order and the `+0x11f` relative offset, but does not synthesize the original segment:offset pointer or map the runtime-owned code and owner buffers yet.
`CreateFromSlotIndex` initializes the embedded interpreter, copies the item parameter only when mode flags are nonzero, copies optional `ucparam` bytes into the backward-growing stack, then reserves four bytes for the prior stack pointer when both are present. `createUsecodeContext` now builds a bounded host context from caller-supplied runtime pointers: it allocates the `0x124` process body and `0x82` call-stack buffer, maps code/owner pointers through `UsecodeRuntimeMemory`, writes the interpreter's stack/base/limit fields, copies only the active stack window, stores the saved pointer at the recovered relative location, and seeds the initial far-call pair. Invalid source ranges and stack overflow fail before leaving partial allocations. `usecode_vm_tests` checks the process bytes, pointer ranges, parameter ordering, call-stack bytes, and cleanup.
`createUsecodeEventContext` adds the retail owner-row and event-slot inputs. `UsecodeTaskRegistry::addWithContext` owns the resulting context allocations for the task lifetime. The window use-request path now searches nearby item candidates, selects an eligible event through the owner rows, creates the context-backed task, writes its process ID output word, and copies the context bytes into the owned process entry. The retail BOX_NS test exercises candidate-driven selection and VM execution through injected item/timer services; it verifies frame/render changes and releases all context allocations on task removal.
This is still host-side selection and execution, not the original `Usecode_ItemCallEvent` dispatcher or proven `CreateFromSlotIndex` call chain. The exact runtime meaning of the initial slot/value-add call-frame pair remains unverified, as do original owner-resource callback parity and general far-pointer dispatch.
The embedded interpreter initializes its stack limit to 199 bytes; the host bootstrap rejects a combined initial item/context payload and saved-pointer slot above that limit.
The host opcode service currently maps segment `0` to the task's item-memory buffer through `UsecodeProcessTask::readMappedItemWord`; all other segments are rejected. This centralizes the existing fixture behavior but is not a general segmented address space.
`UsecodeClass` now reads the first header dword as the extra-slot count. `initializeUsecodeSlotOwnerBuffers` copies the six-byte descriptors from class `+0x14` into a host buffer of `0xc0 + 6 * extraSlotCount` bytes and creates `32 + extraSlotCount` state words initialized to `0xffff`, matching `InitSlotOwnerBuffers`; retail `EVENT` and `NPCTRIG` cover the base and one-extra-slot cases. `UsecodeSlotChunkCache` reads descriptors through this buffer, tracks ensure/release counts, and unloads only at count zero, restoring the state sentinel so a later ensure reloads the body. This remains an archive-backed host model: the 0x16-byte `UProcess_1420_0000` object, owner-helper materialization, runtime loaded-list record, and far-pointer allocation/restoration are not implemented.
`UsecodeClass` now reads the first header dword as the extra-slot count. `initializeUsecodeSlotOwnerBuffers` copies the six-byte descriptors from class `+0x14` into a host buffer of `0xc0 + 6 * extraSlotCount` bytes and creates `32 + extraSlotCount` state words initialized to `0xffff`, matching `InitSlotOwnerBuffers`; retail `EVENT` and `NPCTRIG` cover the base and one-extra-slot cases.
`UsecodeSlotChunkCache` now models the verified `UProcess_1420_0000` layout as a 0x16-byte record: a 16-bit vtable offset, previous/next far links at `+0x02/+0x06`, chunk-buffer far pointer at `+0x0a`, saved source pair at `+0x0e`, and slot/chunk indices at `+0x12/+0x14`. The host allocates a bounded segment for each process and chunk buffer, initializes the chunk buffer to `0xff`, copies the archive-backed body, replaces the descriptor's source pair with the buffer pointer, and links the process at the loaded-list head. On zero-count eviction it restores the saved source pair, resets state to `0xffff`, unlinks both neighbors, and releases both allocations. Retail `EVENT`/`NPCTRIG` tests and a synthetic two-node list cover reuse, eviction, pointer restoration, and list unlinking.
`UsecodeRuntimeMemory` currently assigns one bounded region per nonzero segment, with at most 64 KiB per allocation and range-checked reads/writes. This is host address-space scaffolding, not a recovered DOS allocator. Materialization reads from the loaded `UsecodeClass` byte buffer; the original `EntityVmOwnerResource` callback and live runtime allocation/address parity remain unverified.
Current caution: