From cf39a24da2375575d0f93b95c7ae258aae1658a7 Mon Sep 17 00:00:00 2001 From: aunsane Date: Sun, 20 May 2018 17:24:28 +0300 Subject: MirLua: no need to keep two instances of CMPlugin --- plugins/MirLua/src/main.cpp | 13 ++- plugins/MirLua/src/mlua.cpp | 139 ------------------------------ plugins/MirLua/src/mlua.h | 27 ------ plugins/MirLua/src/mlua_options.cpp | 11 +-- plugins/MirLua/src/mlua_options.h | 3 +- plugins/MirLua/src/mlua_script_loader.cpp | 2 +- plugins/MirLua/src/mplugin.cpp | 139 ++++++++++++++++++++++++++++++ plugins/MirLua/src/mplugin.h | 24 ++++++ plugins/MirLua/src/stdafx.h | 9 +- 9 files changed, 177 insertions(+), 190 deletions(-) delete mode 100644 plugins/MirLua/src/mlua.cpp delete mode 100644 plugins/MirLua/src/mlua.h create mode 100644 plugins/MirLua/src/mplugin.cpp create mode 100644 plugins/MirLua/src/mplugin.h diff --git a/plugins/MirLua/src/main.cpp b/plugins/MirLua/src/main.cpp index f65f6293ad..b38ed43347 100644 --- a/plugins/MirLua/src/main.cpp +++ b/plugins/MirLua/src/main.cpp @@ -3,8 +3,6 @@ int &hLangpack(g_plugin.m_hLang); CMPlugin g_plugin; -CMLua *g_mLua; - HANDLE g_hCLibsFolder; HANDLE g_hScriptsFolder; @@ -27,9 +25,9 @@ PLUGININFOEX pluginInfoEx = }; -CMPlugin::CMPlugin() : +/*CMPlugin::CMPlugin() : PLUGIN(MODULENAME, pluginInfoEx) -{} +{}*/ extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD) { @@ -46,7 +44,7 @@ int OnOptionsInit(WPARAM wParam, LPARAM) odp.szGroup.w = LPGENW("Services"); odp.szTitle.w = L"Lua"; odp.szTab.w = LPGENW("Scripts"); - odp.pDialog = new CMLuaOptions(g_mLua); + odp.pDialog = new CMLuaOptions(); Options_AddPage(wParam, &odp); return 0; } @@ -75,8 +73,7 @@ extern "C" int __declspec(dllexport) Load(void) hRecvMessage = CreateHookableEvent(MODULENAME PSR_MESSAGE); CreateProtoServiceFunction(MODULENAME, PSR_MESSAGE, FilterRecvMessage); - g_mLua = new CMLua(); - g_mLua->Load(); + g_plugin.Load(); HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded); return 0; @@ -86,7 +83,7 @@ extern "C" int __declspec(dllexport) Load(void) extern "C" int __declspec(dllexport) Unload(void) { - delete g_mLua; + g_plugin.Unload(); if (hNetlib) { Netlib_CloseHandle(hNetlib); diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp deleted file mode 100644 index 169dcb6a91..0000000000 --- a/plugins/MirLua/src/mlua.cpp +++ /dev/null @@ -1,139 +0,0 @@ -#include "stdafx.h" - -extern PLUGININFOEX pluginInfoEx; - -int hMLuaLangpack; - -CMLua::CMLua() - : PLUGIN(MODULENAME, pluginInfoEx), - L(nullptr), - Scripts(1) -{ - MUUID muidLast = MIID_LAST; - hMLuaLangpack = GetPluginLangId(muidLast, 0); - - CreatePluginService(MS_LUA_CALL, &CMLua::Call); - CreatePluginService(MS_LUA_EXEC, &CMLua::Exec); - CreatePluginService(MS_LUA_EVAL, &CMLua::Eval); -} - -CMLua::~CMLua() -{ - Unload(); -} - -void CMLua::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); -} - -void CMLua::Unload() -{ - Log("Unloading lua engine"); - - Scripts.destroy(); - - KillModuleIcons(hMLuaLangpack); - KillModuleSounds(hMLuaLangpack); - KillModuleMenus(hMLuaLangpack); - KillModuleHotkeys(hMLuaLangpack); - - KillObjectEventHooks(L); - KillObjectServices(L); - - lua_close(L); -} - -void CMLua::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 CMLua::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 CMLua::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 CMLua::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/mlua.h b/plugins/MirLua/src/mlua.h deleted file mode 100644 index d3f9cc3acd..0000000000 --- a/plugins/MirLua/src/mlua.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef _LUA_CORE_H_ -#define _LUA_CORE_H_ - -class CMLua : public PLUGIN -{ - friend class CMLuaOptions; - -private: - lua_State *L; - - void Unload(); - - INT_PTR __cdecl Eval(WPARAM, LPARAM); - INT_PTR __cdecl Call(WPARAM, LPARAM); - INT_PTR __cdecl Exec(WPARAM, LPARAM); - -public: - OBJLIST Scripts; - - CMLua(); - ~CMLua(); - - void Load(); - void Reload(); -}; - -#endif //_LUA_CORE_H_ diff --git a/plugins/MirLua/src/mlua_options.cpp b/plugins/MirLua/src/mlua_options.cpp index 226a925fd9..3c2dade81b 100644 --- a/plugins/MirLua/src/mlua_options.cpp +++ b/plugins/MirLua/src/mlua_options.cpp @@ -1,11 +1,12 @@ #include "stdafx.h" -CMLuaOptions::CMLuaOptions(CMLua *mLua) +CMLuaOptions::CMLuaOptions() : CPluginDlgBase(g_plugin, IDD_OPTIONS, MODULENAME), - m_mLua(mLua), isScriptListInit(false), + isScriptListInit(false), m_popupOnError(this, IDC_POPUPONERROR), m_popupOnObsolete(this, IDC_POPUPONOBSOLETE), - m_scripts(this, IDC_SCRIPTS), m_reload(this, IDC_RELOAD) + m_scripts(this, IDC_SCRIPTS), + m_reload(this, IDC_RELOAD) { CreateLink(m_popupOnError, "PopupOnError", DBVT_BYTE, 1); CreateLink(m_popupOnObsolete, "PopupOnObsolete", DBVT_BYTE, 1); @@ -16,7 +17,7 @@ CMLuaOptions::CMLuaOptions(CMLua *mLua) void CMLuaOptions::LoadScripts() { - for (auto &script : m_mLua->Scripts) { + for (auto &script : g_plugin.Scripts) { wchar_t *fileName = NEWWSTR_ALLOCA(script->GetFileName()); int iIcon = script->GetStatus() - 1; int iItem = m_scripts.AddItem(fileName, iIcon, (LPARAM)script); @@ -124,7 +125,7 @@ void CMLuaOptions::OnReload(CCtrlBase*) { isScriptListInit = false; m_scripts.DeleteAllItems(); - m_mLua->Reload(); + g_plugin.Reload(); LoadScripts(); isScriptListInit = true; } diff --git a/plugins/MirLua/src/mlua_options.h b/plugins/MirLua/src/mlua_options.h index 2e3b4ff7f7..373827fc42 100644 --- a/plugins/MirLua/src/mlua_options.h +++ b/plugins/MirLua/src/mlua_options.h @@ -4,7 +4,6 @@ class CMLuaOptions : public CPluginDlgBase { private: - CMLua *m_mLua; bool isScriptListInit; CCtrlCheck m_popupOnError; @@ -25,7 +24,7 @@ protected: INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam); public: - CMLuaOptions(CMLua *mLua); + CMLuaOptions(); }; #endif //_LUA_OPTIONS_H_ \ No newline at end of file diff --git a/plugins/MirLua/src/mlua_script_loader.cpp b/plugins/MirLua/src/mlua_script_loader.cpp index 95701b4559..0092cca372 100644 --- a/plugins/MirLua/src/mlua_script_loader.cpp +++ b/plugins/MirLua/src/mlua_script_loader.cpp @@ -29,7 +29,7 @@ void CMLuaScriptLoader::LoadScript(const wchar_t *scriptDir, const wchar_t *file PathToRelativeW(fullPath, path); CMLuaScript *script = new CMLuaScript(L, path); - g_mLua->Scripts.insert(script); + g_plugin.Scripts.insert(script); if (!script->IsEnabled()) { Log(L"%s:PASS", path); diff --git a/plugins/MirLua/src/mplugin.cpp b/plugins/MirLua/src/mplugin.cpp new file mode 100644 index 0000000000..b688876fce --- /dev/null +++ b/plugins/MirLua/src/mplugin.cpp @@ -0,0 +1,139 @@ +#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); + + CreatePluginService(MS_LUA_CALL, &CMPlugin::Call); + CreatePluginService(MS_LUA_EXEC, &CMPlugin::Exec); + CreatePluginService(MS_LUA_EVAL, &CMPlugin::Eval); +} + +CMPlugin::~CMPlugin() +{ + Unload(); +} + +void 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); +} + +void CMPlugin::Unload() +{ + Log("Unloading lua engine"); + + Scripts.destroy(); + + KillModuleIcons(hMLuaLangpack); + KillModuleSounds(hMLuaLangpack); + KillModuleMenus(hMLuaLangpack); + KillModuleHotkeys(hMLuaLangpack); + + KillObjectEventHooks(L); + KillObjectServices(L); + + lua_close(L); +} + +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 new file mode 100644 index 0000000000..8f3a847c5f --- /dev/null +++ b/plugins/MirLua/src/mplugin.h @@ -0,0 +1,24 @@ +#pragma once + +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(); + + void Load(); + void Unload(); + void Reload(); +}; diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index f97ecd09de..06d1de2056 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -35,7 +35,7 @@ class CMLuaScript; -#include "mlua.h" +#include "mplugin.h" #include "mlua_environment.h" #include "mlua_script.h" #include "mlua_function_loader.h" @@ -46,13 +46,6 @@ class CMLuaScript; #define MODULENAME "MirLua" -struct CMPlugin : public PLUGIN -{ - CMPlugin(); -}; - -extern CMLua *g_mLua; - extern int hMLuaLangpack; extern HANDLE g_hCLibsFolder; -- cgit v1.2.3