From f3b0de06090d55406990b1226a19adaf9d234873 Mon Sep 17 00:00:00 2001 From: Misha <106913236+MishaProductions@users.noreply.github.com> Date: Mon, 30 Oct 2023 09:54:00 -0400 Subject: [PATCH] more MxDSBuffer functions (#255) * FUN_100c6f80 * AllocateBuffer * Update mxdsbuffer.h * Update mxdsbuffer.cpp * Update mxdsbuffer.cpp * Return reference * Fix delete call * Fix GetWriteOffset * Remove cast --------- Co-authored-by: Christian Semmler --- LEGO1/mxdsbuffer.cpp | 105 +++++++++++++++++++++++++++++--- LEGO1/mxdsbuffer.h | 21 +++++-- LEGO1/mxdssource.cpp | 2 +- LEGO1/mxramstreamcontroller.cpp | 2 +- LEGO1/mxstreamer.h | 7 ++- 5 files changed, 121 insertions(+), 16 deletions(-) diff --git a/LEGO1/mxdsbuffer.cpp b/LEGO1/mxdsbuffer.cpp index 4163e39b..a09bb101 100644 --- a/LEGO1/mxdsbuffer.cpp +++ b/LEGO1/mxdsbuffer.cpp @@ -1,5 +1,8 @@ #include "mxdsbuffer.h" +#include "mxomni.h" +#include "mxstreamer.h" + DECOMP_SIZE_ASSERT(MxDSBuffer, 0x34); // OFFSET: LEGO1 0x100c6470 @@ -14,24 +17,112 @@ MxDSBuffer::MxDSBuffer() m_unk1c = 0; m_writeOffset = 0; m_bytesRemaining = 0; - m_mode = 2; + m_mode = MxDSBufferType_Preallocated; m_unk30 = 0; } // OFFSET: LEGO1 0x100c6530 MxDSBuffer::~MxDSBuffer() { - // TODO + if (m_pBuffer != NULL) { + if (m_mode == MxDSBufferType_Chunk) { + // TODO + } + else if (m_mode == MxDSBufferType_Allocate || m_mode == MxDSBufferType_Unknown) { + delete[] m_pBuffer; + } + } + + m_unk14 = 0; + m_unk1c = 0; +} + +// OFFSET: LEGO1 0x100c6640 +MxResult MxDSBuffer::AllocateBuffer(MxU32 p_bufferSize, MxDSBufferType p_mode) +{ + MxResult result = FAILURE; + MxU32 i = 0; + if (p_mode == MxDSBufferType_Allocate) { + m_pBuffer = new MxU8[p_bufferSize]; + } + else if (p_mode == MxDSBufferType_Chunk) { + MxStreamer* streamer = Streamer(); + // I have no clue as to what this does, or even if its correct. Maybe it's related to storing chunks in + // MxRamStreamController? + if (p_bufferSize >> 10 == 0x40) { + i = 0; + while (i < 22) { + if ((*(MxU32*) ((streamer->GetSubclass1().GetUnk08() + ((i & 0xffffffe7) >> 3)) & 1 << ((MxU8) i & 0x1f) + )) == 0) { + MxU32* ptr = (MxU32*) ((streamer->GetSubclass1().GetUnk08() + ((i & 0xffffffe7) >> 3)) & + 1 << ((MxU8) i & 0x1f)); + + // mark it as used? + *ptr = *ptr ^ 1 << (i & 0x1f); + + m_pBuffer = + (MxU8*) (streamer->GetSubclass1().GetSize() * i * 0x400 + streamer->GetSubclass1().GetBuffer()); + break; + } + i++; + } + + m_pBuffer = NULL; + } + else if (p_bufferSize >> 10 == 0x80) { + i = 0; + // Same thing as above but it uses subclass2 + while (i < 22) { + if ((*(MxU32*) ((streamer->GetSubclass2().GetUnk08() + ((i & 0xffffffe7) >> 3)) & 1 << ((MxU8) i & 0x1f) + )) == 0) { + MxU32* ptr = (MxU32*) ((streamer->GetSubclass2().GetUnk08() + ((i & 0xffffffe7) >> 3)) & + 1 << ((MxU8) i & 0x1f)); + + // mark it as used? + *ptr = *ptr ^ 1 << (i & 0x1f); + + m_pBuffer = + (MxU8*) (streamer->GetSubclass2().GetSize() * i * 0x400 + streamer->GetSubclass2().GetBuffer()); + break; + } + i++; + } + + m_pBuffer = NULL; + } + else { + m_pIntoBuffer = NULL; + } + } + + m_pIntoBuffer = m_pBuffer; + m_pIntoBuffer2 = m_pBuffer; + if (m_pBuffer != NULL) { + m_mode = p_mode; + m_bytesRemaining = p_bufferSize; + m_writeOffset = p_bufferSize; + result = SUCCESS; + } + return result; } // OFFSET: LEGO1 0x100c6780 -MxResult MxDSBuffer::FUN_100c6780(void* p_buffer, MxU32 p_size) +MxResult MxDSBuffer::SetBufferPointer(MxU32* p_buffer, MxU32 p_size) { - m_pBuffer = p_buffer; - m_pIntoBuffer = p_buffer; - m_pIntoBuffer2 = p_buffer; + m_pBuffer = (MxU8*) p_buffer; + m_pIntoBuffer = (MxU8*) p_buffer; + m_pIntoBuffer2 = (MxU8*) p_buffer; m_bytesRemaining = p_size; m_writeOffset = p_size; - m_mode = 2; + m_mode = MxDSBufferType_Preallocated; return SUCCESS; } + +// OFFSET: LEGO1 0x100c6f80 +void MxDSBuffer::FUN_100c6f80(MxU32 p_unk) +{ + if (p_unk < m_writeOffset) { + m_pIntoBuffer2 = m_pBuffer + p_unk; + m_pIntoBuffer = m_pBuffer + p_unk; + } +} diff --git a/LEGO1/mxdsbuffer.h b/LEGO1/mxdsbuffer.h index e521b47f..33706899 100644 --- a/LEGO1/mxdsbuffer.h +++ b/LEGO1/mxdsbuffer.h @@ -4,6 +4,13 @@ #include "decomp.h" #include "mxcore.h" +enum MxDSBufferType { + MxDSBufferType_Chunk = 0, + MxDSBufferType_Allocate = 1, + MxDSBufferType_Preallocated = 2, + MxDSBufferType_Unknown = 3, +}; + // VTABLE 0x100dcca0 // SIZE 0x34 class MxDSBuffer : public MxCore { @@ -18,20 +25,22 @@ class MxDSBuffer : public MxCore { return "MxDSBuffer"; } - MxResult FUN_100c6780(void* p_buffer, MxU32 p_size); + MxResult AllocateBuffer(MxU32 p_bufferSize, MxDSBufferType p_mode); + MxResult SetBufferPointer(MxU32* p_buffer, MxU32 p_size); + void FUN_100c6f80(MxU32 p_unk); - inline void* GetBuffer() { return m_pBuffer; } + inline MxU8* GetBuffer() { return m_pBuffer; } inline MxU32 GetWriteOffset() { return m_writeOffset; } private: - void* m_pBuffer; - void* m_pIntoBuffer; - void* m_pIntoBuffer2; + MxU8* m_pBuffer; + MxU8* m_pIntoBuffer; + MxU8* m_pIntoBuffer2; undefined4 m_unk14; undefined4 m_unk18; undefined4 m_unk1c; undefined2 m_unk20; - undefined4 m_mode; + MxDSBufferType m_mode; MxU32 m_writeOffset; MxU32 m_bytesRemaining; undefined4 m_unk30; diff --git a/LEGO1/mxdssource.cpp b/LEGO1/mxdssource.cpp index f94318d1..7110c9ee 100644 --- a/LEGO1/mxdssource.cpp +++ b/LEGO1/mxdssource.cpp @@ -5,7 +5,7 @@ // OFFSET: LEGO1 0x100bffd0 void MxDSSource::ReadToBuffer(MxDSBuffer* p_buffer) { - Read((unsigned char*) p_buffer->GetBuffer(), p_buffer->GetWriteOffset()); + Read(p_buffer->GetBuffer(), p_buffer->GetWriteOffset()); } // OFFSET: LEGO1 0x100bfff0 diff --git a/LEGO1/mxramstreamcontroller.cpp b/LEGO1/mxramstreamcontroller.cpp index ae3d9634..2e8efc36 100644 --- a/LEGO1/mxramstreamcontroller.cpp +++ b/LEGO1/mxramstreamcontroller.cpp @@ -29,7 +29,7 @@ MxResult MxRAMStreamController::Open(const char* p_filename) ((MxRAMStreamProvider*) m_provider)->GetBufferOfFileSize(), ((MxRAMStreamProvider*) m_provider)->GetFileSize() ); - m_buffer.FUN_100c6780( + m_buffer.SetBufferPointer( ((MxRAMStreamProvider*) m_provider)->GetBufferOfFileSize(), ((MxRAMStreamProvider*) m_provider)->GetFileSize() ); diff --git a/LEGO1/mxstreamer.h b/LEGO1/mxstreamer.h index 6a62146a..f7d1d448 100644 --- a/LEGO1/mxstreamer.h +++ b/LEGO1/mxstreamer.h @@ -18,9 +18,11 @@ class MxStreamerSubClass1 { ~MxStreamerSubClass1() { delete[] m_buffer; } - undefined4 GetSize() { return m_size; } + undefined4 GetSize() const { return m_size; } void SetBuffer(undefined* p_buf) { m_buffer = p_buf; } + inline undefined* GetBuffer() const { return m_buffer; } + inline undefined4 GetUnk08() const { return m_unk08; } private: undefined* m_buffer; @@ -93,6 +95,9 @@ class MxStreamer : public MxCore { MxResult AddStreamControllerToOpenList(MxStreamController* p_stream); MxResult FUN_100b99b0(MxDSAction* p_action); + inline const MxStreamerSubClass2& GetSubclass1() { return m_subclass1; } + inline const MxStreamerSubClass3& GetSubclass2() { return m_subclass2; } + private: list m_openStreams; // 0x8 MxStreamerSubClass2 m_subclass1; // 0x14