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