Browse Source

- Taml serialization WIP.

MelvMay-GG 12 years ago
parent
commit
6e7aa14b29

+ 104 - 96
engine/source/persistence/taml/tamlBinaryReader.cc

@@ -212,102 +212,6 @@ SimObject* TamlBinaryReader::parseElement( Stream& stream, const U32 versionId )
 
 //-----------------------------------------------------------------------------
 
-void TamlBinaryReader::parseCustomElements( Stream& stream, TamlCallbacks* pCallbacks, TamlCustomNodes& customNodes, const U32 versionId )
-{
-    // Debug Profiling.
-    PROFILE_SCOPE(TamlBinaryReader_ParseCustomElement);
-
-    // Read custom element count.
-    U32 customPropertyCount;
-    stream.read( &customPropertyCount );
-
-    // Finish if no custom properties.
-    if ( customPropertyCount == 0 )
-        return;
-
-    // Iterate custom properties.
-    for ( U32 propertyIndex = 0; propertyIndex < customPropertyCount; ++propertyIndex )
-    {
-        // Read custom element name.
-        StringTableEntry propertyName = stream.readSTString();
-
-        // Add custom property.
-        TamlCustomProperty* pCustomProperty = customProperties.addProperty( propertyName );
-
-        // Read property alias count.
-        U32 propertyAliasCount;
-        stream.read( &propertyAliasCount );
-
-        // Skip if no property alias.
-        if ( propertyAliasCount == 0 )
-            continue;
-
-        // Iterate property alias.
-        for( U32 propertyAliasIndex = 0; propertyAliasIndex < propertyAliasCount; ++propertyAliasIndex )
-        {
-            // Read property alias name.
-            StringTableEntry propertyAliasName = stream.readSTString();
-
-            // Add property alias.
-            TamlPropertyAlias* pPropertyAlias = pCustomProperty->addAlias( propertyAliasName );
-
-            // Read property field count.
-            U32 propertyFieldCount;
-            stream.read( &propertyFieldCount );
-
-            // Skip if no property fields.
-            if ( propertyFieldCount == 0 )
-                continue;
-
-            // Iterate property fields.
-            for( U32 propertyFieldIndex = 0; propertyFieldIndex < propertyFieldCount; ++propertyFieldIndex )
-            {
-                // Read is object field flag.
-                bool isObjectField;
-                stream.read( &isObjectField );
-
-                // Is it an object field?
-                if ( isObjectField )
-                {
-                    // Yes, so read reference field.
-                    StringTableEntry fieldName = stream.readSTString();
-
-                    // Read field object.
-                    SimObject* pFieldObject = parseElement( stream, versionId );
-
-                    // Add property field.
-                    pPropertyAlias->addField( fieldName, pFieldObject );
-                }
-                else
-                {
-                    // No, so read field name.
-                    StringTableEntry propertyFieldName = stream.readSTString();
-
-                    // Read field value.
-                    char valueBuffer[MAX_TAML_NODE_FIELDVALUE_LENGTH];
-                    stream.readLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, valueBuffer );
-
-                    // Add property field.
-                    pPropertyAlias->addField( propertyFieldName, valueBuffer );
-                }
-            }
-        }
-    }
-
-    // Do we have callbacks?
-    if ( pCallbacks == NULL )
-    {
-        // No, so warn.
-        Con::warnf( "Taml: Encountered custom data but object does not support custom data." );
-        return;
-    }
-
-    // Custom read callback.
-    mpTaml->tamlCustomRead( pCallbacks, customProperties );
-}
-
-//-----------------------------------------------------------------------------
-
 void TamlBinaryReader::parseAttributes( Stream& stream, SimObject* pSimObject, const U32 versionId )
 {
     // Debug Profiling.
@@ -391,3 +295,107 @@ void TamlBinaryReader::parseChildren( Stream& stream, TamlCallbacks* pCallbacks,
         }
     }
 }
