@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
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Discriminated union codec. u8 variant tag + variant payload.
|
|
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>
|
|
@@ -11,42 +11,42 @@ type InternalCodec<T> = Types.InternalCodec<T>
|
|
|
11
11
|
|
|
12
12
|
local alloc = Channel.alloc
|
|
13
13
|
|
|
14
|
-
-- Public
|
|
14
|
+
-- Public --------------------------------------------------------------
|
|
15
15
|
|
|
16
16
|
local Tagged = {}
|
|
17
17
|
|
|
18
18
|
-- Read injects tagField into the decoded table. Variant codecs must return mutable tables.
|
|
19
|
-
function Tagged.define
|
|
19
|
+
function Tagged.define(tagField: string, variants: { [string]: Codec<any> }): Codec<any>
|
|
20
20
|
if #tagField == 0 then
|
|
21
|
-
error
|
|
21
|
+
error("[Lync] Tagged union tagField must not be empty")
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
local names = {} :: { string }
|
|
25
25
|
for name in variants do
|
|
26
26
|
if #name == 0 then
|
|
27
|
-
error
|
|
27
|
+
error("[Lync] Tagged union variant name must not be empty")
|
|
28
28
|
end
|
|
29
|
-
table.insert
|
|
29
|
+
table.insert(names, name)
|
|
30
30
|
end
|
|
31
|
-
table.sort
|
|
31
|
+
table.sort(names)
|
|
32
32
|
|
|
33
33
|
local count = #names
|
|
34
34
|
if count == 0 then
|
|
35
|
-
error
|
|
35
|
+
error("[Lync] Tagged union requires at least one variant")
|
|
36
36
|
end
|
|
37
37
|
if count > 256 then
|
|
38
|
-
error
|
|
38
|
+
error(`[Lync] Tagged union exceeds 256 variants: {count}`)
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
local nameToTag = {} :: { [string]: number }
|
|
42
|
-
local tagCodecs = table.create
|
|
43
|
-
local tagNames = table.create
|
|
44
|
-
local tagSizes = table.create
|
|
42
|
+
local tagCodecs = table.create(count) :: { Codec<any> }
|
|
43
|
+
local tagNames = table.create(count) :: { string }
|
|
44
|
+
local tagSizes = table.create(count) :: { number? }
|
|
45
45
|
|
|
46
46
|
local allDirect = true
|
|
47
47
|
local uniformSize: number? = (variants[names[1]] :: InternalCodec<any>)._size
|
|
48
|
-
local directWrites = table.create
|
|
49
|
-
local directReads = table.create
|
|
48
|
+
local directWrites = table.create(count) :: { any }
|
|
49
|
+
local directReads = table.create(count) :: { any }
|
|
50
50
|
|
|
51
51
|
for i, name in names do
|
|
52
52
|
local codec = variants[name] :: InternalCodec<any>
|
|
@@ -71,63 +71,63 @@ function Tagged.define (tagField: string, variants: { [string]: Codec<any> }): C
|
|
|
71
71
|
end
|
|
72
72
|
end
|
|
73
73
|
|
|
74
|
-
table.freeze
|
|
75
|
-
table.freeze
|
|
76
|
-
table.freeze
|
|
77
|
-
table.freeze
|
|
74
|
+
table.freeze(nameToTag)
|
|
75
|
+
table.freeze(tagCodecs)
|
|
76
|
+
table.freeze(tagNames)
|
|
77
|
+
table.freeze(tagSizes)
|
|
78
78
|
|
|
79
79
|
local fixedSize: number? = if uniformSize then 1 + uniformSize else nil
|
|
80
80
|
|
|
81
81
|
if allDirect then
|
|
82
|
-
table.freeze
|
|
83
|
-
table.freeze
|
|
82
|
+
table.freeze(directWrites)
|
|
83
|
+
table.freeze(directReads)
|
|
84
84
|
|
|
85
85
|
-- If uniform size, expose _directWrite/_directRead for parent struct/array
|
|
86
86
|
if fixedSize then
|
|
87
|
-
return table.freeze
|
|
87
|
+
return table.freeze({
|
|
88
88
|
_size = fixedSize,
|
|
89
|
-
_directWrite = function
|
|
89
|
+
_directWrite = function(b: buffer, offset: number, value: any): ()
|
|
90
90
|
local tag = nameToTag[value[tagField]]
|
|
91
91
|
if not tag then
|
|
92
|
-
error
|
|
92
|
+
error(`[Lync] Unknown variant: {value[tagField]}`)
|
|
93
93
|
end
|
|
94
|
-
buffer.writeu8
|
|
95
|
-
directWrites[tag]
|
|
94
|
+
buffer.writeu8(b, offset, tag - 1)
|
|
95
|
+
directWrites[tag](b, offset + 1, value)
|
|
96
96
|
end,
|
|
97
|
-
_directRead = function
|
|
98
|
-
local tag = buffer.readu8
|
|
97
|
+
_directRead = function(b: buffer, offset: number): any
|
|
98
|
+
local tag = buffer.readu8(b, offset) + 1
|
|
99
99
|
local name = tagNames[tag]
|
|
100
100
|
if not name then
|
|
101
|
-
error
|
|
101
|
+
error(`[Lync] Unknown variant index: {tag - 1}`)
|
|
102
102
|
end
|
|
103
|
-
local data = directReads[tag]
|
|
104
|
-
if type
|
|
103
|
+
local data = directReads[tag](b, offset + 1)
|
|
104
|
+
if type(data) == "table" then
|
|
105
105
|
data[tagField] = name
|
|
106
106
|
end
|
|
107
107
|
return data
|
|
108
108
|
end,
|
|
109
|
-
write = function
|
|
109
|
+
write = function(ch: ChannelState, value: any): ()
|
|
110
110
|
local tag = nameToTag[value[tagField]]
|
|
111
111
|
if not tag then
|
|
112
|
-
error
|
|
112
|
+
error(`[Lync] Unknown variant: {value[tagField]}`)
|
|
113
113
|
end
|
|
114
114
|
local c = ch.cursor
|
|
115
115
|
if c + (fixedSize :: number) > ch.size then
|
|
116
|
-
alloc
|
|
116
|
+
alloc(ch, fixedSize :: number)
|
|
117
117
|
end
|
|
118
118
|
local b = ch.buff
|
|
119
|
-
buffer.writeu8
|
|
120
|
-
directWrites[tag]
|
|
119
|
+
buffer.writeu8(b, c, tag - 1)
|
|
120
|
+
directWrites[tag](b, c + 1, value)
|
|
121
121
|
ch.cursor = c + (fixedSize :: number)
|
|
122
122
|
end,
|
|
123
|
-
read = function
|
|
124
|
-
local tag = buffer.readu8
|
|
123
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): (any, number)
|
|
124
|
+
local tag = buffer.readu8(src, pos) + 1
|
|
125
125
|
local name = tagNames[tag]
|
|
126
126
|
if not name then
|
|
127
|
-
error
|
|
127
|
+
error(`[Lync] Unknown variant index: {tag - 1}`)
|
|
128
128
|
end
|
|
129
|
-
local data = directReads[tag]
|
|
130
|
-
if type
|
|
129
|
+
local data = directReads[tag](src, pos + 1)
|
|
130
|
+
if type(data) == "table" then
|
|
131
131
|
data[tagField] = name
|
|
132
132
|
end
|
|
133
133
|
return data, fixedSize :: number
|
|
@@ -136,35 +136,35 @@ function Tagged.define (tagField: string, variants: { [string]: Codec<any> }): C
|
|
|
136
136
|
end
|
|
137
137
|
|
|
138
138
|
-- Non-uniform sizes but all have direct: use direct dispatch internally
|
|
139
|
-
local fieldSizes = table.clone
|
|
140
|
-
table.freeze
|
|
139
|
+
local fieldSizes = table.clone(tagSizes) :: { number }
|
|
140
|
+
table.freeze(fieldSizes)
|
|
141
141
|
|
|
142
|
-
return table.freeze
|
|
142
|
+
return table.freeze({
|
|
143
143
|
_size = nil,
|
|
144
|
-
write = function
|
|
144
|
+
write = function(ch: ChannelState, value: any): ()
|
|
145
145
|
local tag = nameToTag[value[tagField]]
|
|
146
146
|
if not tag then
|
|
147
|
-
error
|
|
147
|
+
error(`[Lync] Unknown variant: {value[tagField]}`)
|
|
148
148
|
end
|
|
149
149
|
local varSize = fieldSizes[tag]
|
|
150
150
|
local totalNeeded = 1 + varSize
|
|
151
151
|
local c = ch.cursor
|
|
152
152
|
if c + totalNeeded > ch.size then
|
|
153
|
-
alloc
|
|
153
|
+
alloc(ch, totalNeeded)
|
|
154
154
|
end
|
|
155
155
|
local b = ch.buff
|
|
156
|
-
buffer.writeu8
|
|
157
|
-
directWrites[tag]
|
|
156
|
+
buffer.writeu8(b, c, tag - 1)
|
|
157
|
+
directWrites[tag](b, c + 1, value)
|
|
158
158
|
ch.cursor = c + totalNeeded
|
|
159
159
|
end,
|
|
160
|
-
read = function
|
|
161
|
-
local tag = buffer.readu8
|
|
160
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): (any, number)
|
|
161
|
+
local tag = buffer.readu8(src, pos) + 1
|
|
162
162
|
local name = tagNames[tag]
|
|
163
163
|
if not name then
|
|
164
|
-
error
|
|
164
|
+
error(`[Lync] Unknown variant index: {tag - 1}`)
|
|
165
165
|
end
|
|
166
|
-
local data = directReads[tag]
|
|
167
|
-
if type
|
|
166
|
+
local data = directReads[tag](src, pos + 1)
|
|
167
|
+
if type(data) == "table" then
|
|
168
168
|
data[tagField] = name
|
|
169
169
|
end
|
|
170
170
|
return data, 1 + fieldSizes[tag]
|
|
@@ -172,45 +172,45 @@ function Tagged.define (tagField: string, variants: { [string]: Codec<any> }): C
|
|
|
172
172
|
})
|
|
173
173
|
end
|
|
174
174
|
|
|
175
|
-
local writeFns = table.create
|
|
176
|
-
local readFns = table.create
|
|
175
|
+
local writeFns = table.create(count) :: { any }
|
|
176
|
+
local readFns = table.create(count) :: { any }
|
|
177
177
|
for i = 1, count do
|
|
178
178
|
writeFns[i] = tagCodecs[i].write
|
|
179
179
|
readFns[i] = tagCodecs[i].read
|
|
180
180
|
end
|
|
181
|
-
table.freeze
|
|
182
|
-
table.freeze
|
|
181
|
+
table.freeze(writeFns)
|
|
182
|
+
table.freeze(readFns)
|
|
183
183
|
|
|
184
|
-
return table.freeze
|
|
184
|
+
return table.freeze({
|
|
185
185
|
_size = fixedSize,
|
|
186
|
-
write = function
|
|
186
|
+
write = function(ch: ChannelState, value: any): ()
|
|
187
187
|
local tag = nameToTag[value[tagField]]
|
|
188
188
|
if not tag then
|
|
189
|
-
error
|
|
189
|
+
error(`[Lync] Unknown variant: {value[tagField]}`)
|
|
190
190
|
end
|
|
191
191
|
|
|
192
192
|
local variantSize = tagSizes[tag]
|
|
193
193
|
if variantSize then
|
|
194
|
-
alloc
|
|
194
|
+
alloc(ch, 1 + variantSize)
|
|
195
195
|
else
|
|
196
|
-
alloc
|
|
196
|
+
alloc(ch, 1)
|
|
197
197
|
end
|
|
198
198
|
|
|
199
199
|
local c = ch.cursor
|
|
200
|
-
buffer.writeu8
|
|
200
|
+
buffer.writeu8(ch.buff, c, tag - 1)
|
|
201
201
|
ch.cursor = c + 1
|
|
202
|
-
writeFns[tag]
|
|
202
|
+
writeFns[tag](ch, value)
|
|
203
203
|
end,
|
|
204
|
-
read = function
|
|
205
|
-
local tag = buffer.readu8
|
|
204
|
+
read = function(src: buffer, pos: number, refs: { Instance }?): (any, number)
|
|
205
|
+
local tag = buffer.readu8(src, pos) + 1
|
|
206
206
|
local name = tagNames[tag]
|
|
207
207
|
if not name then
|
|
208
|
-
error
|
|
208
|
+
error(`[Lync] Unknown variant index: {tag - 1}`)
|
|
209
209
|
end
|
|
210
210
|
|
|
211
|
-
local data, bytes = readFns[tag]
|
|
211
|
+
local data, bytes = readFns[tag](src, pos + 1, refs)
|
|
212
212
|
|
|
213
|
-
if type
|
|
213
|
+
if type(data) == "table" then
|
|
214
214
|
data[tagField] = name
|
|
215
215
|
end
|
|
216
216
|
|
|
@@ -219,4 +219,4 @@ function Tagged.define (tagField: string, variants: { [string]: Codec<any> }): C
|
|
|
219
219
|
})
|
|
220
220
|
end
|
|
221
221
|
|
|
222
|
-
return table.freeze
|
|
222
|
+
return table.freeze(Tagged)
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Positional ordered codec. Like struct but indexed, not keyed.
|
|
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>
|
|
@@ -11,24 +11,24 @@ type InternalCodec<T> = Types.InternalCodec<T>
|
|
|
11
11
|
|
|
12
12
|
local alloc = Channel.alloc
|
|
13
13
|
|
|
14
|
-
-- Public
|
|
14
|
+
-- Public --------------------------------------------------------------
|
|
15
15
|
|
|
16
16
|
local Tuple = {}
|
|
17
17
|
|
|
18
|
-
function Tuple.define
|
|
18
|
+
function Tuple.define(...: Codec<any>): Codec<{ any }>
|
|
19
19
|
local codecs = { ... }
|
|
20
20
|
local count = #codecs
|
|
21
21
|
if count == 0 then
|
|
22
|
-
error
|
|
22
|
+
error("[Lync] Tuple requires at least one codec")
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
table.freeze
|
|
25
|
+
table.freeze(codecs)
|
|
26
26
|
|
|
27
27
|
local canDirect = true
|
|
28
28
|
local totalSize: number = 0
|
|
29
|
-
local directWrites = table.create
|
|
30
|
-
local directReads = table.create
|
|
31
|
-
local fieldSizes = table.create
|
|
29
|
+
local directWrites = table.create(count) :: { any }
|
|
30
|
+
local directReads = table.create(count) :: { any }
|
|
31
|
+
local fieldSizes = table.create(count) :: { number }
|
|
32
32
|
|
|
33
33
|
for i = 1, count do
|
|
34
34
|
local internal = codecs[i] :: InternalCodec<any>
|
|
@@ -67,72 +67,72 @@ function Tuple.define (...: Codec<any>): Codec<{ any }>
|
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
if canDirect then
|
|
70
|
-
table.freeze
|
|
71
|
-
table.freeze
|
|
72
|
-
table.freeze
|
|
70
|
+
table.freeze(directWrites)
|
|
71
|
+
table.freeze(directReads)
|
|
72
|
+
table.freeze(fieldSizes)
|
|
73
73
|
|
|
74
|
-
local function directWriteBody
|
|
74
|
+
local function directWriteBody(b: buffer, c: number, value: { any }): ()
|
|
75
75
|
for i = 1, count do
|
|
76
|
-
directWrites[i]
|
|
76
|
+
directWrites[i](b, c, value[i])
|
|
77
77
|
c += fieldSizes[i]
|
|
78
78
|
end
|
|
79
79
|
end
|
|
80
80
|
|
|
81
|
-
local function directReadBody
|
|
82
|
-
local result = table.create
|
|
81
|
+
local function directReadBody(b: buffer, pos: number): { any }
|
|
82
|
+
local result = table.create(count)
|
|
83
83
|
local c = pos
|
|
84
84
|
for i = 1, count do
|
|
85
|
-
result[i] = directReads[i]
|
|
85
|
+
result[i] = directReads[i](b, c)
|
|
86
86
|
c += fieldSizes[i]
|
|
87
87
|
end
|
|
88
88
|
return result
|
|
89
89
|
end
|
|
90
90
|
|
|
91
|
-
return table.freeze
|
|
91
|
+
return table.freeze({
|
|
92
92
|
_size = totalSize,
|
|
93
93
|
_directWrite = directWriteBody,
|
|
94
94
|
_directRead = directReadBody,
|
|
95
|
-
write = function
|
|
95
|
+
write = function(ch: ChannelState, value: { any }): ()
|
|
96
96
|
local c = ch.cursor
|
|
97
97
|
if c + totalSize > ch.size then
|
|
98
|
-
alloc
|
|
98
|
+
alloc(ch, totalSize)
|
|
99
99
|
end
|
|
100
|
-
directWriteBody
|
|
100
|
+
directWriteBody(ch.buff, c, value)
|
|
101
101
|
ch.cursor = c + totalSize
|
|
102
102
|
end,
|
|
103
|
-
read = function
|
|
104
|
-
return directReadBody
|
|
103
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): ({ any }, number)
|
|
104
|
+
return directReadBody(src, pos), totalSize
|
|
105
105
|
end,
|
|
106
106
|
})
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
-
local writeFns = table.create
|
|
110
|
-
local readFns = table.create
|
|
109
|
+
local writeFns = table.create(count) :: { any }
|
|
110
|
+
local readFns = table.create(count) :: { any }
|
|
111
111
|
for i = 1, count do
|
|
112
112
|
writeFns[i] = codecs[i].write
|
|
113
113
|
readFns[i] = codecs[i].read
|
|
114
114
|
end
|
|
115
|
-
table.freeze
|
|
116
|
-
table.freeze
|
|
115
|
+
table.freeze(writeFns)
|
|
116
|
+
table.freeze(readFns)
|
|
117
117
|
|
|
118
118
|
local hasFixedTotal = totalSize ~= nil
|
|
119
119
|
|
|
120
|
-
return table.freeze
|
|
120
|
+
return table.freeze({
|
|
121
121
|
_size = totalSize,
|
|
122
|
-
write = function
|
|
122
|
+
write = function(ch: ChannelState, value: { any }): ()
|
|
123
123
|
if hasFixedTotal then
|
|
124
|
-
alloc
|
|
124
|
+
alloc(ch, totalSize :: number)
|
|
125
125
|
end
|
|
126
126
|
for i = 1, count do
|
|
127
|
-
writeFns[i]
|
|
127
|
+
writeFns[i](ch, value[i])
|
|
128
128
|
end
|
|
129
129
|
end,
|
|
130
|
-
read = function
|
|
131
|
-
local result = table.create
|
|
130
|
+
read = function(src: buffer, pos: number, refs: { Instance }?): ({ any }, number)
|
|
131
|
+
local result = table.create(count)
|
|
132
132
|
local absPos = pos
|
|
133
133
|
|
|
134
134
|
for i = 1, count do
|
|
135
|
-
local value, n = readFns[i]
|
|
135
|
+
local value, n = readFns[i](src, absPos, refs)
|
|
136
136
|
result[i] = value
|
|
137
137
|
absPos += n
|
|
138
138
|
end
|
|
@@ -142,4 +142,4 @@ function Tuple.define (...: Codec<any>): Codec<{ any }>
|
|
|
142
142
|
})
|
|
143
143
|
end
|
|
144
144
|
|
|
145
|
-
return table.freeze
|
|
145
|
+
return table.freeze(Tuple)
|
|
@@ -2,44 +2,44 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Raw buffer codec with varint length prefix.
|
|
4
4
|
|
|
5
|
-
local Channel = require
|
|
6
|
-
local Types = require
|
|
7
|
-
local Varint = require
|
|
5
|
+
local Channel = require(script.Parent.Parent.Parent.internal.Channel)
|
|
6
|
+
local Types = require(script.Parent.Parent.Parent.Types)
|
|
7
|
+
local Varint = require(script.Parent.Parent.primitive.Varint)
|
|
8
8
|
|
|
9
9
|
type ChannelState = Types.ChannelState
|
|
10
10
|
|
|
11
11
|
local alloc = Channel.alloc
|
|
12
12
|
|
|
13
|
-
-- Public
|
|
13
|
+
-- Public --------------------------------------------------------------
|
|
14
14
|
|
|
15
15
|
local Buffer = {}
|
|
16
16
|
|
|
17
|
-
Buffer.buff = table.freeze
|
|
18
|
-
write = function
|
|
19
|
-
local len = buffer.len
|
|
20
|
-
Varint.write
|
|
17
|
+
Buffer.buff = table.freeze({
|
|
18
|
+
write = function(ch: ChannelState, value: buffer): ()
|
|
19
|
+
local len = buffer.len(value)
|
|
20
|
+
Varint.write(ch, len)
|
|
21
21
|
local c = ch.cursor
|
|
22
22
|
if c + len > ch.size then
|
|
23
|
-
alloc
|
|
23
|
+
alloc(ch, len)
|
|
24
24
|
end
|
|
25
|
-
buffer.copy
|
|
25
|
+
buffer.copy(ch.buff, c, value, 0, len)
|
|
26
26
|
ch.cursor = c + len
|
|
27
27
|
end,
|
|
28
|
-
read = function
|
|
29
|
-
local len, lenBytes = Varint.read
|
|
28
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): (buffer, number)
|
|
29
|
+
local len, lenBytes = Varint.read(src, pos)
|
|
30
30
|
if len == 0 then
|
|
31
|
-
return buffer.create
|
|
31
|
+
return buffer.create(0), lenBytes
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
-
local remaining = buffer.len
|
|
34
|
+
local remaining = buffer.len(src) - pos - lenBytes
|
|
35
35
|
if len > remaining then
|
|
36
|
-
error
|
|
36
|
+
error(`[Lync] Buffer length exceeds remaining: {len} > {remaining}`)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
local out = buffer.create
|
|
40
|
-
buffer.copy
|
|
39
|
+
local out = buffer.create(len)
|
|
40
|
+
buffer.copy(out, 0, src, pos + lenBytes, len)
|
|
41
41
|
return out, lenBytes + len
|
|
42
42
|
end,
|
|
43
43
|
})
|
|
44
44
|
|
|
45
|
-
return table.freeze
|
|
45
|
+
return table.freeze(Buffer)
|
|
@@ -2,51 +2,51 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Axis-angle CFrame codec. Zero axis at identity for clean deltas.
|
|
4
4
|
|
|
5
|
-
local Base = require
|
|
5
|
+
local Base = require(script.Parent.Parent.Base)
|
|
6
6
|
|
|
7
7
|
local sqrt = math.sqrt
|
|
8
8
|
|
|
9
9
|
-- f32 machine epsilon * 2 to absorb round-trip noise.
|
|
10
10
|
local EPSILON = 2.4e-7
|
|
11
11
|
|
|
12
|
-
local function writeCFrame
|
|
12
|
+
local function writeCFrame(b: buffer, c: number, value: CFrame): ()
|
|
13
13
|
local position = value.Position
|
|
14
|
-
local axis, angle = value:ToAxisAngle
|
|
14
|
+
local axis, angle = value:ToAxisAngle()
|
|
15
15
|
|
|
16
|
-
buffer.writef32
|
|
17
|
-
buffer.writef32
|
|
18
|
-
buffer.writef32
|
|
16
|
+
buffer.writef32(b, c, position.X)
|
|
17
|
+
buffer.writef32(b, c + 4, position.Y)
|
|
18
|
+
buffer.writef32(b, c + 8, position.Z)
|
|
19
19
|
|
|
20
20
|
if angle < EPSILON then
|
|
21
|
-
buffer.writef32
|
|
22
|
-
buffer.writef32
|
|
23
|
-
buffer.writef32
|
|
21
|
+
buffer.writef32(b, c + 12, 0)
|
|
22
|
+
buffer.writef32(b, c + 16, 0)
|
|
23
|
+
buffer.writef32(b, c + 20, 0)
|
|
24
24
|
else
|
|
25
|
-
buffer.writef32
|
|
26
|
-
buffer.writef32
|
|
27
|
-
buffer.writef32
|
|
25
|
+
buffer.writef32(b, c + 12, axis.X * angle)
|
|
26
|
+
buffer.writef32(b, c + 16, axis.Y * angle)
|
|
27
|
+
buffer.writef32(b, c + 20, axis.Z * angle)
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
-
local function readCFrame
|
|
32
|
-
local px = buffer.readf32
|
|
33
|
-
local py = buffer.readf32
|
|
34
|
-
local pz = buffer.readf32
|
|
35
|
-
local rx = buffer.readf32
|
|
36
|
-
local ry = buffer.readf32
|
|
37
|
-
local rz = buffer.readf32
|
|
31
|
+
local function readCFrame(b: buffer, pos: number): CFrame
|
|
32
|
+
local px = buffer.readf32(b, pos)
|
|
33
|
+
local py = buffer.readf32(b, pos + 4)
|
|
34
|
+
local pz = buffer.readf32(b, pos + 8)
|
|
35
|
+
local rx = buffer.readf32(b, pos + 12)
|
|
36
|
+
local ry = buffer.readf32(b, pos + 16)
|
|
37
|
+
local rz = buffer.readf32(b, pos + 20)
|
|
38
38
|
|
|
39
|
-
local angle = sqrt
|
|
40
|
-
local origin = CFrame.new
|
|
39
|
+
local angle = sqrt(rx * rx + ry * ry + rz * rz)
|
|
40
|
+
local origin = CFrame.new(px, py, pz)
|
|
41
41
|
|
|
42
42
|
if angle < EPSILON then
|
|
43
43
|
return origin
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
local inv = 1 / angle
|
|
47
|
-
return origin * CFrame.fromAxisAngle
|
|
47
|
+
return origin * CFrame.fromAxisAngle(Vector3.new(rx * inv, ry * inv, rz * inv), angle)
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
return table.freeze
|
|
51
|
-
cframe = Base.define
|
|
50
|
+
return table.freeze({
|
|
51
|
+
cframe = Base.define(24, writeCFrame, readCFrame),
|
|
52
52
|
})
|
|
@@ -2,21 +2,17 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Color3 codec. 3 bytes, 0-255 per channel, clamped.
|
|
4
4
|
|
|
5
|
-
local Base = require
|
|
5
|
+
local Base = require(script.Parent.Parent.Base)
|
|
6
6
|
|
|
7
7
|
local round = math.round
|
|
8
8
|
local clamp = math.clamp
|
|
9
9
|
|
|
10
|
-
return table.freeze
|
|
11
|
-
color3 = Base.define
|
|
12
|
-
buffer.writeu8
|
|
13
|
-
buffer.writeu8
|
|
14
|
-
buffer.writeu8
|
|
15
|
-
end, function
|
|
16
|
-
return Color3.fromRGB (
|
|
17
|
-
buffer.readu8 (b, o),
|
|
18
|
-
buffer.readu8 (b, o + 1),
|
|
19
|
-
buffer.readu8 (b, o + 2)
|
|
20
|
-
)
|
|
10
|
+
return table.freeze({
|
|
11
|
+
color3 = Base.define(3, function(b: buffer, o: number, v: Color3): ()
|
|
12
|
+
buffer.writeu8(b, o, round(clamp(v.R, 0, 1) * 255))
|
|
13
|
+
buffer.writeu8(b, o + 1, round(clamp(v.G, 0, 1) * 255))
|
|
14
|
+
buffer.writeu8(b, o + 2, round(clamp(v.B, 0, 1) * 255))
|
|
15
|
+
end, function(b: buffer, o: number): Color3
|
|
16
|
+
return Color3.fromRGB(buffer.readu8(b, o), buffer.readu8(b, o + 1), buffer.readu8(b, o + 2))
|
|
21
17
|
end),
|
|
22
18
|
})
|
|
@@ -2,48 +2,48 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Instance codec via sidecar reference array.
|
|
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
|
-
-- Public
|
|
12
|
+
-- Public --------------------------------------------------------------
|
|
13
13
|
|
|
14
14
|
local InstanceCodec = {}
|
|
15
15
|
|
|
16
|
-
InstanceCodec.inst = table.freeze
|
|
16
|
+
InstanceCodec.inst = table.freeze({
|
|
17
17
|
_size = 2,
|
|
18
|
-
write = function
|
|
18
|
+
write = function(ch: ChannelState, value: Instance): ()
|
|
19
19
|
local refs = ch.refs
|
|
20
20
|
local idx = #refs + 1
|
|
21
21
|
if idx > 65535 then
|
|
22
|
-
error
|
|
22
|
+
error(`[Lync] Instance ref overflow: {idx} exceeds u16 max`)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
refs[idx] = value
|
|
26
26
|
|
|
27
27
|
local c = ch.cursor
|
|
28
28
|
if c + 2 > ch.size then
|
|
29
|
-
alloc
|
|
29
|
+
alloc(ch, 2)
|
|
30
30
|
end
|
|
31
|
-
buffer.writeu16
|
|
31
|
+
buffer.writeu16(ch.buff, c, idx)
|
|
32
32
|
ch.cursor = c + 2
|
|
33
33
|
end,
|
|
34
|
-
read = function
|
|
35
|
-
local idx = buffer.readu16
|
|
34
|
+
read = function(src: buffer, pos: number, refs: { Instance }?): (Instance, number)
|
|
35
|
+
local idx = buffer.readu16(src, pos)
|
|
36
36
|
if not refs then
|
|
37
|
-
error
|
|
37
|
+
error("[Lync] Instance read requires refs array")
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
local inst = refs[idx]
|
|
41
41
|
if not inst then
|
|
42
|
-
error
|
|
42
|
+
error(`[Lync] Instance ref index out of bounds: {idx}`)
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
return inst, 2
|
|
46
46
|
end,
|
|
47
47
|
})
|
|
48
48
|
|
|
49
|
-
return table.freeze
|
|
49
|
+
return table.freeze(InstanceCodec)
|