LuaManager.cpp: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
m (→LuaManager CPP for LUA 5.1.4 and 5.2: updated the block comment at the top to have correct links and replaced duplicate Created By to Updated by) |
||
(8 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
Save file | Save file as ge_luamanager.cpp | ||
See also: [[LuaManager.h]] | |||
< | <source lang=cpp> | ||
///////////// Copyright © | ///////////// Copyright © 2008 LodleNet. All rights reserved. ///////////// | ||
// | // | ||
// Project : Server | // Project : Server | ||
Line 10: | Line 11: | ||
// | // | ||
// Created On: 3/5/2009 4:58:54 PM | // Created On: 3/5/2009 4:58:54 PM | ||
// Created By: <mailto:admin | // Created By: <mailto:admin@lodle.net> | ||
// Updated By: <mailto:matt.shirleys@gmail.com> | |||
//////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////// | ||
Line 19: | Line 21: | ||
#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 53: | ||
void RegPublicFunctions(lua_State *L) | void RegPublicFunctions(lua_State *L) | ||
{ | { | ||
RegisterLUAFuncs(L); | |||
} | } | ||
void RegPublicGlobals(lua_State *L) | void RegPublicGlobals(lua_State *L) | ||
{ | { | ||
RegisterLUAGlobals(L); | |||
} | } | ||
Line 47: | Line 67: | ||
LuaHandle::LuaHandle() | LuaHandle::LuaHandle() | ||
{ | { | ||
pL=NULL; | m_bStarted = false; | ||
pL = NULL; | |||
} | } | ||
Line 57: | Line 78: | ||
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(); | ||
_luaOpenLibs( pL ); | |||
RegFunctions(); | RegFunctions(); | ||
Line 73: | Line 91: | ||
RegPublicGlobals(pL); | RegPublicGlobals(pL); | ||
m_bStarted = true; | |||
} | } | ||
void LuaHandle::ShutdownDll() | void LuaHandle::ShutdownDll() | ||
{ | { | ||
if ( !m_bStarted ) | |||
return; | |||
Shutdown(); | Shutdown(); | ||
lua_close(pL); | |||
m_bStarted = false; | |||
} | } | ||
Line 106: | Line 128: | ||
// 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++) | ||
{ | { | ||
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++) | ||
{ | { | ||
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(); | m_vHandles[x]->Shutdown(); | ||
} | } | ||
} | } | ||
void CGELUAManager::DeRegisterLuaHandle(LuaHandle* handle) | void CGELUAManager::DeRegisterLuaHandle(LuaHandle* handle) | ||
Line 140: | Line 176: | ||
{ | { | ||
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; | |||
} | |||
} | } | ||
} | } | ||
Line 161: | Line 200: | ||
handle->InitDll(); | handle->InitDll(); | ||
} | } | ||
</ | </source> | ||
== LuaManager CPP for LUA 5.1.4 and 5.2 == | |||
Edit: Changed from ''5.2'' to ''5.1.4 and 5.2'' to solve issues with compile errors --[[User:Warlock psc|Warlock psc]] 03:15, 17 May 2011 (UTC) | |||
Since the latests update for LUA has changed some functions, we need to update the script which I have done for you. | |||
<source lang=cpp> | |||
///////////// Copyright © 2009 LodleNet. All rights reserved. ///////////// | |||
// | |||
// Project : Server | |||
// File : ge_luamanager.cpp | |||
// Description : | |||
// Updated for LUA 5.2 | |||
// | |||
// Created On: 3/5/2009 4:58:54 PM | |||
// Created By: <mailto:admin@lodle.net> | |||
// Updated By: <mailto:matt.shirleys@gmail.com> | |||
//////////////////////////////////////////////////////////////////////////// | |||
#include "cbase.h" | |||
#include "ge_luamanager.h" | |||
// memdbgon must be the last include file in a .cpp file!!! | |||
#include "tier0/memdbgon.h" | |||
void RegisterLUAFuncs(lua_State *L); | |||
void RegisterLUAGlobals(lua_State *L); | |||
CGELUAManager gLuaMng; | |||
CGELUAManager* GELua() | |||
{ | |||
return &gLuaMng; | |||
} | |||
void RegPublicFunctions(lua_State *L) | |||
{ | |||
//add global lua functions here | |||
//RegisterLUAFuncs(L); | |||
} | |||
void RegPublicGlobals(lua_State *L) | |||
{ | |||
//add global lua defines here | |||
//RegisterLUAGlobals(L); | |||
} | |||
//////////////////////////////////////////////////////////////// | |||
// | |||
//////////////////////////////////////////////////////////////// | |||
LuaHandle* g_NLuaHandle = NULL; | |||
LuaHandle* GetNLuaHandle() | |||
{ | |||
return g_NLuaHandle; | |||
} | |||
LuaHandle::LuaHandle() | |||
{ | |||
pL=NULL; | |||
} | |||
LuaHandle::~LuaHandle() | |||
{ | |||
GELua()->DeRegisterLuaHandle(this); | |||
} | |||
void LuaHandle::InitDll() | |||
{ | |||
//Create an instance; Load the core libs. | |||
pL = luaL_newstate(); | |||
luaopen_base(pL); /* opens the basic library */ | |||
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(); | |||
RegGlobals(); | |||
RegPublicFunctions(pL); | |||
RegPublicGlobals(pL); | |||
Init(); | |||
} | |||
void LuaHandle::ShutdownDll() | |||
{ | |||
Shutdown(); | |||
lua_close(pL); | |||
} | |||
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 | |||
for (size_t x=0; x<m_vHandles.size(); x++) | |||
{ | |||
if (m_vHandles[x]) | |||
continue; | |||
m_vHandles[x]->InitDll(); | |||
} | |||
m_bInit = true; | |||
//ZMSLuaGamePlay *p = GetGP(); | |||
//p->m_bLuaLoaded = true; | |||
} | |||
void CGELUAManager::ShutdownDll() | |||
{ | |||
for (size_t x=0; x<m_vHandles.size(); x++) | |||
{ | |||
if (m_vHandles[x]) | |||
continue; | |||
m_vHandles[x]->Shutdown(); | |||
} | |||
m_vHandles.clear(); | |||
} | |||
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); | |||
} | |||
} | |||
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(); | |||
} | |||
void RegisterLUAFuncs(lua_State *L) | |||
{ | |||
} | |||
void RegisterLUAGlobals(lua_State *L) | |||
{ | |||
} | |||
</source> | |||
[[Category:Programming]] |
Latest revision as of 13:20, 22 October 2021
Save file as ge_luamanager.cpp See also: LuaManager.h
///////////// 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>
// Updated By: <mailto:matt.shirleys@gmail.com>
////////////////////////////////////////////////////////////////////////////
#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();
}
LuaManager CPP for LUA 5.1.4 and 5.2
Edit: Changed from 5.2 to 5.1.4 and 5.2 to solve issues with compile errors --Warlock psc 03:15, 17 May 2011 (UTC)
Since the latests update for LUA has changed some functions, we need to update the script which I have done for you.
///////////// Copyright © 2009 LodleNet. All rights reserved. /////////////
//
// Project : Server
// File : ge_luamanager.cpp
// Description :
// Updated for LUA 5.2
//
// Created On: 3/5/2009 4:58:54 PM
// Created By: <mailto:admin@lodle.net>
// Updated By: <mailto:matt.shirleys@gmail.com>
////////////////////////////////////////////////////////////////////////////
#include "cbase.h"
#include "ge_luamanager.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
void RegisterLUAFuncs(lua_State *L);
void RegisterLUAGlobals(lua_State *L);
CGELUAManager gLuaMng;
CGELUAManager* GELua()
{
return &gLuaMng;
}
void RegPublicFunctions(lua_State *L)
{
//add global lua functions here
//RegisterLUAFuncs(L);
}
void RegPublicGlobals(lua_State *L)
{
//add global lua defines here
//RegisterLUAGlobals(L);
}
////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////
LuaHandle* g_NLuaHandle = NULL;
LuaHandle* GetNLuaHandle()
{
return g_NLuaHandle;
}
LuaHandle::LuaHandle()
{
pL=NULL;
}
LuaHandle::~LuaHandle()
{
GELua()->DeRegisterLuaHandle(this);
}
void LuaHandle::InitDll()
{
//Create an instance; Load the core libs.
pL = luaL_newstate();
luaopen_base(pL); /* opens the basic library */
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();
RegGlobals();
RegPublicFunctions(pL);
RegPublicGlobals(pL);
Init();
}
void LuaHandle::ShutdownDll()
{
Shutdown();
lua_close(pL);
}
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
for (size_t x=0; x<m_vHandles.size(); x++)
{
if (m_vHandles[x])
continue;
m_vHandles[x]->InitDll();
}
m_bInit = true;
//ZMSLuaGamePlay *p = GetGP();
//p->m_bLuaLoaded = true;
}
void CGELUAManager::ShutdownDll()
{
for (size_t x=0; x<m_vHandles.size(); x++)
{
if (m_vHandles[x])
continue;
m_vHandles[x]->Shutdown();
}
m_vHandles.clear();
}
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);
}
}
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();
}
void RegisterLUAFuncs(lua_State *L)
{
}
void RegisterLUAGlobals(lua_State *L)
{
}