summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/mlua_enviroment.cpp
blob: 4e7225fd164d4e12d6a5966c73f0a01abc057d8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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()
{
	KillModuleIcons(id);
	KillModuleSounds(id);
	KillModuleMenus(id);
	KillModuleHotkeys(id);

	KillObjectEventHooks(this);
	KillObjectServices(this);
}

CMLuaEnviroment* CMLuaEnviroment::GetEnviroment(lua_State *L)
{
	if (!luaM_getenv(L))
		return nullptr;

	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()
{
	luaL_checktype(L, -1, LUA_TFUNCTION);

	CreateEnviromentTable();
	lua_setupvalue(L, -2, 1);

	return lua_pcall(L, 0, 1, 0) == LUA_OK;
}