From 3bfd3b9c9062048fde854b4c45ab62c29624d8f8 Mon Sep 17 00:00:00 2001 From: aunsane Date: Mon, 28 May 2018 21:26:54 +0300 Subject: MirLua: removed global Load & Unload --- plugins/MirLua/src/Modules/m_core.cpp | 6 +- plugins/MirLua/src/Modules/m_database.cpp | 48 +++++--- plugins/MirLua/src/Modules/m_http.cpp | 2 +- plugins/MirLua/src/Modules/m_json.cpp | 120 ++++++++++++-------- plugins/MirLua/src/Modules/m_srmm.cpp | 6 +- plugins/MirLua/src/environment.cpp | 2 +- plugins/MirLua/src/icons.cpp | 4 +- plugins/MirLua/src/main.cpp | 78 ------------- plugins/MirLua/src/mplugin.cpp | 143 ------------------------ plugins/MirLua/src/mplugin.h | 27 ----- plugins/MirLua/src/netlib.cpp | 20 ++++ plugins/MirLua/src/options.cpp | 15 +++ plugins/MirLua/src/plugin.cpp | 178 ++++++++++++++++++++++++++++++ plugins/MirLua/src/plugin.h | 26 +++++ plugins/MirLua/src/script_loader.cpp | 3 + plugins/MirLua/src/stdafx.h | 19 ++-- plugins/MirLua/src/utils.cpp | 4 +- 17 files changed, 369 insertions(+), 332 deletions(-) delete mode 100644 plugins/MirLua/src/main.cpp delete mode 100644 plugins/MirLua/src/mplugin.cpp delete mode 100644 plugins/MirLua/src/mplugin.h create mode 100644 plugins/MirLua/src/netlib.cpp create mode 100644 plugins/MirLua/src/plugin.cpp create mode 100644 plugins/MirLua/src/plugin.h (limited to 'plugins/MirLua') diff --git a/plugins/MirLua/src/Modules/m_core.cpp b/plugins/MirLua/src/Modules/m_core.cpp index 365b89bb43..281aa288fe 100644 --- a/plugins/MirLua/src/Modules/m_core.cpp +++ b/plugins/MirLua/src/Modules/m_core.cpp @@ -89,12 +89,12 @@ static int core_HookTemporaryEvent(lua_State *L) static int core_UnhookEvent(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); - HANDLE hEvent = lua_touserdata(L, 1); + HANDLE hHook = lua_touserdata(L, 1); CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L); int res = env != nullptr - ? env->UnhookEvent(hEvent) - : UnhookEvent(hEvent); + ? env->UnhookEvent(hHook) + : UnhookEvent(hHook); lua_pushboolean(L, !res); return 1; diff --git a/plugins/MirLua/src/Modules/m_database.cpp b/plugins/MirLua/src/Modules/m_database.cpp index 3d0d53ba48..875b1e6eb0 100644 --- a/plugins/MirLua/src/Modules/m_database.cpp +++ b/plugins/MirLua/src/Modules/m_database.cpp @@ -1,5 +1,7 @@ #include "../stdafx.h" +#define MT_CONTACT "CONTACT" + void luaM_pushdbvt(lua_State *L, const DBVARIANT &value) { switch (value.type) { @@ -134,7 +136,7 @@ static int db_GetContactInfo(lua_State *L) static int db_GetEventCount(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); int res = db_event_count(hContact); lua_pushinteger(L, res); @@ -144,7 +146,7 @@ static int db_GetEventCount(lua_State *L) static int db_GetFirstEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT res = db_event_first(hContact); lua_pushinteger(L, res); @@ -154,7 +156,7 @@ static int db_GetFirstEvent(lua_State *L) static int db_GetPrevEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT hDbEvent = luaL_checkinteger(L, 2); MEVENT res = db_event_prev(hContact, hDbEvent); @@ -165,7 +167,7 @@ static int db_GetPrevEvent(lua_State *L) static int db_GetNextEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT hDbEvent = luaL_checkinteger(L, 2); MEVENT res = db_event_next(hContact, hDbEvent); @@ -176,7 +178,7 @@ static int db_GetNextEvent(lua_State *L) static int db_GetLastEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT res = db_event_last(hContact); lua_pushinteger(L, res); @@ -186,7 +188,7 @@ static int db_GetLastEvent(lua_State *L) static int db_GetFirstUnreadEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT res = db_event_firstUnread(hContact); lua_pushinteger(L, res); @@ -216,7 +218,7 @@ static int db_EventIterator(lua_State *L) static int db_Events(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); lua_pushinteger(L, hContact); lua_pushinteger(L, NULL); @@ -247,7 +249,7 @@ static int db_EventReverseIterator(lua_State *L) static int db_EventsFromEnd(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); lua_pushinteger(L, hContact); lua_pushinteger(L, NULL); @@ -276,7 +278,7 @@ static int db_UnreadEventIterator(lua_State *L) static int db_UnreadEvents(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); lua_pushinteger(L, hContact); lua_pushinteger(L, NULL); @@ -327,8 +329,8 @@ void MakeDbEvent(lua_State *L, DBEVENTINFO &dbei) static int db_AddEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); - + MCONTACT hContact = luaL_optinteger(L, 1, 0); + DBEVENTINFO dbei; MakeDbEvent(L, dbei); MEVENT hDbEvent = db_event_add(hContact, &dbei); @@ -341,9 +343,20 @@ static int db_AddEvent(lua_State *L) return 1; } +static int db_DeleteEvent(lua_State *L) +{ + MCONTACT hContact = luaL_optinteger(L, 1, 0); + MEVENT hDbEvent = luaL_checkinteger(L, 2); + + int res = db_event_delete(hContact, hDbEvent); + lua_pushboolean(L, res == 0); + + return 1; +} + static int db_MarkReadEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT hDbEvent = luaL_checkinteger(L, 2); int res = db_event_markRead(hContact, hDbEvent); @@ -398,7 +411,7 @@ static int db_Modules(lua_State *L) static int db_DeleteModule(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); const char *szModule = luaL_checkstring(L, 2); INT_PTR res = db_delete_module(hContact, szModule); @@ -437,7 +450,7 @@ static int db_SettingIterator(lua_State *L) static int db_Settings(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); const char* szModule = luaL_checkstring(L, 2); LIST *param = new LIST(5, PtrKeySortT); @@ -452,7 +465,7 @@ static int db_Settings(lua_State *L) static int db_GetSetting(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); const char *szModule = luaL_checkstring(L, 2); const char *szSetting = luaL_checkstring(L, 3); @@ -475,7 +488,7 @@ static int db_GetSetting(lua_State *L) static int db_WriteSetting(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); const char *szModule = luaL_checkstring(L, 2); const char *szSetting = luaL_checkstring(L, 3); luaL_checkany(L, 4); @@ -549,7 +562,7 @@ static int db_WriteSetting(lua_State *L) static int db_DeleteSetting(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); LPCSTR szModule = luaL_checkstring(L, 2); LPCSTR szSetting = luaL_checkstring(L, 3); @@ -577,6 +590,7 @@ static luaL_Reg databaseApi[] = { "EventsFromEnd", db_EventsFromEnd }, { "UnreadEvents", db_UnreadEvents }, { "AddEvent", db_AddEvent }, + { "DeleteEvent", db_DeleteEvent }, { "MarkReadEvent", db_MarkReadEvent }, { "Settings", db_Settings }, diff --git a/plugins/MirLua/src/Modules/m_http.cpp b/plugins/MirLua/src/Modules/m_http.cpp index 31def57a9a..81750619da 100644 --- a/plugins/MirLua/src/Modules/m_http.cpp +++ b/plugins/MirLua/src/Modules/m_http.cpp @@ -165,7 +165,7 @@ static const luaL_Reg contentApi[] = static NETLIBHTTPREQUEST* response_Create(lua_State *L, NETLIBHTTPREQUEST *request) { NETLIBHTTPREQUEST **response = (NETLIBHTTPREQUEST**)lua_newuserdata(L, sizeof(NETLIBHTTPREQUEST*)); - *response = Netlib_HttpTransaction(hNetlib, request); + *response = Netlib_HttpTransaction(g_hNetlib, request); luaL_setmetatable(L, MT_NETLIBHTTPRESPONSE); return *response; diff --git a/plugins/MirLua/src/Modules/m_json.cpp b/plugins/MirLua/src/Modules/m_json.cpp index 24e32cab09..907e0f582b 100644 --- a/plugins/MirLua/src/Modules/m_json.cpp +++ b/plugins/MirLua/src/Modules/m_json.cpp @@ -17,7 +17,7 @@ static void lua2json(lua_State *L, JSONNode &node) case LUA_TNUMBER: { lua_Integer val = lua_tointeger(L, -1); - if (lua_isinteger(L, -1) && val >= LONG_MIN && val <= LONG_MIN) + if (lua_isinteger(L, -1) && val >= LONG_MIN && val <= LONG_MAX) node = (long)val; else node = lua_tonumber(L, -1); @@ -48,6 +48,21 @@ static void lua2json(lua_State *L, JSONNode &node) break; } + case LUA_TLIGHTUSERDATA: + if (lua_touserdata(L, -1)) + luaL_argerror(L, -1, luaL_typename(L, -1)); + node.nullify(); + break; + case LUA_TUSERDATA: + { + ptrA name(mir_strdup(node.name())); + JSONNode *other = *(JSONNode**)luaL_checkudata(L, -1, MT_JSON); + node = other->duplicate(); + node.set_name((char*)name); + break; + } + default: + luaL_argerror(L, -1, luaL_typename(L, -1)); } } @@ -167,73 +182,86 @@ static int lua_Decode(lua_State *L) return 1; } +/***********************************************/ + static int lua_Encode(lua_State *L) { + //JSONNode node; + //lua_pushnil(L); + //lua_pushvalue(L, 1); + //lua2json(L, node); + //lua_pop(L, 2); + //lua_pushstring(L, node.write().c_str()); + switch (lua_type(L, 1)) { - case LUA_TNIL: - lua_pushliteral(L, "null"); - break; - case LUA_TBOOLEAN: - lua_pushstring(L, lua_toboolean(L, 1) ? "true" : "false"); - break; - case LUA_TNUMBER: - { - if (lua_isinteger(L, 1)) { - lua_pushfstring(L, "%I", lua_tointeger(L, 1)); + case LUA_TNIL: + lua_pushliteral(L, "null"); + break; + case LUA_TBOOLEAN: + lua_pushstring(L, lua_toboolean(L, 1) ? "true" : "false"); + break; + case LUA_TNUMBER: + { + if (lua_isinteger(L, 1)) { + lua_pushfstring(L, "%I", lua_tointeger(L, 1)); + break; + } + char decpoint = lua_getlocaledecpoint(); + if (decpoint != '.') { + char p[2] = { decpoint }; + luaL_gsub(L, lua_tostring(L, 1), p, "."); + } + else + lua_pushfstring(L, "%f", lua_tonumber(L, 1)); break; } - char decpoint = lua_getlocaledecpoint(); - if (decpoint != '.') { - char p[2] = { decpoint }; - luaL_gsub(L, lua_tostring(L, 1), p, "."); + case LUA_TSTRING: + lua_pushfstring(L, "\"%s\"", lua_tostring(L, 1)); + break; + case LUA_TTABLE: + { + JSONNode node; + lua_pushnil(L); + lua_pushvalue(L, 1); + lua2json(L, node); + lua_pop(L, 2); + lua_pushstring(L, node.write().c_str()); + break; } - else - lua_pushfstring(L, "%f", lua_tonumber(L, 1)); - break; - } - case LUA_TSTRING: - lua_pushfstring(L, "\"%s\"", lua_tostring(L, 1)); - break; - case LUA_TTABLE: - { - JSONNode node; - lua_pushnil(L); - lua_pushvalue(L, 1); - lua2json(L, node); - lua_pop(L, 2); - lua_pushstring(L, node.write().c_str()); - break; - } - case LUA_TUSERDATA: - { - JSONNode *node = *(JSONNode**)luaL_checkudata(L, 1, MT_JSON); - lua_pushstring(L, node->write().c_str()); - break; - } - case LUA_TLIGHTUSERDATA: - if (lua_touserdata(L, 1) == nullptr) + case LUA_TUSERDATA: { - lua_pushliteral(L, "null"); + JSONNode *node = *(JSONNode**)luaL_checkudata(L, 1, MT_JSON); + lua_pushstring(L, node->write().c_str()); break; } - default: - luaL_argerror(L, 1, luaL_typename(L, 1)); - } + case LUA_TLIGHTUSERDATA: + if (lua_touserdata(L, 1) == nullptr) + { + lua_pushliteral(L, "null"); + break; + } + default: + luaL_argerror(L, 1, luaL_typename(L, 1)); + } - return 1; -} + return 1; + } static const luaL_Reg methods[] = { { "Decode", lua_Decode }, { "Encode", lua_Encode }, + { "null", nullptr }, + { nullptr, nullptr } }; LUAMOD_API int luaopen_m_json(lua_State *L) { luaL_newlib(L, methods); + lua_pushlightuserdata(L, nullptr); + lua_setfield(L, -2, "null"); luaL_newmetatable(L, MT_JSON); luaL_setfuncs(L, jsonApi, 0); diff --git a/plugins/MirLua/src/Modules/m_srmm.cpp b/plugins/MirLua/src/Modules/m_srmm.cpp index f7296d1d70..bbaf3edc3f 100644 --- a/plugins/MirLua/src/Modules/m_srmm.cpp +++ b/plugins/MirLua/src/Modules/m_srmm.cpp @@ -51,8 +51,7 @@ static int lua_AddButton(lua_State *L) HANDLE res = Srmm_AddButton(&bbb, hScriptLangpack); CleanBBButton(bbb); - if (!res) - { + if (!res) { lua_pushnil(L); return 1; } @@ -82,8 +81,7 @@ static int lua_RemoveButton(lua_State *L) { BBButton bbb = {}; - switch (lua_type(L, 1)) - { + switch (lua_type(L, 1)) { case LUA_TSTRING: bbb.pszModuleName = mir_utf8decodeA(lua_tostring(L, 1)); bbb.dwButtonID = luaL_checkinteger(L, 2); diff --git a/plugins/MirLua/src/environment.cpp b/plugins/MirLua/src/environment.cpp index 321eace843..76a34cbc55 100644 --- a/plugins/MirLua/src/environment.cpp +++ b/plugins/MirLua/src/environment.cpp @@ -46,7 +46,7 @@ int CMLuaEnvironment::GetEnvironmentId(lua_State *L) CMLuaEnvironment *env = GetEnvironment(L); return env != nullptr ? env->m_hLang - : hMLuaLangpack; + : g_hMLuaLangpack; } static int HookEventEnvParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) diff --git a/plugins/MirLua/src/icons.cpp b/plugins/MirLua/src/icons.cpp index 4a6cc91219..5d5feacb3e 100644 --- a/plugins/MirLua/src/icons.cpp +++ b/plugins/MirLua/src/icons.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" -IconItem Icons[] = +static IconItem Icons[] = { { LPGEN("Script"), "script", IDI_SCRIPT }, { LPGEN("Loaded"), "loaded", IDI_LOADED }, @@ -9,7 +9,7 @@ IconItem Icons[] = { LPGEN("Reload"), "reload", IDI_RELOAD }, }; -void InitIcons() +void LoadIcons() { g_plugin.registerIcon(MODULENAME, Icons, MODULENAME); } diff --git a/plugins/MirLua/src/main.cpp b/plugins/MirLua/src/main.cpp deleted file mode 100644 index 98bdf213d4..0000000000 --- a/plugins/MirLua/src/main.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include "stdafx.h" - -CMPlugin g_plugin; - -HANDLE g_hCLibsFolder; -HANDLE g_hScriptsFolder; - -HNETLIBUSER hNetlib = nullptr; - -///////////////////////////////////////////////////////////////////////////////////////// - -PLUGININFOEX pluginInfoEx = -{ - sizeof(PLUGININFOEX), - __PLUGIN_NAME, - PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM), - __DESCRIPTION, - __AUTHOR, - __COPYRIGHT, - __AUTHORWEB, - UNICODE_AWARE, - // {27d41d81-991f-4dc6-8749-b0321c87e694} - { 0x27d41d81, 0x991f, 0x4dc6, { 0x87, 0x49, 0xb0, 0x32, 0x1c, 0x87, 0xe6, 0x94 } } - -}; - -///////////////////////////////////////////////////////////////////////////////////////// - -int OnOptionsInit(WPARAM wParam, LPARAM) -{ - OPTIONSDIALOGPAGE odp = {}; - odp.hInstance = g_plugin.getInst(); - odp.flags = ODPF_BOLDGROUPS | ODPF_UNICODE | ODPF_DONTTRANSLATE; - odp.szGroup.w = LPGENW("Services"); - odp.szTitle.w = L"Lua"; - odp.szTab.w = LPGENW("Scripts"); - odp.pDialog = new CMLuaOptions(); - g_plugin.addOptions(wParam, &odp); - return 0; -} - -int OnModulesLoaded(WPARAM, LPARAM) -{ - g_hCLibsFolder = FoldersRegisterCustomPathT(MODULENAME, "CLibsFolder", MIRLUA_PATHT, TranslateT("C libs folder")); - g_hScriptsFolder = FoldersRegisterCustomPathT(MODULENAME, "ScriptsFolder", MIRLUA_PATHT, TranslateT("Scripts folder")); - - HookEvent(ME_OPT_INITIALISE, OnOptionsInit); - return 0; -} - -extern "C" int __declspec(dllexport) Load(void) -{ - InitIcons(); - - NETLIBUSER nlu = {}; - nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS; - nlu.szDescriptiveName.a = MODULENAME; - nlu.szSettingsModule = MODULENAME; - hNetlib = Netlib_RegisterUser(&nlu); - - HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded); - - g_plugin.Load(); - - return 0; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -extern "C" int __declspec(dllexport) Unload(void) -{ - if (hNetlib) { - Netlib_CloseHandle(hNetlib); - hNetlib = nullptr; - } - - return 0; -} diff --git a/plugins/MirLua/src/mplugin.cpp b/plugins/MirLua/src/mplugin.cpp deleted file mode 100644 index 44e73ae9cc..0000000000 --- a/plugins/MirLua/src/mplugin.cpp +++ /dev/null @@ -1,143 +0,0 @@ -#include "stdafx.h" - -extern PLUGININFOEX pluginInfoEx; - -int hMLuaLangpack; - -CMPlugin::CMPlugin() - : PLUGIN(MODULENAME, pluginInfoEx), - L(nullptr), - Scripts(1) -{ - MUUID muidLast = MIID_LAST; - hMLuaLangpack = GetPluginLangId(muidLast, 0); - - RegisterProtocol(PROTOTYPE_FILTER); - - CreatePluginService(MS_LUA_CALL, &CMPlugin::Call); - CreatePluginService(MS_LUA_EXEC, &CMPlugin::Exec); - CreatePluginService(MS_LUA_EVAL, &CMPlugin::Eval); -} - -CMPlugin::~CMPlugin() -{ - Unload(); -} - -int CMPlugin::Load() -{ - Log("Loading lua engine"); - L = luaL_newstate(); - Log("Loading standard modules"); - luaL_openlibs(L); - - lua_atpanic(L, luaM_atpanic); - - CMLuaFunctionLoader::Load(L); - CMLuaModuleLoader::Load(L); - CMLuaScriptLoader::Load(L); - return 0; -} - -int CMPlugin::Unload() -{ - Log("Unloading lua engine"); - - Scripts.destroy(); - - KillModuleIcons(hMLuaLangpack); - KillModuleSounds(hMLuaLangpack); - KillModuleMenus(hMLuaLangpack); - KillModuleHotkeys(hMLuaLangpack); - - KillObjectEventHooks(L); - KillObjectServices(L); - - lua_close(L); - return 0; -} - -void CMPlugin::Reload() -{ - Unload(); - Load(); -} - -/***********************************************/ - -static int mlua_call(lua_State *L) -{ - const char *module = luaL_checkstring(L, -3); - const char *function = luaL_checkstring(L, -2); - - if (module && module[0]) { - lua_getglobal(L, "require"); - lua_pushstring(L, module); - lua_pcall(L, 1, 1, 0); - - lua_getfield(L, -1, function); - lua_replace(L, -2); - } - else - lua_getglobal(L, function); - - lua_pcall(L, 0, 1, 0); - - return 1; -} - -INT_PTR CMPlugin::Call(WPARAM wParam, LPARAM lParam) -{ - const wchar_t *module = (const wchar_t*)wParam; - const wchar_t *function = (const wchar_t*)lParam; - - lua_pushstring(L, ptrA(mir_utf8encodeW(module))); - lua_pushstring(L, ptrA(mir_utf8encodeW(function))); - - lua_newtable(L); - lua_pushcclosure(L, mlua_call, 1); - - CMLuaEnvironment env(L); - env.Load(); - - wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); - lua_pop(L, 1); - - return (INT_PTR)result; -} - -INT_PTR CMPlugin::Eval(WPARAM, LPARAM lParam) -{ - const wchar_t *script = (const wchar_t*)lParam; - - if (luaL_loadstring(L, ptrA(mir_utf8encodeW(script)))) { - ReportError(L); - return NULL; - } - - CMLuaEnvironment env(L); - env.Load(); - - wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); - lua_pop(L, 1); - - return (INT_PTR)result; -} - -INT_PTR CMPlugin::Exec(WPARAM, LPARAM lParam) -{ - const wchar_t *path = (const wchar_t*)lParam; - - if (luaL_loadfile(L, ptrA(mir_utf8encodeW(path)))) { - ReportError(L); - return NULL; - } - - CMLuaEnvironment env(L); - env.Load(); - - wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); - lua_pop(L, 1); - - return (INT_PTR)result; -} diff --git a/plugins/MirLua/src/mplugin.h b/plugins/MirLua/src/mplugin.h deleted file mode 100644 index f448d06daf..0000000000 --- a/plugins/MirLua/src/mplugin.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef _LUA_CORE_H_ -#define _LUA_CORE_H_ - -struct CMPlugin : public PLUGIN -{ - friend class CMLuaOptions; - -private: - lua_State *L; - - INT_PTR __cdecl Eval(WPARAM, LPARAM); - INT_PTR __cdecl Call(WPARAM, LPARAM); - INT_PTR __cdecl Exec(WPARAM, LPARAM); - -public: - OBJLIST Scripts; - - CMPlugin(); - ~CMPlugin(); - - int Load() override; - int Unload() override; - - void Reload(); -}; - -#endif //_LUA_CORE_H_ diff --git a/plugins/MirLua/src/netlib.cpp b/plugins/MirLua/src/netlib.cpp new file mode 100644 index 0000000000..246ed9ce04 --- /dev/null +++ b/plugins/MirLua/src/netlib.cpp @@ -0,0 +1,20 @@ +#include "stdafx.h" + +HNETLIBUSER g_hNetlib = nullptr; + +void LoadNetlib() +{ + NETLIBUSER nlu = {}; + nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS; + nlu.szDescriptiveName.a = MODULENAME; + nlu.szSettingsModule = MODULENAME; + g_hNetlib = Netlib_RegisterUser(&nlu); +} + +void UnloadNetlib() +{ + if (g_hNetlib) { + Netlib_CloseHandle(g_hNetlib); + g_hNetlib = nullptr; + } +} \ No newline at end of file diff --git a/plugins/MirLua/src/options.cpp b/plugins/MirLua/src/options.cpp index 3c2dade81b..2698bc1498 100644 --- a/plugins/MirLua/src/options.cpp +++ b/plugins/MirLua/src/options.cpp @@ -129,3 +129,18 @@ void CMLuaOptions::OnReload(CCtrlBase*) LoadScripts(); isScriptListInit = true; } + +/***********************************************/ + +int OnOptionsInit(WPARAM wParam, LPARAM) +{ + OPTIONSDIALOGPAGE odp = {}; + odp.hInstance = g_plugin.getInst(); + odp.flags = ODPF_BOLDGROUPS | ODPF_UNICODE | ODPF_DONTTRANSLATE; + odp.szGroup.w = LPGENW("Services"); + odp.szTitle.w = L"Lua"; + odp.szTab.w = LPGENW("Scripts"); + odp.pDialog = new CMLuaOptions(); + g_plugin.addOptions(wParam, &odp); + return 0; +} \ No newline at end of file diff --git a/plugins/MirLua/src/plugin.cpp b/plugins/MirLua/src/plugin.cpp new file mode 100644 index 0000000000..9dd1e7c82e --- /dev/null +++ b/plugins/MirLua/src/plugin.cpp @@ -0,0 +1,178 @@ +#include "stdafx.h" + +int g_hMLuaLangpack; +CMPlugin g_plugin; + +PLUGININFOEX pluginInfoEx = +{ + sizeof(PLUGININFOEX), + __PLUGIN_NAME, + PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM), + __DESCRIPTION, + __AUTHOR, + __COPYRIGHT, + __AUTHORWEB, + UNICODE_AWARE, + // {27d41d81-991f-4dc6-8749-b0321c87e694} + { 0x27d41d81, 0x991f, 0x4dc6,{ 0x87, 0x49, 0xb0, 0x32, 0x1c, 0x87, 0xe6, 0x94 } } +}; + +CMPlugin::CMPlugin() + : PLUGIN(MODULENAME, pluginInfoEx), + L(nullptr), + Scripts(1) +{ + MUUID muidLast = MIID_LAST; + g_hMLuaLangpack = GetPluginLangId(muidLast, 0); + + RegisterProtocol(PROTOTYPE_FILTER); + + CreatePluginService(MS_LUA_CALL, &CMPlugin::Call); + CreatePluginService(MS_LUA_EXEC, &CMPlugin::Exec); + CreatePluginService(MS_LUA_EVAL, &CMPlugin::Eval); +} + +void CMPlugin::LoadLua() +{ + Log("Loading lua engine"); + L = luaL_newstate(); + Log("Loading standard modules"); + luaL_openlibs(L); + + lua_atpanic(L, luaM_atpanic); + + CMLuaFunctionLoader::Load(L); + CMLuaModuleLoader::Load(L); + CMLuaScriptLoader::Load(L); +} + +void CMPlugin::UnloadLua() +{ + Log("Unloading lua engine"); + + Scripts.destroy(); + + KillModuleIcons(g_hMLuaLangpack); + KillModuleSounds(g_hMLuaLangpack); + KillModuleMenus(g_hMLuaLangpack); + KillModuleHotkeys(g_hMLuaLangpack); + + KillObjectEventHooks(L); + KillObjectServices(L); + + lua_close(L); +} + +void CMPlugin::Reload() +{ + Unload(); + Load(); +} + +/***********************************************/ + +static int OnModulesLoaded(WPARAM, LPARAM) +{ + g_hCLibsFolder = FoldersRegisterCustomPathT(MODULENAME, "CLibsFolder", MIRLUA_PATHT, TranslateT("C libs folder")); + g_hScriptsFolder = FoldersRegisterCustomPathT(MODULENAME, "ScriptsFolder", MIRLUA_PATHT, TranslateT("Scripts folder")); + + HookEvent(ME_OPT_INITIALISE, OnOptionsInit); + return 0; +} + +int CMPlugin::Load() +{ + LoadIcons(); + LoadNetlib(); + LoadLua(); + + HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded); + + return 0; +} + +int CMPlugin::Unload() +{ + UnloadLua(); + UnloadNetlib(); + return 0; +} + +/***********************************************/ + +static int mlua_call(lua_State *L) +{ + const char *module = luaL_checkstring(L, -3); + const char *function = luaL_checkstring(L, -2); + + if (module && module[0]) { + lua_getglobal(L, "require"); + lua_pushstring(L, module); + lua_pcall(L, 1, 1, 0); + + lua_getfield(L, -1, function); + lua_replace(L, -2); + } + else + lua_getglobal(L, function); + + lua_pcall(L, 0, 1, 0); + + return 1; +} + +INT_PTR CMPlugin::Call(WPARAM wParam, LPARAM lParam) +{ + const wchar_t *module = (const wchar_t*)wParam; + const wchar_t *function = (const wchar_t*)lParam; + + lua_pushstring(L, ptrA(mir_utf8encodeW(module))); + lua_pushstring(L, ptrA(mir_utf8encodeW(function))); + + lua_newtable(L); + lua_pushcclosure(L, mlua_call, 1); + + CMLuaEnvironment env(L); + env.Load(); + + wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); + lua_pop(L, 1); + + return (INT_PTR)result; +} + +INT_PTR CMPlugin::Eval(WPARAM, LPARAM lParam) +{ + const wchar_t *script = (const wchar_t*)lParam; + + if (luaL_loadstring(L, ptrA(mir_utf8encodeW(script)))) { + ReportError(L); + return NULL; + } + + CMLuaEnvironment env(L); + env.Load(); + + wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); + lua_pop(L, 1); + + return (INT_PTR)result; +} + +INT_PTR CMPlugin::Exec(WPARAM, LPARAM lParam) +{ + const wchar_t *path = (const wchar_t*)lParam; + + if (luaL_loadfile(L, ptrA(mir_utf8encodeW(path)))) { + ReportError(L); + return NULL; + } + + CMLuaEnvironment env(L); + env.Load(); + + wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); + lua_pop(L, 1); + + return (INT_PTR)result; +} diff --git a/plugins/MirLua/src/plugin.h b/plugins/MirLua/src/plugin.h new file mode 100644 index 0000000000..6339b36353 --- /dev/null +++ b/plugins/MirLua/src/plugin.h @@ -0,0 +1,26 @@ +#pragma once + +struct CMPlugin : public PLUGIN +{ + friend class CMLuaOptions; + +private: + lua_State *L; + + void LoadLua(); + void UnloadLua(); + + INT_PTR __cdecl Eval(WPARAM, LPARAM); + INT_PTR __cdecl Call(WPARAM, LPARAM); + INT_PTR __cdecl Exec(WPARAM, LPARAM); + +public: + OBJLIST Scripts; + + CMPlugin(); + + void Reload(); + + int Load() override; + int Unload() override; +}; diff --git a/plugins/MirLua/src/script_loader.cpp b/plugins/MirLua/src/script_loader.cpp index e192446968..998fffc376 100644 --- a/plugins/MirLua/src/script_loader.cpp +++ b/plugins/MirLua/src/script_loader.cpp @@ -1,5 +1,8 @@ #include "stdafx.h" +HANDLE g_hCLibsFolder = nullptr; +HANDLE g_hScriptsFolder = nullptr; + CMLuaScriptLoader::CMLuaScriptLoader(lua_State *L) : L(L) { diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index e8a358ac24..9f0397a828 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -37,7 +37,7 @@ class CMLuaScript; -#include "mplugin.h" +#include "plugin.h" #include "modules.h" #include "environment.h" #include "script.h" @@ -49,7 +49,8 @@ class CMLuaScript; #define MODULENAME "MirLua" -extern int hMLuaLangpack; +extern int g_hMLuaLangpack; +extern PLUGININFOEX g_pluginInfoEx; extern HANDLE g_hCLibsFolder; extern HANDLE g_hScriptsFolder; @@ -59,12 +60,18 @@ extern HANDLE g_hScriptsFolder; #define MIRLUA_PATHT MIRANDA_PATH "\\Scripts" #endif -/* modules */ +extern HNETLIBUSER g_hNetlib; +void LoadNetlib(); +void UnloadNetlib(); +void LoadIcons(); +HICON GetIcon(int iconId); +HANDLE GetIconHandle(int iconId); + +int OnOptionsInit(WPARAM wParam, LPARAM); /* utils */ -extern HNETLIBUSER hNetlib; void Log(const char *format, ...); void Log(const wchar_t *format, ...); @@ -80,7 +87,3 @@ int luaM_pcall(lua_State *L, int n = 0, int r = 0); int luaM_getenv(lua_State *L); bool luaM_toboolean(lua_State *L, int idx); - -void InitIcons(); -HICON GetIcon(int iconId); -HANDLE GetIconHandle(int iconId); diff --git a/plugins/MirLua/src/utils.cpp b/plugins/MirLua/src/utils.cpp index f8f15f185f..5704bfc786 100644 --- a/plugins/MirLua/src/utils.cpp +++ b/plugins/MirLua/src/utils.cpp @@ -4,7 +4,7 @@ void Log(const char *format, ...) { va_list args; va_start(args, format); - Netlib_Log(hNetlib, CMStringA().FormatV(format, args)); + Netlib_Log(g_hNetlib, CMStringA().FormatV(format, args)); va_end(args); } @@ -12,7 +12,7 @@ void Log(const wchar_t *format, ...) { va_list args; va_start(args, format); - Netlib_LogW(hNetlib, CMStringW().FormatV(format, args)); + Netlib_LogW(g_hNetlib, CMStringW().FormatV(format, args)); va_end(args); } -- cgit v1.2.3