From 3af4bb5ed2c17d17ecc8073cd233277ed8541cf1 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 18 Jun 2015 15:27:08 +0000 Subject: MirLua: scripts reloading pt.1 git-svn-id: http://svn.miranda-ng.org/main/trunk@14245 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/res/resource.rc | 3 +- plugins/MirLua/src/main.cpp | 42 +++-------------- plugins/MirLua/src/mlua.cpp | 89 ++++++++++++++++++++++++++++--------- plugins/MirLua/src/mlua.h | 13 ++++-- plugins/MirLua/src/mlua_loader.cpp | 43 ++++++++++++++++++ plugins/MirLua/src/mlua_loader.h | 17 +++++++ plugins/MirLua/src/mlua_options.cpp | 89 +++++++++++++++---------------------- plugins/MirLua/src/mlua_options.h | 8 ++-- plugins/MirLua/src/resource.h | 6 ++- plugins/MirLua/src/stdafx.h | 6 ++- 10 files changed, 193 insertions(+), 123 deletions(-) create mode 100644 plugins/MirLua/src/mlua_loader.cpp create mode 100644 plugins/MirLua/src/mlua_loader.h (limited to 'plugins') diff --git a/plugins/MirLua/res/resource.rc b/plugins/MirLua/res/resource.rc index 3cdd4f6afa..f58f20b403 100644 --- a/plugins/MirLua/res/resource.rc +++ b/plugins/MirLua/res/resource.rc @@ -74,7 +74,8 @@ STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD EXSTYLE WS_EX_CONTROLPARENT FONT 8, "MS Shell Dlg", 0, 0, 0x1 BEGIN - CONTROL "",IDC_SCRIPTS,"SysListView32",LVS_REPORT | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP | LVS_NOCOLUMNHEADER,7,20,140,172 + CONTROL "",IDC_SCRIPTS,"SysListView32",LVS_REPORT | LVS_ALIGNLEFT | LVS_NOCOLUMNHEADER | WS_BORDER | WS_TABSTOP,7,7,296,198 + PUSHBUTTON "Reload",IDC_RELOAD,253,214,50,14 END diff --git a/plugins/MirLua/src/main.cpp b/plugins/MirLua/src/main.cpp index 35d572a4c7..618ffb6c67 100644 --- a/plugins/MirLua/src/main.cpp +++ b/plugins/MirLua/src/main.cpp @@ -6,7 +6,7 @@ HINSTANCE g_hInstance; HANDLE g_hCommonFolderPath; HANDLE g_hCustomFolderPath; -CMLua *mLua; +CMLua *g_mLua; HANDLE hConsole = NULL; PLUGININFOEX pluginInfo = @@ -37,33 +37,6 @@ extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD) return &pluginInfo; } -void LoadScripts(const TCHAR *scriptDir) -{ - mLua->AddPath(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->Load(T2Utf(path)); - } - } while (FindNextFile(hFind, &fd)); - FindClose(hFind); - } -} - int OnOptionsInit(WPARAM wParam, LPARAM) { OPTIONSDIALOGPAGE odp = { 0 }; @@ -99,18 +72,13 @@ extern "C" int __declspec(dllexport) Load(void) } } - mLua = new CMLua(); - g_hCommonFolderPath = FoldersRegisterCustomPathT("MirLua", Translate("Common scripts folder"), COMMON_SCRIPTS_PATHT); g_hCustomFolderPath = FoldersRegisterCustomPathT("MirLua", Translate("Custom scripts folder"), CUSTOM_SCRIPTS_PATHT); - TCHAR commonScriptDir[MAX_PATH]; - FoldersGetCustomPathT(g_hCommonFolderPath, commonScriptDir, SIZEOF(commonScriptDir), VARST(COMMON_SCRIPTS_PATHT)); - LoadScripts(commonScriptDir); + g_mLua = new CMLua(); - TCHAR customScriptDir[MAX_PATH]; - FoldersGetCustomPathT(g_hCustomFolderPath, customScriptDir, SIZEOF(customScriptDir), VARST(CUSTOM_SCRIPTS_PATHT)); - LoadScripts(customScriptDir); + CLuaLoader loader(g_mLua); + loader.LoadScripts(); HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded); @@ -123,7 +91,7 @@ extern "C" int __declspec(dllexport) Unload(void) CloseHandle(hConsole); FreeConsole(); - delete mLua; + delete g_mLua; return 0; } diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index 9c5f9a8906..5945947441 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -1,6 +1,16 @@ #include "stdafx.h" CMLua::CMLua() : L(NULL) +{ + Load(); +} + +CMLua::~CMLua() +{ + Unload(); +} + +void CMLua::Load() { L = luaL_newstate(); luaL_openlibs(L); @@ -12,27 +22,72 @@ CMLua::CMLua() : L(NULL) lua_setfield(L, -2, "cpath"); lua_pop(L, 1); + LoadMirandaModules(); +} + +void CMLua::Unload() +{ + if (L) + lua_close(L); +} + +void CMLua::Reload() +{ + /*lua_getglobal(L, "m"); + lua_getfield(L, -1, "OnPreShutdown"); + if (lua_isfunction(L, -1)) + { + lua_pushlightuserdata(L, NULL); + lua_pushlightuserdata(L, NULL); + if (lua_pcall(L, 2, 1, 0)) + printf("%s\n", lua_tostring(L, -1)); + } + lua_pop(L, 1);*/ + Unload(); + + Load(); + /*lua_getglobal(L, "m"); + lua_getfield(L, -1, "OnModulesLoaded"); + if (lua_isfunction(L, -1)) + { + lua_pushlightuserdata(L, NULL); + lua_pushlightuserdata(L, NULL); + if (lua_pcall(L, 2, 1, 0)) + printf("%s\n", lua_tostring(L, -1)); + } + lua_pop(L, 1);*/ +} + +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"); - - Preload(MLUA_DATABASE, luaopen_m_database); - Preload(MLUA_ICOLIB, luaopen_m_icolib); - Preload(MLUA_GENMENU, luaopen_m_genmenu); - 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); } -CMLua::~CMLua() +void CMLua::LoadMirandaModules() { - if(L) - lua_close(L); + 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::AddPath(const char *path) +void CMLua::AddScriptsPath(const char *path) { lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); @@ -43,20 +98,12 @@ void CMLua::AddPath(const char *path) lua_pop(L, 1); } -void CMLua::Load(const char *path) +void CMLua::LoadScript(const char *path) { if (luaL_dofile(L, path)) printf("%s\n", lua_tostring(L, -1)); } -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); -} - WPARAM CMLua::GetWParam(lua_State *L, int idx) { WPARAM wParam = NULL; diff --git a/plugins/MirLua/src/mlua.h b/plugins/MirLua/src/mlua.h index cf1eb7c0fa..fc10545c42 100644 --- a/plugins/MirLua/src/mlua.h +++ b/plugins/MirLua/src/mlua.h @@ -7,15 +7,22 @@ private: lua_State *L; static luaL_Reg coreLib[15]; - void Preload(const char *name, lua_CFunction func); + void LoadModule(const char *name, lua_CFunction func); + + void LoadCoreModule(); + void LoadMirandaModules(); + + void Load(); + void Unload(); public: CMLua(); ~CMLua(); - void AddPath(const char *path); + void Reload(); - void Load(const char *name); + void AddScriptsPath(const char *path); + void LoadScript(const char *name); static WPARAM GetWParam(lua_State *L, int idx); static LPARAM GetLParam(lua_State *L, int idx); diff --git a/plugins/MirLua/src/mlua_loader.cpp b/plugins/MirLua/src/mlua_loader.cpp new file mode 100644 index 0000000000..c3b1d2edf2 --- /dev/null +++ b/plugins/MirLua/src/mlua_loader.cpp @@ -0,0 +1,43 @@ +#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, SIZEOF(scriptDir), VARST(COMMON_SCRIPTS_PATHT)); + LoadScripts(scriptDir); + + FoldersGetCustomPathT(g_hCustomFolderPath, scriptDir, SIZEOF(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 new file mode 100644 index 0000000000..40273ae951 --- /dev/null +++ b/plugins/MirLua/src/mlua_loader.h @@ -0,0 +1,17 @@ +#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_options.cpp b/plugins/MirLua/src/mlua_options.cpp index 63389ef54e..0b06623bde 100644 --- a/plugins/MirLua/src/mlua_options.cpp +++ b/plugins/MirLua/src/mlua_options.cpp @@ -1,8 +1,10 @@ #include "stdafx.h" CLuaOptions::CLuaOptions(int idDialog) : CDlgBase(g_hInstance, idDialog), - m_scripts(this, IDC_SCRIPTS), isScriptListInit(false) + m_scripts(this, IDC_SCRIPTS), isScriptListInit(false), + m_reload(this, IDC_RELOAD) { + m_reload.OnClick = Callback(this, &CLuaOptions::OnReload); } void CLuaOptions::CreateLink(CCtrlData& ctrl, const char *szSetting, BYTE type, DWORD iValue) @@ -15,7 +17,7 @@ void CLuaOptions::CreateLink(CCtrlData& ctrl, const char *szSetting, TCHAR *szVa ctrl.CreateDbLink(MODULE, szSetting, szValue); } -void CLuaOptions::LoadScripts(const TCHAR *scriptDir) +void CLuaOptions::LoadScripts(const TCHAR *scriptDir, int iGroup) { TCHAR searchMask[MAX_PATH]; mir_sntprintf(searchMask, _T("%s\\%s"), scriptDir, _T("*.lua")); @@ -30,10 +32,9 @@ void CLuaOptions::LoadScripts(const TCHAR *scriptDir) { if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - mir_sntprintf(fullPath, _T("%s\\%s"), scriptDir, fd.cFileName); - PathToRelativeT(fullPath, path); - - m_scripts.AddItem(fd.cFileName, -1, NULL, 0); + int iItem = m_scripts.AddItem(fd.cFileName, -1, NULL, iGroup); + if (db_get_b(NULL, MODULE, _T2A(fd.cFileName), 1)) + m_scripts.SetCheckState(iItem, TRUE); } } while (FindNextFile(hFind, &fd)); FindClose(hFind); @@ -44,51 +45,38 @@ void CLuaOptions::OnInitDialog() { CDlgBase::OnInitDialog(); - m_scripts.SetExtendedListViewStyle(LVS_EX_CHECKBOXES); - + m_scripts.SetExtendedListViewStyle(LVS_EX_CHECKBOXES | LVS_EX_INFOTIP); m_scripts.EnableGroupView(TRUE); - m_scripts.AddGroup(0, TranslateT("Common scripts")); - m_scripts.AddGroup(1, TranslateT("Custom scripts")); - m_scripts.AddColumn(0, _T("Script"), 300); - WIN32_FIND_DATA fd; - HANDLE hFind = NULL; - TCHAR scriptDir[MAX_PATH]; - TCHAR searchMask[MAX_PATH]; - + TCHAR scriptDir[MAX_PATH], relativeScriptDir[MAX_PATH], header[MAX_PATH + 100]; FoldersGetCustomPathT(g_hCommonFolderPath, scriptDir, SIZEOF(scriptDir), VARST(COMMON_SCRIPTS_PATHT)); - mir_sntprintf(searchMask, _T("%s\\%s"), scriptDir, _T("*.lua")); - hFind = FindFirstFile(searchMask, &fd); - if (hFind != INVALID_HANDLE_VALUE) - { - do - { - if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) - { - int iItem = m_scripts.AddItem(fd.cFileName, -1, NULL, 0); - if (db_get_b(NULL, MODULE, _T2A(fd.cFileName), 1)) - m_scripts.SetCheckState(iItem, TRUE); - } - } while (FindNextFile(hFind, &fd)); - FindClose(hFind); - } + PathToRelativeT(scriptDir, relativeScriptDir, NULL); + mir_sntprintf(header, _T("%s (%s)"), TranslateT("Common scripts"), relativeScriptDir); + m_scripts.AddGroup(0, header); + LoadScripts(scriptDir, 0); FoldersGetCustomPathT(g_hCustomFolderPath, scriptDir, SIZEOF(scriptDir), VARST(CUSTOM_SCRIPTS_PATHT)); - mir_sntprintf(searchMask, _T("%s\\%s"), scriptDir, _T("*.lua")); - hFind = FindFirstFile(searchMask, &fd); - if (hFind != INVALID_HANDLE_VALUE) + PathToRelativeT(scriptDir, relativeScriptDir, NULL); + mir_sntprintf(header, _T("%s (%s)"), TranslateT("Custom scripts"), relativeScriptDir); + m_scripts.AddGroup(1, header); + LoadScripts(scriptDir, 1); + + isScriptListInit = true; +} + +void CLuaOptions::OnApply() +{ + int count = m_scripts.GetItemCount(); + for (int iItem = 0; iItem < count; iItem++) { - do - { - if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) - { - m_scripts.AddItem(fd.cFileName, -1, NULL, 1); - } - } while (FindNextFile(hFind, &fd)); - FindClose(hFind); + TCHAR fileName[MAX_PATH]; + m_scripts.GetItemText(iItem, 0, fileName, SIZEOF(fileName)); + if (!m_scripts.GetCheckState(iItem)) + db_set_b(NULL, MODULE, _T2A(fileName), 0); + else + db_unset(NULL, MODULE, _T2A(fileName)); } - isScriptListInit = true; } INT_PTR CLuaOptions::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) @@ -114,16 +102,9 @@ INT_PTR CLuaOptions::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) return CDlgBase::DlgProc(msg, wParam, lParam); } -void CLuaOptions::OnApply() +void CLuaOptions::OnReload(CCtrlBase*) { - int count = m_scripts.GetItemCount(); - for (int iItem = 0; iItem < count; iItem++) - { - TCHAR fileName[MAX_PATH]; - m_scripts.GetItemText(iItem, 0, fileName, SIZEOF(fileName)); - if (!m_scripts.GetCheckState(iItem)) - db_set_b(NULL, MODULE, _T2A(fileName), 0); - else - db_unset(NULL, MODULE, _T2A(fileName)); - } + g_mLua->Reload(); + CLuaLoader loader(g_mLua); + loader.LoadScripts(); } \ No newline at end of file diff --git a/plugins/MirLua/src/mlua_options.h b/plugins/MirLua/src/mlua_options.h index edbb574a5c..490a465395 100644 --- a/plugins/MirLua/src/mlua_options.h +++ b/plugins/MirLua/src/mlua_options.h @@ -6,15 +6,17 @@ class CLuaOptions : public CDlgBase private: bool isScriptListInit; CCtrlListView m_scripts; + CCtrlButton m_reload; - void LoadScripts(const TCHAR *scriptDir); + void LoadScripts(const TCHAR *scriptDir, int iGroup = -1); protected: void OnInitDialog(); + void OnApply(); - INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam); + void OnReload(CCtrlBase*); - void OnApply(); + INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam); public: CLuaOptions(int idDialog); diff --git a/plugins/MirLua/src/resource.h b/plugins/MirLua/src/resource.h index 6adee66c1c..612051cbad 100644 --- a/plugins/MirLua/src/resource.h +++ b/plugins/MirLua/src/resource.h @@ -1,12 +1,14 @@ //{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. -// Used by D:\Projects\MirandaNG\plugins\MirLua\res\resource.rc +// Used by d:\Projects\MirandaNG\plugins\MirLua\res\resource.rc // #define IDI_ICON 100 #define IDD_OPTIONS_MAIN 106 #define IDD_OPTIONS 106 #define IDC_LIST1 1011 #define IDC_SCRIPTS 1011 +#define IDC_BUTTON1 1012 +#define IDC_RELOAD 1012 // Next default values for new objects // @@ -14,7 +16,7 @@ #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 105 #define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1012 +#define _APS_NEXT_CONTROL_VALUE 1013 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index f31ee69800..1d9ca47929 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -32,13 +32,15 @@ extern "C" #include "version.h" #include "resource.h" -class CMLua; - #include "mlua.h" +#include "mlua_loader.h" #include "mlua_options.h" #define MODULE "MirLua" + +extern CMLua *g_mLua; + extern HINSTANCE g_hInstance; extern HANDLE g_hCommonFolderPath; -- cgit v1.2.3