Skip to content

Commit

Permalink
Merge branch 'main' into darklua-workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
YetAnotherClown authored Jul 10, 2024
2 parents 0a526ef + 7f4c764 commit ce17b68
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 28 deletions.
5 changes: 4 additions & 1 deletion lib/SendRequest.luau
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type NetServer = Bridge.NetServer
local Identifier = require("@yetanothernet/Identifier")
export type Identifier = Identifier.Identifier

local isRunning = RunService:IsRunning()
local isClient = RunService:IsClient()

--[=[
@class SendRequest
@since v0.4.0
Expand Down Expand Up @@ -44,7 +47,7 @@ SendRequest.__index = SendRequest
@param recipient { Player } | Player | "NET_SERVER"
]=]
function SendRequest:to(recipient: Recipient)
if RunService:IsRunning() and RunService:IsClient() then
if isRunning and isClient then
warn("You should not use the SendRequest:to() method on the Client")
end

Expand Down
23 changes: 15 additions & 8 deletions lib/serdes/datatypes/primitive/boolean.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

local BUFFER_SIZE = 1

local map = {
[true] = 1,
[false] = 0,
}
local _true = buffer.create(BUFFER_SIZE)
buffer.writei8(_true, 0, 1)

local function serialize(bool: boolean?): buffer
local _buffer = buffer.create(BUFFER_SIZE)
buffer.writei8(_buffer, 0, bool and map[bool] or -1)
local _false = buffer.create(BUFFER_SIZE)
buffer.writei8(_false, 0, 0)

return _buffer
local _nil = buffer.create(BUFFER_SIZE)
buffer.writei8(_nil, 0, -1)

local function serialize(bool: boolean?): buffer
if bool == true then
return _true
elseif bool == false then
return _false
else
return _nil
end
end

local function deserialize(_buffer: buffer): boolean?
Expand Down
8 changes: 8 additions & 0 deletions lib/serdes/datatypes/userdata/BrickColor.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

local BUFFER_SIZE = 2

local brickColorCache = {}

local function serialize(color: BrickColor): buffer
if brickColorCache[color.Number] then
return brickColorCache[color.Number]
end

local _buffer = buffer.create(BUFFER_SIZE)
buffer.writeu16(_buffer, 0, color.Number)

brickColorCache[color.Number] = _buffer

return _buffer
end

Expand Down
28 changes: 18 additions & 10 deletions lib/serdes/datatypes/userdata/EnumItem.luau
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
--!native

local enumMap = {}
for _, enum in Enum:GetEnums() do
enumMap[tostring(enum)] = {}

for _, enumItem in enum:GetEnumItems() do
enumMap[tostring(enum)][enumItem.Value] = enumItem
end
end

local enumItemCache = {}

local function serialize(enumItem: EnumItem): buffer
if enumItemCache[enumItem] then
return enumItemCache[enumItem]
end

local typeBuffer = buffer.fromstring(tostring(enumItem.EnumType))

local _buffer = buffer.create(1 + buffer.len(typeBuffer))
buffer.writei8(_buffer, 0, enumItem.Value)
buffer.copy(_buffer, 1, typeBuffer)

enumItemCache[enumItem] = _buffer

return _buffer
end

Expand All @@ -16,17 +33,8 @@ local function deserialize(_buffer: buffer): EnumItem

local _type = buffer.tostring(typeBuffer)
local value = buffer.readi8(_buffer, 0)
local enumItem

for _, enum in Enum:GetEnums() do
for _, _enumItem in enum:GetEnumItems() do
if tostring(_enumItem.EnumType) == _type and _enumItem.Value == value then
enumItem = _enumItem
end
end
end

return enumItem
return enumMap[_type][value]
end

return {
Expand Down
26 changes: 17 additions & 9 deletions lib/serdes/utilities.luau
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
local datatypes = script.Parent.datatypes

local primitives = datatypes.primitive
local userdata = datatypes.userdata
local custom = datatypes.custom

local idsToTypeMap
local modulesMap

local function getSerDesById(id: number)
if not idsToTypeMap then
Expand All @@ -29,11 +26,22 @@ local function getSerDes(_type: string)
_type = "boolean"
end

local module = primitives:FindFirstChild(_type)
or userdata:FindFirstChild(_type)
or custom:FindFirstChild(_type)
if not modulesMap then
modulesMap = {}

for _, module in datatypes:GetDescendants() do
if not module:IsA("ModuleScript") then
continue
end

local serdes = require(module) :: any
modulesMap[module.Name] = serdes
end
end

local module = modulesMap[_type]
if module then
return require(module) :: any
return module
end

warn(`Unexpected type {_type} passed`)
Expand Down Expand Up @@ -67,7 +75,7 @@ local function deserializeBuffer(_buffer: buffer, cursor: number): (any, number)
end

-- Source: https://github.com/jsdotlua/luau-polyfill/blob/main/modules/collections/src/Array/isArray.lua
local function isArray(value: { [number]: any })
local function isArray(value)
if next(value) == nil then
return true
end
Expand Down

0 comments on commit ce17b68

Please sign in to comment.