Files
itgmania212121/stepmania/src/LuaManager.cpp
T

740 lines
18 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"
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"
#include "XmlFile.h"
#include "Command.h"
2005-12-28 04:22:24 +00:00
#include <sstream>
2004-04-05 05:22:32 +00:00
#include <csetjmp>
#include <cassert>
LuaManager *LUA = NULL;
static vector<lua_State *> g_FreeStateList;
static LuaFunctionList *g_LuaFunctions = NULL;
2004-02-14 21:08:12 +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
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();
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 );
}
namespace LuaHelpers
2005-02-22 04:20:58 +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
}
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
{
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 );
}
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
m_pLock = new RageMutex( "Lua" );
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
/* 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();
}
LuaManager::~LuaManager()
{
2006-10-14 20:49:35 +00:00
lua_close( m_pLuaMain );
delete m_pLock;
g_FreeStateList.clear();
}
Lua *LuaManager::Get()
{
m_pLock->Lock();
ASSERT( lua_gettop(m_pLuaMain) == 1 );
lua_State *pRet;
if( g_FreeStateList.empty() )
{
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
{
pRet = g_FreeStateList.back();
g_FreeStateList.pop_back();
2006-01-14 06:53:09 +00:00
}
return pRet;
}
void LuaManager::Release( Lua *&p )
{
g_FreeStateList.push_back( p );
ASSERT( lua_gettop(p) == 0 );
m_pLock->Unlock();
p = NULL;
}
2005-02-16 17:33:28 +00:00
void LuaManager::RegisterTypes()
{
2006-10-14 20:37:30 +00:00
Lua *L = LUA->Get();
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
}
2004-02-14 21:08:12 +00:00
namespace
{
struct LClass
{
RString m_sBaseName;
vector<RString> m_vMethods;
};
}
XNode *LuaHelpers::GetLuaInformation()
{
2006-10-14 20:33:08 +00:00
Lua *L = LUA->Get();
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" );
// Tricky. We have to get the classes from lua.
vector<RString> vFunctions;
map<RString, LClass> mClasses;
map<RString, RString> mSingletons;
map<RString, float> mConstants;
map<RString, RString> mStringConstants;
map<RString, vector<RString> > mEnums;
lua_pushnil( L ); // initial key
while( lua_next(L, LUA_GLOBALSINDEX) )
{
// key is at -2, value is at -1
/* Tricky. FromStack() calls lua_tolstring() which changes the underlying cell
* if it is not a string. This confuses lua_next(). Copy the value first.
* http://www.lua.org/manual/5.1/manual.html#lua_tolstring */
RString sKey;
lua_pushvalue( L, -2 );
LuaHelpers::Pop( L, sKey );
switch( lua_type(L, -1) )
{
case LUA_TTABLE:
{
if( luaL_getmetafield(L, -1, "class") )
{
const char *name = lua_tostring( L, -1 );
if( !name )
{
lua_pop( L, 1 ); // pop nil
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-10-05 20:04:14 +00:00
name = lua_tostring( L, -1 );
if( name )
c.m_sBaseName = name;
lua_pop( L, 1 ); // pop name
}
// Get methods.
lua_pushnil( L ); // initial key
while( lua_next(L, -2) )
{
// Again, be careful about reading the key as a string.
lua_pop( L, 1 ); // pop value
lua_pushvalue( L, -1 );
RString sMethod;
if( LuaHelpers::Pop(L, sMethod) )
c.m_vMethods.push_back( sMethod );
}
sort( c.m_vMethods.begin(), c.m_vMethods.end() );
break;
}
}
// fall through
case LUA_TUSERDATA: // table or userdata: class instance
{
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-06 00:46:46 +00:00
vector<RString> &vEnum = mEnums[sKey];
LuaHelpers::ReadArrayFromTable( vEnum, L );
}
else
{
mSingletons[sKey] = sType;
}
break;
}
case LUA_TNUMBER:
2006-09-29 20:35:14 +00:00
LuaHelpers::FromStack( L, mConstants[sKey], -1 );
break;
case LUA_TSTRING:
2006-09-29 20:35:14 +00:00
LuaHelpers::FromStack( L, mStringConstants[sKey], -1 );
break;
case LUA_TFUNCTION:
vFunctions.push_back( sKey );
break;
}
lua_pop( L, 1 ); // pop value
}
sort( vFunctions.begin(), vFunctions.end() );
FOREACH_CONST( RString, vFunctions, func )
{
XNode *pFunctionNode = pGlobalsNode->AppendChild( "Function" );
pFunctionNode->AppendAttr( "name", *func );
}
FOREACHM_CONST( RString, LClass, mClasses, c )
{
2006-09-29 20:34:18 +00:00
XNode *pClassNode = pClassesNode->AppendChild( "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 = pClassNode->AppendChild( "Function" );
pMethodNode->AppendAttr( "name", *m );
}
}
FOREACHM_CONST( RString, RString, mSingletons, s )
{
if( mClasses.find(s->first) != mClasses.end() )
continue;
2006-09-29 20:34:18 +00:00
XNode *pSingletonNode = pSingletonsNode->AppendChild( "Singleton" );
pSingletonNode->AppendAttr( "name", s->first );
pSingletonNode->AppendAttr( "class", s->second );
}
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" );
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" );
pEnumValueNode->AppendAttr( "name", ssprintf("'%s'", vEnum[i].c_str()) );
pEnumValueNode->AppendAttr( "value", i );
}
}
FOREACHM_CONST( RString, float, mConstants, c )
{
2006-09-29 20:34:18 +00:00
XNode *pConstantNode = pConstantsNode->AppendChild( "Constant" );
pConstantNode->AppendAttr( "name", c->first );
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" );
pConstantNode->AppendAttr( "name", s->first );
pConstantNode->AppendAttr( "value", ssprintf("'%s'", s->second.c_str()) );
}
2006-10-14 20:33:08 +00:00
LUA->Release( L );
return pLuaNode;
}
2006-01-22 01:00:06 +00:00
void LuaHelpers::PrepareExpression( RString &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 );
}
2006-01-22 01:00:06 +00:00
bool LuaHelpers::RunScriptFile( const RString &sFile )
{
2006-01-22 01:00:06 +00:00
RString sScript;
2006-10-09 05:43:24 +00:00
if( !GetFileContents(sFile, sScript) )
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 ) )
{
2006-09-21 07:25:38 +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;
}
2006-09-21 07:25:38 +00:00
LUA->Release( L );
2005-06-16 22:10:24 +00:00
return true;
2006-08-18 00:04:08 +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 )
{
// load string
{
2006-10-13 20:41:33 +00:00
int ret = luaL_loadbuffer( L, sScript.data(), sScript.size(), sName );
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 );
for( int i = 0; i < iReturnValues; ++i )
lua_pushnil( L );
return false;
}
}
2006-10-15 01:14:32 +00:00
// move the function above the params
lua_insert( L, lua_gettop(L) - iArgs );
// evaluate
{
2006-10-15 01:14:32 +00:00
int ret = lua_pcall( L, iArgs, iReturnValues, 0 );
if( ret )
{
2006-09-22 08:48:49 +00:00
LuaHelpers::Pop( L, sError );
for( int i = 0; i < iReturnValues; ++i )
lua_pushnil( L );
return false;
}
}
return true;
}
2005-01-19 23:01:53 +00:00
bool LuaHelpers::RunExpression( Lua *L, const RString &sExpression, const RString &sName )
{
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) )
{
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;
}
return true;
}
2006-01-22 01:00:06 +00:00
bool LuaHelpers::RunExpressionB( const RString &str )
{
2005-06-16 06:23:59 +00:00
Lua *L = LUA->Get();
RunExpression( L, str );
bool result;
LuaHelpers::Pop( L, result );
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
float LuaHelpers::RunExpressionF( const RString &str )
{
2005-06-16 06:30:33 +00:00
Lua *L = LUA->Get();
2006-09-22 02:08:00 +00:00
RunExpression( L, str );
float result;
LuaHelpers::Pop( L, result );
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
}
void LuaHelpers::RunExpressionS( const RString &str, RString &sOut )
{
Lua *L = LUA->Get();
2006-09-22 02:08:00 +00:00
RunExpression( L, str );
2006-09-22 08:48:49 +00:00
LuaHelpers::Pop( L, sOut );
2006-09-21 07:25:38 +00:00
LUA->Release( L );
}
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;
LuaHelpers::RunExpressionS( sStr, sOut );
2005-01-20 01:08:26 +00:00
sStr = sOut;
return true;
}
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;
s << "return function(self,parent)\n";
FOREACH_CONST( Command, cmds.v, c )
{
const Command& cmd = (*c);
RString sName = cmd.GetName();
TrimLeft( sName );
TrimRight( sName );
s << "\tself:" << sName << "(";
bool bFirstParamIsString =
sName == "effectclock" ||
sName == "playcommand" ||
sName == "queuecommand" ||
sName == "queuemessage";
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() );
if( i==1 && bFirstParamIsString ) // string literal
{
sArg.Replace( "'", "\\'" ); // escape quote
s << "'" << sArg << "'";
}
else if( sArg[0] == '#' ) // HTML color
{
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) )
LOG->Warn( "Compiling \"%s\": %s", sLuaFunction.c_str(), sError.c_str() );
/* The function is now on the stack. */
}
/* 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 )
{
2006-10-06 01:15:31 +00:00
RString sType;
luaL_pushtype( L, iArgNo );
LuaHelpers::Pop( L, sType );
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() );
}
}
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_;
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)) );
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
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)) );
2004-02-14 21:08: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
*/