]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/server.cpp
radiant: does not use Linux home path on MacOS
[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 #include "globaldefs.h"
24
25 #include "debugging/debugging.h"
26 #include "warnings.h"
27
28 #include <vector>
29 #include <map>
30 #include "os/path.h"
31
32 #include "modulesystem.h"
33
34 class RadiantModuleServer : public ModuleServer
35 {
36 typedef std::pair<CopiedString, int> ModuleType;
37 typedef std::pair<ModuleType, CopiedString> ModuleKey;
38 typedef std::map<ModuleKey, Module*> Modules_;
39 Modules_ m_modules;
40 bool m_error;
41
42 public:
43 RadiantModuleServer() : m_error( false ){
44 }
45
46 void setError( bool error ){
47         m_error = error;
48 }
49 bool getError() const {
50         return m_error;
51 }
52
53 TextOutputStream& getOutputStream(){
54         return globalOutputStream();
55 }
56 TextOutputStream& getErrorStream(){
57         return globalErrorStream();
58 }
59 DebugMessageHandler& getDebugMessageHandler(){
60         return globalDebugMessageHandler();
61 }
62
63 void registerModule( const char* type, int version, const char* name, Module& 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 GDEF_OS_WINDOWS
93
94 #include <windows.h>
95
96 const int 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 = (FunctionPointer) 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 GDEF_OS_POSIX
144
145 #include <dlfcn.h>
146
147 class DynamicLibrary {
148         void *m_library;
149 public:
150 typedef int ( *FunctionPointer )();
151
152 DynamicLibrary( const char* filename ){
153         m_library = dlopen( filename, RTLD_NOW );
154 }
155 ~DynamicLibrary(){
156         if ( !failed() ) {
157                 dlclose( m_library );
158         }
159 }
160 bool failed(){
161         return m_library == 0;
162 }
163 FunctionPointer findSymbol( const char* symbol ){
164         FunctionPointer p = (FunctionPointer)dlsym( m_library, symbol );
165         if ( p == 0 ) {
166                 const char* error = reinterpret_cast<const char*>( dlerror() );
167                 if ( error != 0 ) {
168                         globalErrorStream() << error;
169                 }
170         }
171         return p;
172 }
173 };
174
175 #else
176 #error "unsupported platform"
177 #endif
178
179 class DynamicLibraryModule
180 {
181 typedef void ( RADIANT_DLLIMPORT * RegisterModulesFunc )( ModuleServer& server );
182 DynamicLibrary m_library;
183 RegisterModulesFunc m_registerModule;
184 public:
185 DynamicLibraryModule( const char* filename )
186         : m_library( filename ), m_registerModule( 0 ){
187         if ( !m_library.failed() ) {
188                 m_registerModule = reinterpret_cast<RegisterModulesFunc>( m_library.findSymbol( "Radiant_RegisterModules" ) );
189 #if 0
190                 if ( !m_registerModule ) {
191                         m_registerModule = reinterpret_cast<RegisterModulesFunc>( m_library.findSymbol( "Radiant_RegisterModules@4" ) );
192                 }
193 #endif
194         }
195 }
196 bool failed(){
197         return m_registerModule == 0;
198 }
199 void registerModules( ModuleServer& server ){
200         m_registerModule( server );
201 }
202 };
203
204
205 class Libraries
206 {
207 typedef std::vector<DynamicLibraryModule*> libraries_t;
208 libraries_t m_libraries;
209
210 public:
211 ~Libraries(){
212         release();
213 }
214 void registerLibrary( const char* filename, ModuleServer& server ){
215         DynamicLibraryModule* library = new DynamicLibraryModule( filename );
216
217         if ( library->failed() ) {
218                 delete library;
219         }
220         else
221         {
222                 m_libraries.push_back( library );
223                 library->registerModules( server );
224         }
225 }
226 void release(){
227         for ( libraries_t::iterator i = m_libraries.begin(); i != m_libraries.end(); ++i )
228         {
229                 delete *i;
230         }
231 }
232 void clear(){
233         m_libraries.clear();
234 }
235 };
236
237
238 Libraries g_libraries;
239 RadiantModuleServer g_server;
240
241 ModuleServer& GlobalModuleServer_get(){
242         return g_server;
243 }
244
245 void GlobalModuleServer_loadModule( const char* filename ){
246         g_libraries.registerLibrary( filename, g_server );
247 }
248
249 void GlobalModuleServer_Initialise(){
250 }
251
252 void GlobalModuleServer_Shutdown(){
253 }