diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2015-07-18 19:43:12 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2015-07-18 19:43:12 +0000 |
commit | 059a7ea5928d49d81ed1d884e59c60f152983539 (patch) | |
tree | 8d4065bfa11f5cba5714d131842108c6a1286922 /plugins/MirLua/src/mlua_script.cpp | |
parent | 952154728cfc9a390f9f7b2e3326ea32d76e0003 (diff) |
MirLua:
- removed OnScriptLoaded/OnScriptUnload
- removed second script path
- module loading and options refactoring
- version bump
git-svn-id: http://svn.miranda-ng.org/main/trunk@14587 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/MirLua/src/mlua_script.cpp')
-rw-r--r-- | plugins/MirLua/src/mlua_script.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/plugins/MirLua/src/mlua_script.cpp b/plugins/MirLua/src/mlua_script.cpp new file mode 100644 index 0000000000..aa181a3a22 --- /dev/null +++ b/plugins/MirLua/src/mlua_script.cpp @@ -0,0 +1,98 @@ +#include "stdafx.h"
+
+CMLuaScript::CMLuaScript(lua_State *L, const TCHAR* path, int iGroup) : L(L)
+{
+ mir_tstrcpy(filePath, path);
+
+ group = iGroup;
+
+ fileName = _tcsrchr(filePath, '\\') + 1;
+ size_t length = mir_tstrlen(fileName) - 3;
+
+ ptrT name((TCHAR*)mir_calloc(sizeof(TCHAR) * length));
+ mir_tstrncpy(name, fileName, mir_tstrlen(fileName) - 3);
+
+ moduleName = mir_utf8encodeT(name);
+}
+
+CMLuaScript::~CMLuaScript()
+{
+ mir_free(this->moduleName);
+}
+
+const char* CMLuaScript::GetModuleName() const
+{
+ return moduleName;
+}
+
+const TCHAR* CMLuaScript::GetFilePath() const
+{
+ return filePath;
+}
+
+const TCHAR* CMLuaScript::GetFileName() const
+{
+ return fileName;
+}
+
+const int CMLuaScript::GetGroup() const
+{
+ return group;
+}
+
+bool CMLuaScript::Load()
+{
+ if (luaL_loadfile(L, T2Utf(filePath)))
+ {
+ CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
+ return false;
+ }
+
+ if (lua_pcall(L, 0, 1, 0))
+ {
+ CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
+ return false;
+ }
+
+ isLoaded = true;
+
+ if (!lua_istable(L, -1))
+ return true;
+
+ lua_pushliteral(L, "Load");
+ lua_gettable(L, -2);
+ if (lua_isfunction(L, -1) && lua_pcall(L, 0, 0, 0))
+ CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
+ lua_pop(L, 1);
+
+ lua_pushliteral(L, "Unload");
+ lua_gettable(L, -2);
+ if (lua_isfunction(L, -1))
+ {
+ lua_pushvalue(L, -1);
+ unloadRef = luaL_ref(L, LUA_REGISTRYINDEX);
+ }
+ lua_pop(L, 1);
+
+ return true;
+}
+
+void CMLuaScript::Unload()
+{
+ if (isLoaded)
+ {
+ lua_rawgeti(L, LUA_REGISTRYINDEX, unloadRef);
+ if (lua_pcall(L, 0, 0, 0))
+ CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
+ luaL_unref(L, LUA_REGISTRYINDEX, unloadRef);
+ isLoaded = false;
+ }
+
+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
+ lua_pushnil(L);
+ lua_setfield(L, -2, moduleName);
+ lua_pop(L, 1);
+
+ lua_pushnil(L);
+ lua_setglobal(L, moduleName);
+}
\ No newline at end of file |