summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2018-05-20 21:24:22 +0300
committeraunsane <aunsane@gmail.com>2018-05-20 21:35:25 +0300
commit3aaa121979662b5127f54a244a328a161be281ea (patch)
tree63c8c3d6200348a7328b0a1aef6a2553bd3bdc0e /plugins/MirLua/src
parenta955d18f62f335f84e0926f351eb85d78224cba6 (diff)
MirLua: refactoring
Diffstat (limited to 'plugins/MirLua/src')
-rw-r--r--plugins/MirLua/src/Modules/m_core.cpp96
-rw-r--r--plugins/MirLua/src/environment.cpp69
-rw-r--r--plugins/MirLua/src/environment.h20
3 files changed, 83 insertions, 102 deletions
diff --git a/plugins/MirLua/src/Modules/m_core.cpp b/plugins/MirLua/src/Modules/m_core.cpp
index 027ff163ed..747957dbcc 100644
--- a/plugins/MirLua/src/Modules/m_core.cpp
+++ b/plugins/MirLua/src/Modules/m_core.cpp
@@ -13,7 +13,7 @@ static int core_CreateHookableEvent(lua_State *L)
return 1;
}
-int HookEventLuaParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
+static int HookEventLuaParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
lua_State *L = (lua_State*)obj;
@@ -35,28 +35,6 @@ int HookEventLuaParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
return lua_tointeger(L, -1);
}
-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);
-}
-
static int core_HookEvent(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
@@ -65,20 +43,14 @@ static int core_HookEvent(lua_State *L)
lua_pushvalue(L, 2);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
- HANDLE res = nullptr;
CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
- if (env) {
- res = HookEventObjParam(name, HookEventEnvParam, env, ref);
- if (res)
- env->AddHookRef(res, ref);
- }
- else
- res = HookEventObjParam(name, HookEventLuaParam, L, ref);
+ HANDLE res = env != nullptr
+ ? env->HookEvent(name, ref)
+ : HookEventObjParam(name, HookEventLuaParam, L, ref);
if (res == nullptr) {
luaL_unref(L, LUA_REGISTRYINDEX, ref);
lua_pushnil(L);
-
return 1;
}
@@ -95,15 +67,10 @@ static int core_HookTemporaryEvent(lua_State *L)
lua_pushvalue(L, 2);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
- HANDLE res = nullptr;
CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
- if (env) {
- res = HookEventObjParam(name, HookEventEnvParam, env, ref);
- if (res)
- env->AddHookRef(res, ref);
- }
- else
- res = HookEventObjParam(name, HookEventLuaParam, L, ref);
+ HANDLE res = env != nullptr
+ ? env->HookEvent(name, ref)
+ : HookEventObjParam(name, HookEventLuaParam, L, ref);
// event does not exists, call hook immideatelly
if (res == nullptr) {
@@ -124,12 +91,10 @@ 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) {
- CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
- if (env)
- env->ReleaseHookRef(hEvent);
- }
+ CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
+ int res = env != nullptr
+ ? env->UnhookEvent(hEvent)
+ : UnhookEvent(hEvent);
lua_pushboolean(L, !res);
return 1;
@@ -167,23 +132,6 @@ INT_PTR CreateServiceFunctionLuaStateParam(void *obj, WPARAM wParam, LPARAM lPar
return res;
}
-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;
-}
-
static int core_CreateServiceFunction(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
@@ -192,15 +140,10 @@ static int core_CreateServiceFunction(lua_State *L)
lua_pushvalue(L, 2);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
- HANDLE res = nullptr;
CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
- if (env) {
- res = CreateServiceFunctionObjParam(name, CreateServiceFunctionEnvParam, env, ref);
- if (res)
- env->AddServiceRef(res, ref);
- }
- else
- res = CreateServiceFunctionObjParam(name, CreateServiceFunctionLuaStateParam, L, ref);
+ HANDLE res = env != nullptr
+ ? env->CreateServiceFunction(name, ref)
+ : CreateServiceFunctionObjParam(name, CreateServiceFunctionLuaStateParam, L, ref);
if (!res) {
luaL_unref(L, LUA_REGISTRYINDEX, ref);
@@ -242,9 +185,9 @@ static int core_DestroyServiceFunction(lua_State *L)
CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
if (env)
- env->ReleaseHookRef(hService);
-
- DestroyServiceFunction(hService);
+ env->DestroyServiceFunction(hService);
+ else
+ DestroyServiceFunction(hService);
return 0;
}
@@ -255,7 +198,7 @@ static int core_IsPluginLoaded(lua_State *L)
{
const char *value = lua_tostring(L, 1);
- MUUID uuid = { 0 };
+ MUUID uuid = { };
bool res = UuidFromStringA((RPC_CSTR)value, (UUID*)&uuid) == RPC_S_OK;
if (res)
res = IsPluginLoaded(uuid) > 0;
@@ -266,8 +209,7 @@ static int core_IsPluginLoaded(lua_State *L)
static int core_Free(lua_State *L)
{
- if (lua_islightuserdata(L, 1))
- {
+ if (lua_islightuserdata(L, 1)) {
void *ptr = lua_touserdata(L, 1);
mir_free(ptr);
}
diff --git a/plugins/MirLua/src/environment.cpp b/plugins/MirLua/src/environment.cpp
index 4a82e63231..e0090c6d70 100644
--- a/plugins/MirLua/src/environment.cpp
+++ b/plugins/MirLua/src/environment.cpp
@@ -46,33 +46,78 @@ int CMLuaEnvironment::GetEnvironmentId(lua_State *L)
: hMLuaLangpack;
}
-int CMLuaEnvironment::GetId() const
+static int HookEventEnvParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
{
- return m_id;
+ 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);
}
-void CMLuaEnvironment::AddHookRef(HANDLE h, int ref)
+HANDLE CMLuaEnvironment::HookEvent(const char *name, int ref)
{
- m_hookRefs[h] = ref;
+ HANDLE hHook = HookEventObjParam(name, HookEventEnvParam, this, ref);
+ if (hHook)
+ m_hookRefs[hHook] = ref;
+ return hHook;
}
-void CMLuaEnvironment::ReleaseHookRef(HANDLE h)
+int CMLuaEnvironment::UnhookEvent(HANDLE hEvent)
{
- auto it = m_hookRefs.find(h);
- if (it != m_hookRefs.end())
- luaL_unref(L, LUA_REGISTRYINDEX, it->second);
+ 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;
}
-void CMLuaEnvironment::AddServiceRef(HANDLE h, int ref)
+HANDLE CMLuaEnvironment::CreateServiceFunction(const char *name, int ref)
{
- m_serviceRefs[h] = ref;
+ HANDLE hService = CreateServiceFunctionObjParam(name, CreateServiceFunctionEnvParam, this, ref);
+ if (hService)
+ m_serviceRefs[hService] = ref;
+ return hService;
}
-void CMLuaEnvironment::ReleaseServiceRef(HANDLE h)
+void CMLuaEnvironment::DestroyServiceFunction(HANDLE hService)
{
- auto it = m_serviceRefs.find(h);
+ auto it = m_serviceRefs.find(hService);
if (it != m_serviceRefs.end())
luaL_unref(L, LUA_REGISTRYINDEX, it->second);
+ ::DestroyServiceFunction(hService);
}
void CMLuaEnvironment::CreateEnvironmentTable()
diff --git a/plugins/MirLua/src/environment.h b/plugins/MirLua/src/environment.h
index aa6ebb103a..1ede34756f 100644
--- a/plugins/MirLua/src/environment.h
+++ b/plugins/MirLua/src/environment.h
@@ -1,17 +1,15 @@
-#ifndef _LUA_ENVIRONMENT_H_
-#define _LUA_ENVIRONMENT_H_
+#pragma once
-class CMLuaEnvironment
+class CMLuaEnvironment : public CMPluginBase
{
private:
- int m_id;
std::map<HANDLE, int> m_hookRefs;
std::map<HANDLE, int> m_serviceRefs;
void CreateEnvironmentTable();
public:
- lua_State *L;
+ lua_State * L;
CMLuaEnvironment(lua_State *L);
virtual ~CMLuaEnvironment();
@@ -19,15 +17,11 @@ public:
static CMLuaEnvironment* GetEnvironment(lua_State *L);
static int GetEnvironmentId(lua_State *L);
- int GetId() const;
+ HANDLE HookEvent(const char *name, int ref);
+ int UnhookEvent(HANDLE hHook);
- void AddHookRef(HANDLE h, int ref);
- void ReleaseHookRef(HANDLE h);
-
- void AddServiceRef(HANDLE h, int ref);
- void ReleaseServiceRef(HANDLE h);
+ HANDLE CreateServiceFunction(const char *name, int ref);
+ void DestroyServiceFunction(HANDLE hService);
bool Load();
};
-
-#endif //_LUA_ENVIRONMENT_H_