Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations + better CFrame #205

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ packages/*/Packages
*.rbxl
*.rbxl.lock
sourcemap.json
*.code-workspace
119 changes: 67 additions & 52 deletions modules/buffer-util/BufferReader.luau
Original file line number Diff line number Diff line change
Expand Up @@ -13,123 +13,136 @@ local Types = require(script.Parent.Types)
local BufferReader = {}
BufferReader.__index = BufferReader

function BufferReader.new(buf: string | buffer | Types.BufferWriter): Types.BufferReader
if typeof(buf) == "string" then
return BufferReader.fromString(buf)
elseif typeof(buf) == "buffer" then
return BufferReader.fromBuffer(buf)
elseif typeof(buf) == "table" and getmetatable(buf :: any) == BufferWriter then
return BufferReader.fromBuffer(buf:GetBuffer())
end

error(`expected string or buffer; got {typeof(buf)}`)
end
-- these don't have fast call yet
local buffer_fromstring = buffer.fromstring

function BufferReader.fromBuffer(buf: buffer)
local self = setmetatable({
local function fromBuffer(buf: buffer): Types.BufferReader
return setmetatable({
_buffer = buf,
_size = buffer.len(buf),
_cursor = 0,
}, BufferReader)

return self
}, BufferReader) :: never
end
local function fromString(str: string)
return fromBuffer(buffer_fromstring(str))
end

function BufferReader.new(buf: string | buffer | Types.BufferWriter): Types.BufferReader
if type(buf) == "string" then
return fromString(buf)
elseif type(buf) == "buffer" then
return fromBuffer(buf)
elseif type(buf) == "table" and getmetatable(buf :: any) == BufferWriter then
return fromBuffer((buf :: never)._buffer)
end

function BufferReader.fromString(str: string)
return BufferReader.fromBuffer(buffer.fromstring(str))
error(`expected string or buffer; got {typeof(buf)}`)
end

function BufferReader:_assertSize(desiredSize: number)
BufferReader.fromBuffer = fromBuffer
BufferReader.fromString = fromString

local function assertSize(self, desiredSize: number)
if desiredSize > self._size then
error(`cursor out of bounds`, 3)
error("cursor out of bounds", 3)
end
end

--[=[
Read a signed 8-bit integer from the buffer.
]=]
function BufferReader:ReadInt8(): number
self:_assertSize(self._cursor + 1)
local n = buffer.readi8(self._buffer, self._cursor)
self._cursor += 1
local cursor = self._cursor
assertSize(self, cursor + 1)
local n = buffer.readi8(self._buffer, cursor)
self._cursor = cursor + 1
return n
end

--[=[
Read an unsigned 8-bit integer from the buffer.
]=]
function BufferReader:ReadUInt8(): number
self:_assertSize(self._cursor + 1)
local n = buffer.readu8(self._buffer, self._cursor)
self._cursor += 1
local cursor = self._cursor
assertSize(self, cursor + 1)
local n = buffer.readu8(self._buffer, cursor)
self._cursor = cursor + 1
return n
end
local readUInt8 = BufferReader.ReadUInt8

--[=[
Read a signed 16-bit integer from the buffer.
]=]
function BufferReader:ReadInt16(): number
self:_assertSize(self._cursor + 2)
local n = buffer.readi16(self._buffer, self._cursor)
self._cursor += 2
local cursor = self._cursor
assertSize(self, cursor + 2)
local n = buffer.readi16(self._buffer, cursor)
self._cursor = cursor + 2
return n
end

--[=[
Read an unsigned 16-bit integer from the buffer.
]=]
function BufferReader:ReadUInt16(): number
self:_assertSize(self._cursor + 2)
local n = buffer.readu16(self._buffer, self._cursor)
self._cursor += 2
local cursor = self._cursor
assertSize(self, cursor + 2)
local n = buffer.readu16(self._buffer, cursor)
self._cursor = cursor + 2
return n
end

--[=[
Read a signed 32-bit integer from the buffer.
]=]
function BufferReader:ReadInt32(): number
self:_assertSize(self._cursor + 4)
local n = buffer.readi32(self._buffer, self._cursor)
self._cursor += 4
local cursor = self._cursor
assertSize(self, cursor + 4)
local n = buffer.readi32(self._buffer, cursor)
self._cursor = cursor + 4
return n
end

--[=[
Read an unsigned 32-bit integer from the buffer.
]=]
function BufferReader:ReadUInt32(): number
self:_assertSize(self._cursor + 4)
local n = buffer.readu32(self._buffer, self._cursor)
self._cursor += 4
local cursor = self._cursor
assertSize(self, cursor + 4)
local n = buffer.readu32(self._buffer, cursor)
self._cursor = cursor + 4
return n
end
local readUInt32 = BufferReader.ReadUInt32

--[=[
Read a 32-bit single-precision float from the buffer.
]=]
function BufferReader:ReadFloat32(): number
self:_assertSize(self._cursor + 4)
local n = buffer.readf32(self._buffer, self._cursor)
self._cursor += 4
local cursor = self._cursor
assertSize(self, cursor + 4)
local n = buffer.readf32(self._buffer, cursor)
self._cursor = cursor + 4
return n
end

--[=[
Read a 64-bit double-precision float from the buffer.
]=]
function BufferReader:ReadFloat64(): number
self:_assertSize(self._cursor + 8)
local n = buffer.readf64(self._buffer, self._cursor)
self._cursor += 8
local cursor = self._cursor
assertSize(self, cursor + 8)
local n = buffer.readf64(self._buffer, cursor)
self._cursor = cursor + 8
return n
end

--[=[
Read a boolean from the buffer.
]=]
function BufferReader:ReadBool(): boolean
local n = self:ReadUInt8()
local n = readUInt8(self)
return n == 1
end

Expand All @@ -141,10 +154,11 @@ end
method, which stores an extra integer to mark the size of the string.
]=]
function BufferReader:ReadString(): string
local strLen = self:ReadUInt32()
self:_assertSize(self._cursor + strLen)
local s = buffer.readstring(self._buffer, self._cursor, strLen)
self._cursor += strLen
local strLen = readUInt32(self)
local cursor = self._cursor
assertSize(self, cursor + strLen)
local s = buffer.readstring(self._buffer, cursor, strLen)
self._cursor = cursor + strLen
return s
end

Expand All @@ -156,9 +170,10 @@ end
]=]
function BufferReader:ReadStringRaw(length: number): string
length = math.max(0, math.floor(length))
self:_assertSize(self._cursor + length)
local s = buffer.readstring(self._buffer, self._cursor, length)
self._cursor += length
local cursor = self._cursor
assertSize(self, cursor + length)
local s = buffer.readstring(self._buffer, cursor, length)
self._cursor = cursor + length
return s
end

Expand Down
Loading
Loading