Skip to content

Commit

Permalink
Refactor surrounding MxDSChunk utility functions (#1313)
Browse files Browse the repository at this point in the history
  • Loading branch information
disinvite authored Jan 6, 2025
1 parent 7c452e9 commit 93815ca
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 110 deletions.
5 changes: 4 additions & 1 deletion LEGO1/omni/include/mxdsbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class MxDSBuffer : public MxCore {
// FUNCTION: BETA10 0x10148c60
MxU8* GetBuffer() { return m_pBuffer; }

MxU8** GetBufferRef() { return &m_pBuffer; }
// FUNCTION: BETA10 0x10164240
undefined4 GetUnknown14() { return m_unk0x14; }

// FUNCTION: BETA10 0x10156420
Expand All @@ -85,7 +85,10 @@ class MxDSBuffer : public MxCore {

void SetUnknown14(undefined4 p_unk0x14) { m_unk0x14 = p_unk0x14; }
void SetUnknown1c(undefined4 p_unk0x1c) { m_unk0x1c = p_unk0x1c; }

// FUNCTION: BETA10 0x10164260
void SetMode(Type p_mode) { m_mode = p_mode; }

void SetUnk30(MxDSStreamingAction* p_unk0x30) { m_unk0x30 = p_unk0x30; }

// SYNTHETIC: LEGO1 0x100c6510
Expand Down
10 changes: 6 additions & 4 deletions LEGO1/omni/include/mxdschunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class MxDSChunk : public MxCore {
}

static MxU32 GetHeaderSize();
static MxU32* IntoType(MxU8* p_buffer) { return (MxU32*) p_buffer; }
static MxU32* IntoLength(MxU8* p_buffer) { return (MxU32*) (p_buffer + 4); }
static MxU32 Size(MxU32 p_dataSize) { return (p_dataSize & 1) + p_dataSize + 8; }
static MxU8* End(MxU8* p_buffer) { return p_buffer + Size(*IntoLength(p_buffer)); }

// FUNCTION: BETA10 0x101641f0
static MxU32 Size(MxU8* p_buffer) { return (*(MxU32*) (p_buffer + 4) & 1) + *(MxU32*) (p_buffer + 4) + 8; }

// FUNCTION: BETA10 0x10164220
static MxU8* End(MxU8* p_buffer) { return p_buffer + Size(p_buffer); }

void SetChunkFlags(MxU16 p_flags) { m_flags = p_flags; }
void SetObjectId(undefined4 p_objectid) { m_objectId = p_objectid; }
Expand Down
22 changes: 22 additions & 0 deletions LEGO1/omni/include/mxdsstreamingaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,39 @@ class MxDSStreamingAction : public MxDSAction {
void SetInternalAction(MxDSAction* p_dsAction);
void FUN_100cd2d0();

// FUNCTION: BETA10 0x10156530
MxU32 GetUnknown94() { return m_unk0x94; }

// FUNCTION: BETA10 0x10156380
MxS32 GetUnknown9c() { return m_unk0x9c; }

// FUNCTION: BETA10 0x10156630
MxDSBuffer* GetUnknowna0() { return m_unk0xa0; }

// FUNCTION: BETA10 0x101563d0
MxDSBuffer* GetUnknowna4() { return m_unk0xa4; }

// FUNCTION: BETA10 0x10159190
MxLong GetUnknowna8() { return m_unk0xa8; }

MxDSAction* GetInternalAction() { return m_internalAction; }

// FUNCTION: BETA10 0x10156580
MxU32 GetBufferOffset() { return m_bufferOffset; }

// FUNCTION: BETA10 0x10156550
void SetUnknown94(MxU32 p_unk0x94) { m_unk0x94 = p_unk0x94; }

// FUNCTION: BETA10 0x101563a0
void SetUnknown9c(MxS32 p_unk0x9c) { m_unk0x9c = p_unk0x9c; }

// FUNCTION: BETA10 0x10156470
void SetUnknowna0(MxDSBuffer* p_unk0xa0) { m_unk0xa0 = p_unk0xa0; }

// FUNCTION: BETA10 0x101563f0
void SetUnknowna4(MxDSBuffer* p_unk0xa4) { m_unk0xa4 = p_unk0xa4; }

// FUNCTION: BETA10 0x10151150
void SetBufferOffset(MxU32 p_bufferOffset) { m_bufferOffset = p_bufferOffset; }

// FUNCTION: BETA10 0x10156650
Expand Down
110 changes: 48 additions & 62 deletions LEGO1/omni/src/stream/mxdiskstreamprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ MxBool MxDiskStreamProvider::FUN_100d1af0(MxDSStreamingAction* p_action)
}

// FUNCTION: LEGO1 0x100d1b20
// FUNCTION: BETA10 0x10163712
MxResult MxDiskStreamProvider::FUN_100d1b20(MxDSStreamingAction* p_action)
{
MxDSBuffer* buffer = new MxDSBuffer();
Expand All @@ -294,91 +295,74 @@ MxResult MxDiskStreamProvider::FUN_100d1b20(MxDSStreamingAction* p_action)
return FAILURE;
}

MxU32 size = p_action->GetUnknowna0()->GetWriteOffset() - p_action->GetUnknown94() + p_action->GetBufferOffset() +
(p_action->GetUnknowna4() ? p_action->GetUnknowna4()->GetWriteOffset() : 0);
MxU32 size = (p_action->GetUnknowna4() ? p_action->GetUnknowna4()->GetWriteOffset() : 0) +
p_action->GetUnknowna0()->GetWriteOffset() - (p_action->GetUnknown94() - p_action->GetBufferOffset());

if (buffer->AllocateBuffer(size, MxDSBuffer::e_allocate) != SUCCESS) {
if (!buffer) {
return FAILURE;
}

delete buffer;
return FAILURE;
}

MxDSBuffer* buffer2 = p_action->GetUnknowna4();
MxU8** pdata;
MxU8* data;

if (buffer2 == NULL) {
pdata = buffer->GetBufferRef();
if (p_action->GetUnknowna4()) {
buffer->FUN_100c7090(p_action->GetUnknowna4());
data = buffer->GetBuffer() + p_action->GetUnknowna4()->GetWriteOffset();

memcpy(
data = *pdata,
p_action->GetUnknowna0()->GetBuffer() - p_action->GetBufferOffset() + p_action->GetUnknown94(),
size
);
memcpy(data, p_action->GetUnknowna0()->GetBuffer(), p_action->GetUnknowna0()->GetWriteOffset());

delete p_action->GetUnknowna4();
}
else {
buffer->FUN_100c7090(buffer2);
pdata = buffer->GetBufferRef();
data = buffer->GetBuffer();

memcpy(
data = (p_action->GetUnknowna4()->GetWriteOffset() + *pdata),
p_action->GetUnknowna0()->GetBuffer(),
p_action->GetUnknowna0()->GetWriteOffset()
data,
p_action->GetUnknowna0()->GetBuffer() + (p_action->GetUnknown94() - p_action->GetBufferOffset()),
size
);

delete p_action->GetUnknowna4();
}

p_action->SetUnknowna4(buffer);

#define IntoType(p) ((MxU32*) (p))

while (data) {
if (*MxDSChunk::IntoType(data) != FOURCC('M', 'x', 'O', 'b')) {
if (*MxStreamChunk::IntoTime(data) > p_action->GetUnknown9c()) {
*MxDSChunk::IntoType(data) = FOURCC('p', 'a', 'd', ' ');
if (*IntoType(data) != FOURCC('M', 'x', 'O', 'b') &&
*MxStreamChunk::IntoTime(data) > p_action->GetUnknown9c()) {
*IntoType(data) = FOURCC('p', 'a', 'd', ' ');

// DECOMP: prefer order that matches retail versus beta
*(MxU32*) (data + 4) = buffer->GetBuffer() + buffer->GetWriteOffset() - data - 8;
memset(data + 8, 0, *(MxU32*) (data + 4));
size = ReadData(buffer->GetBuffer(), buffer->GetWriteOffset());

buffer = new MxDSBuffer();
if (buffer == NULL || buffer->AllocateBuffer(size, MxDSBuffer::e_allocate) != SUCCESS) {
delete buffer;
return FAILURE;
}

memcpy(data + 8, *pdata, buffer->GetWriteOffset() + *pdata - data - 8);
size = ReadData(*pdata, buffer->GetWriteOffset());
memcpy(buffer->GetBuffer(), p_action->GetUnknowna4()->GetBuffer(), size);
p_action->GetUnknowna4()->SetMode(MxDSBuffer::e_allocate);
delete p_action->GetUnknowna4();

MxDSBuffer* buffer3 = new MxDSBuffer();
if (!buffer3) {
return FAILURE;
}
buffer->SetMode(MxDSBuffer::e_unknown);
p_action->SetUnknowna4(buffer);
MxU32 unk0x14 = p_action->GetUnknowna0()->GetUnknown14();

if (buffer3->AllocateBuffer(size, MxDSBuffer::e_allocate) == SUCCESS) {
memcpy(buffer3->GetBuffer(), p_action->GetUnknowna4()->GetBuffer(), size);
p_action->GetUnknowna4()->SetMode(MxDSBuffer::e_allocate);
delete p_action->GetUnknowna4();

buffer3->SetMode(MxDSBuffer::e_unknown);
p_action->SetUnknowna4(buffer3);
MxDSBuffer* buffer4 = p_action->GetUnknowna0();
MxU32 unk0x14 = buffer4->GetUnknown14();
MxU8* data2 = buffer4->GetBuffer();

while (TRUE) {
if (*MxStreamChunk::IntoTime(data2) > p_action->GetUnknown9c()) {
break;
}

data += MxDSChunk::Size(*MxDSChunk::IntoLength(data));
unk0x14 += MxDSChunk::Size(*MxDSChunk::IntoLength(data));
}

p_action->SetUnknown94(unk0x14);
p_action->SetBufferOffset(p_action->GetUnknowna0()->GetUnknown14());
delete p_action->GetUnknowna0();
p_action->SetUnknowna0(NULL);
((MxDiskStreamController*) m_pLookup)->FUN_100c7890(p_action);
return SUCCESS;
}
else {
delete buffer3;
return FAILURE;
}
for (data = p_action->GetUnknowna0()->GetBuffer();
*MxStreamChunk::IntoTime(data) <= p_action->GetUnknown9c();
data = MxDSChunk::End(data)) {
unk0x14 += MxDSChunk::Size(data);
}

p_action->SetUnknown94(unk0x14);
p_action->SetBufferOffset(p_action->GetUnknowna0()->GetUnknown14());
delete p_action->GetUnknowna0();
p_action->ClearUnknowna0();
((MxDiskStreamController*) m_pLookup)->FUN_100c7890(p_action);
return SUCCESS;
}

data = buffer->FUN_100c6fa0(data);
Expand All @@ -388,6 +372,8 @@ MxResult MxDiskStreamProvider::FUN_100d1b20(MxDSStreamingAction* p_action)
p_action->SetBufferOffset(GetFileSize() + p_action->GetBufferOffset());
FUN_100d1780(p_action);
return SUCCESS;

#undef IntoType
}

// FUNCTION: LEGO1 0x100d1e90
Expand Down
1 change: 1 addition & 0 deletions LEGO1/omni/src/stream/mxdsbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ MxU8* MxDSBuffer::FUN_100c6fa0(MxU8* p_data)
}

// FUNCTION: LEGO1 0x100c7090
// FUNCTION: BETA10 0x1015842d
MxResult MxDSBuffer::FUN_100c7090(MxDSBuffer* p_buf)
{
MxResult result = FAILURE;
Expand Down
87 changes: 44 additions & 43 deletions LEGO1/omni/src/stream/mxramstreamprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,62 +102,63 @@ MxResult MxRAMStreamProvider::SetResourceToGet(MxStreamController* p_resource)
}

// FUNCTION: LEGO1 0x100d0d80
// FUNCTION: BETA10 0x1016492f
MxU32 ReadData(MxU8* p_buffer, MxU32 p_size)
{
MxU32 id;
MxU8* data = p_buffer;
MxU8* end = p_buffer + p_size;
MxU8* data2;

if (p_buffer < end) {
do {
if (*MxDSChunk::IntoType(data) == FOURCC('M', 'x', 'O', 'b')) {
data2 = data;
data += 8;

MxDSObject* obj = DeserializeDSObjectDispatch(data, -1);
id = obj->GetObjectId();
delete obj;

data = MxDSChunk::End(data2);
while (data < end) {
if (*MxDSChunk::IntoType(data) == FOURCC('M', 'x', 'C', 'h')) {
MxU8* data3 = data;
MxU32* psize = MxDSChunk::IntoLength(data);
data += MxDSChunk::Size(*psize);

if ((*MxDSChunk::IntoType(data2) == FOURCC('M', 'x', 'C', 'h')) &&
(*MxStreamChunk::IntoFlags(data2) & DS_CHUNK_SPLIT)) {
if (*MxStreamChunk::IntoObjectId(data2) == *MxStreamChunk::IntoObjectId(data3) &&
(*MxStreamChunk::IntoFlags(data3) & DS_CHUNK_SPLIT) &&
*MxStreamChunk::IntoTime(data2) == *MxStreamChunk::IntoTime(data3)) {
MxDSBuffer::Append(data2, data3);
continue;
}
else {
*MxStreamChunk::IntoFlags(data2) &= ~DS_CHUNK_SPLIT;
}
#define IntoType(p) ((MxU32*) (p))

while (data < p_buffer + p_size) {
if (*IntoType(data) == FOURCC('M', 'x', 'O', 'b')) {
data2 = data;
data = data2 + 8;

MxDSObject* obj = DeserializeDSObjectDispatch(data, -1);
id = obj->GetObjectId();
delete obj;

data = MxDSChunk::End(data2);
while (data < p_buffer + p_size) {
if (*IntoType(data) == FOURCC('M', 'x', 'C', 'h')) {
MxU8* data3 = data;
data = MxDSChunk::End(data3);

if ((*IntoType(data2) == FOURCC('M', 'x', 'C', 'h')) &&
(*MxStreamChunk::IntoFlags(data2) & DS_CHUNK_SPLIT)) {
if (*MxStreamChunk::IntoObjectId(data2) == *MxStreamChunk::IntoObjectId(data3) &&
(*MxStreamChunk::IntoFlags(data3) & DS_CHUNK_SPLIT) &&
*MxStreamChunk::IntoTime(data2) == *MxStreamChunk::IntoTime(data3)) {
MxDSBuffer::Append(data2, data3);
continue;
}

data2 += MxDSChunk::Size(*MxDSChunk::IntoLength(data2));
memcpy(data2, data3, MxDSChunk::Size(*psize));

if (*MxStreamChunk::IntoObjectId(data2) == id &&
(*MxStreamChunk::IntoFlags(data2) & DS_CHUNK_END_OF_STREAM)) {
break;
else {
*MxStreamChunk::IntoFlags(data2) &= ~DS_CHUNK_SPLIT;
}
}
else {
data++;

data2 = MxDSChunk::End(data2);
memcpy(data2, data3, MxDSChunk::Size(data3));

if (*MxStreamChunk::IntoObjectId(data2) == id &&
(*MxStreamChunk::IntoFlags(data2) & DS_CHUNK_END_OF_STREAM)) {
break;
}
}
else {
data++;
}
}
else {
data++;
}
} while (data < end);
}
else {
data++;
}
}

*MxStreamChunk::IntoFlags(data2) &= ~DS_CHUNK_SPLIT;
return MxDSChunk::End(data2) - p_buffer;
return MxDSChunk::Size(data2) + (MxU32) (data2 - p_buffer);

#undef IntoType
}
1 change: 1 addition & 0 deletions LEGO1/omni/src/stream/mxstreamchunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ MxU32* MxStreamChunk::IntoObjectId(MxU8* p_buffer)
}

// FUNCTION: LEGO1 0x100c31a0
// FUNCTION: BETA10 0x10151626
MxLong* MxStreamChunk::IntoTime(MxU8* p_buffer)
{
return (MxLong*) (p_buffer + 0x0e);
Expand Down

0 comments on commit 93815ca

Please sign in to comment.