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
|
#include <lua-plugin.h>
#include <Input.h>
#if defined(_WIN32)
#define SHARED_EXT "dll"
#elif defined(__APPLE__)
#define SHARED_EXT "dylib"
#else
#define SHARED_EXT "so"
#endif
typedef void(*init_ptr_t)(Lua *);
#if defined(_WIN32)
#include <windows.h>
#include "MemoryModule.h"
void LuaLoadPlugin(const String & _fname, Lua * L) throw (GeneralException) {
HMODULE handle;
String fname = _fname + "." SHARED_EXT;
Base::printm(M_INFO, "Loading library " + fname + "\n");
if (!(handle = LoadLibraryEx(fname.to_charp(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH)) &&
!(handle = LoadLibraryEx(fname.to_charp(), NULL, NULL))) {
LuaLoadPlugin(&Input(fname), L);
return;
}
init_ptr_t init_ptr = (init_ptr_t) GetProcAddress(handle, "init_plugin");
if (!init_ptr) {
FreeLibrary(handle);
throw GeneralException("No init pointer on plugin " + fname);
}
Base::printm(M_INFO, "Library loaded, init ptr = %p\n", init_ptr);
init_ptr(L);
}
void LuaLoadPlugin(Handle * h, Lua * L) throw (GeneralException) {
Byte * buffer;
buffer = (Byte *) malloc(h->GetSize());
h->read(buffer, h->GetSize());
HMEMORYMODULE module;
if (!(module = MemoryLoadLibrary(buffer)))
throw("Can't load library " + h->GetName() + " from memory.");
init_ptr_t init_ptr = (init_ptr_t) MemoryGetProcAddress(module, "init_plugin");
if (!init_ptr) {
MemoryFreeLibrary(module);
throw GeneralException("No init pointer on plugin " + h->GetName());
}
Base::printm(M_INFO, "Library loaded, init ptr = %p\n", init_ptr);
init_ptr(L);
free(buffer);
}
#else
#include <dlfcn.h>
void LuaLoadPlugin(const String & fname, Lua * L) throw (GeneralException) {
String full_fname = "./" + fname + "." SHARED_EXT;
void * handle = dlopen(full_fname.to_charp(), RTLD_NOW | RTLD_GLOBAL);
Base::printm(M_INFO, "Loading library " + fname + "\n");
if (!handle) {
LuaLoadPlugin(&Input(fname + "." SHARED_EXT), L);
return;
}
init_ptr_t init_ptr = (init_ptr_t) dlsym(handle, "init_plugin");
if (!init_ptr) {
dlclose(handle);
throw GeneralException("No init pointer on plugin " + fname);
}
Base::printm(M_INFO, "Library loaded, init ptr = %p\n", init_ptr);
init_ptr(L);
}
#if defined(__APPLE__)
#include <mach/vm_map.h>
#include <mach-o/dyld.h>
#include <mach/mach_init.h>
void LuaLoadPlugin(Handle * h, Lua * L) throw (GeneralException) {
Byte * buffer;
NSObjectFileImage image;
kern_return_t result = vm_allocate(mach_task_self(), (vm_address_t *) &buffer, h->GetSize(), VM_FLAGS_ANYWHERE);
h->read(buffer, h->GetSize());
if (NSCreateObjectFileImageFromMemory(buffer, h->GetSize(), &image) != NSObjectFileImageSuccess)
throw("Can't load library " + h->GetName() + " from memory.");
NSModule module;
if (!(module = NSLinkModule(image, h->GetName().to_charp(), NSLINKMODULE_OPTION_PRIVATE)))
throw("Can't link library " + h->GetName() + " from memory.");
NSSymbol symbol = NSLookupSymbolInModule(module, "init_plugin");
init_ptr_t init_ptr = (init_ptr_t) NSAddressOfSymbol(symbol);
if (!init_ptr) {
NSDestroyObjectFileImage(image);
throw GeneralException("No init pointer on plugin " + h->GetName());
}
Base::printm(M_INFO, "Library loaded, init ptr = %p\n", init_ptr);
init_ptr(L);
}
#else
#include <unistd.h>
// I don't know of any good way of doing this under linux except this one... *shrug*
void LuaLoadPlugin(Handle * h, Lua * L) throw (GeneralException) {
char ftemplate[] = "/tmp/luaplugin.so.XXXXXX";
int hdl;
if ((hdl = mkstemp(ftemplate)) == -1)
throw GeneralException("Can't create temporary file to load plugin " + h->GetName());
Byte * buffer;
buffer = (Byte *) malloc(h->GetSize());
h->read(buffer, h->GetSize());
if (write(hdl, buffer, h->GetSize()) != h->GetSize()) {
free(buffer);
close(hdl);
unlink(ftemplate);
throw GeneralException("Couldn't write to temporary file.");
}
free(buffer);
void * handle = dlopen(ftemplate, RTLD_NOW | RTLD_GLOBAL);
unlink(ftemplate);
close(hdl);
Base::printm(M_INFO, "Loading library " + h->GetName() + "\n");
if (!handle)
throw("Can't load library " + h->GetName() + " from memory.");
init_ptr_t init_ptr = (init_ptr_t) dlsym(handle, "init_plugin");
if (!init_ptr) {
dlclose(handle);
throw GeneralException("No init pointer on plugin " + h->GetName());
}
Base::printm(M_INFO, "Library loaded, init ptr = %p\n", init_ptr);
init_ptr(L);
}
#endif
#endif
|