// 0: success, <0: failure int L_CreateState(L_State *estate, const char *name, void (*StateSetupFunc)(lua_State *)) { lua_State *L; estate->name = Xstrdup(name); L = estate->L = luaL_newstate(); if (!estate->L) { DO_FREE_AND_NULL(estate->name); return -2; } luaL_openlibs(L); L_SetupDebugTraceback(L); if (StateSetupFunc) StateSetupFunc(L); if (lua_gettop(L)==0) L_PushDebugTraceback(L); // Otherwise, it is assumed that StateSetupFunc pushed a custom traceback // function onto the stack. Bassert(lua_gettop(L)==1); return 0; }
void L_PushDebugTraceback(lua_State *L) { // get debug.traceback lua_pushlightuserdata(L, &debug_traceback_key); lua_gettable(L, LUA_REGISTRYINDEX); Bassert(lua_isfunction(L, -1)); }
int L_HandleError(lua_State *L, int errcode, void (*ErrorPrintFunc)(const char *)) { if (errcode == LUA_ERRMEM) L_OutOfMemFunc(); if (errcode == LUA_ERRRUN || errcode == LUA_ERRERR) { if (lua_isboolean(L, -1)) { int32_t killit = lua_toboolean(L, -1); lua_pop(L, 1); return killit; } else { const char *errstr = (lua_type(L, -1)==LUA_TSTRING) ? lua_tostring(L, -1) : "??? (error message not a string)"; ErrorPrintFunc(errstr); if (L_ErrorFunc) L_ErrorFunc(errstr); lua_pop(L, 1); return -1; } } /* unreachable */ #ifndef NDEBUG Bassert(0); #endif return 0; }
static void L_SetupDebugTraceback(lua_State *L) { // get debug.traceback lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); Bassert(lua_isfunction(L, -1)); L_CheckAndRegisterFunction(L, &debug_traceback_key); lua_pop(L, 2); }
// Copy FN to WBUF and append an extension if it's not there, which is checked // case-insensitively. // Returns: 1 if not all characters could be written to WBUF, 0 else. int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char *ext) { const int32_t slen=Bstrlen(fn), extslen=Bstrlen(ext); const int32_t haveext = (slen>=extslen && Bstrcasecmp(&fn[slen-extslen], ext)==0); Bassert((intptr_t)wbuf != (intptr_t)fn); // no aliasing // If 'fn' has no extension suffixed, append one. return (Bsnprintf(wbuf, wbufsiz, "%s%s", fn, haveext ? "" : ext) >= wbufsiz); }
// size < 0: length of <buf> is determined using strlen() // size >= 0: size given, for loading of LuaJIT bytecode int L_RunString(L_State *estate, char *buf, int dofreebuf, int size, const char *name) { int32_t i; lua_State *L = estate->L; // -- lua -- Bassert(lua_gettop(L)==1); // on top: a traceback function Bassert(lua_iscfunction(L, 1)); if (size < 0) i = luaL_loadstring(L, buf); else i = luaL_loadbuffer(L, buf, size, name); Bassert(lua_gettop(L)==2); if (dofreebuf) Bfree(buf); if (i == LUA_ERRMEM) L_OutOfMemFunc(); if (i == LUA_ERRSYNTAX) { OSD_Printf(OSD_ERROR "state \"%s\" syntax error: %s\n", estate->name, lua_tostring(L, -1)); // get err msg lua_pop(L, 1); // pop errmsg return 3; } // call the lua chunk! i = lua_pcall(L, 0, 0, 1); Bassert(lua_gettop(L) == 1 + (i!=0)); if (i != 0) L_HandleError(L, i, &L_ErrorPrint); Bassert(lua_gettop(L)==1); return i ? 4 : 0; }
void G_DoCheats(void) { int32_t ch, i, j, k=0, weapon; static int32_t vol1inited=0; char consolecheat = 0; // JBF 20030914 if (osdcmd_cheatsinfo_stat.cheatnum != -1) { if (ud.player_skill == 4) { P_DoQuote(QUOTE_CHEATS_DISABLED, g_player[myconnectindex].ps); osdcmd_cheatsinfo_stat.cheatnum = -1; return; } // JBF 20030914 k = osdcmd_cheatsinfo_stat.cheatnum; osdcmd_cheatsinfo_stat.cheatnum = -1; consolecheat = 1; } if (VOLUMEONE && !vol1inited) { // change "scotty###" to "scotty##" uint32_t const warpend = Bstrlen(CheatStrings[2]); if (strcmp(&CheatStrings[2][warpend-3], "###") == 0) CheatStrings[2][warpend-1] = '\0'; Bstrcpy(CheatStrings[6], "<RESERVED>"); vol1inited = 1; } if (consolecheat && numplayers < 2 && ud.recstat == 0) goto FOUNDCHEAT; if (g_player[myconnectindex].ps->gm & (MODE_TYPE|MODE_MENU)) return; if (g_player[myconnectindex].ps->cheat_phase == 1) { while (KB_KeyWaiting()) { ch = Btolower(KB_GetCh()); if (!((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'))) { g_player[myconnectindex].ps->cheat_phase = 0; // P_DoQuote(QUOTE_46,g_player[myconnectindex].ps); return; } cheatbuf[cheatbuflen++] = (int8_t) ch; // This assertion is not obvious, but it should hold because of the // cheat string matching logic below. Bassert(cheatbuflen < (signed)sizeof(cheatbuf)); cheatbuf[cheatbuflen] = 0; // KB_ClearKeysDown(); for (k=0; k < NUMCHEATCODES; k++) { for (j = 0; j<cheatbuflen; j++) { if (cheatbuf[j] == CheatStrings[k][j] || (CheatStrings[k][j] == '#' && ch >= '0' && ch <= '9')) { if (CheatStrings[k][j+1] == 0) goto FOUNDCHEAT; if (j == cheatbuflen-1) return; } else break; } } g_player[myconnectindex].ps->cheat_phase = 0; return; FOUNDCHEAT: i = VM_OnEventWithReturn(EVENT_ACTIVATECHEAT, g_player[myconnectindex].ps->i, myconnectindex, k); if (k != CHEAT_COMEGETSOME) // Users are not allowed to interfere with TX's debugging cheat. k = i; { switch (k) { case CHEAT_WEAPONS: j = 0; if (VOLUMEONE) j = 6; for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS-j; weapon++) { P_AddAmmo(weapon, g_player[myconnectindex].ps, g_player[myconnectindex].ps->max_ammo_amount[weapon]); g_player[myconnectindex].ps->gotweapon |= (1<<weapon); } P_DoQuote(QUOTE_CHEAT_ALL_WEAPONS, g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_INVENTORY: G_CheatGetInv(); P_DoQuote(QUOTE_CHEAT_ALL_INV, g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_KEYS: g_player[myconnectindex].ps->got_access = 7; KB_FlushKeyboardQueue(); P_DoQuote(QUOTE_CHEAT_ALL_KEYS, g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_DEBUG: g_Debug = 1-g_Debug; G_DumpDebugInfo(); Bsprintf(tempbuf, "Gamevars dumped to log"); G_AddUserQuote(tempbuf); Bsprintf(tempbuf, "Map dumped to debug.map"); G_AddUserQuote(tempbuf); end_cheat(); break; case CHEAT_CLIP: ud.noclip = !ud.noclip; P_DoQuote(QUOTE_CHEAT_NOCLIP-!ud.noclip, g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_RESERVED2: g_player[myconnectindex].ps->player_par = 0; g_player[myconnectindex].ps->gm = MODE_EOL; end_cheat(); return; case CHEAT_ALLEN: P_DoQuote(QUOTE_CHEAT_ALLEN, g_player[myconnectindex].ps); g_player[myconnectindex].ps->cheat_phase = 0; KB_ClearKeyDown(sc_N); return; case CHEAT_CORNHOLIO: case CHEAT_KROZ: case CHEAT_COMEGETSOME: { const int32_t pi = g_player[myconnectindex].ps->i; ud.god = 1-ud.god; if (ud.god) { pus = 1; pub = 1; sprite[pi].cstat = 257; actor[pi].t_data[0] = 0; actor[pi].t_data[1] = 0; actor[pi].t_data[2] = 0; actor[pi].t_data[3] = 0; actor[pi].t_data[4] = 0; actor[pi].t_data[5] = 0; sprite[pi].hitag = 0; sprite[pi].lotag = 0; sprite[pi].pal = g_player[myconnectindex].ps->palookup; if (k != CHEAT_COMEGETSOME) { P_DoQuote(QUOTE_CHEAT_GODMODE_ON, g_player[myconnectindex].ps); } else { Bstrcpy(ScriptQuotes[QUOTE_RESERVED4], "Come Get Some!"); S_PlaySound(DUKE_GETWEAPON2); P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); G_CheatGetInv(); for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS; weapon++) g_player[myconnectindex].ps->gotweapon |= (1<<weapon); for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS; weapon++) P_AddAmmo(weapon, g_player[myconnectindex].ps, g_player[myconnectindex].ps->max_ammo_amount[weapon]); g_player[myconnectindex].ps->got_access = 7; } } else { sprite[pi].extra = g_player[myconnectindex].ps->max_player_health; actor[pi].extra = -1; g_player[myconnectindex].ps->last_extra = g_player[myconnectindex].ps->max_player_health; P_DoQuote(QUOTE_CHEAT_GODMODE_OFF, g_player[myconnectindex].ps); } sprite[pi].extra = g_player[myconnectindex].ps->max_player_health; actor[pi].extra = 0; if (k != CHEAT_COMEGETSOME) g_player[myconnectindex].ps->dead_flag = 0; end_cheat(); return; } case CHEAT_STUFF: j = 0; if (VOLUMEONE) j = 6; for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS-j; weapon++) g_player[myconnectindex].ps->gotweapon |= (1<<weapon); for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS-j; weapon++) P_AddAmmo(weapon, g_player[myconnectindex].ps, g_player[myconnectindex].ps->max_ammo_amount[weapon]); G_CheatGetInv(); g_player[myconnectindex].ps->got_access = 7; P_DoQuote(QUOTE_CHEAT_EVERYTHING, g_player[myconnectindex].ps); // P_DoQuote(QUOTE_21,g_player[myconnectindex].ps); g_player[myconnectindex].ps->inven_icon = ICON_FIRSTAID; end_cheat(); return; case CHEAT_SCOTTY: case CHEAT_SKILL: if (k == CHEAT_SCOTTY) { i = Bstrlen(CheatStrings[k])-3+VOLUMEONE; if (!consolecheat) { // JBF 20030914 int16_t volnume, levnume; if (VOLUMEALL) { volnume = cheatbuf[i] - '0'; levnume = (cheatbuf[i+1] - '0')*10+(cheatbuf[i+2]-'0'); } else { volnume = cheatbuf[i] - '0'; levnume = cheatbuf[i+1] - '0'; } volnume--; levnume--; if ((VOLUMEONE && volnume > 0) || volnume > g_numVolumes-1 || levnume >= MAXLEVELS || MapInfo[volnume *MAXLEVELS+levnume].filename == NULL) { end_cheat(); return; } ud.m_volume_number = ud.volume_number = volnume; ud.m_level_number = ud.level_number = levnume; } else { // JBF 20030914 ud.m_volume_number = ud.volume_number = osdcmd_cheatsinfo_stat.volume; ud.m_level_number = ud.level_number = osdcmd_cheatsinfo_stat.level; } } else { if (!consolecheat) { i = Bstrlen(CheatStrings[k])-1; ud.m_player_skill = ud.player_skill = cheatbuf[i] - '1'; } else { ud.m_player_skill = ud.player_skill = osdcmd_cheatsinfo_stat.volume; } } /*if (numplayers > 1 && g_netServer) Net_NewGame(ud.m_volume_number,ud.m_level_number); else*/ g_player[myconnectindex].ps->gm |= MODE_RESTART; end_cheat(); return; case CHEAT_COORDS: #ifdef USE_OPENGL if (++ud.coords >= 3) ud.coords = 0; #else if (++ud.coords >= 2) ud.coords = 0; #endif end_cheat(); return; case CHEAT_VIEW: if (g_player[myconnectindex].ps->over_shoulder_on) g_player[myconnectindex].ps->over_shoulder_on = 0; else { g_player[myconnectindex].ps->over_shoulder_on = 1; CAMERADIST = 0; CAMERACLOCK = totalclock; } // P_DoQuote(QUOTE_CHEATS_DISABLED,g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_TIME: // P_DoQuote(QUOTE_21,g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_UNLOCK: if (VOLUMEONE) return; for (i=numsectors-1; i>=0; i--) //Unlock { j = sector[i].lotag; if (j == -1 || j == 32767) continue; if ((j & 0x7fff) > 2) { if (j&(0xffff-16384)) sector[i].lotag &= (0xffff-16384); G_OperateSectors(i, g_player[myconnectindex].ps->i); } } G_OperateForceFields(g_player[myconnectindex].ps->i, -1); P_DoQuote(QUOTE_CHEAT_UNLOCK, g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_CASHMAN: ud.cashman = 1-ud.cashman; KB_ClearKeyDown(sc_N); g_player[myconnectindex].ps->cheat_phase = 0; return; case CHEAT_ITEMS: G_CheatGetInv(); g_player[myconnectindex].ps->got_access = 7; P_DoQuote(QUOTE_CHEAT_EVERYTHING, g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_SHOWMAP: // SHOW ALL OF THE MAP TOGGLE; ud.showallmap = !ud.showallmap; for (i=0; i<(MAXSECTORS>>3); i++) show2dsector[i] = ud.showallmap*255; P_DoQuote(ud.showallmap ? QUOTE_SHOW_MAP_ON : QUOTE_SHOW_MAP_OFF, g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_TODD: if (NAM) { Bstrcpy(ScriptQuotes[QUOTE_RESERVED4], g_NAMMattCheatQuote); P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); } else { P_DoQuote(QUOTE_CHEAT_TODD, g_player[myconnectindex].ps); } end_cheat(); return; case CHEAT_RATE: if (ud.tickrate++ > 2) ud.tickrate = 0; end_cheat(); return; case CHEAT_BETA: P_DoQuote(QUOTE_CHEAT_BETA, g_player[myconnectindex].ps); KB_ClearKeyDown(sc_H); end_cheat(); return; case CHEAT_HYPER: g_player[myconnectindex].ps->inv_amount[GET_STEROIDS] = 399; g_player[myconnectindex].ps->inv_amount[GET_HEATS] = 1200; P_DoQuote(QUOTE_CHEAT_STEROIDS, g_player[myconnectindex].ps); end_cheat(); return; case CHEAT_MONSTERS: { const char *s [] ={ "On", "Off", "On (2)" }; if (++g_noEnemies == 3) g_noEnemies = 0; Bsprintf(ScriptQuotes[QUOTE_RESERVED4], "Monsters: %s", s[g_noEnemies]); P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); end_cheat(); return; } case CHEAT_RESERVED: case CHEAT_RESERVED3: ud.eog = 1; g_player[myconnectindex].ps->player_par = 0; g_player[myconnectindex].ps->gm |= MODE_EOL; KB_FlushKeyboardQueue(); return; default: end_cheat(); return; } } } } else { if (KB_KeyPressed((uint8_t) CheatKeys[0]))