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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
#include "stdafx.h"
static int core_CreateHookableEvent(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
HANDLE res = CreateHookableEvent(name);
if (res == NULL)
{
lua_pushnil(L);
return 1;
}
lua_pushlightuserdata(L, res);
return 1;
}
int HookEventLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
lua_State *L = (lua_State*)obj;
int ref = param;
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
if (wParam)
lua_pushlightuserdata(L, (void*)wParam);
else
lua_pushnil(L);
if (lParam)
lua_pushlightuserdata(L, (void*)lParam);
else
lua_pushnil(L);
luaM_pcall(L, 2, 1);
return lua_tointeger(L, -1);
}
int HookEventScriptParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
CMLuaScript *script = (CMLuaScript*)obj;
int ref = param;
lua_rawgeti(script->L, LUA_REGISTRYINDEX, ref);
if (wParam)
lua_pushlightuserdata(script->L, (void*)wParam);
else
lua_pushnil(script->L);
if (lParam)
lua_pushlightuserdata(script->L, (void*)lParam);
else
lua_pushnil(script->L);
luaM_pcall(script->L, 2, 1);
return lua_tointeger(script->L, -1);
}
static int core_HookEvent(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
HANDLE res = NULL;
CMLuaScript *script = CMLuaScript::GetScriptFromEnviroment(L);
if (script)
res = HookEventObjParam(name, HookEventScriptParam, script, ref);
else
res = HookEventObjParam(name, HookEventLuaStateParam, L, ref);
if (res == NULL)
{
luaL_unref(L, LUA_REGISTRYINDEX, ref);
lua_pushnil(L);
return 1;
}
CMLua::HookRefs.insert(new HandleRefParam(L, res, ref));
lua_pushlightuserdata(L, res);
return 1;
}
static int core_UnhookEvent(lua_State *L)
{
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
HANDLE hEvent = lua_touserdata(L, 1);
int res = UnhookEvent(hEvent);
if (!res)
{
HandleRefParam *param = (HandleRefParam*)CMLua::HookRefs.find(hEvent);
if (param != NULL)
{
luaL_unref(param->L, LUA_REGISTRYINDEX, param->ref);
CMLua::HookRefs.remove(param);
delete param;
}
}
lua_pushboolean(L, !res);
return 1;
}
static int core_NotifyEventHooks(lua_State *L)
{
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
HANDLE hEvent = lua_touserdata(L, 1);
WPARAM wParam = (WPARAM)luaM_tomparam(L, 2);
LPARAM lParam = (LPARAM)luaM_tomparam(L, 3);
int res = NotifyEventHooks(hEvent, wParam, lParam);
lua_pushboolean(L, res != -1);
return 1;
}
static int core_DestroyHookableEvent(lua_State *L)
{
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
HANDLE hEvent = lua_touserdata(L, 1);
int res = DestroyHookableEvent(hEvent);
lua_pushboolean(L, !res);
return 1;
}
/***********************************************/
INT_PTR CreateServiceFunctionLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
lua_State *L = (lua_State*)obj;
int ref = param;
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
lua_pushlightuserdata(L, (void*)wParam);
lua_pushlightuserdata(L, (void*)lParam);
luaM_pcall(L, 2, 1);
INT_PTR res = lua_tointeger(L, 1);
lua_pushinteger(L, res);
return res;
}
INT_PTR CreateServiceFunctionScriptParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
CMLuaScript *script = (CMLuaScript*)obj;
int ref = param;
lua_rawgeti(script->L, LUA_REGISTRYINDEX, ref);
lua_pushlightuserdata(script->L, (void*)wParam);
lua_pushlightuserdata(script->L, (void*)lParam);
luaM_pcall(script->L, 2, 1);
INT_PTR res = lua_tointeger(script->L, 1);
lua_pushinteger(script->L, res);
return res;
}
static int core_CreateServiceFunction(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
HANDLE res = NULL;
CMLuaScript *script = CMLuaScript::GetScriptFromEnviroment(L);
if (script)
res = CreateServiceFunctionObjParam(name, CreateServiceFunctionScriptParam, script, ref);
else
res = CreateServiceFunctionObjParam(name, CreateServiceFunctionLuaStateParam, L, ref);
if (!res)
{
luaL_unref(L, LUA_REGISTRYINDEX, ref);
lua_pushnil(L);
return 1;
}
CMLua::ServiceRefs.insert(new HandleRefParam(L, res, ref));
lua_pushlightuserdata(L, res);
return 1;
}
static int core_CallService(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
WPARAM wParam = (WPARAM)luaM_tomparam(L, 2);
LPARAM lParam = (LPARAM)luaM_tomparam(L, 3);
INT_PTR res = CallService(name, wParam, lParam);
lua_pushinteger(L, res);
return 1;
}
static int core_ServiceExists(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
int res = ServiceExists(name);
lua_pushboolean(L, res);
return 1;
}
static int core_DestroyServiceFunction(lua_State *L)
{
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
HANDLE hService = lua_touserdata(L, 1);
HandleRefParam *param = (HandleRefParam*)CMLua::ServiceRefs.find(hService);
if (param != NULL)
{
luaL_unref(param->L, LUA_REGISTRYINDEX, param->ref);
CMLua::ServiceRefs.remove(param);
delete param;
}
DestroyServiceFunction(hService);
return 0;
}
/***********************************************/
static int core_IsPluginLoaded(lua_State *L)
{
const char *value = lua_tostring(L, 1);
MUUID uuid = { 0 };
bool res = UuidFromStringA((RPC_CSTR)value, (UUID*)&uuid) == RPC_S_OK;
if (res)
res = IsPluginLoaded(uuid) > 0;
lua_pushboolean(L, res);
return 1;
}
static int core_Utf8DecodeA(lua_State *L)
{
return luaM_toansi(L);
}
static int core_Utf8DecodeW(lua_State *L)
{
return luaM_toucs2(L);
}
static int core_Free(lua_State *L)
{
if (lua_islightuserdata(L, 1))
{
void *ptr = lua_touserdata(L, 1);
mir_free(ptr);
}
return 0;
}
static int core_Translate(lua_State *L)
{
char *what = (char*)luaL_checkstring(L, 1);
ptrT value(mir_utf8decodeT(what));
lua_pushstring(L, T2Utf(TranslateW_LP(value, hLangpack)));
return 1;
}
static int core_Parse(lua_State *L)
{
char *what = (char*)luaL_checkstring(L, 1);
ptrT value(mir_utf8decodeT(what));
lua_pushstring(L, T2Utf(VARST(value)));
return 1;
}
static int core_GetFullPath(lua_State *L)
{
TCHAR path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
lua_pushstring(L, ptrA(mir_utf8encodeT(path)));
return 1;
}
struct core_ForkThreadParam
{
lua_State *L;
int threadRef;
int functionRef;
HANDLE hThread;
};
std::map<HANDLE, core_ForkThreadParam*> lstThreads;
void DestroyThread(core_ForkThreadParam *ftp)
{
luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef);
luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->threadRef);
lstThreads.erase(ftp->hThread);
delete ftp;
}
void __cdecl ThreadFunc(void *p)
{
core_ForkThreadParam *ftp = (core_ForkThreadParam*)p;
lua_rawgeti(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef);
luaM_pcall(ftp->L, 0, 1);
DestroyThread(ftp);
}
static int core_ForkThread(lua_State *L)
{
core_ForkThreadParam *p = new core_ForkThreadParam();
p->L = lua_newthread(L);
p->threadRef = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushvalue(L, 1);
p->functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
p->hThread = mir_forkthread(ThreadFunc, p);
lstThreads[p->hThread] = p;
lua_pushlightuserdata(L, p->hThread);
return 1;
}
static int core_TerminateThread(lua_State *L)
{
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
HANDLE hThread = (HANDLE)lua_touserdata(L, 1);
auto it = lstThreads.find(hThread);
if (it != lstThreads.end())
{
DestroyThread(it->second);
lua_pushboolean(L, TerminateThread(hThread, 0));
}
else lua_pushboolean(L, 0);
return 1;
}
int core_ptr2number(lua_State *L)
{
ObsoleteMethod(L, "Use tonumber(x) instead");
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
lua_pushnumber(L, (intptr_t)lua_touserdata(L, 1));
return 1;
}
luaL_Reg coreApi[] =
{
{ "CreateHookableEvent", core_CreateHookableEvent },
//{ "DestroyHookableEvent", core_DestroyHookableEvent },
{ "NotifyEventHooks", core_NotifyEventHooks },
{ "HookEvent", core_HookEvent },
{ "UnhookEvent", core_UnhookEvent },
{ "CreateServiceFunction", core_CreateServiceFunction },
{ "DestroyServiceFunction", core_DestroyServiceFunction },
{ "ServiceExists", core_ServiceExists },
{ "CallService", core_CallService },
{ "IsPluginLoaded", core_IsPluginLoaded },
{ "Utf8DecodeA", core_Utf8DecodeA },
{ "Utf8DecodeW", core_Utf8DecodeW },
{ "Free", core_Free },
{ "Translate", core_Translate },
{ "Parse", core_Parse },
{ "ReplaceVariables", core_Parse },
{ "ForkThread", core_ForkThread },
{ "TerminateThread", core_TerminateThread },
{ "PointerToNumber", core_ptr2number },
{ "GetFullPath", core_GetFullPath },
{ "Version", NULL },
{ "NULL", NULL },
{ "INVALID_HANDLE_VALUE", NULL },
{ "CALLSERVICE_NOTFOUND", NULL },
{ NULL, NULL }
};
/***********************************************/
LUAMOD_API int luaopen_m_core(lua_State *L)
{
luaL_newlib(L, coreApi);
lua_pushlightuserdata(L, NULL);
lua_setfield(L, -2, "NULL");
lua_pushlightuserdata(L, INVALID_HANDLE_VALUE);
lua_setfield(L, -2, "INVALID_HANDLE_VALUE");
lua_pushinteger(L, CALLSERVICE_NOTFOUND);
lua_setfield(L, -2, "CALLSERVICE_NOTFOUND");
char version[128];
CallService(MS_SYSTEM_GETVERSIONTEXT, (WPARAM)_countof(version), (LPARAM)version);
lua_pushstring(L, ptrA(mir_utf8encode(version)));
lua_setfield(L, -2, "Version");
// set copy to global variable m
lua_pushvalue(L, -1);
lua_setglobal(L, "m");
return 1;
}
|