diff options
Diffstat (limited to 'plugins/MirLua/src/mlua_enviroment.cpp')
-rw-r--r-- | plugins/MirLua/src/mlua_enviroment.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/plugins/MirLua/src/mlua_enviroment.cpp b/plugins/MirLua/src/mlua_enviroment.cpp new file mode 100644 index 0000000000..5d3b7cf37c --- /dev/null +++ b/plugins/MirLua/src/mlua_enviroment.cpp @@ -0,0 +1,71 @@ +#include "stdafx.h" + +#define MT_ENVIROMENT "ENVIROMENT" + +CMLuaEnviroment::CMLuaEnviroment(lua_State *L) + : L(L) +{ + MUUID muidLast = MIID_LAST; + id = GetPluginLangId(muidLast, 0); +} + +CMLuaEnviroment* CMLuaEnviroment::GetEnviroment(lua_State *L) +{ + if (!luaM_getenv(L)) + return NULL; + + lua_rawgeti(L, -1, NULL); + CMLuaEnviroment *env = (CMLuaEnviroment*)lua_touserdata(L, -1); + lua_pop(L, 3); + + return env; +} + +int CMLuaEnviroment::GetEnviromentId(lua_State *L) +{ + CMLuaEnviroment *script = GetEnviroment(L); + return script != nullptr + ? script->GetId() + : hMLuaLangpack; +} + +int CMLuaEnviroment::GetId() const +{ + return id; +} + +void CMLuaEnviroment::CreateEnviromentTable() +{ + lua_createtable(L, 1, 1); + lua_pushlightuserdata(L, this); + lua_rawseti(L, -2, NULL); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "_G"); + lua_createtable(L, 0, 2); + lua_pushliteral(L, MT_ENVIROMENT); + lua_setfield(L, -2, "__metatable"); + lua_getglobal(L, "_G"); + lua_setfield(L, -2, "__index"); + lua_setmetatable(L, -2); +} + +bool CMLuaEnviroment::Load(int ind) +{ + luaL_checktype(L, ind, LUA_TFUNCTION); + + CreateEnviromentTable(); + lua_setupvalue(L, -2, 1); + + return luaM_pcall(L, 0, 1) == LUA_OK; +} + +void CMLuaEnviroment::Unload() +{ + KillModuleIcons(id); + KillModuleSounds(id); + KillModuleMenus(id); + KillModuleHotkeys(id); + + KillObjectEventHooks(this); + KillObjectServices(this); +}
\ No newline at end of file |