]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/server.cpp
Merge branch 'master' into divVerent/farplanedist-sky-fix
[xonotic/netradiant.git] / radiant / server.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 "server.h"
23
24 #include "debugging/debugging.h"
25 #include "warnings.h"
26
27 #include <vector>
28 #include <map>
29 #include "os/path.h"
30
31 #include "modulesystem.h"
32
33 class RadiantModuleServer : public ModuleServer
34 {
35 typedef std::pair<CopiedString, int> ModuleType;
36 typedef std::pair<ModuleType, CopiedString> ModuleKey;
37 typedef std::map<ModuleKey, Module*> Modules_;
38 Modules_ m_modules;
39 bool m_error;
40
41 public:
42 RadiantModuleServer() : m_error( false ){
43 }
44
45 void setError( bool error ){
46         m_error = error;
47 }
48 bool getError() const {
49         return m_error;
50 }
51
52 TextOutputStream& getOutputStream(){
53         return globalOutputStream();
54 }
55 TextOutputStream& getErrorStream(){
56         return globalErrorStream();
57 }
58 DebugMessageHandler& getDebugMessageHandler(){
59         return globalDebugMessageHandler();
60 }
61
62 void registerModule( const char* type, int version, const char* name, Module& module ){
63         ASSERT_NOTNULL( &module );
64         if ( !m_modules.insert( Modules_::value_type( ModuleKey( ModuleType( type, version ), name ), &module ) ).second ) {
65                 globalErrorStream() << "module already registered: type=" << makeQuoted( type ) << " name=" << makeQuoted( name ) << "\n";
66         }
67         else
68         {
69                 globalOutputStream() << "Module Registered: type=" << makeQuoted( type ) << " version=" << makeQuoted( version ) << " name=" << makeQuoted( name ) << "\n";
70         }
71 }
72
73 Module* findModule( const char* type, int version, const char* name ) const {
74         Modules_::const_iterator i = m_modules.find( ModuleKey( ModuleType( type, version ), name ) );
75         if ( i != m_modules.end() ) {
76                 return ( *i ).second;
77         }
78         return 0;
79 }
80
81 void foreachModule( const char* type, int version, const Visitor& visitor ){
82         for ( Modules_::const_iterator i = m_modules.begin(); i != m_modules.end(); ++i )
83         {
84                 if ( string_equal( ( *i ).first.first.first.c_str(), type ) ) {
85                         visitor.visit( ( *i ).first.second.c_str(), *( *i ).second );
86                 }
87         }
88 }
89 };
90
91
92 #if defined( WIN32 )
93
94 #include <windows.h>
95
96 #define FORMAT_BUFSIZE 2048
97 const char* FormatGetLastError(){
98         static char buf[FORMAT_BUFSIZE];
99         FormatMessage(
100                 FORMAT_MESSAGE_FROM_SYSTEM |
101                 FORMAT_MESSAGE_IGNORE_INSERTS,
102                 NULL,
103                 GetLastError(),
104                 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
105                 buf,
106                 FORMAT_BUFSIZE,
107                 NULL
108                 );
109         return buf;
110 }
111
112 class DynamicLibrary
113 {
114 HMODULE m_library;
115 public:
116 typedef int ( __stdcall * FunctionPointer )();
117
118 DynamicLibrary( const char* filename ){
119         m_library = LoadLibrary( filename );
120         if ( m_library == 0 ) {
121                 globalErrorStream() << "LoadLibrary failed: '" << filename << "'\n";
122                 globalErrorStream() << "GetLastError: " << FormatGetLastError();
123         }
124 }
125 ~DynamicLibrary(){
126         if ( !failed() ) {
127                 FreeLibrary( m_library );
128         }
129 }
130 bool failed(){
131         return m_library == 0;
132 }
133 FunctionPointer findSymbol( const char* symbol ){
134         FunctionPointer address = GetProcAddress( m_library, symbol );
135         if ( address == 0 ) {
136                 globalErrorStream() << "GetProcAddress failed: '" << symbol << "'\n";
137                 globalErrorStream() << "GetLastError: " << FormatGetLastError();
138         }
139         return address;
140 }
141 };
142
143 #elif defined( POSIX )
144
145 #include <dlfcn.h>
146
147 class DynamicLibrary
148 {
149 void* m_library;
150 public:
151 typedef int ( *FunctionPointer )();
152
153 DynamicLibrary( const char* filename ){
154         m_library = dlopen( filename, RTLD_NOW );
155 }
156 ~DynamicLibrary(){
157         if ( !failed() ) {
158                 dlclose( m_library );
159         }
160 }
161 bool failed(){
162         return m_library == 0;
163 }
164 FunctionPointer findSymbol( const char* symbol ){
165         FunctionPointer p = (FunctionPointer)dlsym( m_library, symbol );
166         if ( p == 0 ) {
167                 const char* error = reinterpret_cast<const char*>( dlerror() );
168                 if ( error != 0 ) {
169                         globalErrorStream() << error;
170                 }
171         }
172         return p;
173 }
174 };
175
176 #else
177 #error "unsupported platform"
178 #endif
179
180 class DynamicLibraryModule
181 {
182 typedef void ( RADIANT_DLLIMPORT * RegisterModulesFunc )( ModuleServer& server );
183 DynamicLibrary m_library;
184 RegisterModulesFunc m_registerModule;
185 public:
186 DynamicLibraryModule( const char* filename )
187         : m_library( filename ), m_registerModule( 0 ){
188         if ( !m_library.failed() ) {
189                 m_registerModule = reinterpret_cast<RegisterModulesFunc>( m_library.findSymbol( "Radiant_RegisterModules" ) );
190 #if 0
191                 if ( !m_registerModule ) {
192                         m_registerModule = reinterpret_cast<RegisterModulesFunc>( m_library.findSymbol( "Radiant_RegisterModules@4" ) );
193                 }
194 #endif
195         }
196 }
197 bool failed(){
198         return m_registerModule == 0;
199 }
200 void registerModules( ModuleServer& server ){
201         m_registerModule( server );
202 }
203 };
204
205
206 class Libraries
207 {
208 typedef std::vector<DynamicLibraryModule*> libraries_t;
209 libraries_t m_libraries;
210
211 public:
212 ~Libraries(){
213         release();
214 }
215 void registerLibrary( const char* filename, ModuleServer& server ){
216         DynamicLibraryModule* library = new DynamicLibraryModule( filename );
217
218         if ( library->failed() ) {
219                 delete library;
220         }
221         else
222         {
223                 m_libraries.push_back( library );
224                 library->registerModules( server );
225         }
226 }
227 void release(){
228         for ( libraries_t::iterator i = m_libraries.begin(); i != m_libraries.end(); ++i )
229         {
230                 delete *i;
231         }
232 }
233 void clear(){
234         m_libraries.clear();
235 }
236 };
237
238
239 Libraries g_libraries;
240 RadiantModuleServer g_server;
241
242 ModuleServer& GlobalModuleServer_get(){
243         return g_server;
244 }
245
246 void GlobalModuleServer_loadModule( const char* filename ){
247         g_libraries.registerLibrary( filename, g_server );
248 }
249
250 void GlobalModuleServer_Initialise(){
251 }
252
253 void GlobalModuleServer_Shutdown(){
254 }