+
+//-----------------------------------------------------------------------------
+
+void TamlBinaryReader::parseCustomElements( Stream& stream, TamlCallbacks* pCallbacks, TamlCustomNodes& customNodes, const U32 versionId )
+{
+    // Debug Profiling.
+    PROFILE_SCOPE(TamlBinaryReader_ParseCustomElement);
+
+    // Read custom node count.
+    U32 customNodeCount;
+    stream.read( &customNodeCount );
+
+    // Finish if no custom nodes.
+    if ( customNodeCount == 0 )
+        return;
+
+    // Iterate custom nodes.
+    for ( U32 nodeIndex = 0; nodeIndex < customNodeCount; ++nodeIndex )
+    {
+        //Read custom node name.
+        StringTableEntry nodeName = stream.readSTString();
+
+        // Add custom node.
+        TamlCustomNode* pCustomNode = customNodes.addNode( nodeName );
+
+        // Parse the custom node.
+        parseCustomNode( stream, pCustomNode, versionId );
+    }
+
+    // Do we have callbacks?
+    if ( pCallbacks == NULL )
+    {
+        // No, so warn.
+        Con::warnf( "Taml: Encountered custom data but object does not support custom data." );
+        return;
+    }
+
+    // Custom read callback.
+    mpTaml->tamlCustomRead( pCallbacks, customNodes );
+}
+
+//-----------------------------------------------------------------------------
+
+void TamlBinaryReader::parseCustomNode( Stream& stream, TamlCustomNode* pCustomNode, const U32 versionId )
+{
+    // Fetch if a proxy object.
+    bool isProxyObject;
+    stream.read( &isProxyObject );
+
+    // Is this a proxy object?
+    if ( isProxyObject )
+    {
+        // Yes, so parse proxy object.
+        SimObject* pSimObject = parseElement( stream, versionId );
+
+        // Add child node.
+        pCustomNode->addNode( pSimObject );
+
+        return;
+    }
+
+    // No, so read custom node name.
+    StringTableEntry nodeName = stream.readSTString();
+
+    // Add child node.
+    TamlCustomNode* pChildNode = pCustomNode->addNode( nodeName );
+
+    // Read child node count.
+    U32 childNodeCount;
+    stream.read( &childNodeCount );
+
+    // Do we have any children nodes?
+    if ( childNodeCount > 0 )
+    {
+        // Yes, so parse children nodes.
+        for( U32 childIndex = 0; childIndex < childNodeCount; ++childIndex )
+        {
+            // Parse child node.
+            parseCustomNode( stream, pChildNode, versionId );
+        }
+    }
+
+    // Read child field count.
+    U32 childFieldCount;
+    stream.read( &childFieldCount );
+
+    // Do we have any child fields?
+    if ( childFieldCount > 0 )
+    {
+        // Yes, so parse child fields.
+        for( U32 childFieldIndex = 0; childFieldIndex < childFieldCount; ++childFieldIndex )
+        {
+            // Read field name.
+            StringTableEntry fieldName = stream.readSTString();
+
+            // Read field value.
+            char valueBuffer[MAX_TAML_NODE_FIELDVALUE_LENGTH];
+            stream.readLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, valueBuffer );
+
+            // Add node field.
+            pChildNode->addField( fieldName, valueBuffer );
+        }
+    }
+}

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

@@ -58,9 +58,10 @@ private:
     void resetParse( void );
 
     SimObject* parseElement( Stream& stream, const U32 versionId );
-    void parseCustomElements( Stream& stream, TamlCallbacks* pCallbacks, TamlCustomNodes& customNodes, const U32 versionId );
     void parseAttributes( Stream& stream, SimObject* pSimObject, const U32 versionId );
     void parseChildren( Stream& stream, TamlCallbacks* pCallbacks, SimObject* pSimObject, const U32 versionId );
+    void parseCustomElements( Stream& stream, TamlCallbacks* pCallbacks, TamlCustomNodes& customNodes, const U32 versionId );
+    void parseCustomNode( Stream& stream, TamlCustomNode* pCustomNode, const U32 versionId );
 };
 
 #endif // _TAML_BINARYREADER_H_

+ 30 - 17
engine/source/persistence/taml/tamlBinaryWriter.cc

