From 77ca92d20507319f7cfd3e281a8e503b51a24735 Mon Sep 17 00:00:00 2001 From: lahm86 <33758420+lahm86@users.noreply.github.com> Date: Fri, 14 Mar 2025 20:14:26 +0000 Subject: [PATCH] inject: introduce OBJECT_3D editor This adds the ability to edit OBJECT_3Ds (static mesh properties). It alos addresses potential memory corruption in the scenario where a new data type is present in an injection but it is being loaded into a version of the game that doesn't recognise it. --- src/libtrx/game/inject/common.c | 4 ++- src/libtrx/game/inject/editors/meshes.c | 38 ++++++++++++++++++++ src/libtrx/include/libtrx/game/inject/enum.h | 3 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/libtrx/game/inject/common.c b/src/libtrx/game/inject/common.c index 489121746..d7fa374e2 100644 --- a/src/libtrx/game/inject/common.c +++ b/src/libtrx/game/inject/common.c @@ -125,7 +125,9 @@ static void M_InitialiseBlock(VFILE *const file) const INJECTION_DATA_TYPE data_type = VFile_ReadS32(file); const int32_t data_count = VFile_ReadS32(file); const int32_t data_size = VFile_ReadS32(file); - m_DataCounts[data_type] += data_count; + if (data_type >= 0 && data_type < IDT_NUMBER_OF) { + m_DataCounts[data_type] += data_count; + } switch (data_type) { case IDT_ROOM_EDIT_META: { diff --git a/src/libtrx/game/inject/editors/meshes.c b/src/libtrx/game/inject/editors/meshes.c index c227afa73..9544fae4b 100644 --- a/src/libtrx/game/inject/editors/meshes.c +++ b/src/libtrx/game/inject/editors/meshes.c @@ -37,6 +37,9 @@ static void M_ApplyFace3Edit( const FACE_EDIT *edit, FACE3 *faces, uint16_t texture); static uint16_t *M_GetMeshTexture(const FACE_EDIT *edit); +static void M_Object3DEdits(const INJECTION *injection, int32_t data_count); +static BOUNDS_16 M_ReadBounds16(VFILE *file); + static void M_MeshEdits( const INJECTION *const injection, const int32_t data_count) { @@ -207,4 +210,39 @@ static uint16_t *M_GetMeshTexture(const FACE_EDIT *const edit) return nullptr; } +static void M_Object3DEdits( + const INJECTION *const injection, const int32_t data_count) +{ + for (int32_t i = 0; i < data_count; i++) { + const int32_t obj_id = VFile_ReadS32(injection->fp); + const bool collidable = VFile_ReadU8(injection->fp) == 1; + const bool visible = VFile_ReadU8(injection->fp) == 1; + const BOUNDS_16 collision_bounds = M_ReadBounds16(injection->fp); + const BOUNDS_16 draw_bounds = M_ReadBounds16(injection->fp); + + STATIC_OBJECT_3D *const obj = Object_Get3DStatic(obj_id); + if (!obj->loaded) { + continue; + } + + obj->collidable = collidable; + obj->visible = visible; + obj->collision_bounds = collision_bounds; + obj->draw_bounds = draw_bounds; + } +} + +static BOUNDS_16 M_ReadBounds16(VFILE *const file) +{ + BOUNDS_16 bounds = {}; + bounds.min.x = VFile_ReadS16(file); + bounds.max.x = VFile_ReadS16(file); + bounds.min.y = VFile_ReadS16(file); + bounds.max.y = VFile_ReadS16(file); + bounds.min.z = VFile_ReadS16(file); + bounds.max.z = VFile_ReadS16(file); + return bounds; +} + REGISTER_INJECT_EDITOR(IDT_MESH_EDITS, M_MeshEdits) +REGISTER_INJECT_EDITOR(IDT_OBJECT_3D_EDITS, M_Object3DEdits) diff --git a/src/libtrx/include/libtrx/game/inject/enum.h b/src/libtrx/include/libtrx/game/inject/enum.h index ab4f21d16..7c6ee589f 100644 --- a/src/libtrx/include/libtrx/game/inject/enum.h +++ b/src/libtrx/include/libtrx/game/inject/enum.h @@ -65,7 +65,8 @@ typedef enum { IDT_VIS_PORTAL_EDITS = 23, IDT_CAMERA_EDITS = 24, IDT_FRAME_EDITS = 25, - IDT_NUMBER_OF = 26, + IDT_OBJECT_3D_EDITS = 26, + IDT_NUMBER_OF = 27, } INJECTION_DATA_TYPE; typedef enum {