-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathmemutils.cpp
More file actions
70 lines (61 loc) · 1.63 KB
/
Copy pathmemutils.cpp
File metadata and controls
70 lines (61 loc) · 1.63 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
#include <string>
#include "LuaTools.h"
#include "LuaWrapper.h"
using namespace DFHack;
using namespace DFHack::LuaWrapper;
using namespace std;
namespace memutils {
static lua_State *state;
static color_ostream_proxy *out;
struct initializer {
Lua::StackUnwinder *unwinder;
initializer()
{
if (!out)
out = new color_ostream_proxy(Core::getInstance().getConsole());
if (!state)
state = Lua::Open(*out);
unwinder = new Lua::StackUnwinder(state);
}
~initializer()
{
delete unwinder;
}
};
struct cleaner {
~cleaner()
{
if (state)
{
lua_close(state);
state = NULL;
}
if (out)
{
delete out;
out = NULL;
}
}
};
static cleaner g_cleaner;
void *lua_expr_to_addr(const char *expr)
{
initializer init;
Lua::PushModulePublic(*out, state, "utils", "df_expr_to_ref");
lua_pushstring(state, expr);
if (!Lua::SafeCall(*out, state, 1, 1))
{
out->printerr("Failed to evaluate {}\n", expr);
return NULL;
}
Lua::PushModulePublic(*out, state, "utils", "addressof");
lua_swap(state);
if (!Lua::SafeCall(*out, state, 1, 1) || !lua_isinteger(state, -1))
{
out->printerr("Failed to get address: {}\n", expr);
return NULL;
}
auto addr = uintptr_t(lua_tointeger(state, -1));
return (void*)addr;
}
}