forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugLoad.cpp
More file actions
99 lines (91 loc) · 2.72 KB
/
PlugLoad.cpp
File metadata and controls
99 lines (91 loc) · 2.72 KB
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
#include "Core.h"
#include "Debug.h"
#include "Export.h"
#include "PluginManager.h"
#include "Hooks.h"
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdint.h>
#ifdef WIN32
#define NOMINMAX
#include <windows.h>
#define global_search_handle() GetModuleHandle(nullptr)
#define get_function_address(plugin, function) GetProcAddress((HMODULE)plugin, function)
#define clear_error()
#define load_library(fn) LoadLibraryW(fn.c_str())
#define close_library(handle) (!(FreeLibrary((HMODULE)handle)))
#else
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define global_search_handle() (RTLD_DEFAULT)
#define get_function_address(plugin, function) dlsym((void*)plugin, function)
#define clear_error() dlerror()
#define load_library(fn) dlopen(fn.c_str(), RTLD_NOW | RTLD_LOCAL);
#define close_library(handle) dlclose((void*)handle)
#endif
/*
* Plugin loading functions
*/
namespace DFHack
{
DBG_DECLARE(core, plugins, DebugCategory::LINFO);
DFHack::DFLibrary* GLOBAL_NAMES = (DFLibrary*)global_search_handle();
namespace {
std::string get_error()
{
#ifdef WIN32
DWORD error = GetLastError();
LPSTR buf = NULL;
DWORD len = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL);
if (len > 0) {
std::string message{ buf };
LocalFree(buf);
return message;
}
std::ostringstream b{};
b << "unknown error " << error;
return b.str();
#else
char* error = dlerror();
if (error)
return std::string{ error };
return std::string{};
#endif
}
}
DFLibrary * OpenPlugin (std::filesystem::path filename)
{
clear_error();
DFLibrary* ret = (DFLibrary*)load_library(filename);
if (!ret) {
auto error = get_error();
WARN(plugins).print("OpenPlugin on %s failed: %s\n", filename.string().c_str(), error.c_str());
}
return ret;
}
void * LookupPlugin (DFLibrary * plugin ,const char * function)
{
return (void *) get_function_address(plugin, function);
}
bool ClosePlugin (DFLibrary * plugin)
{
int res = close_library(plugin);
if (res != 0)
{
auto error = get_error();
WARN(plugins).print("ClosePlugin failed: %s\n", error.c_str());
}
return (res == 0);
}
}