From 6a6a1d097e043dba142d5cdc485e6b4dbe67e12a Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 7 Jan 2016 10:42:38 +0000 Subject: MirLua: - added script cache on load - refactoring git-svn-id: http://svn.miranda-ng.org/main/trunk@16044 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/res/resource.rc | 4 +- plugins/MirLua/src/m_clist.cpp | 51 +++++++++++------------- plugins/MirLua/src/m_core.cpp | 66 +++++++++++++++---------------- plugins/MirLua/src/m_genmenu.cpp | 34 +++++++++------- plugins/MirLua/src/m_icolib.cpp | 23 +++++------ plugins/MirLua/src/main.cpp | 1 - plugins/MirLua/src/mlua.cpp | 18 +++++---- plugins/MirLua/src/mlua.h | 6 +-- plugins/MirLua/src/mlua_module_loader.cpp | 56 +++++++++++++++----------- plugins/MirLua/src/mlua_module_loader.h | 4 +- plugins/MirLua/src/mlua_options.cpp | 4 +- plugins/MirLua/src/mlua_script.cpp | 35 ++++++++++++---- plugins/MirLua/src/mlua_script.h | 19 ++++++--- plugins/MirLua/src/stdafx.h | 1 - 14 files changed, 181 insertions(+), 141 deletions(-) (limited to 'plugins/MirLua') diff --git a/plugins/MirLua/res/resource.rc b/plugins/MirLua/res/resource.rc index 5255c2d860..017381b5a7 100644 --- a/plugins/MirLua/res/resource.rc +++ b/plugins/MirLua/res/resource.rc @@ -80,9 +80,9 @@ BEGIN CONTROL "",IDC_SCRIPTS,"SysListView32",LVS_REPORT | LVS_SINGLESEL | LVS_ALIGNLEFT | LVS_NOCOLUMNHEADER | WS_BORDER | WS_TABSTOP,8,57,294,169 PUSHBUTTON "Reload all",IDC_RELOAD,226,228,76,14 GROUPBOX "Scripts",IDC_STATIC,0,46,309,203 - CONTROL "Popup on script error",IDC_POPUPONERROR,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,8,10,229,10 + CONTROL "Popup on script error",IDC_POPUPONERROR,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,8,10,294,10 GROUPBOX "General",IDC_STATIC,1,0,308,44 - CONTROL "Popup on obsolete methods",IDC_POPUPONOBSOLETE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,8,21,229,10 + CONTROL "Popup on obsolete methods",IDC_POPUPONOBSOLETE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,8,21,294,10 END diff --git a/plugins/MirLua/src/m_clist.cpp b/plugins/MirLua/src/m_clist.cpp index 5349c20b75..1a43b298ab 100644 --- a/plugins/MirLua/src/m_clist.cpp +++ b/plugins/MirLua/src/m_clist.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" -static int lua_AddMainMenuItem(lua_State *L) +static int clist_AddMainMenuItem(lua_State *L) { if (lua_type(L, 1) != LUA_TTABLE) { @@ -11,21 +11,26 @@ static int lua_AddMainMenuItem(lua_State *L) CMenuItem mi; MakeMenuItem(L, mi); - HGENMENU res = ::Menu_AddMainMenuItem(&mi); + HGENMENU res = Menu_AddMainMenuItem(&mi); lua_pushlightuserdata(L, res); return 1; } -static int lua_BuildMainMenu(lua_State *L) +static int clist_CreateMainMenuRoot(lua_State *L) { - HMENU res = ::Menu_GetMainMenu(); + int hMenuObject = luaL_checkinteger(L, 1); + const char *name = luaL_checkstring(L, 2); + int position = lua_tointeger(L, 3); + HANDLE hIcon = (HANDLE)lua_touserdata(L, 4); + + HGENMENU res = Menu_CreateRoot(MO_MAIN, ptrT(Utf8DecodeT(name)), position, hIcon); lua_pushlightuserdata(L, res); return 1; } -static int lua_AddContactMenuItem(lua_State *L) +static int clist_AddContactMenuItem(lua_State *L) { if (lua_type(L, 1) != LUA_TTABLE) { @@ -38,23 +43,26 @@ static int lua_AddContactMenuItem(lua_State *L) ptrA szProto(mir_utf8decode((char*)lua_tostring(L, 2), NULL)); - HGENMENU res = ::Menu_AddContactMenuItem(&mi, szProto); + HGENMENU res = Menu_AddContactMenuItem(&mi, szProto); lua_pushlightuserdata(L, res); return 1; } -static int lua_BuildContactMenu(lua_State *L) +static int clist_CreateContactMenuRoot(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + int hMenuObject = luaL_checkinteger(L, 1); + const char *name = luaL_checkstring(L, 2); + int position = lua_tointeger(L, 3); + HANDLE hIcon = (HANDLE)lua_touserdata(L, 4); - HMENU res = ::Menu_BuildContactMenu(hContact); + HGENMENU res = Menu_CreateRoot(MO_CONTACT, ptrT(Utf8DecodeT(name)), position, hIcon); lua_pushlightuserdata(L, res); return 1; } -static int lua_AddTrayMenuItem(lua_State *L) +static int clist_AddTrayMenuItem(lua_State *L) { if (lua_type(L, 1) != LUA_TTABLE) { @@ -65,15 +73,7 @@ static int lua_AddTrayMenuItem(lua_State *L) CMenuItem mi; MakeMenuItem(L, mi); - HGENMENU res = ::Menu_AddTrayMenuItem(&mi); - lua_pushlightuserdata(L, res); - - return 1; -} - -static int lua_BuildTrayMenu(lua_State *L) -{ - HMENU res = Menu_BuildTrayMenu(); + HGENMENU res = Menu_AddTrayMenuItem(&mi); lua_pushlightuserdata(L, res); return 1; @@ -81,14 +81,11 @@ static int lua_BuildTrayMenu(lua_State *L) static luaL_Reg clistApi[] = { - { "AddMainMenuItem", lua_AddMainMenuItem }, - { "BuildMainMenu", lua_BuildMainMenu }, - - { "AddContactMenuItem", lua_AddContactMenuItem }, - { "BuildContactMenu", lua_BuildContactMenu }, - - { "AddTrayMenuItem", lua_AddTrayMenuItem }, - { "BuildTrayMenu", lua_BuildTrayMenu }, + { "AddMainMenuItem", clist_AddMainMenuItem }, + { "CreateMainMenuRoot", clist_CreateMainMenuRoot }, + { "AddContactMenuItem", clist_AddContactMenuItem }, + { "CreateContactMenuRoot", clist_CreateContactMenuRoot }, + { "AddTrayMenuItem", clist_AddTrayMenuItem }, { NULL, NULL } }; diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp index fb8211dda8..1c39729333 100644 --- a/plugins/MirLua/src/m_core.cpp +++ b/plugins/MirLua/src/m_core.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" -static int lua_CreateHookableEvent(lua_State *L) +static int core_CreateHookableEvent(lua_State *L) { const char *name = luaL_checkstring(L, 1); @@ -38,7 +38,7 @@ int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) return lua_tointeger(L, 1); } -static int lua_HookEvent(lua_State *L) +static int core_HookEvent(lua_State *L) { const char *name = luaL_checkstring(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); @@ -62,7 +62,7 @@ static int lua_HookEvent(lua_State *L) return 1; } -static int lua_UnhookEvent(lua_State *L) +static int core_UnhookEvent(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HANDLE hEvent = lua_touserdata(L, 1); @@ -83,7 +83,7 @@ static int lua_UnhookEvent(lua_State *L) return 1; } -static int lua_NotifyEventHooks(lua_State *L) +static int core_NotifyEventHooks(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HANDLE hEvent = lua_touserdata(L, 1); @@ -96,7 +96,7 @@ static int lua_NotifyEventHooks(lua_State *L) return 1; } -static int lua_DestroyHookableEvent(lua_State *L) +static int core_DestroyHookableEvent(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HANDLE hEvent = lua_touserdata(L, 1); @@ -126,7 +126,7 @@ INT_PTR CreateServiceFunctionObjParam(void *obj, WPARAM wParam, LPARAM lParam, L return res; } -static int lua_CreateServiceFunction(lua_State *L) +static int core_CreateServiceFunction(lua_State *L) { const char *name = luaL_checkstring(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); @@ -149,7 +149,7 @@ static int lua_CreateServiceFunction(lua_State *L) return 1; } -static int lua_CallService(lua_State *L) +static int core_CallService(lua_State *L) { const char *name = luaL_checkstring(L, 1); WPARAM wParam = luaM_towparam(L, 2); @@ -161,7 +161,7 @@ static int lua_CallService(lua_State *L) return 1; } -static int lua_ServiceExists(lua_State *L) +static int core_ServiceExists(lua_State *L) { const char *name = luaL_checkstring(L, 1); @@ -171,7 +171,7 @@ static int lua_ServiceExists(lua_State *L) return 1; } -static int lua_DestroyServiceFunction(lua_State *L) +static int core_DestroyServiceFunction(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HANDLE hService = lua_touserdata(L, 1); @@ -191,7 +191,7 @@ static int lua_DestroyServiceFunction(lua_State *L) /***********************************************/ -static int lua_IsPluginLoaded(lua_State *L) +static int core_IsPluginLoaded(lua_State *L) { const char *value = lua_tostring(L, 1); @@ -204,17 +204,17 @@ static int lua_IsPluginLoaded(lua_State *L) return 1; } -static int lua_Utf8DecodeA(lua_State *L) +static int core_Utf8DecodeA(lua_State *L) { return luaM_toansi(L); } -static int lua_Utf8DecodeW(lua_State *L) +static int core_Utf8DecodeW(lua_State *L) { return luaM_toucs2(L); } -static int lua_Free(lua_State *L) +static int core_Free(lua_State *L) { if (lua_type(L, 1) == LUA_TLIGHTUSERDATA) { @@ -225,17 +225,17 @@ static int lua_Free(lua_State *L) return 0; } -static int lua_Translate(lua_State *L) +static int core_Translate(lua_State *L) { char *what = (char*)luaL_checkstring(L, 1); ptrT value(mir_utf8decodeT(what)); - lua_pushstring(L, T2Utf(TranslateW_LP(value, hScriptsLangpack))); + lua_pushstring(L, T2Utf(TranslateW_LP(value, hLangpack))); return 1; } -static int lua_ReplaceVariables(lua_State *L) +static int core_ReplaceVariables(lua_State *L) { char *what = (char*)luaL_checkstring(L, 1); @@ -245,7 +245,7 @@ static int lua_ReplaceVariables(lua_State *L) return 1; } -static int lua_GetFullPath(lua_State *L) +static int core_GetFullPath(lua_State *L) { TCHAR path[MAX_PATH]; GetModuleFileName(NULL, path, MAX_PATH); @@ -257,31 +257,31 @@ static int lua_GetFullPath(lua_State *L) luaL_Reg coreApi[] = { - { "CreateHookableEvent", lua_CreateHookableEvent }, - { "DestroyHookableEvent", lua_DestroyHookableEvent }, + { "CreateHookableEvent", core_CreateHookableEvent }, + { "DestroyHookableEvent", core_DestroyHookableEvent }, - { "NotifyEventHooks", lua_NotifyEventHooks }, + { "NotifyEventHooks", core_NotifyEventHooks }, - { "HookEvent", lua_HookEvent }, - { "UnhookEvent", lua_UnhookEvent }, + { "HookEvent", core_HookEvent }, + { "UnhookEvent", core_UnhookEvent }, - { "CreateServiceFunction", lua_CreateServiceFunction }, - //{ "DestroyServiceFunction", lua_DestroyServiceFunction }, + { "CreateServiceFunction", core_CreateServiceFunction }, + //{ "DestroyServiceFunction", core_DestroyServiceFunction }, - { "ServiceExists", lua_ServiceExists }, - { "CallService", lua_CallService }, + { "ServiceExists", core_ServiceExists }, + { "CallService", core_CallService }, - { "IsPluginLoaded", lua_IsPluginLoaded }, + { "IsPluginLoaded", core_IsPluginLoaded }, - { "Utf8DecodeA", lua_Utf8DecodeA }, - { "Utf8DecodeW", lua_Utf8DecodeW }, + { "Utf8DecodeA", core_Utf8DecodeA }, + { "Utf8DecodeW", core_Utf8DecodeW }, - { "Free", lua_Free }, + { "Free", core_Free }, - { "Translate", lua_Translate }, - { "ReplaceVariables", lua_ReplaceVariables }, + { "Translate", core_Translate }, + { "ReplaceVariables", core_ReplaceVariables }, - { "GetFullPath", lua_GetFullPath }, + { "GetFullPath", core_GetFullPath }, { "NULL", NULL }, { "INVALID_HANDLE_VALUE", NULL }, diff --git a/plugins/MirLua/src/m_genmenu.cpp b/plugins/MirLua/src/m_genmenu.cpp index 25252f2d7b..7d25468980 100644 --- a/plugins/MirLua/src/m_genmenu.cpp +++ b/plugins/MirLua/src/m_genmenu.cpp @@ -2,7 +2,7 @@ void MakeMenuItem(lua_State *L, CMenuItem &mi) { - mi.hLangpack = hScriptsLangpack; + mi.hLangpack = g_mLua->GetHLangpack(); lua_getfield(L, -1, "Flags"); mi.flags = lua_tointeger(L, -1); @@ -38,8 +38,10 @@ void MakeMenuItem(lua_State *L, CMenuItem &mi) lua_pop(L, 1); } -static int lua_CreateRoot(lua_State *L) +static int genmenu_CreateRoot(lua_State *L) { + ObsoleteMethod(L, "use m_clist module instead"); + int hMenuObject = luaL_checkinteger(L, 1); const char *name = luaL_checkstring(L, 2); int position = lua_tointeger(L, 3); @@ -51,8 +53,10 @@ static int lua_CreateRoot(lua_State *L) return 1; } -static int lua_AddMenuItem(lua_State *L) +static int genmenu_AddMenuItem(lua_State *L) { + ObsoleteMethod(L, "use m_clist module instead"); + int hMenuObject = luaL_checkinteger(L, 1); if (lua_type(L, 2) != LUA_TTABLE) @@ -70,7 +74,7 @@ static int lua_AddMenuItem(lua_State *L) return 1; } -static int lua_ModifyMenuItem(lua_State *L) +static int genmenu_ModifyMenuItem(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); @@ -86,7 +90,7 @@ static int lua_ModifyMenuItem(lua_State *L) return 1; } -static int lua_ShowMenuItem(lua_State *L) +static int genmenu_ShowMenuItem(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); @@ -97,7 +101,7 @@ static int lua_ShowMenuItem(lua_State *L) return 0; } -static int lua_EnableMenuItem(lua_State *L) +static int genmenu_EnableMenuItem(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); @@ -108,7 +112,7 @@ static int lua_EnableMenuItem(lua_State *L) return 0; } -static int lua_CheckMenuItem(lua_State *L) +static int genmenu_CheckMenuItem(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); @@ -119,7 +123,7 @@ static int lua_CheckMenuItem(lua_State *L) return 0; } -static int lua_RemoveMenuItem(lua_State *L) +static int genmenu_RemoveMenuItem(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); @@ -132,13 +136,13 @@ static int lua_RemoveMenuItem(lua_State *L) static luaL_Reg genmenuApi[] = { - { "CreateRoot", lua_CreateRoot }, - { "AddMenuItem", lua_AddMenuItem }, - { "ModifyMenuItem", lua_ModifyMenuItem }, - { "ShowMenuItem", lua_ShowMenuItem }, - { "EnableMenuItem", lua_EnableMenuItem }, - { "CheckMenuItem", lua_CheckMenuItem }, - { "RemoveMenuItem", lua_RemoveMenuItem }, + { "CreateRoot", genmenu_CreateRoot }, + { "AddMenuItem", genmenu_AddMenuItem }, + { "ModifyMenuItem", genmenu_ModifyMenuItem }, + { "ShowMenuItem", genmenu_ShowMenuItem }, + { "EnableMenuItem", genmenu_EnableMenuItem }, + { "CheckMenuItem", genmenu_CheckMenuItem }, + { "RemoveMenuItem", genmenu_RemoveMenuItem }, //{ "MO_MAIN", NULL }, //{ "MO_CONTACT", NULL }, diff --git a/plugins/MirLua/src/m_icolib.cpp b/plugins/MirLua/src/m_icolib.cpp index eea44838d4..de5195341d 100644 --- a/plugins/MirLua/src/m_icolib.cpp +++ b/plugins/MirLua/src/m_icolib.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" -static int lua_AddIcon(lua_State *L) +static int icolib_AddIcon(lua_State *L) { const char *name = luaL_checkstring(L, 1); ptrT description(mir_utf8decodeT(luaL_checkstring(L, 2))); @@ -21,40 +21,37 @@ static int lua_AddIcon(lua_State *L) si.defaultFile.t = filePath; si.hDefaultIcon = GetIcon(IDI_SCRIPT); - HANDLE res = ::IcoLib_AddIcon(&si, hScriptsLangpack); + HANDLE res = IcoLib_AddIcon(&si, g_mLua->GetHLangpack()); lua_pushlightuserdata(L, res); return 1; } -static int lua_GetIcon(lua_State *L) +static int icolib_GetIcon(lua_State *L) { const char *name = luaL_checkstring(L, 1); - HANDLE res = ::IcoLib_GetIconHandle(name); + HANDLE res = IcoLib_GetIconHandle(name); lua_pushlightuserdata(L, res); return 1; } -static int lua_RemoveIcon(lua_State *L) +static int icolib_RemoveIcon(lua_State *L) { if (lua_isuserdata(L, 1)) - ::IcoLib_RemoveIconByHandle(lua_touserdata(L, 1)); + IcoLib_RemoveIconByHandle(lua_touserdata(L, 1)); else if (lua_isstring(L, 1)) - ::IcoLib_RemoveIcon(luaL_checkstring(L, 1)); + IcoLib_RemoveIcon(luaL_checkstring(L, 1)); return 0; } static luaL_Reg icolibApi[] = { - { "AddIcon", lua_AddIcon }, - { "Add", lua_AddIcon }, - { "GetIcon", lua_GetIcon }, - { "Get", lua_GetIcon }, - { "RemoveIcon", lua_RemoveIcon }, - { "Remove", lua_RemoveIcon }, + { "AddIcon", icolib_AddIcon }, + { "GetIcon", icolib_GetIcon }, + { "RemoveIcon", icolib_RemoveIcon }, { NULL, NULL } }; diff --git a/plugins/MirLua/src/main.cpp b/plugins/MirLua/src/main.cpp index e1b9950fc6..c6a83765b9 100644 --- a/plugins/MirLua/src/main.cpp +++ b/plugins/MirLua/src/main.cpp @@ -1,7 +1,6 @@ #include "stdafx.h" int hLangpack; -int hScriptsLangpack; HINSTANCE g_hInstance; diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index dc0699182e..1dc4ef440d 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -10,6 +10,8 @@ static int CompareScripts(const CMLuaScript* p1, const CMLuaScript* p2) CMLua::CMLua() : L(NULL), Scripts(10, CompareScripts) { + MUUID muidLast = MIID_LAST; + hLangpack = GetPluginLangId(muidLast, 0); } CMLua::~CMLua() @@ -17,6 +19,11 @@ CMLua::~CMLua() Unload(); } +const int CMLua::GetHLangpack() const +{ + return hLangpack; +} + void CMLua::SetPaths() { TCHAR path[MAX_PATH]; @@ -53,9 +60,6 @@ void CMLua::Load() lua_atpanic(L, luaM_atpanic); - MUUID muidLast = MIID_LAST; - hScriptsLangpack = GetPluginLangId(muidLast, 0); - Log("Loading miranda modules"); CLuaModuleLoader::Load(L); CLuaScriptLoader::Load(L); @@ -77,10 +81,10 @@ void CMLua::Unload() ::KillModuleMBButtons(); ::KillModuleTTBButton(); - ::KillModuleIcons(hScriptsLangpack); - ::KillModuleSounds(hScriptsLangpack); - ::KillModuleMenus(hScriptsLangpack); - ::KillModuleHotkeys(hScriptsLangpack); + ::KillModuleIcons(hLangpack); + ::KillModuleSounds(hLangpack); + ::KillModuleMenus(hLangpack); + ::KillModuleHotkeys(hLangpack); ::KillObjectEventHooks(L); ::KillObjectServices(L); diff --git a/plugins/MirLua/src/mlua.h b/plugins/MirLua/src/mlua.h index 33e973956e..f4ae7237ca 100644 --- a/plugins/MirLua/src/mlua.h +++ b/plugins/MirLua/src/mlua.h @@ -14,15 +14,13 @@ class CMLua { private: lua_State *L; + int hLangpack; void SetPaths(); static void KillLuaRefs(); public: - //static LIST Hooks; - //static LIST Events; - //static LIST Services; static LIST HookRefs; static LIST ServiceRefs; @@ -31,6 +29,8 @@ public: CMLua(); ~CMLua(); + const int GetHLangpack() const; + void Load(); void Unload(); diff --git a/plugins/MirLua/src/mlua_module_loader.cpp b/plugins/MirLua/src/mlua_module_loader.cpp index bd5074a592..d48c600de1 100644 --- a/plugins/MirLua/src/mlua_module_loader.cpp +++ b/plugins/MirLua/src/mlua_module_loader.cpp @@ -4,7 +4,22 @@ CLuaModuleLoader::CLuaModuleLoader(lua_State *L) : L(L) { } -void CLuaModuleLoader::PreloadModule(const char *name, lua_CFunction loader) +void CLuaModuleLoader::Load(const char *name, lua_CFunction loader) +{ + luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, -1, name); + if (lua_toboolean(L, -1)) + Log("Module %s will be replaced with new one"); + lua_pop(L, 1); + lua_pushcfunction(L, loader); + lua_pushstring(L, name); + luaM_pcall(L, 1, 1); + lua_setfield(L, -2, name); + lua_pop(L, 1); + lua_pop(L, 1); +} + +void CLuaModuleLoader::Preload(const char *name, lua_CFunction loader) { luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); lua_pushcfunction(L, loader); @@ -14,29 +29,24 @@ void CLuaModuleLoader::PreloadModule(const char *name, lua_CFunction loader) void CLuaModuleLoader::LoadModules() { - // regirter delay loading of miranda modules - PreloadModule(MLUA_CORE, luaopen_m_core); - PreloadModule(MLUA_CLIST, luaopen_m_clist); - PreloadModule(MLUA_DATABASE, luaopen_m_database); - PreloadModule(MLUA_ICOLIB, luaopen_m_icolib); - PreloadModule(MLUA_GENMENU, luaopen_m_genmenu); - PreloadModule(MLUA_HOTKEYS, luaopen_m_hotkeys); - PreloadModule(MLUA_MESSAGE, luaopen_m_message); - PreloadModule(MLUA_MSGBUTTONSBAR, luaopen_m_msg_buttonsbar); - PreloadModule(MLUA_POPUP, luaopen_m_popup); - PreloadModule(MLUA_PROTOCOLS, luaopen_m_protocols); - PreloadModule(MLUA_SCHEDULE, luaopen_m_schedule); - PreloadModule(MLUA_SOUNDS, luaopen_m_sounds); - PreloadModule(MLUA_TOPTOOLBAR, luaopen_m_toptoolbar); - PreloadModule(MLUA_VARIABLES, luaopen_m_variables); - PreloadModule(MLUA_WINDOWS, luaopen_m_windows); - // load m_core module - lua_pushglobaltable(L); - lua_getfield(L, -1, "require"); - lua_pushstring(L, "m_core"); - luaM_pcall(L, 1, 1); - lua_pop(L, 2); + Load(MLUA_CORE, luaopen_m_core); + // load all internal modules + Preload(MLUA_CLIST, luaopen_m_clist); + Preload(MLUA_DATABASE, luaopen_m_database); + Preload(MLUA_ICOLIB, luaopen_m_icolib); + Preload(MLUA_GENMENU, luaopen_m_genmenu); + Preload(MLUA_HOTKEYS, luaopen_m_hotkeys); + Preload(MLUA_MESSAGE, luaopen_m_message); + Preload(MLUA_PROTOCOLS, luaopen_m_protocols); + Preload(MLUA_SCHEDULE, luaopen_m_schedule); + Preload(MLUA_SOUNDS, luaopen_m_sounds); + // regirter delay loading of other modules + Preload(MLUA_MSGBUTTONSBAR, luaopen_m_msg_buttonsbar); + Preload(MLUA_POPUP, luaopen_m_popup); + Preload(MLUA_TOPTOOLBAR, luaopen_m_toptoolbar); + Preload(MLUA_VARIABLES, luaopen_m_variables); + Preload(MLUA_WINDOWS, luaopen_m_windows); } void CLuaModuleLoader::Load(lua_State *L) diff --git a/plugins/MirLua/src/mlua_module_loader.h b/plugins/MirLua/src/mlua_module_loader.h index b90e18c892..6ce4e16bd8 100644 --- a/plugins/MirLua/src/mlua_module_loader.h +++ b/plugins/MirLua/src/mlua_module_loader.h @@ -8,7 +8,9 @@ private: CLuaModuleLoader(lua_State *L); - void PreloadModule(const char *name, lua_CFunction loader); + void Load(const char *name, lua_CFunction loader); + void Preload(const char *name, lua_CFunction loader); + void LoadModules(); public: diff --git a/plugins/MirLua/src/mlua_options.cpp b/plugins/MirLua/src/mlua_options.cpp index 9d44bbde83..da62cef1d1 100644 --- a/plugins/MirLua/src/mlua_options.cpp +++ b/plugins/MirLua/src/mlua_options.cpp @@ -59,8 +59,8 @@ void CLuaOptions::LoadScripts() { for (int i = 0; i < g_mLua->Scripts.getCount(); i++) { - CMLuaScript* script = g_mLua->Scripts[i]; - TCHAR* fileName = NEWTSTR_ALLOCA(script->GetFileName()); + CMLuaScript *script = g_mLua->Scripts[i]; + TCHAR *fileName = NEWTSTR_ALLOCA(script->GetFileName()); int iItem = m_scripts.AddItem(fileName, -1, (LPARAM)script); if (db_get_b(NULL, MODULE, _T2A(fileName), 1)) m_scripts.SetCheckState(iItem, TRUE); diff --git a/plugins/MirLua/src/mlua_script.cpp b/plugins/MirLua/src/mlua_script.cpp index 8f76f864a4..3d99429ec2 100644 --- a/plugins/MirLua/src/mlua_script.cpp +++ b/plugins/MirLua/src/mlua_script.cpp @@ -1,15 +1,17 @@ #include "stdafx.h" -CMLuaScript::CMLuaScript(lua_State *L, const TCHAR* path) - : L(L), unloadRef(0) +CMLuaScript::CMLuaScript(lua_State *L, const TCHAR *path) + : L(L), status(None), unloadRef(0) { mir_tstrcpy(filePath, path); fileName = _tcsrchr(filePath, '\\') + 1; - size_t length = mir_tstrlen(fileName) - 3; + TCHAR *dot = _tcsrchr(fileName, '.'); - ptrT name((TCHAR*)mir_calloc(sizeof(TCHAR) * length)); - mir_tstrncpy(name, fileName, mir_tstrlen(fileName) - 3); + size_t length = mir_tstrlen(fileName) - mir_tstrlen(dot); + + ptrT name((TCHAR*)mir_calloc(sizeof(TCHAR) * (length + 1))); + mir_tstrncpy(name, fileName, length); moduleName = mir_utf8encodeT(name); } @@ -34,6 +36,11 @@ const TCHAR* CMLuaScript::GetFileName() const return fileName; } +const CMLuaScript::Status CMLuaScript::GetStatus() const +{ + return status; +} + bool CMLuaScript::Load() { if (luaL_loadfile(L, T2Utf(filePath))) @@ -45,7 +52,17 @@ bool CMLuaScript::Load() if (luaM_pcall(L, 0, 1)) return false; - isLoaded = true; + if (lua_isnoneornil(L, -1)) + return true; + + luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, -1, moduleName); + if (lua_toboolean(L, -1)) + Log("Module %s will be replaced with new one"); + lua_pop(L, 1); + lua_pushvalue(L, -2); + lua_setfield(L, -2, moduleName); + lua_pop(L, 1); if (!lua_istable(L, -1)) return true; @@ -64,18 +81,20 @@ bool CMLuaScript::Load() } lua_pop(L, 1); + lua_pop(L, 1); + return true; } void CMLuaScript::Unload() { - if (isLoaded && unloadRef) + if (status == Loaded && unloadRef) { lua_rawgeti(L, LUA_REGISTRYINDEX, unloadRef); if (lua_isfunction(L, -1)) luaM_pcall(L); luaL_unref(L, LUA_REGISTRYINDEX, unloadRef); - isLoaded = false; + status == None; } luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); diff --git a/plugins/MirLua/src/mlua_script.h b/plugins/MirLua/src/mlua_script.h index 38e207549d..76731395fb 100644 --- a/plugins/MirLua/src/mlua_script.h +++ b/plugins/MirLua/src/mlua_script.h @@ -3,24 +3,33 @@ class CMLuaScript { +public: + enum Status + { + None, + Loaded, + Failed + }; + private: lua_State *L; - char* moduleName; - TCHAR* fileName; + char *moduleName; + TCHAR *fileName; TCHAR filePath[MAX_PATH]; - bool isLoaded; + Status status; int unloadRef; public: - CMLuaScript(lua_State *L, const TCHAR* path); + CMLuaScript(lua_State *L, const TCHAR *path); ~CMLuaScript(); const char* GetModuleName() const; const TCHAR* GetFilePath() const; const TCHAR* GetFileName() const; - const int GetGroup() const; + + const Status GetStatus() const; bool Load(); void Unload(); diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index db8a0b2e48..9282c6a77a 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -50,7 +50,6 @@ class CMLuaScript; #define MODULE "MirLua" -extern int hScriptsLangpack; extern HINSTANCE g_hInstance; extern CMLua *g_mLua; -- cgit v1.2.3