Files
itgmania212121/stepmania/src/LuaManager.cpp
T

436 lines
10 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"
2004-11-07 10:36:59 +00:00
#include "arch/Dialog/Dialog.h"
2005-01-24 02:04:03 +00:00
#include "Foreach.h"
2004-02-14 21:08:12 +00:00
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
#if defined(_MSC_VER) && !defined(_XBOX)
2004-06-20 02:41:53 +00:00
#pragma comment(lib, "lua-5.0/lib/LibLua.lib")
#pragma comment(lib, "lua-5.0/lib/LibLuaLib.lib")
#elif defined(_XBOX)
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
#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
2004-05-26 20:52:42 +00:00
#if defined (DARWIN)
extern void NORETURN longjmp(jmp_buf env, int val);
#endif
2004-02-14 21:08:12 +00:00
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;
}
void LuaManager::PushStackNil()
2004-02-14 21:08:12 +00:00
{
lua_pushnil( 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 ); }
void LuaHelpers::Push( const CString &Object, lua_State *L ) { lua_pushstring( L, Object ); }
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;
}
2005-02-21 06:51:10 +00:00
void LuaManager::CreateTableFromArrayB( const vector<bool> &aIn, lua_State *L )
2005-02-13 04:09:28 +00:00
{
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 );
}
}
2005-02-21 06:51:10 +00:00
void LuaManager::ReadArrayFromTableB( vector<bool> &aOut, lua_State *L )
2005-02-13 04:09:28 +00:00
{
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 );
}
}
2005-01-19 23:01:53 +00:00
bool LuaManager::GetStack( int pos, int &out )
2004-02-25 23:58:47 +00:00
{
if( pos < 0 )
pos = lua_gettop(L) - pos - 1;
if( pos < 1 )
return false;
out = (int) lua_tonumber( L, pos );
return true;
}
2005-01-19 23:01:53 +00:00
void LuaManager::SetGlobal( const CString &sName )
2005-01-09 01:31:06 +00:00
{
lua_setglobal( L, sName );
}
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;
LuaHelpers::PopStack( sErr, NULL );
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;
ResetState();
}
LuaManager::~LuaManager()
{
lua_close( L );
}
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()
{
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 );
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();
}
2004-02-14 21:08:12 +00:00
2005-01-19 22:27:24 +00:00
void LuaManager::PrepareExpression( CString &sInOut )
{
// HACK: Many metrics have "//" comments that Lua fails to parse.
// Replace them with Lua-style comments.
sInOut.Replace( "//", "--" );
2004-11-06 18:56:39 +00:00
// comment out HTML style color values
sInOut.Replace( "#", "--" );
2004-11-07 06:07:34 +00:00
// Remove leading +, eg. "+50"; Lua doesn't handle that.
if( sInOut.size() >= 1 && sInOut[0] == '+' )
sInOut.erase( 0, 1 );
}
bool LuaManager::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() );
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() );
Dialog::OK( sError, "LUA_ERROR" );
return false;
}
CString sError;
if( !RunScript( sScript, sFile, sError, 0 ) )
{
sError = ssprintf( "Lua runtime error: %s", sError.c_str() );
Dialog::OK( sError, "LUA_ERROR" );
return false;
}
return true;
}
bool LuaManager::RunScript( 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 )
{
LuaHelpers::PopStack( sError, NULL );
return false;
}
}
// evaluate
{
int ret = lua_pcall( L, 0, iReturnValues, 0 );
if( ret )
{
LuaHelpers::PopStack( sError, NULL );
return false;
}
}
return true;
}
2005-01-19 23:01:53 +00:00
2005-02-12 22:53:51 +00:00
bool LuaManager::RunScript( const CString &sExpression, const CString &sName, int iReturnValues )
{
CString sError;
2005-02-25 04:37:27 +00:00
if( !RunScript( 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() );
Dialog::OK( sError, "LUA_ERROR" );
2005-02-15 02:07:59 +00:00
return false;
}
return true;
}
2005-01-19 22:27:24 +00:00
bool LuaManager::RunExpressionB( const CString &str )
{
if( !RunScript( "return " + str, "", 1 ) )
2004-12-01 02:37:18 +00:00
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 \"()\"?" );
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 );
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-01-19 22:27:24 +00:00
float LuaManager::RunExpressionF( const CString &str )
{
if( !RunScript( "return " + str, "", 1 ) )
2004-12-01 02:37:18 +00:00
return 0;
/* 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 );
2004-12-01 02:37:18 +00:00
return result;
2004-02-14 21:08:12 +00:00
}
2005-02-16 21:20:33 +00:00
int LuaManager::RunExpressionI( const CString &str )
{
2005-02-16 21:41:07 +00:00
return (int)RunExpressionF(str);
2005-02-16 21:20:33 +00:00
}
2005-01-19 22:27:24 +00:00
bool LuaManager::RunExpressionS( const CString &str, CString &sOut )
{
if( !RunScript( "return " + str, "", 1 ) )
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 );
return true;
}
2005-02-16 00:16:14 +00:00
bool LuaManager::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;
RunExpressionS( sStr, sOut );
sStr = sOut;
return true;
}
2005-02-16 00:16:14 +00:00
float LuaManager::RunAtExpressionF( const CString &_sStr )
{
if( _sStr.size() == 0 || _sStr[0] != '@' )
return 0;
CString sStr = _sStr;
/* Erase "@". */
sStr.erase( 0, 1 );
CString sOut;
RunExpressionS( sStr, sOut );
return strtof( sOut, NULL );
2005-02-16 00:16:14 +00:00
}
2005-01-19 23:01:53 +00:00
void LuaManager::Fail( const CString &err )
2004-02-14 21:08:12 +00:00
{
lua_pushstring( L, err );
lua_error( L );
}
/* 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. */
int LuaHelpers::TypeError( int iArgNo, const char *szName )
{
lua_Debug debug;
lua_getstack( LUA->L, 0, &debug );
lua_getinfo( LUA->L, "n", &debug );
return luaL_error( LUA->L, "bad argument #%d to \"%s\" (%s expected, got %s)",
iArgNo, debug.name? debug.name:"(unknown)", szName, lua_typename(LUA->L, lua_type(LUA->L, iArgNo)) );
}
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
}
2004-02-25 23:58:47 +00:00
LuaFunction_NoArgs( MonthOfYear, GetLocalTime().tm_mon+1 );
2004-02-14 21:08:12 +00:00
LuaFunction_NoArgs( DayOfMonth, GetLocalTime().tm_mday );
LuaFunction_NoArgs( Hour, GetLocalTime().tm_hour );
LuaFunction_NoArgs( Minute, GetLocalTime().tm_min );
LuaFunction_NoArgs( Second, GetLocalTime().tm_sec );
LuaFunction_NoArgs( Year, GetLocalTime().tm_year+1900 );
LuaFunction_NoArgs( Weekday, GetLocalTime().tm_wday );
LuaFunction_NoArgs( DayOfYear, GetLocalTime().tm_yday );
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)) );
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
*/