Browse Source

- Taml serialization WIP.

MelvMay-GG 12 years ago
parent
commit
bd2bd46

+ 4 - 4
engine/source/2d/assets/ImageAsset.cc

@@ -944,15 +944,15 @@ void ImageAsset::onTamlCustomRead( const TamlCustomNodes& customNodes )
         S32 cellHeight = 0;
 
         // Fetch field nodes.
-        const TamlCustomFieldVector& fieldNodes = pCellNode->getFields();
+        const TamlCustomFieldVector& nodeFields = pCellNode->getFields();
 
         // Iterate property fields.
-        for ( TamlCustomFieldVector::const_iterator nodeFieldItr = fieldNodes.begin(); nodeFieldItr != fieldNodes.end(); ++nodeFieldItr )
+        for ( TamlCustomFieldVector::const_iterator nodeFieldItr = nodeFields.begin(); nodeFieldItr != nodeFields.end(); ++nodeFieldItr )
         {
             // Fetch node field.
-            TamlCustomNodeField* pNodeField = *nodeFieldItr;
+            const TamlCustomNodeField* pNodeField = *nodeFieldItr;
 
-            // Fetch property field name.
+            // Fetch field name.
             StringTableEntry fieldName = pNodeField->getFieldName();
 
             // Check common fields.

+ 4 - 4
engine/source/2d/sceneobject/SceneObject.cc

@@ -3580,7 +3580,7 @@ void SceneObject::onTamlCustomRead( const TamlCustomNodes& customNodes )
             for ( TamlCustomFieldVector::const_iterator shapeFieldItr = shapeFields.begin(); shapeFieldItr != shapeFields.end(); ++shapeFieldItr )
             {
                 // Fetch field node.
-                TamlCustomNodeField* pFieldNode = *shapeFieldItr;
+                const TamlCustomNodeField* pFieldNode = *shapeFieldItr;
 
                 // Fetch property field name.
                 StringTableEntry fieldName = pFieldNode->getFieldName();
@@ -3640,7 +3640,7 @@ void SceneObject::onTamlCustomRead( const TamlCustomNodes& customNodes )
             for ( TamlCustomFieldVector::const_iterator shapeFieldItr = shapeFields.begin(); shapeFieldItr != shapeFields.end(); ++shapeFieldItr )
             {
                 // Fetch field node.
-                TamlCustomNodeField* pFieldNode = *shapeFieldItr;
+                const TamlCustomNodeField* pFieldNode = *shapeFieldItr;
 
                 // Fetch property field name.
                 StringTableEntry fieldName = pFieldNode->getFieldName();
@@ -3708,7 +3708,7 @@ void SceneObject::onTamlCustomRead( const TamlCustomNodes& customNodes )
             for ( TamlCustomFieldVector::const_iterator shapeFieldItr = shapeFields.begin(); shapeFieldItr != shapeFields.end(); ++shapeFieldItr )
             {
                 // Fetch field node.
-                TamlCustomNodeField* pFieldNode = *shapeFieldItr;
+                const TamlCustomNodeField* pFieldNode = *shapeFieldItr;
 
                 // Fetch property field name.
                 StringTableEntry fieldName = pFieldNode->getFieldName();
@@ -3779,7 +3779,7 @@ void SceneObject::onTamlCustomRead( const TamlCustomNodes& customNodes )
             for ( TamlCustomFieldVector::const_iterator shapeFieldItr = shapeFields.begin(); shapeFieldItr != shapeFields.end(); ++shapeFieldItr )
             {
                 // Fetch field node.
-                TamlCustomNodeField* pFieldNode = *shapeFieldItr;
+                const TamlCustomNodeField* pFieldNode = *shapeFieldItr;
 
                 // Fetch property field name.
                 StringTableEntry fieldName = pFieldNode->getFieldName();

+ 2 - 2
engine/source/persistence/taml/tamlBinaryReader.cc

@@ -284,8 +284,8 @@ void TamlBinaryReader::parseCustomElements( Stream& stream, TamlCallbacks* pCall
                     StringTableEntry propertyFieldName = stream.readSTString();
 
                     // Read field value.
-                    char valueBuffer[MAX_TAML_PROPERTY_FIELDVALUE_LENGTH];
-                    stream.readLongString( MAX_TAML_PROPERTY_FIELDVALUE_LENGTH, valueBuffer );
+                    char valueBuffer[MAX_TAML_NODE_FIELDVALUE_LENGTH];
+                    stream.readLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, valueBuffer );
 
                     // Add property field.
                     pPropertyAlias->addField( propertyFieldName, valueBuffer );

+ 1 - 1
engine/source/persistence/taml/tamlBinaryWriter.cc

@@ -200,7 +200,7 @@ void TamlBinaryWriter::writeCustomElements( Stream& stream, const TamlWriteNode*
                 {
                     // No, so write property attribute.
                     stream.writeString( pPropertyField->getFieldName() );
-                    stream.writeLongString( MAX_TAML_PROPERTY_FIELDVALUE_LENGTH, pPropertyField->getFieldValue() );
+                    stream.writeLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, pPropertyField->getFieldValue() );
                 }
             }
         }

+ 2 - 0
engine/source/persistence/taml/tamlCustom.h

@@ -464,6 +464,8 @@ public:
     inline const Vector<TamlCustomNode*>& getChildren( void ) const { return mChildren; }
     inline const TamlCustomFieldVector& getFields( void ) const { return mFields; }
 
+    inline bool isEmpty( void ) const { return mChildren.size() == 0 && mFields.size() == 0; }
+
     StringTableEntry        mNodeName;
     Vector<TamlCustomNode*> mChildren;
     TamlCustomFieldVector   mFields;

+ 86 - 65
engine/source/persistence/taml/tamlXmlWriter.cc

@@ -80,24 +80,10 @@ TiXmlNode* TamlXmlWriter::compileElement( const TamlWriteNode* pTamlWriteNode )
         // Set reference to Id attribute.
         pElement->SetAttribute( TAML_REFID_ATTRIBUTE_NAME, referenceToId );
 
-        // Do we have a reference field?
-        if ( pTamlWriteNode->mRefField != StringTable->EmptyString )
-        {
-            // Yes, so set attribute.
-            pElement->SetAttribute( TAML_REF_FIELD_NAME, pTamlWriteNode->mRefField );
-        }
-
         // Finish because we're a reference to another object.
         return pElement;
     }
 
-    // Do we have a reference field?
-    if ( pTamlWriteNode->mRefField != StringTable->EmptyString )
-    {
-        // Yes, so set attribute.
-        pElement->SetAttribute( TAML_REF_FIELD_NAME, pTamlWriteNode->mRefField );
-    }
-
     // Fetch object name.
     const char* pObjectName = pTamlWriteNode->mpObjectName;
 
@@ -133,6 +119,31 @@ TiXmlNode* TamlXmlWriter::compileElement( const TamlWriteNode* pTamlWriteNode )
 
 //-----------------------------------------------------------------------------
 
+void TamlXmlWriter::compileAttributes( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode )
+{
+    // Debug Profiling.
+    PROFILE_SCOPE(TamlXmlWriter_CompileAttributes);
+
+    // Fetch fields.
+    const Vector<TamlWriteNode::FieldValuePair*>& fields = pTamlWriteNode->mFields;
+
+    // Ignore if no fields.
+    if ( fields.size() == 0 )
+        return;
+
+    // Iterate fields.
+    for( Vector<TamlWriteNode::FieldValuePair*>::const_iterator itr = fields.begin(); itr != fields.end(); ++itr )
+    {
+        // Fetch field/value pair.
+        TamlWriteNode::FieldValuePair* pFieldValue = (*itr);
+
+        // Set field attribute.
+        pXmlElement->SetAttribute( pFieldValue->mName, pFieldValue->mpValue );
+    }
+}
+
+//-----------------------------------------------------------------------------
+
 void TamlXmlWriter::compileCustomElements( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode )
 {
     // Debug Profiling.
@@ -163,45 +174,20 @@ void TamlXmlWriter::compileCustomElements( TiXmlElement* pXmlElement, const Taml
         TiXmlElement* pExtendedPropertyElement = new TiXmlElement( extendedElementName );
 
         // Fetch node children.
+        const TamlCustomNodeVector& nodeChildren = pCustomNode->getChildren();
 
-        // Iterate node children.
-        for( TamlCustomProperty::const_iterator propertyAliasItr = pCustomProperty->begin(); propertyAliasItr != pCustomProperty->end(); ++propertyAliasItr )
+        // Iterate children nodes.
+        for( TamlCustomNodeVector::const_iterator childNodeItr = nodeChildren.begin(); childNodeItr != nodeChildren.end(); ++childNodeItr )
         {
-            // Fetch property alias.
-            TamlPropertyAlias* pPropertyAlias = *propertyAliasItr;
-
-            // Skip if the alias is set to ignore no properties and there are none.
-            if ( pPropertyAlias->mIgnoreEmpty && pPropertyAlias->size() == 0 )
-                continue;
-
-            // Create element.
-            TiXmlElement* pPropertyElement = new TiXmlElement( pPropertyAlias->mAliasName );
-
-            // Iterate property fields.
-            for ( TamlPropertyAlias::const_iterator propertyFieldItr = pPropertyAlias->begin(); propertyFieldItr != pPropertyAlias->end(); ++propertyFieldItr )
-            {
-                // Fetch property field.
-                TamlCustomNodeField* pPropertyField = *propertyFieldItr;
-
-                // Is it an object field?
-                if ( pPropertyField->isObjectField() )
-                {
-                    // Yes, so write child element.
-                    pPropertyElement->LinkEndChild( compileElement( pPropertyField->getWriteNode() ) );
-                }
-                else
-                {
-                    // No, so set property attribute.
-                    pPropertyElement->SetAttribute( pPropertyField->getFieldName(), pPropertyField->getFieldValue() );
-                }
-            }
-
-            // Write property element as child.
-            pExtendedPropertyElement->LinkEndChild( pPropertyElement );
+            // Fetch child node.
+            const TamlCustomNode* pChildNode = *childNodeItr;
+
+            // Compile the custom nodes.
+            compileCustomNodes( pExtendedPropertyElement, pChildNode );
         }
 
-        // Is the custom property set to ignore no alias' and there are none.
-        if ( pCustomProperty->mIgnoreEmpty && pExtendedPropertyElement->NoChildren() )
+        // Finish if the node is set to ignore if empty and it is empty.
+        if ( pCustomNode->mIgnoreEmpty && pExtendedPropertyElement->NoChildren() )
         {
             // Yes, so delete the extended element.
             delete pExtendedPropertyElement;
@@ -209,7 +195,7 @@ void TamlXmlWriter::compileCustomElements( TiXmlElement* pXmlElement, const Taml
         }
         else
         {
-            // No, so write the extended property element as child.
+            // No, so add elementt as child.
             pXmlElement->LinkEndChild( pExtendedPropertyElement );
         }
     }
@@ -217,25 +203,60 @@ void TamlXmlWriter::compileCustomElements( TiXmlElement* pXmlElement, const Taml
 
 //-----------------------------------------------------------------------------
 
-void TamlXmlWriter::compileAttributes( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode )
+void TamlXmlWriter::compileCustomNodes( TiXmlElement* pXmlElement, const TamlCustomNode* pCustomNode )
 {
-    // Debug Profiling.
-    PROFILE_SCOPE(TamlXmlWriter_CompileAttributes);
+    // Finish if the node is set to ignore if empty and it is empty.
+    if ( pCustomNode->mIgnoreEmpty && pCustomNode->isEmpty() )
+        return;
 
-    // Fetch fields.
-    const Vector<TamlWriteNode::FieldValuePair*>& fields = pTamlWriteNode->mFields;
+    // Create element.
+    TiXmlElement* pNodeElement = new TiXmlElement( pCustomNode->mNodeName );
 
-    // Ignore if no fields.
-    if ( fields.size() == 0 )
-        return;
+    // Fetch node children.
+    const TamlCustomNodeVector& nodeChildren = pCustomNode->getChildren();
 
-    // Iterate fields.
-    for( Vector<TamlWriteNode::FieldValuePair*>::const_iterator itr = fields.begin(); itr != fields.end(); ++itr )
+    // Iterate children nodes.
+    for( TamlCustomNodeVector::const_iterator childNodeItr = nodeChildren.begin(); childNodeItr != nodeChildren.end(); ++childNodeItr )
     {
-        // Fetch field/value pair.
-        TamlWriteNode::FieldValuePair* pFieldValue = (*itr);
+        // Fetch child node.
+        const TamlCustomNode* pChildNode = *childNodeItr;
 
-        // Set field attribute.
-        pXmlElement->SetAttribute( pFieldValue->mName, pFieldValue->mpValue );
+        // Is the node a proxy object?
+        if ( pChildNode->isProxyObject() )
+        {
+            // Yes, so write the proxy object.
+            pNodeElement->LinkEndChild( compileElement( pChildNode->getProxyWriteNode() ) );
+        }
+        else
+        {
+            // No, so compile the child nodes.
+            compileCustomNodes( pNodeElement, pChildNode );
+        }
+    }
+
+    // Fetch node fields.
+    const TamlCustomFieldVector& nodeFields = pCustomNode->getFields();
+
+    // Iterate property fields.
+    for ( TamlCustomFieldVector::const_iterator nodeFieldItr = nodeFields.begin(); nodeFieldItr != nodeFields.end(); ++nodeFieldItr )
+    {
+        // Fetch node field.
+        const TamlCustomNodeField* pNodeField = *nodeFieldItr;
+
+        // Set field.
+        pNodeElement->SetAttribute( pNodeField->getFieldName(), pNodeField->getFieldValue() );
+    }
+
+    // Finish if the node is set to ignore if empty and it is empty (including fields).
+    if ( pCustomNode->mIgnoreEmpty && nodeFields.size() == 0 && pNodeElement->NoChildren() )
+    {
+        // Yes, so delete the extended element.
+        delete pNodeElement;
+        pNodeElement = NULL;
+    }
+    else
+    {
+        // Add node element as child.
+        pXmlElement->LinkEndChild( pNodeElement );
     }
-}
+}

+ 2 - 1
engine/source/persistence/taml/tamlXmlWriter.h

@@ -51,8 +51,9 @@ private:
 
 private:
     TiXmlNode* compileElement( const TamlWriteNode* pTamlWriteNode );
-    void compileCustomElements( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode );
     void compileAttributes( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode );
+    void compileCustomElements( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode );
+    void compileCustomNodes( TiXmlElement* pXmlElement, const TamlCustomNode* pCustomNode );
 };
 
 #endif // _TAML_XMLWRITER_H_