Browse Source

- Taml serialization WIP.

MelvMay-GG 12 years ago
parent
commit
5aea3bc

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

@@ -379,7 +379,7 @@ Taml::TamlFormatMode Taml::getFileAutoFormatMode( const char* pFilename )
 
 //-----------------------------------------------------------------------------
 
-TamlWriteNode* Taml::compileObject( SimObject* pSimObject )
+TamlWriteNode* Taml::compileObject( SimObject* pSimObject, const bool forceId )
 {
     // Debug Profiling.
     PROFILE_SCOPE(Taml_CompileObject);
@@ -426,6 +426,13 @@ TamlWriteNode* Taml::compileObject( SimObject* pSimObject )
     TamlWriteNode* pNewNode = new TamlWriteNode();
     pNewNode->set( pSimObject );
 
+    // Is an Id being forced for this object?
+    if ( forceId )
+    {
+        // Yes, so allocate one.
+        pNewNode->mRefId = ++mMasterNodeId;
+    }
+
     // Push new node.
     mCompiledNodes.push_back( pNewNode );
 
@@ -718,7 +725,10 @@ void Taml::compileCustomNodeState( TamlCustomNode* pCustomNode )
         AssertFatal( children.size() == 0, "Taml: Cannot compile a proxy object on a custom node that has children." );
 
         // Yes, so compile it.
-        pCustomNode->setWriteNode( compileObject( pProxyObject ) );
+        // NOTE: We force an Id for custom compiled objects so we guarantee an Id.  The reason for this is fairly
+        // weak in that the XML parser currently has no way of distinguishing between a compiled object node
+        // and a custom node.  If the node has an Id or an Id-Ref then it's obviously an object and should be parsed as such.
+        pCustomNode->setWriteNode( compileObject( pProxyObject, true ) );
         return;
     }
 

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

@@ -103,7 +103,7 @@ private:
 private:
     void resetCompilation( void );
 
-    TamlWriteNode* compileObject( SimObject* pSimObject );
+    TamlWriteNode* compileObject( SimObject* pSimObject, const bool forceId = false );
     void compileStaticFields( TamlWriteNode* pTamlWriteNode );
     void compileDynamicFields( TamlWriteNode* pTamlWriteNode );
     void compileChildren( TamlWriteNode* pTamlWriteNode );

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

