re-register all ActorCommands after resetting Lua

This commit is contained in:
Chris Danford
2005-02-05 10:15:15 +00:00
parent 69a175f001
commit e2da825c30
3 changed files with 52 additions and 13 deletions
+37 -4
View File
@@ -7,6 +7,22 @@
#include "LuaManager.h"
#include "LuaBinding.h"
static vector<ActorCommands*>* g_pActorCommands;
static void Subscribe( ActorCommands* p )
{
if( g_pActorCommands == NULL )
g_pActorCommands = new vector<ActorCommands*>;
g_pActorCommands->push_back( p );
}
static void Unsubscribe( ActorCommands* p )
{
vector<ActorCommands*>::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.
+5 -2
View File
@@ -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;
};
+10 -7
View File
@@ -6,6 +6,7 @@
#include "RageFile.h"
#include "arch/Dialog/Dialog.h"
#include "Foreach.h"
#include "ActorCommands.h"
#include <csetjmp>
#include <cassert>
@@ -141,14 +142,14 @@ static int LuaPanic( lua_State *L )
// Actor registration
static vector<RegisterActorFn> *g_vRegisterActors = NULL;
static vector<RegisterActorFn> *g_vRegisterActorTypes = NULL;
void LuaManager::Register( RegisterActorFn pfn )
{
if( g_vRegisterActors == NULL )
g_vRegisterActors = new vector<RegisterActorFn>;
if( g_vRegisterActorTypes == NULL )
g_vRegisterActorTypes = new vector<RegisterActorFn>;
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; i<g_vRegisterActors->size(); i++ )
for( unsigned i=0; i<g_vRegisterActorTypes->size(); i++ )
{
RegisterActorFn fn = (*g_vRegisterActors)[i];
RegisterActorFn fn = (*g_vRegisterActorTypes)[i];
fn( L );
}
}
ActorCommands::ReRegisterAll();
}
void LuaManager::PrepareExpression( CString &sInOut )