diff --git a/stepmania/src/ActorCommands.cpp b/stepmania/src/ActorCommands.cpp index e9468911c9..9861f810a8 100644 --- a/stepmania/src/ActorCommands.cpp +++ b/stepmania/src/ActorCommands.cpp @@ -4,71 +4,20 @@ #include "Foreach.h" #include "RageUtil.h" #include -#include "LuaManager.h" #include "LuaBinding.h" -#include "SubscriptionManager.h" -template<> -set* SubscriptionManager::s_pSubscribers = NULL; - - ActorCommands::ActorCommands( const Commands& cmds ) { - SubscriptionManager::Subscribe( this ); - m_cmds = cmds; - m_iLuaFunction = LUA_NOREF; - Register(); } -ActorCommands::~ActorCommands() -{ - SubscriptionManager::Unsubscribe( this ); - - Unregister(); -} - -ActorCommands::ActorCommands( const ActorCommands& cpy ) -{ - SubscriptionManager::Subscribe( this ); - - /* Make a new reference. */ - lua_rawgeti( LUA->L, LUA_REGISTRYINDEX, cpy.m_iLuaFunction ); - m_iLuaFunction = luaL_ref( LUA->L, LUA_REGISTRYINDEX ); -} - -ActorCommands &ActorCommands::operator=( const ActorCommands& cpy ) -{ - if( this == &cpy ) - return *this; - - Unregister(); - - /* Make a new reference. */ - lua_rawgeti( LUA->L, LUA_REGISTRYINDEX, cpy.m_iLuaFunction ); - m_iLuaFunction = luaL_ref( LUA->L, LUA_REGISTRYINDEX ); - - return *this; -} - -void ActorCommands::PushSelf( lua_State *L ) const -{ - ASSERT( m_iLuaFunction != LUA_NOREF ); - - if( m_iLuaFunction != LUA_REFNIL ) - lua_rawgeti( LUA->L, LUA_REGISTRYINDEX, m_iLuaFunction ); - else - LUA->PushNopFunction(); - - ASSERT_M( !lua_isnil(L, -1), ssprintf("%i", m_iLuaFunction) ) -} - void ActorCommands::Register() { if( m_cmds.v.size() == 0 ) { - m_iLuaFunction = LUA_REFNIL; + LUA->PushNopFunction(); + this->SetFromStack(); return; } @@ -132,34 +81,7 @@ void ActorCommands::Register() LUA->RunScript( s2, 1 ); /* The function is now on the stack. */ - m_iLuaFunction = luaL_ref( LUA->L, LUA_REGISTRYINDEX ); -} - -void ActorCommands::Unregister() -{ - if( LUA == NULL ) - return; // nothing to do - - luaL_unref( LUA->L, LUA_REGISTRYINDEX, m_iLuaFunction ); - m_iLuaFunction = LUA_NOREF; -} - -void ActorCommands::ReRegister() -{ - /* When called, the Lua state has been wiped. Don't try to unregister our old - * function reference, since it's already gone (and the number may point somewhere - * else). */ - m_iLuaFunction = LUA_NOREF; - Register(); -} - - -void ActorCommands::ReRegisterAll() -{ - if( SubscriptionManager::s_pSubscribers == NULL ) - return; - FOREACHS( ActorCommands*, *SubscriptionManager::s_pSubscribers, p ) - (*p)->ReRegister(); + this->SetFromStack(); } diff --git a/stepmania/src/ActorCommands.h b/stepmania/src/ActorCommands.h index 806d0ef018..a731bcc25f 100644 --- a/stepmania/src/ActorCommands.h +++ b/stepmania/src/ActorCommands.h @@ -5,28 +5,17 @@ #include "RageUtil_AutoPtr.h" #include "Command.h" +#include "LuaReference.h" -struct lua_State; -class ActorCommands +class ActorCommands: public LuaReference { public: ActorCommands( const Commands& cmds ); - ~ActorCommands(); - ActorCommands( const ActorCommands& cpy ); - ActorCommands &operator=( const ActorCommands& cpy ); - - void PushSelf( lua_State *L ) const; - - static void ReRegisterAll(); // call this after resetting Lua private: void Register(); - void ReRegister(); - void Unregister(); - Commands m_cmds; - int m_iLuaFunction; }; typedef AutoPtrCopyOnWrite apActorCommands;