X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=radiant%2Fserver.cpp;h=714521c7ec6e14e8abe06706f463a00c38b98c26;hb=318eb70605627162c9aefbaf240697e800e08bd6;hp=28b5659d4ea51e0f65d10bfa2ecff02b4d6d5f45;hpb=3591d7dc01732577ebbb25f3818ccec8f440338f;p=xonotic%2Fnetradiant.git diff --git a/radiant/server.cpp b/radiant/server.cpp index 28b5659d..714521c7 100644 --- a/radiant/server.cpp +++ b/radiant/server.cpp @@ -1,201 +1,186 @@ /* -Copyright (C) 2001-2006, William Joseph. -All Rights Reserved. + Copyright (C) 2001-2006, William Joseph. + All Rights Reserved. -This file is part of GtkRadiant. + This file is part of GtkRadiant. -GtkRadiant is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. + GtkRadiant is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. -GtkRadiant is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + GtkRadiant is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. -You should have received a copy of the GNU General Public License -along with GtkRadiant; if not, write to the Free Software -Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ + You should have received a copy of the GNU General Public License + along with GtkRadiant; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ #include "server.h" #include "debugging/debugging.h" -#include "warnings.h" -#include #include +#include #include "os/path.h" #include "modulesystem.h" class RadiantModuleServer : public ModuleServer { - typedef std::pair ModuleType; - typedef std::pair ModuleKey; - typedef std::map Modules_; - Modules_ m_modules; - bool m_error; +typedef std::pair ModuleType; +typedef std::pair ModuleKey; +typedef std::map Modules_; +Modules_ m_modules; +bool m_error; public: - RadiantModuleServer() : m_error(false) - { - } - - void setError(bool error) - { - m_error = error; - } - bool getError() const - { - return m_error; - } - - TextOutputStream& getOutputStream() - { - return globalOutputStream(); - } - TextOutputStream& getErrorStream() - { - return globalErrorStream(); - } - DebugMessageHandler& getDebugMessageHandler() - { - return globalDebugMessageHandler(); - } - - void registerModule(const char* type, int version, const char* name, Module& module) - { - ASSERT_NOTNULL(&module); - if(!m_modules.insert(Modules_::value_type(ModuleKey(ModuleType(type, version), name), &module)).second) - { - globalErrorStream() << "module already registered: type=" << makeQuoted(type) << " name=" << makeQuoted(name) << "\n"; - } - else - { - globalOutputStream() << "Module Registered: type=" << makeQuoted(type) << " version=" << makeQuoted(version) << " name=" << makeQuoted(name) << "\n"; - } - } - - Module* findModule(const char* type, int version, const char* name) const - { - Modules_::const_iterator i = m_modules.find(ModuleKey(ModuleType(type, version), name)); - if(i != m_modules.end()) - { - return (*i).second; - } - return 0; - } - - void foreachModule(const char* type, int version, Visitor& visitor) - { - for(Modules_::const_iterator i = m_modules.begin(); i != m_modules.end(); ++i) - { - if(string_equal((*i).first.first.first.c_str(), type)) - { - visitor.visit((*i).first.second.c_str(), *(*i).second); - } - } - } +RadiantModuleServer() : m_error( false ){ +} + +void setError( bool error ){ + m_error = error; +} +bool getError() const { + return m_error; +} + +TextOutputStream& getOutputStream(){ + return globalOutputStream(); +} +TextOutputStream& getErrorStream(){ + return globalErrorStream(); +} +DebugMessageHandler& getDebugMessageHandler(){ + return globalDebugMessageHandler(); +} + +void registerModule( const char* type, int version, const char* name, Module& module ){ + ASSERT_NOTNULL( &module ); + if ( !m_modules.insert( Modules_::value_type( ModuleKey( ModuleType( type, version ), name ), &module ) ).second ) { + globalErrorStream() << "module already registered: type=" << makeQuoted( type ) << " name=" << makeQuoted( name ) << "\n"; + } + else + { + globalOutputStream() << "Module Registered: type=" << makeQuoted( type ) << " version=" << makeQuoted( version ) << " name=" << makeQuoted( name ) << "\n"; + } +} + +Module* findModule( const char* type, int version, const char* name ) const { + Modules_::const_iterator i = m_modules.find( ModuleKey( ModuleType( type, version ), name ) ); + if ( i != m_modules.end() ) { + return ( *i ).second; + } + return 0; +} + +void foreachModule( const char* type, int version, const Visitor& visitor ){ + for ( Modules_::const_iterator i = m_modules.begin(); i != m_modules.end(); ++i ) + { + if ( string_equal( ( *i ).first.first.first.c_str(), type ) ) { + visitor.visit( ( *i ).first.second.c_str(), *( *i ).second ); + } + } +} }; -#if defined(WIN32) +#if defined( WIN32 ) #include #define FORMAT_BUFSIZE 2048 -const char* FormatGetLastError() -{ - static char buf[FORMAT_BUFSIZE]; - FormatMessage( - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - buf, - FORMAT_BUFSIZE, - NULL - ); - return buf; +const char* FormatGetLastError(){ + static char buf[FORMAT_BUFSIZE]; + FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + buf, + FORMAT_BUFSIZE, + NULL + ); + return buf; } class DynamicLibrary { - HMODULE m_library; +HMODULE m_library; public: - typedef int (__stdcall* FunctionPointer)(); - - DynamicLibrary(const char* filename) - { - m_library = LoadLibrary(filename); - if(m_library == 0) - { - globalErrorStream() << "LoadLibrary failed: '" << filename << "'\n"; - globalErrorStream() << "GetLastError: " << FormatGetLastError(); - } - } - ~DynamicLibrary() - { - if(!failed()) - { - FreeLibrary(m_library); - } - } - bool failed() - { - return m_library == 0; - } - FunctionPointer findSymbol(const char* symbol) - { - FunctionPointer address = GetProcAddress(m_library, symbol); - if(address == 0) - { - globalErrorStream() << "GetProcAddress failed: '" << symbol << "'\n"; - globalErrorStream() << "GetLastError: " << FormatGetLastError(); - } - return address; - } +typedef int ( __stdcall * FunctionPointer )(); + +DynamicLibrary( const char* filename ){ + m_library = LoadLibrary( filename ); + if ( m_library == 0 ) { + globalErrorStream() << "LoadLibrary failed: '" << filename << "'\n"; + globalErrorStream() << "GetLastError: " << FormatGetLastError(); + } +} +~DynamicLibrary(){ + if ( !failed() ) { + FreeLibrary( m_library ); + } +} +bool failed(){ + return m_library == 0; +} +FunctionPointer findSymbol( const char* symbol ){ + FunctionPointer address = (FunctionPointer) GetProcAddress( m_library, symbol ); + if ( address == 0 ) { + globalErrorStream() << "GetProcAddress failed: '" << symbol << "'\n"; + globalErrorStream() << "GetLastError: " << FormatGetLastError(); + } + return address; +} }; -#elif defined(POSIX) +#elif defined( POSIX ) #include class DynamicLibrary { - void* m_library; +void* m_library; public: - typedef int (* FunctionPointer)(); - - DynamicLibrary(const char* filename) - { - m_library = dlopen(filename, RTLD_NOW); - } - ~DynamicLibrary() - { - if(!failed()) - dlclose(m_library); - } - bool failed() - { - return m_library == 0; - } - FunctionPointer findSymbol(const char* symbol) - { - FunctionPointer p = (FunctionPointer)dlsym(m_library, symbol); - if(p == 0) - { - const char* error = reinterpret_cast(dlerror()); - if(error != 0) - { - globalErrorStream() << error; - } - } - return p; - } +typedef int ( *FunctionPointer )(); + +DynamicLibrary( const char* filename ){ + m_library = dlopen(filename, RTLD_LOCAL + | RTLD_NOW + #ifndef __APPLE__ + | RTLD_DEEPBIND + #endif + ); + if ( !m_library ) + { + globalErrorStream() << "LoadLibrary failed: '" << filename << "'\n"; + if ( const char* error = dlerror() ) + globalErrorStream() << "GetLastError: " << error; + } +} +~DynamicLibrary(){ + if ( !failed() ) { + dlclose( m_library ); + } +} +bool failed(){ + return m_library == 0; +} +FunctionPointer findSymbol( const char* symbol ){ + FunctionPointer p = (FunctionPointer)dlsym( m_library, symbol ); + if ( p == 0 ) { + const char* error = reinterpret_cast( dlerror() ); + if ( error != 0 ) { + globalErrorStream() << error; + } + } + return p; +} }; #else @@ -204,84 +189,76 @@ public: class DynamicLibraryModule { - typedef void (RADIANT_DLLEXPORT* RegisterModulesFunc)(ModuleServer& server); - DynamicLibrary m_library; - RegisterModulesFunc m_registerModule; +typedef void ( RADIANT_DLLIMPORT * RegisterModulesFunc )( ModuleServer& server ); +DynamicLibrary m_library; +RegisterModulesFunc m_registerModule; public: - DynamicLibraryModule(const char* filename) - : m_library(filename), m_registerModule(0) - { - if(!m_library.failed()) - { - m_registerModule = reinterpret_cast(m_library.findSymbol("Radiant_RegisterModules")); - } - } - bool failed() - { - return m_registerModule == 0; - } - void registerModules(ModuleServer& server) - { - m_registerModule(server); - } +DynamicLibraryModule( const char* filename ) + : m_library( filename ), m_registerModule( 0 ){ + if ( !m_library.failed() ) { + m_registerModule = reinterpret_cast( m_library.findSymbol( "Radiant_RegisterModules" ) ); +#if 0 + if ( !m_registerModule ) { + m_registerModule = reinterpret_cast( m_library.findSymbol( "Radiant_RegisterModules@4" ) ); + } +#endif + } +} +bool failed(){ + return m_registerModule == 0; +} +void registerModules( ModuleServer& server ){ + m_registerModule( server ); +} }; class Libraries { - typedef std::vector libraries_t; - libraries_t m_libraries; +typedef std::vector libraries_t; +libraries_t m_libraries; public: - ~Libraries() - { - release(); - } - void registerLibrary(const char* filename, ModuleServer& server) - { - DynamicLibraryModule* library = new DynamicLibraryModule(filename); - - if(library->failed()) - { - delete library; - } - else - { - m_libraries.push_back(library); - library->registerModules(server); - } - } - void release() - { - for(libraries_t::iterator i = m_libraries.begin(); i != m_libraries.end(); ++i) - { - delete *i; - } - } - void clear() - { - m_libraries.clear(); - } +~Libraries(){ + release(); +} +void registerLibrary( const char* filename, ModuleServer& server ){ + DynamicLibraryModule* library = new DynamicLibraryModule( filename ); + + if ( library->failed() ) { + delete library; + } + else + { + m_libraries.push_back( library ); + library->registerModules( server ); + } +} +void release(){ + for ( libraries_t::iterator i = m_libraries.begin(); i != m_libraries.end(); ++i ) + { + delete *i; + } +} +void clear(){ + m_libraries.clear(); +} }; Libraries g_libraries; RadiantModuleServer g_server; -ModuleServer& GlobalModuleServer_get() -{ - return g_server; +ModuleServer& GlobalModuleServer_get(){ + return g_server; } -void GlobalModuleServer_loadModule(const char* filename) -{ - g_libraries.registerLibrary(filename, g_server); +void GlobalModuleServer_loadModule( const char* filename ){ + g_libraries.registerLibrary( filename, g_server ); } -void GlobalModuleServer_Initialise() -{ +void GlobalModuleServer_Initialise(){ } -void GlobalModuleServer_Shutdown() -{ +void GlobalModuleServer_Shutdown(){ }