From fe6cd41543862c8fc96e93c0da48acdf57ebd4d5 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 13 Feb 2005 04:09:28 +0000 Subject: [PATCH] update add LuaManager::CreateTableFromArray, LuaManager::ReadArrayFromTable --- stepmania/src/LuaManager.cpp | 33 +++++++++++++++++++++++++++++++-- stepmania/src/LuaManager.h | 5 +++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/stepmania/src/LuaManager.cpp b/stepmania/src/LuaManager.cpp index c7d7b20065..db444832bf 100644 --- a/stepmania/src/LuaManager.cpp +++ b/stepmania/src/LuaManager.cpp @@ -1,12 +1,12 @@ #include "global.h" #include "LuaManager.h" #include "LuaFunctions.h" +#include "LuaReference.h" #include "RageUtil.h" #include "RageLog.h" #include "RageFile.h" #include "arch/Dialog/Dialog.h" #include "Foreach.h" -#include "ActorCommands.h" #include #include @@ -111,6 +111,35 @@ void LuaManager::PushStack( const CString &out, lua_State *L ) lua_pushstring( L, out ); } +void LuaManager::CreateTableFromArray( const vector &aIn, lua_State *L ) +{ + if( L == NULL ) + L = LUA->L; + + lua_newtable( L ); + for( unsigned i = 0; i < aIn.size(); ++i ) + { + lua_pushboolean( L, aIn[i] ); + lua_rawseti( L, -2, i+1 ); + } +} + +void LuaManager::ReadArrayFromTable( vector &aOut, lua_State *L ) +{ + if( L == NULL ) + L = LUA->L; + + luaL_checktype( L, -1, LUA_TTABLE ); + + for( unsigned i = 0; i < aOut.size(); ++i ) + { + lua_rawgeti( L, -1, i+1 ); + bool bOn = !!lua_toboolean( L, -1 ); + aOut[i] = bOn; + lua_pop( L, 1 ); + } +} + void LuaManager::PopStack( CString &out ) { /* There must be at least one entry on the stack. */ @@ -208,7 +237,7 @@ void LuaManager::ResetState() } } - ActorCommands::ReRegisterAll(); + LuaReference::ReRegisterAll(); } void LuaManager::PrepareExpression( CString &sInOut ) diff --git a/stepmania/src/LuaManager.h b/stepmania/src/LuaManager.h index d36a980b4e..0933ddf83a 100644 --- a/stepmania/src/LuaManager.h +++ b/stepmania/src/LuaManager.h @@ -49,6 +49,11 @@ public: bool GetStack( int pos, int &out ); void SetGlobal( const CString &sName ); + /* Create a Lua array (a table with indices starting at 1) of the given vector, + * and push it on the stack. */ + static void CreateTableFromArray( const vector &aIn, lua_State *L = NULL ); + /* Read the table at the top of the stack back into a vector. */ + static void ReadArrayFromTable( vector &aOut, lua_State *L = NULL ); /* Run an expression. The result is left on the Lua stack. */ bool RunExpression( const CString &str );