1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include "UnityPrefix.h"
#include "LoadDylib.h"
#include <map>
#if UNITY_OSX || UNITY_LINUX
#include "dlfcn.h"
#endif
#if UNITY_WIN
#include "PlatformDependent/Win/PathUnicodeConversion.h"
#endif
#if UNITY_WINRT
#include "PlatformDependent/MetroPlayer/MetroUtils.h"
#endif
using namespace std;
map<string, void*> gLoaded;
std::string GetPathWithPlatformSpecificDllExtension(const std::string& path)
{
string extended = path;
#if UNITY_OSX
extended += ".dylib";
#elif UNITY_WIN
extended += ".dll";
#elif UNITY_LINUX
extended += ".so";
#else
ErrorString ("Dynamic module loading not implemented");
#endif
return extended;
}
void* LoadDynamicLibrary (const std::string& absolutePath)
{
void* library = NULL;
// load library if needed
if (gLoaded.find(absolutePath) != gLoaded.end())
{
library = gLoaded[absolutePath];
}
else
{
#if UNITY_OSX || UNITY_LINUX
/// RTLD_LOCAL is supposedly default and also completely fails on 10.3.9
library = dlopen (absolutePath.c_str (), RTLD_NOW);
if ( !library )
ErrorStringMsg( "ERROR: LoadDynamicLibrary(): %s\n", dlerror() );
#elif UNITY_WIN
#if UNITY_WINRT
HMODULE module = LoadPackagedLibrary (ConvertToWindowsPath(absolutePath.c_str())->Data(), 0);
#else
std::wstring widePath;
ConvertUnityPathName( absolutePath, widePath );
HINSTANCE module = LoadLibraryW( widePath.c_str() );
#endif
library = module;
#endif
if (library)
{
gLoaded[absolutePath] = library;
}
}
return library;
}
void* LoadAndLookupSymbol (const std::string& absolutePath, const std::string& name)
{
void* library = LoadDynamicLibrary(absolutePath);
void* symbol = LookupSymbol(library, name);
return symbol;
}
void UnloadDynamicLibrary (void* libraryReference)
{
map<string, void*>::iterator it;
for (it = gLoaded.begin(); it != gLoaded.end(); ++it)
{
if (it->second == libraryReference)
{
#if UNITY_OSX || UNITY_LINUX
dlclose (it->second);
#elif UNITY_WIN
FreeLibrary( (HMODULE)it->second );
#endif
gLoaded.erase(it);
break;
}
}
}
void UnloadDynamicLibrary (const std::string& absolutePath)
{
if (gLoaded.count (absolutePath) && gLoaded[absolutePath])
{
#if UNITY_OSX || UNITY_LINUX
dlclose (gLoaded[absolutePath]);
#elif UNITY_WIN
FreeLibrary( (HMODULE)gLoaded[absolutePath] );
#endif
}
gLoaded.erase (absolutePath);
}
bool LoadAndLookupSymbols (const char* path, ...)
{
va_list ap;
va_start (ap, path);
while (true)
{
const char* symbolName = va_arg (ap, const char*);
if (symbolName == NULL)
return true;
void** functionHandle = va_arg (ap, void**);
AssertIf(functionHandle == NULL);
*functionHandle = LoadAndLookupSymbol (path, symbolName);
if (*functionHandle == NULL)
return false;
}
return false;
}
void* LookupSymbol(void* libraryReference, const std::string& symbolName)
{
#if UNITY_OSX || UNITY_LINUX
void *sym = dlsym (libraryReference, symbolName.c_str ());
if (!sym)
ErrorStringMsg("Could not load symbol %s : %s\n", symbolName.c_str(), dlerror());
return sym;
#elif UNITY_WIN
if( !libraryReference )
return NULL;
return GetProcAddress( (HMODULE)libraryReference, symbolName.c_str() );
#else
ErrorStringMsg("LoadAndLookupSymbol is not supported on this platform.\n");
return NULL;
#endif
}
/*
#include <Carbon/Carbon.h>
#import <mach-o/dyld.h>
#import <stdlib.h>
#import <string.h>
#include "FileUtilities.h"
using namespace std;
CFBundleRef LoadDynamicLibrary (const string& absolutePath)
{
CFStringRef cfstring = CFStringCreateWithCString (NULL, ResolveSymlinks(absolutePath).c_str (), kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault,
cfstring,
kCFURLPOSIXPathStyle,
false);
// CFURLRef url = CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault, (const UInt8*)absolutePath.c_str (), absolutePath.size (), false);
CFBundleRef bundle = CFBundleCreate (NULL, url);
CFRelease (url);
CFRelease (cfstring);
if (!bundle)
return false;
// Check if already loaded
if (CFBundleIsExecutableLoaded (bundle))
return bundle;
// Load the bundle
if (!CFBundleLoadExecutable (bundle))
return NULL;
return bundle;
}
void UnloadDynamicLibrary (const string& absolutePath)
{
// CFURLRef url = CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault, (const UInt8*)absolutePath.c_str (), absolutePath.size (), false);
CFStringRef cfstring = CFStringCreateWithCString (kCFAllocatorDefault, absolutePath.c_str (), kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault,
cfstring,
kCFURLPOSIXPathStyle,
true);
CFBundleRef bundle = CFBundleCreate (kCFAllocatorDefault, url);
CFRelease (url);
CFRelease (cfstring);
if (!bundle)
return;
CFBundleUnloadExecutable (bundle);
}
void* LoadAndLookupSymbol (const std::string& absolutePath, const std::string& name)
{
CFBundleRef bundle = LoadDynamicLibrary (absolutePath);
CFStringRef cfstring = CFStringCreateWithCString (kCFAllocatorDefault, name.c_str (), kCFStringEncodingUTF8);
if (bundle == NULL)
return NULL;
return CFBundleGetFunctionPointerForName(bundle, cfstring);
}*/
|