Browse Source

- Taml now writes a relative schema location if "$pref::T2D::TAMLSchema" is set. Simply specifying the filename is relative to the executable. Using "blah/foo.xsd" is a sub-folder relative to the executable.
- Taml now generates a schema using the global function "generateTamlSchema()" which now has no arguments as it uses the variable "$pref::T2D::TAMLSchema" for consistency. You will get a warning if this is not set and no schema will be written.
- Added an empty "$pref::T2D::TAMLSchema" in AppCore default preferences.

MelvMay-GG 12 years ago
parent
commit
50d9c9c

+ 25 - 16
engine/source/persistence/taml/taml.cc

@@ -856,10 +856,32 @@ SimObject* Taml::createType( StringTableEntry typeName, const Taml* pTaml, const
 
 //-----------------------------------------------------------------------------
 
-bool Taml::generateTamlSchema( const char* pFilename )
+bool Taml::generateTamlSchema()
 {
-    // Sanity!
-    AssertFatal( pFilename != NULL, "Taml::generateTamlSchema() - Cannot write a NULL filename." );
+    // Fetch any TAML Schema file reference.
+    const char* pTamlSchemaFile = Con::getVariable( TAML_SCHEMA_VARIABLE );
+
+    // Do we have a schema file reference?
+    if ( pTamlSchemaFile == NULL || *pTamlSchemaFile == 0 )
+    {
+        // No, so warn.
+        Con::warnf( "Taml::generateTamlSchema() - Cannot write a TAML schema as no schema variable is set ('%s').", TAML_SCHEMA_VARIABLE );
+        return false;
+    }
+
+    // Expand the file-name into the file-path buffer.
+    char filePathBuffer[1024];
+    Con::expandPath( filePathBuffer, sizeof(filePathBuffer), pTamlSchemaFile );
+
+    FileStream stream;
+
+    // File opened?
+    if ( !stream.open( filePathBuffer, FileStream::Write ) )
+    {
+        // No, so warn.
+        Con::warnf("Taml::GenerateTamlSchema() - Could not open filename '%s' for write.", filePathBuffer );
+        return false;
+    }
 
     // Create document.
     TiXmlDocument schemaDocument;
@@ -1287,19 +1309,6 @@ bool Taml::generateTamlSchema( const char* pFilename )
         pComplexTypeElement->LinkEndChild( pAnyAttributeElement );
     }
 
-    // Expand the file-name into the file-path buffer.
-    char filePathBuffer[1024];
-    Con::expandPath( filePathBuffer, sizeof(filePathBuffer), pFilename );
-
-    FileStream stream;
-
-    // File opened?
-    if ( !stream.open( filePathBuffer, FileStream::Write ) )
-    {
-        // No, so warn.
-        Con::warnf("Taml::GenerateTamlSchema() - Could not open filename '%s' for write.", filePathBuffer );
-        return false;
-    }
     // Write the schema document.
     schemaDocument.SaveFile( stream );
 

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

@@ -61,6 +61,10 @@
 
 //-----------------------------------------------------------------------------
 
+#define TAML_SCHEMA_VARIABLE            "$pref::T2D::TAMLSchema"
+
+//-----------------------------------------------------------------------------
+
 class TamlXmlWriter;
 class TamlXmlReader;
 class TamlBinaryWriter;
@@ -193,7 +197,7 @@ public:
     static const char* getFormatModeDescription( const TamlFormatMode formatMode );
 
     /// Schema generation.
-    static bool generateTamlSchema( const char* pFilename );
+    static bool generateTamlSchema();
 
     /// Write a unrestricted custom Taml schema.
     static void WriteUnrestrictedCustomTamlSchema( const char* pCustomNodeName, const AbstractClassRep* pClassRep, TiXmlElement* pParentElement );

+ 35 - 3
engine/source/persistence/taml/tamlXmlWriter.cc

@@ -35,8 +35,40 @@ bool TamlXmlWriter::write( FileStream& stream, const TamlWriteNode* pTamlWriteNo
     // Create document.
     TiXmlDocument xmlDocument;
 
-    // Compile root element.
-    xmlDocument.LinkEndChild( compileElement( pTamlWriteNode ) );
+    // Compile the root element.
+    TiXmlElement* pRootElement = compileElement( pTamlWriteNode );
+
+    // Fetch any TAML Schema file reference.
+    const char* pTamlSchemaFile = Con::getVariable( TAML_SCHEMA_VARIABLE );
+
+    // Do we have a schema file reference?
+    if ( pTamlSchemaFile != NULL && *pTamlSchemaFile != 0 )
+    {
+        // Yes, so add namespace attribute to root.
+        pRootElement->SetAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
+
+        // Expand the file-path reference.
+        char schemaFilePathBuffer[1024];
+        Con::expandPath( schemaFilePathBuffer, sizeof(schemaFilePathBuffer), pTamlSchemaFile );
+
+        // Fetch the output path for the Taml file.
+        char outputFileBuffer[1024];
+        dSprintf( outputFileBuffer, sizeof(outputFileBuffer), "%s", mpTaml->getFilePathBuffer() );
+        char* pFileStart = dStrrchr( outputFileBuffer, '/' );
+        if ( pFileStart == NULL )
+            *outputFileBuffer = 0;
+        else
+            *pFileStart = 0;
+
+        // Fetch the schema file-path relative to the output file.
+        StringTableEntry relativeSchemaFilePath = Platform::makeRelativePathName( schemaFilePathBuffer, outputFileBuffer );
+
+        // Add schema location attribute to root.
+        pRootElement->SetAttribute( "xsi:noNamespaceSchemaLocation", relativeSchemaFilePath );
+    }
+
+    // Link the root element.
+    xmlDocument.LinkEndChild( pRootElement );
 
     // Save document to stream.
     return xmlDocument.SaveFile( stream );
@@ -44,7 +76,7 @@ bool TamlXmlWriter::write( FileStream& stream, const TamlWriteNode* pTamlWriteNo
 
 //-----------------------------------------------------------------------------
 
-TiXmlNode* TamlXmlWriter::compileElement( const TamlWriteNode* pTamlWriteNode )
+TiXmlElement* TamlXmlWriter::compileElement( const TamlWriteNode* pTamlWriteNode )
 {
     // Debug Profiling.
     PROFILE_SCOPE(TamlXmlWriter_CompileElement);

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

@@ -50,7 +50,7 @@ private:
     Taml* mpTaml;
 
 private:
-    TiXmlNode* compileElement( const TamlWriteNode* pTamlWriteNode );
+    TiXmlElement* compileElement( const TamlWriteNode* pTamlWriteNode );
     void compileAttributes( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode );
     void compileCustomElements( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode );
     void compileCustomNode( TiXmlElement* pXmlElement, const TamlCustomNode* pCustomNode );

+ 3 - 6
engine/source/persistence/taml/taml_ScriptBinding.h

@@ -294,13 +294,10 @@ ConsoleFunction(TamlRead, const char*, 2, 4,    "(filename, [format]) - Read an
 
 //-----------------------------------------------------------------------------
 
-ConsoleFunction(GenerateTamlSchema, bool, 2, 2, "(filename) - Generate a TAML schema file of all engine types.\n"
-                                                "@param filename The schema file to generate.\n"
+ConsoleFunction(GenerateTamlSchema, bool, 1, 1, "() - Generate a TAML schema file of all engine types.\n"
+                                                "The schema file is specified using the console variable '"TAML_SCHEMA_VARIABLE"'.\n"
                                                 "@return Whether the schema file was writtent or not." )
 {
-    // Fetch the filename.
-    const char* pFilename = argv[1];
-
     // Generate the schema.
-    return Taml::generateTamlSchema( pFilename );
+    return Taml::generateTamlSchema();
 }

+ 1 - 0
modules/AppCore/1/scripts/defaultPreferences.cs

@@ -54,6 +54,7 @@ $pref::T2D::ParticlePlayerTimeScale = 1.0;
 $pref::T2D::warnFileDeprecated = 1;
 $pref::T2D::warnSceneOccupancy = 1;
 $pref::T2D::imageAssetGlobalFilterMode = Bilinear;
+$pref::T2D::TAMLSchema="";
 
 /// Video
 $pref::Video::appliedPref = 0;