Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TAML Serialization Improvements #380

Draft
wants to merge 1 commit into
base: Preview4_0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions Engine/source/T3D/convexShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2159,3 +2159,119 @@ void ConvexShape::Geometry::generate(const Vector< PlaneF > &planes, const Vecto
faces.push_back( newFace );
}
}

static StringTableEntry surfacesCustomNodeName = StringTable->insert("Surfaces");
static StringTableEntry surfaceNodeName = StringTable->insert("Surface");
static StringTableEntry surfaceQuatName = StringTable->insert("Rotation");
static StringTableEntry surfacePositionName = StringTable->insert("Position");

void ConvexShape::onTamlCustomWrite(TamlCustomNodes& customNodes)
{
// Debug Profiling.
PROFILE_SCOPE(ConvexShape_OnTamlCustomWrite);

// Call parent.
Parent::onTamlCustomWrite(customNodes);

if (mSurfaces.size() > 0)
{
// Add cell custom node.
TamlCustomNode* pCustomCellNodes = customNodes.addNode(surfacesCustomNodeName);

// Iterate explicit frames.
for (Vector<MatrixF>::iterator surfaceItr = mSurfaces.begin(); surfaceItr != mSurfaces.end(); ++surfaceItr)
{
QuatF quat(*surfaceItr);
Point3F pos = surfaceItr->getPosition();
// Add cell alias.
TamlCustomNode* pNode = pCustomCellNodes->addNode(surfaceNodeName);

AngAxisF axis(quat);

// Add cell properties.
pNode->addField(surfaceQuatName, axis);
pNode->addField(surfacePositionName, pos);
}
}
}

void ConvexShape::onTamlCustomRead(const TamlCustomNodes& customNodes)
{
// Debug Profiling.
PROFILE_SCOPE(ConvexShape_OnTamlCustomRead);

// Call parent.
Parent::onTamlCustomRead(customNodes);

// Find cell custom node.
const TamlCustomNode* pCustomCellNodes = customNodes.findNode(surfacesCustomNodeName);

// Continue if we have explicit cells.
if (pCustomCellNodes != NULL)
{
mSurfaces.clear();

// Fetch children cell nodes.
const TamlCustomNodeVector& cellNodes = pCustomCellNodes->getChildren();

// Iterate cells.
for (TamlCustomNodeVector::const_iterator cellNodeItr = cellNodes.begin(); cellNodeItr != cellNodes.end(); ++cellNodeItr)
{
// Fetch cell node.
TamlCustomNode* pCellNode = *cellNodeItr;

// Fetch node name.
StringTableEntry nodeName = pCellNode->getNodeName();

// Is this a valid alias?
if (nodeName != surfaceNodeName)
{
// No, so warn.
Con::warnf("ConvexShape::onTamlCustomRead() - Encountered an unknown custom name of '%s'. Only '%s' is valid.", nodeName, surfaceNodeName);
continue;
}

QuatF quat;
Point3F pos;

// Fetch fields.
const TamlCustomFieldVector& fields = pCellNode->getFields();

// Iterate property fields.
for (TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr)
{
// Fetch field.
const TamlCustomField* pField = *fieldItr;

// Fetch field name.
StringTableEntry fieldName = pField->getFieldName();

// Check common fields.
if (fieldName == surfaceQuatName)
{
AngAxisF axis;
pField->getFieldValue(axis);
quat = QuatF(axis);
}
else if (fieldName == surfacePositionName)
{
pField->getFieldValue(pos);
}
else
{
// Unknown name so warn.
Con::warnf("MeshRoad::onTamlCustomRead() - Encountered an unknown custom field name of '%s'.", fieldName);
continue;
}
}

MatrixF surface;
quat.setMatrix(&surface);
surface.setPosition(pos);

mSurfaces.push_back(surface);
}
_updateGeometry(true);
setMaskBits(UpdateMask);
}
}
6 changes: 6 additions & 0 deletions Engine/source/T3D/convexShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ class ConvexShape : public SceneObject
virtual void writeFields(Stream &stream, U32 tabStop);
virtual bool writeField( StringTableEntry fieldname, const char *value );

