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