General improvements to Lua error messages for out-of-bounds stuff.

The idea is for the layman Lua user to understand better what range of values to use for mobj types, states, sfxs, player #s etc. Additionally, mobjinfo/states/sfxinfo/hudinfo tables all now have actual bound checks when accessing/editing them. Yikes, why didn't they have any before?!
This commit is contained in:
Monster Iestyn 2016-05-01 22:14:42 +01:00
parent a077be85cf
commit 2ddde83601
5 changed files with 65 additions and 29 deletions

View file

@ -295,8 +295,8 @@ static int lib_pSpawnMobj(lua_State *L)
fixed_t z = luaL_checkfixed(L, 3); fixed_t z = luaL_checkfixed(L, 3);
mobjtype_t type = luaL_checkinteger(L, 4); mobjtype_t type = luaL_checkinteger(L, 4);
NOHUD NOHUD
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
LUA_PushUserdata(L, P_SpawnMobj(x, y, z, type), META_MOBJ); LUA_PushUserdata(L, P_SpawnMobj(x, y, z, type), META_MOBJ);
return 1; return 1;
} }
@ -321,8 +321,8 @@ static int lib_pSpawnMissile(lua_State *L)
NOHUD NOHUD
if (!source || !dest) if (!source || !dest)
return LUA_ErrInvalid(L, "mobj_t"); return LUA_ErrInvalid(L, "mobj_t");
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
LUA_PushUserdata(L, P_SpawnMissile(source, dest, type), META_MOBJ); LUA_PushUserdata(L, P_SpawnMissile(source, dest, type), META_MOBJ);
return 1; return 1;
} }
@ -338,8 +338,8 @@ static int lib_pSpawnXYZMissile(lua_State *L)
NOHUD NOHUD
if (!source || !dest) if (!source || !dest)
return LUA_ErrInvalid(L, "mobj_t"); return LUA_ErrInvalid(L, "mobj_t");
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
LUA_PushUserdata(L, P_SpawnXYZMissile(source, dest, type, x, y, z), META_MOBJ); LUA_PushUserdata(L, P_SpawnXYZMissile(source, dest, type, x, y, z), META_MOBJ);
return 1; return 1;
} }
@ -357,8 +357,8 @@ static int lib_pSpawnPointMissile(lua_State *L)
NOHUD NOHUD
if (!source) if (!source)
return LUA_ErrInvalid(L, "mobj_t"); return LUA_ErrInvalid(L, "mobj_t");
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
LUA_PushUserdata(L, P_SpawnPointMissile(source, xa, ya, za, type, x, y, z), META_MOBJ); LUA_PushUserdata(L, P_SpawnPointMissile(source, xa, ya, za, type, x, y, z), META_MOBJ);
return 1; return 1;
} }
@ -374,8 +374,8 @@ static int lib_pSpawnAlteredDirectionMissile(lua_State *L)
NOHUD NOHUD
if (!source) if (!source)
return LUA_ErrInvalid(L, "mobj_t"); return LUA_ErrInvalid(L, "mobj_t");
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
LUA_PushUserdata(L, P_SpawnAlteredDirectionMissile(source, type, x, y, z, shiftingAngle), META_MOBJ); LUA_PushUserdata(L, P_SpawnAlteredDirectionMissile(source, type, x, y, z, shiftingAngle), META_MOBJ);
return 1; return 1;
} }
@ -403,8 +403,8 @@ static int lib_pSPMAngle(lua_State *L)
NOHUD NOHUD
if (!source) if (!source)
return LUA_ErrInvalid(L, "mobj_t"); return LUA_ErrInvalid(L, "mobj_t");
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
LUA_PushUserdata(L, P_SPMAngle(source, type, angle, allowaim, flags2), META_MOBJ); LUA_PushUserdata(L, P_SPMAngle(source, type, angle, allowaim, flags2), META_MOBJ);
return 1; return 1;
} }
@ -417,8 +417,8 @@ static int lib_pSpawnPlayerMissile(lua_State *L)
NOHUD NOHUD
if (!source) if (!source)
return LUA_ErrInvalid(L, "mobj_t"); return LUA_ErrInvalid(L, "mobj_t");
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
LUA_PushUserdata(L, P_SpawnPlayerMissile(source, type, flags2), META_MOBJ); LUA_PushUserdata(L, P_SpawnPlayerMissile(source, type, flags2), META_MOBJ);
return 1; return 1;
} }
@ -437,8 +437,8 @@ static int lib_pWeaponOrPanel(lua_State *L)
{ {
mobjtype_t type = luaL_checkinteger(L, 1); mobjtype_t type = luaL_checkinteger(L, 1);
//HUDSAFE //HUDSAFE
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
lua_pushboolean(L, P_WeaponOrPanel(type)); lua_pushboolean(L, P_WeaponOrPanel(type));
return 1; return 1;
} }
@ -477,8 +477,10 @@ static int lib_pSpawnParaloop(lua_State *L)
statenum_t nstate = luaL_optinteger(L, 8, S_NULL); statenum_t nstate = luaL_optinteger(L, 8, S_NULL);
boolean spawncenter = lua_optboolean(L, 9); boolean spawncenter = lua_optboolean(L, 9);
NOHUD NOHUD
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
if (nstate >= NUMSTATES)
return luaL_error(L, "state %d out of range (0 - %d)", nstate, NUMSTATES-1);
P_SpawnParaloop(x, y, z, radius, number, type, nstate, rotangle, spawncenter); P_SpawnParaloop(x, y, z, radius, number, type, nstate, rotangle, spawncenter);
return 0; return 0;
} }
@ -908,8 +910,8 @@ static int lib_pSpawnSpinMobj(lua_State *L)
NOHUD NOHUD
if (!player) if (!player)
return LUA_ErrInvalid(L, "player_t"); return LUA_ErrInvalid(L, "player_t");
if (type > MT_LASTFREESLOT) if (type >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
P_SpawnSpinMobj(player, type); P_SpawnSpinMobj(player, type);
return 0; return 0;
} }
@ -1274,6 +1276,8 @@ static int lib_pSetMobjStateNF(lua_State *L)
NOHUD NOHUD
if (!mobj) if (!mobj)
return LUA_ErrInvalid(L, "mobj_t"); return LUA_ErrInvalid(L, "mobj_t");
if (state >= NUMSTATES)
return luaL_error(L, "state %d out of range (0 - %d)", state, NUMSTATES-1);
if (mobj->player && state == S_NULL) if (mobj->player && state == S_NULL)
return luaL_error(L, "Attempt to remove player mobj with S_NULL."); return luaL_error(L, "Attempt to remove player mobj with S_NULL.");
lua_pushboolean(L, P_SetMobjStateNF(mobj, state)); lua_pushboolean(L, P_SetMobjStateNF(mobj, state));
@ -1385,8 +1389,8 @@ static int lib_pIsFlagAtBase(lua_State *L)
{ {
mobjtype_t flag = luaL_checkinteger(L, 1); mobjtype_t flag = luaL_checkinteger(L, 1);
NOHUD NOHUD
if (flag > MT_LASTFREESLOT) if (flag >= NUMMOBJTYPES)
return luaL_error(L, "mobjtype_t out of bounds error!"); return luaL_error(L, "mobj type %d out of range (0 - %d)", flag, NUMMOBJTYPES-1);
lua_pushboolean(L, P_IsFlagAtBase(flag)); lua_pushboolean(L, P_IsFlagAtBase(flag));
return 1; return 1;
} }
@ -1619,7 +1623,7 @@ static int lib_rSetPlayerSkin(lua_State *L)
{ {
INT32 i = luaL_checkinteger(L, 2); INT32 i = luaL_checkinteger(L, 2);
if (i < 0 || i >= MAXSKINS) if (i < 0 || i >= MAXSKINS)
return luaL_error(L, "argument #2 cannot exceed MAXSKINS"); return luaL_error(L, "skin number (argument #2) %d out of range (0 - %d)", i, MAXSKINS-1);
SetPlayerSkinByNum(player-players, i); SetPlayerSkinByNum(player-players, i);
} }
else // skin name else // skin name
@ -1639,6 +1643,8 @@ static int lib_sStartSound(lua_State *L)
sfxenum_t sound_id = luaL_checkinteger(L, 2); sfxenum_t sound_id = luaL_checkinteger(L, 2);
player_t *player = NULL; player_t *player = NULL;
NOHUD NOHUD
if (sound_id >= NUMSFX)
return luaL_error(L, "sfx %d out of range (0 - %d)", sound_id, NUMSFX-1);
if (!lua_isnil(L, 1)) if (!lua_isnil(L, 1))
{ {
origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ)); origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
@ -1663,12 +1669,15 @@ static int lib_sStartSoundAtVolume(lua_State *L)
INT32 volume = (INT32)luaL_checkinteger(L, 3); INT32 volume = (INT32)luaL_checkinteger(L, 3);
player_t *player = NULL; player_t *player = NULL;
NOHUD NOHUD
if (!lua_isnil(L, 1)) if (!lua_isnil(L, 1))
{ {
origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ)); origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
if (!origin) if (!origin)
return LUA_ErrInvalid(L, "mobj_t"); return LUA_ErrInvalid(L, "mobj_t");
} }
if (sound_id >= NUMSFX)
return luaL_error(L, "sfx %d out of range (0 - %d)", sound_id, NUMSFX-1);
if (!lua_isnone(L, 4) && lua_isuserdata(L, 4)) if (!lua_isnone(L, 4) && lua_isuserdata(L, 4))
{ {
player = *((player_t **)luaL_checkudata(L, 4, META_PLAYER)); player = *((player_t **)luaL_checkudata(L, 4, META_PLAYER));
@ -1800,6 +1809,8 @@ static int lib_sIdPlaying(lua_State *L)
{ {
sfxenum_t id = luaL_checkinteger(L, 1); sfxenum_t id = luaL_checkinteger(L, 1);
NOHUD NOHUD
if (id >= NUMSFX)
return luaL_error(L, "sfx %d out of range (0 - %d)", id, NUMSFX-1);
lua_pushboolean(L, S_IdPlaying(id)); lua_pushboolean(L, S_IdPlaying(id));
return 1; return 1;
} }
@ -1811,6 +1822,8 @@ static int lib_sSoundPlaying(lua_State *L)
NOHUD NOHUD
if (!origin) if (!origin)
return LUA_ErrInvalid(L, "mobj_t"); return LUA_ErrInvalid(L, "mobj_t");
if (id >= NUMSFX)
return luaL_error(L, "sfx %d out of range (0 - %d)", id, NUMSFX-1);
lua_pushboolean(L, S_SoundPlaying(origin, id)); lua_pushboolean(L, S_SoundPlaying(origin, id));
return 1; return 1;
} }
@ -1831,7 +1844,7 @@ static int lib_gDoReborn(lua_State *L)
INT32 playernum = luaL_checkinteger(L, 1); INT32 playernum = luaL_checkinteger(L, 1);
NOHUD NOHUD
if (playernum >= MAXPLAYERS) if (playernum >= MAXPLAYERS)
return luaL_error(L, "playernum out of bounds error!"); return luaL_error(L, "playernum %d out of range (0 - %d)", playernum, MAXPLAYERS-1);
G_DoReborn(playernum); G_DoReborn(playernum);
return 0; return 0;
} }