/// Called during the writing of the object to allow custom properties to be written.
virtual void onTamlCustomWrite(TamlCustomNodes& customNodes);

/// Called during the reading of the object to allow custom properties to be read.
virtual void onTamlCustomRead(const TamlCustomNodes& customNodes);

// NetObject
virtual U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
virtual void unpackUpdate( NetConnection *conn, BitStream *stream );
Expand Down
10 changes: 5 additions & 5 deletions Engine/source/assets/assetBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,23 @@ class AssetBase : public SimObject
protected:
static bool setAssetName(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetName(data); return false; }
static const char* getAssetName(void* obj, const char* data) { return static_cast<AssetBase*>(obj)->getAssetName(); }
static bool writeAssetName(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetName() != StringTable->EmptyString(); }
static bool writeAssetName(void* obj, const char* idx, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetName() != StringTable->EmptyString(); }

static bool setAssetDescription(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetDescription(data); return false; }
static const char* getAssetDescription(void* obj, const char* data) { return static_cast<AssetBase*>(obj)->getAssetDescription(); }
static bool writeAssetDescription(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetDescription() != StringTable->EmptyString(); }
static bool writeAssetDescription(void* obj, const char* idx, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetDescription() != StringTable->EmptyString(); }

static bool setAssetCategory(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetCategory(data); return false; }
static const char* getAssetCategory(void* obj, const char* data) { return static_cast<AssetBase*>(obj)->getAssetCategory(); }
static bool writeAssetCategory(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetCategory() != StringTable->EmptyString(); }
static bool writeAssetCategory(void* obj, const char* idx, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetCategory() != StringTable->EmptyString(); }

static bool setAssetAutoUnload(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetAutoUnload(dAtob(data)); return false; }
static const char* getAssetAutoUnload(void* obj, const char* data) { return Con::getBoolArg(static_cast<AssetBase*>(obj)->getAssetAutoUnload()); }
static bool writeAssetAutoUnload(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetAutoUnload() == false; }
static bool writeAssetAutoUnload(void* obj, const char* idx, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetAutoUnload() == false; }

static bool setAssetInternal(void *obj, const char *array, const char *data) { static_cast<AssetBase*>(obj)->setAssetInternal(dAtob(data)); return false; }
static const char* getAssetInternal(void* obj, const char* data) { return Con::getBoolArg(static_cast<AssetBase*>(obj)->getAssetInternal()); }
static bool writeAssetInternal(void* obj, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetInternal() == true; }
static bool writeAssetInternal(void* obj, const char* idx, StringTableEntry pFieldName) { return static_cast<AssetBase*>(obj)->getAssetInternal() == true; }

static const char* getAssetPrivate(void* obj, const char* data) { return Con::getBoolArg(static_cast<AssetBase*>(obj)->getAssetPrivate()); }

Expand Down
2 changes: 1 addition & 1 deletion Engine/source/assets/assetQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AssetQuery : public SimObject
virtual void onTamlCustomRead( const TamlCustomNodes& customNodes );

static const char* getCount(void* obj, const char* data) { return Con::getIntArg(static_cast<AssetQuery*>(obj)->mAssetList.size()); }
static bool writeCount( void* obj, StringTableEntry pFieldName ) { return false; }
static bool writeCount( void* obj, const char* idx, StringTableEntry pFieldName ) { return false; }

public:
AssetQuery() {}
Expand Down
10 changes: 5 additions & 5 deletions Engine/source/console/consoleObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class AbstractClassRep : public ConsoleBaseType
typedef const char *(*GetDataNotify)( void *obj, const char *data );

/// This is a function pointer typedef to support optional writing for fields.
typedef bool(*WriteDataNotify)(void* obj, StringTableEntry pFieldName);
typedef bool(*WriteDataNotify)(void* obj, const char* idx, StringTableEntry fieldName);

