[5631] | 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 | |
---|
[8351] | 44 | #ifdef ORXONOX_PLATFORM_UNIX |
---|
[5750] | 45 | # include <dlfcn.h> |
---|
| 46 | #endif |
---|
| 47 | |
---|
[5631] | 48 | #ifdef ORXONOX_PLATFORM_APPLE |
---|
[8351] | 49 | # include <OSX/macUtils.h> // OGRE include |
---|
[5631] | 50 | #endif |
---|
| 51 | |
---|
| 52 | namespace orxonox |
---|
| 53 | { |
---|
| 54 | //----------------------------------------------------------------------- |
---|
| 55 | DynLib::DynLib( const std::string& name ) |
---|
| 56 | { |
---|
| 57 | mName = name; |
---|
[10295] | 58 | #if defined(ORXONOX_PLATFORM_WINDOWS) |
---|
| 59 | //altered search path doesn't work with paths with forward slashes |
---|
| 60 | std::replace(mName.begin(), mName.end(), '/', '\\'); |
---|
| 61 | #endif |
---|
[5631] | 62 | m_hInst = NULL; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | //----------------------------------------------------------------------- |
---|
| 66 | DynLib::~DynLib() |
---|
| 67 | { |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | //----------------------------------------------------------------------- |
---|
| 71 | void DynLib::load() |
---|
| 72 | { |
---|
| 73 | // Log library load |
---|
[8858] | 74 | orxout(internal_info) << "Loading module " << mName << endl; |
---|
[5631] | 75 | |
---|
[6073] | 76 | std::string name = mName; |
---|
[5631] | 77 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
| 78 | // dlopen() does not add .so to the filename, like windows does for .dll |
---|
| 79 | if (name.substr(name.length() - 3, 3) != ".so") |
---|
| 80 | name += ".so"; |
---|
[8351] | 81 | #elif defined(ORXONOX_PLATFORM_APPLE) |
---|
| 82 | // dlopen() does not add .dylib to the filename, like windows does for .dll |
---|
| 83 | if (name.substr(name.length() - 6, 6) != ".dylib") |
---|
| 84 | name += ".dylib"; |
---|
| 85 | #elif defined(ORXONOX_PLATFORM_WINDOWS) |
---|
| 86 | // Although LoadLibraryEx will add .dll itself when you only specify the library name, |
---|
| 87 | // if you include a relative path then it does not. So, add it to be sure. |
---|
| 88 | if (name.substr(name.length() - 4, 4) != ".dll") |
---|
| 89 | name += ".dll"; |
---|
[5631] | 90 | #endif |
---|
| 91 | |
---|
| 92 | m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD( name.c_str() ); |
---|
| 93 | |
---|
[6417] | 94 | if (!m_hInst) |
---|
[5631] | 95 | ThrowException( |
---|
| 96 | General, |
---|
| 97 | "Could not load dynamic library " + mName + |
---|
| 98 | ". System Error: " + dynlibError()); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | //----------------------------------------------------------------------- |
---|
| 102 | void DynLib::unload() |
---|
| 103 | { |
---|
| 104 | // Log library unload |
---|
[8858] | 105 | orxout(internal_info) << "Unloading module " << mName << endl; |
---|
[5631] | 106 | |
---|
[6417] | 107 | if (DYNLIB_UNLOAD( m_hInst )) |
---|
[6073] | 108 | { |
---|
[5631] | 109 | ThrowException( |
---|
| 110 | General, |
---|
| 111 | "Could not unload dynamic library " + mName + |
---|
| 112 | ". System Error: " + dynlibError()); |
---|
[6073] | 113 | } |
---|
[5631] | 114 | |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | //----------------------------------------------------------------------- |
---|
| 118 | void* DynLib::getSymbol( const std::string& strName ) const throw() |
---|
| 119 | { |
---|
| 120 | return (void*)DYNLIB_GETSYM( m_hInst, strName.c_str() ); |
---|
| 121 | } |
---|
| 122 | //----------------------------------------------------------------------- |
---|
| 123 | std::string DynLib::dynlibError( void ) |
---|
| 124 | { |
---|
| 125 | #if defined(ORXONOX_PLATFORM_WINDOWS) |
---|
| 126 | LPVOID lpMsgBuf; |
---|
| 127 | FormatMessage( |
---|
| 128 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
---|
| 129 | FORMAT_MESSAGE_FROM_SYSTEM | |
---|
| 130 | FORMAT_MESSAGE_IGNORE_INSERTS, |
---|
| 131 | NULL, |
---|
| 132 | GetLastError(), |
---|
| 133 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
---|
| 134 | (LPTSTR) &lpMsgBuf, |
---|
| 135 | 0, |
---|
| 136 | NULL |
---|
| 137 | ); |
---|
| 138 | std::string ret = (char*)lpMsgBuf; |
---|
| 139 | // Free the buffer. |
---|
| 140 | LocalFree( lpMsgBuf ); |
---|
| 141 | return ret; |
---|
[8351] | 142 | #elif defined(ORXONOX_PLATFORM_UNIX) |
---|
[5631] | 143 | return std::string(dlerror()); |
---|
| 144 | #else |
---|
[6417] | 145 | return ""; |
---|
[5631] | 146 | #endif |
---|
| 147 | } |
---|
| 148 | } |
---|