From f70adf5e6e7462e235947935edde760b6ce9df1e Mon Sep 17 00:00:00 2001 From: Stephen Leitnick Date: Thu, 30 Nov 2023 23:17:32 -0500 Subject: [PATCH] BufferUtil boolean --- README.md | 2 +- modules/buffer-util/BufferReader.lua | 15 +++++++++++++++ modules/buffer-util/BufferWriter.lua | 14 ++++++++++++++ modules/buffer-util/Types.lua | 2 ++ modules/buffer-util/index.d.ts | 6 ++++++ modules/buffer-util/package.json | 2 +- modules/buffer-util/wally.toml | 2 +- 7 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ee204fdd..29e62a62 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ | [Symbol](https://sleitnick.github.io/RbxUtil/api/Symbol) | `Symbol = "sleitnick/symbol@2.0.1"` | Symbol | | [Option](https://sleitnick.github.io/RbxUtil/api/Option) | `Option = "sleitnick/option@1.0.5"` | Represent optional values in Lua | | [EnumList](https://sleitnick.github.io/RbxUtil/api/EnumList) | `EnumList = "sleitnick/enum-list@2.1.0"` | 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/pid@1.1.0"` | PID Controller class | | [Loader](https://sleitnick.github.io/RbxUtil/api/Loader) | `Loader = "sleitnick/loader@2.0.0"` | Requires all modules within a given instance | | [Concur](https://sleitnick.github.io/RbxUtil/api/Concur) | `Concur = "sleitnick/concur@0.1.2"` | Concurrent task handler | diff --git a/modules/buffer-util/BufferReader.lua b/modules/buffer-util/BufferReader.lua index d8bb62c2..59c18be2 100644 --- a/modules/buffer-util/BufferReader.lua +++ b/modules/buffer-util/BufferReader.lua @@ -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(dataType: T): T local name = DataTypeBuffer.DataTypesToString[dataType] if not name then diff --git a/modules/buffer-util/BufferWriter.lua b/modules/buffer-util/BufferWriter.lua index 5a5c7865..ef0119ba 100644 --- a/modules/buffer-util/BufferWriter.lua +++ b/modules/buffer-util/BufferWriter.lua @@ -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] diff --git a/modules/buffer-util/Types.lua b/modules/buffer-util/Types.lua index 87274496..cf886097 100644 --- a/modules/buffer-util/Types.lua +++ b/modules/buffer-util/Types.lua @@ -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: (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) -> (), diff --git a/modules/buffer-util/index.d.ts b/modules/buffer-util/index.d.ts index 6a030667..24b04fc0 100644 --- a/modules/buffer-util/index.d.ts +++ b/modules/buffer-util/index.d.ts @@ -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; diff --git a/modules/buffer-util/package.json b/modules/buffer-util/package.json index 5b995dbf..8a870002 100644 --- a/modules/buffer-util/package.json +++ b/modules/buffer-util/package.json @@ -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", diff --git a/modules/buffer-util/wally.toml b/modules/buffer-util/wally.toml index 3ca15b35..d14e73b9 100644 --- a/modules/buffer-util/wally.toml +++ b/modules/buffer-util/wally.toml @@ -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"