From 5b223580e5c897697c793e9bbfb028590dd2e0b9 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Mon, 24 Jan 2005 02:04:03 +0000 Subject: [PATCH] allow classes to register with Lua --- stepmania/src/LuaHelpers.cpp | 27 +++++++++++++++++++++++++++ stepmania/src/LuaHelpers.h | 6 ++++++ 2 files changed, 33 insertions(+) diff --git a/stepmania/src/LuaHelpers.cpp b/stepmania/src/LuaHelpers.cpp index fa352a2079..e44eeac106 100644 --- a/stepmania/src/LuaHelpers.cpp +++ b/stepmania/src/LuaHelpers.cpp @@ -5,6 +5,7 @@ #include "RageLog.h" #include "RageFile.h" #include "arch/Dialog/Dialog.h" +#include "Foreach.h" #include #include @@ -121,8 +122,27 @@ static int LuaPanic( lua_State *L ) longjmp( jbuf, 1 ); } + + +// Actor registration +static vector *g_vRegisterActors = NULL; + +void LuaManager::Register( RegisterActorFn pfn ) +{ + if( g_vRegisterActors == NULL ) + g_vRegisterActors = new vector; + + g_vRegisterActors->push_back( pfn ); +} + + + + + LuaManager::LuaManager() { + LUA = this; // so that LUA is available when we call the Register functions + if( setjmp(jbuf) ) RageException::Throw("%s", jbuf_error.c_str()); L = NULL; @@ -152,6 +172,12 @@ void LuaManager::Init() for( const LuaFunctionList *p = g_LuaFunctions; p; p=p->next ) lua_register( L, p->name, p->func ); + + if( g_vRegisterActors ) + { + FOREACH( RegisterActorFn, *g_vRegisterActors, fn ) + (*fn)( L ); + } } void LuaManager::PrepareExpression( CString &sInOut ) @@ -322,6 +348,7 @@ void LuaManager::Fail( const CString &err ) lua_error( L ); } + LuaFunctionList::LuaFunctionList( CString name_, lua_CFunction func_ ) { name = name_; diff --git a/stepmania/src/LuaHelpers.h b/stepmania/src/LuaHelpers.h index a56f84dfb4..674c0b82bf 100644 --- a/stepmania/src/LuaHelpers.h +++ b/stepmania/src/LuaHelpers.h @@ -2,9 +2,15 @@ #define LUA_HELPERS_H struct lua_State; +class LuaManager; +typedef void (*RegisterActorFn)(lua_State*); + class LuaManager { public: + // Every Actor should register its class at program initialization. + static void Register( RegisterActorFn pfn ); + LuaManager(); ~LuaManager();