From d93bdfc096f018d3b9c80dad756e5b573e886f68 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Tue, 9 Jun 2015 06:44:57 +0000 Subject: MirLua: - added loading of internal modules - added loading of lua scripts - added folders plugin support git-svn-id: http://svn.miranda-ng.org/main/trunk@14069 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/main.cpp | 44 ++++++++++++++++++++++++-- plugins/MirLua/src/mlua.cpp | 36 ++++++++++++--------- plugins/MirLua/src/mlua.h | 6 ++-- plugins/MirLua/src/mlua_core.cpp | 68 ++++++++++++++++++++-------------------- plugins/MirLua/src/stdafx.h | 21 ++++++++++--- 5 files changed, 117 insertions(+), 58 deletions(-) (limited to 'plugins/MirLua') diff --git a/plugins/MirLua/src/main.cpp b/plugins/MirLua/src/main.cpp index d696848850..a374da2daa 100644 --- a/plugins/MirLua/src/main.cpp +++ b/plugins/MirLua/src/main.cpp @@ -1,9 +1,12 @@ #include "stdafx.h" int hLangpack; -CMLua *g_luaCore; HINSTANCE g_hInstance; +CMLua *g_mLua; +HANDLE hCommonFolderPath; +HANDLE hCustomFolderPath; + PLUGININFOEX pluginInfo = { sizeof(PLUGININFOEX), @@ -32,18 +35,53 @@ extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD) return &pluginInfo; } +void LoadScripts(const TCHAR *scriptDir) +{ + TCHAR searchMask[MAX_PATH]; + mir_sntprintf(searchMask, _T("%s\\%s"), scriptDir, _T("*.lua")); + + TCHAR fullPath[MAX_PATH], path[MAX_PATH]; + + WIN32_FIND_DATA fd; + HANDLE hFind = FindFirstFile(searchMask, &fd); + if (hFind != INVALID_HANDLE_VALUE) + { + do + { + if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + { + mir_sntprintf(fullPath, _T("%s\\%s"), scriptDir, fd.cFileName); + PathToRelativeT(fullPath, path); + g_mLua->Load(T2Utf(path)); + } + } while (FindNextFile(hFind, &fd)); + FindClose(hFind); + } +} + extern "C" int __declspec(dllexport) Load(void) { mir_getLP(&pluginInfo); - g_luaCore = new CMLua(); + g_mLua = new CMLua(); + + hCommonFolderPath = FoldersRegisterCustomPathT("MirLua", Translate("Common scripts folder"), COMMON_SCRIPTS_PATHT); + hCustomFolderPath = FoldersRegisterCustomPathT("MirLua", Translate("Custom scripts folder"), CUSTOM_SCRIPTS_PATHT); + + TCHAR commonScriptDir[MAX_PATH]; + FoldersGetCustomPathT(hCommonFolderPath, commonScriptDir, SIZEOF(commonScriptDir), VARST(COMMON_SCRIPTS_PATHT)); + LoadScripts(commonScriptDir); + + TCHAR customScriptDir[MAX_PATH]; + FoldersGetCustomPathT(hCommonFolderPath, customScriptDir, SIZEOF(customScriptDir), VARST(CUSTOM_SCRIPTS_PATHT)); + LoadScripts(customScriptDir); return 0; } extern "C" int __declspec(dllexport) Unload(void) { - delete g_luaCore; + delete g_mLua; return 0; } diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index ee6d91e117..04200e3777 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -1,20 +1,28 @@ -#include "stdafx.h" - +#include "stdafx.h" + CMLua::CMLua() { - lua = luaL_newstate(); + L = luaL_newstate(); + luaL_openlibs(L); + + luaL_newlib(L, CMLua::coreFunctions); + lua_setglobal(L, "M"); +} + +CMLua::~CMLua() +{ + lua_close(L); +} - luaL_openlibs(lua); - luaL_newlib(lua, CMLua::CoreFunctions); - lua_setglobal(lua, "M"); +void CMLua::Load(const char *path) +{ + luaL_dofile(L, path); } -CMLua::~CMLua() -{ - lua_close(lua); -} - -void CMLua::Load(const char *name) -{ - luaL_dofile(lua, name); +void CMLua::Preload(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); } \ No newline at end of file diff --git a/plugins/MirLua/src/mlua.h b/plugins/MirLua/src/mlua.h index bdeef96815..7a4ffd036b 100644 --- a/plugins/MirLua/src/mlua.h +++ b/plugins/MirLua/src/mlua.h @@ -4,13 +4,15 @@ class CMLua { private: - lua_State *lua; + lua_State *L; + + void Preload(const char *name, lua_CFunction func); public: CMLua(); ~CMLua(); - static luaL_Reg CoreFunctions[10]; + static luaL_Reg coreFunctions[10]; void Load(const char *name); }; diff --git a/plugins/MirLua/src/mlua_core.cpp b/plugins/MirLua/src/mlua_core.cpp index 1c38ee0e4a..7f1bd3cf48 100644 --- a/plugins/MirLua/src/mlua_core.cpp +++ b/plugins/MirLua/src/mlua_core.cpp @@ -1,37 +1,37 @@ -#include "stdafx.h" - -static int lua_CreateHookableEvent(lua_State *L) -{ +#include "stdafx.h" + +static int lua_CreateHookableEvent(lua_State *L) +{ const char *name = luaL_checkstring(L, 1); HANDLE res = ::CreateHookableEvent(name); lua_pushlightuserdata(L, res); - return 1; -} - -static int lua_DestroyHookableEvent(lua_State *L) -{ + return 1; +} + +static int lua_DestroyHookableEvent(lua_State *L) +{ HANDLE hEvent = (HANDLE)lua_touserdata(L, 1); int res = ::DestroyHookableEvent(hEvent); lua_pushinteger(L, res); - return 1; -} - -static int lua_NotifyEventHooks(lua_State *L) -{ + return 1; +} + +static int lua_NotifyEventHooks(lua_State *L) +{ HANDLE hEvent = (HANDLE)lua_touserdata(L, 1); WPARAM wParam = (WPARAM)luaL_checkinteger(L, 2); - LPARAM lParam = (LPARAM)luaL_checkinteger(L, 3); - + LPARAM lParam = (LPARAM)luaL_checkinteger(L, 3); + int res = ::NotifyEventHooks(hEvent, wParam, lParam); lua_pushinteger(L, res); - return 1; -} - + return 1; +} + static int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) { lua_State *L = (lua_State*)obj; @@ -47,8 +47,8 @@ static int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM par luaL_unref(L, LUA_REGISTRYINDEX, ref); return res; -} - +} + static int lua_HookEvent(lua_State *L) { const char *name = luaL_checkstring(L, 1); @@ -59,19 +59,19 @@ static int lua_HookEvent(lua_State *L) HANDLE res = ::HookEventObjParam(name, HookEventObjParam, L, ref); lua_pushlightuserdata(L, res); - return 1; -} - -static int lua_UnhookEvent(lua_State *L) -{ + return 1; +} + +static int lua_UnhookEvent(lua_State *L) +{ HANDLE hEvent = (HANDLE)lua_touserdata(L, 1); int res = ::UnhookEvent(hEvent); lua_pushinteger(L, res); - return 1; -} - + return 1; +} + static INT_PTR ServiceFunctionObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) { lua_State *L = (lua_State*)obj; @@ -87,8 +87,8 @@ static INT_PTR ServiceFunctionObjParam(void *obj, WPARAM wParam, LPARAM lParam, luaL_unref(L, LUA_REGISTRYINDEX, ref); return res; -} - +} + static int lua_CreateServiceFunction(lua_State *L) { const char *name = luaL_checkstring(L, 1); @@ -135,7 +135,7 @@ static int lua_CallService(lua_State *L) return 1; } -luaL_Reg CMLua::CoreFunctions[10] = +luaL_Reg CMLua::coreFunctions[10] = { { "CreateHookableEvent", lua_CreateHookableEvent }, { "DestroyHookableEvent", lua_DestroyHookableEvent }, @@ -151,5 +151,5 @@ luaL_Reg CMLua::CoreFunctions[10] = { "ServiceExists", lua_ServiceExists }, { "CallService", lua_CallService }, - { 0, 0 } -}; + { NULL, NULL } +}; diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index e91befca3d..676028d425 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -4,13 +4,16 @@ #include #include +#include +#include #include +#include -extern "C" -{ - #include "lua\lua.h" - #include "lua\lualib.h" - #include "lua\lauxlib.h" +extern "C" +{ + #include "lua\lua.h" + #include "lua\lualib.h" + #include "lua\lauxlib.h" } #include "version.h" @@ -24,4 +27,12 @@ class CMLua; extern HINSTANCE g_hInstance; +#ifdef _UNICODE + #define COMMON_SCRIPTS_PATHT MIRANDA_PATHW L"\\Scripts" + #define CUSTOM_SCRIPTS_PATHT MIRANDA_USERDATAW L"\\Scripts" +#else + #define COMMON_SCRIPTS_PATHT MIRANDA_PATH "\\Scripts" + #define CUSTOM_SCRIPTS_PATHT MIRANDA_USERDATA "\\Scripts" +#endif + #endif //_COMMON_H_ -- cgit v1.2.3