]> git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/mapq3/parse.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / plugins / mapq3 / parse.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 "parse.h"
23
24 #include <list>
25
26 #include "ientity.h"
27 #include "ibrush.h"
28 #include "ipatch.h"
29 #include "ieclass.h"
30 #include "iscriplib.h"
31 #include "scenelib.h"
32 #include "string/string.h"
33 #include "stringio.h"
34 #include "eclasslib.h"
35
36 inline MapImporter *Node_getMapImporter(scene::Node &node)
37 {
38     return NodeTypeCast<MapImporter>::cast(node);
39 }
40
41
42 typedef std::list<std::pair<CopiedString, CopiedString> > KeyValues;
43
44 NodeSmartReference g_nullNode(NewNullNode());
45
46
47 NodeSmartReference Entity_create(EntityCreator &entityTable, EntityClass *entityClass, const KeyValues &keyValues)
48 {
49     scene::Node &entity(entityTable.createEntity(entityClass));
50     for (KeyValues::const_iterator i = keyValues.begin(); i != keyValues.end(); ++i) {
51         Node_getEntity(entity)->setKeyValue((*i).first.c_str(), (*i).second.c_str());
52     }
53     return NodeSmartReference(entity);
54 }
55
56 NodeSmartReference
57 Entity_parseTokens(Tokeniser &tokeniser, EntityCreator &entityTable, const PrimitiveParser &parser, int index)
58 {
59     NodeSmartReference entity(g_nullNode);
60     KeyValues keyValues;
61     const char *classname = "";
62
63     int count_primitives = 0;
64     while (1) {
65         tokeniser.nextLine();
66         const char *token = tokeniser.getToken();
67         if (token == 0) {
68             Tokeniser_unexpectedError(tokeniser, token, "#entity-token");
69             return g_nullNode;
70         }
71         if (!strcmp(token, "}")) { // end entity
72             if (entity == g_nullNode) {
73                 // entity does not have brushes
74                 entity = Entity_create(entityTable, GlobalEntityClassManager().findOrInsert(classname, false),
75                                        keyValues);
76             }
77             return entity;
78         } else if (!strcmp(token, "{")) { // begin primitive
79             if (entity == g_nullNode) {
80                 // entity has brushes
81                 entity = Entity_create(entityTable, GlobalEntityClassManager().findOrInsert(classname, true),
82                                        keyValues);
83             }
84
85             tokeniser.nextLine();
86
87             NodeSmartReference primitive(parser.parsePrimitive(tokeniser));
88             if (primitive == g_nullNode || !Node_getMapImporter(primitive)->importTokens(tokeniser)) {
89                 globalErrorStream() << "brush " << count_primitives << ": parse error\n";
90                 return g_nullNode;
91             }
92
93             scene::Traversable *traversable = Node_getTraversable(entity);
94             if (Node_getEntity(entity)->isContainer() && traversable != 0) {
95                 traversable->insert(primitive);
96             } else {
97                 globalErrorStream() << "entity " << index << ": type " << classname << ": discarding brush "
98                                     << count_primitives << "\n";
99             }
100             ++count_primitives;
101         } else // epair
102         {
103             CopiedString key(token);
104             token = tokeniser.getToken();
105             if (token == 0) {
106                 Tokeniser_unexpectedError(tokeniser, token, "#epair-value");
107                 return g_nullNode;
108             }
109             keyValues.push_back(KeyValues::value_type(key, token));
110             if (string_equal(key.c_str(), "classname")) {
111                 classname = keyValues.back().second.c_str();
112             }
113         }
114     }
115     // unreachable code
116     return g_nullNode;
117 }
118
119 void Map_Read(scene::Node &root, Tokeniser &tokeniser, EntityCreator &entityTable, const PrimitiveParser &parser)
120 {
121     int count_entities = 0;
122     for (;;) {
123         tokeniser.nextLine();
124         if (!tokeniser.getToken()) { // { or 0
125             break;
126         }
127
128         NodeSmartReference entity(Entity_parseTokens(tokeniser, entityTable, parser, count_entities));
129
130         if (entity == g_nullNode) {
131             globalErrorStream() << "entity " << count_entities << ": parse error\n";
132             return;
133         }
134
135         Node_getTraversable(root)->insert(entity);
136
137         ++count_entities;
138     }
139 }