util/build.lua: lose packing, use fread(), make bound-check nan-safe.

git-svn-id: https://svn.eduke32.com/eduke32@4046 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-09-05 17:37:40 +00:00
parent 7543ba7226
commit 3b69417792

View file

@ -18,7 +18,6 @@ local tonumber = tonumber
module(...) module(...)
ffi.cdef[[ ffi.cdef[[
#pragma pack(push,1)
typedef struct typedef struct
{ {
int16_t wallptr, wallnum; int16_t wallptr, wallnum;
@ -58,7 +57,10 @@ typedef struct
int16_t ang, owner, xvel, yvel, zvel; int16_t ang, owner, xvel, yvel, zvel;
int16_t lotag, hitag, extra; int16_t lotag, hitag, extra;
} spritetype; } spritetype;
#pragma pack(pop) ]]
ffi.cdef[[
size_t fread(void *ptr, size_t size, size_t nmemb, void *stream);
]] ]]
local C = ffi.C local C = ffi.C
@ -74,30 +76,25 @@ MAX =
} }
local function doread(fh, basectype, numelts) local function doread(fh, basectype, numelts)
local cd = ffi.new(basectype.."[?]", numelts)
local size = ffi.sizeof(cd)
if (numelts==0) then if (numelts==0) then
return nil return nil
end end
assert(size % numelts == 0) local cd = ffi.new(basectype.."[?]", numelts)
local datstr = fh:read(size) local size = ffi.sizeof(basectype)*numelts
if (datstr == nil or #datstr < size) then if (C.fread(cd, ffi.sizeof(basectype), numelts, fh) ~= numelts) then
fh:close() fh:close()
return nil return nil
end end
ffi.copy(cd, datstr, size)
return cd return cd
end end
local function set_secwalspr_mt(structar, maxidx) local function set_secwalspr_mt(structar, maxidx)
local mt = { local mt = {
__index = function(tab, idx) __index = function(tab, idx)
if (idx < 0 or idx >= maxidx) then if (not (idx >= 0 and idx < maxidx)) then
error("Invalid structure array read access", 2) error("Invalid structure array read access", 2)
end end
return structar[idx] return structar[idx]
@ -281,9 +278,11 @@ function loadboard(filename, do_canonicalize_sprite)
return nil, "Invalid number of sprites" return nil, "Invalid number of sprites"
end end
map.sprite = doread(fh, "spritetype", map.numsprites) if (map.numsprites ~= 0) then
if (map.numsprites~=0 and map.sprite == nil) then map.sprite = doread(fh, "spritetype", map.numsprites)
return nil, "Couldn't read sprites" if (map.sprite == nil) then
return nil, "Couldn't read sprites"
end
end end
fh:close() fh:close()
@ -306,14 +305,14 @@ end
local function set_sizarray_mt(sizar) local function set_sizarray_mt(sizar)
local mt = { local mt = {
__index = function(tab, idx) __index = function(tab, idx)
if (idx < 0 or idx >= MAX.TILES) then if (not (idx >= 0 and idx < MAX.TILES)) then
error("Invalid tile size array read access", 2) error("Invalid tile size array read access", 2)
end end
return sizar[idx] return sizar[idx]
end, end,
__newindex = function(tab, idx, newval) __newindex = function(tab, idx, newval)
if (idx < 0 or idx >= MAX.TILES) then if (not (idx >= 0 and idx < MAX.TILES)) then
error("Invalid tile size array write access", 2) error("Invalid tile size array write access", 2)
end end
sizar[idx] = newval sizar[idx] = newval