[395] | 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 |
|
---|
[319] | 7 | #include "nv/core/library.hh"
|
---|
[4] | 8 |
|
---|
| 9 | #if NV_PLATFORM == NV_WINDOWS
|
---|
| 10 | # define WIN32_LEAN_AND_MEAN
|
---|
| 11 | # include <windows.h>
|
---|
| 12 | # define NV_LIB_EXT ".dll"
|
---|
[402] | 13 | # define NV_LIB_OPEN( name ) static_cast<void*>( LoadLibraryEx( name, NULL, LOAD_WITH_ALTERED_SEARCH_PATH ) )
|
---|
| 14 | # define NV_LIB_GET( handle, name ) reinterpret_cast<void*>( GetProcAddress( static_cast<HMODULE>( handle ), name ) )
|
---|
| 15 | # define NV_LIB_CLOSE( handle ) ( FreeLibrary( static_cast<HMODULE>( handle ) ) != 0 )
|
---|
[113] | 16 | #elif NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE
|
---|
[4] | 17 | # include <dlfcn.h>
|
---|
| 18 | # define NV_LIB_EXT ".so"
|
---|
| 19 | # define NV_LIB_OPEN( name ) dlopen( name, RTLD_LAZY | RTLD_GLOBAL)
|
---|
[402] | 20 | # define NV_LIB_GET( handle, name ) dlsym( static_cast<void*>( handle ), name )
|
---|
| 21 | # define NV_LIB_CLOSE( handle ) ( dlclose( static_cast<void*>( handle ) ) == 0 )
|
---|
[4] | 22 | #elif NV_PLATFORM == NV_APPLE
|
---|
| 23 | # include "macUtils.h"
|
---|
| 24 | # include <dlfcn.h>
|
---|
| 25 | # define NV_LIB_EXT ".dylib"
|
---|
| 26 | # define NV_LIB_OPEN( name ) mac_loadExeBundle( name )
|
---|
| 27 | # define NV_LIB_GET( handle, name ) mac_getBundleSym( handle, name )
|
---|
[402] | 28 | # define NV_LIB_CLOSE( handle ) ( mac_unloadExeBundle( handle ) == 0 )
|
---|
[4] | 29 | #endif
|
---|
| 30 |
|
---|
[319] | 31 | #include "nv/core/logging.hh"
|
---|
[4] | 32 |
|
---|
| 33 | using namespace nv;
|
---|
| 34 |
|
---|
[109] | 35 | library::library()
|
---|
[121] | 36 | : m_handle( nullptr ), m_name()
|
---|
[4] | 37 | {
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[399] | 40 | void library::open( string_view name )
|
---|
[4] | 41 | {
|
---|
[437] | 42 | m_name = name;
|
---|
[166] | 43 | if ( !open() )
|
---|
| 44 | {
|
---|
| 45 | m_handle = nullptr;
|
---|
[403] | 46 | NV_LOG_CRITICAL( "library \"", name, "\" : failed to load!" );
|
---|
| 47 | NV_ABORT( "Can't load library!" );
|
---|
[166] | 48 | }
|
---|
[4] | 49 | }
|
---|
| 50 |
|
---|
[399] | 51 | bool nv::library::try_open( string_view name )
|
---|
[166] | 52 | {
|
---|
[437] | 53 | m_name = name;
|
---|
[166] | 54 | if ( !open() )
|
---|
| 55 | {
|
---|
| 56 | m_handle = nullptr;
|
---|
| 57 | return false;
|
---|
| 58 | }
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[399] | 62 | string_view library::get_name() const
|
---|
[4] | 63 | {
|
---|
[437] | 64 | return m_name;
|
---|
[4] | 65 | }
|
---|
| 66 |
|
---|
[166] | 67 | bool library::open( )
|
---|
[4] | 68 | {
|
---|
| 69 | if ( m_handle != NULL )
|
---|
| 70 | {
|
---|
[166] | 71 | return true;
|
---|
[4] | 72 | }
|
---|
[437] | 73 | NV_LOG_NOTICE( "library \"", m_name, "\" : loading..." );
|
---|
[4] | 74 |
|
---|
[438] | 75 | string128 name( m_name );
|
---|
| 76 | string_view ext( NV_LIB_EXT );
|
---|
[4] | 77 |
|
---|
[438] | 78 | if ( name.length() < ext.length() || !name.ends_with( ext ) )
|
---|
[4] | 79 | {
|
---|
[438] | 80 | name.append( ext );
|
---|
[4] | 81 | }
|
---|
| 82 |
|
---|
[438] | 83 | m_handle = NV_LIB_OPEN( name.data() );
|
---|
[4] | 84 |
|
---|
| 85 | if ( m_handle == NULL )
|
---|
| 86 | {
|
---|
[438] | 87 | NV_LOG_NOTICE( "library \"", name, "\" : failed to open!" );
|
---|
[166] | 88 | return false;
|
---|
[4] | 89 | }
|
---|
[166] | 90 | return true;
|
---|
[4] | 91 | }
|
---|
| 92 |
|
---|
[399] | 93 | void* library::get( string_view symbol )
|
---|
[4] | 94 | {
|
---|
[402] | 95 | void* result = NV_LIB_GET( m_handle, symbol.data() );
|
---|
[4] | 96 | if ( !result )
|
---|
| 97 | {
|
---|
[437] | 98 | NV_LOG_CRITICAL( "library \"", m_name, "\" : can't find symbol \"", symbol, "\"" );
|
---|
[403] | 99 | NV_ABORT( "Library symbol load failed!" );
|
---|
[4] | 100 | }
|
---|
| 101 | return result;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[399] | 104 | void* nv::library::try_get( string_view symbol )
|
---|
[166] | 105 | {
|
---|
[402] | 106 | return NV_LIB_GET( m_handle, symbol.data() );
|
---|
[166] | 107 | }
|
---|
| 108 |
|
---|
[109] | 109 | bool library::is_open() const
|
---|
| 110 | {
|
---|
| 111 | return m_handle != nullptr;
|
---|
| 112 | }
|
---|
[4] | 113 |
|
---|
| 114 | void library::close()
|
---|
| 115 | {
|
---|
[402] | 116 | if ( ! NV_LIB_CLOSE( m_handle ) )
|
---|
[4] | 117 | {
|
---|
[437] | 118 | NV_LOG_ERROR( "library \"", m_name, "\" : can't close library!" );
|
---|
[4] | 119 | }
|
---|
[402] | 120 | m_handle = nullptr;
|
---|
[4] | 121 | }
|
---|
| 122 |
|
---|
| 123 | library::~library()
|
---|
| 124 | {
|
---|
[402] | 125 | if ( m_handle != nullptr )
|
---|
[4] | 126 | {
|
---|
| 127 | close();
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[437] | 131 | nv::string_view library::get_error()
|
---|
[4] | 132 | {
|
---|
| 133 | #if NV_PLATFORM == NV_WINDOWS
|
---|
| 134 | // We do hate WinAPI for code like this, don't we?
|
---|
[437] | 135 | static TCHAR buffer[256];
|
---|
| 136 | FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
---|
| 137 | NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>( &buffer ), 256, NULL );
|
---|
| 138 | return string_view( reinterpret_cast<char*>( buffer ) );
|
---|
[4] | 139 | #elif NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE
|
---|
[437] | 140 | return nv::string_view( dlerror() );
|
---|
[4] | 141 | #else
|
---|
[437] | 142 | return nv::string_view();
|
---|
[4] | 143 | #endif
|
---|
| 144 | }
|
---|