allow classes to register with Lua

This commit is contained in:
Chris Danford
2005-01-24 02:04:03 +00:00
parent bc9c3d1af9
commit 5b223580e5
2 changed files with 33 additions and 0 deletions
+27
View File
@@ -5,6 +5,7 @@
#include "RageLog.h" #include "RageLog.h"
#include "RageFile.h" #include "RageFile.h"
#include "arch/Dialog/Dialog.h" #include "arch/Dialog/Dialog.h"
#include "Foreach.h"
#include <csetjmp> #include <csetjmp>
#include <cassert> #include <cassert>
@@ -121,8 +122,27 @@ static int LuaPanic( lua_State *L )
longjmp( jbuf, 1 ); longjmp( jbuf, 1 );
} }
// Actor registration
static vector<RegisterActorFn> *g_vRegisterActors = NULL;
void LuaManager::Register( RegisterActorFn pfn )
{
if( g_vRegisterActors == NULL )
g_vRegisterActors = new vector<RegisterActorFn>;
g_vRegisterActors->push_back( pfn );
}
LuaManager::LuaManager() LuaManager::LuaManager()
{ {
LUA = this; // so that LUA is available when we call the Register functions
if( setjmp(jbuf) ) if( setjmp(jbuf) )
RageException::Throw("%s", jbuf_error.c_str()); RageException::Throw("%s", jbuf_error.c_str());
L = NULL; L = NULL;
@@ -152,6 +172,12 @@ void LuaManager::Init()
for( const LuaFunctionList *p = g_LuaFunctions; p; p=p->next ) for( const LuaFunctionList *p = g_LuaFunctions; p; p=p->next )
lua_register( L, p->name, p->func ); lua_register( L, p->name, p->func );
if( g_vRegisterActors )
{
FOREACH( RegisterActorFn, *g_vRegisterActors, fn )
(*fn)( L );
}
} }
void LuaManager::PrepareExpression( CString &sInOut ) void LuaManager::PrepareExpression( CString &sInOut )
@@ -322,6 +348,7 @@ void LuaManager::Fail( const CString &err )
lua_error( L ); lua_error( L );
} }
LuaFunctionList::LuaFunctionList( CString name_, lua_CFunction func_ ) LuaFunctionList::LuaFunctionList( CString name_, lua_CFunction func_ )
{ {
name = name_; name = name_;
+6
View File
@@ -2,9 +2,15 @@
#define LUA_HELPERS_H #define LUA_HELPERS_H
struct lua_State; struct lua_State;
class LuaManager;
typedef void (*RegisterActorFn)(lua_State*);
class LuaManager class LuaManager
{ {
public: public:
// Every Actor should register its class at program initialization.
static void Register( RegisterActorFn pfn );
LuaManager(); LuaManager();
~LuaManager(); ~LuaManager();