Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 5a2a72c

Browse files
authored
Merge pull request #201 from cortex-command-community/CF173-Lua-Loading
CF#173 - Lua loading
2 parents 8926041 + 4f4ba84 commit 5a2a72c

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

Managers/LuaMan.cpp

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -427,43 +427,27 @@ void LuaMan::Clear()
427427

428428
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
429429

430-
int LuaMan::Create()
431-
{
432-
// Create the master state
433-
m_pMasterState = lua_open();
430+
int LuaMan::Create() {
431+
m_pMasterState = luaL_newstate();
434432
// Attach the master state to LuaBind
435-
open(m_pMasterState);
436-
// Open the lua libs for the master state
437-
//luaL_openlibs(m_pMasterState);
438-
439-
// Load only libraries we need
440-
lua_pushcfunction(m_pMasterState, luaopen_base);
441-
lua_pushliteral(m_pMasterState, LUA_COLIBNAME);
442-
lua_call(m_pMasterState, 1, 0);
443-
444-
lua_pushcfunction(m_pMasterState, luaopen_table);
445-
lua_pushliteral(m_pMasterState, LUA_TABLIBNAME);
446-
lua_call(m_pMasterState, 1, 0);
447-
448-
lua_pushcfunction(m_pMasterState, luaopen_string);
449-
lua_pushliteral(m_pMasterState, LUA_STRLIBNAME);
450-
lua_call(m_pMasterState, 1, 0);
451-
452-
lua_pushcfunction(m_pMasterState, luaopen_math);
453-
lua_pushliteral(m_pMasterState, LUA_MATHLIBNAME);
454-
lua_call(m_pMasterState, 1, 0);
455-
456-
lua_pushcfunction(m_pMasterState, luaopen_debug);
457-
lua_pushliteral(m_pMasterState, LUA_DBLIBNAME);
458-
lua_call(m_pMasterState, 1, 0);
459-
460-
lua_pushcfunction(m_pMasterState, luaopen_package);
461-
lua_pushliteral(m_pMasterState, LUA_LOADLIBNAME);
462-
lua_call(m_pMasterState, 1, 0);
463-
464-
lua_pushcfunction(m_pMasterState, luaopen_jit);
465-
lua_pushliteral(m_pMasterState, LUA_LOADLIBNAME);
466-
lua_call(m_pMasterState, 1, 0);
433+
luabind::open(m_pMasterState);
434+
435+
const luaL_Reg libsToLoad[] = {
436+
{ LUA_COLIBNAME, luaopen_base },
437+
{ LUA_LOADLIBNAME, luaopen_package },
438+
{ LUA_TABLIBNAME, luaopen_table },
439+
{ LUA_STRLIBNAME, luaopen_string },
440+
{ LUA_MATHLIBNAME, luaopen_math },
441+
{ LUA_DBLIBNAME, luaopen_debug },
442+
{ LUA_JITLIBNAME, luaopen_jit },
443+
{ NULL, NULL } // End of array
444+
};
445+
446+
for (const luaL_Reg *lib = libsToLoad; lib->func; lib++) {
447+
lua_pushcfunction(m_pMasterState, lib->func);
448+
lua_pushstring(m_pMasterState, lib->name);
449+
lua_call(m_pMasterState, 1, 0);
450+
}
467451

468452
// LuaJIT should start automatically after we load the library but we're making sure it did anyway.
469453
if (!luaJIT_setmode(m_pMasterState, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_ON)) { RTEAbort("Failed to initialize LuaJIT!"); }

0 commit comments

Comments
 (0)