LuaManager.cpp: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
(Modified LUA initialization code)
Line 2: Line 2:


<source lang=cpp>
<source lang=cpp>
///////////// Copyright © 2009 LodleNet. All rights reserved. /////////////
///////////// Copyright © 2008 LodleNet. All rights reserved. /////////////
//
//
//  Project    : Server
//  Project    : Server
Line 10: Line 10:
//
//
//  Created On: 3/5/2009 4:58:54 PM
//  Created On: 3/5/2009 4:58:54 PM
//  Created By:  <mailto:admin[at]lodle[dot]net>
//  Created By:  <mailto:admin@lodle.net>
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////


Line 19: Line 19:
#include "tier0/memdbgon.h"
#include "tier0/memdbgon.h"


// DEFINE THESE FUNCTIONS TO OPEN UP THE LUA LIBRARIES WE WANT --KM
static void _luaOpenLib(lua_State* L, const char* name, lua_CFunction fn)
{
    lua_pushcfunction(L, fn);
    lua_pushstring(L, name);
    lua_call(L, 1, 0);
}
static void _luaOpenLibs(lua_State* L)
{
    _luaOpenLib(L, "", luaopen_base) ;
    _luaOpenLib(L, LUA_LOADLIBNAME, luaopen_package);
    _luaOpenLib(L, LUA_TABLIBNAME, luaopen_table);
    _luaOpenLib(L, LUA_STRLIBNAME, luaopen_string);
    _luaOpenLib(L, LUA_MATHLIBNAME, luaopen_math);
#ifdef _DEBUG
_luaOpenLib(L, LUA_DBLIBNAME, luaopen_debug);
#endif
}


void RegisterLUAFuncs(lua_State *L);
void RegisterLUAFuncs(lua_State *L);
Line 31: Line 51:
void RegPublicFunctions(lua_State *L)
void RegPublicFunctions(lua_State *L)
{
{
//add global lua functions here
RegisterLUAFuncs(L);
//RegisterLUAFuncs(L);
}
}


void RegPublicGlobals(lua_State *L)
void RegPublicGlobals(lua_State *L)
{
{
//add global lua defines here
RegisterLUAGlobals(L);
//RegisterLUAGlobals(L);
}
}


Line 47: Line 65:
LuaHandle::LuaHandle()
LuaHandle::LuaHandle()
{
{
pL=NULL;
m_bStarted = false;
pL = NULL;
}
}


Line 57: Line 76:
void LuaHandle::InitDll()
void LuaHandle::InitDll()
{
{
if ( m_bStarted )
return;
//Create an instance; Load the core libs.
//Create an instance; Load the core libs.
pL = lua_open();
pL = lua_open();
luaopen_base(pL); /* opens the basic library */
_luaOpenLibs( pL );
luaopen_table(pL); /* opens the table library */
luaopen_string(pL); /* opens the string lib. */
luaopen_math(pL); /* opens the math lib. */
#ifdef _DEBUG
luaopen_debug(pL);
#endif


RegFunctions();
RegFunctions();
Line 73: Line 89:
RegPublicGlobals(pL);
RegPublicGlobals(pL);


Init();
m_bStarted = true;
}
}


void LuaHandle::ShutdownDll()
void LuaHandle::ShutdownDll()
{
{
if ( !m_bStarted )
return;
Shutdown();
Shutdown();
lua_close(pL);


lua_close(pL);
m_bStarted = false;
}
}


Line 106: Line 126:
// Register our LUA Functions and Globals so we can call them
// Register our LUA Functions and Globals so we can call them
// from .lua scripts
// from .lua scripts
if ( m_bInit )
return;


for (size_t x=0; x<m_vHandles.size(); x++)
for (size_t x=0; x<m_vHandles.size(); x++)
{
{
if (m_vHandles[x])
continue;
m_vHandles[x]->InitDll();
m_vHandles[x]->InitDll();
}
}


m_bInit = true;
m_bInit = true;
}
void CGELUAManager::InitHandles( void )
{
for (size_t x=0; x<m_vHandles.size(); x++)
{
m_vHandles[x]->Init();
}
}
}


void CGELUAManager::ShutdownDll()
void CGELUAManager::ShutdownDll()
{
{
if ( !m_bInit )
return;
for (size_t x=0; x<m_vHandles.size(); x++)
for (size_t x=0; x<m_vHandles.size(); x++)
{
{
if (m_vHandles[x])
m_vHandles[x]->ShutdownDll();
continue;
}


m_bInit = false;
}
void CGELUAManager::ShutdownHandles( void )
{
for (size_t x=0; x<m_vHandles.size(); x++)
{
m_vHandles[x]->Shutdown();
m_vHandles[x]->Shutdown();
}
}
m_vHandles.clear();
}
}


