diff options
Diffstat (limited to 'plugins/MirLua/src/script.cpp')
-rw-r--r-- | plugins/MirLua/src/script.cpp | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/plugins/MirLua/src/script.cpp b/plugins/MirLua/src/script.cpp new file mode 100644 index 0000000000..976ed79688 --- /dev/null +++ b/plugins/MirLua/src/script.cpp @@ -0,0 +1,137 @@ +#include "stdafx.h"
+
+#define MT_SCRIPT "SCRIPT"
+
+CMLuaScript::CMLuaScript(lua_State *L, const wchar_t *path)
+ : CMLuaEnvironment(L), status(None), unloadRef(LUA_NOREF)
+{
+ mir_wstrcpy(filePath, path);
+
+ fileName = wcsrchr(filePath, L'\\') + 1;
+ const wchar_t *dot = wcsrchr(fileName, '.');
+
+ size_t length = mir_wstrlen(fileName) - mir_wstrlen(dot) + 1;
+
+ ptrW name((wchar_t*)mir_calloc(sizeof(wchar_t) * (length + 1)));
+ mir_wstrncpy(name, fileName, length);
+
+ m_szModuleName = mir_utf8encodeW(name);
+}
+
+CMLuaScript::CMLuaScript(const CMLuaScript &script)
+ : CMLuaEnvironment(script.L), status(None), unloadRef(LUA_NOREF)
+{
+ mir_wstrcpy(filePath, script.filePath);
+ fileName = mir_wstrdup(script.fileName);
+ m_szModuleName = mir_strdup(script.m_szModuleName);
+}
+
+CMLuaScript::~CMLuaScript()
+{
+ Unload();
+ mir_free((char*)m_szModuleName);
+}
+
+const wchar_t* CMLuaScript::GetFilePath() const
+{
+ return filePath;
+}
+
+const wchar_t* CMLuaScript::GetFileName() const
+{
+ return fileName;
+}
+
+bool CMLuaScript::IsEnabled()
+{
+ return db_get_b(NULL, MODULENAME, _T2A(fileName), 1);
+}
+
+void CMLuaScript::Enable()
+{
+ db_unset(NULL, MODULENAME, _T2A(fileName));
+}
+
+void CMLuaScript::Disable()
+{
+ db_set_b(NULL, MODULENAME, _T2A(fileName), 0);
+}
+
+CMLuaScript::Status CMLuaScript::GetStatus() const
+{
+ return status;
+}
+
+bool CMLuaScript::Load()
+{
+ status = Failed;
+
+ if (luaL_loadfile(L, _T2A(filePath))) {
+ ReportError(L);
+ return false;
+ }
+
+ if (!CMLuaEnvironment::Load()) {
+ ReportError(L);
+ return false;
+ }
+
+ status = Loaded;
+
+ if (lua_isnoneornil(L, -1))
+ return true;
+
+ luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
+ lua_getfield(L, -1, m_szModuleName);
+ if (!lua_toboolean(L, -1)) {
+ lua_pop(L, 1);
+ lua_pushvalue(L, -2);
+ lua_setfield(L, -2, m_szModuleName);
+ lua_pop(L, 1);
+ }
+ else
+ lua_remove(L, -2);
+
+ if (!lua_istable(L, -1))
+ return true;
+
+ lua_getfield(L, -1, "Load");
+ if (lua_isfunction(L, -1))
+ luaM_pcall(L);
+ else
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "Unload");
+ if (lua_isfunction(L, -1)) {
+ lua_pushvalue(L, -1);
+ unloadRef = luaL_ref(L, LUA_REGISTRYINDEX);
+ }
+ lua_pop(L, 1);
+
+ lua_pop(L, 1);
+
+ return true;
+}
+
+void CMLuaScript::Unload()
+{
+ if (status == Loaded) {
+ lua_rawgeti(L, LUA_REGISTRYINDEX, unloadRef);
+ if (lua_isfunction(L, -1))
+ luaM_pcall(L);
+ lua_pushnil(L);
+ lua_rawsetp(L, LUA_REGISTRYINDEX, this);
+ status = None;
+ }
+
+ luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
+ lua_pushnil(L);
+ lua_setfield(L, -2, m_szModuleName);
+ lua_pop(L, 1);
+}
+
+bool CMLuaScript::Reload()
+{
+ Unload();
+ return Load();
+}
|