| 1 | /* | 
|---|
| 2 | ----------------------------------------------------------------------------- | 
|---|
| 3 | This source file is part of OGRE | 
|---|
| 4 |     (Object-oriented Graphics Rendering Engine) | 
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ | 
|---|
| 6 |  | 
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd | 
|---|
| 8 | Also see acknowledgements in Readme.html | 
|---|
| 9 |  | 
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under | 
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software | 
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later | 
|---|
| 13 | version. | 
|---|
| 14 |  | 
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT | 
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | 
|---|
| 18 |  | 
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with | 
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to | 
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. | 
|---|
| 23 |  | 
|---|
| 24 | You may alternatively use this source under the terms of a specific version of | 
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from | 
|---|
| 26 | Torus Knot Software Ltd. | 
|---|
| 27 | ----------------------------------------------------------------------------- | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // 08/11/2009: Small adjustments for Orxonox by Fabian 'x3n' Landau | 
|---|
| 31 |  | 
|---|
| 32 | #include "DynLib.h" | 
|---|
| 33 |  | 
|---|
| 34 | #include "util/Exception.h" | 
|---|
| 35 |  | 
|---|
| 36 | #ifdef ORXONOX_PLATFORM_WINDOWS | 
|---|
| 37 | #  define WIN32_LEAN_AND_MEAN | 
|---|
| 38 | #  ifndef NOMINMAX | 
|---|
| 39 | #    define NOMINMAX // required to stop windows.h messing up std::min | 
|---|
| 40 | #  endif | 
|---|
| 41 | #  include <windows.h> | 
|---|
| 42 | #endif | 
|---|
| 43 |  | 
|---|
| 44 | #ifdef ORXONOX_PLATFORM_LINUX | 
|---|
| 45 | #  include <dlfcn.h> | 
|---|
| 46 | #endif | 
|---|
| 47 |  | 
|---|
| 48 | #ifdef ORXONOX_PLATFORM_APPLE | 
|---|
| 49 | #  include <macPlugins.h> | 
|---|
| 50 | #endif | 
|---|
| 51 |  | 
|---|
| 52 | namespace orxonox | 
|---|
| 53 | { | 
|---|
| 54 |     //----------------------------------------------------------------------- | 
|---|
| 55 |     DynLib::DynLib( const std::string& name ) | 
|---|
| 56 |     { | 
|---|
| 57 |         mName = name; | 
|---|
| 58 |         m_hInst = NULL; | 
|---|
| 59 |     } | 
|---|
| 60 |  | 
|---|
| 61 |     //----------------------------------------------------------------------- | 
|---|
| 62 |     DynLib::~DynLib() | 
|---|
| 63 |     { | 
|---|
| 64 |     } | 
|---|
| 65 |  | 
|---|
| 66 |     //----------------------------------------------------------------------- | 
|---|
| 67 |     void DynLib::load() | 
|---|
| 68 |     { | 
|---|
| 69 |         // Log library load | 
|---|
| 70 |         COUT(2) << "Loading module " << mName << std::endl; | 
|---|
| 71 |  | 
|---|
| 72 |         std::string name = mName; | 
|---|
| 73 | #ifdef ORXONOX_PLATFORM_LINUX | 
|---|
| 74 |         // dlopen() does not add .so to the filename, like windows does for .dll | 
|---|
| 75 |         if (name.substr(name.length() - 3, 3) != ".so") | 
|---|
| 76 |            name += ".so"; | 
|---|
| 77 | #endif | 
|---|
| 78 |  | 
|---|
| 79 |         m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD( name.c_str() ); | 
|---|
| 80 |  | 
|---|
| 81 |         if( !m_hInst ) | 
|---|
| 82 |             ThrowException( | 
|---|
| 83 |                 General, | 
|---|
| 84 |                 "Could not load dynamic library " + mName + | 
|---|
| 85 |                 ".  System Error: " + dynlibError()); | 
|---|
| 86 |     } | 
|---|
| 87 |  | 
|---|
| 88 |     //----------------------------------------------------------------------- | 
|---|
| 89 |     void DynLib::unload() | 
|---|
| 90 |     { | 
|---|
| 91 |         // Log library unload | 
|---|
| 92 |         COUT(4) << "Unloading module " << mName << std::endl; | 
|---|
| 93 |  | 
|---|
| 94 |         if( DYNLIB_UNLOAD( m_hInst ) ) | 
|---|
| 95 |         { | 
|---|
| 96 |             ThrowException( | 
|---|
| 97 |                 General, | 
|---|
| 98 |                 "Could not unload dynamic library " + mName + | 
|---|
| 99 |                 ".  System Error: " + dynlibError()); | 
|---|
| 100 |         } | 
|---|
| 101 |  | 
|---|
| 102 |     } | 
|---|
| 103 |  | 
|---|
| 104 |     //----------------------------------------------------------------------- | 
|---|
| 105 |     void* DynLib::getSymbol( const std::string& strName ) const throw() | 
|---|
| 106 |     { | 
|---|
| 107 |         return (void*)DYNLIB_GETSYM( m_hInst, strName.c_str() ); | 
|---|
| 108 |     } | 
|---|
| 109 |     //----------------------------------------------------------------------- | 
|---|
| 110 |     std::string DynLib::dynlibError( void ) | 
|---|
| 111 |     { | 
|---|
| 112 | #if defined(ORXONOX_PLATFORM_WINDOWS) | 
|---|
| 113 |         LPVOID lpMsgBuf; | 
|---|
| 114 |         FormatMessage( | 
|---|
| 115 |             FORMAT_MESSAGE_ALLOCATE_BUFFER | | 
|---|
| 116 |             FORMAT_MESSAGE_FROM_SYSTEM | | 
|---|
| 117 |             FORMAT_MESSAGE_IGNORE_INSERTS, | 
|---|
| 118 |             NULL, | 
|---|
| 119 |             GetLastError(), | 
|---|
| 120 |             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | 
|---|
| 121 |             (LPTSTR) &lpMsgBuf, | 
|---|
| 122 |             0, | 
|---|
| 123 |             NULL | 
|---|
| 124 |             ); | 
|---|
| 125 |         std::string ret = (char*)lpMsgBuf; | 
|---|
| 126 |         // Free the buffer. | 
|---|
| 127 |         LocalFree( lpMsgBuf ); | 
|---|
| 128 |         return ret; | 
|---|
| 129 | #elif defined(ORXONOX_PLATFORM_LINUX) | 
|---|
| 130 |         return std::string(dlerror()); | 
|---|
| 131 | #elif defined(ORXONOX_PLATFORM_APPLE) | 
|---|
| 132 |         return std::string(mac_errorBundle()); | 
|---|
| 133 | #else | 
|---|
| 134 |         return std::string(""); | 
|---|
| 135 | #endif | 
|---|
| 136 |     } | 
|---|
| 137 |  | 
|---|
| 138 | } | 
|---|