2004-02-14 21:08:12 +00:00
|
|
|
#include "global.h"
|
2005-01-24 02:26:55 +00:00
|
|
|
#include "LuaManager.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"
|
2006-10-08 22:24:15 +00:00
|
|
|
#include "Command.h"
|
2006-10-23 20:18:28 +00:00
|
|
|
#include "RageTypes.h"
|
2005-12-28 04:22:24 +00:00
|
|
|
|
2006-10-08 22:24:15 +00:00
|
|
|
#include <sstream>
|
2004-04-05 05:22:32 +00:00
|
|
|
#include <csetjmp>
|
|
|
|
|
#include <cassert>
|
2006-10-15 01:58:36 +00:00
|
|
|
#include <map>
|
2005-05-29 21:12:30 +00:00
|
|
|
|
2005-01-19 23:33:19 +00:00
|
|
|
LuaManager *LUA = NULL;
|
2006-10-15 20:50:27 +00:00
|
|
|
struct Impl
|
|
|
|
|
{
|
2006-10-31 01:23:25 +00:00
|
|
|
Impl(): g_pLock("Lua") {}
|
2006-10-15 20:50:27 +00:00
|
|
|
vector<lua_State *> g_FreeStateList;
|
|
|
|
|
map<lua_State *, bool> g_ActiveStates;
|
2006-10-31 01:23:25 +00:00
|
|
|
|
|
|
|
|
RageMutex g_pLock;
|
2006-10-15 20:50:27 +00:00
|
|
|
};
|
|
|
|
|
static Impl *pImpl = NULL;
|
2005-01-19 23:33:19 +00:00
|
|
|
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-10-07 01:22:28 +00:00
|
|
|
namespace LuaHelpers
|
|
|
|
|
{
|
|
|
|
|
template<> void Push<bool>( lua_State *L, const bool &Object );
|
|
|
|
|
template<> void Push<float>( lua_State *L, const float &Object );
|
|
|
|
|
template<> void Push<int>( lua_State *L, const int &Object );
|
|
|
|
|
template<> void Push<RString>( lua_State *L, const RString &Object );
|
|
|
|
|
|
|
|
|
|
template<> bool FromStack<bool>( Lua *L, bool &Object, int iOffset );
|
|
|
|
|
template<> bool FromStack<float>( Lua *L, float &Object, int iOffset );
|
|
|
|
|
template<> bool FromStack<int>( Lua *L, int &Object, int iOffset );
|
|
|
|
|
template<> bool FromStack<RString>( Lua *L, RString &Object, int iOffset );
|
|
|
|
|
}
|
|
|
|
|
|
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-26 08:54:54 +00:00
|
|
|
LuaHelpers::Push( L, val );
|
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-26 08:54:54 +00:00
|
|
|
LuaHelpers::Push( L, val );
|
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
|
|
|
}
|
|
|
|
|
|
2006-10-07 01:22:28 +00:00
|
|
|
namespace LuaHelpers
|
2005-02-22 04:20:58 +00:00
|
|
|
{
|
2006-10-07 01:22:28 +00:00
|
|
|
template<> void Push<bool>( lua_State *L, const bool &Object ) { lua_pushboolean( L, Object ); }
|
|
|
|
|
template<> void Push<float>( lua_State *L, const float &Object ) { lua_pushnumber( L, Object ); }
|
|
|
|
|
template<> void Push<int>( lua_State *L, const int &Object ) { lua_pushinteger( L, Object ); }
|
|
|
|
|
template<> void Push<RString>( lua_State *L, const RString &Object ) { lua_pushlstring( L, Object.data(), Object.size() ); }
|
|
|
|
|
|
|
|
|
|
template<> bool FromStack<bool>( Lua *L, bool &Object, int iOffset ) { Object = !!lua_toboolean( L, iOffset ); return true; }
|
|
|
|
|
template<> bool FromStack<float>( Lua *L, float &Object, int iOffset ) { Object = (float)lua_tonumber( L, iOffset ); return true; }
|
|
|
|
|
template<> bool FromStack<int>( Lua *L, int &Object, int iOffset ) { Object = lua_tointeger( L, iOffset ); return true; }
|
|
|
|
|
template<> bool FromStack<RString>( Lua *L, RString &Object, int iOffset )
|
|
|
|
|
{
|
|
|
|
|
size_t iLen;
|
|
|
|
|
const char *pStr = lua_tolstring( L, iOffset, &iLen );
|
|
|
|
|
if( pStr != NULL )
|
|
|
|
|
Object.assign( pStr, iLen );
|
|
|
|
|
else
|
|
|
|
|
Object.clear();
|
|
|
|
|
|
|
|
|
|
return pStr != NULL;
|
|
|
|
|
}
|
2005-02-22 04:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2006-10-15 20:50:27 +00:00
|
|
|
pImpl = new Impl;
|
2005-01-24 02:04:03 +00:00
|
|
|
LUA = this; // so that LUA is available when we call the Register functions
|
|
|
|
|
|
2006-10-14 20:49:35 +00:00
|
|
|
lua_State *L = lua_open();
|
2006-09-20 23:47:08 +00:00
|
|
|
ASSERT( L );
|
|
|
|
|
|
|
|
|
|
lua_atpanic( L, LuaPanic );
|
2006-10-14 20:49:35 +00:00
|
|
|
m_pLuaMain = L;
|
2006-09-20 23:47:08 +00:00
|
|
|
|
|
|
|
|
luaopen_base( L );
|
|
|
|
|
luaopen_math( L );
|
|
|
|
|
luaopen_string( L );
|
|
|
|
|
luaopen_table( L );
|
2006-10-09 07:28:01 +00:00
|
|
|
luaopen_debug( L );
|
2006-09-20 23:47:08 +00:00
|
|
|
lua_settop(L, 0); // luaopen_* pushes stuff onto the stack that we don't need
|
|
|
|
|
|
2006-10-14 22:34:15 +00:00
|
|
|
/* Store the thread pool in a table on the stack, in the main thread. */
|
|
|
|
|
#define THREAD_POOL 1
|
|
|
|
|
lua_newtable( L );
|
|
|
|
|
|
2006-09-20 23:47:08 +00:00
|
|
|
RegisterTypes();
|
2005-01-19 23:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaManager::~LuaManager()
|
|
|
|
|
{
|
2006-10-14 20:49:35 +00:00
|
|
|
lua_close( m_pLuaMain );
|
2006-10-15 20:50:27 +00:00
|
|
|
SAFE_DELETE( pImpl );
|
2005-01-19 23:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
2005-06-15 08:16:28 +00:00
|
|
|
Lua *LuaManager::Get()
|
|
|
|
|
{
|
2006-10-15 01:58:36 +00:00
|
|
|
bool bLocked = false;
|
2006-10-31 01:23:25 +00:00
|
|
|
if( !pImpl->g_pLock.IsLockedByThisThread() )
|
2006-10-15 01:58:36 +00:00
|
|
|
{
|
2006-10-31 01:23:25 +00:00
|
|
|
pImpl->g_pLock.Lock();
|
2006-10-15 01:58:36 +00:00
|
|
|
bLocked = true;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-14 22:34:15 +00:00
|
|
|
ASSERT( lua_gettop(m_pLuaMain) == 1 );
|
|
|
|
|
|
|
|
|
|
lua_State *pRet;
|
2006-10-15 20:50:27 +00:00
|
|
|
if( pImpl->g_FreeStateList.empty() )
|
2006-10-14 22:34:15 +00:00
|
|
|
{
|
|
|
|
|
pRet = lua_newthread( m_pLuaMain );
|
|
|
|
|
|
|
|
|
|
/* Store the new thread in THREAD_POOL, so it isn't collected. */
|
|
|
|
|
int iLast = lua_objlen( m_pLuaMain, THREAD_POOL );
|
|
|
|
|
lua_rawseti( m_pLuaMain, THREAD_POOL, iLast+1 );
|
|
|
|
|
}
|
|
|
|
|
else
|
2006-01-14 06:53:09 +00:00
|
|
|
{
|
2006-10-15 20:50:27 +00:00
|
|
|
pRet = pImpl->g_FreeStateList.back();
|
|
|
|
|
pImpl->g_FreeStateList.pop_back();
|
2006-01-14 06:53:09 +00:00
|
|
|
}
|
2006-10-14 22:34:15 +00:00
|
|
|
|
2006-10-15 20:50:27 +00:00
|
|
|
pImpl->g_ActiveStates[pRet] = bLocked;
|
2006-10-14 22:34:15 +00:00
|
|
|
return pRet;
|
2005-06-15 08:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaManager::Release( Lua *&p )
|
|
|
|
|
{
|
2006-10-15 20:50:27 +00:00
|
|
|
pImpl->g_FreeStateList.push_back( p );
|
2006-10-14 22:34:15 +00:00
|
|
|
|
|
|
|
|
ASSERT( lua_gettop(p) == 0 );
|
2006-10-15 20:50:27 +00:00
|
|
|
ASSERT( pImpl->g_ActiveStates.find(p) != pImpl->g_ActiveStates.end() );
|
|
|
|
|
bool bDoUnlock = pImpl->g_ActiveStates[p];
|
|
|
|
|
pImpl->g_ActiveStates.erase( p );
|
2006-10-15 01:58:36 +00:00
|
|
|
|
|
|
|
|
if( bDoUnlock )
|
2006-10-31 01:23:25 +00:00
|
|
|
pImpl->g_pLock.Unlock();
|
2005-06-15 08:16:28 +00:00
|
|
|
p = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-15 01:58:36 +00:00
|
|
|
/*
|
2006-10-31 01:23:25 +00:00
|
|
|
* Low-level access to Lua is always serialized through pImpl->g_pLock; we never run the Lua
|
2006-10-15 01:58:36 +00:00
|
|
|
* core simultaneously from multiple threads. However, when a thread has an acquired
|
|
|
|
|
* lua_State, it can release Lua for use by other threads. This allows Lua bindings
|
|
|
|
|
* to process long-running actions, without blocking all other threads from using Lua
|
|
|
|
|
* until it finishes.
|
|
|
|
|
*
|
|
|
|
|
* Lua *L = LUA->Get(); // acquires L and locks Lua
|
|
|
|
|
* lua_newtable(L); // does something with Lua
|
|
|
|
|
* LUA->YieldLua(); // unlocks Lua for lengthy operation; L is still owned, but can't be used
|
|
|
|
|
* RString s = ReadFile("/filename.txt"); // time-consuming operation; other threads may use Lua in the meantime
|
|
|
|
|
* LUA->UnyieldLua(); // relock Lua
|
|
|
|
|
* lua_pushstring( L, s ); // finish working with it
|
|
|
|
|
* LUA->Release( L ); // release L and unlock Lua
|
|
|
|
|
*
|
|
|
|
|
* YieldLua() must not be called when already yielded, or when a lua_State has not been
|
|
|
|
|
* acquired (you have nothing to yield), and always unyield before releasing the
|
|
|
|
|
* state. Recursive handling is OK:
|
|
|
|
|
*
|
|
|
|
|
* L1 = LUA->Get();
|
|
|
|
|
* LUA->YieldLua(); // yields
|
|
|
|
|
* L2 = LUA->Get(); // unyields
|
|
|
|
|
* LUA->Release(L2); // re-yields
|
|
|
|
|
* LUA->UnyieldLua();
|
|
|
|
|
* LUA->Release(L1);
|
|
|
|
|
*/
|
|
|
|
|
void LuaManager::YieldLua()
|
|
|
|
|
{
|
2006-10-31 01:23:25 +00:00
|
|
|
ASSERT( pImpl->g_pLock.IsLockedByThisThread() );
|
2006-10-15 01:58:36 +00:00
|
|
|
|
2006-10-31 01:23:25 +00:00
|
|
|
pImpl->g_pLock.Unlock();
|
2006-10-15 01:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaManager::UnyieldLua()
|
|
|
|
|
{
|
2006-10-31 01:23:25 +00:00
|
|
|
pImpl->g_pLock.Lock();
|
2006-10-15 01:58:36 +00:00
|
|
|
}
|
2005-06-15 08:16:28 +00:00
|
|
|
|
2005-02-16 17:33:28 +00:00
|
|
|
void LuaManager::RegisterTypes()
|
|
|
|
|
{
|
2006-10-14 20:37:30 +00:00
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-10-14 20:37:30 +00:00
|
|
|
|
|
|
|
|
LUA->Release( L );
|
2005-02-16 17:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-02 04:23:19 +00:00
|
|
|
LuaThreadVariable::LuaThreadVariable( const RString &sName, const RString &sValue )
|
|
|
|
|
{
|
|
|
|
|
m_sName = sName;
|
|
|
|
|
m_pOldValue = new LuaReference;
|
|
|
|
|
|
|
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
LuaHelpers::Push( L, sValue );
|
|
|
|
|
SetFromStack( L );
|
|
|
|
|
LUA->Release( L );
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-02 05:47:32 +00:00
|
|
|
LuaThreadVariable::LuaThreadVariable( const RString &sName, const LuaReference &Value )
|
2006-11-02 04:23:19 +00:00
|
|
|
{
|
|
|
|
|
m_sName = sName;
|
|
|
|
|
m_pOldValue = new LuaReference;
|
|
|
|
|
|
|
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
Value.PushSelf( L );
|
|
|
|
|
SetFromStack( L );
|
|
|
|
|
LUA->Release( L );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RString LuaThreadVariable::GetCurrentThreadIDString()
|
|
|
|
|
{
|
|
|
|
|
uint64_t iID = RageThread::GetCurrentThreadID();
|
|
|
|
|
return ssprintf( "%08x%08x", uint32_t(iID >> 32), uint32_t(iID) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaThreadVariable::PushThreadTable( lua_State *L, bool bCreate )
|
|
|
|
|
{
|
|
|
|
|
lua_getfield( L, LUA_REGISTRYINDEX, "LuaThreadVariableTable" );
|
|
|
|
|
if( lua_isnil(L, -1) )
|
|
|
|
|
{
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
if( !bCreate )
|
|
|
|
|
return false;
|
|
|
|
|
lua_newtable( L );
|
|
|
|
|
|
|
|
|
|
lua_pushvalue( L, -1 );
|
|
|
|
|
lua_setfield( L, LUA_REGISTRYINDEX, "LuaThreadVariableTable" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RString sThreadIDString = GetCurrentThreadIDString();
|
|
|
|
|
LuaHelpers::Push( L, sThreadIDString );
|
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
|
if( lua_isnil(L, -1) )
|
|
|
|
|
{
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
if( !bCreate )
|
|
|
|
|
{
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
lua_newtable( L );
|
|
|
|
|
|
|
|
|
|
lua_pushinteger( L, 0 );
|
|
|
|
|
lua_rawseti( L, -2, 0 );
|
|
|
|
|
|
|
|
|
|
LuaHelpers::Push( L, sThreadIDString );
|
|
|
|
|
lua_pushvalue( L, -2 );
|
|
|
|
|
lua_settable( L, -4 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_remove( L, -2 );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaThreadVariable::GetThreadVariable( lua_State *L )
|
|
|
|
|
{
|
|
|
|
|
if( !PushThreadTable(L, false) )
|
|
|
|
|
{
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
lua_pushnil( L );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_pushvalue( L, -2 );
|
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
|
lua_remove( L, -2 );
|
|
|
|
|
lua_remove( L, -2 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LuaThreadVariable::AdjustCount( lua_State *L, int iAdd )
|
|
|
|
|
{
|
|
|
|
|
ASSERT( lua_istable(L, -1) );
|
|
|
|
|
|
|
|
|
|
lua_rawgeti( L, -1, 0 );
|
|
|
|
|
ASSERT( lua_isnumber(L, -1) );
|
|
|
|
|
|
|
|
|
|
int iCount = lua_tointeger( L, -1 );
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
|
|
|
|
|
iCount += iAdd;
|
|
|
|
|
lua_pushinteger( L, iCount );
|
|
|
|
|
lua_rawseti( L, -2, 0 );
|
|
|
|
|
|
|
|
|
|
return iCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaThreadVariable::SetFromStack( lua_State *L )
|
|
|
|
|
{
|
|
|
|
|
ASSERT( !m_pOldValue->IsSet() ); // don't call twice
|
|
|
|
|
|
|
|
|
|
PushThreadTable( L, true );
|
|
|
|
|
|
|
|
|
|
lua_getfield( L, -1, m_sName );
|
|
|
|
|
m_pOldValue->SetFromStack( L );
|
|
|
|
|
|
|
|
|
|
lua_pushvalue( L, -2 );
|
|
|
|
|
lua_setfield( L, -2, m_sName );
|
|
|
|
|
|
|
|
|
|
AdjustCount( L, +1 );
|
|
|
|
|
|
|
|
|
|
lua_pop( L, 2 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaThreadVariable::~LuaThreadVariable()
|
|
|
|
|
{
|
|
|
|
|
Lua *L = LUA->Get();
|
|
|
|
|
|
|
|
|
|
PushThreadTable( L, true );
|
|
|
|
|
m_pOldValue->PushSelf( L );
|
|
|
|
|
lua_setfield( L, -2, m_sName );
|
|
|
|
|
|
|
|
|
|
if( AdjustCount( L, -1 ) == 0 )
|
|
|
|
|
{
|
|
|
|
|
// if empty, delete the table
|
|
|
|
|
lua_getfield( L, LUA_REGISTRYINDEX, "LuaThreadVariableTable" );
|
|
|
|
|
ASSERT( lua_istable(L, -1) );
|
|
|
|
|
|
|
|
|
|
LuaHelpers::Push( L, GetCurrentThreadIDString() );
|
|
|
|
|
lua_pushnil( L );
|
|
|
|
|
lua_settable( L, -3 );
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
}
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
|
|
|
|
|
LUA->Release( L );
|
|
|
|
|
|
|
|
|
|
delete m_pOldValue;
|
|
|
|
|
}
|
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;
|
|
|
|
|
};
|
2006-10-15 03:16:06 +00:00
|
|
|
}
|
2006-08-08 07:43:12 +00:00
|
|
|
|
2006-10-14 20:35:32 +00:00
|
|
|
XNode *LuaHelpers::GetLuaInformation()
|
2006-08-08 07:43:12 +00:00
|
|
|
{
|
2006-10-01 14:51:50 +00:00
|
|
|
XNode *pLuaNode = new XNode( "Lua" );
|
2006-09-29 20:34:18 +00:00
|
|
|
|
|
|
|
|
XNode *pGlobalsNode = pLuaNode->AppendChild( "GlobalFunctions" );
|
|
|
|
|
XNode *pClassesNode = pLuaNode->AppendChild( "Classes" );
|
|
|
|
|
XNode *pSingletonsNode = pLuaNode->AppendChild( "Singletons" );
|
|
|
|
|
XNode *pEnumsNode = pLuaNode->AppendChild( "Enums" );
|
|
|
|
|
XNode *pConstantsNode = pLuaNode->AppendChild( "Constants" );
|
2006-08-08 07:43:12 +00:00
|
|
|
|
|
|
|
|
// Tricky. We have to get the classes from lua.
|
2006-09-29 21:18:50 +00:00
|
|
|
vector<RString> vFunctions;
|
2006-08-08 07:43:12 +00:00
|
|
|
map<RString, LClass> mClasses;
|
2006-08-08 11:47:18 +00:00
|
|
|
map<RString, RString> mSingletons;
|
2006-09-26 10:33:32 +00:00
|
|
|
map<RString, float> mConstants;
|
|
|
|
|
map<RString, RString> mStringConstants;
|
|
|
|
|
map<RString, vector<RString> > mEnums;
|
2006-10-15 07:51:38 +00:00
|
|
|
|
|
|
|
|
Lua *L = LUA->Get();
|
2006-10-15 03:16:06 +00:00
|
|
|
FOREACH_LUATABLE( L, LUA_GLOBALSINDEX )
|
2006-08-08 07:43:12 +00:00
|
|
|
{
|
2006-09-26 10:33:32 +00:00
|
|
|
RString sKey;
|
|
|
|
|
LuaHelpers::Pop( L, sKey );
|
|
|
|
|
|
2006-08-08 07:43:12 +00:00
|
|
|
switch( lua_type(L, -1) )
|
|
|
|
|
{
|
|
|
|
|
case LUA_TTABLE:
|
|
|
|
|
{
|
2006-09-29 21:18:50 +00:00
|
|
|
if( luaL_getmetafield(L, -1, "class") )
|
2006-08-08 07:43:12 +00:00
|
|
|
{
|
2006-09-29 21:18:50 +00:00
|
|
|
const char *name = lua_tostring( L, -1 );
|
2006-08-08 07:43:12 +00:00
|
|
|
|
2006-09-29 21:18:50 +00:00
|
|
|
if( !name )
|
|
|
|
|
break;
|
|
|
|
|
LClass &c = mClasses[name];
|
|
|
|
|
lua_pop( L, 1 ); // pop name
|
|
|
|
|
|
|
|
|
|
// Get base class.
|
2006-10-05 20:04:14 +00:00
|
|
|
if( luaL_getmetafield( L, -1, "base" ) )
|
2006-09-29 21:18:50 +00:00
|
|
|
{
|
2006-10-05 20:04:14 +00:00
|
|
|
name = lua_tostring( L, -1 );
|
|
|
|
|
|
|
|
|
|
if( name )
|
|
|
|
|
c.m_sBaseName = name;
|
|
|
|
|
lua_pop( L, 1 ); // pop name
|
2006-09-29 21:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get methods.
|
2006-10-15 03:16:06 +00:00
|
|
|
FOREACH_LUATABLE( L, -1 )
|
2006-09-29 21:18:50 +00:00
|
|
|
{
|
|
|
|
|
RString sMethod;
|
2006-10-15 03:16:06 +00:00
|
|
|
if( LuaHelpers::FromStack(L, sMethod, -1) )
|
2006-09-29 21:18:50 +00:00
|
|
|
c.m_vMethods.push_back( sMethod );
|
|
|
|
|
}
|
|
|
|
|
sort( c.m_vMethods.begin(), c.m_vMethods.end() );
|
|
|
|
|
break;
|
2006-08-08 07:43:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-09-29 21:18:50 +00:00
|
|
|
// fall through
|
|
|
|
|
case LUA_TUSERDATA: // table or userdata: class instance
|
2006-08-08 07:43:12 +00:00
|
|
|
{
|
2006-10-06 01:07:53 +00:00
|
|
|
if( !luaL_callmeta(L, -1, "__type") )
|
2006-10-06 00:46:46 +00:00
|
|
|
break;
|
|
|
|
|
RString sType;
|
|
|
|
|
if( !LuaHelpers::Pop(L, sType) )
|
|
|
|
|
break;
|
|
|
|
|
if( sType == "Enum" )
|
2006-10-15 07:51:38 +00:00
|
|
|
LuaHelpers::ReadArrayFromTable( mEnums[sKey], L );
|
2006-10-06 00:46:46 +00:00
|
|
|
else
|
|
|
|
|
mSingletons[sKey] = sType;
|
2006-08-08 07:43:12 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case LUA_TNUMBER:
|
2006-09-29 20:35:14 +00:00
|
|
|
LuaHelpers::FromStack( L, mConstants[sKey], -1 );
|
2006-08-08 07:43:12 +00:00
|
|
|
break;
|
2006-09-26 10:33:32 +00:00
|
|
|
case LUA_TSTRING:
|
2006-09-29 20:35:14 +00:00
|
|
|
LuaHelpers::FromStack( L, mStringConstants[sKey], -1 );
|
2006-09-26 10:33:32 +00:00
|
|
|
break;
|
2006-09-29 21:18:50 +00:00
|
|
|
case LUA_TFUNCTION:
|
|
|
|
|
vFunctions.push_back( sKey );
|
|
|
|
|
break;
|
2006-08-08 07:43:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-10-15 07:51:38 +00:00
|
|
|
LUA->Release( L );
|
|
|
|
|
|
2006-09-29 21:18:50 +00:00
|
|
|
sort( vFunctions.begin(), vFunctions.end() );
|
|
|
|
|
FOREACH_CONST( RString, vFunctions, func )
|
|
|
|
|
{
|
|
|
|
|
XNode *pFunctionNode = pGlobalsNode->AppendChild( "Function" );
|
|
|
|
|
pFunctionNode->AppendAttr( "name", *func );
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-08 07:43:12 +00:00
|
|
|
FOREACHM_CONST( RString, LClass, mClasses, c )
|
|
|
|
|
{
|
2006-09-29 20:34:18 +00:00
|
|
|
XNode *pClassNode = pClassesNode->AppendChild( "Class" );
|
2006-08-08 07:43:12 +00:00
|
|
|
|
|
|
|
|
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 )
|
|
|
|
|
{
|
2006-09-29 21:18:50 +00:00
|
|
|
XNode *pMethodNode = pClassNode->AppendChild( "Function" );
|
2006-08-08 07:43:12 +00:00
|
|
|
pMethodNode->AppendAttr( "name", *m );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2006-09-29 20:34:18 +00:00
|
|
|
XNode *pSingletonNode = pSingletonsNode->AppendChild( "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
|
|
|
}
|
2006-09-26 10:33:32 +00:00
|
|
|
|
|
|
|
|
for( map<RString, vector<RString> >::const_iterator iter = mEnums.begin(); iter != mEnums.end(); ++iter )
|
|
|
|
|
{
|
2006-09-29 20:34:18 +00:00
|
|
|
XNode *pEnumNode = pEnumsNode->AppendChild( "Enum" );
|
|
|
|
|
|
2006-09-26 10:33:32 +00:00
|
|
|
const vector<RString> &vEnum = iter->second;
|
|
|
|
|
pEnumNode->AppendAttr( "name", iter->first );
|
|
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < vEnum.size(); ++i )
|
|
|
|
|
{
|
2006-09-29 20:34:18 +00:00
|
|
|
XNode *pEnumValueNode = pEnumNode->AppendChild( "EnumValue" );
|
2006-10-01 07:35:59 +00:00
|
|
|
pEnumValueNode->AppendAttr( "name", ssprintf("'%s'", vEnum[i].c_str()) );
|
2006-09-26 10:33:32 +00:00
|
|
|
pEnumValueNode->AppendAttr( "value", i );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FOREACHM_CONST( RString, float, mConstants, c )
|
2006-08-08 07:43:12 +00:00
|
|
|
{
|
2006-09-29 20:34:18 +00:00
|
|
|
XNode *pConstantNode = pConstantsNode->AppendChild( "Constant" );
|
2006-08-08 07:43:12 +00:00
|
|
|
|
|
|
|
|
pConstantNode->AppendAttr( "name", c->first );
|
2006-09-26 10:33:32 +00:00
|
|
|
if( c->second == truncf(c->second) )
|
|
|
|
|
pConstantNode->AppendAttr( "value", int(c->second) );
|
|
|
|
|
else
|
|
|
|
|
pConstantNode->AppendAttr( "value", c->second );
|
|
|
|
|
}
|
|
|
|
|
FOREACHM_CONST( RString, RString, mStringConstants, s )
|
|
|
|
|
{
|
2006-09-29 20:34:18 +00:00
|
|
|
XNode *pConstantNode = pConstantsNode->AppendChild( "Constant" );
|
2006-09-26 10:33:32 +00:00
|
|
|
pConstantNode->AppendAttr( "name", s->first );
|
2006-10-01 07:35:59 +00:00
|
|
|
pConstantNode->AppendAttr( "value", ssprintf("'%s'", s->second.c_str()) );
|
2006-08-08 07:43:12 +00:00
|
|
|
}
|
2006-10-14 20:33:08 +00:00
|
|
|
|
2006-08-08 07:43:12 +00:00
|
|
|
return pLuaNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
bool LuaHelpers::RunScriptFile( const RString &sFile )
|
2005-01-19 23:33:19 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sScript;
|
2006-10-09 05:43:24 +00:00
|
|
|
if( !GetFileContents(sFile, sScript) )
|
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;
|
2006-10-14 02:27:49 +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-10-15 01:14:32 +00:00
|
|
|
bool LuaHelpers::RunScript( Lua *L, const RString &sScript, const RString &sName, RString &sError, int iArgs, int iReturnValues )
|
2004-09-22 04:55:31 +00:00
|
|
|
{
|
2005-01-19 23:33:19 +00:00
|
|
|
// load string
|
|
|
|
|
{
|
2006-10-13 20:41:33 +00:00
|
|
|
int ret = luaL_loadbuffer( L, sScript.data(), sScript.size(), sName );
|
2005-01-19 23:33:19 +00:00
|
|
|
if( ret )
|
|
|
|
|
{
|
2006-09-22 08:48:49 +00:00
|
|
|
LuaHelpers::Pop( L, sError );
|
2006-10-15 01:17:30 +00:00
|
|
|
lua_pop( L, iArgs );
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-15 01:14:32 +00:00
|
|
|
// move the function above the params
|
|
|
|
|
lua_insert( L, lua_gettop(L) - iArgs );
|
|
|
|
|
|
2005-01-19 23:33:19 +00:00
|
|
|
// evaluate
|
|
|
|
|
{
|
2006-10-15 01:14:32 +00:00
|
|
|
int ret = lua_pcall( L, iArgs, 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
|
|
|
|
2006-09-26 05:35:24 +00:00
|
|
|
bool LuaHelpers::RunExpression( Lua *L, const RString &sExpression, const RString &sName )
|
2005-01-19 23:33:19 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sError;
|
2006-10-15 01:14:32 +00:00
|
|
|
if( !LuaHelpers::RunScript(L, "return " + sExpression, sName.empty()? RString("in"):sName, sError, 0, 1) )
|
2005-02-15 07:17:49 +00:00
|
|
|
{
|
2006-09-26 05:44:44 +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-10-08 22:24:15 +00:00
|
|
|
void LuaHelpers::ParseCommandList( Lua *L, const RString &sCommands, const RString &sName )
|
|
|
|
|
{
|
|
|
|
|
RString sLuaFunction;
|
|
|
|
|
if( sCommands.size() > 0 && sCommands[0] == '\033' )
|
|
|
|
|
{
|
|
|
|
|
/* This is a compiled Lua chunk. Just pass it on directly. */
|
|
|
|
|
sLuaFunction = sCommands;
|
|
|
|
|
}
|
|
|
|
|
else if( sCommands.size() > 0 && sCommands[0] == '%' )
|
|
|
|
|
{
|
|
|
|
|
sLuaFunction = "return ";
|
|
|
|
|
sLuaFunction.append( sCommands.begin()+1, sCommands.end() );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Commands cmds;
|
|
|
|
|
ParseCommands( sCommands, cmds );
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Convert cmds to a Lua function
|
|
|
|
|
//
|
|
|
|
|
ostringstream s;
|
|
|
|
|
|
2006-10-23 09:14:27 +00:00
|
|
|
s << "return function(self)\n";
|
2006-10-08 22:24:15 +00:00
|
|
|
|
|
|
|
|
FOREACH_CONST( Command, cmds.v, c )
|
|
|
|
|
{
|
|
|
|
|
const Command& cmd = (*c);
|
|
|
|
|
RString sName = cmd.GetName();
|
|
|
|
|
TrimLeft( sName );
|
|
|
|
|
TrimRight( sName );
|
|
|
|
|
s << "\tself:" << sName << "(";
|
|
|
|
|
|
|
|
|
|
for( unsigned i=1; i<cmd.m_vsArgs.size(); i++ )
|
|
|
|
|
{
|
|
|
|
|
RString sArg = cmd.m_vsArgs[i];
|
|
|
|
|
|
|
|
|
|
// "+200" -> "200"
|
|
|
|
|
if( sArg[0] == '+' )
|
|
|
|
|
sArg.erase( sArg.begin() );
|
|
|
|
|
|
2006-10-23 21:13:51 +00:00
|
|
|
if( sArg[0] == '#' ) // HTML color
|
2006-10-08 22:24:15 +00:00
|
|
|
{
|
|
|
|
|
RageColor c; // in case FromString fails
|
|
|
|
|
c.FromString( sArg );
|
|
|
|
|
// c is still valid if FromString fails
|
|
|
|
|
s << c.r << "," << c.g << "," << c.b << "," << c.a;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s << sArg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( i != cmd.m_vsArgs.size()-1 )
|
|
|
|
|
s << ",";
|
|
|
|
|
}
|
|
|
|
|
s << ")\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s << "end\n";
|
|
|
|
|
|
|
|
|
|
sLuaFunction = s.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RString sError;
|
2006-10-15 01:14:32 +00:00
|
|
|
if( !LuaHelpers::RunScript(L, sLuaFunction, sName, sError, 0, 1) )
|
2006-10-08 22:24:15 +00:00
|
|
|
LOG->Warn( "Compiling \"%s\": %s", sLuaFunction.c_str(), sError.c_str() );
|
|
|
|
|
|
|
|
|
|
/* The function is now on the stack. */
|
|
|
|
|
}
|
|
|
|
|
|
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-10-06 01:15:31 +00:00
|
|
|
RString sType;
|
|
|
|
|
luaL_pushtype( L, iArgNo );
|
|
|
|
|
LuaHelpers::Pop( L, sType );
|
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
|
|
|
}
|
|
|
|
|
|
2006-10-15 01:40:11 +00:00
|
|
|
void LuaHelpers::DeepCopy( lua_State *L )
|
|
|
|
|
{
|
|
|
|
|
luaL_checktype( L, -2, LUA_TTABLE );
|
|
|
|
|
luaL_checktype( L, -1, LUA_TTABLE );
|
|
|
|
|
|
|
|
|
|
/* Call DeepCopy(t, u), where t is our referenced object and u is the new table. */
|
|
|
|
|
lua_getglobal( L, "DeepCopy" );
|
|
|
|
|
|
|
|
|
|
ASSERT_M( !lua_isnil(L, -1), "DeepCopy() missing" );
|
|
|
|
|
ASSERT_M( lua_isfunction(L, -1), "DeepCopy() not a function" );
|
|
|
|
|
lua_insert( L, lua_gettop(L)-2 );
|
|
|
|
|
|
|
|
|
|
lua_call( L, 2, 0 );
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-05 19:57:07 +00:00
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
int lua_pushvalues( lua_State *L )
|
|
|
|
|
{
|
|
|
|
|
int iArgs = lua_tointeger( L, lua_upvalueindex(1) );
|
|
|
|
|
for( int i = 0; i < iArgs; ++i )
|
|
|
|
|
lua_pushvalue( L, lua_upvalueindex(i+2) );
|
|
|
|
|
return iArgs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaHelpers::PushValueFunc( lua_State *L, int iArgs )
|
|
|
|
|
{
|
|
|
|
|
int iTop = lua_gettop( L ) - iArgs + 1;
|
|
|
|
|
lua_pushinteger( L, iArgs );
|
|
|
|
|
lua_insert( L, iTop );
|
|
|
|
|
lua_pushcclosure( L, lua_pushvalues, iArgs+1 );
|
|
|
|
|
}
|
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
|
|
|
|
2006-10-14 02:26:33 +00:00
|
|
|
static bool Warn( const RString &sString )
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn( "%s", sString.c_str() );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
LuaFunction( Warn, Warn(SArg(1)) );
|
|
|
|
|
|
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)) );
|
|
|
|
|
|
2006-10-15 02:35:33 +00:00
|
|
|
#include "LuaBinding.h"
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
static int ReadFile( lua_State *L )
|
|
|
|
|
{
|
|
|
|
|
RString sPath = SArg(1);
|
|
|
|
|
|
|
|
|
|
/* Release Lua while we call GetFileContents, so we don't it while we read from he disk. */
|
|
|
|
|
LUA->YieldLua();
|
|
|
|
|
|
|
|
|
|
RString sFileContents;
|
|
|
|
|
bool bRet = GetFileContents( sPath, sFileContents );
|
|
|
|
|
|
|
|
|
|
LUA->UnyieldLua();
|
|
|
|
|
if( !bRet )
|
|
|
|
|
{
|
|
|
|
|
lua_pushnil( L );
|
|
|
|
|
lua_pushstring( L, "error" ); // XXX
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LuaHelpers::Push( L, sFileContents );
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-02 04:23:19 +00:00
|
|
|
static int GetThreadVariable( lua_State *L )
|
|
|
|
|
{
|
|
|
|
|
luaL_checkstring( L, 1 );
|
|
|
|
|
lua_pushvalue( L, 1 );
|
|
|
|
|
LuaThreadVariable::GetThreadVariable( L );
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-15 02:35:33 +00:00
|
|
|
const luaL_Reg luaTable[] =
|
|
|
|
|
{
|
|
|
|
|
LIST_METHOD( ReadFile ),
|
2006-11-02 04:23:19 +00:00
|
|
|
LIST_METHOD( GetThreadVariable ),
|
2006-10-15 02:35:33 +00:00
|
|
|
{ NULL, NULL }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LUA_REGISTER_NAMESPACE( lua )
|
|
|
|
|
|
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
|
|
|
*/
|