uint32 Script::runString(const int8 *cmdString) { assert(mEngine->getMasterState() && "Invalid engine master state"); assert(mThreadState && "Invalid thread state"); if(luaL_loadbuffer(mThreadState,cmdString,strlen(cmdString),"Console") == 0) { if(lua_pcall(mThreadState,lua_gettop(mThreadState) - 1,0,0) != 0) { _formatError(); return(1); } } else { _formatError(); return(1); } return(0); }
void Script::runFile(const int8 *fileName) { assert(mEngine->getMasterState() && "Invalid engine master state"); assert(mThreadState && "Invalid thread state"); if(luaL_loadfile(mThreadState,fileName) == 0) { _resumeScript(0); } else { _formatError(); } }
void Script::runFile(const int8 *fileName) { assert(mEngine->getMasterState() && "Invalid engine master state"); assert(mThreadState && "Invalid thread state"); if(luaL_loadfile(mThreadState,fileName) == 0) { _resumeScript(0); } else { _formatError(); gLogger->logMsgF("ScriptingEngine::Syntax Error: %s",MSG_NORMAL,mLastError); } }
void Script::run() { assert(mEngine->getMasterState() && "Invalid engine master state"); assert(mThreadState && "Invalid thread state"); if(luaL_loadfile(mThreadState,mFile) == 0) { _resumeScript(0); } else { _formatError(); gLogger->log(LogManager::EMERGENCY,"ScriptingEngine::Syntax Error: %s",mLastError); } }
void Script::_resumeScript(uint32 param) { mState = SS_Running; lua_pushnumber(mThreadState,param); int ret = lua_resume(mThreadState,1); switch(ret) { case 0: mState = SS_Not_Loaded; break; case LUA_YIELD: break; default: { _formatError(); gLogger->logMsgF("ScriptingEngine::_resumeScript Runtime Error: %s",MSG_NORMAL,mLastError); } break; } }
void Script::_resumeScript(uint32 param) { mState = SS_Running; lua_pushnumber(mThreadState,param); int ret = lua_resume(mThreadState,1); switch(ret) { case 0: mState = SS_Not_Loaded; break; case LUA_YIELD: break; default: { _formatError(); } break; } }
void Script::callFunction(const char *func,const char *sig,...) { va_list vl; int narg,nres; va_start(vl,sig); lua_getglobal(mThreadState,func); if(lua_isfunction(mThreadState,-1)) { narg = 0; while(*sig) { switch (*sig++) { case 'd': lua_pushnumber(mThreadState,va_arg(vl,double)); break; case 'i': lua_pushnumber(mThreadState,va_arg(vl,int)); break; case 's': lua_pushstring(mThreadState,va_arg(vl,char *)); break; case '>': goto endwhile; break; default: break; } narg++; luaL_checkstack(mThreadState,1,"too many arguments"); } endwhile: nres = strlen(sig); if(lua_pcall(mThreadState,narg,nres,0) != 0) { _formatError(); gLogger->logMsgF("ScriptingEngine::callFunction Runtime Error: %s",MSG_NORMAL,mLastError); } nres = -nres; while(*sig) { switch (*sig++) { case 'd': { if(!lua_isnumber(mThreadState,nres)) { _formatError(); gLogger->logMsgF("ScriptingEngine::callFunction wrong result type: %s",MSG_NORMAL,mLastError); } *va_arg(vl,double *) = lua_tonumber(mThreadState,nres); } break; case 'i': { if(!lua_isnumber(mThreadState,nres)) { _formatError(); gLogger->logMsgF("ScriptingEngine::callFunction wrong result type: %s",MSG_NORMAL,mLastError); } *va_arg(vl,int*) = (int)lua_tonumber(mThreadState,nres); } break; case 's': { if(!lua_isstring(mThreadState,nres)) { _formatError(); gLogger->logMsgF("ScriptingEngine::callFunction wrong result type: %s",MSG_NORMAL,mLastError); } *va_arg(vl,const char **) = lua_tostring(mThreadState,nres); } break; default: { _formatError(); gLogger->logMsgF("ScriptingEngine::callFunction invalid option: %s",MSG_NORMAL,mLastError); } break; } nres++; } } va_end(vl); }