summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/mlua_module_loader.cpp
blob: 426f7bd9e60774e86a88d34ce9bccd6c2ec8209e (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
#include "stdafx.h"

CLuaModuleLoader::CLuaModuleLoader(lua_State *L) : L(L)
{
}

void CLuaModuleLoader::Load(const char *name, lua_CFunction loader)
{
	luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
	lua_getfield(L, -1, name);
	if (lua_toboolean(L, -1))
		Log("Module %s will be replaced with new one");
	lua_pop(L, 1);
	lua_pushcfunction(L, loader);
	lua_pushstring(L, name);
	luaM_pcall(L, 1, 1);
	lua_setfield(L, -2, name);
	lua_pop(L, 1);
	lua_pop(L, 1);
}

void CLuaModuleLoader::Preload(const char *name, lua_CFunction loader)
{
	luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
	lua_pushcfunction(L, loader);
	lua_setfield(L, -2, name);
	lua_pop(L, 1);
}

void CLuaModuleLoader::LoadModules()
{
	// load m_core module
	Load(MLUA_CORE, luaopen_m_core);
	// load all internal modules
	Preload(MLUA_CLIST, luaopen_m_clist);
	Preload(MLUA_DATABASE, luaopen_m_database);
	Preload(MLUA_ICOLIB, luaopen_m_icolib);
	Preload(MLUA_GENMENU, luaopen_m_genmenu);
	Preload(MLUA_HOTKEYS, luaopen_m_hotkeys);
	Preload(MLUA_MESSAGE, luaopen_m_message);
	Preload(MLUA_PROTOCOLS, luaopen_m_protocols);
	Preload(MLUA_SCHEDULE, luaopen_m_schedule);
	Preload(MLUA_SOUNDS, luaopen_m_sounds);
	// regirter delay loading of other modules
	Preload(MLUA_MSGBUTTONSBAR, luaopen_m_msg_buttonsbar);
	Preload(MLUA_POPUP, luaopen_m_popup);
	Preload(MLUA_TOPTOOLBAR, luaopen_m_toptoolbar);
	Preload(MLUA_VARIABLES, luaopen_m_variables);
}

void CLuaModuleLoader::Load(lua_State *L)
{
	CLuaModuleLoader loader(L);
	loader.LoadModules();
}