@@ -348,10 +348,10 @@ void TamlBinaryReader::parseCustomNode( Stream& stream, TamlCustomNode* pCustomN
     if ( isProxyObject )
     {
         // Yes, so parse proxy object.
-        SimObject* pSimObject = parseElement( stream, versionId );
+        SimObject* pProxyObject = parseElement( stream, versionId );
 
         // Add child node.
-        pCustomNode->addNode( pSimObject );
+        pCustomNode->addNode( pProxyObject );
 
         return;
     }

+ 86 - 75
engine/source/persistence/taml/tamlXmlReader.cc

@@ -247,6 +247,34 @@ SimObject* TamlXmlReader::parseElement( TiXmlElement* pXmlElement )
 
 //-----------------------------------------------------------------------------
 
+void TamlXmlReader::parseAttributes( TiXmlElement* pXmlElement, SimObject* pSimObject )
+{
+    // Debug Profiling.
+    PROFILE_SCOPE(TamlXmlReader_ParseAttributes);
+
+    // Sanity!
+    AssertFatal( pSimObject != NULL, "Taml: Cannot parse attributes on a NULL object." );
+
+    // Iterate attributes.
+    for ( TiXmlAttribute* pAttribute = pXmlElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->Next() )
+    {
+        // Insert attribute name.
+        StringTableEntry attributeName = StringTable->insert( pAttribute->Name() );
+
+        // Ignore if this is a Taml attribute.
+        if (    attributeName == mTamlRefId ||
+                attributeName == mTamlRefToId ||
+                attributeName == mTamlObjectName ||
+                attributeName == mTamlRefField )
+                continue;
+
+        // We can assume this is a field for now.
+        pSimObject->setPrefixedDataField( attributeName, NULL, pAttribute->Value() );
+    }
+}
+
+//-----------------------------------------------------------------------------
+
 void TamlXmlReader::parseCustomElement( TiXmlElement* pXmlElement, TamlCustomNodes& customNodes )
 {
     // Debug Profiling.
@@ -258,91 +286,77 @@ void TamlXmlReader::parseCustomElement( TiXmlElement* pXmlElement, TamlCustomNod
     // Sanity!
     AssertFatal( pPeriod != NULL, "Parsing extended element but no period character found." );
 
-    // Fetch any property alias.
-    TiXmlNode* pPropertyAliasXmlNode = pXmlElement->FirstChild();
+    // Fetch any custom XML node.
+    TiXmlNode* pCustomXmlNode = pXmlElement->FirstChild();
+
+    // Finish is no XML node exists.
+    if ( pCustomXmlNode == NULL )
+        return;
 
-    // Do we have any property alias?
-    if ( pPropertyAliasXmlNode != NULL )
+    // Yes, so add custom node.
+    TamlCustomNode* pCustomNode = customNodes.addNode( pPeriod+1 );
+
+    do
     {
-        // Yes, so add custom property.
-        TamlCustomProperty* pCustomProperty = customProperties.addProperty( pPeriod+1 );
+        // Fetch element.
+        TiXmlElement* pCustomXmlElement = dynamic_cast<TiXmlElement*>( pCustomXmlNode );
 
-        do
-        {
-            // Fetch element.
-            TiXmlElement* pPropertyAliasXmlElement = dynamic_cast<TiXmlElement*>( pPropertyAliasXmlNode );
+        // Move to next sibling.
+        pCustomXmlNode = pCustomXmlNode->NextSibling();
 
-            // Move to next sibling.
-            pPropertyAliasXmlNode = pPropertyAliasXmlNode->NextSibling();
+        // Skip if this is not an element.
+        if ( pCustomXmlElement == NULL )
+            continue;
 
-            // Skip if this is not an element?
-            if ( pPropertyAliasXmlElement == NULL )
-                continue;
+        // Parse custom node.
+        parseCustomNode( pCustomXmlElement, pCustomNode );
 
-            // Add property alias.
-            TamlPropertyAlias* pPropertyAlias = pCustomProperty->addAlias( pPropertyAliasXmlElement->Value() );
+    }
+    while ( pCustomXmlNode != NULL );
+}
 
-            // Iterate property field attributes.
-            for ( TiXmlAttribute* pAttribute = pPropertyAliasXmlElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->Next() )
-            {
-                // Insert attribute name.
-                StringTableEntry attributeName = StringTable->insert( pAttribute->Name() );
+//-----------------------------------------------------------------------------
 
-                // Add property field.
-                pPropertyAlias->addField( attributeName, pAttribute->Value() );
-            }
+void TamlXmlReader::parseCustomNode( TiXmlElement* pXmlElement, TamlCustomNode* pCustomNode )
+{
+    // Is the node a proxy object?
+    if (  getTamlRefId( pXmlElement ) != 0 || getTamlRefToId( pXmlElement ) != 0 )
+    {
+        // Yes, so parse proxy object.
+        SimObject* pProxyObject = parseElement( pXmlElement );
 
-            // Fetch any children.
-            TiXmlNode* pChildXmlNode = pPropertyAliasXmlElement->FirstChild();
+        // Add child node.
+        pCustomNode->addNode( pProxyObject );
 
-            // Do we have any element children?
-            if ( pChildXmlNode != NULL )
-            {
-                do
-                {
-                    // Fetch element.
-                    TiXmlElement* pChildXmlElement = dynamic_cast<TiXmlElement*>( pChildXmlNode );
+        return;
+    }
 
-                    // Move to next sibling.
-                    pChildXmlNode = pChildXmlNode->NextSibling();
+    // Add child node.
+    TamlCustomNode* pChildNode = pCustomNode->addNode( pXmlElement->Value() );
 
-                    // Skip if this is not an element?
-                    if ( pChildXmlElement == NULL )
-                        continue;
+    // Fetch any children.
+    TiXmlNode* pChildXmlNode = pXmlElement->FirstChild();
 
-                    // Fetch the reference field.
-                    const char* pRefField = getTamlRefField( pChildXmlElement );
+    // Do we have any element children?
+    if ( pChildXmlNode != NULL )
+    {
+        do
+        {
+            // Yes, so fetch child element.
+            TiXmlElement* pChildXmlElement = dynamic_cast<TiXmlElement*>( pChildXmlNode );
 
-                    // Was a reference field found?
-                    if ( pRefField == NULL )
-                    {
-                        // No, so warn.
-                        Con::warnf( "Taml: Encountered a child element in a custom element but it did not have a field reference using '%s'.", mTamlRefField );
-                        continue;
-                    }
+            // Move to next sibling.
+            pChildXmlNode = pChildXmlNode->NextSibling();
 
-                    // Parse the child element.
-                    SimObject* pFieldObject = parseElement( pChildXmlElement );
+            // Skip if this is not an element.
+            if ( pChildXmlElement == NULL )
+                continue;
 
-                    // Add property field.
-                    pPropertyAlias->addField( pRefField, pFieldObject );
-                }
-                while( pChildXmlNode != NULL );
-            }
+            // Parse custom node.
+            parseCustomNode( pChildXmlElement, pChildNode );
         }
-        while ( pPropertyAliasXmlNode != NULL );
+        while( pChildXmlNode != NULL );
     }
-}
-
-//-----------------------------------------------------------------------------
-
-void TamlXmlReader::parseAttributes( TiXmlElement* pXmlElement, SimObject* pSimObject )
-{
-    // Debug Profiling.
-    PROFILE_SCOPE(TamlXmlReader_ParseAttributes);
-
-    // Sanity!
-    AssertFatal( pSimObject != NULL, "Taml: Cannot parse attributes on a NULL object." );
 
     // Iterate attributes.
     for ( TiXmlAttribute* pAttribute = pXmlElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->Next() )
@@ -350,15 +364,12 @@ void TamlXmlReader::parseAttributes( TiXmlElement* pXmlElement, SimObject* pSimO
         // Insert attribute name.
         StringTableEntry attributeName = StringTable->insert( pAttribute->Name() );
 
-        // Ignore if this is a Taml attribute.
-        if (    attributeName == mTamlRefId ||
-                attributeName == mTamlRefToId ||
-                attributeName == mTamlObjectName ||
-                attributeName == mTamlRefField )
-                continue;
+        // Skip if a Taml reference attribute.
+        if ( attributeName == mTamlRefId || attributeName == mTamlRefToId )
+            continue;
 
-        // We can assume this is a field for now.
-        pSimObject->setPrefixedDataField( attributeName, NULL, pAttribute->Value() );
+        // Add node field.
+        pChildNode->addField( attributeName, pAttribute->Value() );
     }
 }
 

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

@@ -69,8 +69,9 @@ private:
     void resetParse( void );
 
     SimObject* parseElement( TiXmlElement* pXmlElement );
-    void parseCustomElement( TiXmlElement* pXmlElement, TamlCustomNodes& customNodes );
     void parseAttributes( TiXmlElement* pXmlElement, SimObject* pSimObject );
+    void parseCustomElement( TiXmlElement* pXmlElement, TamlCustomNodes& customNodes );
+    void parseCustomNode( TiXmlElement* pXmlElement, TamlCustomNode* pCustomNode );
 
     U32 getTamlRefId( TiXmlElement* pXmlElement );
     U32 getTamlRefToId( TiXmlElement* pXmlElement );