From 0b778de7319265db04eff7a46622e869a7e91d00 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Tue, 20 May 2025 02:18:11 +0200 Subject: [PATCH] Let LegoStorage know of memory size (#129) --- CMakeLists.txt | 1 + LEGO1/lego/legoomni/include/legopathcontroller.h | 9 +++++++-- LEGO1/lego/legoomni/src/common/legoanimationmanager.cpp | 1 + LEGO1/lego/legoomni/src/common/legogamestate.cpp | 1 + LEGO1/lego/legoomni/src/common/legoplantmanager.cpp | 1 + LEGO1/lego/legoomni/src/paths/legopathcontroller.cpp | 9 +++++++-- LEGO1/lego/legoomni/src/paths/legopathpresenter.cpp | 2 +- LEGO1/lego/legoomni/src/video/legoanimpresenter.cpp | 2 +- LEGO1/lego/legoomni/src/video/legomodelpresenter.cpp | 3 ++- LEGO1/lego/legoomni/src/video/legopalettepresenter.cpp | 2 +- LEGO1/lego/legoomni/src/video/legopartpresenter.cpp | 2 +- LEGO1/lego/legoomni/src/video/legotexturepresenter.cpp | 2 +- LEGO1/lego/sources/misc/legostorage.cpp | 5 ++++- LEGO1/lego/sources/misc/legostorage.h | 5 ++++- LEGO1/omni/include/mxtypes.h | 2 +- LEGO1/omni/src/stream/mxdsbuffer.cpp | 2 +- LEGO1/omni/src/video/mxvideopresenter.cpp | 2 +- util/decomp.h | 4 ++++ 18 files changed, 40 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dab8e65d..b3115a87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,7 @@ target_include_directories(lego1 PUBLIC "$") target_include_directories(lego1 PUBLIC "$") target_link_libraries(lego1 PRIVATE SDL3::SDL3) +target_link_libraries(lego1 PUBLIC SDL3::Headers) target_link_libraries(lego1 PRIVATE $<$:DirectX5::DirectX5>) if(WIN32) set_property(TARGET lego1 PROPERTY PREFIX "") diff --git a/LEGO1/lego/legoomni/include/legopathcontroller.h b/LEGO1/lego/legoomni/include/legopathcontroller.h index 1a13f961..99f6d939 100644 --- a/LEGO1/lego/legoomni/include/legopathcontroller.h +++ b/LEGO1/lego/legoomni/include/legopathcontroller.h @@ -85,8 +85,13 @@ class LegoPathController : public MxCore { // SYNTHETIC: LEGO1 0x10045740 // LegoPathController::`scalar deleting destructor' - virtual MxResult Create(MxU8* p_data, const Vector3& p_location, const MxAtomId& p_trigger); // vtable+0x14 - virtual void Destroy(); // vtable+0x18 + virtual MxResult Create( + MxU8* p_data, + LegoU32 p_dataSize, + const Vector3& p_location, + const MxAtomId& p_trigger + ); // vtable+0x14 + virtual void Destroy(); // vtable+0x18 MxResult PlaceActor( LegoPathActor* p_actor, diff --git a/LEGO1/lego/legoomni/src/common/legoanimationmanager.cpp b/LEGO1/lego/legoomni/src/common/legoanimationmanager.cpp index c7c619cc..c9c4a9ec 100644 --- a/LEGO1/lego/legoomni/src/common/legoanimationmanager.cpp +++ b/LEGO1/lego/legoomni/src/common/legoanimationmanager.cpp @@ -665,6 +665,7 @@ MxResult LegoAnimationManager::LoadWorldInfo(LegoOmni::World p_worldId) goto done; } + assert(version == 3); if (version != 3) { OmniError("World animation version mismatch", 0); goto done; diff --git a/LEGO1/lego/legoomni/src/common/legogamestate.cpp b/LEGO1/lego/legoomni/src/common/legogamestate.cpp index a7bae791..2192253f 100644 --- a/LEGO1/lego/legoomni/src/common/legogamestate.cpp +++ b/LEGO1/lego/legoomni/src/common/legogamestate.cpp @@ -369,6 +369,7 @@ MxResult LegoGameState::Load(MxULong p_slot) storage.ReadS32(version); + assert(version == 0x1000c); if (version != 0x1000c) { OmniError("Saved game version mismatch", 0); goto done; diff --git a/LEGO1/lego/legoomni/src/common/legoplantmanager.cpp b/LEGO1/lego/legoomni/src/common/legoplantmanager.cpp index 0350fcb4..aed60b7f 100644 --- a/LEGO1/lego/legoomni/src/common/legoplantmanager.cpp +++ b/LEGO1/lego/legoomni/src/common/legoplantmanager.cpp @@ -226,6 +226,7 @@ LegoEntity* LegoPlantManager::CreatePlant(MxS32 p_index, LegoWorld* p_world, Leg sprintf(lodName, "%s", g_plantLodNames[g_plantInfo[p_index].m_variant][g_plantInfo[p_index].m_color]); LegoROI* roi = CharacterManager()->CreateAutoROI(name, lodName, TRUE); + assert(roi != NULL); roi->SetVisibility(TRUE); entity = roi->GetEntity(); diff --git a/LEGO1/lego/legoomni/src/paths/legopathcontroller.cpp b/LEGO1/lego/legoomni/src/paths/legopathcontroller.cpp index c7dc69ba..d2be7bb8 100644 --- a/LEGO1/lego/legoomni/src/paths/legopathcontroller.cpp +++ b/LEGO1/lego/legoomni/src/paths/legopathcontroller.cpp @@ -83,10 +83,15 @@ LegoPathController::LegoPathController() // FUNCTION: LEGO1 0x10045880 // FUNCTION: BETA10 0x100b6959 -MxResult LegoPathController::Create(MxU8* p_data, const Vector3& p_location, const MxAtomId& p_trigger) +MxResult LegoPathController::Create( + MxU8* p_data, + LegoU32 p_dataSize, + const Vector3& p_location, + const MxAtomId& p_trigger +) { MxResult result = FAILURE; - LegoMemory storage(p_data); + LegoMemory storage(p_data, p_dataSize); if ((result = Read(&storage)) == SUCCESS) { MxS32 i; diff --git a/LEGO1/lego/legoomni/src/paths/legopathpresenter.cpp b/LEGO1/lego/legoomni/src/paths/legopathpresenter.cpp index da57b366..5b2fabfc 100644 --- a/LEGO1/lego/legoomni/src/paths/legopathpresenter.cpp +++ b/LEGO1/lego/legoomni/src/paths/legopathpresenter.cpp @@ -81,7 +81,7 @@ void LegoPathPresenter::ReadyTickle() else { ParseExtra(); - controller->Create(chunk->GetData(), m_action->GetLocation(), m_trigger); + controller->Create(chunk->GetData(), chunk->GetLength(), m_action->GetLocation(), m_trigger); world->AddPath(controller); m_subscriber->FreeDataChunk(chunk); diff --git a/LEGO1/lego/legoomni/src/video/legoanimpresenter.cpp b/LEGO1/lego/legoomni/src/video/legoanimpresenter.cpp index b7b5a751..6fd1fc36 100644 --- a/LEGO1/lego/legoomni/src/video/legoanimpresenter.cpp +++ b/LEGO1/lego/legoomni/src/video/legoanimpresenter.cpp @@ -140,7 +140,7 @@ void LegoAnimPresenter::Destroy(MxBool p_fromDestructor) MxResult LegoAnimPresenter::CreateAnim(MxStreamChunk* p_chunk) { MxResult result = FAILURE; - LegoMemory storage(p_chunk->GetData()); + LegoMemory storage(p_chunk->GetData(), p_chunk->GetLength()); MxS32 magicSig; LegoS32 parseScene = 0; MxS32 val3; diff --git a/LEGO1/lego/legoomni/src/video/legomodelpresenter.cpp b/LEGO1/lego/legoomni/src/video/legomodelpresenter.cpp index f1fe63bc..405154c1 100644 --- a/LEGO1/lego/legoomni/src/video/legomodelpresenter.cpp +++ b/LEGO1/lego/legoomni/src/video/legomodelpresenter.cpp @@ -52,7 +52,7 @@ MxResult LegoModelPresenter::CreateROI(MxDSChunk* p_chunk) MxResult result = FAILURE; LegoU32 numROIs; Mx3DPointFloat vect; - LegoMemory storage(p_chunk->GetData()); + LegoMemory storage(p_chunk->GetData(), p_chunk->GetLength()); LegoAnim anim; LegoU32 version, textureInfoOffset, i, numTextures, skipTextures; MxMatrix mat; @@ -70,6 +70,7 @@ MxResult LegoModelPresenter::CreateROI(MxDSChunk* p_chunk) if (storage.Read(&version, sizeof(LegoU32)) != SUCCESS) { goto done; } + assert(version == MODEL_VERSION); if (version != MODEL_VERSION) { goto done; } diff --git a/LEGO1/lego/legoomni/src/video/legopalettepresenter.cpp b/LEGO1/lego/legoomni/src/video/legopalettepresenter.cpp index 6ad7f6ec..ea5c7326 100644 --- a/LEGO1/lego/legoomni/src/video/legopalettepresenter.cpp +++ b/LEGO1/lego/legoomni/src/video/legopalettepresenter.cpp @@ -55,7 +55,7 @@ MxResult LegoPalettePresenter::ParsePalette(MxStreamChunk* p_chunk) RGBQUAD palette[256]; MxResult result = FAILURE; - LegoMemory stream((char*) p_chunk->GetData()); + LegoMemory stream((char*) p_chunk->GetData(), p_chunk->GetLength()); if (stream.Read(buffer, 40 * sizeof(MxU8)) == SUCCESS) { if (stream.Read(palette, 256 * 4 * sizeof(MxU8)) == SUCCESS) { m_palette = new MxPalette(palette); diff --git a/LEGO1/lego/legoomni/src/video/legopartpresenter.cpp b/LEGO1/lego/legoomni/src/video/legopartpresenter.cpp index 9cbb716f..8c960d6e 100644 --- a/LEGO1/lego/legoomni/src/video/legopartpresenter.cpp +++ b/LEGO1/lego/legoomni/src/video/legopartpresenter.cpp @@ -59,7 +59,7 @@ MxResult LegoPartPresenter::Read(MxDSChunk& p_chunk) { MxResult result = FAILURE; LegoU32 numROIs, numLODs; - LegoMemory storage(p_chunk.GetData()); + LegoMemory storage(p_chunk.GetData(), p_chunk.GetLength()); LegoU32 textureInfoOffset, i, j, numTextures; LegoU32 roiNameLength, roiInfoOffset, surplusLODs; LegoLODList* lods; diff --git a/LEGO1/lego/legoomni/src/video/legotexturepresenter.cpp b/LEGO1/lego/legoomni/src/video/legotexturepresenter.cpp index 9897f1fa..b6605fc0 100644 --- a/LEGO1/lego/legoomni/src/video/legotexturepresenter.cpp +++ b/LEGO1/lego/legoomni/src/video/legotexturepresenter.cpp @@ -33,7 +33,7 @@ MxResult LegoTexturePresenter::AddToManager() MxResult LegoTexturePresenter::Read(MxDSChunk& p_chunk) { MxResult result = FAILURE; - LegoMemory storage(p_chunk.GetData()); + LegoMemory storage(p_chunk.GetData(), p_chunk.GetLength()); LegoChar* textureName = NULL; LegoS32 hardwareMode = VideoManager()->GetDirect3D()->AssignedDevice()->GetHardwareMode(); diff --git a/LEGO1/lego/sources/misc/legostorage.cpp b/LEGO1/lego/sources/misc/legostorage.cpp index 24bbee09..6004977c 100644 --- a/LEGO1/lego/sources/misc/legostorage.cpp +++ b/LEGO1/lego/sources/misc/legostorage.cpp @@ -10,15 +10,17 @@ DECOMP_SIZE_ASSERT(LegoMemory, 0x10); DECOMP_SIZE_ASSERT(LegoFile, 0x0c); // FUNCTION: LEGO1 0x10099080 -LegoMemory::LegoMemory(void* p_buffer) : LegoStorage() +LegoMemory::LegoMemory(void* p_buffer, LegoU32 p_size) : LegoStorage() { m_buffer = (LegoU8*) p_buffer; m_position = 0; + m_size = p_size; } // FUNCTION: LEGO1 0x10099160 LegoResult LegoMemory::Read(void* p_buffer, LegoU32 p_size) { + assert(m_position + p_size <= m_size); memcpy(p_buffer, m_buffer + m_position, p_size); m_position += p_size; return SUCCESS; @@ -27,6 +29,7 @@ LegoResult LegoMemory::Read(void* p_buffer, LegoU32 p_size) // FUNCTION: LEGO1 0x10099190 LegoResult LegoMemory::Write(const void* p_buffer, LegoU32 p_size) { + assert(m_position + p_size <= m_size); memcpy(m_buffer + m_position, p_buffer, p_size); m_position += p_size; return SUCCESS; diff --git a/LEGO1/lego/sources/misc/legostorage.h b/LEGO1/lego/sources/misc/legostorage.h index da084b05..4ec29810 100644 --- a/LEGO1/lego/sources/misc/legostorage.h +++ b/LEGO1/lego/sources/misc/legostorage.h @@ -6,6 +6,7 @@ #include "mxstring.h" #include +#include // VTABLE: LEGO1 0x100d7d80 // SIZE 0x08 @@ -192,7 +193,7 @@ class LegoStorage { // SIZE 0x10 class LegoMemory : public LegoStorage { public: - LegoMemory(void* p_buffer); + LegoMemory(void* p_buffer, LegoU32 p_size); LegoResult Read(void* p_buffer, LegoU32 p_size) override; // vtable+0x04 LegoResult Write(const void* p_buffer, LegoU32 p_size) override; // vtable+0x08 @@ -206,6 +207,7 @@ class LegoMemory : public LegoStorage { // FUNCTION: LEGO1 0x100994b0 LegoResult SetPosition(LegoU32 p_position) override // vtable+0x10 { + assert(p_position <= m_size); m_position = p_position; return SUCCESS; } @@ -219,6 +221,7 @@ class LegoMemory : public LegoStorage { protected: LegoU8* m_buffer; // 0x04 LegoU32 m_position; // 0x08 + LegoU32 m_size; }; // VTABLE: LEGO1 0x100db730 diff --git a/LEGO1/omni/include/mxtypes.h b/LEGO1/omni/include/mxtypes.h index ce8d4769..f8069432 100644 --- a/LEGO1/omni/include/mxtypes.h +++ b/LEGO1/omni/include/mxtypes.h @@ -30,7 +30,6 @@ typedef MxLong MxResult; #endif typedef MxU8 MxBool; -static_assert(sizeof(MxBool) == 1, "Incorrect size"); #ifndef TRUE #define TRUE 1 @@ -62,6 +61,7 @@ typedef union { // BYTE all; // ? } FlagBitfield; +static_assert(sizeof(MxBool) == 1, "sizeof(MxBool) == 1"); static_assert(sizeof(MxU8) == 1, "sizeof(MxU8) == 1"); static_assert(sizeof(MxS8) == 1, "sizeof(MxS8) == 1"); static_assert(sizeof(MxU16) == 2, "sizeof(MxU16) == 2"); diff --git a/LEGO1/omni/src/stream/mxdsbuffer.cpp b/LEGO1/omni/src/stream/mxdsbuffer.cpp index 4d0b0113..9cfe3b24 100644 --- a/LEGO1/omni/src/stream/mxdsbuffer.cpp +++ b/LEGO1/omni/src/stream/mxdsbuffer.cpp @@ -430,7 +430,7 @@ MxResult MxDSBuffer::CalcBytesRemaining(MxU8* p_data) } else { ptr = p_data + MxStreamChunk::GetHeaderSize() + 8; - bytesRead = (*(MxU32*) (p_data + 4)) - MxStreamChunk::GetHeaderSize(); + bytesRead = UnalignedRead(p_data + 4) - MxStreamChunk::GetHeaderSize(); } if (bytesRead <= m_bytesRemaining) { diff --git a/LEGO1/omni/src/video/mxvideopresenter.cpp b/LEGO1/omni/src/video/mxvideopresenter.cpp index db0ab581..84b1c663 100644 --- a/LEGO1/omni/src/video/mxvideopresenter.cpp +++ b/LEGO1/omni/src/video/mxvideopresenter.cpp @@ -216,7 +216,7 @@ inline MxS32 MxVideoPresenter::PrepareRects(RECT& p_rectDest, RECT& p_rectSrc) p_rectSrc.right = 640; } - LONG height, width; + int height, width; if ((height = (p_rectDest.bottom - p_rectDest.top) + 1) <= 1 || (width = (p_rectDest.right - p_rectDest.left) + 1) <= 1) { return -1; diff --git a/util/decomp.h b/util/decomp.h index 673df553..99d8f757 100644 --- a/util/decomp.h +++ b/util/decomp.h @@ -27,4 +27,8 @@ typedef unsigned char undefined; typedef unsigned short undefined2; typedef unsigned int undefined4; +static_assert(sizeof(undefined) == 1, "sizeof(undefined) == 1"); +static_assert(sizeof(undefined2) == 2, "sizeof(undefined2) == 2"); +static_assert(sizeof(undefined4) == 4, "sizeof(undefined4) == 4"); + #endif // DECOMP_H