Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/libraries/src/core/DynLib.cc @ 5629

Last change on this file since 5629 was 5626, checked in by landauf, 15 years ago

Added a dynamic library loader (more or less a copy of Ogre::DynLibManager but with some adjustments for Orxonox). This allows us to load plugins at runtime. Plugin-libraries must be declared with the "PLUGIN" switch in ORXONOX_ADD_LIBRARY in CMake.

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