Lunatic: Centralize all bound-checking in bcheck.lua.

git-svn-id: https://svn.eduke32.com/eduke32@3445 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-02-01 13:04:56 +00:00
parent f95b6fddb5
commit 627c0625ae
5 changed files with 78 additions and 55 deletions

View file

@ -156,7 +156,8 @@ ifneq (0,$(LUNATIC))
$(OBJ)/luaJIT_BC_stat.$o \
$(OBJ)/luaJIT_BC_bitar.$o \
$(OBJ)/luaJIT_BC_control.$o \
$(OBJ)/luaJIT_BC_bcarray.$o
$(OBJ)/luaJIT_BC_bcarray.$o \
$(OBJ)/luaJIT_BC_bcheck.$o
# now, take care of having the necessary symbols (sector, wall, etc.) in the
# executable no matter what the debugging level

View file

@ -0,0 +1,56 @@
local ffiC = require("ffi").C
local bcheck = {}
--== ENGINE ==--
function bcheck.sector_idx(sectnum)
if (sectnum >= ffiC.numsectors+0ULL) then
error("invalid sector number "..sectnum, 3)
end
end
-- TODO: Provide another function that also checks whether the sprite exists in
-- the game world (statnum != MAXSTATUS).
function bcheck.sprite_idx(spritenum)
if (spritenum >= ffiC.MAXSPRITES+0ULL) then
error("invalid sprite number "..spritenum, 3)
end
end
function bcheck.tile_idx(tilenum)
if (tilenum >= ffiC.MAXTILES+0ULL) then
error("invalid tile number "..tilenum, 3)
end
end
--== GAME ==--
function bcheck.player_idx(snum)
if (snum >= ffiC.playerswhenstarted+0ULL) then
error("invalid player number "..snum, 3)
end
end
function bcheck.sound_idx(sndidx)
if (sndidx >= con_lang.MAXSOUNDS+0ULL) then
error("invalid sound number "..sndidx, 3)
end
end
function bcheck.weapon_idx(weap)
if (weap >= ffiC.MAX_WEAPONS+0ULL) then
error("Invalid weapon ID "..weap, 3)
end
end
function bcheck.inventory_idx(inv)
if (inv >= ffiC.GET_MAX+0ULL) then
error("Invalid inventory ID "..inv, 3)
end
end
return bcheck

View file

@ -1,5 +1,6 @@
-- Game control module for Lunatic.
local require = require
local ffi = require("ffi")
local ffiC = ffi.C
@ -130,18 +131,13 @@ end
---=== RUNTIME CON FUNCTIONS ===---
-- TODO: also check whether sprite exists in the game world (statnum != MAXSTATUS)
local function check_sprite_idx(i)
if (i >= ffiC.MAXSPRITES+0ULL) then
error("invalid argument: must be a valid sprite index", 3)
end
end
local bcheck = require("bcheck")
local check_sector_idx = bcheck.sector_idx
local check_tile_idx = bcheck.tile_idx
local check_sprite_idx = bcheck.sprite_idx
local check_player_idx = bcheck.player_idx
local check_sound_idx = bcheck.sound_idx
local function check_tile_idx(tilenum)
if (tilenum >= ffiC.MAXTILES+0ULL) then
error("invalid argument: must be a valid tile number", 3)
end
end
local function krandand(mask)
return bit.band(ffiC.krand(), mask)
@ -182,8 +178,10 @@ function insertsprite(tab_or_tilenum, ...)
if (type(sectnum)~="number" or type(tilenum) ~= "number") then
error("invalid insertsprite call: 'sectnum' and 'tilenum' must be numbers", 2)
end
check_tile_idx(tilenum)
dc.check_sector_idx(sectnum)
check_sector_idx(sectnum)
if (statnum >= ffiC.MAXSTATUS) then
error("invalid 'statnum' argument to insertsprite: must be a status number (0 .. MAXSTATUS-1)", 2)
end
@ -329,8 +327,7 @@ end
function _quote(pli, qnum)
check_quote_idx(qnum)
local p = player[pli] -- bound-check
check_player_idx(pli)
ffiC.P_DoQuote(qnum+MAXQUOTES, ffiC.g_player[pli].ps)
end
@ -978,13 +975,6 @@ function _checkrespawn(spr)
end
-- SOUNDS
local function check_sound_idx(sndidx)
if (sndidx >= con_lang.MAXSOUNDS+0ULL) then
error("invalid sound number "..sndidx, 3)
end
end
function _ianysound(aci)
check_sprite_idx(aci)
return (ffiC.A_CheckAnySoundPlaying(aci)~=0)

View file

@ -154,6 +154,11 @@ local ACTOR_STRUCT = [[
local bcarray = require("bcarray")
local bcheck = require("bcheck")
local check_sector_idx, check_tile_idx = bcheck.sector_idx, bcheck.tile_idx
local check_weapon_idx, check_inventory_idx = bcheck.weapon_idx, bcheck.inventory_idx
local check_sound_idx = bcheck.sound_idx
-- TODO: randomize member names
bcarray.new("int16_t", 64, "loogie", "int16_x_64")
bcarray.new("int16_t", ffiC.MAX_WEAPONS, "weapon", "int16_x_MAX_WEAPONS")
@ -757,32 +762,6 @@ local actor_mt = {
ffi.metatype("actor_t", actor_mt)
local function check_weapon_idx(weap)
if (weap >= ffiC.MAX_WEAPONS+0ULL) then
error("Invalid weapon ID "..weap, 3)
end
end
local function check_inventory_idx(inv)
if (inv >= ffiC.GET_MAX+0ULL) then
error("Invalid inventory ID "..inv, 3)
end
end
-- XXX: CODEDUP control.lua
local function check_sound_idx(sndidx)
if (sndidx >= con_lang.MAXSOUNDS+0ULL) then
error("invalid sound number "..sndidx, 3)
end
end
-- XXX: CODEDUP control.lua
local function check_tile_idx(tilenum)
if (tilenum >= ffiC.MAXTILES+0ULL) then
error("invalid argument: must be a valid tile number", 3)
end
end
--- PER-PLAYER WEAPON SETTINGS
local weapondata_mt = {
__newindex = function(wd, member, val)
@ -910,7 +889,7 @@ local camera_mt = {
__index = ffiC.g_camera,
__newindex = function(_, key, val)
if (key=="sect") then
defs_c.check_sector_idx(val)
check_sector_idx(val)
end
ffiC.g_camera[key] = val
end,

View file

@ -260,6 +260,9 @@ int32_t ksqrt(uint32_t num);
int32_t __fastcall getangle(int32_t xvect, int32_t yvect);
]]
local bcheck = require("bcheck")
local check_sector_idx = bcheck.sector_idx
local ivec3_
local ivec3_mt = {
-- '^' is the "translate upwards" operator
@ -399,12 +402,6 @@ function creategtab(ctab, maxidx, name)
return setmtonce(tab, tmpmt)
end
function check_sector_idx(sectnum)
if (sectnum >= ffiC.numsectors+0ULL) then
error("passed out-of-bounds sector number "..sectnum, 3)
end
end
local vars_to_ignore = {}
for varname,_ in pairs(getfenv(1)) do