int CLuaFunctionDefs::BindKey ( lua_State* luaVM ) { CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { if ( lua_type ( luaVM, 1 ) == LUA_TSTRING && lua_type ( luaVM, 2 ) == LUA_TSTRING ) { const char* szKey = lua_tostring ( luaVM, 1 ); const char* szHitState = lua_tostring ( luaVM, 2 ); if ( lua_type ( luaVM, 3 ) == LUA_TSTRING ) { const char* szResource = pLuaMain->GetResource()->GetName(); const char* szCommand = lua_tostring ( luaVM, 3 ); const char* szArguments = ""; if ( lua_type ( luaVM, 4 ) == LUA_TSTRING ) szArguments = lua_tostring ( luaVM, 4 ); if ( CStaticFunctionDefinitions::BindKey ( szKey, szHitState, szCommand, szArguments, szResource ) ) { lua_pushboolean ( luaVM, true ); return 1; } } else { // Jax: grab our arguments first, luaM_toref pops the stack! CLuaArguments Arguments; Arguments.ReadArguments ( luaVM, 4 ); int iLuaFunction = luaM_toref ( luaVM, 3 ); if ( VERIFY_FUNCTION ( iLuaFunction ) ) { if ( CStaticFunctionDefinitions::BindKey ( szKey, szHitState, pLuaMain, iLuaFunction, Arguments ) ) { lua_pushboolean ( luaVM, true ); return 1; } } else m_pScriptDebugging->LogBadPointer ( luaVM, "bindKey", "function", 3 ); } } } lua_pushboolean ( luaVM, false ); return 1; }
CLuaTimer* CLuaTimerManager::AddTimer ( lua_State* luaVM ) { if ( luaVM ) { int iArgument2 = lua_type ( luaVM, 2 ); int iArgument3 = lua_type ( luaVM, 3 ); if ( lua_type ( luaVM, 1 ) && (( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) || ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) )) { // Grab the string argument, start-time, delay and repeats unsigned long ulTimeDelay = static_cast < unsigned long > ( lua_tonumber ( luaVM, 2 ) ); unsigned int uiRepeats = static_cast < unsigned int > ( lua_tonumber ( luaVM, 3 ) ); // Check for the minimum interval if ( ulTimeDelay < LUA_TIMER_MIN_INTERVAL ) return NULL; // Grab the arguments from argument 4 and up CLuaArguments Arguments; Arguments.ReadArguments ( luaVM, 4 ); int iLuaFunction = luaM_toref ( luaVM, 1 ); if ( iLuaFunction != LUA_REFNIL ) { // Add the timer CLuaTimer* pLuaTimer = new CLuaTimer ( iLuaFunction, Arguments ); pLuaTimer->SetStartTime ( GetTime () ); pLuaTimer->SetDelay ( ulTimeDelay ); pLuaTimer->SetRepeats ( uiRepeats ); m_TimerList.push_back ( pLuaTimer ); return pLuaTimer; } } } return false; }
int CLuaFunctionDefs::Call ( lua_State* luaVM ) { CResource * pResource = NULL; SString strFunctionName = ""; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pResource ); argStream.ReadString ( strFunctionName ); if ( !argStream.HasErrors ( ) ) { // Grab our VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { // Grab this resource CResource* pThisResource = pLuaMain->GetResource (); if ( pThisResource ) { if ( pResource ) { //Get the target Lua VM lua_State* targetLuaVM = pResource->GetVM()->GetVM(); // Read out the vargs CLuaArguments args; args.ReadArguments ( luaVM, 3 ); CLuaArguments returns; LUA_CHECKSTACK ( targetLuaVM, 1 ); // Ensure some room //Lets grab the original hidden variables so we can restore them later lua_getglobal ( targetLuaVM, "sourceResource" ); CLuaArgument OldResource ( luaVM, -1 ); lua_pop( targetLuaVM, 1 ); lua_getglobal ( targetLuaVM, "sourceResourceRoot" ); CLuaArgument OldResourceRoot ( luaVM, -1 ); lua_pop( targetLuaVM, 1 ); //Set the new values for the current sourceResource, and sourceResourceRoot lua_pushresource ( targetLuaVM, pThisResource ); lua_setglobal ( targetLuaVM, "sourceResource" ); lua_pushelement ( targetLuaVM, pThisResource->GetResourceEntity() ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); // Call the exported function with the given name and the args if ( pResource->CallExportedFunction ( strFunctionName, args, returns, *pThisResource ) ) { // Push return arguments returns.PushArguments ( luaVM ); //Restore the old variables OldResource.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResource" ); OldResourceRoot.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); return returns.Count (); } else { //Restore the old variables OldResource.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResource" ); OldResourceRoot.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); m_pScriptDebugging->LogError ( luaVM, "call: failed to call '%s:%s'", pResource->GetName (), *strFunctionName ); } } else { m_pScriptDebugging->LogBadType ( luaVM ); } } } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaDatabaseDefs::ExecuteSQLQuery ( lua_State* luaVM ) { SString strQuery; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strQuery ); if ( !argStream.HasErrors () ) { CLuaArguments Args; CRegistryResult Result; Args.ReadArguments ( luaVM, 2 ); CPerfStatSqliteTiming::GetSingleton ()->SetCurrentResource ( luaVM ); if ( CStaticFunctionDefinitions::ExecuteSQLQuery ( strQuery, &Args, &Result ) ) { lua_newtable ( luaVM ); int i = 0; for ( CRegistryResultIterator iter = Result->begin (); iter != Result->end (); ++iter, ++i ) { const CRegistryResultRow& row = *iter; //for ( int i = 0; i < Result.nRows; i++ ) { lua_newtable ( luaVM ); // new table lua_pushnumber ( luaVM, i + 1 ); // row index number (starting at 1, not 0) lua_pushvalue ( luaVM, -2 ); // value lua_settable ( luaVM, -4 ); // refer to the top level table for ( int j = 0; j < Result->nColumns; j++ ) { const CRegistryResultCell& cell = row[j]; if ( cell.nType == SQLITE_NULL ) continue; // Push the column name lua_pushlstring ( luaVM, Result->ColNames[j].c_str (), Result->ColNames[j].size () ); switch ( cell.nType ) // push the value with the right type { case SQLITE_INTEGER: lua_pushnumber ( luaVM, static_cast <double> ( cell.nVal ) ); break; case SQLITE_FLOAT: lua_pushnumber ( luaVM, cell.fVal ); break; case SQLITE_BLOB: lua_pushlstring ( luaVM, (const char *) cell.pVal, cell.nLength ); break; case SQLITE_TEXT: lua_pushlstring ( luaVM, (const char *) cell.pVal, cell.nLength - 1 ); break; default: lua_pushnil ( luaVM ); } lua_settable ( luaVM, -3 ); } lua_pop ( luaVM, 1 ); // pop the inner table } return 1; } else { SString strError = "Database query failed: " + CStaticFunctionDefinitions::SQLGetLastError (); m_pScriptDebugging->LogError ( luaVM, "%s", strError.c_str () ); lua_pushstring ( luaVM, strError ); lua_pushboolean ( luaVM, false ); return 2; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::Call ( lua_State* luaVM ) { // Grab our VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { // Grab this resource CResource* pThisResource = pLuaMain->GetResource (); if ( pThisResource ) { // Typechecking if ( lua_istype ( luaVM, 1, LUA_TLIGHTUSERDATA ) && lua_istype ( luaVM, 2, LUA_TSTRING ) ) { // Grab the resource CResource* pResource = lua_toresource ( luaVM, 1 ); if ( pResource ) { //Get the target Lua VM lua_State* targetLuaVM = pResource->GetVM()->GetVM(); // The function name const char* szFunctionName = lua_tostring ( luaVM, 2 ); // Read out the vargs CLuaArguments args; args.ReadArguments ( luaVM, 3 ); CLuaArguments returns; //Lets grab the original hidden variables so we can restore them later lua_getglobal ( targetLuaVM, "sourceResource" ); CLuaArgument OldResource ( luaVM, -1 ); lua_getglobal ( targetLuaVM, "sourceResourceRoot" ); CLuaArgument OldResourceRoot ( luaVM, -1 ); //Set the new values for the current sourceResource, and sourceResourceRoot lua_pushresource ( targetLuaVM, pThisResource ); lua_setglobal ( targetLuaVM, "sourceResource" ); lua_pushelement ( targetLuaVM, pThisResource->GetResourceEntity() ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); // Call the exported function with the given name and the args if ( pResource->CallExportedFunction ( szFunctionName, args, returns, *pThisResource ) ) { // Push return arguments returns.PushArguments ( luaVM ); //Restore the old variables OldResource.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResource" ); OldResourceRoot.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); return returns.Count (); } else { //Restore the old variables OldResource.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResource" ); OldResourceRoot.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); m_pScriptDebugging->LogError ( luaVM, "call: failed to call '%s:%s'", pResource->GetName (), szFunctionName ); } } else { m_pScriptDebugging->LogBadPointer ( luaVM, "call", "resource", 1 ); } } else { m_pScriptDebugging->LogBadType ( luaVM, "call" ); } } } // Failed lua_pushboolean ( luaVM, false ); return 1; }