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; 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 *); ExtraActionType MatchActionString(const char *);
void ConvertHSVToRGB(float r, float g, float b, float* out_r, float* out_g, float* out_b); void ConvertHSVToRGB(float r, float g, float b, float* out_r, float* out_g, float* out_b);
void SetAppCursor(WPARAM p_wparam); void SetAppCursor(WPARAM p_wparam);

View file

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

View file

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

View file

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

View file

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