summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/mlua.cpp
blob: 61405de9287e17f06183cde990afc82a7ceee9e9 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "stdafx.h"

LIST<void> CMLua::Hooks(1, PtrKeySortT);
LIST<void> CMLua::Events(1, PtrKeySortT);
LIST<void> CMLua::Services(1, PtrKeySortT);
LIST<void> CMLua::HookRefs(1, HandleKeySortT);
LIST<void> CMLua::ServiceRefs(1, HandleKeySortT);

CMLua::CMLua() : L(NULL)
{
	hLoadedEvent = CreateHookableEvent("Lua/Script/Loaded");
	hUnloadEvent = CreateHookableEvent("Lua/Script/Unload");

	Load();
}

CMLua::~CMLua()
{
	Unload();
}

void CMLua::Load()
{
	CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)"Loading lua engine");
	L = luaL_newstate();
	CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)"Loading std modules");
	luaL_openlibs(L);

	lua_getglobal(L, "package");
	lua_pushstring(L, "");
	lua_setfield(L, -2, "path");
	lua_pushstring(L, "");
	lua_setfield(L, -2, "cpath");
	lua_pop(L, 1);

	lua_getglobal(L, "_G");
	lua_pushcclosure(L, luaM_print, 0);
	lua_setfield(L, -2, "print");
	lua_pushcclosure(L, luaM_toansi, 0);
	lua_setfield(L, -2, "a");
	lua_pushcclosure(L, luaM_toucs2, 0);
	lua_setfield(L, -2, "u");
	lua_pop(L, 1);

	lua_atpanic(L, luaM_atpanic);

	MUUID muidLast = MIID_LAST;
	hScriptsLangpack = GetPluginLangId(muidLast, 0);

	CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)"Loading miranda modules");
	CLuaModuleLoader::Load(L);
	CLuaScriptLoader::Load(L);

	NotifyEventHooks(hLoadedEvent);
}

void CMLua::Unload()
{
	CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)"Unloading lua engine");

	NotifyEventHooks(hUnloadEvent);
	
	::KillModuleIcons(hScriptsLangpack);
	::KillModuleMenus(hScriptsLangpack);
	CMLua::KillModuleServices();
	CMLua::KillModuleEventHooks();
	//KillModuleSubclassing

	if (L)
		lua_close(L);
}

void CMLua::Reload()
{
	Unload();
	Load();
}

void CMLua::Reload(const TCHAR* path)
{
	CLuaScriptLoader::Reload(g_mLua->L, path);
}

void CMLua::KillModuleEventHooks()
{
	while (Hooks.getCount())
	{
		HANDLE hHook = Hooks[0];
		Hooks.remove(0);
		UnhookEvent(hHook);
	}

	while (Events.getCount())
	{
		HANDLE hEvent = Events[0];
		Events.remove(hEvent);
		DestroyHookableEvent(hEvent);
	}

	while (HookRefs.getCount())
	{
		HandleRefParam *param = (HandleRefParam*)HookRefs[0];
		if (param != NULL)
		{
			luaL_unref(param->L, LUA_REGISTRYINDEX, param->ref);
			HookRefs.remove(0);
			delete param;
		}
	}
}

void CMLua::KillModuleServices()
{
	while (Services.getCount())
	{
		HANDLE hService = Services[0];
		Services.remove(0);
		DestroyServiceFunction(hService);
	}

	while (ServiceRefs.getCount())
	{
		HandleRefParam *param = (HandleRefParam*)ServiceRefs[0];
		if (param != NULL)
		{
			luaL_unref(param->L, LUA_REGISTRYINDEX, param->ref);
			ServiceRefs.remove(0);
			delete param;
		}
	}
}

int CMLua::OnScriptLoaded(lua_State *L)
{
	if (!lua_isfunction(L, 1))
	{
		lua_pushlightuserdata(L, NULL);
		return 1;
	}

	lua_pushvalue(L, 1);
	int ref = luaL_ref(L, LUA_REGISTRYINDEX);

	HANDLE res = ::HookEventObjParam("Lua/Script/Loaded", CMLua::HookEventObjParam, L, ref);
	lua_pushlightuserdata(L, res);

	Hooks.insert(res);
	HookRefs.insert(new HandleRefParam(L, res, ref));

	return 1;
}

int CMLua::OnScriptUnload(lua_State *L)
{
	if (!lua_isfunction(L, 1))
	{
		lua_pushlightuserdata(L, NULL);
		return 1;
	}

	lua_pushvalue(L, 1);
	int ref = luaL_ref(L, LUA_REGISTRYINDEX);

	HANDLE res = ::HookEventObjParam("Lua/Script/Unload", CMLua::HookEventObjParam, L, ref);
	lua_pushlightuserdata(L, res);

	Hooks.insert(res);
	HookRefs.insert(new HandleRefParam(L, res, ref));

	return 1;
}

int CMLua::HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
	lua_State *L = (lua_State*)obj;

	int ref = param;
	lua_rawgeti(L, LUA_REGISTRYINDEX, ref);

	lua_pushnumber(L, wParam);
	lua_pushnumber(L, lParam);
	if (lua_pcall(L, 2, 1, 0))
		CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));

	int res = (int)lua_tointeger(L, 1);

	return res;
}