1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #include "nv/core/library.hh"
|
---|
8 |
|
---|
9 | #if NV_PLATFORM == NV_WINDOWS
|
---|
10 | # define WIN32_LEAN_AND_MEAN
|
---|
11 | # include <windows.h>
|
---|
12 | # define NV_LIB_EXT ".dll"
|
---|
13 | # define NV_LIB_HANDLE HMODULE
|
---|
14 | # define NV_LIB_OPEN( name ) LoadLibraryEx( name, NULL, LOAD_WITH_ALTERED_SEARCH_PATH )
|
---|
15 | # define NV_LIB_GET( handle, name ) GetProcAddress( handle, name )
|
---|
16 | # define NV_LIB_CLOSE( name ) !FreeLibrary( name )
|
---|
17 | #elif NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE
|
---|
18 | # include <dlfcn.h>
|
---|
19 | # define NV_LIB_EXT ".so"
|
---|
20 | # define NV_LIB_HANDLE void*
|
---|
21 | # define NV_LIB_OPEN( name ) dlopen( name, RTLD_LAZY | RTLD_GLOBAL)
|
---|
22 | # define NV_LIB_GET( handle, name ) dlsym( handle, name )
|
---|
23 | # define NV_LIB_CLOSE( name ) dlclose( name )
|
---|
24 | #elif NV_PLATFORM == NV_APPLE
|
---|
25 | # include "macUtils.h"
|
---|
26 | # include <dlfcn.h>
|
---|
27 | # define NV_LIB_EXT ".dylib"
|
---|
28 | # define NV_LIB_HANDLE CFBundleRef
|
---|
29 | # define NV_LIB_OPEN( name ) mac_loadExeBundle( name )
|
---|
30 | # define NV_LIB_GET( handle, name ) mac_getBundleSym( handle, name )
|
---|
31 | # define NV_LIB_CLOSE( name ) mac_unloadExeBundle( name )
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include "nv/core/logging.hh"
|
---|
35 |
|
---|
36 | using namespace nv;
|
---|
37 |
|
---|
38 | library::library()
|
---|
39 | : m_handle( nullptr ), m_name()
|
---|
40 | {
|
---|
41 | }
|
---|
42 |
|
---|
43 | void library::open( string_ref name )
|
---|
44 | {
|
---|
45 | m_name.assign( name.data(), name.size() );
|
---|
46 | if ( !open() )
|
---|
47 | {
|
---|
48 | m_handle = nullptr;
|
---|
49 | NV_THROW( library_error, "Can't load library!", name.data() );
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | bool nv::library::try_open( string_ref name )
|
---|
54 | {
|
---|
55 | m_name.assign( name.data(), name.size() );
|
---|
56 | if ( !open() )
|
---|
57 | {
|
---|
58 | m_handle = nullptr;
|
---|
59 | return false;
|
---|
60 | }
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 |
|
---|
64 | string_ref library::get_name() const
|
---|
65 | {
|
---|
66 | return string_ref( m_name );
|
---|
67 | }
|
---|
68 |
|
---|
69 | bool library::open( )
|
---|
70 | {
|
---|
71 | if ( m_handle != NULL )
|
---|
72 | {
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 | NV_LOG_NOTICE( "library : loading '", m_name, "'..." );
|
---|
76 |
|
---|
77 | std::string name = m_name;
|
---|
78 | string_ref ext( NV_LIB_EXT );
|
---|
79 |
|
---|
80 | if ( name.length() < ext.length() || name.substr( name.length() - ext.length(), ext.length() ) != ext )
|
---|
81 | {
|
---|
82 | name.append( ext.data(), ext.length() );
|
---|
83 | }
|
---|
84 |
|
---|
85 | m_handle = (void*)NV_LIB_OPEN( name.c_str() );
|
---|
86 |
|
---|
87 | if ( m_handle == NULL )
|
---|
88 | {
|
---|
89 | NV_LOG_NOTICE( "library : '", name, "' failed to open." );
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 | NV_LOG_NOTICE( "library : '", name, "' loaded." );
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | void* library::get( string_ref symbol )
|
---|
97 | {
|
---|
98 | void* result = (void*) NV_LIB_GET( (NV_LIB_HANDLE) m_handle, symbol.data() );
|
---|
99 | if ( !result )
|
---|
100 | {
|
---|
101 | NV_THROW( library_error, "Can't find symbol " + std::string(symbol.data(),symbol.size()) + "!", m_name );
|
---|
102 | }
|
---|
103 | return result;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void* nv::library::try_get( string_ref symbol )
|
---|
107 | {
|
---|
108 | return (void*) NV_LIB_GET( (NV_LIB_HANDLE) m_handle, symbol.data() );
|
---|
109 | }
|
---|
110 |
|
---|
111 | bool library::is_open() const
|
---|
112 | {
|
---|
113 | return m_handle != nullptr;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void library::close()
|
---|
117 | {
|
---|
118 | if ( NV_LIB_CLOSE( (NV_LIB_HANDLE)m_handle ) )
|
---|
119 | {
|
---|
120 | NV_LOG_ERROR( "library : can't close library '", m_name, "'!" );
|
---|
121 | }
|
---|
122 | m_handle = NULL;
|
---|
123 | }
|
---|
124 |
|
---|
125 | library::~library()
|
---|
126 | {
|
---|
127 | if ( m_handle != NULL )
|
---|
128 | {
|
---|
129 | close();
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | std::string library::get_error()
|
---|
134 | {
|
---|
135 | #if NV_PLATFORM == NV_WINDOWS
|
---|
136 | // We do hate WinAPI for code like this, don't we?
|
---|
137 | LPTSTR buffer = NULL;
|
---|
138 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
---|
139 | NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buffer, 0, NULL );
|
---|
140 | std::string msg( (char*)buffer );
|
---|
141 | LocalFree( buffer );
|
---|
142 | return msg;
|
---|
143 | #elif NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE
|
---|
144 | return string(dlerror());
|
---|
145 | #else
|
---|
146 | return string("");
|
---|
147 | #endif
|
---|
148 | }
|
---|