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
24 #include "debugging/debugging.h"
30 #include "modulesystem.h"
32 class RadiantModuleServer : public ModuleServer
34 typedef std::pair<std::string, int> ModuleType;
35 typedef std::pair<ModuleType, std::string> ModuleKey;
36 typedef std::map<ModuleKey, Module*> Modules_;
41 RadiantModuleServer() : m_error( false ){
44 void setError( bool error ){
47 bool getError() const {
51 TextOutputStream& getOutputStream(){
52 return globalOutputStream();
54 TextOutputStream& getErrorStream(){
55 return globalErrorStream();
57 DebugMessageHandler& getDebugMessageHandler(){
58 return globalDebugMessageHandler();
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";
68 globalOutputStream() << "Module Registered: type=" << makeQuoted( type ) << " version=" << makeQuoted( version ) << " name=" << makeQuoted( name ) << "\n";
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() ) {
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 )
83 if ( string_equal( ( *i ).first.first.first.c_str(), type ) ) {
84 visitor.visit( ( *i ).first.second.c_str(), *( *i ).second );
95 #define FORMAT_BUFSIZE 2048
96 const char* FormatGetLastError(){
97 static char buf[FORMAT_BUFSIZE];
99 FORMAT_MESSAGE_FROM_SYSTEM |
100 FORMAT_MESSAGE_IGNORE_INSERTS,
103 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
115 typedef int ( __stdcall * FunctionPointer )();
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();
126 FreeLibrary( m_library );
130 return m_library == 0;
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();
142 #elif defined( POSIX )
150 typedef int ( *FunctionPointer )();
152 DynamicLibrary( const char* filename ){
153 m_library = dlopen(filename, RTLD_LOCAL
161 globalErrorStream() << "LoadLibrary failed: '" << filename << "'\n";
162 if ( const char* error = dlerror() )
163 globalErrorStream() << "GetLastError: " << error;
168 dlclose( m_library );
172 return m_library == 0;
174 FunctionPointer findSymbol( const char* symbol ){
175 FunctionPointer p = (FunctionPointer)dlsym( m_library, symbol );
177 const char* error = reinterpret_cast<const char*>( dlerror() );
179 globalErrorStream() << error;
187 #error "unsupported platform"
190 class DynamicLibraryModule
192 typedef void ( RADIANT_DLLIMPORT * RegisterModulesFunc )( ModuleServer& server );
193 DynamicLibrary m_library;
194 RegisterModulesFunc m_registerModule;
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" ) );
201 if ( !m_registerModule ) {
202 m_registerModule = reinterpret_cast<RegisterModulesFunc>( m_library.findSymbol( "Radiant_RegisterModules@4" ) );
208 return m_registerModule == 0;
210 void registerModules( ModuleServer& server ){
211 m_registerModule( server );
218 typedef std::vector<DynamicLibraryModule*> libraries_t;
219 libraries_t m_libraries;
225 void registerLibrary( const char* filename, ModuleServer& server ){
226 DynamicLibraryModule* library = new DynamicLibraryModule( filename );
228 if ( library->failed() ) {
233 m_libraries.push_back( library );
234 library->registerModules( server );
238 for ( libraries_t::iterator i = m_libraries.begin(); i != m_libraries.end(); ++i )
249 Libraries g_libraries;
250 RadiantModuleServer g_server;
252 ModuleServer& GlobalModuleServer_get(){
256 void GlobalModuleServer_loadModule( const char* filename ){
257 g_libraries.registerLibrary( filename, g_server );
260 void GlobalModuleServer_Initialise(){
263 void GlobalModuleServer_Shutdown(){