From ef8b4c7d5e2a8b8aff536b641646dc3f293369ef Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 11 Apr 2020 19:30:09 +0200 Subject: [PATCH] - put the state pointer serializer in a virtual function so that types.cpp can operate without any knowledge of Doom states. --- src/common/engine/serializer.cpp | 12 ++++ src/common/engine/serializer.h | 3 + src/common/scripting/core/types.cpp | 90 +++++++++++++---------------- src/serializer_doom.cpp | 21 +++++++ src/serializer_doom.h | 3 +- 5 files changed, 78 insertions(+), 51 deletions(-) diff --git a/src/common/engine/serializer.cpp b/src/common/engine/serializer.cpp index 263df178f..ae2f872f9 100644 --- a/src/common/engine/serializer.cpp +++ b/src/common/engine/serializer.cpp @@ -445,6 +445,18 @@ FSerializer &FSerializer::Sprite(const char *key, int32_t &spritenum, int32_t *d return *this; } +//========================================================================== +// +// only here so that it can be virtually overridden. Without reference this cannot save anything. +// +//========================================================================== + +FSerializer& FSerializer::StatePointer(const char* key, void* ptraddr, bool *res) +{ + if (res) *res = false; + return *this; +} + //========================================================================== // // diff --git a/src/common/engine/serializer.h b/src/common/engine/serializer.h index 708d17d1e..fe20f247b 100644 --- a/src/common/engine/serializer.h +++ b/src/common/engine/serializer.h @@ -93,6 +93,9 @@ public: FCompressedBuffer GetCompressedOutput(); // The sprite serializer is a special case because it is needed by the VM to handle its 'spriteid' type. virtual FSerializer &Sprite(const char *key, int32_t &spritenum, int32_t *def); + // This is only needed by the type system. + virtual FSerializer& StatePointer(const char* key, void* ptraddr, bool *res); + FSerializer &StringPtr(const char *key, const char *&charptr); // This only retrieves the address but creates no permanent copy of the string unlike the regular char* serializer. FSerializer &AddString(const char *key, const char *charptr); const char *GetString(const char *key); diff --git a/src/common/scripting/core/types.cpp b/src/common/scripting/core/types.cpp index f9a8074a6..447af4601 100644 --- a/src/common/scripting/core/types.cpp +++ b/src/common/scripting/core/types.cpp @@ -38,7 +38,6 @@ #include "types.h" #include "printf.h" #include "textureid.h" -#include "version.h" FTypeTable TypeTable; @@ -1079,6 +1078,46 @@ bool PName::ReadValue(FSerializer &ar, const char *key, void *addr) const } } +/* PStatePointer **********************************************************/ + +//========================================================================== +// +// PStatePointer Default Constructor +// +//========================================================================== + +PStatePointer::PStatePointer() +{ + mDescriptiveName = "Pointer"; + PointedType = NewStruct(NAME_State, nullptr, true); + IsConst = true; +} + +//========================================================================== +// +// PStatePointer :: WriteValue +// +//========================================================================== + +void PStatePointer::WriteValue(FSerializer& ar, const char* key, const void* addr) const +{ + ar.StatePointer(key, const_cast(addr), nullptr); +} + +//========================================================================== +// +// PStatePointer :: ReadValue +// +//========================================================================== + +bool PStatePointer::ReadValue(FSerializer& ar, const char* key, void* addr) const +{ + bool res = false; + ar.StatePointer(key, addr, &res); + return res; +} + + /* PSpriteID ******************************************************************/ //========================================================================== @@ -2524,52 +2563,3 @@ CCMD(typetable) DumpTypeTable(); } -// fixme: The VM depends on this but it's engine specific. Needs a better solution. - -/* PStatePointer **********************************************************/ -#ifdef GZDOOM -#include "info.h" -#include "serializer_doom.h" -#endif - -//========================================================================== -// -// PStatePointer Default Constructor -// -//========================================================================== - -PStatePointer::PStatePointer() -{ - mDescriptiveName = "Pointer"; - PointedType = NewStruct(NAME_State, nullptr, true); - IsConst = true; -} - -//========================================================================== -// -// PStatePointer :: WriteValue -// -//========================================================================== - -void PStatePointer::WriteValue(FSerializer& ar, const char* key, const void* addr) const -{ -#ifdef GZDOOM - ar(key, *(FState**)addr); -#endif -} - -//========================================================================== -// -// PStatePointer :: ReadValue -// -//========================================================================== - -bool PStatePointer::ReadValue(FSerializer& ar, const char* key, void* addr) const -{ - bool res = false; -#ifdef GZDOOM - ::Serialize(ar, key, *(FState**)addr, nullptr, &res); -#endif - return res; -} - diff --git a/src/serializer_doom.cpp b/src/serializer_doom.cpp index 2ed1e5181..250e275be 100644 --- a/src/serializer_doom.cpp +++ b/src/serializer_doom.cpp @@ -165,6 +165,27 @@ FSerializer &FDoomSerializer::Sprite(const char *key, int32_t &spritenum, int32_ return *this; } +//========================================================================== +// +// +// +//========================================================================== + +FSerializer& FDoomSerializer::StatePointer(const char* key, void* ptraddr, bool *res) +{ + if (isWriting()) + { + if (res) *res = true; + (*this)(key, *(FState**)ptraddr); + } + else + { + ::Serialize(*this, key, *(FState**)ptraddr, nullptr, res); + } + return *this; +} + + template<> FSerializer &Serialize(FSerializer &ar, const char *key, FPolyObj *&value, FPolyObj **defval) { diff --git a/src/serializer_doom.h b/src/serializer_doom.h index 95256396d..26a71426f 100644 --- a/src/serializer_doom.h +++ b/src/serializer_doom.h @@ -29,7 +29,8 @@ public: {} FSerializer &Sprite(const char *key, int32_t &spritenum, int32_t *def) override; - + FSerializer& StatePointer(const char* key, void* ptraddr, bool *res) override; + }; FSerializer &SerializeArgs(FSerializer &arc, const char *key, int *args, int *defargs, int special);