00001 #ifndef LUA_HELPERS_H 00002 #define LUA_HELPERS_H 00003 00004 struct lua_State; 00005 class LuaManager; 00006 typedef void (*RegisterActorFn)(lua_State*); 00007 00008 class LuaManager 00009 { 00010 public: 00011 // Every Actor should register its class at program initialization. 00012 static void Register( RegisterActorFn pfn ); 00013 00014 LuaManager(); 00015 ~LuaManager(); 00016 00017 void PrepareExpression( CString &sInOut ); // strip "//" comments and "+" 00018 00019 bool RunScriptFile( const CString &sFile ); 00020 00021 /* Reset the environment, freeing any globals left over by previously executed scripts. */ 00022 void ResetState(); 00023 00024 /* Run a complete script in the global environment, which returns no value. */ 00025 bool RunScript( const CString &sScript ); 00026 00027 /* Run an expression in the global environment, returning the given type. */ 00028 bool RunExpressionB( const CString &str ); 00029 float RunExpressionF( const CString &str ); 00030 bool RunExpressionS( const CString &str, CString &sOut ); 00031 00032 /* If sStr begins with @, evaluate the rest as an expression and store the result over sStr. */ 00033 bool RunAtExpression( CString &sStr ); 00034 00035 void Fail( const CString &err ); 00036 00037 void SetGlobal( const CString &sName, int val ) { PushStack(val); SetGlobal( sName ); } 00038 00039 void PushStack( bool val ); 00040 void PushStack( int val ); 00041 void PushStack( void *val ); 00042 void PushStack( const CString &val ); 00043 void PopStack( CString &out ); 00044 bool GetStack( int pos, int &out ); 00045 void SetGlobal( const CString &sName ); 00046 00047 /* Run an expression. The result is left on the Lua stack. */ 00048 bool RunExpression( const CString &str ); 00049 lua_State *L; 00050 private: 00051 }; 00052 00053 extern LuaManager *LUA; 00054 00055 #endif 00056 00057 /* 00058 * (c) 2004 Glenn Maynard 00059 * All rights reserved. 00060 * 00061 * Permission is hereby granted, free of charge, to any person obtaining a 00062 * copy of this software and associated documentation files (the 00063 * "Software"), to deal in the Software without restriction, including 00064 * without limitation the rights to use, copy, modify, merge, publish, 00065 * distribute, and/or sell copies of the Software, and to permit persons to 00066 * whom the Software is furnished to do so, provided that the above 00067 * copyright notice(s) and this permission notice appear in all copies of 00068 * the Software and that both the above copyright notice(s) and this 00069 * permission notice appear in supporting documentation. 00070 * 00071 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00072 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00073 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00074 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00075 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00076 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00077 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00078 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00079 * PERFORMANCE OF THIS SOFTWARE. 00080 */