View file

@ -166,6 +166,8 @@ static int lib_getHudInfo(lua_State *L)
lua_remove(L, 1); lua_remove(L, 1);
i = luaL_checkinteger(L, 1); i = luaL_checkinteger(L, 1);
if (i >= NUMHUDITEMS)
return luaL_error(L, "hudinfo[] index %d out of range (0 - %d)", i, NUMHUDITEMS-1);
LUA_PushUserdata(L, &hudinfo[i], META_HUDINFO); LUA_PushUserdata(L, &hudinfo[i], META_HUDINFO);
return 1; return 1;
} }

View file

@ -146,6 +146,8 @@ static int lib_getState(lua_State *L)
lua_remove(L, 1); lua_remove(L, 1);
i = luaL_checkinteger(L, 1); i = luaL_checkinteger(L, 1);
if (i >= NUMSTATES)
return luaL_error(L, "states[] index %d out of range (0 - %d)", i, NUMSTATES-1);
LUA_PushUserdata(L, &states[i], META_STATE); LUA_PushUserdata(L, &states[i], META_STATE);
return 1; return 1;
} }
@ -155,7 +157,12 @@ static int lib_setState(lua_State *L)
{ {
state_t *state; state_t *state;
lua_remove(L, 1); // don't care about states[] userdata. lua_remove(L, 1); // don't care about states[] userdata.
state = &states[luaL_checkinteger(L, 1)]; // get the state to assign to. {
UINT32 i = luaL_checkinteger(L, 1);
if (i >= NUMSTATES)
return luaL_error(L, "states[] index %d out of range (0 - %d)", i, NUMSTATES-1);
state = &states[i]; // get the state to assign to.
}
luaL_checktype(L, 2, LUA_TTABLE); // check that we've been passed a table. luaL_checktype(L, 2, LUA_TTABLE); // check that we've been passed a table.
lua_remove(L, 1); // pop state num, don't need it any more. lua_remove(L, 1); // pop state num, don't need it any more.
lua_settop(L, 1); // cut the stack here. the only thing left now is the table of data we're assigning to the state. lua_settop(L, 1); // cut the stack here. the only thing left now is the table of data we're assigning to the state.
@ -436,6 +443,8 @@ static int lib_getMobjInfo(lua_State *L)
lua_remove(L, 1); lua_remove(L, 1);
i = luaL_checkinteger(L, 1); i = luaL_checkinteger(L, 1);
if (i >= NUMMOBJTYPES)
return luaL_error(L, "mobjinfo[] index %d out of range (0 - %d)", i, NUMMOBJTYPES-1);
LUA_PushUserdata(L, &mobjinfo[i], META_MOBJINFO); LUA_PushUserdata(L, &mobjinfo[i], META_MOBJINFO);
return 1; return 1;
} }
@ -445,7 +454,12 @@ static int lib_setMobjInfo(lua_State *L)
{ {
mobjinfo_t *info; mobjinfo_t *info;
lua_remove(L, 1); // don't care about mobjinfo[] userdata. lua_remove(L, 1); // don't care about mobjinfo[] userdata.
info = &mobjinfo[luaL_checkinteger(L, 1)]; // get the mobjinfo to assign to. {
UINT32 i = luaL_checkinteger(L, 1);
if (i >= NUMMOBJTYPES)
return luaL_error(L, "mobjinfo[] index %d out of range (0 - %d)", i, NUMMOBJTYPES-1);
info = &mobjinfo[i]; // get the mobjinfo to assign to.
}
luaL_checktype(L, 2, LUA_TTABLE); // check that we've been passed a table. luaL_checktype(L, 2, LUA_TTABLE); // check that we've been passed a table.
lua_remove(L, 1); // pop mobjtype num, don't need it any more. lua_remove(L, 1); // pop mobjtype num, don't need it any more.
lua_settop(L, 1); // cut the stack here. the only thing left now is the table of data we're assigning to the mobjinfo. lua_settop(L, 1); // cut the stack here. the only thing left now is the table of data we're assigning to the mobjinfo.
@ -717,6 +731,8 @@ static int lib_getSfxInfo(lua_State *L)
lua_remove(L, 1); lua_remove(L, 1);
i = luaL_checkinteger(L, 1); i = luaL_checkinteger(L, 1);
if (i >= NUMSFX)
return luaL_error(L, "sfxinfo[] index %d out of range (0 - %d)", i, NUMSFX-1);
LUA_PushUserdata(L, &S_sfx[i], META_SFXINFO); LUA_PushUserdata(L, &S_sfx[i], META_SFXINFO);
return 1; return 1;
} }
@ -727,7 +743,12 @@ static int lib_setSfxInfo(lua_State *L)
sfxinfo_t *info; sfxinfo_t *info;
lua_remove(L, 1); lua_remove(L, 1);
info = &S_sfx[luaL_checkinteger(L, 1)]; // get the mobjinfo to assign to. {
UINT32 i = luaL_checkinteger(L, 1);
if (i >= NUMSFX)
return luaL_error(L, "sfxinfo[] index %d out of range (0 - %d)", i, NUMSFX-1);
info = &S_sfx[i]; // get the mobjinfo to assign to.
}
luaL_checktype(L, 2, LUA_TTABLE); // check that we've been passed a table. luaL_checktype(L, 2, LUA_TTABLE); // check that we've been passed a table.
lua_remove(L, 1); // pop mobjtype num, don't need it any more. lua_remove(L, 1); // pop mobjtype num, don't need it any more.
lua_settop(L, 1); // cut the stack here. the only thing left now is the table of data we're assigning to the mobjinfo. lua_settop(L, 1); // cut the stack here. the only thing left now is the table of data we're assigning to the mobjinfo.

