Exemple #1
0
static void getfunc(lua_State* L, int opt)
{
	if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
	else
	{
		lua_Debug ar;
		int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
		luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
		if (lua_getstack(L, level, &ar) == 0)
			luaL_argerror(L, 1, "invalid level");
		lua_getinfo(L, "f", &ar);
		if (lua_isnil(L, -1))
			luaL_error(L, "no function environment for tail call at level %d",
				   level);
	}
}
Exemple #2
0
LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
  lua_Debug ar;
  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
    return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
  lua_getinfo(L, "n", &ar);
  if (strcmp(ar.namewhat, "method") == 0) {
    narg--;  /* do not count `self' */
    if (narg == 0)  /* error is in the self argument itself? */
      return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
                           ar.name, extramsg);
  }
  if (ar.name == NULL)
    ar.name = "?";
  return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
                        narg, ar.name, extramsg);
}
Exemple #3
0
LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
  lua_Debug ar;
  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
    return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
  lua_getinfo(L, "n", &ar);
  if (strcmp(ar.namewhat, "method") == 0) {
    arg--;  /* do not count 'self' */
    if (arg == 0)  /* error is in the self argument itself? */
      return luaL_error(L, "calling '%s' on bad self (%s)",
                           ar.name, extramsg);
  }
  if (ar.name == NULL)
    ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
  return luaL_error(L, "bad argument #%d to '%s' (%s)",
                        arg, ar.name, extramsg);
}
Exemple #4
0
static std::string luaFormatStackInfo(lua_State *L, int level = 1)
{
    lua_Debug ar;
    std::ostringstream os;
    if (lua_getstack(L, level, &ar) && lua_getinfo(L, "Sln", &ar))
    {
        os << ar.short_src << ":" << ar.currentline
            << " ([" << ar.what << "] "  << ar.namewhat << " " << (ar.name ? ar.name : "(?)") << ")";
    }
    else
    {
        os << "???:0";
    }

    return os.str();
}
Exemple #5
0
static void hookf (lua_State *L, lua_Debug *ar) {
  static const char *const hooknames[] =
    {"call", "return", "line", "count", "tail return"};
  lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  lua_rawget(L, LUA_REGISTRYINDEX);
  lua_pushlightuserdata(L, L);
  lua_rawget(L, -2);
  if (lua_isfunction(L, -1)) {
    lua_pushstring(L, hooknames[(int)ar->event]);
    if (ar->currentline >= 0)
      lua_pushinteger(L, ar->currentline);
    else lua_pushnil(L);
    lua_assert(lua_getinfo(L, "lS", ar));
    lua_call(L, 2, 0);
  }
}
Exemple #6
0
// Checks for a valid argument count
bool _Scripting::CheckArguments(lua_State *LuaObject, int Required) {
	int ArgumentCount = lua_gettop(LuaObject);

	// Check for arguments
	if(ArgumentCount != Required) {

		lua_Debug Record;
		lua_getstack(LuaObject, 0, &Record);
		lua_getinfo(LuaObject, "nl", &Record);

		Log.Write("Function %s requires %d arguments\n", Record.name, Required);
		return false;
	}

	return true;
}
Exemple #7
0
void Lua_Trace(lua_State *L, lua_Debug *debug_msg)
{
	char tmp[6]={0};
	char * what = tmp;
	strcpy(what,"nl\0\0");	
	switch(debug_msg->event)
	{
	case LUA_HOOKCALL:
		what = strcat(what,"uS");
		DEBUG_STROUT("LUA_HOOKCALL  ");
		break;
	case LUA_HOOKRET:
		what = strcat(what,"u");
		DEBUG_STROUT("LUA_HOOKRETURN  ");
		break;
	case LUA_HOOKTAILRET:
		what = strcat(what,"uS");
		DEBUG_STROUT("LUA_HOOKTAILRETURN  ");
		break;
	case LUA_HOOKLINE:
		what = strcat(what,"uS");
		DEBUG_STROUT("LUA_HOOKLINE  ");
		break;
	case LUA_HOOKCOUNT:
		break;
	default:
		break;
	}
	//printf("%s",what);
	if(debug_msg->currentline >0 )	
		printf("Current Run:Line %d,",debug_msg->currentline);

	if(lua_getinfo(L, what, debug_msg))
	{
		//printf("开始于%d行,结束于%d行,使用外部变量%d个", debug_msg->linedefined,debug_msg->lastlinedefined,debug_msg->nups);
		if(debug_msg->short_src != NULL) printf(debug_msg->short_src);
		printf("   ");
		if(debug_msg->what != NULL) printf(debug_msg->what);
		printf("   ");
		if(debug_msg->source != NULL) printf(debug_msg->source);
		printf("   ");
		if(debug_msg->name != NULL) printf(debug_msg->name);
		if(debug_msg->namewhat != NULL)printf(debug_msg->namewhat);
		printf("\n");
	}
	printf("\n");
}
Exemple #8
0
int CLuaInstFileHelpers::FileHelpersCp(lua_State *L)
{
	CLuaFileHelpers *D = FileHelpersCheckData(L, 1);
	if (!D) return 0;

	int numargs = lua_gettop(L) - 1;
	int min_numargs = 2;
	if (numargs < min_numargs) {
		printf("luascript cp: not enough arguments (%d, expected %d)\n", numargs, min_numargs);
		lua_pushboolean(L, false);
		return 1;
	}

	if (!lua_isstring(L, 2) || !lua_isstring(L, 3)) {
		printf("%s: argument 1 or 2 is not a string.\n",__func__);
		lua_pushboolean(L, false);
		return 1;
	}
	const char *from = luaL_checkstring(L, 2);
	const char *to = luaL_checkstring(L, 3);

	const char *flags = "";
	if (numargs > min_numargs){
		if (!lua_isstring(L, 4)) {
			printf("%s: argument 3 is not a string.\n",__func__);
			lua_pushboolean(L, false);
			return 1;
		}
		flags = luaL_checkstring(L, 4);
	}
	bool ret = false;
	CFileHelpers fh;
	fh.setConsoleQuiet(true);
	ret = fh.cp(from, to, flags);
	if (ret == false) {
		helpersDebugInfo di;
		fh.readDebugInfo(&di);
		lua_Debug ar;
		lua_getstack(L, 1, &ar);
		lua_getinfo(L, "Sl", &ar);
		printf(">>> Lua script error [%s:%d] %s\n    (error from neutrino: [%s:%d])\n",
		       ar.short_src, ar.currentline, di.msg.c_str(), di.file.c_str(), di.line);
	}

	lua_pushboolean(L, ret);
	return 1;
}
Exemple #9
0
int ModApiUtil::l_request_insecure_environment(lua_State *L)
{
	NO_MAP_LOCK_REQUIRED;

	// Just return _G if security is disabled
	if (!ScriptApiSecurity::isSecure(L)) {
		lua_getglobal(L, "_G");
		return 1;
	}

	// We have to make sure that this function is being called directly by
	// a mod, otherwise a malicious mod could override this function and
	// steal its return value.
	lua_Debug info;
	// Make sure there's only one item below this function on the stack...
	if (lua_getstack(L, 2, &info)) {
		return 0;
	}
	FATAL_ERROR_IF(!lua_getstack(L, 1, &info), "lua_getstack() failed");
	FATAL_ERROR_IF(!lua_getinfo(L, "S", &info), "lua_getinfo() failed");
	// ...and that that item is the main file scope.
	if (strcmp(info.what, "main") != 0) {
		return 0;
	}

	// Get mod name
	lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
	if (!lua_isstring(L, -1)) {
		return 0;
	}

	// Check secure.trusted_mods
	const char *mod_name = lua_tostring(L, -1);
	std::string trusted_mods = g_settings->get("secure.trusted_mods");
	trusted_mods.erase(std::remove_if(trusted_mods.begin(),
			trusted_mods.end(), static_cast<int(*)(int)>(&std::isspace)),
			trusted_mods.end());
	std::vector<std::string> mod_list = str_split(trusted_mods, ',');
	if (std::find(mod_list.begin(), mod_list.end(), mod_name) ==
			mod_list.end()) {
		return 0;
	}

	// Push insecure environment
	lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
	return 1;
}
Exemple #10
0
/*
	string tag
	string userstring
	thread co (default nil)
	integer level
 */
