2005-02-17 07:12:20 +00:00
|
|
|
#ifndef LUA_MANAGER_H
|
|
|
|
|
#define LUA_MANAGER_H
|
2004-02-14 21:08:12 +00:00
|
|
|
|
|
|
|
|
struct lua_State;
|
2005-06-15 08:16:28 +00:00
|
|
|
typedef lua_State Lua;
|
2005-02-17 07:12:20 +00:00
|
|
|
typedef void (*RegisterWithLuaFn)(lua_State*);
|
2005-06-15 08:16:28 +00:00
|
|
|
class RageMutex;
|
2006-08-08 07:43:12 +00:00
|
|
|
class XNode;
|
2005-01-24 02:04:03 +00:00
|
|
|
|
2005-05-28 22:11:20 +00:00
|
|
|
extern "C"
|
|
|
|
|
{
|
2006-09-25 07:36:14 +00:00
|
|
|
#include "lua-5.1/src/lua.h"
|
|
|
|
|
#include "lua-5.1/src/lualib.h"
|
|
|
|
|
#include "lua-5.1/src/lauxlib.h"
|
2005-05-28 22:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
2005-02-25 04:37:27 +00:00
|
|
|
class LuaManager;
|
|
|
|
|
extern LuaManager *LUA;
|
|
|
|
|
|
2005-01-19 22:27:24 +00:00
|
|
|
class LuaManager
|
2004-02-14 21:08:12 +00:00
|
|
|
{
|
2005-01-19 22:27:24 +00:00
|
|
|
public:
|
2005-01-24 02:04:03 +00:00
|
|
|
// Every Actor should register its class at program initialization.
|
2005-02-17 07:12:20 +00:00
|
|
|
static void Register( RegisterWithLuaFn pfn );
|
2005-01-24 02:04:03 +00:00
|
|
|
|
2005-01-19 22:27:24 +00:00
|
|
|
LuaManager();
|
|
|
|
|
~LuaManager();
|
|
|
|
|
|
2005-06-15 08:16:28 +00:00
|
|
|
Lua *Get();
|
|
|
|
|
void Release( Lua *&p );
|
|
|
|
|
|
2006-09-20 23:31:54 +00:00
|
|
|
/* Register all subscribing types. There's no harm in registering when already registered. */
|
|
|
|
|
void RegisterTypes();
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
void SetGlobal( const RString &sName, int val );
|
|
|
|
|
void SetGlobal( const RString &sName, const RString &val );
|
|
|
|
|
void UnsetGlobal( const RString &sName );
|
2006-08-08 07:43:12 +00:00
|
|
|
XNode *GetLuaInformation() const;
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2005-06-16 22:27:11 +00:00
|
|
|
private:
|
2005-01-19 22:27:24 +00:00
|
|
|
lua_State *L;
|
2005-05-29 21:12:30 +00:00
|
|
|
|
2005-06-15 08:16:28 +00:00
|
|
|
RageMutex *m_pLock;
|
2004-02-14 21:08:12 +00:00
|
|
|
};
|
|
|
|
|
|
2005-03-13 00:01:05 +00:00
|
|
|
|
2005-03-12 22:32:54 +00:00
|
|
|
namespace LuaHelpers
|
|
|
|
|
{
|
2006-09-23 02:44:35 +00:00
|
|
|
/* Run a script with the given name. The given number of return values are left on
|
|
|
|
|
* the Lua stack. On error, nils are left on the stack, sError is set and
|
|
|
|
|
* false is returned. */
|
2006-01-22 01:00:06 +00:00
|
|
|
bool RunScript( Lua *L, const RString &sScript, const RString &sName, RString &sError, int iReturnValues = 0 );
|
2005-06-16 06:00:39 +00:00
|
|
|
|
2006-09-22 18:53:39 +00:00
|
|
|
/* Run the given expression, returning a single value, and leave the return value on the
|
|
|
|
|
* stack. On error, push nil. */
|
2006-09-23 02:09:58 +00:00
|
|
|
bool RunExpression( Lua *L, const RString &sExpression, const RString &sName = "" );
|
2006-09-22 18:53:39 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
bool RunScriptFile( const RString &sFile );
|
2005-06-16 06:10:50 +00:00
|
|
|
|
2005-06-16 22:22:37 +00:00
|
|
|
/* Strip "//" comments and "+". */
|
2006-01-22 01:00:06 +00:00
|
|
|
void PrepareExpression( RString &sInOut );
|
2005-06-16 22:22:37 +00:00
|
|
|
|
2005-06-16 05:37:31 +00:00
|
|
|
/* Create a Lua array (a table with indices starting at 1) of the given vector,
|
|
|
|
|
* and push it on the stack. */
|
|
|
|
|
void CreateTableFromArrayB( Lua *L, const vector<bool> &aIn );
|
|
|
|
|
|
|
|
|
|
/* Read the table at the top of the stack back into a vector. */
|
|
|
|
|
void ReadArrayFromTableB( Lua *L, vector<bool> &aOut );
|
|
|
|
|
|
2005-06-16 06:23:59 +00:00
|
|
|
/* Run an expression in the global environment, returning the given type. */
|
2006-01-22 01:00:06 +00:00
|
|
|
bool RunExpressionB( const RString &str );
|
|
|
|
|
float RunExpressionF( const RString &str );
|
|
|
|
|
int RunExpressionI( const RString &str );
|
2006-09-23 02:11:54 +00:00
|
|
|
void RunExpressionS( const RString &str, RString &sOut );
|
2005-06-16 06:23:59 +00:00
|
|
|
|
2005-06-16 22:17:45 +00:00
|
|
|
/* If sStr begins with @, evaluate the rest as an expression and store the result over sStr. */
|
2006-01-22 01:00:06 +00:00
|
|
|
bool RunAtExpressionS( RString &sStr );
|
2005-06-16 22:17:45 +00:00
|
|
|
|
2005-06-20 04:32:24 +00:00
|
|
|
template<class T>
|
2006-09-26 08:54:54 +00:00
|
|
|
void Push( lua_State *L, T *pObject );
|
2005-06-20 04:32:24 +00:00
|
|
|
|
2006-09-26 08:54:54 +00:00
|
|
|
void Push( lua_State *L, const bool &Object );
|
|
|
|
|
void Push( lua_State *L, const float &Object );
|
|
|
|
|
void Push( lua_State *L, const int &Object );
|
|
|
|
|
void Push( lua_State *L, const RString &Object );
|
2005-06-20 04:32:24 +00:00
|
|
|
|
2006-09-22 08:38:30 +00:00
|
|
|
bool FromStack( Lua *L, bool &Object, int iOffset );
|
|
|
|
|
bool FromStack( Lua *L, float &Object, int iOffset );
|
|
|
|
|
bool FromStack( Lua *L, int &Object, int iOffset );
|
|
|
|
|
bool FromStack( Lua *L, RString &Object, int iOffset );
|
2005-06-20 04:32:24 +00:00
|
|
|
|
2006-09-21 07:12:49 +00:00
|
|
|
template<class T>
|
2006-09-22 08:48:49 +00:00
|
|
|
bool Pop( lua_State *L, T &val )
|
2006-09-21 07:12:49 +00:00
|
|
|
{
|
2006-09-22 08:38:30 +00:00
|
|
|
bool bRet = LuaHelpers::FromStack( L, val, -1 );
|
2006-09-22 08:33:20 +00:00
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
return bRet;
|
2006-09-21 07:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
2005-03-12 22:32:54 +00:00
|
|
|
template<class T>
|
2005-04-25 07:01:52 +00:00
|
|
|
void ReadArrayFromTable( vector<T> &aOut, lua_State *L )
|
2005-03-12 22:32:54 +00:00
|
|
|
{
|
|
|
|
|
luaL_checktype( L, -1, LUA_TTABLE );
|
|
|
|
|
|
|
|
|
|
unsigned iCount = luaL_getn( L, -1 );
|
|
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < iCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
lua_rawgeti( L, -1, i+1 );
|
|
|
|
|
T value = T();
|
2006-09-22 08:48:49 +00:00
|
|
|
LuaHelpers::Pop( L, value );
|
2005-03-12 22:32:54 +00:00
|
|
|
aOut.push_back( value );
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-03-13 00:01:05 +00:00
|
|
|
template<class T>
|
2005-02-21 06:51:10 +00:00
|
|
|
void CreateTableFromArray( const vector<T> &aIn, lua_State *L )
|
|
|
|
|
{
|
|
|
|
|
lua_newtable( L );
|
|
|
|
|
for( unsigned i = 0; i < aIn.size(); ++i )
|
|
|
|
|
{
|
2006-09-26 08:54:54 +00:00
|
|
|
LuaHelpers::Push( L, aIn[i] );
|
2005-02-21 06:51:10 +00:00
|
|
|
lua_rawseti( L, -2, i+1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-05-28 07:27:49 +00:00
|
|
|
|
2005-06-15 08:33:26 +00:00
|
|
|
int TypeError( Lua *L, int narg, const char *tname );
|
2006-09-26 01:36:31 +00:00
|
|
|
inline int AbsIndex( Lua *L, int i ) { if( i > 0 || i <= LUA_REGISTRYINDEX ) return i; return lua_gettop( L ) + i + 1; }
|
2005-03-13 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
2005-02-21 06:51:10 +00:00
|
|
|
|
2005-02-17 07:32:21 +00:00
|
|
|
#define REGISTER_WITH_LUA_FUNCTION( Fn ) \
|
|
|
|
|
class Register##Fn { public: Register##Fn() { LuaManager::Register( Fn ); } }; \
|
|
|
|
|
static Register##Fn register##Fn;
|
|
|
|
|
|
2005-06-20 04:32:24 +00:00
|
|
|
inline bool MyLua_checkboolean (lua_State *L, int numArg)
|
|
|
|
|
{
|
2006-09-21 07:25:38 +00:00
|
|
|
luaL_checktype( L, numArg, LUA_TBOOLEAN );
|
|
|
|
|
return !!lua_toboolean( L, numArg );
|
2005-06-20 04:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-09-26 05:50:40 +00:00
|
|
|
#define SArg(n) (luaL_checkstring(L,(n)))
|
|
|
|
|
#define IArg(n) (luaL_checkint(L,(n)))
|
|
|
|
|
#define BArg(n) (MyLua_checkboolean(L,(n)))
|
|
|
|
|
#define FArg(n) ((float) luaL_checknumber(L,(n)))
|
2005-05-29 03:47:40 +00:00
|
|
|
|
2004-02-14 21:08:12 +00:00
|
|
|
#endif
|
2004-05-31 22:42:12 +00:00
|
|
|
|
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
|
|
|
*/
|