Codegen integration + string enum serialization support

This commit is contained in:
Aruki 2018-07-30 20:33:51 -06:00
parent 6a72bae97a
commit 91650a2924
13 changed files with 98 additions and 32 deletions

3
.gitmodules vendored
View File

@ -10,3 +10,6 @@
[submodule "externals/nod"] [submodule "externals/nod"]
path = externals/nod path = externals/nod
url = https://github.com/arukibree/nod url = https://github.com/arukibree/nod
[submodule "externals/CodeGen"]
path = externals/CodeGen
url = https://github.com/arukibree/CodeGen

View File

@ -31,6 +31,7 @@ CONFIG (release, debug|release) {
# Include Paths # Include Paths
INCLUDEPATH += $$PWE_MAIN_INCLUDE \ INCLUDEPATH += $$PWE_MAIN_INCLUDE \
$$EXTERNALS_DIR/CodeGen/include \
$$EXTERNALS_DIR/tinyxml2 $$EXTERNALS_DIR/tinyxml2
# Header Files # Header Files
@ -40,7 +41,6 @@ HEADERS += \
CTimer.h \ CTimer.h \
EKeyInputs.h \ EKeyInputs.h \
EMouseInputs.h \ EMouseInputs.h \
ETransformSpace.h \
Flags.h \ Flags.h \
TString.h \ TString.h \
types.h \ types.h \
@ -101,5 +101,10 @@ SOURCES += \
FileIO\CBitStreamInWrapper.cpp \ FileIO\CBitStreamInWrapper.cpp \
Hash/CCRC32.cpp Hash/CCRC32.cpp
# Codegen
CODEGEN_OUT_PATH = $$BUILD_DIR/Common/codegen_build/auto_codegen.cpp
CODEGEN_SRC_PATH = $$PWD
include($$EXTERNALS_DIR/CodeGen/codegen.pri)
# Library Sources # Library Sources
SOURCES += $$EXTERNALS_DIR/tinyxml2/tinyxml2.cpp SOURCES += $$EXTERNALS_DIR/tinyxml2/tinyxml2.cpp

View File

@ -17,7 +17,7 @@ class CBasicBinaryReader : public IArchive
public: public:
CBasicBinaryReader(const TString& rkFilename, u32 Magic) CBasicBinaryReader(const TString& rkFilename, u32 Magic)
: IArchive(true, false) : IArchive()
, mOwnsStream(true) , mOwnsStream(true)
{ {
mpStream = new CFileInStream(rkFilename, IOUtil::eBigEndian); mpStream = new CFileInStream(rkFilename, IOUtil::eBigEndian);
@ -31,7 +31,7 @@ public:
} }
CBasicBinaryReader(IInputStream *pStream, const CSerialVersion& rkVersion) CBasicBinaryReader(IInputStream *pStream, const CSerialVersion& rkVersion)
: IArchive(true, false) : IArchive()
, mMagicValid(true) , mMagicValid(true)
, mOwnsStream(false) , mOwnsStream(false)
{ {
@ -41,7 +41,7 @@ public:
} }
CBasicBinaryReader(void *pData, u32 DataSize, const CSerialVersion& rkVersion, IOUtil::EEndianness Endian = IOUtil::kSystemEndianness) CBasicBinaryReader(void *pData, u32 DataSize, const CSerialVersion& rkVersion, IOUtil::EEndianness Endian = IOUtil::kSystemEndianness)
: IArchive(true, false) : IArchive()
, mMagicValid(true) , mMagicValid(true)
, mOwnsStream(true) , mOwnsStream(true)
{ {
@ -57,6 +57,10 @@ public:
inline bool IsValid() const { return mpStream->IsValid(); } inline bool IsValid() const { return mpStream->IsValid(); }
// Interface // Interface
virtual bool IsReader() const { return true; }
virtual bool IsWriter() const { return false; }
virtual bool IsTextFormat() const { return false; }
virtual bool ParamBegin(const char*) { return true; } virtual bool ParamBegin(const char*) { return true; }
virtual void ParamEnd() { } virtual void ParamEnd() { }

View File

@ -16,7 +16,7 @@ class CBasicBinaryWriter : public IArchive
public: public:
CBasicBinaryWriter(const TString& rkFilename, u32 Magic, u16 FileVersion, EGame Game) CBasicBinaryWriter(const TString& rkFilename, u32 Magic, u16 FileVersion, EGame Game)
: IArchive(false, true) : IArchive()
, mMagic(Magic) , mMagic(Magic)
, mOwnsStream(true) , mOwnsStream(true)
{ {
@ -31,7 +31,7 @@ public:
} }
CBasicBinaryWriter(IOutputStream *pStream, u16 FileVersion, EGame Game) CBasicBinaryWriter(IOutputStream *pStream, u16 FileVersion, EGame Game)
: IArchive(false, true) : IArchive()
, mOwnsStream(false) , mOwnsStream(false)
{ {
ASSERT(pStream->IsValid()); ASSERT(pStream->IsValid());
@ -40,7 +40,7 @@ public:
} }
CBasicBinaryWriter(IOutputStream *pStream, const CSerialVersion& rkVersion) CBasicBinaryWriter(IOutputStream *pStream, const CSerialVersion& rkVersion)
: IArchive(false, true) : IArchive()
, mOwnsStream(false) , mOwnsStream(false)
{ {
ASSERT(pStream->IsValid()); ASSERT(pStream->IsValid());
@ -62,6 +62,10 @@ public:
inline bool IsValid() const { return mpStream->IsValid(); } inline bool IsValid() const { return mpStream->IsValid(); }
// Interface // Interface
virtual bool IsReader() const { return false; }
virtual bool IsWriter() const { return true; }
virtual bool IsTextFormat() const { return false; }
virtual bool ParamBegin(const char*) { return true; } virtual bool ParamBegin(const char*) { return true; }
virtual void ParamEnd() { } virtual void ParamEnd() { }

View File

@ -23,7 +23,7 @@ class CBinaryReader : public IArchive
public: public:
CBinaryReader(const TString& rkFilename, u32 Magic) CBinaryReader(const TString& rkFilename, u32 Magic)
: IArchive(true, false) : IArchive()
, mOwnsStream(true) , mOwnsStream(true)
{ {
mpStream = new CFileInStream(rkFilename, IOUtil::eBigEndian); mpStream = new CFileInStream(rkFilename, IOUtil::eBigEndian);
@ -39,7 +39,7 @@ public:
} }
CBinaryReader(IInputStream *pStream, const CSerialVersion& rkVersion) CBinaryReader(IInputStream *pStream, const CSerialVersion& rkVersion)
: IArchive(true, false) : IArchive()
, mMagicValid(true) , mMagicValid(true)
, mOwnsStream(false) , mOwnsStream(false)
{ {
@ -70,6 +70,10 @@ private:
public: public:
// Interface // Interface
virtual bool IsReader() const { return true; }
virtual bool IsWriter() const { return false; }
virtual bool IsTextFormat() const { return false; }
u32 ReadSize() u32 ReadSize()
{ {
return (mArchiveVersion < eArVer_32BitBinarySize ? (u32) mpStream->ReadShort() : mpStream->ReadLong()); return (mArchiveVersion < eArVer_32BitBinarySize ? (u32) mpStream->ReadShort() : mpStream->ReadLong());

View File

@ -20,7 +20,7 @@ class CBinaryWriter : public IArchive
public: public:
CBinaryWriter(const TString& rkFilename, u32 Magic, u16 FileVersion, EGame Game) CBinaryWriter(const TString& rkFilename, u32 Magic, u16 FileVersion, EGame Game)
: IArchive(false, true) : IArchive()
, mMagic(Magic) , mMagic(Magic)
, mOwnsStream(true) , mOwnsStream(true)
{ {
@ -37,7 +37,7 @@ public:
} }
CBinaryWriter(IOutputStream *pStream, u16 FileVersion, EGame Game) CBinaryWriter(IOutputStream *pStream, u16 FileVersion, EGame Game)
: IArchive(false, true) : IArchive()
, mMagic(0) , mMagic(0)
, mOwnsStream(false) , mOwnsStream(false)
{ {
@ -48,7 +48,7 @@ public:
} }
CBinaryWriter(IOutputStream *pStream, const CSerialVersion& rkVersion) CBinaryWriter(IOutputStream *pStream, const CSerialVersion& rkVersion)
: IArchive(false, true) : IArchive()
, mMagic(0) , mMagic(0)
, mOwnsStream(false) , mOwnsStream(false)
{ {
@ -88,6 +88,10 @@ private:
public: public:
// Interface // Interface
virtual bool IsReader() const { return false; }
virtual bool IsWriter() const { return true; }
virtual bool IsTextFormat() const { return false; }
virtual bool ParamBegin(const char *pkName) virtual bool ParamBegin(const char *pkName)
{ {
// Update parent param // Update parent param

View File

@ -12,7 +12,7 @@ class CXMLReader : public IArchive
public: public:
CXMLReader(const TString& rkFileName) CXMLReader(const TString& rkFileName)
: IArchive(true, false) : IArchive()
, mJustEndedParam(false) , mJustEndedParam(false)
{ {
// Load XML and set current element to the root element; read version // Load XML and set current element to the root element; read version
@ -38,6 +38,10 @@ public:
} }
// Interface // Interface
virtual bool IsReader() const { return true; }
virtual bool IsWriter() const { return false; }
virtual bool IsTextFormat() const { return true; }
virtual bool ParamBegin(const char *pkName) virtual bool ParamBegin(const char *pkName)
{ {
ASSERT(IsValid()); ASSERT(IsValid());

View File

@ -15,7 +15,7 @@ class CXMLWriter : public IArchive
public: public:
CXMLWriter(const TString& rkFileName, const TString& rkRootName, u16 FileVersion, EGame Game = eUnknownGame) CXMLWriter(const TString& rkFileName, const TString& rkRootName, u16 FileVersion, EGame Game = eUnknownGame)
: IArchive(false, true) : IArchive()
, mOutFilename(rkFileName) , mOutFilename(rkFileName)
, mSaved(false) , mSaved(false)
{ {
@ -69,6 +69,10 @@ public:
} }
// Interface // Interface
virtual bool IsReader() const { return false; }
virtual bool IsWriter() const { return true; }
virtual bool IsTextFormat() const { return true; }
virtual bool ParamBegin(const char *pkName) virtual bool ParamBegin(const char *pkName)
{ {
ASSERT(IsValid()); ASSERT(IsValid());

View File

@ -167,6 +167,8 @@ public:
None); None);
}; };
#define ENABLE_FOR_SERIAL_TYPE(SType) typename std::enable_if<SerialType<ValType, IArchive>::Type == SerialType<ValType, IArchive>::##SType, int>::type = 0
// Actual archive class // Actual archive class
class IArchive class IArchive
{ {
@ -174,8 +176,6 @@ protected:
u16 mArchiveVersion; u16 mArchiveVersion;
u16 mFileVersion; u16 mFileVersion;
EGame mGame; EGame mGame;
bool mIsReader;
bool mIsWriter;
public: public:
enum EArchiveVersion enum EArchiveVersion
@ -187,18 +187,14 @@ public:
}; };
static const u32 skCurrentArchiveVersion = (eArVer_Max - 1); static const u32 skCurrentArchiveVersion = (eArVer_Max - 1);
IArchive(bool IsReader, bool IsWriter) IArchive()
: mFileVersion(0) : mFileVersion(0)
, mArchiveVersion(skCurrentArchiveVersion) , mArchiveVersion(skCurrentArchiveVersion)
, mGame(eUnknownGame) , mGame(eUnknownGame)
, mIsReader(IsReader)
, mIsWriter(IsWriter)
{} {}
virtual ~IArchive() {} virtual ~IArchive() {}
#define ENABLE_FOR_SERIAL_TYPE(SType) typename std::enable_if<SerialType<ValType, IArchive>::Type == SerialType<ValType, IArchive>::##SType, int>::type = 0
// Serialize primitives // Serialize primitives
template<typename ValType, ENABLE_FOR_SERIAL_TYPE(Primitive)> template<typename ValType, ENABLE_FOR_SERIAL_TYPE(Primitive)>
inline IArchive& operator<<(TSerialParameter<ValType> rParam) inline IArchive& operator<<(TSerialParameter<ValType> rParam)
@ -419,12 +415,15 @@ public:
BulkSerialize(InArray.data(), Size); BulkSerialize(InArray.data(), Size);
} }
// Meta
virtual bool IsReader() const = 0;
virtual bool IsWriter() const = 0;
virtual bool IsTextFormat() const = 0;
// Accessors // Accessors
inline u16 ArchiveVersion() const { return mArchiveVersion; } inline u16 ArchiveVersion() const { return mArchiveVersion; }
inline u16 FileVersion() const { return mFileVersion; } inline u16 FileVersion() const { return mFileVersion; }
inline EGame Game() const { return mGame; } inline EGame Game() const { return mGame; }
inline bool IsReader() const { return mIsReader; }
inline bool IsWriter() const { return mIsWriter; }
inline void SetVersion(u16 ArchiveVersion, u16 FileVersion, EGame Game) inline void SetVersion(u16 ArchiveVersion, u16 FileVersion, EGame Game)
{ {
@ -446,6 +445,32 @@ public:
} }
}; };
// Default enum serializer; can be overridden
#include <codegen/EnumReflection.h>
template<typename T, typename = typename std::enable_if<std::is_enum<T>::value>::type>
inline void Serialize(IArchive& Arc, T& Val)
{
if (Arc.IsTextFormat())
{
if (Arc.IsReader())
{
TString ValueName;
Arc.SerializePrimitive(ValueName);
Val = TEnumReflection<T>::ConvertStringToValue( *ValueName );
}
else
{
TString ValueName = TEnumReflection<T>::ConvertValueToString(Val);
Arc.SerializePrimitive(ValueName);
}
}
else
{
Arc.SerializePrimitive((u32&) Val);
}
}
// Container serialize methods // Container serialize methods
#include <list> #include <list>
#include <map> #include <map>

View File

@ -68,6 +68,7 @@ LIBS += -L$$EXTERNALS_DIR/glew-2.1.0/lib/Release/x64 -lglew32s \
# Include Paths # Include Paths
INCLUDEPATH += $$PWE_MAIN_INCLUDE \ INCLUDEPATH += $$PWE_MAIN_INCLUDE \
$$EXTERNALS_DIR/assimp/include \ $$EXTERNALS_DIR/assimp/include \
$$EXTERNALS_DIR/CodeGen/include \
$$EXTERNALS_DIR/glew-2.1.0/include \ $$EXTERNALS_DIR/glew-2.1.0/include \
$$EXTERNALS_DIR/lzo-2.10/include \ $$EXTERNALS_DIR/lzo-2.10/include \
$$EXTERNALS_DIR/nod/include \ $$EXTERNALS_DIR/nod/include \
@ -161,7 +162,6 @@ HEADERS += \
OpenGL/CVertexBuffer.h \ OpenGL/CVertexBuffer.h \
OpenGL/GLCommon.h \ OpenGL/GLCommon.h \
ScriptExtra/CRadiusSphereExtra.h \ ScriptExtra/CRadiusSphereExtra.h \
Resource/EGame.h \
Resource/Cooker/CAreaCooker.h \ Resource/Cooker/CAreaCooker.h \
Resource/Script/IPropertyValue.h \ Resource/Script/IPropertyValue.h \
Resource/Script/IPropertyTemplate.h \ Resource/Script/IPropertyTemplate.h \
@ -368,6 +368,11 @@ SOURCES += \
Resource/Script/CPropertyNameGenerator.cpp \ Resource/Script/CPropertyNameGenerator.cpp \
Resource/Script/IPropertyNew.cpp Resource/Script/IPropertyNew.cpp
# Codegen
CODEGEN_OUT_PATH = $$BUILD_DIR/Core/codegen_build/auto_codegen.cpp
CODEGEN_SRC_PATH = $$PWD
include($$EXTERNALS_DIR/CodeGen/codegen.pri)
# Library Sources # Library Sources
SOURCES += $$EXTERNALS_DIR/lzo-2.10/src/lzo_init.c \ SOURCES += $$EXTERNALS_DIR/lzo-2.10/src/lzo_init.c \
$$EXTERNALS_DIR/lzo-2.10/src/lzo1x_9x.c \ $$EXTERNALS_DIR/lzo-2.10/src/lzo1x_9x.c \

View File

@ -63,10 +63,6 @@ enum class EPropertyTypeNew
Array = FOURCC('ARRY'), Array = FOURCC('ARRY'),
Invalid = FOURCC('INVD') Invalid = FOURCC('INVD')
}; };
inline void Serialize(IArchive& rArc, EPropertyTypeNew& rType)
{
rArc.SerializePrimitive( (CFourCC&) rType );
}
inline const char* PropEnumToHashableTypeName(EPropertyTypeNew Type) inline const char* PropEnumToHashableTypeName(EPropertyTypeNew Type)
{ {
@ -105,10 +101,6 @@ enum class ECookPreferenceNew
Always, Always,
Never Never
}; };
inline void Serialize(IArchive& rArc, ECookPreferenceNew& rPref)
{
rArc.SerializePrimitive( (u32&) rPref );
}
/** New property class */ /** New property class */
class IPropertyNew class IPropertyNew

View File

@ -80,6 +80,7 @@ LIBS += -L$$EXTERNALS_DIR/glew-2.1.0/lib/Release/x64 -lglew32s \
# Include Paths # Include Paths
INCLUDEPATH += $$PWE_MAIN_INCLUDE \ INCLUDEPATH += $$PWE_MAIN_INCLUDE \
$$EXTERNALS_DIR/assimp/include \ $$EXTERNALS_DIR/assimp/include \
$$EXTERNALS_DIR/CodeGen/include \
$$EXTERNALS_DIR/glew-2.1.0/include \ $$EXTERNALS_DIR/glew-2.1.0/include \
$$EXTERNALS_DIR/lzo-2.10/include \ $$EXTERNALS_DIR/lzo-2.10/include \
$$EXTERNALS_DIR/nod/include \ $$EXTERNALS_DIR/nod/include \
@ -303,3 +304,8 @@ FORMS += \
CProgressDialog.ui \ CProgressDialog.ui \
Widgets/CSelectResourcePanel.ui \ Widgets/CSelectResourcePanel.ui \
CGeneratePropertyNamesDialog.ui CGeneratePropertyNamesDialog.ui
# Codegen
CODEGEN_OUT_PATH = $$BUILD_DIR/Editor/codegen_build/auto_codegen.cpp
CODEGEN_SRC_PATH = $$PWD
include($$EXTERNALS_DIR/CodeGen/codegen.pri)

View File

@ -47,6 +47,7 @@ CONFIG (release, debug|release) {
# Include Paths # Include Paths
INCLUDEPATH += $$PWE_MAIN_INCLUDE \ INCLUDEPATH += $$PWE_MAIN_INCLUDE \
$$EXTERNALS_DIR/CodeGen/include \
$$EXTERNALS_DIR/tinyxml2 $$EXTERNALS_DIR/tinyxml2
# Header Files # Header Files
@ -77,3 +78,8 @@ SOURCES += \
CVector3f.cpp \ CVector3f.cpp \
CVector4f.cpp \ CVector4f.cpp \
MathUtil.cpp MathUtil.cpp
# Codegen
CODEGEN_OUT_PATH = $$BUILD_DIR/Common/codegen_build/auto_codegen.cpp
CODEGEN_SRC_PATH = $$PWD
include($$EXTERNALS_DIR/CodeGen/codegen.pri)