re-register all ActorCommands after resetting Lua
This commit is contained in:
@@ -7,6 +7,22 @@
|
|||||||
#include "LuaManager.h"
|
#include "LuaManager.h"
|
||||||
#include "LuaBinding.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 CString GetNextFunctionName()
|
||||||
{
|
{
|
||||||
static int id = 0;
|
static int id = 0;
|
||||||
@@ -16,17 +32,25 @@ static CString GetNextFunctionName()
|
|||||||
|
|
||||||
ActorCommands::ActorCommands( const Commands& cmds )
|
ActorCommands::ActorCommands( const Commands& cmds )
|
||||||
{
|
{
|
||||||
Register( cmds );
|
Subscribe( this );
|
||||||
|
|
||||||
|
m_cmds = cmds;
|
||||||
|
|
||||||
|
Register();
|
||||||
}
|
}
|
||||||
|
|
||||||
ActorCommands::~ActorCommands()
|
ActorCommands::~ActorCommands()
|
||||||
{
|
{
|
||||||
|
Unsubscribe( this );
|
||||||
|
|
||||||
if( m_sLuaFunctionName.size() )
|
if( m_sLuaFunctionName.size() )
|
||||||
Unregister();
|
Unregister();
|
||||||
}
|
}
|
||||||
|
|
||||||
ActorCommands::ActorCommands( const ActorCommands& cpy )
|
ActorCommands::ActorCommands( const ActorCommands& cpy )
|
||||||
{
|
{
|
||||||
|
Subscribe( this );
|
||||||
|
|
||||||
m_sLuaFunctionName = GetNextFunctionName();
|
m_sLuaFunctionName = GetNextFunctionName();
|
||||||
|
|
||||||
/* We need to make a new function, since we'll be unregistered separately. Set
|
/* 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() )
|
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";
|
m_sLuaFunctionName = "nop";
|
||||||
return;
|
return;
|
||||||
@@ -76,7 +100,7 @@ void ActorCommands::Register( const Commands& cmds )
|
|||||||
|
|
||||||
s << m_sLuaFunctionName << " = function(self)\n";
|
s << m_sLuaFunctionName << " = function(self)\n";
|
||||||
|
|
||||||
FOREACH_CONST( Command, cmds.v, c )
|
FOREACH_CONST( Command, m_cmds.v, c )
|
||||||
{
|
{
|
||||||
const Command& cmd = (*c);
|
const Command& cmd = (*c);
|
||||||
CString sName = cmd.GetName();
|
CString sName = cmd.GetName();
|
||||||
@@ -146,6 +170,15 @@ void ActorCommands::Unregister()
|
|||||||
m_sLuaFunctionName = "";
|
m_sLuaFunctionName = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ActorCommands::ReRegisterAll()
|
||||||
|
{
|
||||||
|
if( g_pActorCommands == NULL )
|
||||||
|
return;
|
||||||
|
FOREACH( ActorCommands*, *g_pActorCommands, p )
|
||||||
|
(*p)->Register();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (c) 2004 Chris Danford
|
* (c) 2004 Chris Danford
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#define ActorCommands_H
|
#define ActorCommands_H
|
||||||
|
|
||||||
#include "RageUtil_AutoPtr.h"
|
#include "RageUtil_AutoPtr.h"
|
||||||
class Commands;
|
#include "Command.h"
|
||||||
|
|
||||||
struct lua_State;
|
struct lua_State;
|
||||||
|
|
||||||
@@ -18,10 +18,13 @@ public:
|
|||||||
|
|
||||||
void PushSelf( lua_State *L ) const;
|
void PushSelf( lua_State *L ) const;
|
||||||
|
|
||||||
|
static void ReRegisterAll(); // call this after resetting Lua
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Register( const Commands& cmds );
|
void Register();
|
||||||
void Unregister();
|
void Unregister();
|
||||||
|
|
||||||
|
Commands m_cmds;
|
||||||
CString m_sLuaFunctionName;
|
CString m_sLuaFunctionName;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "RageFile.h"
|
#include "RageFile.h"
|
||||||
#include "arch/Dialog/Dialog.h"
|
#include "arch/Dialog/Dialog.h"
|
||||||
#include "Foreach.h"
|
#include "Foreach.h"
|
||||||
|
#include "ActorCommands.h"
|
||||||
|
|
||||||
#include <csetjmp>
|
#include <csetjmp>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
@@ -141,14 +142,14 @@ static int LuaPanic( lua_State *L )
|
|||||||
|
|
||||||
|
|
||||||
// Actor registration
|
// Actor registration
|
||||||
static vector<RegisterActorFn> *g_vRegisterActors = NULL;
|
static vector<RegisterActorFn> *g_vRegisterActorTypes = NULL;
|
||||||
|
|
||||||
void LuaManager::Register( RegisterActorFn pfn )
|
void LuaManager::Register( RegisterActorFn pfn )
|
||||||
{
|
{
|
||||||
if( g_vRegisterActors == NULL )
|
if( g_vRegisterActorTypes == NULL )
|
||||||
g_vRegisterActors = new vector<RegisterActorFn>;
|
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 )
|
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 )
|
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 );
|
fn( L );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ActorCommands::ReRegisterAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaManager::PrepareExpression( CString &sInOut )
|
void LuaManager::PrepareExpression( CString &sInOut )
|
||||||
|
|||||||
Reference in New Issue
Block a user