/// These are special field type values used to mark
/// groups and arrays in the field list.
Expand Down Expand Up @@ -531,7 +531,7 @@ class AbstractClassRep : public ConsoleBaseType
TypeValidator *validator; ///< Validator, if any.
SetDataNotify setDataFn; ///< Set data notify Fn
GetDataNotify getDataFn; ///< Get data notify Fn
WriteDataNotify writeDataFn; ///< Function to determine whether data should be written or not.
WriteDataNotify writeDataFn; ///< Function to determine whether data should be written or not.
bool doNotSubstitute;
bool keepClearSubsOnly;

Expand Down Expand Up @@ -757,7 +757,7 @@ template< typename T > EnginePropertyTable& ConcreteAbstractClassRep< T >::smPro
//------------------------------------------------------------------------------
// Forward declaration of this function so it can be used in the class
const char *defaultProtectedGetFn( void *obj, const char *data );
bool defaultProtectedWriteFn(void* obj, StringTableEntry pFieldName);
bool defaultProtectedWriteFn(void* obj, const char* idx, StringTableEntry fieldName);

//=============================================================================
// ConsoleObject.
Expand Down Expand Up @@ -1334,7 +1334,7 @@ inline const char *emptyStringProtectedGetFn( void *obj, const char *data )
return "";
}

inline bool defaultProtectedWriteFn(void* obj, StringTableEntry pFieldName)
inline bool defaultProtectedWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
return true;
}
Expand All @@ -1344,7 +1344,7 @@ inline bool defaultProtectedNotSetFn(void* obj, const char *array, const char* d
return false;
}

inline bool defaultProtectedNotWriteFn(void* obj, StringTableEntry pFieldName)
inline bool defaultProtectedNotWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
return false;
}
Expand Down
21 changes: 14 additions & 7 deletions Engine/source/console/simObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,52 +153,59 @@ void SimObject::initPersistFields()
addGroup( "Ungrouped" );

addProtectedField( "name", TypeName, Offset(mObjectName, SimObject), &setProtectedName, &defaultProtectedGetFn,
&PublicStringMemberWriteFn<SimObject, "", &SimObject::mObjectName>,
"Optional global name of this object." );

endGroup( "Ungrouped" );

addGroup( "Object" );