void CGELUAManager::DeRegisterLuaHandle(LuaHandle* handle)
void CGELUAManager::DeRegisterLuaHandle(LuaHandle* handle)
Line 140: Line 174:
{
{
if (m_vHandles[x]==handle)
if (m_vHandles[x]==handle)
{
m_vHandles.erase(m_vHandles.begin()+x);
m_vHandles.erase(m_vHandles.begin()+x);
break;
}
}
}
}
}

Revision as of 20:59, 11 August 2009

Save file us ge_luamanager.cpp

///////////// Copyright © 2008 LodleNet. All rights reserved. /////////////
//
//   Project     : Server
//   File        : ge_luamanager.cpp
//   Description :
//      [TODO: Write the purpose of ge_luamanager.cpp.]
//
//   Created On: 3/5/2009 4:58:54 PM
//   Created By:  <mailto:admin@lodle.net>
////////////////////////////////////////////////////////////////////////////

#include "cbase.h"
#include "ge_luamanager.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"


// DEFINE THESE FUNCTIONS TO OPEN UP THE LUA LIBRARIES WE WANT --KM
static void _luaOpenLib(lua_State* L, const char* name, lua_CFunction fn)
{
    lua_pushcfunction(L, fn);
    lua_pushstring(L, name);
    lua_call(L, 1, 0);
}
 
static void _luaOpenLibs(lua_State* L)
{
    _luaOpenLib(L, "", luaopen_base) ;
    _luaOpenLib(L, LUA_LOADLIBNAME, luaopen_package);
    _luaOpenLib(L, LUA_TABLIBNAME, luaopen_table);
    _luaOpenLib(L, LUA_STRLIBNAME, luaopen_string);
    _luaOpenLib(L, LUA_MATHLIBNAME, luaopen_math);
#ifdef _DEBUG
	_luaOpenLib(L, LUA_DBLIBNAME, luaopen_debug);
#endif
}

void RegisterLUAFuncs(lua_State *L);
void RegisterLUAGlobals(lua_State *L);

CGELUAManager gLuaMng;
CGELUAManager* GELua()
{
	return &gLuaMng;
}

void RegPublicFunctions(lua_State *L)
{
	RegisterLUAFuncs(L);
}

void RegPublicGlobals(lua_State *L)
{
	RegisterLUAGlobals(L);
}

////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////

LuaHandle::LuaHandle()
{
	m_bStarted = false;
	pL = NULL;
}

LuaHandle::~LuaHandle()
{
	GELua()->DeRegisterLuaHandle(this);
}

void LuaHandle::InitDll()
{
	if ( m_bStarted )
		return;

	//Create an instance; Load the core libs.
	pL = lua_open();
	_luaOpenLibs( pL );	

	RegFunctions();
	RegGlobals();

	RegPublicFunctions(pL);
	RegPublicGlobals(pL);

	m_bStarted = true;
}

void LuaHandle::ShutdownDll()
{
	if ( !m_bStarted )
		return;

	Shutdown();
	lua_close(pL);

	m_bStarted = false;
}

void LuaHandle::Register()
{
	GELua()->RegisterLuaHandle(this);
}

////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////

CGELUAManager::CGELUAManager()
{
	m_bInit = false;
}

CGELUAManager::~CGELUAManager()
{

}

void CGELUAManager::InitDll()
{
	// Register our LUA Functions and Globals so we can call them
	// from .lua scripts
	if ( m_bInit )
		return;

	for (size_t x=0; x<m_vHandles.size(); x++)
	{
		m_vHandles[x]->InitDll();
	}

	m_bInit = true;
}

void CGELUAManager::InitHandles( void )
{
	for (size_t x=0; x<m_vHandles.size(); x++)
	{
		m_vHandles[x]->Init();
	}
}

void CGELUAManager::ShutdownDll()
{
	if ( !m_bInit )
		return;

	for (size_t x=0; x<m_vHandles.size(); x++)
	{
		m_vHandles[x]->ShutdownDll();
	}

	m_bInit = false;
}

void CGELUAManager::ShutdownHandles( void )
{
	for (size_t x=0; x<m_vHandles.size(); x++)
	{
		m_vHandles[x]->Shutdown();
	}
}

void CGELUAManager::DeRegisterLuaHandle(LuaHandle* handle)
{
	if (!handle)
		return;
	
	for (size_t x=0; x<m_vHandles.size(); x++)
	{
		if (m_vHandles[x]==handle)
		{
			m_vHandles.erase(m_vHandles.begin()+x);
			break;
		}
	}
}

void CGELUAManager::RegisterLuaHandle(LuaHandle* handle)
{
	if (!handle)
		return;

	for (size_t x=0; x<m_vHandles.size(); x++)
	{
		if (m_vHandles[x]==handle)
			return;
	}

	m_vHandles.push_back(handle);

	//if we are late to the game
	if (m_bInit)
		handle->InitDll();
}