static int
ltrace(lua_State *L) {
	struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
	const char * tag = luaL_checkstring(L, 1);
	const char * user = luaL_checkstring(L, 2);
	if (lua_isthread(L, 3)) {
		lua_State * co = lua_tothread (L, 3);
		struct source_info si[MAX_LEVEL];
		lua_Debug d;
		int level = luaL_checkinteger(L, 4);
		int index = 0;
		do {
			if (!lua_getstack(co, level, &d))
				break;
			lua_getinfo(co, "Sl", &d);
			level++;
			si[index].source = d.source;
			si[index].line = d.currentline;
			if (d.currentline >= 0)
				++index;
		} while (index < MAX_LEVEL);
		switch (index) {
		case 1:
			skynet_error(context, "<TRACE %s> %" PRId64 " %s : %s:%d", tag, get_time(), user, si[0].source, si[0].line);
			break;
		case 2:
			skynet_error(context, "<TRACE %s> %" PRId64 " %s : %s:%d %s:%d", tag, get_time(), user, 
				si[0].source, si[0].line,
				si[1].source, si[1].line
				);
			break;
		case 3:
			skynet_error(context, "<TRACE %s> %" PRId64 " %s : %s:%d %s:%d %s:%d", tag, get_time(), user, 
				si[0].source, si[0].line,
				si[1].source, si[1].line,
				si[2].source, si[2].line
				);
			break;
		default:
			skynet_error(context, "<TRACE %s> %" PRId64 " %s", tag, get_time(), user);
			break;
		}
		return 0;
	}
	skynet_error(context, "<TRACE %s> %" PRId64 " %s", tag, get_time(), user);
	return 0;
}
Exemple #11
0
static int pushglobalfuncname (lua_State *L, lua_Debug *ar)
{
    int top = lua_gettop(L);
    lua_getinfo(L, "f", ar);  /* push function */
    lua_pushglobaltable(L);
    if (findfield(L, top + 1, 2))
    {
        lua_copy(L, -1, top + 1);  /* move name to proper place */
        lua_pop(L, 2);  /* remove pushed values */
        return 1;
    }
    else
    {
        lua_settop(L, top);  /* remove function and global table */
        return 0;
    }
}
Exemple #12
0
void luaPushError(lua_State *lua, char *error) {
    lua_Debug dbg;

    lua_newtable(lua);
    lua_pushstring(lua,"err");

    /* Attempt to figure out where this function was called, if possible */
    if(lua_getstack(lua, 1, &dbg) && lua_getinfo(lua, "nSl", &dbg)) {
        sds msg = sdscatprintf(sdsempty(), "%s: %d: %s",
            dbg.source, dbg.currentline, error);
        lua_pushstring(lua, msg);
        sdsfree(msg);
    } else {
        lua_pushstring(lua, error);
    }
    lua_settable(lua,-3);
}
Exemple #13
0
void line_hook(lua_State *L, lua_Debug *ar) {
    const char *file = NULL;
    struct ldb_context *lctx = NULL;
    lua_pushstring(L, REGISTRY_TAG);
    lua_gettable(L, LUA_REGISTRYINDEX);
    lctx = (struct ldb_context *)lua_touserdata(L, -1); 

    if (!lctx) {
        printf("line hook get ldb context failed!\n");
        return;
    }
    if (lua_getinfo(L, "nSl", ar) == 0) {
        printf("line hook,lua_getinfo failed\n");
        return;
    }

    //printf("ci:%p pre:%p next:%p currentline:%d\n", ar->i_ci, ar->i_ci->previous,ar->i_ci->next, ar->currentline);
    //struct cmd_node *last = get_last_cmd(lctx->cmd_history);
    if( ar->source[0] == '@' ) {
        file = ar->source + 1;
    }
    //check next command
    struct CallInfo *curr_ci = lctx->lprog->curr_ci;
    if(file && (lctx->cmd_mask & CMD_NEXT) &&
        (curr_ci == ar->i_ci || curr_ci == ar->i_ci->next)) { 
        //printf("currci:%p pre:%p next:%p \n", curr_ci, curr_ci->previous, curr_ci->next);
        debug_cmd_loop(lctx);
        return;
    }
    //check step command
    if(file && (lctx->cmd_mask & CMD_STEP)) {
        debug_cmd_loop(lctx);
        return;
    }
    //check if hit any break point
    if (file) {
        int bkt_index = find_breakpoint(lctx, file, ar->currentline);
        if (bkt_index != -1) {
            struct ldb_filebuffer *fb = lctx->bkt_list[bkt_index].filebuffer;
            printf("Breakpoint %d, at %s:%d\n", bkt_index, fb->abspath, lctx->bkt_list[bkt_index].line);
            lctx->lprog->ar = ar;

            debug_cmd_loop(lctx);
        }
    } 
}
Exemple #14
0
static int getinfo (lua_State *L) {
  lua_Debug ar;
  const char *options = luaL_optstring(L, 2, "flnSu");
  if (lua_isnumber(L, 1)) {
    if (!lua_getstack(L, (int)(lua_tonumber(L, 1)), &ar)) {
      lua_pushnil(L);  /* level out of range */
      return 1;
    }
  }
  else if (lua_isfunction(L, 1)) {
    lua_pushfstring(L, ">%s", options);
    options = lua_tostring(L, -1);
    lua_pushvalue(L, 1);
  }
  else
    return luaL_argerror(L, 1, "function or level expected");
  if (!lua_getinfo(L, options, &ar))
    return luaL_argerror(L, 2, "invalid option");
  lua_newtable(L);
  for (; *options; options++) {
    switch (*options) {
      case 'S':
        settabss(L, "source", ar.source);
        settabss(L, "short_src", ar.short_src);
        settabsi(L, "linedefined", ar.linedefined);
        settabss(L, "what", ar.what);
        break;
      case 'l':
        settabsi(L, "currentline", ar.currentline);
        break;
      case 'u':
        settabsi(L, "nups", ar.nups);
        break;
      case 'n':
        settabss(L, "name", ar.name);
        settabss(L, "namewhat", ar.namewhat);
        break;
      case 'f':
        lua_pushliteral(L, "func");
        lua_pushvalue(L, -3);
        lua_rawset(L, -3);
        break;
    }
  }
  return 1;  /* return table */
}
void CScriptDebugging::OutputDebugInfo ( lua_State* luaVM, int iLevel, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue )
{
    char szDebugDump[255];
    
    lua_Debug debugInfo;
    if ( lua_getstack ( luaVM, 1, &debugInfo ) )
    {
        lua_getinfo ( luaVM, "nlS", &debugInfo );
        
        // first version includes script path - makes much longer though
    //  snprintf ( szDebugDump, 255, "Line: %d (%s + %d) %s", debugInfo.currentline, debugInfo.name, debugInfo.currentline - debugInfo.linedefined, debugInfo.short_src );
        snprintf ( szDebugDump, 255, "Line: %d (%s + %d)", debugInfo.currentline, debugInfo.name, debugInfo.currentline - debugInfo.linedefined );
        szDebugDump[255-1] = '\0';

        LogString ( luaVM, szDebugDump, iLevel, ucRed, ucGreen, ucBlue );
    }
}
Exemple #16
0
String Util::where_am_i_lua( lua_State * L )
{
    std::ostringstream result;

    lua_Debug my_debug;

    if( lua_getstack( L, 1, &my_debug ) )
    {
        if( lua_getinfo( L, "Sln", &my_debug ) )
        {
            result << my_debug.source << ":" << my_debug.currentline;
            return result.str();
        }
    }

    return String("(unknown)");
}
Exemple #17
0
// typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
static void LuaHookCall (lua_State *lua)
{

   printf ("---- Call Stack ----\n");
//   printf ("[Level] [Function] [# args] [@line] [src]\n");

   lua_Debug ar;

   // Look at call stack
   for (int iLevel = 0; lua_getstack (lua, iLevel, &ar) != 0; ++iLevel)
   {
      if (lua_getinfo (lua, "Snlu", &ar) != 0)
      {
         printf ("%d %s %s %d @%d %s\n", iLevel, ar.namewhat, ar.name, ar.nups, ar.linedefined, ar.short_src);
      }
   }
}
Exemple #18
0
/*
** Calls 'lua_getinfo' and collects all results in a new table.
** L1 needs stack space for an optional input (function) plus
** two optional outputs (function and line table) from function
** 'lua_getinfo'.
*/
static int db_getinfo (lua_State *L) {
  lua_Debug ar;
  int arg;
  lua_State *L1 = getthread(L, &arg);
  const char *options = luaL_optstring(L, arg+2, "flnStu");
  checkstack(L, L1, 3);
  if (lua_isfunction(L, arg + 1)) {  /* info about a function? */
    options = lua_pushfstring(L, ">%s", options);  /* add '>' to 'options' */
    lua_pushvalue(L, arg + 1);  /* move function to 'L1' stack */
    lua_xmove(L, L1, 1);
  }
  else {  /* stack level */
    if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
      lua_pushnil(L);  /* level out of range */
      return 1;
    }
  }
  if (!lua_getinfo(L1, options, &ar))
    return luaL_argerror(L, arg+2, "invalid option");
  lua_newtable(L);  /* table to collect results */
  if (strchr(options, 'S')) {
    settabss(L, "source", ar.source);
    settabss(L, "short_src", ar.short_src);
    settabsi(L, "linedefined", ar.linedefined);
    settabsi(L, "lastlinedefined", ar.lastlinedefined);
    settabss(L, "what", ar.what);
  }
  if (strchr(options, 'l'))
    settabsi(L, "currentline", ar.currentline);
  if (strchr(options, 'u')) {
    settabsi(L, "nups", ar.nups);
    settabsi(L, "nparams", ar.nparams);
    settabsb(L, "isvararg", ar.isvararg);
  }
  if (strchr(options, 'n')) {
    settabss(L, "name", ar.name);
    settabss(L, "namewhat", ar.namewhat);
  }
  if (strchr(options, 't'))
    settabsb(L, "istailcall", ar.istailcall);
  if (strchr(options, 'L'))
    treatstackoption(L, L1, "activelines");
  if (strchr(options, 'f'))
    treatstackoption(L, L1, "func");
  return 1;  /* return table */
}
Exemple #19
0
int CLuaFile::CharacterPredictedInput(lua_State *L)
{
    lua_getglobal(L, "pLUA");
    CLuaFile *pSelf = (CLuaFile *)lua_touserdata(L, -1);
    lua_Debug Frame;
    lua_getstack(L, 1, &Frame);
    lua_getinfo(L, "nlSf", &Frame);

    if (lua_isnumber(L, 1))
    {
        if(lua_tointeger(L, 1) >= 0 && lua_tointeger(L, 1) < MAX_CLIENTS && pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)] && pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)]->GetCharacter())
        {
            pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)]->GetCharacter()->OnPredictedInput(pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)]->GetCharacter()->GetInput());
        }
    }
    return 0;
}
Exemple #20
0
LUALIB_API void luaL_where (lua_State *L, int level)
{
    lua_Debug ar;

    if ( lua_getstack(L, level, &ar) )
    {  /* check function at level */
        lua_getinfo(L, "Sl", &ar);  /* get info about it */

        if ( ar.currentline > 0 )
        {  /* is there info? */
            lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
            return;
        }
    }

    lua_pushliteral(L, "");  /* else, no information available... */
}
Exemple #21
0
//============================================================================
// int printMessage
//---------------------------------------------------------------------------
// Prints a message to the console
//
// Parameter   Dir      Description
// ---------   ---      -----------
// lua         IN       State variable
//
// Return
// ------
// Number of return varaibles on the stack
//
//============================================================================
static int printMessage (lua_State *lua)
{
   assert (lua_isstring (lua,1));

   const char *msg = lua_tostring (lua, 1);

   // get caller
   lua_Debug ar;
   memset (&ar, 0, sizeof(ar));
   lua_getstack (lua, 1, &ar);
   lua_getinfo (lua, "Snl", &ar);

   // debug output
   const char *str = ar.source;
   printf ("script: %s -- at %s(%d)\n", msg, str, ar.currentline);
   return 0;
}
Exemple #22
0
	int Script::handle_error(lua_State* L){
		lua_Debug d;
		lua_getstack(L, 1, &d);
		lua_getinfo(L, "Sln", &d);
		std::string err = lua_tostring(L, -1);
		lua_pop(L, 1);
		std::stringstream msg;
		msg << d.short_src << ":" << d.currentline;

		if (d.name != 0)
		{
			msg << "(" << d.namewhat << " " << d.name << ")";
		}
		msg << " " << err;
		lua_pushstring(L, msg.str().c_str());
		return 1;
	}
