diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2016-01-07 10:42:38 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2016-01-07 10:42:38 +0000 |
commit | 6a6a1d097e043dba142d5cdc485e6b4dbe67e12a (patch) | |
tree | 8b4700b62e3569fcc0906bf75bf79894dd1623e0 /plugins/MirLua/src/mlua_script.cpp | |
parent | 2742e2cfa0b0c32cbfc30e551f9ffaca29aa8aa9 (diff) |
MirLua:
- added script cache on load
- refactoring
git-svn-id: http://svn.miranda-ng.org/main/trunk@16044 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/MirLua/src/mlua_script.cpp')
-rw-r--r-- | plugins/MirLua/src/mlua_script.cpp | 35 |
1 files changed, 27 insertions, 8 deletions
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");
|