mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 07:13:45 +01:00
Add & use binary.(read|write)_int
This commit is contained in:
parent
7448a50765
commit
9d28fb24d4
9
b3d.lua
9
b3d.lua
@ -1,6 +1,8 @@
|
||||
-- Localize globals
|
||||
local assert, error, math, modlib, next, pairs, setmetatable, table = assert, error, math, modlib, next, pairs, setmetatable, table
|
||||
|
||||
local read_int, read_single = modlib.binary.read_int, modlib.binary.read_single
|
||||
|
||||
-- Set environment
|
||||
local _ENV = {}
|
||||
setfenv(1, _ENV)
|
||||
@ -21,11 +23,7 @@ function read(stream)
|
||||
end
|
||||
|
||||
local function int()
|
||||
local value = byte() + byte() * 0x100 + byte() * 0x10000 + byte() * 0x1000000
|
||||
if value >= 2^31 then
|
||||
return value - 2^32
|
||||
end
|
||||
return value
|
||||
return read_int(byte, 4)
|
||||
end
|
||||
|
||||
local function id()
|
||||
@ -52,7 +50,6 @@ function read(stream)
|
||||
end
|
||||
end
|
||||
|
||||
local read_single = modlib.binary.read_single
|
||||
local function float()
|
||||
return read_single(byte)
|
||||
end
|
||||
|
20
binary.lua
20
binary.lua
@ -66,6 +66,15 @@ function read_uint(read_byte, bytes)
|
||||
return uint
|
||||
end
|
||||
|
||||
function read_int(read_byte, bytes)
|
||||
local uint = read_uint(read_byte, bytes)
|
||||
local max = 0x100 ^ bytes
|
||||
if uint >= max / 2 then
|
||||
return uint - max
|
||||
end
|
||||
return uint
|
||||
end
|
||||
|
||||
function write_uint(write_byte, uint, bytes)
|
||||
for _ = 1, bytes do
|
||||
write_byte(uint % 0x100)
|
||||
@ -74,6 +83,17 @@ function write_uint(write_byte, uint, bytes)
|
||||
assert(uint == 0)
|
||||
end
|
||||
|
||||
function write_int(write_byte, int, bytes)
|
||||
local max = 0x100 ^ bytes / 2
|
||||
if int < 0 then
|
||||
-- No bound checking is needed: If the int is too small, the uint will be too big
|
||||
int = max - int
|
||||
else
|
||||
assert(int < max)
|
||||
end
|
||||
return write_uint(write_byte, int, bytes)
|
||||
end
|
||||
|
||||
--: on_write function(double)
|
||||
--: double set to true to force f64, false for f32, nil for auto
|
||||
function write_float(write_byte, number, on_write, double)
|
||||
|
Loading…
Reference in New Issue
Block a user