//------------------------------------------------------------------------------
void LuaSupport::DumpCallStack(lua_State* L)
{
	for(int i = 1; i < 10; ++i)
	{
		lua_Debug ar;
		if(lua_getstack(L, i, &ar))
		{
			lua_getinfo(L, "Sl", &ar);
			if(ar.currentline > 0)
			{
				FStringFixedBuffer(szTemp, 512);
				szTemp.Format("%s:%d\n", (ar.source)?ar.source:"", ar.currentline);
				OutputDebugString(szTemp.c_str());
			}
		}
	}
}
Exemple #24
0
int CLuaInstFileHelpers::FileHelpersMkdir(lua_State *L)
{
	CLuaFileHelpers *D = FileHelpersCheckData(L, 1);
	if (!D) return 0;

	int numargs = lua_gettop(L) - 1;
	int min_numargs = 1;
	if (numargs < min_numargs) {
		printf("luascript mkdir: not enough arguments (%d, expected %d)\n", numargs, min_numargs);
		lua_pushboolean(L, false);
		return 1;
	}
	if (!lua_isstring(L, 2)) {
		printf("%s: argument 1 is not a string.\n",__func__);
		lua_pushboolean(L, false);
		return 1;
	}
	const char *dir = luaL_checkstring(L, 2);

	mode_t mode = 0755;
	if (numargs > min_numargs) {
		int mode_i = luaL_checkint(L, 3);
		/* Hack for convert lua number to octal */
		std::string mode_s = itoa(mode_i, 10);
		mode = (mode_t)(strtol(mode_s.c_str(), (char **)NULL, 8) & 0x0FFF);
		//printf("\n##### [%s:%d] str: %s, okt: %o \n \n", __func__, __LINE__, mode_s.c_str(), (int)mode);
	}

	bool ret = false;
	CFileHelpers* fh = CFileHelpers::getInstance();
	fh->setConsoleQuiet(true);
	ret = fh->createDir(dir, mode);
	if (ret == false) {
		helpersDebugInfo di;
		fh->readDebugInfo(&di);
		lua_Debug ar;
		lua_getstack(L, 1, &ar);
		lua_getinfo(L, "Sl", &ar);
		printf(">>> Lua script error [%s:%d] %s\n    (error from neutrino: [%s:%d])\n",
		       ar.short_src, ar.currentline, di.msg.c_str(), di.file.c_str(), di.line);
	}

	lua_pushboolean(L, ret);
	return 1;
}
Exemple #25
0
void luaL_traceback (LuaThread *L, LuaThread *L1,
                                const char *msg, int level) {
  THREAD_CHECK(L);
  LuaDebug ar;
  int top = L->stack_.getTopIndex();
  int numlevels;
  {
    THREAD_CHANGE(L1);
    numlevels = countlevels(L1);
  }
  int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0;
  if (msg) {
    lua_pushfstring(L, "%s\n", msg);
  }
  lua_pushliteral(L, "stack traceback:");
  {
    THREAD_CHANGE(L1);
    while (lua_getstack(L1, level++, &ar)) {
      if (level == mark) {  /* too many levels? */
        {
          THREAD_CHANGE(L);
          lua_pushliteral(L, "\n\t...");  /* add a '...' */
        }
        level = numlevels - LEVELS2;  /* and skip to last ones */
      }
      else {
        lua_getinfo(L1, "Slnt", &ar);
        {
          THREAD_CHANGE(L);
          lua_pushfstring(L, "\n\t%s:", ar.short_src2.c_str());
          if (ar.currentline > 0) {
            lua_pushfstring(L, "%d:", ar.currentline);
          }
          lua_pushliteral(L, " in ");
          pushfuncname(L, &ar);
          if (ar.istailcall) {
            lua_pushliteral(L, "\n\t(...tail calls...)");
          }
          lua_concat(L, L->stack_.getTopIndex() - top);
        }
      }
    }
  }
  lua_concat(L, L->stack_.getTopIndex() - top);
}
Exemple #26
0
//LaserCreate(Pos.x, Pos.y, Dir.x, Dir.y, StartEnergy, Owner)
int CLuaFile::LaserCreate(lua_State *L)
{
	lua_getglobal(L, "pLUA");
	CLuaFile *pSelf = (CLuaFile *)lua_touserdata(L, -1);
	lua_Debug Frame;
	lua_getstack(L, 1, &Frame);
	lua_getinfo(L, "nlSf", &Frame);


	vec2 Pos;
	vec2 Dir;
	float StartEnergy = pSelf->m_pServer->Tuning()->m_LaserReach;
	int Owner;
	int Damage = -1;
	int MaxBounces = -1;
	int Delay = -1;
	int FakeEvalTick = -1;
	bool AutoDestroy = true;
	float DecreaseEnergyFactor = 1.0f;

	if(!lua_isnumber(L, 1) || !lua_isnumber(L, 2) || !lua_isnumber(L, 3) || !lua_isnumber(L, 4) || !lua_isnumber(L, 5))
		return 0;

	Pos = vec2(lua_tonumber(L, 1), lua_tonumber(L, 2));
	Dir = vec2(lua_tonumber(L, 3), lua_tonumber(L, 4));
	Owner = lua_tointeger(L, 5);
	if (lua_tointeger(L, 6))
        StartEnergy = lua_tonumber(L, 6);
	if (lua_isnumber(L, 7))
        Damage = lua_tointeger(L, 7);
	if (lua_isnumber(L, 8))
        MaxBounces = lua_tointeger(L, 8);
	if (lua_isnumber(L, 9))
        Delay = lua_tointeger(L, 9);
	if (lua_isnumber(L, 10))
        FakeEvalTick = lua_tointeger(L, 10);
	if (lua_isboolean(L, 11))
        AutoDestroy = lua_toboolean(L, 11);
	if (lua_isboolean(L, 12))
        DecreaseEnergyFactor = lua_tonumber(L, 12);

	CLaser *pTmp = new CLaser(&pSelf->m_pServer->m_World, Pos, Dir, StartEnergy, Owner, Damage, MaxBounces, Delay, FakeEvalTick, AutoDestroy, DecreaseEnergyFactor);
    lua_pushinteger(L, pTmp->GetID());
	return 1;
}
Exemple #27
0
int CLuaFile::CharacterIsGrounded(lua_State *L)
{
    lua_getglobal(L, "pLUA");
    CLuaFile *pSelf = (CLuaFile *)lua_touserdata(L, -1);
    lua_Debug Frame;
    lua_getstack(L, 1, &Frame);
    lua_getinfo(L, "nlSf", &Frame);

    if (lua_isnumber(L, 1))
    {
        if(lua_tointeger(L, 1) >= 0 && lua_tointeger(L, 1) < MAX_CLIENTS && pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)] && pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)]->GetCharacter())
        {
            lua_pushboolean(L, pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)]->GetCharacter()->IsGrounded());
            return 1;
        }
    }
    return 0;
}
Exemple #28
0
int CLuaFile::CharacterIncreaseArmor(lua_State *L)
{
    lua_getglobal(L, "pLUA");
    CLuaFile *pSelf = (CLuaFile *)lua_touserdata(L, -1);
    lua_Debug Frame;
    lua_getstack(L, 1, &Frame);
    lua_getinfo(L, "nlSf", &Frame);

    if (lua_isnumber(L, 1))
    {
        if(lua_tointeger(L, 1) >= 0 && lua_tointeger(L, 1) < MAX_CLIENTS && pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)] && pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)]->GetCharacter())
        {
            int Add = lua_isnumber(L, 2) ? lua_tointeger(L, 2) : 1;
            pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)]->GetCharacter()->IncreaseArmor(Add, lua_isnumber(L, 3) ? lua_tointeger(L, 3) : 10);
        }
    }
    return 0;
}
Exemple #29
0
int CLuaFile::SetCharacterPos(lua_State *L)
{
    lua_getglobal(L, "pLUA");
    CLuaFile *pSelf = (CLuaFile *)lua_touserdata(L, -1);
    lua_Debug Frame;
    lua_getstack(L, 1, &Frame);
    lua_getinfo(L, "nlSf", &Frame);

    if (lua_isnumber(L, 1) && lua_isnumber(L, 2) && lua_isnumber(L, 3))
    {
		if(lua_tointeger(L, 1) >= 0 && lua_tointeger(L, 1) < MAX_CLIENTS && pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)] && pSelf->m_pServer->m_apPlayers[lua_tointeger(L, 1)]->GetCharacter())
		{
			pSelf->m_pServer->m_World.m_Core.m_apCharacters[lua_tointeger(L, 1)]->m_Pos.x = lua_tonumber(L, 2);
			pSelf->m_pServer->m_World.m_Core.m_apCharacters[lua_tointeger(L, 1)]->m_Pos.y = lua_tonumber(L, 3);
		}
	}
	return 0;
}
Exemple #30
0
int luaL_argerror (LuaThread *L, int narg, const char *extramsg) {
  THREAD_CHECK(L);
  LuaDebug ar;
  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
    return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
  lua_getinfo(L, "n", &ar);
  if (strcmp(ar.namewhat2.c_str(), "method") == 0) {
    narg--;  /* do not count `self' */
    if (narg == 0)  /* error is in the self argument itself? */
      return luaL_error(L, "calling " LUA_QS " on bad self", ar.name2.c_str());
  }
  if (ar.name2.empty()) {
    LuaString* name = getglobalfuncname(L, &ar);
    ar.name2 = name ? name->c_str() : "?";
  }
  return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
                        narg, ar.name2.c_str(), extramsg);
}