2005-02-13 04:00:48 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "LuaReference.h"
|
|
|
|
|
#include "LuaManager.h"
|
|
|
|
|
#include "LuaBinding.h"
|
|
|
|
|
#include "Foreach.h"
|
2005-02-16 00:38:40 +00:00
|
|
|
#include "RageLog.h"
|
2005-02-13 04:00:48 +00:00
|
|
|
#include "SubscriptionManager.h"
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
set<LuaReference*>* SubscriptionManager<LuaReference>::s_pSubscribers = NULL;
|
|
|
|
|
|
|
|
|
|
LuaReference::LuaReference()
|
|
|
|
|
{
|
|
|
|
|
m_iReference = LUA_NOREF;
|
|
|
|
|
SubscriptionManager<LuaReference>::Subscribe( this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaReference::~LuaReference()
|
|
|
|
|
{
|
|
|
|
|
Unregister();
|
|
|
|
|
SubscriptionManager<LuaReference>::Unsubscribe( this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaReference::LuaReference( const LuaReference &cpy )
|
|
|
|
|
{
|
|
|
|
|
SubscriptionManager<LuaReference>::Subscribe( this );
|
|
|
|
|
|
2005-02-22 03:22:56 +00:00
|
|
|
if( cpy.m_iReference == LUA_NOREF )
|
|
|
|
|
m_iReference = LUA_NOREF;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Make a new reference. */
|
2005-06-16 03:13:06 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
lua_rawgeti( L, LUA_REGISTRYINDEX, cpy.m_iReference );
|
|
|
|
|
m_iReference = luaL_ref( L, LUA_REGISTRYINDEX );
|
|
|
|
|
LUA->Release( L );
|
2005-02-22 03:22:56 +00:00
|
|
|
}
|
2005-02-13 04:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaReference &LuaReference::operator=( const LuaReference &cpy )
|
|
|
|
|
{
|
|
|
|
|
if( this == &cpy )
|
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
|
|
Unregister();
|
|
|
|
|
|
2005-02-23 06:17:29 +00:00
|
|
|
if( cpy.m_iReference == LUA_NOREF )
|
2005-08-29 00:55:44 +00:00
|
|
|
{
|
2005-02-23 06:17:29 +00:00
|
|
|
m_iReference = LUA_NOREF;
|
2005-08-29 00:55:44 +00:00
|
|
|
}
|
2005-02-23 06:17:29 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Make a new reference. */
|
2005-06-16 03:13:06 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
lua_rawgeti( L, LUA_REGISTRYINDEX, cpy.m_iReference );
|
|
|
|
|
m_iReference = luaL_ref( L, LUA_REGISTRYINDEX );
|
|
|
|
|
LUA->Release( L );
|
2005-02-23 06:17:29 +00:00
|
|
|
}
|
2005-02-13 04:00:48 +00:00
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-16 03:13:06 +00:00
|
|
|
void LuaReference::SetFromStack( Lua *L )
|
2005-02-13 04:00:48 +00:00
|
|
|
{
|
|
|
|
|
Unregister();
|
|
|
|
|
|
2005-06-16 03:13:06 +00:00
|
|
|
m_iReference = luaL_ref( L, LUA_REGISTRYINDEX );
|
2005-02-13 04:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaReference::SetFromNil()
|
|
|
|
|
{
|
|
|
|
|
Unregister();
|
|
|
|
|
m_iReference = LUA_REFNIL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaReference::PushSelf( lua_State *L ) const
|
|
|
|
|
{
|
2005-06-16 03:13:06 +00:00
|
|
|
lua_rawgeti( L, LUA_REGISTRYINDEX, m_iReference );
|
2005-02-13 04:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaReference::IsSet() const
|
|
|
|
|
{
|
|
|
|
|
return m_iReference != LUA_NOREF;
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-23 06:17:29 +00:00
|
|
|
bool LuaReference::IsNil() const
|
|
|
|
|
{
|
|
|
|
|
return m_iReference == LUA_REFNIL;
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-14 01:31:10 +00:00
|
|
|
int LuaReference::GetLuaType() const
|
|
|
|
|
{
|
2005-06-16 03:13:06 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
this->PushSelf( L );
|
|
|
|
|
int iRet = lua_type( L, -1 );
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
LUA->Release( L );
|
|
|
|
|
|
2005-02-14 01:31:10 +00:00
|
|
|
return iRet;
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-13 04:00:48 +00:00
|
|
|
void LuaReference::Unregister()
|
|
|
|
|
{
|
|
|
|
|
if( LUA == NULL )
|
|
|
|
|
return; // nothing to do
|
|
|
|
|
|
2005-06-16 03:13:06 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
luaL_unref( L, LUA_REGISTRYINDEX, m_iReference );
|
|
|
|
|
LUA->Release( L );
|
2005-02-13 04:00:48 +00:00
|
|
|
m_iReference = LUA_NOREF;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-21 02:41:35 +00:00
|
|
|
void LuaReference::BeforeResetAll()
|
2005-02-16 00:38:40 +00:00
|
|
|
{
|
|
|
|
|
if( SubscriptionManager<LuaReference>::s_pSubscribers == NULL )
|
|
|
|
|
return;
|
|
|
|
|
FOREACHS( LuaReference*, *SubscriptionManager<LuaReference>::s_pSubscribers, p )
|
|
|
|
|
(*p)->BeforeReset();
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-21 02:41:35 +00:00
|
|
|
void LuaReference::AfterResetAll()
|
2005-02-13 04:00:48 +00:00
|
|
|
{
|
|
|
|
|
if( SubscriptionManager<LuaReference>::s_pSubscribers == NULL )
|
|
|
|
|
return;
|
|
|
|
|
FOREACHS( LuaReference*, *SubscriptionManager<LuaReference>::s_pSubscribers, p )
|
|
|
|
|
(*p)->ReRegister();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaReference::ReRegister()
|
|
|
|
|
{
|
|
|
|
|
/* When this is 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_iReference = LUA_NOREF;
|
|
|
|
|
|
|
|
|
|
Register();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 01:31:10 +00:00
|
|
|
void LuaExpression::SetFromExpression( const CString &sExpression )
|
|
|
|
|
{
|
2005-02-17 05:46:31 +00:00
|
|
|
m_sExpression = "return " + sExpression;
|
2005-02-14 01:31:10 +00:00
|
|
|
Register();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaExpression::Register()
|
|
|
|
|
{
|
2005-06-16 03:13:06 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
|
2005-06-16 06:17:32 +00:00
|
|
|
if( !LuaHelpers::RunScript(L, m_sExpression, "expression", 1) )
|
2005-02-17 05:46:31 +00:00
|
|
|
{
|
|
|
|
|
this->SetFromNil();
|
2005-06-16 03:13:06 +00:00
|
|
|
LUA->Release( L );
|
2005-02-17 05:46:31 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2005-02-14 01:31:10 +00:00
|
|
|
|
|
|
|
|
/* Store the result. */
|
2005-06-16 03:13:06 +00:00
|
|
|
this->SetFromStack( L );
|
|
|
|
|
LUA->Release( L );
|
2005-02-14 01:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
2005-02-16 02:42:05 +00:00
|
|
|
CString LuaData::Serialize() const
|
2005-02-16 00:38:40 +00:00
|
|
|
{
|
|
|
|
|
/* Call Serialize(t), where t is our referenced object. */
|
2005-06-16 03:13:06 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
lua_pushstring( L, "Serialize" );
|
|
|
|
|
lua_gettable( L, LUA_GLOBALSINDEX );
|
2005-02-16 00:38:40 +00:00
|
|
|
|
2005-06-16 03:13:06 +00:00
|
|
|
ASSERT_M( !lua_isnil(L, -1), "Serialize() missing" );
|
|
|
|
|
ASSERT_M( lua_isfunction(L, -1), "Serialize() not a function" );
|
2005-02-16 00:38:40 +00:00
|
|
|
|
2005-02-16 01:46:29 +00:00
|
|
|
/* Arg 1 (t): */
|
2005-06-16 03:13:06 +00:00
|
|
|
this->PushSelf( L );
|
2005-02-16 01:46:29 +00:00
|
|
|
|
2005-06-16 03:13:06 +00:00
|
|
|
lua_call( L, 1, 1 );
|
2005-02-16 00:38:40 +00:00
|
|
|
|
|
|
|
|
/* The return value is a string, which we store in m_sSerializedData. */
|
2005-06-16 03:13:06 +00:00
|
|
|
const char *pString = lua_tostring( L, -1 );
|
2005-02-17 00:00:04 +00:00
|
|
|
ASSERT_M( pString != NULL, "Serialize() didn't return a string" );
|
2005-02-16 00:38:40 +00:00
|
|
|
|
2005-02-16 02:42:05 +00:00
|
|
|
CString sRet = pString;
|
2005-06-16 03:13:06 +00:00
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
|
|
|
|
|
LUA->Release( L );
|
2005-02-16 02:42:05 +00:00
|
|
|
|
|
|
|
|
return sRet;
|
2005-02-16 00:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
2005-02-16 02:42:05 +00:00
|
|
|
void LuaData::LoadFromString( const CString &s )
|
2005-02-16 00:38:40 +00:00
|
|
|
{
|
2005-06-16 03:13:06 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
|
2005-02-17 00:44:26 +00:00
|
|
|
/* Restore the serialized data by evaluating it. */
|
2005-02-16 00:38:40 +00:00
|
|
|
CString sError;
|
2005-06-16 06:00:39 +00:00
|
|
|
if( !LuaHelpers::RunScript( L, s, "serialization", sError, 1 ) )
|
2005-02-16 00:38:40 +00:00
|
|
|
{
|
|
|
|
|
/* Serialize() should never return an invalid script. Drop the failed
|
|
|
|
|
* script into the log (it may be too big to pass to FAIL_M) and fail. */
|
2005-02-16 02:42:05 +00:00
|
|
|
LOG->Warn( "Unserialization of \"%s\" failed: %s", s.c_str(), sError.c_str() );
|
2005-02-16 00:38:40 +00:00
|
|
|
FAIL_M( "Unserialization failed" );
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-16 03:13:06 +00:00
|
|
|
this->SetFromStack( L );
|
|
|
|
|
LUA->Release( L );
|
2005-02-16 02:42:05 +00:00
|
|
|
}
|
2005-02-16 00:38:40 +00:00
|
|
|
|
2005-02-16 02:42:05 +00:00
|
|
|
void LuaData::BeforeReset()
|
|
|
|
|
{
|
2005-02-27 00:22:13 +00:00
|
|
|
/* If we're unset, Register() should leave us unset, not set us to LUA_REFNIL. */
|
|
|
|
|
m_bWasSet = IsSet();
|
|
|
|
|
|
|
|
|
|
if( m_bWasSet )
|
|
|
|
|
m_sSerializedData = Serialize();
|
2005-02-16 02:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaData::Register()
|
|
|
|
|
{
|
2005-02-27 00:22:13 +00:00
|
|
|
if( !m_bWasSet )
|
|
|
|
|
return;
|
|
|
|
|
|
2005-02-16 02:42:05 +00:00
|
|
|
LoadFromString( m_sSerializedData );
|
2005-02-16 03:29:11 +00:00
|
|
|
m_sSerializedData.erase( m_sSerializedData.begin(), m_sSerializedData.end() );
|
2005-02-16 00:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
2005-03-18 22:40:09 +00:00
|
|
|
LuaTable::LuaTable()
|
|
|
|
|
{
|
2005-06-16 03:13:06 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
lua_newtable( L );
|
|
|
|
|
this->SetFromStack(L);
|
|
|
|
|
LUA->Release( L );
|
2005-03-18 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
2005-06-16 03:13:06 +00:00
|
|
|
void LuaTable::Set( Lua *L, const CString &sKey )
|
2005-03-18 22:40:09 +00:00
|
|
|
{
|
2005-06-16 03:13:06 +00:00
|
|
|
int iTop = lua_gettop( L );
|
|
|
|
|
this->PushSelf( L );
|
|
|
|
|
lua_pushstring( L, sKey ); // push the key
|
|
|
|
|
lua_pushvalue( L, iTop ); // push the value
|
|
|
|
|
lua_settable( L, iTop+1 );
|
|
|
|
|
lua_settop( L, iTop-1 ); // remove all of the above
|
2005-03-18 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
2005-06-16 03:13:06 +00:00
|
|
|
void LuaTable::Unset( Lua *L, const CString &sKey )
|
2005-03-18 22:40:09 +00:00
|
|
|
{
|
2005-06-16 03:13:06 +00:00
|
|
|
lua_pushnil( L );
|
|
|
|
|
Set( L, sKey );
|
2005-03-18 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
2005-06-16 03:13:06 +00:00
|
|
|
void LuaTable::SetKeyAndValue( Lua *L )
|
2005-03-18 22:40:09 +00:00
|
|
|
{
|
2005-06-16 03:13:06 +00:00
|
|
|
int iTop = lua_gettop( L );
|
|
|
|
|
this->PushSelf( L );
|
|
|
|
|
lua_pushvalue( L, iTop-1 ); // push the value after the table
|
|
|
|
|
lua_pushvalue( L, iTop ); // push the key after the value
|
|
|
|
|
lua_settable( L, iTop+1 );
|
|
|
|
|
lua_settop( L, iTop-1 ); // remove all of the above
|
2005-03-18 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
2005-02-13 04:00:48 +00:00
|
|
|
/*
|
|
|
|
|
* (c) 2005 Glenn Maynard, Chris Danford
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, and/or sell copies of the Software, and to permit persons to
|
|
|
|
|
* whom the Software is furnished to do so, provided that the above
|
|
|
|
|
* copyright notice(s) and this permission notice appear in all copies of
|
|
|
|
|
* the Software and that both the above copyright notice(s) and this
|
|
|
|
|
* permission notice appear in supporting documentation.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
|
|
|
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
|
|
|
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
|
|
|
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|