start changing Lua to a singleton object
This commit is contained in:
@@ -27,7 +27,7 @@ Actor* ActorUtil::LoadFromActorFile( const CString& sAniDir, const XNode* pNode
|
|||||||
CString expr;
|
CString expr;
|
||||||
if( pNode->GetAttrValue("Condition",expr) )
|
if( pNode->GetAttrValue("Condition",expr) )
|
||||||
{
|
{
|
||||||
if( !Lua::RunExpressionB(expr) )
|
if( !LUA->RunExpressionB(expr) )
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ void BGAnimation::AddLayersFromAniDir( const CString &_sAniDir, const IniFile& i
|
|||||||
CString expr;
|
CString expr;
|
||||||
if( ini.GetValue( "BGAnimation", "Condition", expr ) || ini.GetValue( "BGAnimation", "Cond", expr ) )
|
if( ini.GetValue( "BGAnimation", "Condition", expr ) || ini.GetValue( "BGAnimation", "Cond", expr ) )
|
||||||
{
|
{
|
||||||
if( !Lua::RunExpressionB( expr ) )
|
if( !LUA->RunExpressionB( expr ) )
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,7 +70,7 @@ void BGAnimation::AddLayersFromAniDir( const CString &_sAniDir, const IniFile& i
|
|||||||
CString expr;
|
CString expr;
|
||||||
if( pKey->GetAttrValue("Condition",expr) )
|
if( pKey->GetAttrValue("Condition",expr) )
|
||||||
{
|
{
|
||||||
if( !Lua::RunExpressionB( expr ) )
|
if( !LUA->RunExpressionB( expr ) )
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -376,7 +376,7 @@ void BGAnimationLayer::LoadFromNode( const CString& sDir, const XNode* pNode )
|
|||||||
CString expr;
|
CString expr;
|
||||||
if( pNode->GetAttrValue("Cond",expr) || pNode->GetAttrValue("Condition",expr) )
|
if( pNode->GetAttrValue("Cond",expr) || pNode->GetAttrValue("Condition",expr) )
|
||||||
{
|
{
|
||||||
if( !Lua::RunExpressionB( expr ) )
|
if( !LUA->RunExpressionB( expr ) )
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -672,7 +672,7 @@ bool BGAnimationLayer::EarlyAbortDraw()
|
|||||||
if( m_sDrawCond.empty() )
|
if( m_sDrawCond.empty() )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if( !Lua::RunExpressionB( m_sDrawCond ) )
|
if( !LUA->RunExpressionB( m_sDrawCond ) )
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -38,20 +38,20 @@ Command::Arg::operator CString ()
|
|||||||
|
|
||||||
Command::Arg::operator float ()
|
Command::Arg::operator float ()
|
||||||
{
|
{
|
||||||
Lua::PrepareExpression( s ); // strip invalid chars
|
LUA->PrepareExpression( s ); // strip invalid chars
|
||||||
return Lua::RunExpressionF( s );
|
return LUA->RunExpressionF( s );
|
||||||
}
|
}
|
||||||
|
|
||||||
Command::Arg::operator int ()
|
Command::Arg::operator int ()
|
||||||
{
|
{
|
||||||
Lua::PrepareExpression( s ); // strip invalid chars
|
LUA->PrepareExpression( s ); // strip invalid chars
|
||||||
return (int)Lua::RunExpressionF( s );
|
return (int)LUA->RunExpressionF( s );
|
||||||
}
|
}
|
||||||
|
|
||||||
Command::Arg::operator bool ()
|
Command::Arg::operator bool ()
|
||||||
{
|
{
|
||||||
Lua::PrepareExpression( s ); // strip invalid chars
|
LUA->PrepareExpression( s ); // strip invalid chars
|
||||||
return Lua::RunExpressionF( s ) != 0.0f;
|
return LUA->RunExpressionF( s ) != 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Command::Load( const CString &sCommand )
|
void Command::Load( const CString &sCommand )
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ extern "C"
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Argument helpers: */
|
/* Argument helpers: */
|
||||||
#define LUA_ASSERT( expr, err ) if( !(expr) ) { Lua::Fail( L, err ); }
|
#define LUA_ASSERT( expr, err ) if( !(expr) ) { LUA->Fail( L, err ); }
|
||||||
|
|
||||||
/* Require exactly "need" arguments. */
|
/* Require exactly "need" arguments. */
|
||||||
#define REQ_ARGS(func, need) { \
|
#define REQ_ARGS(func, need) { \
|
||||||
@@ -35,7 +35,7 @@ extern "C"
|
|||||||
const int val = (int) lua_tonumber( L, n ); \
|
const int val = (int) lua_tonumber( L, n ); \
|
||||||
LUA_ASSERT( val >= minimum && val <= maximum, ssprintf("Argument %i to " func " must be an integer between %i and %i (got %i)", n, minimum, maximum, val) ); \
|
LUA_ASSERT( val >= minimum && val <= maximum, ssprintf("Argument %i to " func " must be an integer between %i and %i (got %i)", n, minimum, maximum, val) ); \
|
||||||
}
|
}
|
||||||
#define LUA_RETURN( expr ) { Lua::PushStack( L, expr ); return 1; }
|
#define LUA_RETURN( expr ) { LUA->PushStack( L, expr ); return 1; }
|
||||||
|
|
||||||
/* Helpers to create common functions: */
|
/* Helpers to create common functions: */
|
||||||
/* Functions that take no arguments: */
|
/* Functions that take no arguments: */
|
||||||
@@ -71,7 +71,7 @@ int LuaFunc_##func( lua_State *L ) { \
|
|||||||
REQ_ARGS( #func, 1 ); \
|
REQ_ARGS( #func, 1 ); \
|
||||||
REQ_ARG( #func, 1, string ); \
|
REQ_ARG( #func, 1, string ); \
|
||||||
CString str; \
|
CString str; \
|
||||||
Lua::PopStack( L, str ); \
|
LUA->PopStack( L, str ); \
|
||||||
LUA_RETURN( call ); \
|
LUA_RETURN( call ); \
|
||||||
} \
|
} \
|
||||||
LuaFunction( func ); /* register it */
|
LuaFunction( func ); /* register it */
|
||||||
@@ -83,8 +83,8 @@ int LuaFunc_##func( lua_State *L ) { \
|
|||||||
REQ_ARG( #func, 2, string ); \
|
REQ_ARG( #func, 2, string ); \
|
||||||
CString str1; \
|
CString str1; \
|
||||||
CString str2; \
|
CString str2; \
|
||||||
Lua::PopStack( L, str2 ); \
|
LUA->PopStack( L, str2 ); \
|
||||||
Lua::PopStack( L, str1 ); \
|
LUA->PopStack( L, str1 ); \
|
||||||
LUA_RETURN( call ); \
|
LUA_RETURN( call ); \
|
||||||
} \
|
} \
|
||||||
LuaFunction( func ); /* register it */
|
LuaFunction( func ); /* register it */
|
||||||
@@ -106,7 +106,7 @@ int LuaFunc_##func( lua_State *L ) { \
|
|||||||
REQ_ARG_NUMBER_RANGE( #func, 1, 1, NUM_PLAYERS ); \
|
REQ_ARG_NUMBER_RANGE( #func, 1, 1, NUM_PLAYERS ); \
|
||||||
const PlayerNumber pn = (PlayerNumber) (int(lua_tonumber( L, 1 ))-1); \
|
const PlayerNumber pn = (PlayerNumber) (int(lua_tonumber( L, 1 ))-1); \
|
||||||
int a1 = def; \
|
int a1 = def; \
|
||||||
Lua::GetStack( L, 2, a1 ); \
|
LUA->GetStack( L, 2, a1 ); \
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Linked list of functions we make available to Lua. */
|
/* Linked list of functions we make available to Lua. */
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
#include <csetjmp>
|
#include <csetjmp>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
static LuaManager LUAObj;
|
||||||
|
LuaManager *LUA = &LUAObj;
|
||||||
LuaFunctionList *g_LuaFunctionList = NULL;
|
LuaFunctionList *g_LuaFunctionList = NULL;
|
||||||
|
|
||||||
#if defined(_WINDOWS)
|
#if defined(_WINDOWS)
|
||||||
@@ -50,20 +52,20 @@ const char *ChunkReaderString( lua_State *L, void *ptr, size_t *size )
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lua::PushStack( lua_State *L, int out )
|
void LuaManager::PushStack( lua_State *L, int out )
|
||||||
{
|
{
|
||||||
/* XXX: stack bounds */
|
/* XXX: stack bounds */
|
||||||
lua_pushnumber( L, out );
|
lua_pushnumber( L, out );
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lua::PushStack( lua_State *L, bool out )
|
void LuaManager::PushStack( lua_State *L, bool out )
|
||||||
{
|
{
|
||||||
/* XXX: stack bounds */
|
/* XXX: stack bounds */
|
||||||
lua_pushboolean( L, out );
|
lua_pushboolean( L, out );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Lua::PushStack( lua_State *L, void *out )
|
void LuaManager::PushStack( lua_State *L, void *out )
|
||||||
{
|
{
|
||||||
if( out )
|
if( out )
|
||||||
lua_pushlightuserdata( L, out );
|
lua_pushlightuserdata( L, out );
|
||||||
@@ -71,12 +73,12 @@ void Lua::PushStack( lua_State *L, void *out )
|
|||||||
lua_pushnil( L );
|
lua_pushnil( L );
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lua::PushStack( lua_State *L, const CString &out )
|
void LuaManager::PushStack( lua_State *L, const CString &out )
|
||||||
{
|
{
|
||||||
lua_pushstring( L, out );
|
lua_pushstring( L, out );
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lua::PopStack( lua_State *L, CString &out )
|
void LuaManager::PopStack( lua_State *L, CString &out )
|
||||||
{
|
{
|
||||||
/* There must be at least one entry on the stack. */
|
/* There must be at least one entry on the stack. */
|
||||||
ASSERT( lua_gettop(L) > 0 );
|
ASSERT( lua_gettop(L) > 0 );
|
||||||
@@ -86,7 +88,7 @@ void Lua::PopStack( lua_State *L, CString &out )
|
|||||||
lua_pop( L, -1 );
|
lua_pop( L, -1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Lua::GetStack( lua_State *L, int pos, int &out )
|
bool LuaManager::GetStack( lua_State *L, int pos, int &out )
|
||||||
{
|
{
|
||||||
if( pos < 0 )
|
if( pos < 0 )
|
||||||
pos = lua_gettop(L) - pos - 1;
|
pos = lua_gettop(L) - pos - 1;
|
||||||
@@ -97,7 +99,7 @@ bool Lua::GetStack( lua_State *L, int pos, int &out )
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lua::SetGlobal( lua_State *L, const CString &sName )
|
void LuaManager::SetGlobal( lua_State *L, const CString &sName )
|
||||||
{
|
{
|
||||||
lua_setglobal( L, sName );
|
lua_setglobal( L, sName );
|
||||||
}
|
}
|
||||||
@@ -110,7 +112,7 @@ static CString jbuf_error;
|
|||||||
static int LuaPanic( lua_State *L )
|
static int LuaPanic( lua_State *L )
|
||||||
{
|
{
|
||||||
CString err;
|
CString err;
|
||||||
Lua::PopStack( L, err );
|
LUA->PopStack( L, err );
|
||||||
|
|
||||||
/* Grr. We can't throw an exception from here: it'll explode going through the
|
/* Grr. We can't throw an exception from here: it'll explode going through the
|
||||||
* Lua stack for some reason. Get it off the stack with a longjmp and throw
|
* Lua stack for some reason. Get it off the stack with a longjmp and throw
|
||||||
@@ -119,9 +121,13 @@ static int LuaPanic( lua_State *L )
|
|||||||
longjmp( jbuf, 1 );
|
longjmp( jbuf, 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_State *L = NULL;
|
|
||||||
|
|
||||||
void OpenLua()
|
void OpenLua()
|
||||||
|
{
|
||||||
|
if( LUA == NULL )
|
||||||
|
LUA = new LuaManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
LuaManager::LuaManager()
|
||||||
{
|
{
|
||||||
ASSERT( L == NULL );
|
ASSERT( L == NULL );
|
||||||
|
|
||||||
@@ -137,25 +143,15 @@ void OpenLua()
|
|||||||
luaopen_string( L );
|
luaopen_string( L );
|
||||||
lua_settop(L, 0); // luaopen_* pushes stuff onto the stack that we don't need
|
lua_settop(L, 0); // luaopen_* pushes stuff onto the stack that we don't need
|
||||||
|
|
||||||
Lua::RegisterFunctions( L );
|
RegisterFunctions( L );
|
||||||
}
|
}
|
||||||
|
|
||||||
void CloseLua()
|
LuaManager::~LuaManager()
|
||||||
{
|
{
|
||||||
ASSERT( L );
|
|
||||||
|
|
||||||
lua_close( L );
|
lua_close( L );
|
||||||
L = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_State *Lua::GetGlobalState()
|
void LuaManager::PrepareExpression( CString &sInOut )
|
||||||
{
|
|
||||||
if( L == NULL )
|
|
||||||
OpenLua();
|
|
||||||
return L;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Lua::PrepareExpression( CString &sInOut )
|
|
||||||
{
|
{
|
||||||
// HACK: Many metrics have "//" comments that Lua fails to parse.
|
// HACK: Many metrics have "//" comments that Lua fails to parse.
|
||||||
// Replace them with Lua-style comments.
|
// Replace them with Lua-style comments.
|
||||||
@@ -169,7 +165,7 @@ void Lua::PrepareExpression( CString &sInOut )
|
|||||||
sInOut.erase( 0, 1 );
|
sInOut.erase( 0, 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RunExpression( const CString &str )
|
bool LuaManager::RunExpression( const CString &str )
|
||||||
{
|
{
|
||||||
// load string
|
// load string
|
||||||
{
|
{
|
||||||
@@ -181,7 +177,7 @@ bool RunExpression( const CString &str )
|
|||||||
if( ret )
|
if( ret )
|
||||||
{
|
{
|
||||||
CString err;
|
CString err;
|
||||||
Lua::PopStack( L, err );
|
LuaManager::PopStack( L, err );
|
||||||
CString sError = ssprintf( "Lua runtime error parsing \"%s\": %s", str.c_str(), err.c_str() );
|
CString sError = ssprintf( "Lua runtime error parsing \"%s\": %s", str.c_str(), err.c_str() );
|
||||||
Dialog::OK( sError, "LUA_ERROR" );
|
Dialog::OK( sError, "LUA_ERROR" );
|
||||||
return false;
|
return false;
|
||||||
@@ -196,7 +192,7 @@ bool RunExpression( const CString &str )
|
|||||||
if( ret )
|
if( ret )
|
||||||
{
|
{
|
||||||
CString err;
|
CString err;
|
||||||
Lua::PopStack( L, err );
|
LuaManager::PopStack( L, err );
|
||||||
CString sError = ssprintf( "Lua runtime error evaluating \"%s\": %s", str.c_str(), err.c_str() );
|
CString sError = ssprintf( "Lua runtime error evaluating \"%s\": %s", str.c_str(), err.c_str() );
|
||||||
Dialog::OK( sError, "LUA_ERROR" );
|
Dialog::OK( sError, "LUA_ERROR" );
|
||||||
return false;
|
return false;
|
||||||
@@ -213,7 +209,7 @@ bool RunExpression( const CString &str )
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Lua::RunExpressionB( const CString &str )
|
bool LuaManager::RunExpressionB( const CString &str )
|
||||||
{
|
{
|
||||||
if( L == NULL )
|
if( L == NULL )
|
||||||
OpenLua();
|
OpenLua();
|
||||||
@@ -227,7 +223,7 @@ bool Lua::RunExpressionB( const CString &str )
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Lua::RunExpressionF( const CString &str )
|
float LuaManager::RunExpressionF( const CString &str )
|
||||||
{
|
{
|
||||||
if( L == NULL )
|
if( L == NULL )
|
||||||
OpenLua();
|
OpenLua();
|
||||||
@@ -241,7 +237,7 @@ float Lua::RunExpressionF( const CString &str )
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Lua::RunExpressionS( const CString &str, CString &sOut )
|
bool LuaManager::RunExpressionS( const CString &str, CString &sOut )
|
||||||
{
|
{
|
||||||
if( L == NULL )
|
if( L == NULL )
|
||||||
OpenLua();
|
OpenLua();
|
||||||
@@ -257,13 +253,13 @@ bool Lua::RunExpressionS( const CString &str, CString &sOut )
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lua::Fail( lua_State *L, const CString &err )
|
void LuaManager::Fail( lua_State *L, const CString &err )
|
||||||
{
|
{
|
||||||
lua_pushstring( L, err );
|
lua_pushstring( L, err );
|
||||||
lua_error( L );
|
lua_error( L );
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lua::RegisterFunctions( lua_State *L )
|
void LuaManager::RegisterFunctions( lua_State *L )
|
||||||
{
|
{
|
||||||
for( const LuaFunctionList *p = g_LuaFunctionList; p; p=p->next )
|
for( const LuaFunctionList *p = g_LuaFunctionList; p; p=p->next )
|
||||||
lua_register( L, p->name, p->func );
|
lua_register( L, p->name, p->func );
|
||||||
|
|||||||
@@ -2,9 +2,15 @@
|
|||||||
#define LUA_HELPERS_H
|
#define LUA_HELPERS_H
|
||||||
|
|
||||||
struct lua_State;
|
struct lua_State;
|
||||||
namespace Lua
|
class LuaManager
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
LuaManager();
|
||||||
|
~LuaManager();
|
||||||
|
|
||||||
void PrepareExpression( CString &sInOut ); // strip "//" comments and "+"
|
void PrepareExpression( CString &sInOut ); // strip "//" comments and "+"
|
||||||
|
|
||||||
|
/* Run an expression in the global environment, returning the given type. */
|
||||||
bool RunExpressionB( const CString &str );
|
bool RunExpressionB( const CString &str );
|
||||||
float RunExpressionF( const CString &str );
|
float RunExpressionF( const CString &str );
|
||||||
bool RunExpressionS( const CString &str, CString &sOut );
|
bool RunExpressionS( const CString &str, CString &sOut );
|
||||||
@@ -22,9 +28,16 @@ namespace Lua
|
|||||||
bool GetStack( lua_State *L, int pos, int &out );
|
bool GetStack( lua_State *L, int pos, int &out );
|
||||||
void SetGlobal( lua_State *L, const CString &sName );
|
void SetGlobal( lua_State *L, const CString &sName );
|
||||||
|
|
||||||
lua_State *GetGlobalState();
|
lua_State *Get() { return L; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
/* Run an expression. The result is left on the Lua stack. */
|
||||||
|
bool RunExpression( const CString &str );
|
||||||
|
lua_State *L;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern LuaManager *LUA;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ ScreenBranch::ScreenBranch( CString sClassName ) : Screen( sClassName )
|
|||||||
CString sChoice = Capitalize( as[i] );
|
CString sChoice = Capitalize( as[i] );
|
||||||
CString sCondition = CONDITION(sChoice);
|
CString sCondition = CONDITION(sChoice);
|
||||||
|
|
||||||
if( Lua::RunExpressionB(sCondition) )
|
if( LUA->RunExpressionB(sCondition) )
|
||||||
{
|
{
|
||||||
m_sChoice = sChoice;
|
m_sChoice = sChoice;
|
||||||
HandleScreenMessage( SM_GoToNextScreen );
|
HandleScreenMessage( SM_GoToNextScreen );
|
||||||
|
|||||||
Reference in New Issue
Block a user