2004-02-14 21:08:12 +00:00
|
|
|
#ifndef LUA_HELPERS_H
|
|
|
|
|
#define LUA_HELPERS_H
|
|
|
|
|
|
|
|
|
|
struct lua_State;
|
2005-01-24 02:04:03 +00:00
|
|
|
class LuaManager;
|
|
|
|
|
typedef void (*RegisterActorFn)(lua_State*);
|
|
|
|
|
|
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.
|
|
|
|
|
static void Register( RegisterActorFn pfn );
|
|
|
|
|
|
2005-01-19 22:27:24 +00:00
|
|
|
LuaManager();
|
|
|
|
|
~LuaManager();
|
|
|
|
|
|
2004-11-06 06:00:58 +00:00
|
|
|
void PrepareExpression( CString &sInOut ); // strip "//" comments and "+"
|
2005-01-19 22:27:24 +00:00
|
|
|
|
2005-01-19 23:33:19 +00:00
|
|
|
bool RunScriptFile( const CString &sFile );
|
|
|
|
|
|
|
|
|
|
/* Reset the environment, freeing any globals left over by previously executed scripts. */
|
2005-01-24 03:05:37 +00:00
|
|
|
void ResetState();
|
2005-01-19 23:33:19 +00:00
|
|
|
|
|
|
|
|
/* Run a complete script in the global environment, which returns no value. */
|
2005-02-15 07:17:49 +00:00
|
|
|
bool RunScript( const CString &sScript, const CString &sName, CString &sError, int iReturnValues = 0 );
|
2005-01-19 23:33:19 +00:00
|
|
|
|
2005-01-19 22:27:24 +00:00
|
|
|
/* Run an expression in the global environment, returning the given type. */
|
2004-09-22 04:55:31 +00:00
|
|
|
bool RunExpressionB( const CString &str );
|
|
|
|
|
float RunExpressionF( const CString &str );
|
2005-01-16 20:36:27 +00:00
|
|
|
bool RunExpressionS( const CString &str, CString &sOut );
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2005-01-20 01:08:26 +00:00
|
|
|
/* If sStr begins with @, evaluate the rest as an expression and store the result over sStr. */
|
2005-02-16 00:16:14 +00:00
|
|
|
bool RunAtExpressionS( CString &sStr );
|
|
|
|
|
float RunAtExpressionF( const CString &sStr );
|
2005-01-20 01:08:26 +00:00
|
|
|
|
2005-01-19 23:01:53 +00:00
|
|
|
void Fail( const CString &err );
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2005-01-19 23:01:53 +00:00
|
|
|
void SetGlobal( const CString &sName, int val ) { PushStack(val); SetGlobal( sName ); }
|
2005-01-30 02:01:27 +00:00
|
|
|
void SetGlobal( const CString &sName, bool val ) { PushStack(val); SetGlobal( sName ); }
|
|
|
|
|
void UnsetGlobal( const CString &sName ) { PushStackNil(); SetGlobal( sName ); }
|
2004-02-14 21:08:12 +00:00
|
|
|
|
2005-01-30 02:01:27 +00:00
|
|
|
void PushStackNil();
|
2005-02-05 23:03:20 +00:00
|
|
|
void PushNopFunction();
|
2005-01-30 02:01:27 +00:00
|
|
|
static void PushStack( bool val, lua_State *L = NULL );
|
|
|
|
|
static void PushStack( float val, lua_State *L = NULL );
|
|
|
|
|
static void PushStack( int val, lua_State *L = NULL );
|
|
|
|
|
static void PushStack( void *val, lua_State *L = NULL );
|
|
|
|
|
static void PushStack( const CString &val, lua_State *L = NULL );
|
2005-01-19 23:01:53 +00:00
|
|
|
void PopStack( CString &out );
|
|
|
|
|
bool GetStack( int pos, int &out );
|
|
|
|
|
void SetGlobal( const CString &sName );
|
2005-01-19 22:27:24 +00:00
|
|
|
|
2005-02-13 04:09:28 +00:00
|
|
|
/* Create a Lua array (a table with indices starting at 1) of the given vector,
|
|
|
|
|
* and push it on the stack. */
|
|
|
|
|
static void CreateTableFromArray( const vector<bool> &aIn, lua_State *L = NULL );
|
|
|
|
|
/* Read the table at the top of the stack back into a vector. */
|
|
|
|
|
static void ReadArrayFromTable( vector<bool> &aOut, lua_State *L = NULL );
|
2005-01-30 02:01:27 +00:00
|
|
|
|
2005-01-19 22:27:24 +00:00
|
|
|
/* Run an expression. The result is left on the Lua stack. */
|
2005-02-15 02:07:59 +00:00
|
|
|
bool RunExpression( const CString &sExpression );
|
2005-01-19 22:27:24 +00:00
|
|
|
lua_State *L;
|
2004-02-14 21:08:12 +00:00
|
|
|
};
|
|
|
|
|
|
2005-01-19 22:27:24 +00:00
|
|
|
extern LuaManager *LUA;
|
|
|
|
|
|
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
|
|
|
*/
|