2023-07-07 12:20:51 -04:00
|
|
|
|
|
|
|
#include "legostream.h"
|
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
#include "mxvariabletable.h"
|
|
|
|
|
2023-07-07 12:20:51 -04:00
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
|
2023-10-12 12:18:24 -04:00
|
|
|
// This is a pointer to the end of the global variable name table, which has
|
|
|
|
// the text "END_OF_VARIABLES" in it.
|
|
|
|
// TODO: make s_endOfVariables reference the actual end of the variable array.
|
2023-12-06 07:10:45 -05:00
|
|
|
// GLOBAL: LEGO1 0x100f3e50
|
2023-10-24 19:38:27 -04:00
|
|
|
const char* s_endOfVariables = "END_OF_VARIABLES";
|
2023-10-12 12:18:24 -04:00
|
|
|
|
2023-07-07 12:20:51 -04:00
|
|
|
// Very likely but not certain sizes.
|
|
|
|
// The classes are only used on the stack in functions we have not 100% matched
|
|
|
|
// yet, we can confirm the size once we have.
|
|
|
|
DECOMP_SIZE_ASSERT(LegoStream, 0x8);
|
|
|
|
DECOMP_SIZE_ASSERT(LegoFileStream, 0xC);
|
|
|
|
DECOMP_SIZE_ASSERT(LegoMemoryStream, 0x10);
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10039f70
|
2023-11-21 03:44:45 -05:00
|
|
|
MxResult LegoStream::WriteVariable(LegoStream* p_stream, MxVariableTable* p_from, const char* p_variableName)
|
|
|
|
{
|
|
|
|
MxResult result = FAILURE;
|
|
|
|
const char* variableValue = p_from->GetVariable(p_variableName);
|
|
|
|
|
|
|
|
if (variableValue) {
|
|
|
|
MxU8 length = strlen(p_variableName);
|
|
|
|
if (p_stream->Write((char*) &length, 1) == SUCCESS) {
|
|
|
|
if (p_stream->Write(p_variableName, length) == SUCCESS) {
|
|
|
|
length = strlen(variableValue);
|
|
|
|
if (p_stream->Write((char*) &length, 1) == SUCCESS)
|
|
|
|
result = p_stream->Write((char*) variableValue, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 95% match, just some instruction ordering differences on the call to
|
|
|
|
// MxVariableTable::SetVariable at the end.
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x1003a080
|
2023-11-21 03:44:45 -05:00
|
|
|
MxS32 LegoStream::ReadVariable(LegoStream* p_stream, MxVariableTable* p_to)
|
|
|
|
{
|
|
|
|
MxS32 result = 1;
|
|
|
|
MxU8 length;
|
|
|
|
|
|
|
|
if (p_stream->Read((char*) &length, 1) == SUCCESS) {
|
|
|
|
char nameBuffer[256];
|
|
|
|
if (p_stream->Read(nameBuffer, length) == SUCCESS) {
|
|
|
|
nameBuffer[length] = '\0';
|
|
|
|
if (strcmp(nameBuffer, s_endOfVariables) == 0)
|
|
|
|
// 2 -> "This was the last entry, done reading."
|
|
|
|
result = 2;
|
|
|
|
else {
|
|
|
|
if (p_stream->Read((char*) &length, 1) == SUCCESS) {
|
|
|
|
char valueBuffer[256];
|
|
|
|
if (p_stream->Read(valueBuffer, length) == SUCCESS) {
|
|
|
|
result = 0;
|
|
|
|
valueBuffer[length] = '\0';
|
|
|
|
p_to->SetVariable(nameBuffer, valueBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10045ae0
|
2023-07-07 12:20:51 -04:00
|
|
|
MxBool LegoStream::IsWriteMode()
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
return m_mode == LEGOSTREAM_MODE_WRITE;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10045af0
|
2023-07-07 12:20:51 -04:00
|
|
|
MxBool LegoStream::IsReadMode()
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
return m_mode == LEGOSTREAM_MODE_READ;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10099080
|
2023-11-21 03:44:45 -05:00
|
|
|
LegoMemoryStream::LegoMemoryStream(char* p_buffer) : LegoStream()
|
|
|
|
{
|
|
|
|
m_buffer = p_buffer;
|
|
|
|
m_offset = 0;
|
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10099160
|
2023-11-21 03:44:45 -05:00
|
|
|
MxResult LegoMemoryStream::Read(void* p_buffer, MxU32 p_size)
|
|
|
|
{
|
|
|
|
memcpy(p_buffer, m_buffer + m_offset, p_size);
|
|
|
|
m_offset += p_size;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10099190
|
2023-11-21 03:44:45 -05:00
|
|
|
MxResult LegoMemoryStream::Write(const void* p_buffer, MxU32 p_size)
|
|
|
|
{
|
|
|
|
memcpy(m_buffer + m_offset, p_buffer, p_size);
|
|
|
|
m_offset += p_size;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100991c0
|
2023-10-24 19:38:27 -04:00
|
|
|
LegoFileStream::LegoFileStream() : LegoStream()
|
2023-07-07 12:20:51 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
m_hFile = NULL;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10099250
|
2023-07-07 12:20:51 -04:00
|
|
|
LegoFileStream::~LegoFileStream()
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
if (m_hFile != NULL)
|
|
|
|
fclose(m_hFile);
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100992c0
|
2023-10-12 12:18:24 -04:00
|
|
|
MxResult LegoFileStream::Read(void* p_buffer, MxU32 p_size)
|
2023-07-07 12:20:51 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
if (m_hFile == NULL)
|
|
|
|
return FAILURE;
|
2023-07-07 12:20:51 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
return (fread(p_buffer, 1, p_size, m_hFile) == p_size) ? SUCCESS : FAILURE;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10099300
|
2023-10-12 12:18:24 -04:00
|
|
|
MxResult LegoFileStream::Write(const void* p_buffer, MxU32 p_size)
|
2023-07-07 12:20:51 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
if (m_hFile == NULL)
|
|
|
|
return FAILURE;
|
2023-07-07 12:20:51 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
return (fwrite(p_buffer, 1, p_size, m_hFile) == p_size) ? SUCCESS : FAILURE;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10099340
|
2023-07-07 12:20:51 -04:00
|
|
|
MxResult LegoFileStream::Tell(MxU32* p_offset)
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
if (m_hFile == NULL)
|
|
|
|
return FAILURE;
|
2023-07-07 12:20:51 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
int got = ftell(m_hFile);
|
|
|
|
if (got == -1)
|
|
|
|
return FAILURE;
|
2023-07-07 12:20:51 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
*p_offset = got;
|
|
|
|
return SUCCESS;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x10099370
|
2023-07-07 12:20:51 -04:00
|
|
|
MxResult LegoFileStream::Seek(MxU32 p_offset)
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
if (m_hFile == NULL)
|
|
|
|
return FAILURE;
|
2023-07-07 12:20:51 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
return (fseek(m_hFile, p_offset, 0) == 0) ? SUCCESS : FAILURE;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100993a0
|
2023-07-07 12:20:51 -04:00
|
|
|
MxResult LegoFileStream::Open(const char* p_filename, OpenFlags p_mode)
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
char modeString[4];
|
|
|
|
|
|
|
|
if (m_hFile != NULL)
|
|
|
|
fclose(m_hFile);
|
2023-07-07 12:20:51 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
modeString[0] = '\0';
|
|
|
|
if (p_mode & ReadBit) {
|
|
|
|
m_mode = LEGOSTREAM_MODE_READ;
|
|
|
|
strcat(modeString, "r");
|
|
|
|
}
|
2023-07-07 12:20:51 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
if (p_mode & WriteBit) {
|
|
|
|
if (m_mode != LEGOSTREAM_MODE_READ)
|
|
|
|
m_mode = LEGOSTREAM_MODE_WRITE;
|
|
|
|
strcat(modeString, "w");
|
|
|
|
}
|
2023-07-07 12:20:51 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
if ((p_mode & 4) != 0)
|
|
|
|
strcat(modeString, "b");
|
|
|
|
else
|
|
|
|
strcat(modeString, "t");
|
2023-07-07 12:20:51 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
return (m_hFile = fopen(p_filename, modeString)) ? SUCCESS : FAILURE;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100994a0
|
2023-07-07 12:20:51 -04:00
|
|
|
MxResult LegoMemoryStream::Tell(MxU32* p_offset)
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
*p_offset = m_offset;
|
|
|
|
return SUCCESS;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100994b0
|
2023-07-07 12:20:51 -04:00
|
|
|
MxResult LegoMemoryStream::Seek(MxU32 p_offset)
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
m_offset = p_offset;
|
|
|
|
return SUCCESS;
|
2023-07-07 12:20:51 -04:00
|
|
|
}
|