]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/mapq3/write.cpp
Merge branch 'NateEag-master-patch-12920' into 'master'
[xonotic/netradiant.git] / plugins / mapq3 / write.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "write.h"
23
24 #include "ientity.h"
25 #include "iscriplib.h"
26 #include "scenelib.h"
27
28 inline MapExporter* Node_getMapExporter( scene::Node& node ){
29         return NodeTypeCast<MapExporter>::cast( node );
30 }
31
32
33 static std::size_t g_count_entities;
34 static std::size_t g_count_brushes;
35
36
37 void Entity_ExportTokens( const Entity& entity, TokenWriter& writer ){
38         g_count_brushes = 0;
39
40         class WriteKeyValue : public Entity::Visitor
41         {
42         TokenWriter& m_writer;
43 public:
44         WriteKeyValue( TokenWriter& writer )
45                 : m_writer( writer ){
46         }
47
48         void visit( const char* key, const char* value ){
49                 m_writer.writeString( key );
50                 m_writer.writeString( value );
51                 m_writer.nextLine();
52         }
53
54         } visitor( writer );
55
56         entity.forEachKeyValue( visitor );
57 }
58
59 class WriteTokensWalker : public scene::Traversable::Walker
60 {
61 mutable Stack<bool> m_stack;
62 TokenWriter& m_writer;
63 bool m_ignorePatches;
64 bool m_writeComments;
65 public:
66 WriteTokensWalker( TokenWriter& writer, bool ignorePatches, bool writeComments )
67         : m_writer( writer ), m_ignorePatches( ignorePatches ), m_writeComments( writeComments ){
68 }
69
70 bool pre( scene::Node &node ) const {
71         m_stack.push( false );
72
73         Entity* entity = Node_getEntity( node );
74         if ( entity != 0 ) {
75                 if( entity->isContainer() && Node_getTraversable( node )->empty() && !string_equal( entity->getKeyValue( "classname" ), "worldspawn" ) ){
76                         globalErrorStream() << "discarding empty group entity: # = " << g_count_entities << "; classname = " << entity->getKeyValue( "classname" ) << "\n";
77                         return false;
78                 }
79                 if ( m_writeComments ) {
80                         m_writer.writeToken( "//" );
81                         m_writer.writeToken( "entity" );
82                         m_writer.writeUnsigned( g_count_entities++ );
83                         m_writer.nextLine();
84                 }
85
86                 m_writer.writeToken( "{" );
87                 m_writer.nextLine();
88                 m_stack.top() = true;
89
90                 Entity_ExportTokens( *entity, m_writer );
91         }
92         else
93         {
94                 MapExporter* exporter = Node_getMapExporter( node );
95                 if ( exporter != 0
96                         && !( m_ignorePatches && Node_isPatch( node ) ) ) {
97                         if ( m_writeComments ) {
98                                 m_writer.writeToken( "//" );
99                                 m_writer.writeToken( "brush" );
100                                 m_writer.writeUnsigned( g_count_brushes++ );
101                                 m_writer.nextLine();
102                         }
103
104                         exporter->exportTokens( m_writer );
105                 }
106         }
107
108         return true;
109 }
110
111 void post( scene::Node& node ) const {
112         if ( m_stack.top() ) {
113                 m_writer.writeToken( "}" );
114                 m_writer.nextLine();
115         }
116         m_stack.pop();
117 }
118 };
119
120 void Map_Write( scene::Node& root, GraphTraversalFunc traverse, TokenWriter& writer, bool ignorePatches, bool writeComments ){
121         g_count_entities = 0;
122         traverse( root, WriteTokensWalker( writer, ignorePatches, writeComments ) );
123 }