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