-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunkbuffer.lua
More file actions
90 lines (65 loc) · 2.23 KB
/
chunkbuffer.lua
File metadata and controls
90 lines (65 loc) · 2.23 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
local floor = math.floor
-- ----------------------------------------------------------------------------
local ChunkBuffer = {}
ChunkBuffer.__index = ChunkBuffer
function ChunkBuffer.New(tbl, chunkSize)
local self = setmetatable(tbl or {}, ChunkBuffer)
self.chunkSize = chunkSize
self.chunkPointer = 1
self.shift = 0
self.invChunkSize = 1 / chunkSize
return self
end
function ChunkBuffer:GetPointers(position)
return floor(position * self.invChunkSize) + 1, position % self.chunkSize
end
function ChunkBuffer:Seek(position)
self.chunkPointer = floor(position * self.invChunkSize) + 1
self.shift = position % self.chunkSize
end
function ChunkBuffer:Skip(length)
length = length + self.shift
if length >= self.chunkSize then
self.chunkPointer = self.chunkPointer + floor(length * self.invChunkSize)
end
self.shift = length % self.chunkSize
end
local MBL = {}
do
for i = 1, 32 do MBL[i] = 2^i - 1 end
end
function ChunkBuffer:Read(length)
local startBytePointer, startShiftPointer = self.chunkPointer, self.shift
self:Skip(length)
local newBytePointer, newShiftPointer = self.chunkPointer, self.shift
local decimal = BitAnd(self[newBytePointer], MBL[newShiftPointer])
local C = self.chunkSize
for i = newBytePointer - 1, startBytePointer, -1 do
decimal = BitOr(BitLShift(decimal, C), self[i])
end
return BitRShift(decimal, startShiftPointer)
end
function ChunkBuffer:Write(decimal, length)
if MBL[length] < decimal then
error(('%d cant be written to buffer with length %d'):format(decimal, length))
end
local C = self.chunkSize
local L = MBL[C]
decimal = BitLShift(decimal, self.shift)
local startBytepointer = self.chunkPointer
self:Skip(length)
local newBytePointer = self.chunkPointer
for i = startBytepointer, newBytePointer do
self[i] = BitOr(BitAnd(decimal, L), self[i])
decimal = BitRShift(decimal, C)
end
end
-- TODO: optimize
function ChunkBuffer:WriteBit(bit)
self:Write(bit, 1)
end
function ChunkBuffer:Available()
return self.chunkPointer <= #self
end
-- ----------------------------------------------------------------------------
LibDataPacker_BinaryBuffer = ChunkBuffer