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"
|
2005-01-19 23:33:19 +00:00
|
|
|
#include "RageFile.h"
|
2005-06-15 08:16:28 +00:00
|
|
|
#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"
|
2006-08-08 07:43:12 +00:00
|
|
|
#include "XmlFile.h"
|
2005-12-28 04:22:24 +00:00
|
|
|
|
2004-04-05 05:22:32 +00:00
|
|
|
#include <csetjmp>
|
|
|
|
|
#include <cassert>
|
2005-05-29 21:12:30 +00:00
|
|
|
|
2005-01-19 23:33:19 +00:00
|
|
|
LuaManager *LUA = NULL;
|
|
|
|
|
static LuaFunctionList *g_LuaFunctions = NULL;
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2005-05-11 04:43:54 +00:00
|
|
|
#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
|
|
|
|
|
|
2006-09-23 02:24:23 +00:00
|
|
|
struct ChunkReaderString
|
2004-02-14 21:08:12 +00:00
|
|
|
{
|
2006-09-23 02:24:23 +00:00
|
|
|
ChunkReaderString( const RString &sBuf ): m_sBuf(sBuf) { m_bDone = false; }
|
|
|
|
|
static const char *Reader( lua_State *L, void *ptr, size_t *size );
|
|
|
|
|
|
|
|
|
|
const RString m_sBuf;
|
|
|
|
|
bool m_bDone;
|
2004-02-14 21:08:12 +00:00
|
|
|
};
|
|
|
|
|
|
2006-09-23 02:24:23 +00:00
|
|
|
const char *ChunkReaderString::Reader( lua_State *L, void *pPtr, size_t *pSize )
|
2004-02-14 21:08:12 +00:00
|
|
|
{
|
2006-09-23 02:24:23 +00:00
|
|
|
ChunkReaderString *pData = (ChunkReaderString *) pPtr;
|
|
|
|
|
if( pData->m_bDone )
|
2004-02-14 21:08:12 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
2006-09-23 02:24:23 +00:00
|
|
|
pData->m_bDone = true;
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2006-09-23 02:24:23 +00:00
|
|
|
*pSize = pData->m_sBuf.size();
|
|
|
|
|
const char *pRet = pData->m_sBuf.data();
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2006-09-23 02:24:23 +00:00
|
|
|
return pRet;
|
2004-02-14 21:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void LuaManager::SetGlobal( const RString &sName, int val )
|
2005-06-15 08:21:38 +00:00
|
|
|
{
|
2005-06-16 22:19:05 +00:00
|
|
|
Lua *L = LUA->Get();
|
2006-09-21 07:06:18 +00:00
|
|
|
LuaHelpers::Push( val, L );
|
2005-06-16 05:46:37 +00:00
|
|
|
lua_setglobal( L, sName );
|
2006-09-21 07:25:38 +00:00
|
|
|
LUA->Release( L );
|
2005-06-15 08:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void LuaManager::SetGlobal( const RString &sName, const RString &val )
|
2005-06-15 08:21:38 +00:00
|
|
|
{
|
2005-06-16 22:19:05 +00:00
|
|
|
Lua *L = LUA->Get();
|
2006-09-21 07:06:18 +00:00
|
|
|
LuaHelpers::Push( val, L );
|
2005-06-16 05:46:37 +00:00
|
|
|
lua_setglobal( L, sName );
|
2006-09-21 07:25:38 +00:00
|
|
|
LUA->Release( L );
|
2005-06-15 08:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void LuaManager::UnsetGlobal( const RString &sName )
|
2004-02-14 21:08:12 +00:00
|
|
|
{
|
2005-06-16 22:19:05 +00:00
|
|
|
Lua *L = LUA->Get();
|
2005-01-30 02:01:27 +00:00
|
|
|
lua_pushnil( L );
|
2005-06-16 05:46:37 +00:00
|
|
|
lua_setglobal( L, sName );
|
2006-09-21 07:25:38 +00:00
|
|
|
LUA->Release( L );
|
2005-01-30 02:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2005-02-24 20:03:07 +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 ); }
|
2006-09-25 08:46:18 +00:00
|
|
|
void LuaHelpers::Push( const int &Object, lua_State *L ) { lua_pushinteger( L, Object ); }
|
2006-01-22 01:00:06 +00:00
|
|
|
void LuaHelpers::Push( const RString &Object, lua_State *L ) { lua_pushlstring( L, Object.data(), Object.size() ); }
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2006-09-22 08:38:30 +00:00
|
|
|
bool LuaHelpers::FromStack( Lua *L, bool &Object, int iOffset ) { Object = !!lua_toboolean( L, iOffset ); return true; }
|
|
|
|
|
bool LuaHelpers::FromStack( Lua *L, float &Object, int iOffset ) { Object = (float)lua_tonumber( L, iOffset ); return true; }
|
2006-09-25 08:46:18 +00:00
|
|
|
bool LuaHelpers::FromStack( Lua *L, int &Object, int iOffset ) { Object = lua_tointeger( L, iOffset ); return true; }
|
2006-09-22 08:38:30 +00:00
|
|
|
bool LuaHelpers::FromStack( Lua *L, RString &Object, int iOffset )
|
2005-02-22 04:20:58 +00:00
|
|
|
{
|
2006-09-25 08:33:01 +00:00
|
|
|
size_t iLen;
|
|
|
|
|
const char *pStr = lua_tolstring( L, iOffset, &iLen );
|
2005-02-22 04:20:58 +00:00
|
|
|
if( pStr != NULL )
|
2006-09-25 08:33:01 +00:00
|
|
|
Object.assign( pStr, iLen );
|
2005-02-22 04:20:58 +00:00
|
|
|
|
|
|
|
|
return pStr != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-16 05:37:31 +00:00
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-16 05:37:31 +00:00
|
|
|
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
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sErr;
|
2006-09-22 08:48:49 +00:00
|
|
|
LuaHelpers::Pop( L, sErr );
|
2006-08-13 03:03:27 +00:00
|
|
|
|
|
|
|
|
lua_Debug ar;
|
|
|
|
|
int level = 0;
|
|
|
|
|
|
|
|
|
|
while( lua_getstack(L, level++, &ar) )
|
|
|
|
|
{
|
|
|
|
|
if( !lua_getinfo(L, "nSluf", &ar) )
|
|
|
|
|
break;
|
|
|
|
|
// The function is now on the top of the stack.
|
|
|
|
|
const char *file = ar.source[0] == '@' ? ar.source + 1 : ar.short_src;
|
|
|
|
|
const char *name;
|
|
|
|
|
vector<RString> vArgs;
|
|
|
|
|
|
2006-08-13 04:21:18 +00:00
|
|
|
for( int i = 1; i <= ar.nups && (name = lua_getupvalue(L, -1, i)) != NULL; ++i )
|
2006-08-13 03:03:27 +00:00
|
|
|
{
|
|
|
|
|
// XXX: do we need to do local variables for lua functions instead?
|
|
|
|
|
vArgs.push_back( ssprintf("%s = %s", name, lua_tostring(L, -1)) );
|
|
|
|
|
lua_pop( L, 1 ); // pop value
|
|
|
|
|
}
|
|
|
|
|
lua_pop( L, 1 ); // pop function
|
|
|
|
|
|
|
|
|
|
name = ar.name ? ar.name : "[UNKNOWN]";
|
|
|
|
|
sErr += ssprintf( "\n%s %s %s( %s ) %s:%d", ar.namewhat, ar.what, name,
|
|
|
|
|
join(",", vArgs).c_str(), file, ar.currentline );
|
|
|
|
|
}
|
2005-01-28 19:42:44 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2005-02-05 10:15:15 +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
|
|
|
|
2005-02-05 10:15:15 +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
|
|
|
|
|
|
2005-06-15 08:16:28 +00:00
|
|
|
m_pLock = new RageMutex( "Lua" );
|
2005-01-19 23:33:19 +00:00
|
|
|
|
2006-09-20 23:47:08 +00:00
|
|
|
L = lua_open();
|
|
|
|
|
ASSERT( L );
|
|
|
|
|
|
|
|
|
|
lua_atpanic( L, LuaPanic );
|
|
|
|
|
|
|
|
|
|
luaopen_base( L );
|
|
|
|
|
luaopen_math( L );
|
|
|
|
|
luaopen_string( L );
|
|
|
|
|
luaopen_table( L );
|
|
|
|
|
lua_settop(L, 0); // luaopen_* pushes stuff onto the stack that we don't need
|
|
|
|
|
|
|
|
|
|
RegisterTypes();
|
2005-01-19 23:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaManager::~LuaManager()
|
|
|
|
|
{
|
|
|
|
|
lua_close( L );
|
2005-06-15 08:16:28 +00:00
|
|
|
delete m_pLock;
|
2005-01-19 23:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-14 06:53:09 +00:00
|
|
|
/* Keep track of Lua stack, and enforce that when we release Lua, the stack is
|
|
|
|
|
* back where it was when we locked it. */
|
|
|
|
|
static int g_iStackCounts[32];
|
|
|
|
|
static int g_iNumStackCounts = 0;
|
2005-06-15 08:16:28 +00:00
|
|
|
Lua *LuaManager::Get()
|
|
|
|
|
{
|
|
|
|
|
m_pLock->Lock();
|
2006-09-13 03:11:38 +00:00
|
|
|
if( size_t(g_iNumStackCounts) < ARRAYLEN(g_iStackCounts) )
|
2006-01-14 06:53:09 +00:00
|
|
|
{
|
2006-09-21 07:25:38 +00:00
|
|
|
g_iStackCounts[g_iNumStackCounts] = lua_gettop( L );
|
2006-01-14 06:53:09 +00:00
|
|
|
}
|
|
|
|
|
++g_iNumStackCounts;
|
2005-06-15 08:16:28 +00:00
|
|
|
return L;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaManager::Release( Lua *&p )
|
|
|
|
|
{
|
|
|
|
|
ASSERT( p == L );
|
2006-01-14 06:53:09 +00:00
|
|
|
ASSERT( g_iNumStackCounts != 0 );
|
|
|
|
|
--g_iNumStackCounts;
|
2006-09-13 03:11:38 +00:00
|
|
|
if( size_t(g_iNumStackCounts) < ARRAYLEN(g_iStackCounts) )
|
2006-01-14 06:53:09 +00:00
|
|
|
{
|
|
|
|
|
ASSERT( g_iStackCounts[g_iNumStackCounts] == lua_gettop(L) );
|
|
|
|
|
}
|
2005-06-15 08:16:28 +00:00
|
|
|
m_pLock->Unlock();
|
|
|
|
|
p = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-02-16 17:33:28 +00:00
|
|
|
void LuaManager::RegisterTypes()
|
|
|
|
|
{
|
2005-05-29 21:12:30 +00:00
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2006-08-08 07:43:12 +00:00
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
struct LClass
|
|
|
|
|
{
|
|
|
|
|
RString m_sBaseName;
|
|
|
|
|
vector<RString> m_vMethods;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XNode *LuaManager::GetLuaInformation() const
|
|
|
|
|
{
|
|
|
|
|
XNode *pLuaNode = new XNode;
|
|
|
|
|
XNode *pGlobalsNode = new XNode;
|
|
|
|
|
XNode *pClassesNode = new XNode;
|
|
|
|
|
XNode *pSingletonsNode = new XNode;
|
|
|
|
|
XNode *pConstantsNode = new XNode;
|
|
|
|
|
|
|
|
|
|
pLuaNode->m_sName = "Lua";
|
|
|
|
|
pGlobalsNode->m_sName = "GlobalFunctions";
|
|
|
|
|
pClassesNode->m_sName = "Classes";
|
|
|
|
|
pSingletonsNode->m_sName = "Singletons";
|
|
|
|
|
pConstantsNode->m_sName = "Constants";
|
|
|
|
|
|
|
|
|
|
vector<RString> vFunctions;
|
|
|
|
|
for( const LuaFunctionList *p = g_LuaFunctions; p; p = p->next )
|
|
|
|
|
vFunctions.push_back( p->name );
|
|
|
|
|
|
|
|
|
|
sort( vFunctions.begin(), vFunctions.end() );
|
|
|
|
|
|
|
|
|
|
FOREACH_CONST( RString, vFunctions, func )
|
|
|
|
|
{
|
|
|
|
|
XNode *pFunctionNode = new XNode;
|
|
|
|
|
|
|
|
|
|
pFunctionNode->m_sName = "Function";
|
|
|
|
|
pFunctionNode->AppendAttr( "name", *func );
|
|
|
|
|
pGlobalsNode->AppendChild( pFunctionNode );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tricky. We have to get the classes from lua.
|
|
|
|
|
map<RString, LClass> mClasses;
|
2006-08-08 11:47:18 +00:00
|
|
|
map<RString, RString> mSingletons;
|
2006-08-08 07:43:12 +00:00
|
|
|
map<RString, int> mConstants;
|
|
|
|
|
|
|
|
|
|
lua_pushnil( L ); // initial key
|
|
|
|
|
while( lua_next(L, LUA_GLOBALSINDEX) )
|
|
|
|
|
{
|
|
|
|
|
// key is at -2, value is at -1
|
|
|
|
|
switch( lua_type(L, -1) )
|
|
|
|
|
{
|
|
|
|
|
case LUA_TTABLE:
|
|
|
|
|
{
|
|
|
|
|
// Check for the metatable.
|
|
|
|
|
if( !lua_getmetatable(L, -1) )
|
|
|
|
|
break;
|
|
|
|
|
lua_pushliteral( L, "type" );
|
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
|
|
|
|
|
|
const char *name = lua_tostring( L, -1 );
|
|
|
|
|
|
|
|
|
|
if( !name )
|
|
|
|
|
{
|
|
|
|
|
lua_pop( L, 2 ); // pop nil and metatable
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
LClass &c = mClasses[name];
|
|
|
|
|
lua_pop( L, 1 ); // pop name
|
|
|
|
|
|
|
|
|
|
// Get base class.
|
|
|
|
|
lua_pushliteral( L, "__index" );
|
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
|
if( lua_istable(L, -1) && lua_getmetatable(L, -1) )
|
|
|
|
|
{
|
|
|
|
|
lua_pushliteral( L, "type" );
|
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
|
name = lua_tostring( L, -1 );
|
|
|
|
|
|
|
|
|
|
if( name )
|
|
|
|
|
c.m_sBaseName = name;
|
|
|
|
|
lua_pop( L, 2 ); // pop name and metatable
|
|
|
|
|
}
|
|
|
|
|
lua_pop( L, 2 ); // pop (table or other) and metatable
|
|
|
|
|
|
|
|
|
|
// Get methods.
|
|
|
|
|
lua_pushnil( L ); // initial key
|
|
|
|
|
while( lua_next(L, -2) )
|
|
|
|
|
{
|
|
|
|
|
lua_pop( L, 1 ); // pop value
|
2006-09-21 09:42:08 +00:00
|
|
|
RString sKey;
|
2006-09-22 08:38:30 +00:00
|
|
|
if( LuaHelpers::FromStack(L, sKey, -1) )
|
2006-09-21 09:42:08 +00:00
|
|
|
c.m_vMethods.push_back( sKey );
|
2006-08-08 07:43:12 +00:00
|
|
|
}
|
2006-08-08 11:47:18 +00:00
|
|
|
sort( c.m_vMethods.begin(), c.m_vMethods.end() );
|
2006-08-08 07:43:12 +00:00
|
|
|
}
|
|
|
|
|
case LUA_TUSERDATA:
|
|
|
|
|
{
|
2006-08-08 11:47:18 +00:00
|
|
|
/* Tricky. The singletons have a metatable but it doesn't have a type key-value pair.
|
|
|
|
|
* Instead, we need to get the methods using the __index key and then get the
|
|
|
|
|
* metatable of the methods table. */
|
|
|
|
|
if( !lua_getmetatable(L, -1) )
|
|
|
|
|
break;
|
|
|
|
|
lua_pushliteral( L, "__index" );
|
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
|
if( !lua_istable(L, -1) || !lua_getmetatable(L, -1) )
|
|
|
|
|
{
|
|
|
|
|
lua_pop( L, 2 ); // pop method table and metatable
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
lua_pushliteral( L, "type" );
|
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
|
|
|
|
|
|
/* The stack now looks like:
|
|
|
|
|
* -1: type
|
|
|
|
|
* -2: method metatable
|
|
|
|
|
* -3: method table
|
|
|
|
|
* -4: metatable
|
|
|
|
|
* -5: user data
|
|
|
|
|
* -6: key (object's name) */
|
|
|
|
|
const char *type = lua_tostring( L, -1 );
|
|
|
|
|
|
|
|
|
|
if( type )
|
2006-09-21 09:42:08 +00:00
|
|
|
{
|
|
|
|
|
RString sKey;
|
2006-09-22 08:38:30 +00:00
|
|
|
if( LuaHelpers::FromStack(L, sKey, -6) )
|
2006-09-21 09:42:08 +00:00
|
|
|
mSingletons[sKey] = type;
|
|
|
|
|
}
|
2006-08-08 11:47:18 +00:00
|
|
|
lua_pop( L, 4 ); // pop type, method metatable, method table, and metatable
|
2006-08-08 07:43:12 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
|
{
|
2006-08-10 05:07:16 +00:00
|
|
|
// lua_Number is most likely a double unless LUA_NUMBER was defined to be float.
|
|
|
|
|
float fNum = float( lua_tonumber(L, -1) );
|
2006-08-08 07:43:12 +00:00
|
|
|
|
|
|
|
|
if( fNum == truncf(fNum) )
|
2006-09-21 09:42:08 +00:00
|
|
|
{
|
|
|
|
|
RString sKey;
|
2006-09-22 08:38:30 +00:00
|
|
|
if( LuaHelpers::FromStack(L, sKey, -2) )
|
2006-09-21 09:42:08 +00:00
|
|
|
mConstants[sKey] = int( fNum );
|
|
|
|
|
}
|
2006-08-08 07:43:12 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lua_pop( L, 1 ); // pop value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FOREACHM_CONST( RString, LClass, mClasses, c )
|
|
|
|
|
{
|
|
|
|
|
XNode *pClassNode = new XNode;
|
|
|
|
|
|
|
|
|
|
pClassNode->m_sName = "Class";
|
|
|
|
|
pClassNode->AppendAttr( "name", c->first );
|
|
|
|
|
if( !c->second.m_sBaseName.empty() )
|
|
|
|
|
pClassNode->AppendAttr( "base", c->second.m_sBaseName );
|
|
|
|
|
FOREACH_CONST( RString, c->second.m_vMethods, m )
|
|
|
|
|
{
|
|
|
|
|
XNode *pMethodNode = new XNode;
|
|
|
|
|
|
|
|
|
|
pMethodNode->m_sName = "Function";
|
|
|
|
|
pMethodNode->AppendAttr( "name", *m );
|
|
|
|
|
pClassNode->AppendChild( pMethodNode );
|
|
|
|
|
}
|
|
|
|
|
pClassesNode->AppendChild( pClassNode );
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-08 11:47:18 +00:00
|
|
|
FOREACHM_CONST( RString, RString, mSingletons, s )
|
2006-08-08 07:43:12 +00:00
|
|
|
{
|
2006-08-08 11:47:18 +00:00
|
|
|
if( mClasses.find(s->first) != mClasses.end() )
|
2006-08-08 07:43:12 +00:00
|
|
|
continue;
|
|
|
|
|
XNode *pSingletonNode = new XNode;
|
|
|
|
|
|
|
|
|
|
pSingletonNode->m_sName = "Singleton";
|
2006-08-08 11:47:18 +00:00
|
|
|
pSingletonNode->AppendAttr( "name", s->first );
|
|
|
|
|
pSingletonNode->AppendAttr( "class", s->second );
|
2006-08-08 07:43:12 +00:00
|
|
|
pSingletonsNode->AppendChild( pSingletonNode );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FOREACHM_CONST( RString, int, mConstants, c )
|
|
|
|
|
{
|
|
|
|
|
XNode *pConstantNode = new XNode;
|
|
|
|
|
|
|
|
|
|
pConstantNode->m_sName = "Constant";
|
|
|
|
|
pConstantNode->AppendAttr( "name", c->first );
|
|
|
|
|
pConstantNode->AppendAttr( "value", c->second );
|
|
|
|
|
pConstantsNode->AppendChild( pConstantNode );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pLuaNode->AppendChild( pGlobalsNode );
|
|
|
|
|
pLuaNode->AppendChild( pClassesNode );
|
|
|
|
|
pLuaNode->AppendChild( pSingletonsNode );
|
|
|
|
|
pLuaNode->AppendChild( pConstantsNode );
|
|
|
|
|
return pLuaNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void LuaHelpers::PrepareExpression( RString &sInOut )
|
2004-11-06 06:00:58 +00:00
|
|
|
{
|
2005-11-29 17:41:10 +00:00
|
|
|
// HACK: Many metrics have "// foo" and "# foo" comments that Lua fails to parse.
|
2004-11-06 06:00:58 +00:00
|
|
|
// 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.
|
2004-11-06 06:00:58 +00:00
|
|
|
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.
|
2004-11-06 06:00:58 +00:00
|
|
|
if( sInOut.size() >= 1 && sInOut[0] == '+' )
|
|
|
|
|
sInOut.erase( 0, 1 );
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
bool LuaHelpers::RunScriptFile( const RString &sFile )
|
2005-01-19 23:33:19 +00:00
|
|
|
{
|
|
|
|
|
RageFile f;
|
|
|
|
|
if( !f.Open( sFile ) )
|
|
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString 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" );
|
2005-01-19 23:33:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sScript;
|
2005-01-19 23:33:19 +00:00
|
|
|
if( f.Read( sScript ) == -1 )
|
|
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString 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" );
|
2005-01-19 23:33:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-16 22:10:24 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sError;
|
2005-06-16 22:10:24 +00:00
|
|
|
if( !LuaHelpers::RunScript( L, sScript, sFile, sError, 0 ) )
|
2005-02-15 07:17:49 +00:00
|
|
|
{
|
2006-09-21 07:25:38 +00:00
|
|
|
LUA->Release( L );
|
2005-02-15 07:17:49 +00:00
|
|
|
sError = ssprintf( "Lua runtime error: %s", sError.c_str() );
|
2005-12-29 05:52:50 +00:00
|
|
|
Dialog::OK( sError, "LUA_ERROR" );
|
2005-02-15 07:17:49 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2006-09-21 07:25:38 +00:00
|
|
|
LUA->Release( L );
|
2005-06-16 22:10:24 +00:00
|
|
|
|
2005-02-15 07:17:49 +00:00
|
|
|
return true;
|
2006-08-18 00:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
2005-01-19 23:33:19 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
bool LuaHelpers::RunScript( Lua *L, const RString &sScript, const RString &sName, RString &sError, int iReturnValues )
|
2004-09-22 04:55:31 +00:00
|
|
|
{
|
2005-01-19 23:33:19 +00:00
|
|
|
// load string
|
|
|
|
|
{
|
2006-09-23 02:24:23 +00:00
|
|
|
ChunkReaderString data( sScript );
|
|
|
|
|
int ret = lua_load( L, ChunkReaderString::Reader, &data, sName );
|
2005-01-19 23:33:19 +00:00
|
|
|
|
|
|
|
|
if( ret )
|
|
|
|
|
{
|
2006-09-22 08:48:49 +00:00
|
|
|
LuaHelpers::Pop( L, sError );
|
2006-09-23 02:44:35 +00:00
|
|
|
for( int i = 0; i < iReturnValues; ++i )
|
|
|
|
|
lua_pushnil( L );
|
2005-01-19 23:33:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// evaluate
|
|
|
|
|
{
|
2005-02-05 23:03:20 +00:00
|
|
|
int ret = lua_pcall( L, 0, iReturnValues, 0 );
|
2005-01-19 23:33:19 +00:00
|
|
|
if( ret )
|
|
|
|
|
{
|
2006-09-22 08:48:49 +00:00
|
|
|
LuaHelpers::Pop( L, sError );
|
2006-09-23 02:44:35 +00:00
|
|
|
for( int i = 0; i < iReturnValues; ++i )
|
|
|
|
|
lua_pushnil( L );
|
2005-01-19 23:33:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2005-01-19 23:01:53 +00:00
|
|
|
|
2005-02-12 22:53:51 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
bool LuaHelpers::RunScript( Lua *L, const RString &sExpression, const RString &sName, int iReturnValues )
|
2005-01-19 23:33:19 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sError;
|
|
|
|
|
if( !LuaHelpers::RunScript( L, sExpression, sName.size()? sName:RString("in"), sError, iReturnValues ) )
|
2005-02-15 07:17:49 +00:00
|
|
|
{
|
2005-02-17 05:46:31 +00:00
|
|
|
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;
|
2005-02-15 07:17:49 +00:00
|
|
|
}
|
2004-11-07 18:07:54 +00:00
|
|
|
|
|
|
|
|
return true;
|
2004-09-22 04:55:31 +00:00
|
|
|
}
|
2004-05-03 04:38:08 +00:00
|
|
|
|
2006-09-23 02:09:58 +00:00
|
|
|
bool LuaHelpers::RunExpression( Lua *L, const RString &sExpression, const RString &sName )
|
2006-09-22 18:53:39 +00:00
|
|
|
{
|
|
|
|
|
RString sFullExpression = "return " + sExpression;
|
|
|
|
|
|
2006-09-23 02:44:35 +00:00
|
|
|
return LuaHelpers::RunScript( L, sFullExpression, sName, 1 );
|
2006-09-22 18:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
bool LuaHelpers::RunExpressionB( const RString &str )
|
2004-09-22 04:55:31 +00:00
|
|
|
{
|
2005-06-16 06:23:59 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
|
2006-09-23 02:11:54 +00:00
|
|
|
RunExpression( L, str );
|
2004-09-22 04:55:31 +00:00
|
|
|
|
2006-09-23 02:11:54 +00:00
|
|
|
bool result;
|
|
|
|
|
LuaHelpers::Pop( L, result );
|
2005-02-15 02:15:26 +00:00
|
|
|
|
2006-09-21 07:25:38 +00:00
|
|
|
LUA->Release( L );
|
2004-12-01 02:37:18 +00:00
|
|
|
return result;
|
2004-02-14 21:08:12 +00:00
|
|
|
}
|
2004-09-22 04:55:31 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
float LuaHelpers::RunExpressionF( const RString &str )
|
2004-09-22 04:55:31 +00:00
|
|
|
{
|
2005-06-16 06:30:33 +00:00
|
|
|
Lua *L = LUA->Get();
|
2006-09-22 02:08:00 +00:00
|
|
|
|
2006-09-23 02:11:54 +00:00
|
|
|
RunExpression( L, str );
|
2005-02-15 02:15:26 +00:00
|
|
|
|
2006-09-23 02:11:54 +00:00
|
|
|
float result;
|
|
|
|
|
LuaHelpers::Pop( L, result );
|
2004-09-22 04:55:31 +00:00
|
|
|
|
2006-09-21 07:25:38 +00:00
|
|
|
LUA->Release( L );
|
2004-12-01 02:37:18 +00:00
|
|
|
return result;
|
2004-02-14 21:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
int LuaHelpers::RunExpressionI( const RString &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
|
|
|
}
|
|
|
|
|
|
2006-09-23 02:11:54 +00:00
|
|
|
void LuaHelpers::RunExpressionS( const RString &str, RString &sOut )
|
2005-01-16 20:36:27 +00:00
|
|
|
{
|
2005-06-16 06:37:45 +00:00
|
|
|
Lua *L = LUA->Get();
|
2006-09-22 02:08:00 +00:00
|
|
|
|
2006-09-23 02:11:54 +00:00
|
|
|
RunExpression( L, str );
|
2005-01-16 20:36:27 +00:00
|
|
|
|
2006-09-22 08:48:49 +00:00
|
|
|
LuaHelpers::Pop( L, sOut );
|
2005-01-16 20:36:27 +00:00
|
|
|
|
2006-09-21 07:25:38 +00:00
|
|
|
LUA->Release( L );
|
2005-01-16 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
bool LuaHelpers::RunAtExpressionS( RString &sStr )
|
2005-01-20 01:08:26 +00:00
|
|
|
{
|
|
|
|
|
if( sStr.size() == 0 || sStr[0] != '@' )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
/* Erase "@". */
|
|
|
|
|
sStr.erase( 0, 1 );
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sOut;
|
2005-06-16 06:37:45 +00:00
|
|
|
LuaHelpers::RunExpressionS( sStr, sOut );
|
2005-01-20 01:08:26 +00:00
|
|
|
sStr = sOut;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-07 06:03:25 +00:00
|
|
|
/* 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. */
|
2006-01-22 01:00:06 +00:00
|
|
|
RString GetLuaBindingType( Lua *L, int iArgNo )
|
2005-07-07 06:03:25 +00:00
|
|
|
{
|
|
|
|
|
if( lua_isnil(L, iArgNo) )
|
|
|
|
|
return "nil";
|
|
|
|
|
|
2006-09-21 07:25:38 +00:00
|
|
|
int iTop = lua_gettop( L );
|
2005-07-07 06:03:25 +00:00
|
|
|
if( !lua_getmetatable(L, iArgNo) )
|
|
|
|
|
{
|
|
|
|
|
lua_settop( L, iTop );
|
|
|
|
|
return ssprintf( "non-bound %s", lua_typename(L, lua_type(L, iArgNo)) );
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-21 07:25:38 +00:00
|
|
|
int iMetatable = lua_gettop( L );
|
2005-07-07 06:03:25 +00:00
|
|
|
lua_pushstring( L, "type" );
|
|
|
|
|
lua_rawget( L, iMetatable );
|
2006-09-21 09:42:08 +00:00
|
|
|
RString sActualType;
|
2006-09-22 08:38:30 +00:00
|
|
|
LuaHelpers::FromStack( L, sActualType, -1 );
|
2005-07-07 06:03:25 +00:00
|
|
|
|
|
|
|
|
lua_settop( L, iTop );
|
|
|
|
|
return sActualType;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-28 07:27:49 +00:00
|
|
|
/* 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 )
|
2005-05-28 07:27:49 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sType = GetLuaBindingType( L, iArgNo );
|
2005-07-07 06:03:25 +00:00
|
|
|
|
2005-05-28 07:27:49 +00:00
|
|
|
lua_Debug debug;
|
2005-07-07 06:03:25 +00:00
|
|
|
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-05-28 07:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
2005-01-24 02:04:03 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
LuaFunctionList::LuaFunctionList( RString name_, lua_CFunction func_ )
|
2004-02-14 21:08:12 +00:00
|
|
|
{
|
|
|
|
|
name = name_;
|
|
|
|
|
func = func_;
|
2005-01-19 23:33:19 +00:00
|
|
|
next = g_LuaFunctions;
|
|
|
|
|
g_LuaFunctions = this;
|
2004-02-14 21:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
static bool Trace( const RString &sString )
|
2005-02-25 04:37:27 +00:00
|
|
|
{
|
|
|
|
|
LOG->Trace( "%s", sString.c_str() );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-29 02:21:24 +00:00
|
|
|
LuaFunction( Trace, Trace(SArg(1)) );
|
2005-01-19 23:43:28 +00:00
|
|
|
|
2005-06-25 02:55:58 +00:00
|
|
|
#include "ProductInfo.h"
|
2006-01-22 01:00:06 +00:00
|
|
|
LuaFunction( ProductVersion, (RString) PRODUCT_VER );
|
2006-05-02 03:31:56 +00:00
|
|
|
LuaFunction( ProductID, (RString) PRODUCT_ID );
|
2005-06-25 02:55:58 +00:00
|
|
|
|
2005-08-23 06:14:11 +00:00
|
|
|
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)) );
|
|
|
|
|
|
2005-08-31 01:25:36 +00:00
|
|
|
#include "RageTypes.h"
|
|
|
|
|
int LuaFunc_color( lua_State *L )
|
|
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sColor = SArg(1);
|
2005-08-31 01:25:36 +00:00
|
|
|
RageColor c;
|
|
|
|
|
c.FromString( sColor );
|
2006-09-22 02:14:34 +00:00
|
|
|
c.PushTable( L );
|
|
|
|
|
return 1;
|
2005-08-31 01:25:36 +00:00
|
|
|
}
|
|
|
|
|
static LuaFunctionList g_color( "color", LuaFunc_color ); /* register it */
|
|
|
|
|
|
2004-02-14 21:08:12 +00:00
|
|
|
/*
|
2006-08-08 07:43:12 +00:00
|
|
|
* (c) 2004-2006 Glenn Maynard, Steve Checkoway
|
2004-05-31 22:42:12 +00:00
|
|
|
* 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
|
|
|
*/
|