summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/mlua.cpp
blob: 5945947441efa061bd74b2c388054a5a3827f254 (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
#include "stdafx.h"

CMLua::CMLua() : L(NULL)
{
	Load();
}

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

void CMLua::Load()
{
	L = luaL_newstate();
	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);

	LoadMirandaModules();
}

void CMLua::Unload()
{
	if (L)
		lua_close(L);
}

void CMLua::Reload()
{
	/*lua_getglobal(L, "m");
	lua_getfield(L, -1, "OnPreShutdown");
	if (lua_isfunction(L, -1))
	{
		lua_pushlightuserdata(L, NULL);
		lua_pushlightuserdata(L, NULL);
		if (lua_pcall(L, 2, 1, 0))
			printf("%s\n", lua_tostring(L, -1));
	}
	lua_pop(L, 1);*/
	Unload();

	Load();
	/*lua_getglobal(L, "m");
	lua_getfield(L, -1, "OnModulesLoaded");
	if (lua_isfunction(L, -1))
	{
		lua_pushlightuserdata(L, NULL);
		lua_pushlightuserdata(L, NULL);
		if (lua_pcall(L, 2, 1, 0))
			printf("%s\n", lua_tostring(L, -1));
	}
	lua_pop(L, 1);*/
}

void CMLua::LoadModule(const char *name, lua_CFunction loader)
{
	luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
	lua_pushcfunction(L, loader);
	lua_setfield(L, -2, name);
	lua_pop(L, 1);
}

void CMLua::LoadCoreModule()
{
	luaL_newlib(L, coreLib);
	lua_pushlightuserdata(L, NULL);
	lua_setfield(L, -2, "NULL");
	lua_setglobal(L, "m");
}

void CMLua::LoadMirandaModules()
{
	LoadCoreModule();

	LoadModule(MLUA_DATABASE, luaopen_m_database);
	LoadModule(MLUA_ICOLIB, luaopen_m_icolib);
	LoadModule(MLUA_GENMENU, luaopen_m_genmenu);
	LoadModule(MLUA_MSGBUTTONSBAR, luaopen_m_msg_buttonsbar);
	LoadModule(MLUA_POPUP, luaopen_m_popup);
	LoadModule(MLUA_TOPTOOLBAR, luaopen_m_toptoolbar);
	LoadModule(MLUA_VARIABLES, luaopen_m_variables);
}

void CMLua::AddScriptsPath(const char *path)
{
	lua_getglobal(L, "package");
	lua_getfield(L, -1, "path");
	const char *oldPath = luaL_checkstring(L, -1);
	lua_pop(L, 1);
	lua_pushfstring(L, "%s;%s\\?.lua", oldPath, path);
	lua_setfield(L, -2, "path");
	lua_pop(L, 1);
}

void CMLua::LoadScript(const char *path)
{
	if (luaL_dofile(L, path))
		printf("%s\n", lua_tostring(L, -1));
}

WPARAM CMLua::GetWParam(lua_State *L, int idx)
{
	WPARAM wParam = NULL;
	switch (lua_type(L, idx))
	{
	case LUA_TBOOLEAN:
		wParam = lua_toboolean(L, idx);
		break;
	case LUA_TNUMBER:
		wParam = lua_tonumber(L, idx);
		break;
	case LUA_TSTRING:
		wParam = (LPARAM)mir_utf8decode((char*)lua_tostring(L, idx), NULL);
		break;
	case LUA_TUSERDATA:
		wParam = (WPARAM)lua_touserdata(L, idx);
		break;
	}
	return wParam;
}

LPARAM CMLua::GetLParam(lua_State *L, int idx)
{
	LPARAM lParam = NULL;
	switch (lua_type(L, idx))
	{
	case LUA_TBOOLEAN:
		lParam = lua_toboolean(L, idx);
		break;
	case LUA_TNUMBER:
		lParam = lua_tonumber(L, idx);
		break;
	case LUA_TSTRING:
		lParam = (LPARAM)mir_utf8decode((char*)lua_tostring(L, idx), NULL);
		break;
	case LUA_TUSERDATA:
		lParam = (LPARAM)lua_touserdata(L, idx);
		break;
	}
	return lParam;
}

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))
		printf("%s\n", lua_tostring(L, -1));

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

	//luaL_unref(L, LUA_REGISTRYINDEX, ref);

	return res;
}