Refactor deserialization of objects (#170)

* Use template helpers for deserialization

* Remove space

* Fix naming
This commit is contained in:
Christian Semmler 2023-10-07 14:07:26 -04:00 committed by GitHub
parent 62b97b8f1f
commit a9fa734844
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 58 deletions

View file

@ -23,6 +23,35 @@ inline T Max(T p_t1, T p_t2)
return p_t1 > p_t2 ? p_t1 : p_t2;
}
template <class T>
inline void GetScalar(char **p_source, T& p_dest)
{
p_dest = *(T*) *p_source;
*p_source += sizeof(T);
}
template <class T>
inline T GetScalar(T **p_source)
{
T val = **p_source;
*p_source += 1;
return val;
}
template <class T>
inline void GetDouble(char **p_source, T& p_dest)
{
p_dest = *(double*) *p_source;
*p_source += sizeof(double);
}
template <class T>
inline void GetString(char **p_source, const char *&p_dest, T *p_obj, void (T::*p_setter)(const char*))
{
(p_obj->*p_setter)(*p_source);
*p_source += strlen(p_dest) + 1;
}
ExtraActionType MatchActionString(const char *);
void ConvertHSVToRGB(float r, float g, float b, float* out_r, float* out_g, float* out_b);
void SetAppCursor(WPARAM p_wparam);

View file

@ -2,6 +2,7 @@
#include "mxomni.h"
#include "mxtimer.h"
#include "legoutil.h"
#include <float.h>
#include <limits.h>
@ -84,38 +85,24 @@ void MxDSAction::Deserialize(char **p_source, MxS16 p_unk24)
{
MxDSObject::Deserialize(p_source, p_unk24);
this->m_flags = *(MxU32*) *p_source;
*p_source += sizeof(MxU32);
this->m_startTime = *(MxLong*) *p_source;
*p_source += sizeof(MxLong);
this->m_duration = *(MxLong*) *p_source;
*p_source += sizeof(MxLong);
this->m_loopCount = *(MxS32*) *p_source;
*p_source += sizeof(MxS32);
this->m_location[0] = *(double*) *p_source;
*p_source += sizeof(double);
this->m_location[1] = *(double*) *p_source;
*p_source += sizeof(double);
this->m_location[2] = *(double*) *p_source;
*p_source += sizeof(double);
this->m_direction[0] = *(double*) *p_source;
*p_source += sizeof(double);
this->m_direction[1] = *(double*) *p_source;
*p_source += sizeof(double);
this->m_direction[2] = *(double*) *p_source;
*p_source += sizeof(double);
this->m_up[0] = *(double*) *p_source;
*p_source += sizeof(double);
this->m_up[1] = *(double*) *p_source;
*p_source += sizeof(double);
this->m_up[2] = *(double*) *p_source;
*p_source += sizeof(double);
GetScalar(p_source, this->m_flags);
GetScalar(p_source, this->m_startTime);
GetScalar(p_source, this->m_duration);
GetScalar(p_source, this->m_loopCount);
GetDouble(p_source, this->m_location[0]);
GetDouble(p_source, this->m_location[1]);
GetDouble(p_source, this->m_location[2]);
GetDouble(p_source, this->m_direction[0]);
GetDouble(p_source, this->m_direction[1]);
GetDouble(p_source, this->m_direction[2]);
GetDouble(p_source, this->m_up[0]);
GetDouble(p_source, this->m_up[1]);
GetDouble(p_source, this->m_up[2]);
MxU16 unkLength = *(MxU16*) *p_source;
*p_source += sizeof(MxU16);
if (unkLength) {
AppendData(unkLength, *p_source);
*p_source += unkLength;
MxU16 extraLength = GetScalar((MxU16**) p_source);
if (extraLength) {
AppendData(extraLength, *p_source);
*p_source += extraLength;
}
}

View file

@ -1,5 +1,7 @@
#include "mxdsmediaaction.h"
#include "legoutil.h"
DECOMP_SIZE_ASSERT(MxDSMediaAction, 0xb8)
// OFFSET: LEGO1 0x100c8b40
@ -68,21 +70,13 @@ void MxDSMediaAction::Deserialize(char **p_source, MxS16 p_unk24)
{
MxDSAction::Deserialize(p_source, p_unk24);
CopyMediaSrcPath(*p_source);
*p_source += strlen(this->m_mediaSrcPath) + 1;
this->m_unk9c = *(undefined4*) *p_source;
*p_source += sizeof(undefined4);
this->m_unka0 = *(undefined4*) *p_source;
*p_source += sizeof(undefined4);
this->m_framesPerSecond = *(MxS32*) *p_source;
*p_source += sizeof(MxS32);
this->m_mediaFormat = *(MxS32*) *p_source;
*p_source += sizeof(MxS32);
this->m_paletteManagement = *(MxS32*) *p_source;
*p_source += sizeof(MxS32);
this->m_sustainTime = *(MxLong*) *p_source;
*p_source += sizeof(MxLong);
GetString(p_source, this->m_mediaSrcPath, this, &MxDSMediaAction::CopyMediaSrcPath);
GetScalar(p_source, this->m_unk9c);
GetScalar(p_source, this->m_unka0);
GetScalar(p_source, this->m_framesPerSecond);
GetScalar(p_source, this->m_mediaFormat);
GetScalar(p_source, this->m_paletteManagement);
GetScalar(p_source, this->m_sustainTime);
}
// OFFSET: LEGO1 0x100c8e80

View file

@ -15,6 +15,7 @@
#include "mxdsselectaction.h"
#include "mxdsstill.h"
#include "mxdsobjectaction.h"
#include "legoutil.h"
DECOMP_SIZE_ASSERT(MxDSObject, 0x2c);
@ -128,20 +129,14 @@ MxU32 MxDSObject::GetSizeOnDisk()
// OFFSET: LEGO1 0x100bfa20
void MxDSObject::Deserialize(char **p_source, MxS16 p_unk24)
{
this->SetSourceName(*p_source);
*p_source += strlen(this->m_sourceName) + 1;
this->m_unk14 = *(undefined4*) *p_source;
*p_source += sizeof(undefined4);
this->SetObjectName(*p_source);
*p_source += strlen(this->m_objectName) + 1;
this->m_objectId = *(MxU32*) *p_source;
*p_source += sizeof(MxU32);
GetString(p_source, this->m_sourceName, this, &MxDSObject::SetSourceName);
GetScalar(p_source, this->m_unk14);
GetString(p_source, this->m_objectName, this, &MxDSObject::SetObjectName);
GetScalar(p_source, this->m_objectId);
this->m_unk24 = p_unk24;
}
// OFFSET: LEGO1 0x100bfb30
MxDSObject *DeserializeDSObjectDispatch(char **p_source, MxS16 p_flags)
{

View file

@ -1,4 +1,5 @@
#include "mxdssound.h"
#include "legoutil.h"
DECOMP_SIZE_ASSERT(MxDSSound, 0xc0)
@ -46,8 +47,7 @@ void MxDSSound::Deserialize(char **p_source, MxS16 p_unk24)
{
MxDSMediaAction::Deserialize(p_source, p_unk24);
this->m_volume = *(MxS32*) *p_source;
*p_source += sizeof(MxS32);
GetScalar(p_source, this->m_volume);
}
// OFFSET: LEGO1 0x100c9510