-
-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathModuleLookup.cpp
More file actions
114 lines (94 loc) · 2.27 KB
/
Copy pathModuleLookup.cpp
File metadata and controls
114 lines (94 loc) · 2.27 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "stdafx.h"
#if defined(XR_PLATFORM_LINUX) || defined(XR_PLATFORM_BSD)
#include <dlfcn.h>
#else
#include <SDL3/SDL_loadso.h>
#endif
#include "ModuleLookup.hpp"
namespace XRay
{
ModuleHandle::ModuleHandle(const bool dontUnload) : handle(nullptr), dontUnload(dontUnload) {}
ModuleHandle::ModuleHandle(pcstr moduleName, bool dontUnload /*= false*/) : handle(nullptr), dontUnload(dontUnload)
{
this->Open(moduleName);
}
ModuleHandle::~ModuleHandle()
{
Close();
}
void* ModuleHandle::Open(pcstr moduleName)
{
ZoneScoped;
if (IsLoaded())
Close();
Log("Loading module:", moduleName);
xr_string buf(moduleName);
#ifdef XR_PLATFORM_WINDOWS
buf += ".dll";
#elif defined(XR_PLATFORM_APPLE)
buf += ".dylib";
#elif defined(XR_PLATFORM_POSIX) // assume .so for POSIX platforms
buf += ".so";
#else
#error add your platform-specific extension here
#endif
pcstr error = nullptr;
#if defined(XR_PLATFORM_LINUX) || defined(XR_PLATFORM_BSD)
// For platforms that use rpath we have to call dlopen() from our own module
handle = dlopen(buf.c_str(), RTLD_NOW);
if (!handle)
error = dlerror();
#else
handle = SDL_LoadObject(buf.c_str());
if (!handle)
error = SDL_GetError();
#endif
if (!handle)
{
Log("! Failed to load module:", moduleName);
if (error)
Log("!", error);
}
return handle;
}
void ModuleHandle::Close()
{
ZoneScoped;
if (dontUnload || !handle)
return;
#if defined(XR_PLATFORM_LINUX) || defined(XR_PLATFORM_BSD)
dlclose(handle);
#else
SDL_UnloadObject(handle);
#endif
handle = nullptr;
}
bool ModuleHandle::IsLoaded() const
{
return handle != nullptr;
}
void* ModuleHandle::operator()() const
{
return handle;
}
void* ModuleHandle::GetProcAddress(pcstr procName) const
{
pcstr error = nullptr;
#if defined(XR_PLATFORM_LINUX) || defined(XR_PLATFORM_BSD)
const auto proc = dlsym(handle, procName);
if (!proc)
error = dlerror();
#else
const auto proc = SDL_LoadFunction(handle, procName);
if (!proc)
error = SDL_GetError();
#endif
if (!proc)
{
Log("! Failed to load function from module:", procName);
if (error)
Log("!", error);
}
return proc;
}
} // namespace XRay