View file

@ -55,7 +55,7 @@ static int lib_getPlayer(lua_State *L)
{ {
lua_Integer i = luaL_checkinteger(L, 2); lua_Integer i = luaL_checkinteger(L, 2);
if (i < 0 || i >= MAXPLAYERS) if (i < 0 || i >= MAXPLAYERS)
return luaL_error(L, "players[] index cannot exceed MAXPLAYERS"); return luaL_error(L, "players[] index %d out of range (0 - %d)", i, MAXPLAYERS-1);
if (!playeringame[i]) if (!playeringame[i])
return 0; return 0;
if (!players[i].mo) if (!players[i].mo)

View file

@ -244,7 +244,7 @@ static int lib_getSkin(lua_State *L)
{ {
i = luaL_checkinteger(L, 2); i = luaL_checkinteger(L, 2);
if (i < 0 || i >= MAXSKINS) if (i < 0 || i >= MAXSKINS)
return luaL_error(L, "skins[] index cannot exceed MAXSKINS"); return luaL_error(L, "skins[] index %d out of range (0 - %d)", i, MAXSKINS-1);
if (i >= numskins) if (i >= numskins)
return 0; return 0;
LUA_PushUserdata(L, &skins[i], META_SKIN); LUA_PushUserdata(L, &skins[i], META_SKIN);