summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/environment.cpp
blob: 24dea9ec5855aee89bf508e6a5f1e4c56ff0801f (plain)
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
#include "stdafx.h"

#define MT_ENVIRONMENT "ENVIRONMENT"

extern PLUGININFOEX pluginInfoEx;

EXTERN_C MIR_APP_DLL(void) RegisterPlugin(CMPluginBase *pPlugin);

CMLuaEnvironment::CMLuaEnvironment(lua_State *L) :
	CMPluginBase(nullptr, pluginInfoEx), L(L)
{
	m_hInst = (HINSTANCE)this;
	::RegisterPlugin(this);
}

int CMLuaEnvironment::Unload()
{
	KillModuleIcons(this);
	KillModuleSounds(this);
	KillModuleMenus(this);
	KillModuleHotkeys(this);

	KillObjectEventHooks(this);
	KillObjectServices(this);

	for (auto &it : m_hookRefs)
		luaL_unref(L, LUA_REGISTRYINDEX, it.second);

	for (auto &it : m_serviceRefs)
		luaL_unref(L, LUA_REGISTRYINDEX, it.second);

	return 0;
}

CMLuaEnvironment* CMLuaEnvironment::GetEnvironment(lua_State *L)
{
	if (!luaM_getenv(L))
		return nullptr;

	lua_rawgeti(L, -1, NULL);
	CMLuaEnvironment *env = (CMLuaEnvironment*)lua_touserdata(L, -1);
	lua_pop(L, 3);

	return env;
}

HPLUGIN CMLuaEnvironment::GetEnvironmentId(lua_State *L)
{
	CMLuaEnvironment *env = GetEnvironment(L);
	return env != nullptr ? HPLUGIN(env) : &g_plugin;
}

static int HookEventEnvParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
	CMLuaEnvironment *env = (CMLuaEnvironment*)obj;

	int ref = param;
	lua_rawgeti(env->L, LUA_REGISTRYINDEX, ref);

	if (wParam)
		lua_pushlightuserdata(env->L, (void*)wParam);
	else
		lua_pushnil(env->L);

	if (lParam)
		lua_pushlightuserdata(env->L, (void*)lParam);
	else
		lua_pushnil(env->L);

	luaM_pcall(env->L, 2, 1);

	return lua_tointeger(env->L, -1);
}

HANDLE CMLuaEnvironment::HookEvent(const char *name, int ref)
{
	HANDLE hHook = HookEventObjParam(name, HookEventEnvParam, this, ref);
	if (hHook)
		m_hookRefs[hHook] = ref;
	return hHook;
}

int CMLuaEnvironment::UnhookEvent(HANDLE hEvent)
{
	int res = ::UnhookEvent(hEvent);
	if (res) {
		auto it = m_hookRefs.find(hEvent);
		if (it != m_hookRefs.end())
			luaL_unref(L, LUA_REGISTRYINDEX, it->second);
	}
	return res;
}

static INT_PTR CreateServiceFunctionEnvParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
	CMLuaEnvironment *env = (CMLuaEnvironment*)obj;

	int ref = param;
	lua_rawgeti(env->L, LUA_REGISTRYINDEX, ref);

	lua_pushlightuserdata(env->L, (void*)wParam);
	lua_pushlightuserdata(env->L, (void*)lParam);
	luaM_pcall(env->L, 2, 1);

	INT_PTR res = lua_tointeger(env->L, 1);
	lua_pushinteger(env->L, res);

	return res;
}

HANDLE CMLuaEnvironment::CreateServiceFunction(const char *name, int ref)
{
	HANDLE hService = CreateServiceFunctionObjParam(name, CreateServiceFunctionEnvParam, this, ref);
	if (hService)
		m_serviceRefs[hService] = ref;
	return hService;
}

void CMLuaEnvironment::DestroyServiceFunction(HANDLE hService)
{
	auto it = m_serviceRefs.find(hService);
	if (it != m_serviceRefs.end())
		luaL_unref(L, LUA_REGISTRYINDEX, it->second);
	::DestroyServiceFunction(hService);
}

void CMLuaEnvironment::CreateEnvironmentTable()
{
	if (!lua_isfunction(L, -1))
		return;

	lua_createtable(L, 1, 1);
	lua_pushlightuserdata(L, this);
	lua_rawseti(L, -2, NULL);
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "_G");
	lua_createtable(L, 0, 2);
	lua_pushliteral(L, MT_ENVIRONMENT);
	lua_setfield(L, -2, "__metatable");
	lua_getglobal(L, "_G");
	lua_setfield(L, -2, "__index");
	lua_setmetatable(L, -2);

	lua_setupvalue(L, -2, 1);
}

/***********************************************/

wchar_t* CMLuaEnvironment::Error()
{
	const char *message = lua_tostring(L, -1);
	wchar_t *error = mir_utf8decodeW(message);
	lua_pop(L, 1);
	return error;
}

int CMLuaEnvironment::Call()
{
	CreateEnvironmentTable();
	return lua_pcall(L, 0, 1, 0);
}

int CMLuaEnvironment::Eval(const wchar_t *script)
{
	if (int res = luaL_loadstring(L, T2Utf(script)))
		return res;
	return Call();
}

int CMLuaEnvironment::Exec(const wchar_t *path)
{
	if (int res = luaL_loadfile(L, T2Utf(path)))
		return res;
	return Call();
}