-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReader.lua
More file actions
103 lines (85 loc) · 2.4 KB
/
Reader.lua
File metadata and controls
103 lines (85 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- Reader
local FLOAT_PRECISION = 24
local Reader = {}
function Reader.new(bytecode)
local stream = buffer.fromstring(bytecode)
local cursor = 0
local self = {}
local string_char = string.char
local format = string.format
local tonumber = tonumber
local bor = bit32.bor
local band = bit32.band
local lshift = bit32.lshift
local btest = bit32.btest
local insert = table.insert
function self:len()
return buffer.len(stream)
end
function self:nextByte()
local result = buffer.readu8(stream, cursor)
cursor = cursor + 1
return result
end
function self:nextSignedByte()
local result = buffer.readi8(stream, cursor)
cursor = cursor + 1
return result
end
function self:nextBytes(count)
local result = table.create(count)
for i = 1, count do
result[i] = self:nextByte()
end
return result
end
function self:nextChar()
return string_char(self:nextByte())
end
function self:nextUInt32()
local result = buffer.readu32(stream, cursor)
cursor = cursor + 4
return result
end
function self:nextInt32()
local result = buffer.readi32(stream, cursor)
cursor = cursor + 4
return result
end
function self:nextFloat()
local result = buffer.readf32(stream, cursor)
cursor = cursor + 4
return tonumber(format("%0." .. FLOAT_PRECISION .. "f", result))
end
function self:nextVarInt()
local result = 0
for i = 0, 4 do
local b = self:nextByte()
result = bor(result, lshift(band(b, 0x7F), i * 7))
if not btest(b, 0x80) then
break
end
end
return result
end
function self:nextString(len)
len = len or self:nextVarInt()
if len == 0 then
return ""
else
local result = buffer.readstring(stream, cursor, len)
cursor = cursor + len
return result
end
end
function self:nextDouble()
local result = buffer.readf64(stream, cursor)
cursor = cursor + 8
return result
end
return self
end
function Reader:Set(precision)
FLOAT_PRECISION = precision
end
return Reader