add an opt-out setting to not write entity and brush number comment on map write
[xonotic/netradiant.git] / plugins / mapq3 / write.cpp
index 7a95458..9cf9c48 100644 (file)
@@ -25,8 +25,9 @@
 #include "iscriplib.h"
 #include "scenelib.h"
 
-inline MapExporter* Node_getMapExporter( scene::Node& node ){
-       return NodeTypeCast<MapExporter>::cast( node );
+inline MapExporter *Node_getMapExporter(scene::Node &node)
+{
+    return NodeTypeCast<MapExporter>::cast(node);
 }
 
 
@@ -34,79 +35,89 @@ static std::size_t g_count_entities;
 static std::size_t g_count_brushes;
 
 
-void Entity_ExportTokens( const Entity& entity, TokenWriter& writer ){
-       g_count_brushes = 0;
-
-       class WriteKeyValue : public Entity::Visitor
-       {
-       TokenWriter& m_writer;
-public:
-       WriteKeyValue( TokenWriter& writer )
-               : m_writer( writer ){
-       }
-
-       void visit( const char* key, const char* value ){
-               m_writer.writeString( key );
-               m_writer.writeString( value );
-               m_writer.nextLine();
-       }
-
-       } visitor( writer );
-
-       entity.forEachKeyValue( visitor );
+void Entity_ExportTokens(const Entity &entity, TokenWriter &writer)
+{
+    g_count_brushes = 0;
+
+    class WriteKeyValue : public Entity::Visitor {
+        TokenWriter &m_writer;
+    public:
+        WriteKeyValue(TokenWriter &writer)
+                : m_writer(writer)
+        {
+        }
+
+        void visit(const char *key, const char *value)
+        {
+            m_writer.writeString(key);
+            m_writer.writeString(value);
+            m_writer.nextLine();
+        }
+
+    } visitor(writer);
+
+    entity.forEachKeyValue(visitor);
 }
 
-class WriteTokensWalker : public scene::Traversable::Walker
-{
-mutable Stack<bool> m_stack;
-TokenWriter& m_writer;
-bool m_ignorePatches;
+class WriteTokensWalker : public scene::Traversable::Walker {
+    mutable Stack<bool> m_stack;
+    TokenWriter &m_writer;
+    bool m_ignorePatches;
+    bool m_writeComments;
 public:
-WriteTokensWalker( TokenWriter& writer, bool ignorePatches )
-       : m_writer( writer ), m_ignorePatches( ignorePatches ){
-}
-bool pre( scene::Node& node ) const {
-       m_stack.push( false );
-
-       Entity* entity = Node_getEntity( node );
-       if ( entity != 0 ) {
-               m_writer.writeToken( "//" );
-               m_writer.writeToken( "entity" );
-               m_writer.writeUnsigned( g_count_entities++ );
-               m_writer.nextLine();
-
-               m_writer.writeToken( "{" );
-               m_writer.nextLine();
-               m_stack.top() = true;
-
-               Entity_ExportTokens( *entity, m_writer );
-       }
-       else
-       {
-               MapExporter* exporter = Node_getMapExporter( node );
-               if ( exporter != 0
-                        && !( m_ignorePatches && Node_isPatch( node ) ) ) {
-                       m_writer.writeToken( "//" );
-                       m_writer.writeToken( "brush" );
-                       m_writer.writeUnsigned( g_count_brushes++ );
-                       m_writer.nextLine();
-
-                       exporter->exportTokens( m_writer );
-               }
-       }
-
-       return true;
-}
-void post( scene::Node& node ) const {
-       if ( m_stack.top() ) {
-               m_writer.writeToken( "}" );
-               m_writer.nextLine();
-       }
-       m_stack.pop();
-}
+    WriteTokensWalker(TokenWriter &writer, bool ignorePatches, bool writeComments)
+            : m_writer(writer), m_ignorePatches(ignorePatches), m_writeComments(writeComments)
+    {
+    }
+
+    bool pre(scene::Node &node) const
+    {
+        m_stack.push(false);
+
+        Entity *entity = Node_getEntity(node);
+        if (entity != 0) {
+            if (m_writeComments) {
+                m_writer.writeToken("//");
+                m_writer.writeToken("entity");
+                m_writer.writeUnsigned(g_count_entities++);
+                m_writer.nextLine();
+            }
+
+            m_writer.writeToken("{");
+            m_writer.nextLine();
+            m_stack.top() = true;
+
+            Entity_ExportTokens(*entity, m_writer);
+        } else {
+            MapExporter *exporter = Node_getMapExporter(node);
+            if (exporter != 0
+                && !(m_ignorePatches && Node_isPatch(node))) {
+                if (m_writeComments) {
+                    m_writer.writeToken("//");
+                    m_writer.writeToken("brush");
+                    m_writer.writeUnsigned(g_count_brushes++);
+                    m_writer.nextLine();
+                }
+
+                exporter->exportTokens(m_writer);
+            }
+        }
+
+        return true;
+    }
+
+    void post(scene::Node &node) const
+    {
+        if (m_stack.top()) {
+            m_writer.writeToken("}");
+            m_writer.nextLine();
+        }
+        m_stack.pop();
+    }
 };
 
-void Map_Write( scene::Node& root, GraphTraversalFunc traverse, TokenWriter& writer, bool ignorePatches ){
-       g_count_entities = 0;
-       traverse( root, WriteTokensWalker( writer, ignorePatches ) );
+void Map_Write(scene::Node &root, GraphTraversalFunc traverse, TokenWriter &writer, bool ignorePatches, bool writeComments)
+{
+    g_count_entities = 0;
+    traverse(root, WriteTokensWalker(writer, ignorePatches, writeComments));
 }