From 8be4814cca3be42be785da6a366f3da978a4ab72 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 22 Jun 2015 09:01:42 +0000 Subject: MirLua: refactoring git-svn-id: http://svn.miranda-ng.org/main/trunk@14321 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_core.cpp | 12 +++++- plugins/MirLua/src/mlua.cpp | 60 +++--------------------------- plugins/MirLua/src/mlua.h | 9 ----- plugins/MirLua/src/mlua_loader.cpp | 43 ---------------------- plugins/MirLua/src/mlua_loader.h | 17 --------- plugins/MirLua/src/mlua_module_loader.cpp | 33 +++++++++++++++++ plugins/MirLua/src/mlua_module_loader.h | 18 +++++++++ plugins/MirLua/src/mlua_script_loader.cpp | 61 +++++++++++++++++++++++++++++++ plugins/MirLua/src/mlua_script_loader.h | 20 ++++++++++ plugins/MirLua/src/stdafx.h | 6 ++- 10 files changed, 154 insertions(+), 125 deletions(-) delete mode 100644 plugins/MirLua/src/mlua_loader.cpp delete mode 100644 plugins/MirLua/src/mlua_loader.h create mode 100644 plugins/MirLua/src/mlua_module_loader.cpp create mode 100644 plugins/MirLua/src/mlua_module_loader.h create mode 100644 plugins/MirLua/src/mlua_script_loader.cpp create mode 100644 plugins/MirLua/src/mlua_script_loader.h (limited to 'plugins') diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp index 917694e9e7..ed30486aad 100644 --- a/plugins/MirLua/src/m_core.cpp +++ b/plugins/MirLua/src/m_core.cpp @@ -185,7 +185,7 @@ static int lua_ReplaceVariables(lua_State *L) return 1; } -luaL_Reg CMLua::coreLib[] = +luaL_Reg coreApi[] = { { "CreateHookableEvent", lua_CreateHookableEvent }, { "DestroyHookableEvent", lua_DestroyHookableEvent }, @@ -210,3 +210,13 @@ luaL_Reg CMLua::coreLib[] = { NULL, NULL } }; + +LUAMOD_API int luaopen_m(lua_State *L) +{ + luaL_newlib(L, coreApi); + lua_pushlightuserdata(L, NULL); + lua_setfield(L, -2, "NULL"); + lua_setglobal(L, "m"); + + return 1; +} \ No newline at end of file diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index bf83c8f2c0..ccd843d5be 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -22,13 +22,10 @@ void CMLua::Load() lua_setfield(L, -2, "cpath"); lua_pop(L, 1); - LoadMirandaModules(); + hScriptsLangpack = GetPluginLangId(MIID_LAST, 0); - MUUID last = MIID_LAST; - hScriptsLangpack = GetPluginLangId(last, 0); - - CLuaLoader loader(this); - loader.LoadScripts(); + CLuaModuleLoader::Load(L); + CLuaScriptLoader::Load(L); } void CMLua::Unload() @@ -36,6 +33,9 @@ void CMLua::Unload() if (L) lua_close(L); KillModuleMenus(hScriptsLangpack); + //KillModuleSubclassing + //KillModuleServices + //KillModuleEventHooks } void CMLua::Reload() @@ -44,54 +44,6 @@ void CMLua::Reload() Load(); } -void CMLua::LoadModule(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); -} - -void CMLua::LoadCoreModule() -{ - luaL_newlib(L, coreLib); - lua_pushlightuserdata(L, NULL); - lua_setfield(L, -2, "NULL"); - lua_setglobal(L, "m"); -} - -void CMLua::LoadMirandaModules() -{ - LoadCoreModule(); - - LoadModule(MLUA_DATABASE, luaopen_m_database); - LoadModule(MLUA_ICOLIB, luaopen_m_icolib); - LoadModule(MLUA_GENMENU, luaopen_m_genmenu); - LoadModule(MLUA_MSGBUTTONSBAR, luaopen_m_msg_buttonsbar); - LoadModule(MLUA_POPUP, luaopen_m_popup); - LoadModule(MLUA_TOPTOOLBAR, luaopen_m_toptoolbar); - LoadModule(MLUA_VARIABLES, luaopen_m_variables); -} - -void CMLua::AddScriptsPath(const char *path) -{ - lua_getglobal(L, "package"); - lua_getfield(L, -1, "path"); - const char *oldPath = luaL_checkstring(L, -1); - lua_pop(L, 1); - lua_pushfstring(L, "%s;%s\\?.lua", oldPath, path); - lua_setfield(L, -2, "path"); - lua_pop(L, 1); -} - -void CMLua::LoadScript(const char *path) -{ - if (luaL_dofile(L, path)) - printf("%s\n", lua_tostring(L, -1)); -} - - - int CMLua::HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) { lua_State *L = (lua_State*)obj; diff --git a/plugins/MirLua/src/mlua.h b/plugins/MirLua/src/mlua.h index 51d3a0b5bb..c792c32780 100644 --- a/plugins/MirLua/src/mlua.h +++ b/plugins/MirLua/src/mlua.h @@ -5,12 +5,6 @@ class CMLua { private: lua_State *L; - static luaL_Reg coreLib[15]; - - void LoadModule(const char *name, lua_CFunction func); - - void LoadCoreModule(); - void LoadMirandaModules(); void Load(); void Unload(); @@ -21,9 +15,6 @@ public: void Reload(); - void AddScriptsPath(const char *path); - void LoadScript(const char *name); - static int HookEventObjParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param); }; diff --git a/plugins/MirLua/src/mlua_loader.cpp b/plugins/MirLua/src/mlua_loader.cpp deleted file mode 100644 index 1e62b2653d..0000000000 --- a/plugins/MirLua/src/mlua_loader.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "stdafx.h" - -CLuaLoader::CLuaLoader(CMLua *mLua) : mLua(mLua) -{ -} - -void CLuaLoader::LoadScripts(const TCHAR *scriptDir) -{ - mLua->AddScriptsPath(ptrA(mir_utf8encodeT(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); - if (db_get_b(NULL, MODULE, _T2A(fd.cFileName), 1)) - mLua->LoadScript(T2Utf(path)); - } - } while (FindNextFile(hFind, &fd)); - FindClose(hFind); - } -} - -void CLuaLoader::LoadScripts() -{ - TCHAR scriptDir[MAX_PATH]; - - FoldersGetCustomPathT(g_hCommonFolderPath, scriptDir, _countof(scriptDir), VARST(COMMON_SCRIPTS_PATHT)); - LoadScripts(scriptDir); - - FoldersGetCustomPathT(g_hCustomFolderPath, scriptDir, _countof(scriptDir), VARST(CUSTOM_SCRIPTS_PATHT)); - LoadScripts(scriptDir); -} \ No newline at end of file diff --git a/plugins/MirLua/src/mlua_loader.h b/plugins/MirLua/src/mlua_loader.h deleted file mode 100644 index 40273ae951..0000000000 --- a/plugins/MirLua/src/mlua_loader.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _LUA_LOADER_H_ -#define _LUA_LOADER_H_ - -class CLuaLoader -{ -private: - CMLua *mLua; - - void LoadScripts(const TCHAR *scriptDir); - -public: - CLuaLoader(CMLua *mLua); - - void LoadScripts(); -}; - -#endif //_LUA_LOADER_H_ \ No newline at end of file diff --git a/plugins/MirLua/src/mlua_module_loader.cpp b/plugins/MirLua/src/mlua_module_loader.cpp new file mode 100644 index 0000000000..f25b69abde --- /dev/null +++ b/plugins/MirLua/src/mlua_module_loader.cpp @@ -0,0 +1,33 @@ +#include "stdafx.h" + +CLuaModuleLoader::CLuaModuleLoader(lua_State *L) : L(L) +{ +} + +void CLuaModuleLoader::PreloadModule(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); +} + +void CLuaModuleLoader::LoadModules() +{ + // load core module + luaopen_m(L); + // regirter delay loading of miranda modules + PreloadModule(MLUA_DATABASE, luaopen_m_database); + PreloadModule(MLUA_ICOLIB, luaopen_m_icolib); + PreloadModule(MLUA_GENMENU, luaopen_m_genmenu); + PreloadModule(MLUA_MSGBUTTONSBAR, luaopen_m_msg_buttonsbar); + PreloadModule(MLUA_POPUP, luaopen_m_popup); + PreloadModule(MLUA_TOPTOOLBAR, luaopen_m_toptoolbar); + PreloadModule(MLUA_VARIABLES, luaopen_m_variables); +} + +void CLuaModuleLoader::Load(lua_State *L) +{ + CLuaModuleLoader loader(L); + loader.LoadModules(); +} \ No newline at end of file diff --git a/plugins/MirLua/src/mlua_module_loader.h b/plugins/MirLua/src/mlua_module_loader.h new file mode 100644 index 0000000000..b90e18c892 --- /dev/null +++ b/plugins/MirLua/src/mlua_module_loader.h @@ -0,0 +1,18 @@ +#ifndef _LUA_MODULE_LOADER_H_ +#define _LUA_MODULE_LOADER_H_ + +class CLuaModuleLoader +{ +private: + lua_State *L; + + CLuaModuleLoader(lua_State *L); + + void PreloadModule(const char *name, lua_CFunction loader); + void LoadModules(); + +public: + static void Load(lua_State *L); +}; + +#endif //_LUA_MODULE_LOADER_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 new file mode 100644 index 0000000000..4ea6c73f80 --- /dev/null +++ b/plugins/MirLua/src/mlua_script_loader.cpp @@ -0,0 +1,61 @@ +#include "stdafx.h" + +CLuaScriptLoader::CLuaScriptLoader(lua_State *L) : L(L) +{ +} + +void CLuaScriptLoader::RegisterScriptsFolder(const char *path) +{ + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); + const char *oldPath = luaL_checkstring(L, -1); + lua_pop(L, 1); + lua_pushfstring(L, "%s;%s\\?.lua", oldPath, path); + lua_setfield(L, -2, "path"); + lua_pop(L, 1); +} + +void CLuaScriptLoader::LoadScript(const char *path) +{ + if (luaL_dofile(L, path)) + printf("%s\n", lua_tostring(L, -1)); +} + +void CLuaScriptLoader::LoadScripts(const TCHAR *scriptDir) +{ + RegisterScriptsFolder(ptrA(mir_utf8encodeT(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); + if (db_get_b(NULL, MODULE, _T2A(fd.cFileName), 1)) + LoadScript(T2Utf(path)); + } + } while (FindNextFile(hFind, &fd)); + FindClose(hFind); + } +} + +void CLuaScriptLoader::Load(lua_State *L) +{ + TCHAR scriptDir[MAX_PATH]; + CLuaScriptLoader loader(L); + + FoldersGetCustomPathT(g_hCommonFolderPath, scriptDir, _countof(scriptDir), VARST(COMMON_SCRIPTS_PATHT)); + loader.LoadScripts(scriptDir); + + FoldersGetCustomPathT(g_hCustomFolderPath, scriptDir, _countof(scriptDir), VARST(CUSTOM_SCRIPTS_PATHT)); + loader.LoadScripts(scriptDir); +} \ No newline at end of file diff --git a/plugins/MirLua/src/mlua_script_loader.h b/plugins/MirLua/src/mlua_script_loader.h new file mode 100644 index 0000000000..0d62e86ffc --- /dev/null +++ b/plugins/MirLua/src/mlua_script_loader.h @@ -0,0 +1,20 @@ +#ifndef _LUA_SCRIPT_LOADER_H_ +#define _LUA_SCRIPT_LOADER_H_ + +class CLuaScriptLoader +{ +private: + lua_State *L; + + CLuaScriptLoader(lua_State *L); + + void RegisterScriptsFolder(const char *path); + + void LoadScript(const char *path); + void LoadScripts(const TCHAR *scriptDir); + +public: + static void Load(lua_State *L); +}; + +#endif //_LUA_SCRIPT_LOADER_H_ \ No newline at end of file diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index 9c8b26fd17..ce3e7948c6 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -33,7 +33,8 @@ extern "C" #include "resource.h" #include "mlua.h" -#include "mlua_loader.h" +#include "mlua_module_loader.h" +#include "mlua_script_loader.h" #include "mlua_options.h" #define MODULE "MirLua" @@ -56,6 +57,9 @@ extern HANDLE g_hCustomFolderPath; #define CUSTOM_SCRIPTS_PATHT MIRANDA_USERDATA "\\Scripts" #endif +#define MLUA_CORE "m" +LUAMOD_API int (luaopen_m)(lua_State *L); + #define MLUA_DATABASE "m_database" LUAMOD_API int (luaopen_m_database)(lua_State *L); -- cgit v1.2.3