2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
23 #include "globaldefs.h"
25 #include "debugging/debugging.h"
32 #include "modulesystem.h"
34 class RadiantModuleServer : public ModuleServer
36 typedef std::pair<CopiedString, int> ModuleType;
37 typedef std::pair<ModuleType, CopiedString> ModuleKey;
38 typedef std::map<ModuleKey, Module*> Modules_;
43 RadiantModuleServer() : m_error( false ){
46 void setError( bool error ){
49 bool getError() const {
53 TextOutputStream& getOutputStream(){
54 return globalOutputStream();
56 TextOutputStream& getErrorStream(){
57 return globalErrorStream();
59 DebugMessageHandler& getDebugMessageHandler(){
60 return globalDebugMessageHandler();
63 void registerModule( const char* type, int version, const char* name, Module& module ){
64 ASSERT_NOTNULL( &module );
65 if ( !m_modules.insert( Modules_::value_type( ModuleKey( ModuleType( type, version ), name ), &module ) ).second ) {
66 globalErrorStream() << "module already registered: type=" << makeQuoted( type ) << " name=" << makeQuoted( name ) << "\n";
70 globalOutputStream() << "Module Registered: type=" << makeQuoted( type ) << " version=" << makeQuoted( version ) << " name=" << makeQuoted( name ) << "\n";
74 Module* findModule( const char* type, int version, const char* name ) const {
75 Modules_::const_iterator i = m_modules.find( ModuleKey( ModuleType( type, version ), name ) );
76 if ( i != m_modules.end() ) {
82 void foreachModule( const char* type, int version, const Visitor& visitor ){
83 for ( Modules_::const_iterator i = m_modules.begin(); i != m_modules.end(); ++i )
85 if ( string_equal( ( *i ).first.first.first.c_str(), type ) ) {
86 visitor.visit( ( *i ).first.second.c_str(), *( *i ).second );
97 const int FORMAT_BUFSIZE = 2048;
98 const char* FormatGetLastError(){
99 static char buf[FORMAT_BUFSIZE];
101 FORMAT_MESSAGE_FROM_SYSTEM |
102 FORMAT_MESSAGE_IGNORE_INSERTS,
105 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
117 typedef int ( __stdcall * FunctionPointer )();
119 DynamicLibrary( const char* filename ){
120 m_library = LoadLibrary( filename );
121 if ( m_library == 0 ) {
122 globalErrorStream() << "LoadLibrary failed: '" << filename << "'\n";
123 globalErrorStream() << "GetLastError: " << FormatGetLastError();
128 FreeLibrary( m_library );
132 return m_library == 0;
134 FunctionPointer findSymbol( const char* symbol ){
135 FunctionPointer address = (FunctionPointer) GetProcAddress( m_library, symbol );
136 if ( address == 0 ) {
137 globalErrorStream() << "GetProcAddress failed: '" << symbol << "'\n";
138 globalErrorStream() << "GetLastError: " << FormatGetLastError();
148 class DynamicLibrary {
151 typedef int ( *FunctionPointer )();
153 DynamicLibrary( const char* filename ){
154 m_library = dlopen( filename, RTLD_NOW );
158 dlclose( m_library );
162 return m_library == 0;
164 FunctionPointer findSymbol( const char* symbol ){
165 FunctionPointer p = (FunctionPointer)dlsym( m_library, symbol );
167 const char* error = reinterpret_cast<const char*>( dlerror() );
169 globalErrorStream() << error;
177 #error "unsupported platform"
180 class DynamicLibraryModule
182 typedef void ( RADIANT_DLLIMPORT * RegisterModulesFunc )( ModuleServer& server );
183 DynamicLibrary m_library;
184 RegisterModulesFunc m_registerModule;
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" ) );
191 if ( !m_registerModule ) {
192 m_registerModule = reinterpret_cast<RegisterModulesFunc>( m_library.findSymbol( "Radiant_RegisterModules@4" ) );
198 return m_registerModule == 0;
200 void registerModules( ModuleServer& server ){
201 m_registerModule( server );
208 typedef std::vector<DynamicLibraryModule*> libraries_t;
209 libraries_t m_libraries;
215 void registerLibrary( const char* filename, ModuleServer& server ){
216 DynamicLibraryModule* library = new DynamicLibraryModule( filename );
218 if ( library->failed() ) {
223 m_libraries.push_back( library );
224 library->registerModules( server );
228 for ( libraries_t::iterator i = m_libraries.begin(); i != m_libraries.end(); ++i )
239 Libraries g_libraries;
240 RadiantModuleServer g_server;
242 ModuleServer& GlobalModuleServer_get(){
246 void GlobalModuleServer_loadModule( const char* filename ){
247 g_libraries.registerLibrary( filename, g_server );
250 void GlobalModuleServer_Initialise(){
253 void GlobalModuleServer_Shutdown(){