2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
30 #include "iscriplib.h"
32 #include "string/string.h"
34 #include "eclasslib.h"
36 inline MapImporter* Node_getMapImporter(scene::Node& node)
38 return NodeTypeCast<MapImporter>::cast(node);
42 typedef std::list< std::pair<CopiedString, CopiedString> > KeyValues;
44 NodeSmartReference g_nullNode(NewNullNode());
47 NodeSmartReference Entity_create(EntityCreator& entityTable, EntityClass* entityClass, const KeyValues& keyValues)
49 scene::Node& entity(entityTable.createEntity(entityClass));
50 for(KeyValues::const_iterator i = keyValues.begin(); i != keyValues.end(); ++i)
52 Node_getEntity(entity)->setKeyValue((*i).first.c_str(), (*i).second.c_str());
54 return NodeSmartReference(entity);
57 NodeSmartReference Entity_parseTokens(Tokeniser& tokeniser, EntityCreator& entityTable, const PrimitiveParser& parser, int index)
59 NodeSmartReference entity(g_nullNode);
61 const char* classname = "";
63 int count_primitives = 0;
67 const char* token = tokeniser.getToken();
70 Tokeniser_unexpectedError(tokeniser, token, "#entity-token");
73 if (!strcmp(token, "}")) // end entity
75 if(entity == g_nullNode)
77 // entity does not have brushes
78 entity = Entity_create(entityTable, GlobalEntityClassManager().findOrInsert(classname, false), keyValues);
82 else if(!strcmp(token, "{")) // begin primitive
84 if(entity == g_nullNode)
87 entity = Entity_create(entityTable, GlobalEntityClassManager().findOrInsert(classname, true), keyValues);
92 NodeSmartReference primitive(parser.parsePrimitive(tokeniser));
93 if(primitive == g_nullNode || !Node_getMapImporter(primitive)->importTokens(tokeniser))
95 globalErrorStream() << "brush " << count_primitives << ": parse error\n";
99 scene::Traversable* traversable = Node_getTraversable(entity);
100 if(Node_getEntity(entity)->isContainer() && traversable != 0)
102 traversable->insert(primitive);
106 globalErrorStream() << "entity " << index << ": type " << classname << ": discarding brush " << count_primitives << "\n";
112 CopiedString key(token);
113 token = tokeniser.getToken();
116 Tokeniser_unexpectedError(tokeniser, token, "#epair-value");
119 keyValues.push_back(KeyValues::value_type(key, token));
120 if(string_equal(key.c_str(), "classname"))
122 classname = keyValues.back().second.c_str();
130 void Map_Read(scene::Node& root, Tokeniser& tokeniser, EntityCreator& entityTable, const PrimitiveParser& parser)
132 int count_entities = 0;
135 tokeniser.nextLine();
136 if (!tokeniser.getToken()) // { or 0
139 NodeSmartReference entity(Entity_parseTokens(tokeniser, entityTable, parser, count_entities));
141 if(entity == g_nullNode)
143 globalErrorStream() << "entity " << count_entities << ": parse error\n";
147 Node_getTraversable(root)->insert(entity);