addField( "internalName", TypeString, Offset(mInternalName, SimObject),
addField( "internalName", TypeString, Offset(mInternalName, SimObject),
&PublicStringMemberWriteFn<SimObject, "", &SimObject::mInternalName>,
"Optional name that may be used to lookup this object within a SimSet.");

addProtectedField( "parentGroup", TYPEID< SimObject >(), Offset(mGroup, SimObject), &setProtectedParent, &defaultProtectedGetFn,
addProtectedField( "parentGroup", TYPEID< SimObject >(), Offset(mGroup, SimObject), &setProtectedParent, &defaultProtectedGetFn,
&PublicMemberWriteFn<SimObject, SimGroup*, NULL, &SimObject::mGroup>,
"Group hierarchy parent of the object." );

addProtectedField( "class", TypeString, Offset(mClassName, SimObject), &setClass, &defaultProtectedGetFn,
&PublicStringMemberWriteFn<SimObject, "", &SimObject::mClassName>,
"Script class of object." );

addProtectedField( "superClass", TypeString, Offset(mSuperClassName, SimObject), &setSuperClass, &defaultProtectedGetFn,
&PublicStringMemberWriteFn<SimObject, "", &SimObject::mSuperClassName>,
"Script super-class of object." );

// For legacy support
addProtectedField( "className", TypeString, Offset(mClassName, SimObject), &setClass, &defaultProtectedGetFn,
&PublicStringMemberWriteFn<SimObject, "", &SimObject::mClassName>,
"Script class of object.", AbstractClassRep::FIELD_HideInInspectors );

endGroup( "Object" );

addGroup( "Editing" );

addProtectedField( "hidden", TypeBool, NULL,
&_setHidden, &_getHidden,
&_setHidden, &_getHidden, &DefaultBoolWriteFn<false>,
"Whether the object is visible." );
addProtectedField( "locked", TypeBool, NULL,
&_setLocked, &_getLocked,
&_setLocked, &_getLocked, &DefaultBoolWriteFn<false>,
"Whether the object can be edited." );

endGroup( "Editing" );

addGroup( "Persistence" );

addProtectedField( "canSave", TypeBool, Offset( mFlags, SimObject ),
&_setCanSave, &_getCanSave,
&_setCanSave, &_getCanSave, &PublicConstMethodWriteFn<SimObject, bool, true, &SimObject::getCanSave>,
"Whether the object can be saved out. If false, the object is purely transient in nature." );

addField( "canSaveDynamicFields", TypeBool, Offset(mCanSaveFieldDictionary, SimObject),
addField( "canSaveDynamicFields", TypeBool, Offset(mCanSaveFieldDictionary, SimObject),
&PublicMemberWriteFn<SimObject, bool, true, &SimObject::mCanSaveFieldDictionary>,
"True if dynamic fields (added at runtime) should be saved. Defaults to true." );

addProtectedField( "persistentId", TypePID, Offset( mPersistentId, SimObject ),
&_setPersistentID, &defaultProtectedGetFn,
&_setPersistentID, &defaultProtectedGetFn, &PublicMemberWriteFn<SimObject, SimPersistID*, NULL, &SimObject::mPersistentId>,
"The universally unique identifier for the object." );

endGroup( "Persistence" );
Expand Down
70 changes: 70 additions & 0 deletions Engine/source/console/simObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -1074,4 +1074,74 @@ class SimObjectPtr : public WeakRefPtr< T >
}
};

inline bool DefaultNonEmptyStringWriteFn(void *obj, const char* idx, StringTableEntry fieldName)
{
const char* value = static_cast<SimObject*>(obj)->getDataField(fieldName, idx);
return value != NULL && dStricmp(value, "") != 0;
}

template<const char* defaultValue>
bool DefaultValueWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
const char* value = static_cast<SimObject*>(obj)->getDataField(fieldName, idx);
return dStricmp(value, defaultValue) != 0;
}

template<const char* defaultValue>
bool DefaultFloatWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
const char* value = static_cast<SimObject*>(obj)->getDataField(fieldName, idx);
return dAtof(value) != dAtof(defaultValue);
}

template<S32 defaultValue>
bool DefaultIntWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
const char* value = static_cast<SimObject*>(obj)->getDataField(fieldName, idx);
return dAtoi(value) != defaultValue;
}

template<U32 defaultValue>
bool DefaultUintWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
const char* value = static_cast<SimObject*>(obj)->getDataField(fieldName, idx);
return dAtoui(value) != defaultValue;
}

template<bool defaultValue>
bool DefaultBoolWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
const char* value = static_cast<SimObject*>(obj)->getDataField(fieldName, idx);
return dAtob(value) != defaultValue;
}

template<typename C, typename T, T defaultValue, T C::*field>
bool PublicMemberWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
C* instance = static_cast<C*>(obj);
return instance->*field != defaultValue;
}

template<typename C, const char* defaultValue, StringTableEntry C::* field>
bool PublicStringMemberWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
C* instance = static_cast<C*>(obj);
return instance->*field != NULL && dStricmp(instance->*field, StringTable->insert(defaultValue)) != 0;
}

template<typename C, typename T, T defaultValue, T(C::* method)(void)>
bool PublicMethodWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
C* instance = static_cast<C*>(obj);
return (instance->*method)() != defaultValue;
}

template<typename C, typename T, T defaultValue, T(C::* method)(void) const>
bool PublicConstMethodWriteFn(void* obj, const char* idx, StringTableEntry fieldName)
{
C* instance = static_cast<C*>(obj);
return (instance->*method)() != defaultValue;
}


#endif // _SIMOBJECT_H_
Loading