static int ErrorCatcher(UIntPtr L) //could be moved into DebugEngine, but this would cost perfomance to fetch the correct delegate for each pcall { string errMsg = BBLua.lua_tostring(L, -1); BBLua.lua_settop(L, -2); LuaErrorCaught callback; if (stateErrHandler.TryGetValue(L, out callback)) { callback(errMsg); } return(0); }
protected void TryFakeVar(string varName, int n, List <FakeVar> memory) { BBLua.lua_getglobal(ls.L, varName); bool fakePossible = BBLua.lua_type(ls.L, -1) == LuaType.Nil; BBLua.lua_settop(ls.L, -2); //remove nil if (fakePossible) { memory.Add(new FakeVar(n, varName)); BBLua.lua_setglobal(ls.L, varName); } else { BBLua.lua_settop(ls.L, -2); //remove value } }
//returns false if game was busy public bool RestoreLoadedFiles() { bool wasRunning = this.DebugEngine.CurrentState == DebugState.Running; if (wasRunning) { if (!GameLoopHook.PauseGame()) { return(false); } } BBLua.lua_getglobal(this.L, "_LuaDebugger_FileData"); if (BBLua.lua_type(this.L, -1) != LuaType.Table) { if (wasRunning) { GameLoopHook.ResumeGame(); } return(true); } StringBuilder sb = new StringBuilder(); for (int i = 1; ; i++) { BBLua.lua_rawgeti(this.L, -1, i); if (BBLua.lua_type(this.L, -1) != LuaType.String) { BBLua.lua_settop(this.L, -2); break; } sb.Append(BBLua.lua_tostring(this.L, -1)); BBLua.lua_settop(this.L, -2); } if (wasRunning) { GameLoopHook.ResumeGame(); } this.LoadedFiles.Clear(); RestoreFromFileString(sb.ToString()); return(true); }
static LuaResult FakePcall(UIntPtr L, int nargs, int nresults, int errfunc) { if (!GlobalState.CatchErrors) { return(BBLua.lua_pcall(L, nargs, nresults, errfunc)); } BBLua.lua_pushcclosure(L, ErrorHook.errorHandlerPtr, 0); BBLua.lua_insert(L, -nargs - 2); LuaResult res; if (nresults >= 0) { res = BBLua.lua_pcall(L, nargs, nresults, -nargs - 2); } else //LUA_MULTRET { int stackBefore = BBLua.lua_gettop(L); res = BBLua.lua_pcall(L, nargs, nresults, -nargs - 2); int stackNow = BBLua.lua_gettop(L); if (res == LuaResult.OK) { nresults = stackNow - stackBefore + nargs + 1; } else { nresults = 0; } } if (res != LuaResult.OK) { BBLua.lua_settop(L, -2); //remove errormsg for (int i = 0; i < nresults; i++) //push dummy returns, hopefully settlers accepts this { BBLua.lua_pushnil(L); } } BBLua.lua_remove(L, -nresults - 1); //remove error handler return(LuaResult.OK); }
public void FakeG() { this.fakedLocals = new List <FakeVar>(); this.fakedUpvalues = new List <FakeVar>(); if (!this.CanFakeEnvironment) { return; } string varName; BBLua.lua_getinfo(ls.L, "f", this.funcInfo); // 'f': pushes func onto stack for (int i = 1; ; i++) { varName = BBLua.lua_getupvalue(ls.L, -1, i); if (varName == null) { break; } TryFakeVar(varName, i, this.fakedUpvalues); } BBLua.lua_settop(ls.L, -2); //remove func from stack for (int i = 1; ; i++) { varName = BBLua.lua_getlocal(ls.L, this.funcInfo, i); if (varName == null) { break; } if (varName.Length > 0 && varName[0] != '(') { TryFakeVar(varName, i, this.fakedLocals); } else { BBLua.lua_settop(ls.L, -2); //remove value } } }
public void UnFakeG() { if (!this.CanFakeEnvironment) { return; } BBLua.lua_getinfo(ls.L, "f", this.funcInfo); // 'f': pushes func onto stack foreach (FakeVar fv in this.fakedUpvalues) { GetVarAndCleanG(fv.VarName); string vn = BBLua.lua_setupvalue(ls.L, -2, fv.Number); } BBLua.lua_settop(ls.L, -2); //remove func foreach (FakeVar fv in this.fakedLocals) { GetVarAndCleanG(fv.VarName); string vn = BBLua.lua_setlocal(ls.L, this.funcInfo, fv.Number); } }
public string TosToString(bool popStack, bool noExpand, Dictionary <IntPtr, bool> printedTables) { LuaType type = BBLua.lua_type(this.L, -1); string result; switch (type) { case LuaType.Nil: result = "nil"; break; case LuaType.Boolean: result = BBLua.lua_toboolean(this.L, -1).ToString(); break; case LuaType.Number: result = BBLua.lua_tonumber(this.L, -1).ToString(); break; case LuaType.String: result = "\"" + BBLua.lua_tostring(this.L, -1) + "\""; break; case LuaType.Function: result = GetTosFunctionInfo(); break; case LuaType.Table: if (noExpand) { goto default; } IntPtr tblPtr = BBLua.lua_topointer(this.L, -1); if (printedTables.ContainsKey(tblPtr)) { result = "<Table, recursion>"; break; } result = "{"; BBLua.lua_pushnil(this.L); while (BBLua.lua_next(this.L, -2) != 0) { printedTables.Add(tblPtr, true); string val = IndentMultiLine(TosToString(true, false, printedTables)); string key = TosToString(false, true, printedTables); printedTables.Remove(tblPtr); if (key[0] == '\"') { string rawString = key.Substring(1, key.Length - 2); if (alphaNumeric.IsMatch(rawString)) { result += "\n " + rawString + " = " + val + ","; } else { result += "\n [" + key + "] = " + val + ","; } } else { result += "\n [" + key + "] = " + val + ","; } } result += "\n}"; break; case LuaType.UserData: case LuaType.LightUserData: IntPtr ptr = BBLua.lua_touserdata(this.L, -1); result = "<" + type.ToString() + ", at 0x" + ptr.ToInt32().ToString("X") + ">"; break; default: result = "<" + type.ToString() + ">"; break; } if (popStack) { BBLua.lua_settop(this.L, -2); } return(result); }