Skip to content

Commit

Permalink
BufferUtil boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Dec 1, 2023
1 parent c884161 commit f70adf5
Showing 7 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
| [Symbol](https://sleitnick.github.io/RbxUtil/api/Symbol) | `Symbol = "sleitnick/[email protected]"` | Symbol |
| [Option](https://sleitnick.github.io/RbxUtil/api/Option) | `Option = "sleitnick/[email protected]"` | Represent optional values in Lua |
| [EnumList](https://sleitnick.github.io/RbxUtil/api/EnumList) | `EnumList = "sleitnick/[email protected]"` | Enum List class |
| [BufferUtil](https://sleitnick.github.io/RbxUtil/api/BufferUtil) | `BufferUtil = "sleitnick/buffer-util@0.2.0"` | Buffer utilities |
| [BufferUtil](https://sleitnick.github.io/RbxUtil/api/BufferUtil) | `BufferUtil = "sleitnick/buffer-util@0.3.0"` | Buffer utilities |
| [PID](https://sleitnick.github.io/RbxUtil/api/PID) | `PID = "sleitnick/[email protected]"` | PID Controller class |
| [Loader](https://sleitnick.github.io/RbxUtil/api/Loader) | `Loader = "sleitnick/[email protected]"` | Requires all modules within a given instance |
| [Concur](https://sleitnick.github.io/RbxUtil/api/Concur) | `Concur = "sleitnick/[email protected]"` | Concurrent task handler |
15 changes: 15 additions & 0 deletions modules/buffer-util/BufferReader.lua
Original file line number Diff line number Diff line change
@@ -125,6 +125,14 @@ function BufferReader:ReadFloat64(): number
return n
end

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

--[=[
Read a string from the buffer.
@@ -154,6 +162,13 @@ function BufferReader:ReadStringRaw(length: number): string
return s
end

--[=[
Read a DataType from the buffer.
```lua
local cframe = reader:ReadDataType(CFrame)
```
]=]
function BufferReader:ReadDataType<T>(dataType: T): T
local name = DataTypeBuffer.DataTypesToString[dataType]
if not name then
14 changes: 14 additions & 0 deletions modules/buffer-util/BufferWriter.lua
Original file line number Diff line number Diff line change
@@ -127,6 +127,13 @@ function BufferWriter:WriteFloat64(f64: number)
self._cursor += 8
end

--[=[
Write a boolean to the buffer.
]=]
function BufferWriter:WriteBool(bool: boolean)
self:WriteUInt8(if bool then 1 else 0)
end

--[=[
Write a string to the buffer. An optional `length` argument can
be provided to limit the number of bytes read from the string.
@@ -162,6 +169,13 @@ function BufferWriter:WriteStringRaw(str: string, length: number?)
self._cursor += len
end

--[=[
Write a DataType to the buffer.
```lua
writer:WriteDataType(CFrame.new(0, 10, 5))
```
]=]
function BufferWriter:WriteDataType(value: any)
local t = typeof(value)
local readWrite = DataTypeBuffer.ReadWrite[t]
2 changes: 2 additions & 0 deletions modules/buffer-util/Types.lua
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ export type BufferReader = {
ReadUInt32: (self: BufferReader) -> number,
ReadFloat32: (self: BufferReader) -> number,
ReadFloat64: (self: BufferReader) -> number,
ReadBool: (self: BufferReader) -> boolean,
ReadString: (self: BufferReader) -> string,
ReadStringRaw: (self: BufferReader, length: number) -> string,
ReadDataType: <T>(self: BufferReader, dataType: { new: (...any) -> T }) -> T,
@@ -42,6 +43,7 @@ export type BufferWriter = {
WriteUInt32: (self: BufferWriter, uint32: number) -> (),
WriteFloat32: (self: BufferWriter, f32: number) -> (),
WriteFloat64: (self: BufferWriter, f64: number) -> (),
WriteBool: (self: BufferWriter, bool: boolean) -> (),
WriteString: (self: BufferWriter, str: string, length: number?) -> (),
WriteStringRaw: (self: BufferWriter, str: string, length: number?) -> (),
WriteDataType: (self: BufferWriter, data: DataTypes) -> (),
6 changes: 6 additions & 0 deletions modules/buffer-util/index.d.ts
Original file line number Diff line number Diff line change
@@ -48,6 +48,9 @@ declare namespace BufferUtil {
/** Read a 64-bit double-precision float from the buffer. */
ReadFloat64(): number;

/** Read a bool from the buffer. */
ReadBool(): boolean;

/** Read a string from the buffer. */
ReadString(): string;

@@ -98,6 +101,9 @@ declare namespace BufferUtil {
/** Write a 64-bit double-precision float to the buffer. */
WriteFloat64(f64: number): void;

/** Write a boolean to the buffer. */
WriteBool(bool: boolean): void;

/** Write a string to the buffer, with an optional `length` of bytes taken from the string. */
WriteString(str: string, length?: number): void;

2 changes: 1 addition & 1 deletion modules/buffer-util/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rbxutil/buffer-util",
"version": "0.2.0",
"version": "0.3.0",
"main": "init.lua",
"repository": "github:Sleitnick/RbxUtil",
"license": "MIT",
2 changes: 1 addition & 1 deletion modules/buffer-util/wally.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sleitnick/buffer-util"
description = "Buffer utilities"
version = "0.2.0"
version = "0.3.0"
license = "MIT"
authors = ["Stephen Leitnick"]
registry = "https://github.com/UpliftGames/wally-index"

0 comments on commit f70adf5

Please sign in to comment.