Files
itgmania212121/stepmania/src/LuaManager.cpp
T

531 lines
12 KiB
C++
Raw Normal View History

2004-02-14 21:08:12 +00:00
#include "global.h"
2005-01-24 02:26:55 +00:00
#include "LuaManager.h"
2004-02-14 21:08:12 +00:00
#include "LuaFunctions.h"
2005-02-13 04:09:28 +00:00
#include "LuaReference.h"
2004-02-14 21:08:12 +00:00
#include "RageUtil.h"
#include "RageLog.h"
#include "RageFile.h"
#include "RageThreads.h"
2005-01-24 02:04:03 +00:00
#include "Foreach.h"
2005-12-28 04:22:24 +00:00
#include "arch/Dialog/Dialog.h"
2004-04-05 05:22:32 +00:00
#include <csetjmp>
#include <cassert>
LuaManager *LUA = NULL;
static LuaFunctionList *g_LuaFunctions = NULL;
2004-02-14 21:08:12 +00:00
2005-08-25 22:08:33 +00:00
#if defined(_XBOX)
#if defined(_XDBG)
#pragma comment(lib, "lua-5.0/lib/LibLuaXboxD.lib")
#pragma comment(lib, "lua-5.0/lib/LibLuaLibXboxD.lib")
#else
2004-06-20 02:41:53 +00:00
#pragma comment(lib, "lua-5.0/lib/LibLuaXbox.lib")
#pragma comment(lib, "lua-5.0/lib/LibLuaLibXbox.lib")
2004-06-20 01:35:25 +00:00
#endif
2005-08-25 22:08:33 +00:00
#elif !defined(_XBOX) && defined(_MSC_VER)
#pragma comment(lib, "lua-5.0/lib/LibLua.lib")
#pragma comment(lib, "lua-5.0/lib/LibLuaLib.lib")
#endif
#if defined(_MSC_VER) || defined (_XBOX)
2004-02-14 21:08:12 +00:00
/* "interaction between '_setjmp' and C++ object destruction is non-portable"
* We don't care; we'll throw a fatal exception immediately anyway. */
#pragma warning (disable : 4611)
#endif
struct ChunkReaderData
{
const CString *buf;
bool done;
ChunkReaderData() { buf = NULL; done = false; }
};
const char *ChunkReaderString( lua_State *L, void *ptr, size_t *size )
{
ChunkReaderData *data = (ChunkReaderData *) ptr;
if( data->done )
return NULL;
data->done = true;
*size = data->buf->size();
const char *ret = data->buf->data();
return ret;
}
2005-06-15 08:21:38 +00:00
void LuaManager::SetGlobal( const CString &sName, int val )
{
2005-06-16 22:19:05 +00:00
Lua *L = LUA->Get();
2005-06-16 05:48:59 +00:00
LuaHelpers::PushStack( val, L );
2005-06-16 05:46:37 +00:00
lua_setglobal( L, sName );
2005-06-16 22:19:05 +00:00
LUA->Release(L);
2005-06-15 08:21:38 +00:00
}
2005-11-29 05:24:48 +00:00
void LuaManager::SetGlobal( const CString &sName, float val )
{
Lua *L = LUA->Get();
LuaHelpers::PushStack( val, L );
lua_setglobal( L, sName );
LUA->Release(L);
}
2005-06-15 08:21:38 +00:00
void LuaManager::SetGlobal( const CString &sName, bool val )
{
2005-06-16 22:19:05 +00:00
Lua *L = LUA->Get();
2005-06-16 05:48:59 +00:00
LuaHelpers::PushStack( val, L );
2005-06-16 05:46:37 +00:00
lua_setglobal( L, sName );
2005-06-16 22:19:05 +00:00
LUA->Release(L);
2005-06-15 08:21:38 +00:00
}
void LuaManager::SetGlobal( const CString &sName, const CString &val )
{
2005-06-16 22:19:05 +00:00
Lua *L = LUA->Get();
2005-06-16 05:48:59 +00:00
LuaHelpers::PushStack( val, L );
2005-06-16 05:46:37 +00:00
lua_setglobal( L, sName );
2005-06-16 22:19:05 +00:00
LUA->Release(L);
2005-06-15 08:21:38 +00:00
}
void LuaManager::SetGlobalFromExpression( const CString &sName, const CString &expr )
{
Lua *L = LUA->Get();
if( !LuaHelpers::RunScript(L, "return " + expr, "", 1) )
{
LUA->Release(L);
return;
}
lua_setglobal( L, sName );
LUA->Release(L);
}
2005-06-15 08:21:38 +00:00
void LuaManager::UnsetGlobal( const CString &sName )
2004-02-14 21:08:12 +00:00
{
2005-06-16 22:19:05 +00:00
Lua *L = LUA->Get();
lua_pushnil( L );
2005-06-16 05:46:37 +00:00
lua_setglobal( L, sName );
2005-06-16 22:19:05 +00:00
LUA->Release(L);
}
2004-02-14 21:08:12 +00:00
void LuaHelpers::Push( const bool &Object, lua_State *L ) { lua_pushboolean( L, Object ); }
void LuaHelpers::Push( const float &Object, lua_State *L ) { lua_pushnumber( L, Object ); }
void LuaHelpers::Push( const int &Object, lua_State *L ) { lua_pushnumber( L, Object ); }
2005-08-25 02:31:07 +00:00
void LuaHelpers::Push( const CString &Object, lua_State *L ) { lua_pushlstring( L, Object.data(), Object.size() ); }
2004-02-14 21:08:12 +00:00
bool LuaHelpers::FromStack( bool &Object, int iOffset, lua_State *L ) { Object = !!lua_toboolean( L, iOffset ); return true; }
bool LuaHelpers::FromStack( float &Object, int iOffset, lua_State *L ) { Object = (float)lua_tonumber( L, iOffset ); return true; }
bool LuaHelpers::FromStack( int &Object, int iOffset, lua_State *L ) { Object = (int) lua_tonumber( L, iOffset ); return true; }
bool LuaHelpers::FromStack( CString &Object, int iOffset, lua_State *L )
2005-02-22 04:20:58 +00:00
{
const char *pStr = lua_tostring( L, iOffset );
2005-02-22 04:20:58 +00:00
if( pStr != NULL )
Object = pStr;
2005-02-22 04:20:58 +00:00
return pStr != NULL;
}
void LuaHelpers::CreateTableFromArrayB( Lua *L, const vector<bool> &aIn )
2005-02-13 04:09:28 +00:00
{
lua_newtable( L );
for( unsigned i = 0; i < aIn.size(); ++i )
{
lua_pushboolean( L, aIn[i] );
lua_rawseti( L, -2, i+1 );
}
}
void LuaHelpers::ReadArrayFromTableB( Lua *L, vector<bool> &aOut )
2005-02-13 04:09:28 +00:00
{
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 );
}
}
2004-02-14 21:08:12 +00:00
2004-02-24 08:00:04 +00:00
static int LuaPanic( lua_State *L )
2004-02-14 21:08:12 +00:00
{
CString sErr;
2005-06-15 08:28:55 +00:00
LuaHelpers::PopStack( sErr, L );
RageException::Throw( "%s", sErr.c_str() );
2004-02-14 21:08:12 +00:00
}
2005-01-24 02:04:03 +00:00
// Actor registration
2005-02-17 07:12:20 +00:00
static vector<RegisterWithLuaFn> *g_vRegisterActorTypes = NULL;
2005-01-24 02:04:03 +00:00
2005-02-17 07:12:20 +00:00
void LuaManager::Register( RegisterWithLuaFn pfn )
2005-01-24 02:04:03 +00:00
{
if( g_vRegisterActorTypes == NULL )
2005-02-17 07:12:20 +00:00
g_vRegisterActorTypes = new vector<RegisterWithLuaFn>;
2005-01-24 02:04:03 +00:00
g_vRegisterActorTypes->push_back( pfn );
2005-01-24 02:04:03 +00:00
}
2005-01-19 22:27:24 +00:00
LuaManager::LuaManager()
2004-02-14 21:08:12 +00:00
{
2005-01-24 02:04:03 +00:00
LUA = this; // so that LUA is available when we call the Register functions
L = NULL;
m_pLock = new RageMutex( "Lua" );
ResetState();
}
LuaManager::~LuaManager()
{
lua_close( L );
delete m_pLock;
}
Lua *LuaManager::Get()
{
m_pLock->Lock();
return L;
}
void LuaManager::Release( Lua *&p )
{
ASSERT( p == L );
m_pLock->Unlock();
p = NULL;
}
2005-02-16 17:33:28 +00:00
void LuaManager::RegisterTypes()
{
for( const LuaFunctionList *p = g_LuaFunctions; p; p=p->next )
lua_register( L, p->name, p->func );
2005-02-16 17:33:28 +00:00
if( g_vRegisterActorTypes )
{
for( unsigned i=0; i<g_vRegisterActorTypes->size(); i++ )
{
2005-02-17 07:12:20 +00:00
RegisterWithLuaFn fn = (*g_vRegisterActorTypes)[i];
2005-02-16 17:33:28 +00:00
fn( L );
}
}
}
void LuaManager::ResetState()
{
2005-06-16 22:17:45 +00:00
m_pLock->Lock();
if( L != NULL )
{
LuaReference::BeforeResetAll();
lua_close( L );
}
L = lua_open();
2004-02-14 21:08:12 +00:00
ASSERT( L );
lua_atpanic( L, LuaPanic );
2005-08-25 22:08:33 +00:00
2004-02-14 21:08:12 +00:00
luaopen_base( L );
2004-02-15 00:42:45 +00:00
luaopen_math( L );
luaopen_string( L );
luaopen_table( L );
2004-02-14 21:08:12 +00:00
lua_settop(L, 0); // luaopen_* pushes stuff onto the stack that we don't need
2005-02-16 17:33:28 +00:00
RegisterTypes();
LuaReference::AfterResetAll();
2005-06-16 22:17:45 +00:00
m_pLock->Unlock();
}
2004-02-14 21:08:12 +00:00
2005-06-16 22:22:37 +00:00
void LuaHelpers::PrepareExpression( CString &sInOut )
{
2005-11-29 17:41:10 +00:00
// HACK: Many metrics have "// foo" and "# foo" comments that Lua fails to parse.
// Replace them with Lua-style comments.
2005-11-29 17:41:10 +00:00
// XXX: "Foo=Func('#AABBCC')" and "Text=Adjust('// subtitle') aren't comments.
sInOut.Replace( "//", "--" );
2004-11-06 18:56:39 +00:00
sInOut.Replace( "#", "--" );
2004-11-07 06:07:34 +00:00
2005-11-29 17:41:10 +00:00
// Remove unary +, eg. "+50"; Lua doesn't support that.
if( sInOut.size() >= 1 && sInOut[0] == '+' )
sInOut.erase( 0, 1 );
}
2005-06-16 06:10:50 +00:00
bool LuaHelpers::RunScriptFile( const CString &sFile )
{
RageFile f;
if( !f.Open( sFile ) )
{
2005-02-12 22:53:51 +00:00
CString sError = ssprintf( "Couldn't open Lua script \"%s\": %s", sFile.c_str(), f.GetError().c_str() );
2005-12-29 05:52:50 +00:00
Dialog::OK( sError, "LUA_ERROR" );
return false;
}
CString sScript;
if( f.Read( sScript ) == -1 )
{
2005-02-12 22:53:51 +00:00
CString sError = ssprintf( "Error reading Lua script \"%s\": %s", sFile.c_str(), f.GetError().c_str() );
2005-12-29 05:52:50 +00:00
Dialog::OK( sError, "LUA_ERROR" );
return false;
}
2005-06-16 22:10:24 +00:00
Lua *L = LUA->Get();
CString sError;
2005-06-16 22:10:24 +00:00
if( !LuaHelpers::RunScript( L, sScript, sFile, sError, 0 ) )
{
2005-06-16 22:10:24 +00:00
LUA->Release(L);
sError = ssprintf( "Lua runtime error: %s", sError.c_str() );
2005-12-29 05:52:50 +00:00
Dialog::OK( sError, "LUA_ERROR" );
return false;
}
2005-06-16 22:10:24 +00:00
LUA->Release(L);
return true;
}
bool LuaHelpers::RunScript( Lua *L, const CString &sScript, const CString &sName, CString &sError, int iReturnValues )
{
// load string
{
ChunkReaderData data;
data.buf = &sScript;
int ret = lua_load( L, ChunkReaderString, &data, sName );
if( ret )
{
2005-06-15 08:28:55 +00:00
LuaHelpers::PopStack( sError, L );
return false;
}
}
// evaluate
{
int ret = lua_pcall( L, 0, iReturnValues, 0 );
if( ret )
{
2005-06-15 08:28:55 +00:00
LuaHelpers::PopStack( sError, L );
return false;
}
}
return true;
}
2005-01-19 23:01:53 +00:00
2005-02-12 22:53:51 +00:00
2005-06-16 06:17:32 +00:00
bool LuaHelpers::RunScript( Lua *L, const CString &sExpression, const CString &sName, int iReturnValues )
{
CString sError;
if( !LuaHelpers::RunScript( L, sExpression, sName.size()? sName:CString("in"), sError, iReturnValues ) )
{
sError = ssprintf( "Lua runtime error parsing \"%s\": %s", sName.size()? sName.c_str():sExpression.c_str(), sError.c_str() );
2005-12-29 05:52:50 +00:00
Dialog::OK( sError, "LUA_ERROR" );
2005-02-15 02:07:59 +00:00
return false;
}
return true;
}
2005-06-16 06:23:59 +00:00
bool LuaHelpers::RunExpressionB( const CString &str )
{
2005-06-16 06:23:59 +00:00
Lua *L = LUA->Get();
2005-06-16 06:17:32 +00:00
if( !LuaHelpers::RunScript(L, "return " + str, "", 1) )
2005-06-16 06:23:59 +00:00
{
LUA->Release(L);
2004-12-01 02:37:18 +00:00
return false;
2005-06-16 06:23:59 +00:00
}
/* Don't accept a function as a return value. */
if( lua_isfunction( L, -1 ) )
RageException::Throw( "result is a function; did you forget \"()\"?" );
2004-12-01 02:37:18 +00:00
bool result = !!lua_toboolean( L, -1 );
2005-04-25 09:42:54 +00:00
lua_pop( L, 1 );
2005-06-16 06:23:59 +00:00
LUA->Release(L);
2004-02-14 21:08:12 +00:00
2004-12-01 02:37:18 +00:00
return result;
2004-02-14 21:08:12 +00:00
}
2005-06-16 06:30:33 +00:00
float LuaHelpers::RunExpressionF( const CString &str )
{
2005-06-16 06:30:33 +00:00
Lua *L = LUA->Get();
2005-06-16 06:17:32 +00:00
if( !LuaHelpers::RunScript(L, "return " + str, "", 1) )
2005-06-16 06:30:33 +00:00
{
LUA->Release(L);
2004-12-01 02:37:18 +00:00
return 0;
2005-06-16 06:30:33 +00:00
}
/* Don't accept a function as a return value. */
if( lua_isfunction( L, -1 ) )
RageException::Throw( "result is a function; did you forget \"()\"?" );
2004-12-01 02:37:18 +00:00
float result = (float) lua_tonumber( L, -1 );
2005-04-25 09:42:54 +00:00
lua_pop( L, 1 );
2005-06-16 06:30:33 +00:00
LUA->Release(L);
2004-12-01 02:37:18 +00:00
return result;
2004-02-14 21:08:12 +00:00
}
int LuaHelpers::RunExpressionI( const CString &str )
2005-02-16 21:20:33 +00:00
{
2005-06-16 06:30:33 +00:00
return (int) LuaHelpers::RunExpressionF(str);
2005-02-16 21:20:33 +00:00
}
bool LuaHelpers::RunExpressionS( const CString &str, CString &sOut )
{
Lua *L = LUA->Get();
2005-06-16 06:17:32 +00:00
if( !LuaHelpers::RunScript(L, "return " + str, "", 1) )
{
LUA->Release(L);
return false;
}
/* Don't accept a function as a return value. */
if( lua_isfunction( L, -1 ) )
RageException::Throw( "result is a function; did you forget \"()\"?" );
sOut = lua_tostring( L, -1 );
2005-04-25 09:42:54 +00:00
lua_pop( L, 1 );
LUA->Release(L);
return true;
}
2005-06-16 22:17:45 +00:00
bool LuaHelpers::RunAtExpressionS( CString &sStr )
2005-01-20 01:08:26 +00:00
{
if( sStr.size() == 0 || sStr[0] != '@' )
return false;
/* Erase "@". */
sStr.erase( 0, 1 );
CString sOut;
LuaHelpers::RunExpressionS( sStr, sOut );
2005-01-20 01:08:26 +00:00
sStr = sOut;
return true;
}
/* Like luaL_typerror, but without the special case for argument 1 being "self"
* in method calls, so we give a correct error message after we remove self. */
CString GetLuaBindingType( Lua *L, int iArgNo )
{
if( lua_isnil(L, iArgNo) )
return "nil";
int iTop = lua_gettop(L);
if( !lua_getmetatable(L, iArgNo) )
{
lua_settop( L, iTop );
return ssprintf( "non-bound %s", lua_typename(L, lua_type(L, iArgNo)) );
}
int iMetatable = lua_gettop(L);
lua_pushstring( L, "type" );
lua_rawget( L, iMetatable );
CString sActualType = lua_tostring( L, -1 );
lua_settop( L, iTop );
return sActualType;
}
/* Like luaL_typerror, but without the special case for argument 1 being "self"
* in method calls, so we give a correct error message after we remove self. */
2005-06-15 08:33:26 +00:00
int LuaHelpers::TypeError( Lua *L, int iArgNo, const char *szName )
{
CString sType = GetLuaBindingType( L, iArgNo );
lua_Debug debug;
if( !lua_getstack( L, 0, &debug ) )
{
return luaL_error( L, "invalid type (%s expected, got %s)",
szName, sType.c_str() );
}
else
{
lua_getinfo( L, "n", &debug );
return luaL_error( L, "bad argument #%d to \"%s\" (%s expected, got %s)",
iArgNo, debug.name? debug.name:"(unknown)", szName, sType.c_str() );
}
}
2005-01-24 02:04:03 +00:00
2004-02-14 21:08:12 +00:00
LuaFunctionList::LuaFunctionList( CString name_, lua_CFunction func_ )
{
name = name_;
func = func_;
next = g_LuaFunctions;
g_LuaFunctions = this;
2004-02-14 21:08:12 +00:00
}
2005-02-25 04:37:27 +00:00
static bool Trace( const CString &sString )
{
LOG->Trace( "%s", sString.c_str() );
return true;
}
2005-05-29 02:21:24 +00:00
LuaFunction( Trace, Trace(SArg(1)) );
2005-06-25 02:55:58 +00:00
#include "ProductInfo.h"
LuaFunction( ProductVersion, (CString) PRODUCT_VER );
static float scale( float x, float l1, float h1, float l2, float h2 )
{
return SCALE( x, l1, h1, l2, h2 );
}
LuaFunction( scale, scale(FArg(1), FArg(2), FArg(3), FArg(4), FArg(5)) );
LuaFunction( clamp, clamp(FArg(1), FArg(2), FArg(3)) );
#include "RageTypes.h"
int LuaFunc_color( lua_State *L )
{
CString sColor = SArg(1);
RageColor c;
c.FromString( sColor );
LuaHelpers::Push( c.r, L );
LuaHelpers::Push( c.g, L );
LuaHelpers::Push( c.b, L );
LuaHelpers::Push( c.a, L );
return 4;
}
static LuaFunctionList g_color( "color", LuaFunc_color ); /* register it */
2004-02-14 21:08:12 +00:00
/*
2004-05-31 22:42:12 +00:00
* (c) 2004 Glenn Maynard
* 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.
2004-02-14 21:08:12 +00:00
*/