summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/m_core.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/MirLua/src/m_core.cpp')
-rw-r--r--plugins/MirLua/src/m_core.cpp94
1 files changed, 84 insertions, 10 deletions
diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp
index 0d2d07eb4d..4c2b6e7a38 100644
--- a/plugins/MirLua/src/m_core.cpp
+++ b/plugins/MirLua/src/m_core.cpp
@@ -16,9 +16,9 @@ static int core_CreateHookableEvent(lua_State *L)
return 1;
}
-int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
+int HookEventLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
- lua_State *L = lua_newthread(*(CMLua*)obj);
+ lua_State *L = (lua_State*)obj;
int ref = param;
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
@@ -38,6 +38,28 @@ int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
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);
@@ -46,15 +68,24 @@ static int core_HookEvent(lua_State *L)
lua_pushvalue(L, 2);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
- HANDLE hEvent = HookEventObjParam(name, HookEventObjParam, g_mLua, ref);
- if (hEvent == NULL)
+ 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;
}
- lua_pushlightuserdata(L, hEvent);
+
+ CMLua::HookRefs.insert(new HandleRefParam(L, res, ref));
+
+ lua_pushlightuserdata(L, res);
+
return 1;
}
@@ -64,6 +95,16 @@ static int core_UnhookEvent(lua_State *L)
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;
@@ -95,9 +136,9 @@ static int core_DestroyHookableEvent(lua_State *L)
/***********************************************/
-INT_PTR CreateServiceFunctionObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
+INT_PTR CreateServiceFunctionLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
- lua_State *L = lua_newthread(*(CMLua*)obj);
+ lua_State *L = (lua_State*)obj;
int ref = param;
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
@@ -112,6 +153,23 @@ INT_PTR CreateServiceFunctionObjParam(void *obj, WPARAM wParam, LPARAM lParam, L
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);
@@ -120,14 +178,22 @@ static int core_CreateServiceFunction(lua_State *L)
lua_pushvalue(L, 2);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
- HANDLE hService = CreateServiceFunctionObjParam(name, CreateServiceFunctionObjParam, g_mLua, ref);
- if (!hService)
+ 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;
}
- lua_pushlightuserdata(L, hService);
+
+ CMLua::ServiceRefs.insert(new HandleRefParam(L, res, ref));
+
+ lua_pushlightuserdata(L, res);
return 1;
}
@@ -159,6 +225,14 @@ 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;