]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/eclass.cpp
it's better to close file and return on non-void function
[xonotic/netradiant.git] / radiant / eclass.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
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 "eclass.h"
23
24 #include "debugging/debugging.h"
25
26 #include <map>
27
28 #include "ifilesystem.h"
29
30 #include "string/string.h"
31 #include "eclasslib.h"
32 #include "os/path.h"
33 #include "os/dir.h"
34 #include "stream/stringstream.h"
35 #include "moduleobservers.h"
36
37 #include "cmdlib.h"
38
39 #include "preferences.h"
40 #include "mainframe.h"
41
42
43 namespace {
44     typedef std::map<const char *, EntityClass *, RawStringLessNoCase> EntityClasses;
45     EntityClasses g_entityClasses;
46     EntityClass *eclass_bad = 0;
47     char eclass_directory[1024];
48     typedef std::map<CopiedString, ListAttributeType> ListAttributeTypes;
49     ListAttributeTypes g_listTypes;
50 }
51
52 EClassModules &EntityClassManager_getEClassModules();
53
54 /*!
55    implementation of the EClass manager API
56  */
57
58 void CleanEntityList(EntityClasses &entityClasses)
59 {
60     for (EntityClasses::iterator i = entityClasses.begin(); i != entityClasses.end(); ++i) {
61         (*i).second->free((*i).second);
62     }
63     entityClasses.clear();
64 }
65
66 void Eclass_Clear()
67 {
68     CleanEntityList(g_entityClasses);
69     g_listTypes.clear();
70 }
71
72 EntityClass *EClass_InsertSortedList(EntityClasses &entityClasses, EntityClass *entityClass)
73 {
74     std::pair<EntityClasses::iterator, bool> result = entityClasses.insert(
75             EntityClasses::value_type(entityClass->name(), entityClass));
76     if (!result.second) {
77         entityClass->free(entityClass);
78     }
79     return (*result.first).second;
80 }
81
82 EntityClass *Eclass_InsertAlphabetized(EntityClass *e)
83 {
84     return EClass_InsertSortedList(g_entityClasses, e);
85 }
86
87 void Eclass_forEach(EntityClassVisitor &visitor)
88 {
89     for (EntityClasses::iterator i = g_entityClasses.begin(); i != g_entityClasses.end(); ++i) {
90         visitor.visit((*i).second);
91     }
92 }
93
94
95 class RadiantEclassCollector : public EntityClassCollector {
96 public:
97     void insert(EntityClass *eclass)
98     {
99         Eclass_InsertAlphabetized(eclass);
100     }
101
102     void insert(const char *name, const ListAttributeType &list)
103     {
104         g_listTypes.insert(ListAttributeTypes::value_type(name, list));
105     }
106 };
107
108 RadiantEclassCollector g_collector;
109
110 const ListAttributeType *EntityClass_findListType(const char *name)
111 {
112     ListAttributeTypes::iterator i = g_listTypes.find(name);
113     if (i != g_listTypes.end()) {
114         return &(*i).second;
115     }
116     return 0;
117 }
118
119
120 class EntityClassFilterMode {
121 public:
122     bool filter_mp_sp;
123     const char *mp_ignore_prefix;
124     const char *sp_ignore_prefix;
125
126     EntityClassFilterMode() :
127             filter_mp_sp(!string_empty(g_pGameDescription->getKeyValue("eclass_filter_gamemode"))),
128             mp_ignore_prefix(g_pGameDescription->getKeyValue("eclass_sp_prefix")),
129             sp_ignore_prefix(g_pGameDescription->getKeyValue("eclass_mp_prefix"))
130     {
131         if (string_empty(mp_ignore_prefix)) {
132             mp_ignore_prefix = "sp_";
133         }
134         if (string_empty(sp_ignore_prefix)) {
135             sp_ignore_prefix = "mp_";
136         }
137     }
138 };
139
140 class EntityClassesLoadFile {
141     const EntityClassScanner &scanner;
142     const char *m_directory;
143 public:
144     EntityClassesLoadFile(const EntityClassScanner &scanner, const char *directory) : scanner(scanner),
145                                                                                       m_directory(directory)
146     {
147     }
148
149     void operator()(const char *name) const
150     {
151         EntityClassFilterMode filterMode;
152
153         if (filterMode.filter_mp_sp) {
154             if (string_empty(GlobalRadiant().getGameMode()) || string_equal(GlobalRadiant().getGameMode(), "sp")) {
155                 if (string_equal_n(name, filterMode.sp_ignore_prefix, strlen(filterMode.sp_ignore_prefix))) {
156                     globalOutputStream() << "Ignoring '" << name << "'\n";
157                     return;
158                 }
159             } else {
160                 if (string_equal_n(name, filterMode.mp_ignore_prefix, strlen(filterMode.mp_ignore_prefix))) {
161                     globalOutputStream() << "Ignoring '" << name << "'\n";
162                     return;
163                 }
164             }
165         }
166
167         // for a given name, we grab the first .def in the vfs
168         // this allows to override baseq3/scripts/entities.def for instance
169         StringOutputStream relPath(256);
170         relPath << m_directory << name;
171
172         scanner.scanFile(g_collector, relPath.c_str());
173     }
174 };
175
176 struct PathLess {
177     bool operator()(const CopiedString &path, const CopiedString &other) const
178     {
179         return path_less(path.c_str(), other.c_str());
180     }
181 };
182
183 typedef std::map<CopiedString, const char *, PathLess> Paths;
184
185 void EntityClassQuake3_constructDirectory(const char *directory, const char *extension, Paths &paths)
186 {
187     globalOutputStream() << "EntityClass: searching " << makeQuoted(directory) << " for *." << extension << '\n';
188     Directory_forEach(directory, matchFileExtension(extension, [&](const char *name) {
189         paths.insert(Paths::value_type(name, directory));
190     }));
191 }
192
193
194 void EntityClassQuake3_Construct()
195 {
196     StringOutputStream baseDirectory(256);
197     StringOutputStream gameDirectory(256);
198     const char *basegame = GlobalRadiant().getRequiredGameDescriptionKeyValue("basegame");
199     const char *gamename = GlobalRadiant().getGameName();
200     baseDirectory << GlobalRadiant().getGameToolsPath() << basegame << '/';
201     gameDirectory << GlobalRadiant().getGameToolsPath() << gamename << '/';
202
203     class LoadEntityDefinitionsVisitor : public EClassModules::Visitor {
204         const char *baseDirectory;
205         const char *gameDirectory;
206     public:
207         LoadEntityDefinitionsVisitor(const char *baseDirectory, const char *gameDirectory)
208                 : baseDirectory(baseDirectory), gameDirectory(gameDirectory)
209         {
210         }
211
212         void visit(const char *name, const EntityClassScanner &table) const
213         {
214             Paths paths;
215             EntityClassQuake3_constructDirectory(baseDirectory, table.getExtension(), paths);
216             if (!string_equal(baseDirectory, gameDirectory)) {
217                 EntityClassQuake3_constructDirectory(gameDirectory, table.getExtension(), paths);
218             }
219
220             for (Paths::iterator i = paths.begin(); i != paths.end(); ++i) {
221                 EntityClassesLoadFile(table, (*i).second)((*i).first.c_str());
222             }
223         }
224     };
225
226     EntityClassManager_getEClassModules().foreachModule(
227             LoadEntityDefinitionsVisitor(baseDirectory.c_str(), gameDirectory.c_str()));
228 }
229
230 EntityClass *Eclass_ForName(const char *name, bool has_brushes)
231 {
232     ASSERT_NOTNULL(name);
233
234     if (string_empty(name)) {
235         return eclass_bad;
236     }
237
238     EntityClasses::iterator i = g_entityClasses.find(name);
239     if (i != g_entityClasses.end() && string_equal((*i).first, name)) {
240         return (*i).second;
241     }
242
243     EntityClass *e = EntityClass_Create_Default(name, has_brushes);
244     return Eclass_InsertAlphabetized(e);
245 }
246
247 class EntityClassQuake3 : public ModuleObserver {
248     std::size_t m_unrealised;
249     ModuleObservers m_observers;
250 public:
251     EntityClassQuake3() : m_unrealised(4)
252     {
253     }
254
255     void realise()
256     {
257         if (--m_unrealised == 0) {
258             //globalOutputStream() << "Entity Classes: realise\n";
259             EntityClassQuake3_Construct();
260             m_observers.realise();
261         }
262     }
263
264     void unrealise()
265     {
266         if (++m_unrealised == 1) {
267             m_observers.unrealise();
268             //globalOutputStream() << "Entity Classes: unrealise\n";
269             Eclass_Clear();
270         }
271     }
272
273     void attach(ModuleObserver &observer)
274     {
275         m_observers.attach(observer);
276     }
277
278     void detach(ModuleObserver &observer)
279     {
280         m_observers.detach(observer);
281     }
282 };
283
284 EntityClassQuake3 g_EntityClassQuake3;
285
286 void EntityClass_attach(ModuleObserver &observer)
287 {
288     g_EntityClassQuake3.attach(observer);
289 }
290
291 void EntityClass_detach(ModuleObserver &observer)
292 {
293     g_EntityClassQuake3.detach(observer);
294 }
295
296 void EntityClass_realise()
297 {
298     g_EntityClassQuake3.realise();
299 }
300
301 void EntityClass_unrealise()
302 {
303     g_EntityClassQuake3.unrealise();
304 }
305
306 void EntityClassQuake3_construct()
307 {
308     // start by creating the default unknown eclass
309     eclass_bad = EClass_Create("UNKNOWN_CLASS", Vector3(0.0f, 0.5f, 0.0f), "");
310
311     EntityClass_realise();
312 }
313
314 void EntityClassQuake3_destroy()
315 {
316     EntityClass_unrealise();
317
318     eclass_bad->free(eclass_bad);
319 }
320
321 #include "modulesystem/modulesmap.h"
322
323 class EntityClassQuake3Dependencies :
324         public GlobalRadiantModuleRef,
325         public GlobalFileSystemModuleRef,
326         public GlobalShaderCacheModuleRef {
327     EClassModulesRef m_eclass_modules;
328 public:
329     EntityClassQuake3Dependencies() :
330             m_eclass_modules(GlobalRadiant().getRequiredGameDescriptionKeyValue("entityclasstype"))
331     {
332     }
333
334     EClassModules &getEClassModules()
335     {
336         return m_eclass_modules.get();
337     }
338 };
339
340 class EclassManagerAPI {
341     EntityClassManager m_eclassmanager;
342 public:
343     typedef EntityClassManager Type;
344
345     STRING_CONSTANT(Name, "quake3");
346
347     EclassManagerAPI()
348     {
349         EntityClassQuake3_construct();
350
351         m_eclassmanager.findOrInsert = &Eclass_ForName;
352         m_eclassmanager.findListType = &EntityClass_findListType;
353         m_eclassmanager.forEach = &Eclass_forEach;
354         m_eclassmanager.attach = &EntityClass_attach;
355         m_eclassmanager.detach = &EntityClass_detach;
356         m_eclassmanager.realise = &EntityClass_realise;
357         m_eclassmanager.unrealise = &EntityClass_unrealise;
358
359         Radiant_attachGameToolsPathObserver(g_EntityClassQuake3);
360         Radiant_attachGameModeObserver(g_EntityClassQuake3);
361         Radiant_attachGameNameObserver(g_EntityClassQuake3);
362     }
363
364     ~EclassManagerAPI()
365     {
366         Radiant_detachGameNameObserver(g_EntityClassQuake3);
367         Radiant_detachGameModeObserver(g_EntityClassQuake3);
368         Radiant_detachGameToolsPathObserver(g_EntityClassQuake3);
369
370         EntityClassQuake3_destroy();
371     }
372
373     EntityClassManager *getTable()
374     {
375         return &m_eclassmanager;
376     }
377 };
378
379 #include "modulesystem/singletonmodule.h"
380 #include "modulesystem/moduleregistry.h"
381
382 typedef SingletonModule<EclassManagerAPI, EntityClassQuake3Dependencies> EclassManagerModule;
383 typedef Static<EclassManagerModule> StaticEclassManagerModule;
384 StaticRegisterModule staticRegisterEclassManager(StaticEclassManagerModule::instance());
385
386 EClassModules &EntityClassManager_getEClassModules()
387 {
388     return StaticEclassManagerModule::instance().getDependencies().getEClassModules();
389 }