@@ -193,7 +193,7 @@ void TamlBinaryWriter::writeCustomElements( Stream& stream, const TamlWriteNode*
     // Fetch custom nodes.
     const TamlCustomNodeVector& nodes = customNodes.getNodes();
 
-    // Write custom element count.
+    // Write custom node count.
     stream.write( (U32)nodes.size() );
 
     // Finish if there are no nodes.
@@ -206,8 +206,21 @@ void TamlBinaryWriter::writeCustomElements( Stream& stream, const TamlWriteNode*
         // Fetch the custom node.
         TamlCustomNode* pCustomNode = *customNodesItr;
 
-        // Write the custom node.
-        writeCustomNode( stream, pCustomNode );
+        // Write custom node name.
+        stream.writeString( pCustomNode->mNodeName );
+
+        // Fetch node children.
+        const TamlCustomNodeVector& nodeChildren = pCustomNode->getChildren();
+
+        // Iterate children nodes.
+        for( TamlCustomNodeVector::const_iterator childNodeItr = nodeChildren.begin(); childNodeItr != nodeChildren.end(); ++childNodeItr )
+        {
+            // Fetch child node.
+            const TamlCustomNode* pChildNode = *childNodeItr;
+
+            // Write the custom node.
+            writeCustomNode( stream, pChildNode );
+        }
     }
 }
 
@@ -215,13 +228,10 @@ void TamlBinaryWriter::writeCustomElements( Stream& stream, const TamlWriteNode*
 
 void TamlBinaryWriter::writeCustomNode( Stream& stream, const TamlCustomNode* pCustomNode )
 {
-    // Write custom node name.
-    stream.writeString( pCustomNode->mNodeName );
-
     // Is the node a proxy object?
     if ( pCustomNode->isProxyObject() )
     {
-        // Yes, so flag as element.
+        // Yes, so flag as proxy object.
         stream.write( true );
 
         // Write the element.
@@ -232,17 +242,20 @@ void TamlBinaryWriter::writeCustomNode( Stream& stream, const TamlCustomNode* pC
     // No, so flag as custom node.
     stream.write( false );
 
+    // Write custom node name.
+    stream.writeString( pCustomNode->mNodeName );
+
     // Fetch node children.
     const TamlCustomNodeVector& nodeChildren = pCustomNode->getChildren();
 
-    // Fetch node count.
-    const U32 customNodeCount = (U32)nodeChildren.size();
+    // Fetch child node count.
+    const U32 childNodeCount = (U32)nodeChildren.size();
 
     // Write custom node count.
-    stream.write( customNodeCount );
+    stream.write( childNodeCount );
 
-    // Do we have any nodes.
-    if ( customNodeCount > 0 )
+    // Do we have any children nodes.
+    if ( childNodeCount > 0 )
     {
         // Yes, so iterate children nodes.
         for( TamlCustomNodeVector::const_iterator childNodeItr = nodeChildren.begin(); childNodeItr != nodeChildren.end(); ++childNodeItr )
@@ -258,14 +271,14 @@ void TamlBinaryWriter::writeCustomNode( Stream& stream, const TamlCustomNode* pC
     // Fetch node fields.
     const TamlCustomFieldVector& nodeFields = pCustomNode->getFields();
 
-    // Fetch node field count.
-    const U32 nodeFieldCount = (U32)nodeFields.size();
+    // Fetch child field count.
+    const U32 childFieldCount = (U32)nodeFields.size();
 
     // Write custom node field count.
-    stream.write( nodeFieldCount );
+    stream.write( childFieldCount );
 
-    // Do we have any node fields?
-    if ( nodeFieldCount > 0 )
+    // Do we have any child fields?
+    if ( childFieldCount > 0 )
     {
         // Yes, so iterate node fields.
         for ( TamlCustomFieldVector::const_iterator nodeFieldItr = nodeFields.begin(); nodeFieldItr != nodeFields.end(); ++nodeFieldItr )

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

@@ -34,7 +34,7 @@ class TamlBinaryWriter
 public:
     TamlBinaryWriter( Taml* pTaml ) :
         mpTaml( pTaml ),
-        mVersionId(1)
+        mVersionId(2)
     {
     }
     virtual ~TamlBinaryWriter() {}