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:
parent
8c23bb02c9
commit
0771c86043
12 changed files with 1091 additions and 253 deletions
|
|
@ -51,6 +51,11 @@ struct ControllerProcessEntry {
|
|||
u8 extra[0x0e];
|
||||
} CR_PACKED;
|
||||
|
||||
struct TeleporterProcessEntry {
|
||||
EntityDispatchEntryBase base;
|
||||
u8 extra[0x04];
|
||||
} CR_PACKED;
|
||||
|
||||
struct AnimPrimitiveProcessEntry {
|
||||
EntityDispatchEntryBase base;
|
||||
u8 extra[0x29];
|
||||
|
|
@ -84,6 +89,7 @@ static_assert(sizeof(SystemTimerProcessEntry) == 0x4a, "SystemTimerProcessEntry
|
|||
static_assert(sizeof(KeyboardProcessEntry) == 0x36, "KeyboardProcessEntry size must match allocation evidence");
|
||||
static_assert(sizeof(KeyDaemonProcessEntry) == 0x40, "KeyDaemonProcessEntry size must match allocation evidence");
|
||||
static_assert(sizeof(ControllerProcessEntry) == 0x40, "ControllerProcessEntry size must match allocation evidence");
|
||||
static_assert(sizeof(TeleporterProcessEntry) == 0x36, "TeleporterProcessEntry size must match allocation evidence");
|
||||
static_assert(sizeof(AnimPrimitiveProcessEntry) == 0x5b, "AnimPrimitiveProcessEntry size must match allocation evidence");
|
||||
static_assert(sizeof(EntityVmContextProcessEntry) == 0x124, "EntityVmContext process size must match allocation evidence");
|
||||
static_assert(sizeof(KeyEvent) == 0x0c, "KeyEvent size must match keyboard event copy");
|
||||
|
|
@ -119,6 +125,8 @@ public:
|
|||
KeyboardProcessEntry* createKeyboardProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
KeyDaemonProcessEntry* createKeyDaemonProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
ControllerProcessEntry* createControllerProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
TeleporterProcessEntry* createTeleporterProcess(u8 activeGroup, u16 mapNumber, u16 teleportId,
|
||||
u16 (&groupCounts)[32]);
|
||||
AnimPrimitiveProcessEntry* createAnimPrimitiveProcess(u8 activeGroup, u16 itemNumber,
|
||||
u16 animationNumber, u8 direction, const AnimationClip& clip, u16 (&groupCounts)[32]);
|
||||
EntityVmContextProcessEntry* createUsecodeProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
|
|
@ -174,6 +182,7 @@ private:
|
|||
std::vector<std::unique_ptr<StandProcessEntry>> ownedStandProcesses_;
|
||||
std::vector<std::unique_ptr<GravityProcessEntry>> ownedGravityProcesses_;
|
||||
std::vector<std::unique_ptr<AnimPrimitiveProcessEntry>> ownedAnimPrimitiveProcesses_;
|
||||
std::vector<std::unique_ptr<TeleporterProcessEntry>> ownedTeleporterProcesses_;
|
||||
std::vector<std::unique_ptr<EntityVmContextProcessEntry>> ownedUsecodeProcesses_;
|
||||
std::unique_ptr<SystemTimerProcessEntry> systemTimerProcess_;
|
||||
std::unique_ptr<KeyboardProcessEntry> keyboardProcess_;
|
||||
|
|
@ -237,6 +246,15 @@ public:
|
|||
static void increment(SystemTimerProcessEntry& entry);
|
||||
};
|
||||
|
||||
class TeleporterProcess {
|
||||
public:
|
||||
static constexpr u16 vtableOffset = 0x0f3a;
|
||||
static constexpr u16 mapNumberOffset = 0x32;
|
||||
static constexpr u16 teleportIdOffset = 0x34;
|
||||
static bool run(const TeleporterProcessEntry& process, LiveFixedMapItems& items,
|
||||
u16 currentMapNumber, const std::vector<ShapeTypeInfo>& shapes);
|
||||
};
|
||||
|
||||
class KeyboardProcess {
|
||||
public:
|
||||
static bool handleKeyEvent(KeyboardProcessEntry& process, u16 selector, u16 filter,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
#include "remorse/flex_archive.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
|
@ -27,20 +29,69 @@ struct UsecodeEventBody {
|
|||
std::vector<u8> bytes;
|
||||
};
|
||||
|
||||
struct UsecodeLoadedChunkProcessLayout {
|
||||
u16 vtableOffset = 0;
|
||||
FarPtr16 previous{0, 0};
|
||||
FarPtr16 next{0, 0};
|
||||
FarPtr16 chunkBuffer{0, 0};
|
||||
FarPtr16 savedSource{0, 0};
|
||||
u16 slotIndex = 0;
|
||||
u16 chunkIndex = 0;
|
||||
};
|
||||
|
||||
static_assert(sizeof(UsecodeLoadedChunkProcessLayout) == 0x16);
|
||||
static_assert(offsetof(UsecodeLoadedChunkProcessLayout, previous) == 0x02);
|
||||
static_assert(offsetof(UsecodeLoadedChunkProcessLayout, next) == 0x06);
|
||||
static_assert(offsetof(UsecodeLoadedChunkProcessLayout, chunkBuffer) == 0x0a);
|
||||
static_assert(offsetof(UsecodeLoadedChunkProcessLayout, savedSource) == 0x0e);
|
||||
static_assert(offsetof(UsecodeLoadedChunkProcessLayout, slotIndex) == 0x12);
|
||||
static_assert(offsetof(UsecodeLoadedChunkProcessLayout, chunkIndex) == 0x14);
|
||||
|
||||
struct UsecodeLoadedChunkProcess {
|
||||
FarPtr16 processAddress{0, 0};
|
||||
UsecodeLoadedChunkProcessLayout layout;
|
||||
UsecodeEventBody body;
|
||||
};
|
||||
|
||||
class UsecodeRuntimeMemory {
|
||||
public:
|
||||
std::optional<FarPtr16> allocate(std::size_t byteCount, u8 initialValue = 0);
|
||||
bool release(FarPtr16 address) noexcept;
|
||||
bool contains(FarPtr16 address, std::size_t byteCount) const noexcept;
|
||||
std::optional<std::vector<u8>> read(FarPtr16 address, std::size_t byteCount) const;
|
||||
bool write(FarPtr16 address, const std::vector<u8>& bytes);
|
||||
std::size_t allocationCount() const noexcept { return allocations_.size(); }
|
||||
|
||||
private:
|
||||
u32 nextSegment_ = 1;
|
||||
std::map<u16, std::vector<u8>> allocations_;
|
||||
};
|
||||
|
||||
class UsecodeSlotChunkCache {
|
||||
public:
|
||||
explicit UsecodeSlotChunkCache(UsecodeClass usecodeClass);
|
||||
const UsecodeEventBody* ensureLoaded(u16 chunkIndex);
|
||||
bool release(u16 chunkIndex) noexcept;
|
||||
bool tryUnload(u16 chunkIndex) noexcept;
|
||||
bool tryUnload(u16 chunkIndex);
|
||||
u16 inUseCount(u16 chunkIndex) const noexcept;
|
||||
const UsecodeLoadedChunkProcess* loadedProcess(u16 chunkIndex) const noexcept;
|
||||
std::size_t loadedProcessCount() const noexcept;
|
||||
FarPtr16 loadedListHead() const noexcept { return loadedListHead_; }
|
||||
std::size_t chunkCount() const noexcept { return chunks_.size(); }
|
||||
const UsecodeSlotOwnerBuffers& ownerBuffers() const noexcept { return ownerBuffers_; }
|
||||
UsecodeRuntimeMemory& runtimeMemory() noexcept { return runtimeMemory_; }
|
||||
const UsecodeRuntimeMemory& runtimeMemory() const noexcept { return runtimeMemory_; }
|
||||
|
||||
private:
|
||||
UsecodeLoadedChunkProcess* findLoadedProcess(FarPtr16 processAddress) noexcept;
|
||||
bool unlinkLoadedProcess(FarPtr16 processAddress);
|
||||
|
||||
UsecodeClass usecodeClass_;
|
||||
UsecodeSlotOwnerBuffers ownerBuffers_;
|
||||
std::vector<std::optional<UsecodeEventBody>> chunks_;
|
||||
std::vector<std::optional<UsecodeLoadedChunkProcess>> chunks_;
|
||||
UsecodeRuntimeMemory runtimeMemory_;
|
||||
FarPtr16 loadedListHead_{0, 0};
|
||||
FarPtr16 loadedListTail_{0, 0};
|
||||
};
|
||||
|
||||
struct UsecodeClassBases {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#include <vector>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
#include "remorse/usecode_archive.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
|
|
@ -15,12 +16,20 @@ constexpr std::size_t usecodeEventBytecodeOffset = 14;
|
|||
constexpr std::size_t usecodeFrameBaseOffset = 0x40;
|
||||
constexpr std::size_t usecodeInterpreterStackLimit = 199;
|
||||
constexpr std::size_t usecodeContextProcessSize = 0x124;
|
||||
constexpr std::size_t usecodeContextInterpreterOffset = 0x36;
|
||||
constexpr std::size_t usecodeContextSlotIndexOffset = 0x32;
|
||||
constexpr std::size_t usecodeContextValueAddOffset = 0x34;
|
||||
constexpr std::size_t usecodeContextStackPointerOffset = 0x102;
|
||||
constexpr std::size_t usecodeContextStackBaseOffset = 0x106;
|
||||
constexpr std::size_t usecodeContextStackLimitOffset = 0x10a;
|
||||
constexpr std::size_t usecodeContextCodeSourceOffset = 0x10c;
|
||||
constexpr std::size_t usecodeContextSelfPointerOffset = 0x110;
|
||||
constexpr std::size_t usecodeContextOwnerRowsOffset = 0x117;
|
||||
constexpr std::size_t usecodeContextOwnerSourceOffset = 0x11b;
|
||||
constexpr std::size_t usecodeContextParameterOffset = 0x11f;
|
||||
constexpr std::size_t usecodeContextModeFlagsOffset = 0x121;
|
||||
constexpr std::size_t usecodeContextActiveFlagOffset = 0x122;
|
||||
constexpr std::size_t usecodeInterpreterCallStackSize = 0x82;
|
||||
|
||||
struct UsecodeCallFrame {
|
||||
u16 offset = 0;
|
||||
|
|
@ -53,7 +62,7 @@ class UsecodeInterpreterState {
|
|||
public:
|
||||
static std::optional<UsecodeInterpreterState> initialize(
|
||||
const std::vector<u8>& itemParameter, const std::vector<u8>& contextParameter = {},
|
||||
u16 modeFlags = 0);
|
||||
u16 modeFlags = 0, bool contextParameterPresent = false);
|
||||
bool pushCodeAddress(u16 offset, u16 segment);
|
||||
bool pushWord(u16 value);
|
||||
bool pushDword(u32 value);
|
||||
|
|
@ -71,6 +80,7 @@ public:
|
|||
u16 stackLimit = usecodeInterpreterStackLimit;
|
||||
u16 temporaryWord = 0;
|
||||
std::optional<u16> contextParameterOffset;
|
||||
std::optional<u16> contextParameterStackTop;
|
||||
bool processExcluded = false;
|
||||
u16 currentProcessId = 0;
|
||||
u16 impliedProcessId = 0;
|
||||
|
|
@ -107,19 +117,57 @@ struct UsecodeContextFactoryFields {
|
|||
u16 parameterOffset = 0;
|
||||
};
|
||||
|
||||
struct UsecodeContextCreateRequest {
|
||||
FarPtr16 ownerSource{};
|
||||
FarPtr16 codeSource{};
|
||||
FarPtr16 ownerRows{};
|
||||
std::optional<std::vector<u8>> itemParameter;
|
||||
std::optional<std::vector<u8>> contextParameter;
|
||||
u16 modeFlags = 0;
|
||||
u16 slotIndex = 0;
|
||||
u16 valueAddOffset = 0;
|
||||
};
|
||||
|
||||
struct UsecodeContextInstance {
|
||||
FarPtr16 processAddress{0, 0};
|
||||
FarPtr16 callStackAddress{0, 0};
|
||||
std::array<u8, usecodeContextProcessSize> processBytes{};
|
||||
UsecodeInterpreterState interpreter;
|
||||
};
|
||||
|
||||
struct UsecodeEventContext {
|
||||
UsecodeContextInstance context;
|
||||
FarPtr16 processIdOutput{0, 0};
|
||||
std::vector<FarPtr16> ownedRegions;
|
||||
};
|
||||
|
||||
bool writeUsecodeContextFactoryFields(u8* processBytes, std::size_t processSize,
|
||||
const UsecodeContextFactoryFields& fields);
|
||||
std::optional<UsecodeContextInstance> createUsecodeContext(UsecodeRuntimeMemory& memory,
|
||||
const UsecodeContextCreateRequest& request);
|
||||
bool destroyUsecodeContext(UsecodeRuntimeMemory& memory,
|
||||
const UsecodeContextInstance& context) noexcept;
|
||||
std::optional<UsecodeEventContext> createUsecodeEventContext(UsecodeRuntimeMemory& memory,
|
||||
const std::vector<u8>& ownerRows, u16 classId, u16 eventSlot, u16 sourceItem,
|
||||
const std::vector<u8>& code, u8 itemArgumentSize);
|
||||
bool destroyUsecodeEventContext(UsecodeRuntimeMemory& memory,
|
||||
const UsecodeEventContext& eventContext) noexcept;
|
||||
|
||||
class UsecodeTaskRegistry {
|
||||
public:
|
||||
UsecodeRuntimeMemory& runtimeMemory() noexcept { return runtimeMemory_; }
|
||||
bool add(UsecodeProcessTask task);
|
||||
bool addWithContext(UsecodeProcessTask task, UsecodeEventContext context);
|
||||
UsecodeProcessTask* find(u16 processId);
|
||||
const UsecodeProcessTask* find(u16 processId) const;
|
||||
const UsecodeEventContext* context(u16 processId) const noexcept;
|
||||
bool remove(u16 processId);
|
||||
std::size_t size() const noexcept { return tasks_.size(); }
|
||||
|
||||
private:
|
||||
std::map<u16, UsecodeProcessTask> tasks_;
|
||||
std::map<u16, UsecodeEventContext> contexts_;
|
||||
UsecodeRuntimeMemory runtimeMemory_;
|
||||
};
|
||||
|
||||
} // namespace remorse
|
||||
Loading…
Add table
Add a link
Reference in a new issue