X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=radiant%2Fserver.cpp;h=718a0a2905f62d32fc03ef89d16a07f9509ad460;hb=3e2279821b1f73b7af46b0925090916c4e2f84f6;hp=500cc58e72682aecf8b733d708529bbf6f1072e9;hpb=62d99f889c0e98be65f779d3983109c84ce58cec;p=xonotic%2Fnetradiant.git diff --git a/radiant/server.cpp b/radiant/server.cpp index 500cc58e..718a0a29 100644 --- a/radiant/server.cpp +++ b/radiant/server.cpp @@ -20,6 +20,7 @@ */ #include "server.h" +#include "globaldefs.h" #include "debugging/debugging.h" #include "warnings.h" @@ -30,83 +31,92 @@ #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; +class RadiantModuleServer : public ModuleServer { + 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, 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 ); - } - } -} + 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) + { + 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 GDEF_OS_WINDOWS #include -#define FORMAT_BUFSIZE 2048 +const int 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; + 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 @@ -116,139 +126,157 @@ 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(); - } + m_library = LoadLibrary( filename ); + if ( m_library == 0 ) { + globalErrorStream() << "LoadLibrary failed: '" << filename << "'\n"; + globalErrorStream() << "GetLastError: " << FormatGetLastError(); + } } ~DynamicLibrary(){ - if ( !failed() ) { - FreeLibrary( m_library ); - } + if ( !failed() ) { + FreeLibrary( m_library ); + } } bool failed(){ - return m_library == 0; + 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; + FunctionPointer address = (FunctionPointer) GetProcAddress( m_library, symbol ); + if ( address == 0 ) { + globalErrorStream() << "GetProcAddress failed: '" << symbol << "'\n"; + globalErrorStream() << "GetLastError: " << FormatGetLastError(); + } + return address; } }; -#elif defined( POSIX ) +#elif GDEF_OS_POSIX #include -class DynamicLibrary -{ -void* m_library; +class DynamicLibrary { + 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_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; + } }; #else #error "unsupported platform" #endif -class DynamicLibraryModule -{ -typedef void ( RADIANT_DLLIMPORT * RegisterModulesFunc )( ModuleServer& server ); -DynamicLibrary m_library; -RegisterModulesFunc m_registerModule; +class DynamicLibraryModule { + 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" ) ); + 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" ) ); - } + 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 ); -} + } + } + + bool failed() + { + return m_registerModule == 0; + } + + void registerModules(ModuleServer &server) + { + m_registerModule(server); + } }; -class Libraries -{ -typedef std::vector libraries_t; -libraries_t m_libraries; +class 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() +{ }