@axpecter/lync 1.4.3 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -2
- package/package.json +1 -1
- package/src/Types.luau +13 -1
- package/src/api/Group.luau +35 -28
- package/src/api/Namespace.luau +107 -89
- package/src/api/Packet.luau +69 -51
- package/src/api/Query.luau +101 -95
- package/src/api/Scope.luau +32 -18
- package/src/api/Signal.luau +78 -56
- package/src/codec/Base.luau +17 -17
- package/src/codec/composite/Array.luau +74 -74
- package/src/codec/composite/Map.luau +80 -80
- package/src/codec/composite/Optional.luau +14 -14
- package/src/codec/composite/Shared.luau +35 -35
- package/src/codec/composite/Struct.luau +108 -108
- package/src/codec/composite/Tagged.luau +71 -71
- package/src/codec/composite/Tuple.luau +35 -35
- package/src/codec/datatype/Buffer.luau +18 -18
- package/src/codec/datatype/CFrame.luau +24 -24
- package/src/codec/datatype/Color.luau +8 -12
- package/src/codec/datatype/Instance.luau +13 -13
- package/src/codec/datatype/IntVector.luau +17 -17
- package/src/codec/datatype/NumberRange.luau +7 -7
- package/src/codec/datatype/Ray.luau +16 -16
- package/src/codec/datatype/Rect.luau +13 -13
- package/src/codec/datatype/Region.luau +32 -36
- package/src/codec/datatype/Sequence.luau +41 -41
- package/src/codec/datatype/String.luau +28 -28
- package/src/codec/datatype/UDim.luau +17 -17
- package/src/codec/datatype/Vector.luau +14 -18
- package/src/codec/meta/Auto.luau +75 -73
- package/src/codec/meta/Bitfield.luau +46 -46
- package/src/codec/meta/Custom.luau +7 -7
- package/src/codec/meta/Enum.luau +25 -25
- package/src/codec/meta/Nothing.luau +3 -3
- package/src/codec/meta/Quantized.luau +63 -60
- package/src/codec/meta/Unknown.luau +11 -11
- package/src/codec/primitive/Bool.luau +12 -12
- package/src/codec/primitive/Float16.luau +28 -28
- package/src/codec/primitive/Number.luau +41 -41
- package/src/codec/primitive/Varint.luau +19 -19
- package/src/init.luau +81 -61
- package/src/internal/Baseline.luau +7 -7
- package/src/internal/Channel.luau +56 -56
- package/src/internal/Middleware.luau +29 -29
- package/src/internal/Pool.luau +15 -15
- package/src/internal/Registry.luau +19 -19
- package/src/transport/Bridge.luau +17 -17
- package/src/transport/Client.luau +46 -46
- package/src/transport/Gate.luau +56 -64
- package/src/transport/Reader.luau +34 -34
- package/src/transport/Server.luau +89 -89
package/src/codec/meta/Enum.luau
CHANGED
|
@@ -2,80 +2,80 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- String enum codec. Maps values to u8 indices at definition time.
|
|
4
4
|
|
|
5
|
-
local Channel = require
|
|
6
|
-
local Types = require
|
|
5
|
+
local Channel = require(script.Parent.Parent.Parent.internal.Channel)
|
|
6
|
+
local Types = require(script.Parent.Parent.Parent.Types)
|
|
7
7
|
|
|
8
8
|
type ChannelState = Types.ChannelState
|
|
9
9
|
type Codec<T> = Types.Codec<T>
|
|
10
10
|
|
|
11
11
|
local alloc = Channel.alloc
|
|
12
12
|
|
|
13
|
-
-- Public
|
|
13
|
+
-- Public --------------------------------------------------------------
|
|
14
14
|
|
|
15
15
|
local EnumCodec = {}
|
|
16
16
|
|
|
17
|
-
function EnumCodec.define
|
|
17
|
+
function EnumCodec.define(...: string): Codec<string>
|
|
18
18
|
local values = { ... }
|
|
19
19
|
local count = #values
|
|
20
20
|
if count == 0 then
|
|
21
|
-
error
|
|
21
|
+
error("[Lync] Enum requires at least one value")
|
|
22
22
|
end
|
|
23
23
|
if count > 256 then
|
|
24
|
-
error
|
|
24
|
+
error(`[Lync] Enum exceeds 256 values: {count}`)
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
local toIndex = {} :: { [string]: number }
|
|
28
|
-
local toValue = table.create
|
|
28
|
+
local toValue = table.create(count) :: { string }
|
|
29
29
|
|
|
30
30
|
for i, value in values do
|
|
31
31
|
if toIndex[value] ~= nil then
|
|
32
|
-
error
|
|
32
|
+
error(`[Lync] Duplicate enum value: "{value}"`)
|
|
33
33
|
end
|
|
34
34
|
toIndex[value] = i - 1
|
|
35
35
|
toValue[i] = value
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
table.freeze
|
|
39
|
-
table.freeze
|
|
38
|
+
table.freeze(toIndex)
|
|
39
|
+
table.freeze(toValue)
|
|
40
40
|
|
|
41
|
-
return table.freeze
|
|
41
|
+
return table.freeze({
|
|
42
42
|
_size = 1,
|
|
43
|
-
_directWrite = function
|
|
43
|
+
_directWrite = function(b: buffer, offset: number, value: string): ()
|
|
44
44
|
local idx = toIndex[value]
|
|
45
45
|
if idx == nil then
|
|
46
|
-
error
|
|
46
|
+
error(`[Lync] Invalid enum value: "{value}"`)
|
|
47
47
|
end
|
|
48
|
-
buffer.writeu8
|
|
48
|
+
buffer.writeu8(b, offset, idx)
|
|
49
49
|
end,
|
|
50
|
-
_directRead = function
|
|
51
|
-
local idx = buffer.readu8
|
|
50
|
+
_directRead = function(b: buffer, offset: number): string
|
|
51
|
+
local idx = buffer.readu8(b, offset)
|
|
52
52
|
local value = toValue[idx + 1]
|
|
53
53
|
if value == nil then
|
|
54
|
-
error
|
|
54
|
+
error(`[Lync] Invalid enum index: {idx}`)
|
|
55
55
|
end
|
|
56
56
|
return value
|
|
57
57
|
end,
|
|
58
|
-
write = function
|
|
58
|
+
write = function(ch: ChannelState, value: string): ()
|
|
59
59
|
local idx = toIndex[value]
|
|
60
60
|
if idx == nil then
|
|
61
|
-
error
|
|
61
|
+
error(`[Lync] Invalid enum value: "{value}"`)
|
|
62
62
|
end
|
|
63
63
|
local c = ch.cursor
|
|
64
64
|
if c + 1 > ch.size then
|
|
65
|
-
alloc
|
|
65
|
+
alloc(ch, 1)
|
|
66
66
|
end
|
|
67
|
-
buffer.writeu8
|
|
67
|
+
buffer.writeu8(ch.buff, c, idx)
|
|
68
68
|
ch.cursor = c + 1
|
|
69
69
|
end,
|
|
70
|
-
read = function
|
|
71
|
-
local idx = buffer.readu8
|
|
70
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): (string, number)
|
|
71
|
+
local idx = buffer.readu8(src, pos)
|
|
72
72
|
local value = toValue[idx + 1]
|
|
73
73
|
if value == nil then
|
|
74
|
-
error
|
|
74
|
+
error(`[Lync] Invalid enum index: {idx}`)
|
|
75
75
|
end
|
|
76
76
|
return value, 1
|
|
77
77
|
end,
|
|
78
78
|
})
|
|
79
79
|
end
|
|
80
80
|
|
|
81
|
-
return table.freeze
|
|
81
|
+
return table.freeze(EnumCodec)
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Zero-byte codec. Reads nil. Good for fire-and-forget signals.
|
|
4
4
|
|
|
5
|
-
local Base = require
|
|
5
|
+
local Base = require(script.Parent.Parent.Base)
|
|
6
6
|
|
|
7
|
-
return table.freeze
|
|
8
|
-
nothing = Base.zero
|
|
7
|
+
return table.freeze({
|
|
8
|
+
nothing = Base.zero(),
|
|
9
9
|
})
|
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Fixed-point compression. Maps float ranges to integer ranges.
|
|
4
4
|
|
|
5
|
-
local Channel = require
|
|
6
|
-
local Types = require
|
|
5
|
+
local Channel = require(script.Parent.Parent.Parent.internal.Channel)
|
|
6
|
+
local Types = require(script.Parent.Parent.Parent.Types)
|
|
7
7
|
|
|
8
8
|
type ChannelState = Types.ChannelState
|
|
9
9
|
type Codec<T> = Types.Codec<T>
|
|
10
10
|
|
|
11
11
|
local alloc = Channel.alloc
|
|
12
12
|
|
|
13
|
-
-- Constants
|
|
13
|
+
-- Constants -----------------------------------------------------------
|
|
14
14
|
|
|
15
15
|
local clamp = math.clamp
|
|
16
16
|
local round = math.round
|
|
@@ -20,9 +20,9 @@ local max = math.max
|
|
|
20
20
|
|
|
21
21
|
local U32_MAX = 4294967295
|
|
22
22
|
|
|
23
|
-
-- Private
|
|
23
|
+
-- Private -------------------------------------------------------------
|
|
24
24
|
|
|
25
|
-
local function selectIO
|
|
25
|
+
local function selectIO(bytes: number): ((b: buffer, offset: number, v: number) -> (), (
|
|
26
26
|
b: buffer,
|
|
27
27
|
offset: number
|
|
28
28
|
) -> number)
|
|
@@ -35,7 +35,7 @@ local function selectIO (bytes: number): ((b: buffer, offset: number, v: number)
|
|
|
35
35
|
return buffer.writeu32, buffer.readu32
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
local function selectWidth
|
|
38
|
+
local function selectWidth(maxInt: number): number
|
|
39
39
|
if maxInt <= 255 then
|
|
40
40
|
return 1
|
|
41
41
|
end
|
|
@@ -45,17 +45,21 @@ local function selectWidth (maxInt: number): number
|
|
|
45
45
|
return 4
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
-
-- Public
|
|
48
|
+
-- Public --------------------------------------------------------------
|
|
49
49
|
|
|
50
50
|
local Quantized = {}
|
|
51
51
|
|
|
52
52
|
-- Shared validation and precomputation for both float and vec3 quantizers.
|
|
53
|
-
local function setup
|
|
53
|
+
local function setup(
|
|
54
|
+
minVal: number,
|
|
55
|
+
maxVal: number,
|
|
56
|
+
precision: number
|
|
57
|
+
): (number?, number?, number?, any?, any?, number?, number?)
|
|
54
58
|
if minVal > maxVal then
|
|
55
|
-
error
|
|
59
|
+
error(`[Lync] Quantized min > max: {minVal} > {maxVal}`)
|
|
56
60
|
end
|
|
57
61
|
if precision <= 0 then
|
|
58
|
-
error
|
|
62
|
+
error(`[Lync] Quantized precision must be positive: {precision}`)
|
|
59
63
|
end
|
|
60
64
|
|
|
61
65
|
local delta = maxVal - minVal
|
|
@@ -63,66 +67,65 @@ local function setup (minVal: number, maxVal: number, precision: number)
|
|
|
63
67
|
return nil
|
|
64
68
|
end
|
|
65
69
|
|
|
66
|
-
local maxInt = min
|
|
67
|
-
local bytes = selectWidth
|
|
68
|
-
local wfn, rfn = selectIO
|
|
70
|
+
local maxInt = min(U32_MAX, max(1, ceil(delta / precision)))
|
|
71
|
+
local bytes = selectWidth(maxInt)
|
|
72
|
+
local wfn, rfn = selectIO(bytes)
|
|
69
73
|
local invDelta = 1 / delta
|
|
70
74
|
local invMaxInt = delta / maxInt
|
|
71
75
|
|
|
72
76
|
return delta, maxInt, bytes, wfn, rfn, invDelta, invMaxInt
|
|
73
77
|
end
|
|
74
78
|
|
|
75
|
-
function Quantized.float
|
|
76
|
-
local delta, maxInt, bytes, wfn, rfn, invDelta, invMaxInt = setup
|
|
79
|
+
function Quantized.float(minVal: number, maxVal: number, precision: number): Codec<number>
|
|
80
|
+
local delta, maxInt, bytes, wfn, rfn, invDelta, invMaxInt = setup(minVal, maxVal, precision)
|
|
77
81
|
if not delta then
|
|
78
|
-
return table.freeze
|
|
82
|
+
return table.freeze({
|
|
79
83
|
_size = 0,
|
|
80
|
-
_directWrite = function
|
|
81
|
-
_directRead = function
|
|
84
|
+
_directWrite = function(_b: buffer, _offset: number, _value: number): () end,
|
|
85
|
+
_directRead = function(_b: buffer, _offset: number): number
|
|
82
86
|
return minVal
|
|
83
87
|
end,
|
|
84
|
-
write = function
|
|
85
|
-
read = function
|
|
88
|
+
write = function(_ch: ChannelState, _value: number): () end,
|
|
89
|
+
read = function(_src: buffer, _pos: number, _refs: { Instance }?): (number, number)
|
|
86
90
|
return minVal, 0
|
|
87
91
|
end,
|
|
88
92
|
})
|
|
89
93
|
end
|
|
90
94
|
|
|
91
|
-
return table.freeze
|
|
95
|
+
return table.freeze({
|
|
92
96
|
_size = bytes,
|
|
93
|
-
_directWrite = function
|
|
94
|
-
wfn
|
|
97
|
+
_directWrite = function(b: buffer, offset: number, value: number): ()
|
|
98
|
+
wfn(b, offset, round(clamp((value - minVal) * invDelta, 0, 1) * maxInt))
|
|
95
99
|
end,
|
|
96
|
-
_directRead = function
|
|
97
|
-
return rfn
|
|
100
|
+
_directRead = function(b: buffer, offset: number): number
|
|
101
|
+
return rfn(b, offset) * invMaxInt + minVal
|
|
98
102
|
end,
|
|
99
|
-
write = function
|
|
103
|
+
write = function(ch: ChannelState, value: number): ()
|
|
100
104
|
local c = ch.cursor
|
|
101
105
|
if c + bytes > ch.size then
|
|
102
|
-
alloc
|
|
106
|
+
alloc(ch, bytes)
|
|
103
107
|
end
|
|
104
|
-
wfn
|
|
108
|
+
wfn(ch.buff, c, round(clamp((value - minVal) * invDelta, 0, 1) * maxInt))
|
|
105
109
|
ch.cursor = c + bytes
|
|
106
110
|
end,
|
|
107
|
-
read = function
|
|
108
|
-
return rfn
|
|
111
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): (number, number)
|
|
112
|
+
return rfn(src, pos) * invMaxInt + minVal, bytes
|
|
109
113
|
end,
|
|
110
114
|
})
|
|
111
115
|
end
|
|
112
116
|
|
|
113
|
-
function Quantized.vec3
|
|
114
|
-
local delta, maxInt, compBytes, wfn, rfn, invDelta, invMaxInt =
|
|
115
|
-
setup (minVal, maxVal, precision)
|
|
117
|
+
function Quantized.vec3(minVal: number, maxVal: number, precision: number): Codec<Vector3>
|
|
118
|
+
local delta, maxInt, compBytes, wfn, rfn, invDelta, invMaxInt = setup(minVal, maxVal, precision)
|
|
116
119
|
if not delta then
|
|
117
|
-
local val = Vector3.new
|
|
118
|
-
return table.freeze
|
|
120
|
+
local val = Vector3.new(minVal, minVal, minVal)
|
|
121
|
+
return table.freeze({
|
|
119
122
|
_size = 0,
|
|
120
|
-
_directWrite = function
|
|
121
|
-
_directRead = function
|
|
123
|
+
_directWrite = function(_b: buffer, _offset: number, _value: Vector3): () end,
|
|
124
|
+
_directRead = function(_b: buffer, _offset: number): Vector3
|
|
122
125
|
return val
|
|
123
126
|
end,
|
|
124
|
-
write = function
|
|
125
|
-
read = function
|
|
127
|
+
write = function(_ch: ChannelState, _value: Vector3): () end,
|
|
128
|
+
read = function(_src: buffer, _pos: number, _refs: { Instance }?): (Vector3, number)
|
|
126
129
|
return val, 0
|
|
127
130
|
end,
|
|
128
131
|
})
|
|
@@ -132,40 +135,40 @@ function Quantized.vec3 (minVal: number, maxVal: number, precision: number): Cod
|
|
|
132
135
|
local stride = compBytes
|
|
133
136
|
local stride2 = compBytes * 2
|
|
134
137
|
|
|
135
|
-
return table.freeze
|
|
138
|
+
return table.freeze({
|
|
136
139
|
_size = totalBytes,
|
|
137
|
-
_directWrite = function
|
|
138
|
-
wfn
|
|
139
|
-
wfn
|
|
140
|
-
wfn
|
|
140
|
+
_directWrite = function(b: buffer, offset: number, value: Vector3): ()
|
|
141
|
+
wfn(b, offset, round(clamp((value.X - minVal) * invDelta, 0, 1) * maxInt))
|
|
142
|
+
wfn(b, offset + stride, round(clamp((value.Y - minVal) * invDelta, 0, 1) * maxInt))
|
|
143
|
+
wfn(b, offset + stride2, round(clamp((value.Z - minVal) * invDelta, 0, 1) * maxInt))
|
|
141
144
|
end,
|
|
142
|
-
_directRead = function
|
|
143
|
-
return Vector3.new
|
|
144
|
-
rfn
|
|
145
|
-
rfn
|
|
146
|
-
rfn
|
|
145
|
+
_directRead = function(b: buffer, offset: number): Vector3
|
|
146
|
+
return Vector3.new(
|
|
147
|
+
rfn(b, offset) * invMaxInt + minVal,
|
|
148
|
+
rfn(b, offset + stride) * invMaxInt + minVal,
|
|
149
|
+
rfn(b, offset + stride2) * invMaxInt + minVal
|
|
147
150
|
)
|
|
148
151
|
end,
|
|
149
|
-
write = function
|
|
152
|
+
write = function(ch: ChannelState, value: Vector3): ()
|
|
150
153
|
local c = ch.cursor
|
|
151
154
|
if c + totalBytes > ch.size then
|
|
152
|
-
alloc
|
|
155
|
+
alloc(ch, totalBytes)
|
|
153
156
|
end
|
|
154
157
|
local b = ch.buff
|
|
155
|
-
wfn
|
|
156
|
-
wfn
|
|
157
|
-
wfn
|
|
158
|
+
wfn(b, c, round(clamp((value.X - minVal) * invDelta, 0, 1) * maxInt))
|
|
159
|
+
wfn(b, c + stride, round(clamp((value.Y - minVal) * invDelta, 0, 1) * maxInt))
|
|
160
|
+
wfn(b, c + stride2, round(clamp((value.Z - minVal) * invDelta, 0, 1) * maxInt))
|
|
158
161
|
ch.cursor = c + totalBytes
|
|
159
162
|
end,
|
|
160
|
-
read = function
|
|
161
|
-
return Vector3.new
|
|
162
|
-
rfn
|
|
163
|
-
rfn
|
|
164
|
-
rfn
|
|
163
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): (Vector3, number)
|
|
164
|
+
return Vector3.new(
|
|
165
|
+
rfn(src, pos) * invMaxInt + minVal,
|
|
166
|
+
rfn(src, pos + stride) * invMaxInt + minVal,
|
|
167
|
+
rfn(src, pos + stride2) * invMaxInt + minVal
|
|
165
168
|
),
|
|
166
169
|
totalBytes
|
|
167
170
|
end,
|
|
168
171
|
})
|
|
169
172
|
end
|
|
170
173
|
|
|
171
|
-
return table.freeze
|
|
174
|
+
return table.freeze(Quantized)
|
|
@@ -2,37 +2,37 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Sidecar codec. Bypasses buffer serialization, uses Roblox remote refs.
|
|
4
4
|
|
|
5
|
-
local Channel = require
|
|
6
|
-
local Types = require
|
|
5
|
+
local Channel = require(script.Parent.Parent.Parent.internal.Channel)
|
|
6
|
+
local Types = require(script.Parent.Parent.Parent.Types)
|
|
7
7
|
|
|
8
8
|
type ChannelState = Types.ChannelState
|
|
9
9
|
|
|
10
10
|
local alloc = Channel.alloc
|
|
11
11
|
|
|
12
|
-
return table.freeze
|
|
13
|
-
unknown = table.freeze
|
|
14
|
-
write = function
|
|
12
|
+
return table.freeze({
|
|
13
|
+
unknown = table.freeze({
|
|
14
|
+
write = function(ch: ChannelState, value: any): ()
|
|
15
15
|
local refs = ch.refs
|
|
16
16
|
local idx = #refs + 1
|
|
17
17
|
if idx > 65535 then
|
|
18
|
-
error
|
|
18
|
+
error(`[Lync] Unknown ref overflow: {idx} exceeds u16 max`)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
refs[idx] = value
|
|
22
22
|
|
|
23
23
|
local c = ch.cursor
|
|
24
24
|
if c + 2 > ch.size then
|
|
25
|
-
alloc
|
|
25
|
+
alloc(ch, 2)
|
|
26
26
|
end
|
|
27
|
-
buffer.writeu16
|
|
27
|
+
buffer.writeu16(ch.buff, c, idx)
|
|
28
28
|
ch.cursor = c + 2
|
|
29
29
|
end,
|
|
30
|
-
read = function
|
|
30
|
+
read = function(src: buffer, pos: number, refs: { Instance }?): (any, number)
|
|
31
31
|
if not refs then
|
|
32
|
-
error
|
|
32
|
+
error("[Lync] Unknown read requires refs array")
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
local idx = buffer.readu16
|
|
35
|
+
local idx = buffer.readu16(src, pos)
|
|
36
36
|
return refs[idx], 2
|
|
37
37
|
end,
|
|
38
38
|
}),
|
|
@@ -2,29 +2,29 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Boolean codec. _isBool tells struct to pack into bitfields.
|
|
4
4
|
|
|
5
|
-
local Channel = require
|
|
5
|
+
local Channel = require(script.Parent.Parent.Parent.internal.Channel)
|
|
6
6
|
local alloc = Channel.alloc
|
|
7
7
|
|
|
8
|
-
return table.freeze
|
|
9
|
-
bool = table.freeze
|
|
8
|
+
return table.freeze({
|
|
9
|
+
bool = table.freeze({
|
|
10
10
|
_size = 1,
|
|
11
11
|
_isBool = true,
|
|
12
|
-
_directWrite = function
|
|
13
|
-
buffer.writeu8
|
|
12
|
+
_directWrite = function(b: buffer, offset: number, value: boolean): ()
|
|
13
|
+
buffer.writeu8(b, offset, if value then 1 else 0)
|
|
14
14
|
end,
|
|
15
|
-
_directRead = function
|
|
16
|
-
return buffer.readu8
|
|
15
|
+
_directRead = function(b: buffer, offset: number): boolean
|
|
16
|
+
return buffer.readu8(b, offset) ~= 0
|
|
17
17
|
end,
|
|
18
|
-
write = function
|
|
18
|
+
write = function(ch: any, value: boolean): ()
|
|
19
19
|
local c = ch.cursor
|
|
20
20
|
if c + 1 > ch.size then
|
|
21
|
-
alloc
|
|
21
|
+
alloc(ch, 1)
|
|
22
22
|
end
|
|
23
|
-
buffer.writeu8
|
|
23
|
+
buffer.writeu8(ch.buff, c, if value then 1 else 0)
|
|
24
24
|
ch.cursor = c + 1
|
|
25
25
|
end,
|
|
26
|
-
read = function
|
|
27
|
-
return buffer.readu8
|
|
26
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): (boolean, number)
|
|
27
|
+
return buffer.readu8(src, pos) ~= 0, 1
|
|
28
28
|
end,
|
|
29
29
|
}),
|
|
30
30
|
})
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Half-precision float codec. 2 bytes, ±65504, ~3 decimal digits.
|
|
4
4
|
|
|
5
|
-
local Channel = require
|
|
6
|
-
local Types = require
|
|
5
|
+
local Channel = require(script.Parent.Parent.Parent.internal.Channel)
|
|
6
|
+
local Types = require(script.Parent.Parent.Parent.Types)
|
|
7
7
|
|
|
8
8
|
local alloc = Channel.alloc
|
|
9
9
|
|
|
10
10
|
type ChannelState = Types.ChannelState
|
|
11
11
|
|
|
12
|
-
-- Constants
|
|
12
|
+
-- Constants -----------------------------------------------------------
|
|
13
13
|
|
|
14
14
|
local MAX_HALF = 65504
|
|
15
15
|
local BIAS = 15
|
|
@@ -29,9 +29,9 @@ local ldexp = math.ldexp
|
|
|
29
29
|
local round = math.round
|
|
30
30
|
local huge = math.huge
|
|
31
31
|
|
|
32
|
-
-- Private
|
|
32
|
+
-- Private -------------------------------------------------------------
|
|
33
33
|
|
|
34
|
-
local function encode
|
|
34
|
+
local function encode(value: number): number
|
|
35
35
|
if value ~= value then
|
|
36
36
|
return NAN_BITS
|
|
37
37
|
end
|
|
@@ -46,10 +46,10 @@ local function encode (value: number): number
|
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
if value > MAX_HALF then
|
|
49
|
-
return bor
|
|
49
|
+
return bor(sign, INF_BITS)
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
-
local mantissa, exponent = frexp
|
|
52
|
+
local mantissa, exponent = frexp(value)
|
|
53
53
|
exponent += 14
|
|
54
54
|
|
|
55
55
|
local bits: number
|
|
@@ -58,55 +58,55 @@ local function encode (value: number): number
|
|
|
58
58
|
if shift < 0 then
|
|
59
59
|
return sign
|
|
60
60
|
end
|
|
61
|
-
bits = round
|
|
61
|
+
bits = round(mantissa * lshift(1, shift))
|
|
62
62
|
elseif exponent >= EXP_MAX then
|
|
63
63
|
bits = INF_BITS
|
|
64
64
|
else
|
|
65
|
-
bits = bor
|
|
65
|
+
bits = bor(lshift(exponent, MANT_BITS), round((mantissa * 2 - 1) * MANT_SCALE))
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
-
return bor
|
|
68
|
+
return bor(sign, bits)
|
|
69
69
|
end
|
|
70
70
|
|
|
71
|
-
local function decode
|
|
72
|
-
local exponent = band
|
|
73
|
-
local mantissa = band
|
|
71
|
+
local function decode(packed: number): number
|
|
72
|
+
local exponent = band(rshift(packed, MANT_BITS), 0x1F)
|
|
73
|
+
local mantissa = band(packed, 0x03FF)
|
|
74
74
|
|
|
75
75
|
local result: number
|
|
76
76
|
if exponent == 0 then
|
|
77
|
-
result = if mantissa == 0 then 0 else ldexp
|
|
77
|
+
result = if mantissa == 0 then 0 else ldexp(mantissa / MANT_SCALE, 1 - BIAS)
|
|
78
78
|
elseif exponent == EXP_MAX then
|
|
79
79
|
result = if mantissa == 0 then huge else 0 / 0
|
|
80
80
|
else
|
|
81
|
-
result = ldexp
|
|
81
|
+
result = ldexp(mantissa / MANT_SCALE + 1, exponent - BIAS)
|
|
82
82
|
end
|
|
83
83
|
|
|
84
|
-
return if band
|
|
84
|
+
return if band(packed, SIGN_BIT) ~= 0 then -result else result
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
-
-- Public
|
|
87
|
+
-- Public --------------------------------------------------------------
|
|
88
88
|
|
|
89
89
|
local Float16 = {}
|
|
90
90
|
|
|
91
|
-
Float16.f16 = table.freeze
|
|
91
|
+
Float16.f16 = table.freeze({
|
|
92
92
|
_size = 2,
|
|
93
|
-
_directWrite = function
|
|
94
|
-
buffer.writeu16
|
|
93
|
+
_directWrite = function(b: buffer, offset: number, value: number): ()
|
|
94
|
+
buffer.writeu16(b, offset, encode(value))
|
|
95
95
|
end,
|
|
96
|
-
_directRead = function
|
|
97
|
-
return decode
|
|
96
|
+
_directRead = function(b: buffer, offset: number): number
|
|
97
|
+
return decode(buffer.readu16(b, offset))
|
|
98
98
|
end,
|
|
99
|
-
write = function
|
|
99
|
+
write = function(ch: ChannelState, value: number): ()
|
|
100
100
|
local c = ch.cursor
|
|
101
101
|
if c + 2 > ch.size then
|
|
102
|
-
alloc
|
|
102
|
+
alloc(ch, 2)
|
|
103
103
|
end
|
|
104
|
-
buffer.writeu16
|
|
104
|
+
buffer.writeu16(ch.buff, c, encode(value))
|
|
105
105
|
ch.cursor = c + 2
|
|
106
106
|
end,
|
|
107
|
-
read = function
|
|
108
|
-
return decode
|
|
107
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): (number, number)
|
|
108
|
+
return decode(buffer.readu16(src, pos)), 2
|
|
109
109
|
end,
|
|
110
110
|
})
|
|
111
111
|
|
|
112
|
-
return table.freeze
|
|
112
|
+
return table.freeze(Float16)
|
|
@@ -2,47 +2,47 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- u8, u16, u32, i8, i16, i32, f32, f64 codecs.
|
|
4
4
|
|
|
5
|
-
local Base = require
|
|
5
|
+
local Base = require(script.Parent.Parent.Base)
|
|
6
6
|
|
|
7
|
-
return table.freeze
|
|
8
|
-
u8 = Base.define
|
|
9
|
-
buffer.writeu8
|
|
10
|
-
end, function
|
|
11
|
-
return buffer.readu8
|
|
12
|
-
end),
|
|
13
|
-
u16 = Base.define
|
|
14
|
-
buffer.writeu16
|
|
15
|
-
end, function
|
|
16
|
-
return buffer.readu16
|
|
17
|
-
end),
|
|
18
|
-
u32 = Base.define
|
|
19
|
-
buffer.writeu32
|
|
20
|
-
end, function
|
|
21
|
-
return buffer.readu32
|
|
22
|
-
end),
|
|
23
|
-
i8 = Base.define
|
|
24
|
-
buffer.writei8
|
|
25
|
-
end, function
|
|
26
|
-
return buffer.readi8
|
|
27
|
-
end),
|
|
28
|
-
i16 = Base.define
|
|
29
|
-
buffer.writei16
|
|
30
|
-
end, function
|
|
31
|
-
return buffer.readi16
|
|
32
|
-
end),
|
|
33
|
-
i32 = Base.define
|
|
34
|
-
buffer.writei32
|
|
35
|
-
end, function
|
|
36
|
-
return buffer.readi32
|
|
37
|
-
end),
|
|
38
|
-
f32 = Base.define
|
|
39
|
-
buffer.writef32
|
|
40
|
-
end, function
|
|
41
|
-
return buffer.readf32
|
|
42
|
-
end),
|
|
43
|
-
f64 = Base.define
|
|
44
|
-
buffer.writef64
|
|
45
|
-
end, function
|
|
46
|
-
return buffer.readf64
|
|
7
|
+
return table.freeze({
|
|
8
|
+
u8 = Base.define(1, function(b: buffer, o: number, v: number): ()
|
|
9
|
+
buffer.writeu8(b, o, v)
|
|
10
|
+
end, function(b: buffer, o: number): number
|
|
11
|
+
return buffer.readu8(b, o)
|
|
12
|
+
end),
|
|
13
|
+
u16 = Base.define(2, function(b: buffer, o: number, v: number): ()
|
|
14
|
+
buffer.writeu16(b, o, v)
|
|
15
|
+
end, function(b: buffer, o: number): number
|
|
16
|
+
return buffer.readu16(b, o)
|
|
17
|
+
end),
|
|
18
|
+
u32 = Base.define(4, function(b: buffer, o: number, v: number): ()
|
|
19
|
+
buffer.writeu32(b, o, v)
|
|
20
|
+
end, function(b: buffer, o: number): number
|
|
21
|
+
return buffer.readu32(b, o)
|
|
22
|
+
end),
|
|
23
|
+
i8 = Base.define(1, function(b: buffer, o: number, v: number): ()
|
|
24
|
+
buffer.writei8(b, o, v)
|
|
25
|
+
end, function(b: buffer, o: number): number
|
|
26
|
+
return buffer.readi8(b, o)
|
|
27
|
+
end),
|
|
28
|
+
i16 = Base.define(2, function(b: buffer, o: number, v: number): ()
|
|
29
|
+
buffer.writei16(b, o, v)
|
|
30
|
+
end, function(b: buffer, o: number): number
|
|
31
|
+
return buffer.readi16(b, o)
|
|
32
|
+
end),
|
|
33
|
+
i32 = Base.define(4, function(b: buffer, o: number, v: number): ()
|
|
34
|
+
buffer.writei32(b, o, v)
|
|
35
|
+
end, function(b: buffer, o: number): number
|
|
36
|
+
return buffer.readi32(b, o)
|
|
37
|
+
end),
|
|
38
|
+
f32 = Base.define(4, function(b: buffer, o: number, v: number): ()
|
|
39
|
+
buffer.writef32(b, o, v)
|
|
40
|
+
end, function(b: buffer, o: number): number
|
|
41
|
+
return buffer.readf32(b, o)
|
|
42
|
+
end),
|
|
43
|
+
f64 = Base.define(8, function(b: buffer, o: number, v: number): ()
|
|
44
|
+
buffer.writef64(b, o, v)
|
|
45
|
+
end, function(b: buffer, o: number): number
|
|
46
|
+
return buffer.readf64(b, o)
|
|
47
47
|
end),
|
|
48
48
|
})
|