From 90cac394f9e49fb2ccc5e588dd78e093e0510c4e Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 2 Jan 2016 14:34:22 +0000 Subject: MirLua: added setting c libs path via folders plugin git-svn-id: http://svn.miranda-ng.org/main/trunk@15992 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/main.cpp | 6 ++++-- plugins/MirLua/src/mlua.cpp | 24 ++++++++++++++++++------ plugins/MirLua/src/mlua.h | 2 ++ plugins/MirLua/src/mlua_script_loader.cpp | 27 +++++---------------------- plugins/MirLua/src/mlua_script_loader.h | 4 +--- plugins/MirLua/src/stdafx.h | 13 ++++++++++--- 6 files changed, 40 insertions(+), 36 deletions(-) (limited to 'plugins') diff --git a/plugins/MirLua/src/main.cpp b/plugins/MirLua/src/main.cpp index fabad6a287..4652610222 100644 --- a/plugins/MirLua/src/main.cpp +++ b/plugins/MirLua/src/main.cpp @@ -7,7 +7,8 @@ HINSTANCE g_hInstance; CMLua *g_mLua; -HANDLE g_hCommonScriptFolder; +HANDLE g_hCLibsFolder; +HANDLE g_hScriptsFolder; HANDLE hNetlib = NULL; @@ -41,7 +42,8 @@ extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD) int OnModulesLoaded(WPARAM, LPARAM) { - g_hCommonScriptFolder = FoldersRegisterCustomPathT(MODULE, Translate("Common scripts folder"), COMMON_SCRIPTS_PATHT); + g_hCLibsFolder = FoldersRegisterCustomPathT(MODULE, Translate("C libs folder"), MIRLUA_CPATHT); + g_hScriptsFolder = FoldersRegisterCustomPathT(MODULE, Translate("Scripts folder"), MIRLUA_PATHT); HookEvent(ME_OPT_INITIALISE, CLuaOptions::OnOptionsInit); diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index 81dbc6f612..50ea5070f8 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -17,6 +17,23 @@ CMLua::~CMLua() Unload(); } +void CMLua::SetPaths() +{ + TCHAR path[MAX_PATH]; + + lua_getglobal(L, "package"); + + FoldersGetCustomPathT(g_hScriptsFolder, path, _countof(path), VARST(MIRLUA_CPATHT)); + lua_pushfstring(L, "%s\\?.dll", ptrA(mir_utf8encodeT(path))); + lua_setfield(L, -2, "cpath"); + + FoldersGetCustomPathT(g_hScriptsFolder, path, _countof(path), VARST(MIRLUA_PATHT)); + lua_pushfstring(L, "%s\\?.lua", ptrA(mir_utf8encodeT(path))); + lua_setfield(L, -2, "path"); + + lua_pop(L, 1); +} + void CMLua::Load() { CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)"Loading lua engine"); @@ -24,12 +41,7 @@ void CMLua::Load() CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)"Loading std modules"); luaL_openlibs(L); - lua_getglobal(L, "package"); - lua_pushliteral(L, ""); - lua_setfield(L, -2, "path"); - lua_pushliteral(L, ""); - lua_setfield(L, -2, "cpath"); - lua_pop(L, 1); + SetPaths(); lua_pushcclosure(L, luaM_print, 0); lua_setglobal(L, "print"); diff --git a/plugins/MirLua/src/mlua.h b/plugins/MirLua/src/mlua.h index a2b7301baa..33e973956e 100644 --- a/plugins/MirLua/src/mlua.h +++ b/plugins/MirLua/src/mlua.h @@ -15,6 +15,8 @@ class CMLua private: lua_State *L; + void SetPaths(); + static void KillLuaRefs(); public: diff --git a/plugins/MirLua/src/mlua_script_loader.cpp b/plugins/MirLua/src/mlua_script_loader.cpp index b95b907c8a..b374007dce 100644 --- a/plugins/MirLua/src/mlua_script_loader.cpp +++ b/plugins/MirLua/src/mlua_script_loader.cpp @@ -4,20 +4,6 @@ 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); - if (!mir_strlen(oldPath)) - lua_pushfstring(L, "%s\\?.lua", path); - else - lua_pushfstring(L, "%s;%s\\?.lua", oldPath, path); - lua_setfield(L, -2, "path"); - lua_pop(L, 1); -} - void CLuaScriptLoader::LoadScript(const TCHAR *scriptDir, const TCHAR *file) { TCHAR fullPath[MAX_PATH], path[MAX_PATH]; @@ -43,14 +29,15 @@ void CLuaScriptLoader::LoadScript(const TCHAR *scriptDir, const TCHAR *file) } } -void CLuaScriptLoader::LoadScripts(const TCHAR *scriptDir) +void CLuaScriptLoader::LoadScripts() { + TCHAR scriptDir[MAX_PATH]; + FoldersGetCustomPathT(g_hScriptsFolder, scriptDir, _countof(scriptDir), VARST(MIRLUA_PATHT)); + TCHAR buf[4096]; mir_sntprintf(buf, _T("Loading scripts from %s"), scriptDir); CallService(MS_NETLIB_LOGW, (WPARAM)hNetlib, (LPARAM)buf); - RegisterScriptsFolder(ptrA(mir_utf8encodeT(scriptDir))); - TCHAR searchMask[MAX_PATH]; mir_sntprintf(searchMask, _T("%s\\%s"), scriptDir, _T("*.lua")); @@ -70,9 +57,5 @@ void CLuaScriptLoader::LoadScripts(const TCHAR *scriptDir) void CLuaScriptLoader::Load(lua_State *L) { - TCHAR scriptDir[MAX_PATH]; - CLuaScriptLoader loader(L); - - FoldersGetCustomPathT(g_hCommonScriptFolder, scriptDir, _countof(scriptDir), VARST(COMMON_SCRIPTS_PATHT)); - loader.LoadScripts(scriptDir); + CLuaScriptLoader(L).LoadScripts(); } \ No newline at end of file diff --git a/plugins/MirLua/src/mlua_script_loader.h b/plugins/MirLua/src/mlua_script_loader.h index 28e2d9332c..e996fc1fdd 100644 --- a/plugins/MirLua/src/mlua_script_loader.h +++ b/plugins/MirLua/src/mlua_script_loader.h @@ -8,10 +8,8 @@ private: CLuaScriptLoader(lua_State *L); - void RegisterScriptsFolder(const char *path); - void LoadScript(const TCHAR *scriptDir, const TCHAR *file); - void LoadScripts(const TCHAR *scriptDir); + void LoadScripts(); public: static void Load(lua_State *L); diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index d781edee8b..f791f4a183 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -56,14 +56,21 @@ extern int hScriptsLangpack; extern HINSTANCE g_hInstance; -extern HANDLE g_hCommonScriptFolder; +extern HANDLE g_hCLibsFolder; +extern HANDLE g_hScriptsFolder; extern HANDLE hNetlib; #ifdef _UNICODE - #define COMMON_SCRIPTS_PATHT MIRANDA_PATHW L"\\Scripts" +#define MIRLUA_CPATHT MIRANDA_PATHW L"" #else - #define COMMON_SCRIPTS_PATHT MIRANDA_PATH "\\Scripts" +#define MIRLUA_CPATHT MIRANDA_PATH "" +#endif + +#ifdef _UNICODE + #define MIRLUA_PATHT MIRANDA_PATHW L"\\Scripts" +#else + #define MIRLUA_PATHT MIRANDA_PATH "\\Scripts" #endif #define MLUA_CORE "m_core" -- cgit v1.2.3