diff --git a/stepmania/src/ActorCommands.cpp b/stepmania/src/ActorCommands.cpp index 33b9199fd3..c7aa657c40 100644 --- a/stepmania/src/ActorCommands.cpp +++ b/stepmania/src/ActorCommands.cpp @@ -7,6 +7,22 @@ #include "LuaManager.h" #include "LuaBinding.h" +static vector* g_pActorCommands; + +static void Subscribe( ActorCommands* p ) +{ + if( g_pActorCommands == NULL ) + g_pActorCommands = new vector; + g_pActorCommands->push_back( p ); +} + +static void Unsubscribe( ActorCommands* p ) +{ + vector::iterator iter = find( g_pActorCommands->begin(), g_pActorCommands->end(), p ); + ASSERT( iter != g_pActorCommands->end() ); + g_pActorCommands->erase( iter ); +} + static CString GetNextFunctionName() { static int id = 0; @@ -16,17 +32,25 @@ static CString GetNextFunctionName() ActorCommands::ActorCommands( const Commands& cmds ) { - Register( cmds ); + Subscribe( this ); + + m_cmds = cmds; + + Register(); } ActorCommands::~ActorCommands() { + Unsubscribe( this ); + if( m_sLuaFunctionName.size() ) Unregister(); } ActorCommands::ActorCommands( const ActorCommands& cpy ) { + Subscribe( this ); + m_sLuaFunctionName = GetNextFunctionName(); /* We need to make a new function, since we'll be unregistered separately. Set @@ -56,9 +80,9 @@ void ActorCommands::PushSelf( lua_State *L ) const ASSERT_M( !lua_isnil(L, -1), m_sLuaFunctionName.c_str() ) } -void ActorCommands::Register( const Commands& cmds ) +void ActorCommands::Register() { - if( cmds.v.size() == 0 ) + if( m_cmds.v.size() == 0 ) { m_sLuaFunctionName = "nop"; return; @@ -76,7 +100,7 @@ void ActorCommands::Register( const Commands& cmds ) s << m_sLuaFunctionName << " = function(self)\n"; - FOREACH_CONST( Command, cmds.v, c ) + FOREACH_CONST( Command, m_cmds.v, c ) { const Command& cmd = (*c); CString sName = cmd.GetName(); @@ -146,6 +170,15 @@ void ActorCommands::Unregister() m_sLuaFunctionName = ""; } +void ActorCommands::ReRegisterAll() +{ + if( g_pActorCommands == NULL ) + return; + FOREACH( ActorCommands*, *g_pActorCommands, p ) + (*p)->Register(); +} + + /* * (c) 2004 Chris Danford * All rights reserved. diff --git a/stepmania/src/ActorCommands.h b/stepmania/src/ActorCommands.h index 26e11e7925..12e99b8edc 100644 --- a/stepmania/src/ActorCommands.h +++ b/stepmania/src/ActorCommands.h @@ -4,7 +4,7 @@ #define ActorCommands_H #include "RageUtil_AutoPtr.h" -class Commands; +#include "Command.h" struct lua_State; @@ -18,10 +18,13 @@ public: void PushSelf( lua_State *L ) const; + static void ReRegisterAll(); // call this after resetting Lua + private: - void Register( const Commands& cmds ); + void Register(); void Unregister(); + Commands m_cmds; CString m_sLuaFunctionName; }; diff --git a/stepmania/src/LuaManager.cpp b/stepmania/src/LuaManager.cpp index b467899d88..7674385553 100644 --- a/stepmania/src/LuaManager.cpp +++ b/stepmania/src/LuaManager.cpp @@ -6,6 +6,7 @@ #include "RageFile.h" #include "arch/Dialog/Dialog.h" #include "Foreach.h" +#include "ActorCommands.h" #include #include @@ -141,14 +142,14 @@ static int LuaPanic( lua_State *L ) // Actor registration -static vector *g_vRegisterActors = NULL; +static vector *g_vRegisterActorTypes = NULL; void LuaManager::Register( RegisterActorFn pfn ) { - if( g_vRegisterActors == NULL ) - g_vRegisterActors = new vector; + if( g_vRegisterActorTypes == NULL ) + g_vRegisterActorTypes = new vector; - g_vRegisterActors->push_back( pfn ); + g_vRegisterActorTypes->push_back( pfn ); } @@ -189,14 +190,16 @@ void LuaManager::ResetState() for( const LuaFunctionList *p = g_LuaFunctions; p; p=p->next ) lua_register( L, p->name, p->func ); - if( g_vRegisterActors ) + if( g_vRegisterActorTypes ) { - for( unsigned i=0; isize(); i++ ) + for( unsigned i=0; isize(); i++ ) { - RegisterActorFn fn = (*g_vRegisterActors)[i]; + RegisterActorFn fn = (*g_vRegisterActorTypes)[i]; fn( L ); } } + + ActorCommands::ReRegisterAll(); } void LuaManager::PrepareExpression( CString &sInOut )