-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDecode.lua
More file actions
419 lines (379 loc) · 11.5 KB
/
Decode.lua
File metadata and controls
419 lines (379 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
local module = {}
local ti = table.insert
local b3ba = bit32.band
local b3ls = bit32.lshift
local b3rs = bit32.rshift
local buru8 = buffer.readu8
local buru16 = buffer.readu16
local buri16 = buffer.readi16
local buru32 = buffer.readu32
local burf32 = buffer.readf32
local burf64 = buffer.readf64
local burs = buffer.readstring
local buco = buffer.copy
local bucr = buffer.create
local V2n = Vector2.new
local V3n = Vector3.new
local BCn = BrickColor.new
local C3n = Color3.new
local C3r = Color3.fromRGB
local CFnew = CFrame.new
local CFe = CFrame.fromEulerAnglesXYZ
local UD2new = UDim2.new
local UDnew = UDim.new
local enumKeycode = Enum.KeyCode
local EncodingService = game:GetService("EncodingService")
local Comp = require(script.Parent.Compression)
local compressModeTargets = {
"Deflate",
"Zlib",
"ZlibNative"
}
--local enumMap: {[Enum]: number} = {} --> number -> enum
local enumMap = nil
local keycodeEnumMapIdx = nil
local enumMapFallback = {}
local enumCache = {} -- enum --> {name --> number}
for i,v in Enum:GetEnums() do
enumMapFallback[i] = v
end
module.TryLoadEnumMap = function()
if enumMap == nil then
local enumStrMap = script:FindFirstChild("EnumStringMap")
if not enumStrMap then return false end
enumMap = {}
for _, str in enumStrMap.Value:split("/") do
local contents = str:split("-")
local isValid = pcall(function() assert(Enum[contents[2]]) end)
local i = tonumber(contents[1])
if not isValid then
enumMap[i] = "SERVER_ONLY_ENUM"
continue
end
if Enum[contents[2]] == enumKeycode then
keycodeEnumMapIdx = i
end
enumMap[i] = Enum[contents[2]]
end
else
return true
end
end
module.Init = function()
if game:GetService("RunService"):IsServer() then
--> we also store a stringvalue for the client to use, because the client and server have different enums
local strMap: string = ""
enumMap = {}
for i, v: Enum in Enum:GetEnums() do
if v == enumKeycode then
keycodeEnumMapIdx = i
end
strMap ..= `{i}-{v}/`
enumMap[i] = v
end
strMap = strMap:sub(1, #strMap - 1) -- remove last /
if script:FindFirstChild("EnumStringMap") then return end
local obj = Instance.new("StringValue")
obj.Value = strMap
obj.Name = "EnumStringMap"
obj.Parent = script
return
end
module.TryLoadEnumMap()
end
module.DecodeVarLength = function(input: buffer, offset: number)
if not offset then offset = 0 end
local data,shift = 0,1
local loop = 0
while true do
local x = buru8(input, loop+offset)
data += b3ba(x, 0x7F) * shift
loop += 1
if b3ba(x, 0x80) ~= 0 then
break
end
shift = b3ls(shift, 7)
data += shift
end
return data,loop
end
local functions = {
function(input: buffer, offset: number) -- String
local len,amt = module.DecodeVarLength(input,offset)
offset+=amt
local mode = buru8(input, offset)
offset+=1
local str
if mode > 0 then
if mode == 3 then
local strBuf = bucr(len)
buco(strBuf, 0, input, offset, len)
str = buffer.tostring(EncodingService:DecompressBuffer(strBuf, Enum.CompressionAlgorithm.Zstd))
else
str = burs(input, offset, len)
str = Comp[compressModeTargets[mode]].Decompress(str)
end
else
str = burs(input, offset, len)
end
offset+=len
return str,offset
end,
function(input: buffer, offset: number) -- Boolean5
local byte = buru8(input, offset)
offset+=1
local amt = b3rs(b3ba(byte, 224), 5) + 1
local bools = {}
for i = 1, amt do
local bool = b3ba(b3rs(byte, 5-i),1)
ti(bools,bool==1)
end
if amt == 1 then bools = unpack(bools) end
return bools,offset
end,
function(input: buffer, offset: number) -- UInt8
local byte = buru8(input, offset)
offset+=1
return byte,offset
end,
function(input: buffer, offset: number) -- UInt16
local val = buru16(input, offset)
offset+=2
return val,offset
end,
function(input: buffer, offset: number) -- UInt32
local val = buru32(input, offset)
offset+=4
return val,offset
end,
function(input: buffer, offset: number) -- float
local val = burf32(input, offset)
offset+=4
return val,offset
end,
function(input: buffer, offset: number) -- double
local val = burf64(input, offset)
offset+=8
return val,offset
end,
function(input: buffer, offset: number) -- Vector2
local comp = buru8(input, offset)
local func,mult
if comp == 1 then
func = burf32
mult = 1
else
func = burf64
mult = 2
end
offset+=1
local X = func(input, offset)
local Y = func(input, offset+4*mult)
offset+=8*mult
return V2n(X,Y),offset
end,
function(input: buffer, offset: number) -- Vector3
local comp = buru8(input, offset)
local func,mult
if comp == 1 then
func = burf32
mult = 1
else
func = burf64
mult = 2
end
offset+=1
local X = func(input, offset)
local Y = func(input, offset+4*mult)
local Z = func(input, offset+8*mult)
offset+=12*mult
return V3n(X,Y,Z),offset
end,
function(input: buffer, offset: number) -- CFrame
--> roblox always stores cframes as 3 f32s for position and 9 i16s for rotation matrices
--> since the rotation vectors are always perpendicular we can only save two
--> and reconstruct the other when decoding from cross product
local x, y, z = burf32(input, offset), burf32(input, offset + 4), burf32(input, offset + 8)
local r00, r01, r02 =
buri16(input, offset + 12) / 32767,
buri16(input, offset + 14) / 32767,
buri16(input, offset + 16) / 32767
local r10, r11, r12 =
buri16(input, offset + 18) / 32767,
buri16(input, offset + 20) / 32767,
buri16(input, offset + 22) / 32767
offset += 24
local r2 = Vector3.new(r00, r01, r02):Cross(Vector3.new(r10, r11, r12))
return CFnew(x, y, z, r00, r01, r02, r10, r11, r12, r2.X, r2.Y, r2.Z), offset
end,
function(input: buffer, offset: number) -- CFrameEuler
local comp = buru8(input, offset)
local func,mult
if comp == 1 then
func = burf32
mult = 1
else
func = burf64
mult = 2
end
offset+=1
local X = func(input, offset)
local Y = func(input, offset+4*mult)
local Z = func(input, offset+8*mult)
local rX = func(input, offset+12*mult)
local rY = func(input, offset+16*mult)
local rZ = func(input, offset+20*mult)
offset+=24*mult
return (CFe(rX,rY,rZ)+V3n(X,Y,Z)),offset
end,
function(input: buffer, offset: number) -- Color3
local brick = buru8(input, offset)
local comp = buru8(input, offset+1)
local func,mult
if comp == 1 then
func = burf32
mult = 1
else
func = burf64
mult = 2
end
offset+=2
local R = func(input, offset)
local G = func(input, offset+4*mult)
local B = func(input, offset+8*mult)
offset+=12*mult
if brick == 1 then
return BCn(R,G,B),offset
else
return C3n(R,G,B),offset
end
end,
function(input: buffer, offset: number) -- Color3b
local brick = buru8(input, offset)
local R = buru8(input, offset+1)
local G = buru8(input, offset+2)
local B = buru8(input, offset+3)
offset+=4
if brick == 1 then
return BCn(R/255,G/255,B/255),offset
else
return C3r(R,G,B),offset
end
end,
nil, -- DO NOT USE: Handled elsewhere, begin marker for tables.
nil, -- DO NOT USE: End marker for tables.
nil, -- DO NOT USE: Handled elsewhere, begin marker for dictionaries.
function(input: buffer, offset:number) -- nil
return nil,offset
end,
function(input: buffer, offset:number) -- ColorSequence
local count, off = module.DecodeVarLength(input, offset)
offset += off
local float = buru8(input, offset)==1 offset += 1
local bytes = buru8(input, offset)==1 offset += 1
local times = {}
local keypoints = {}
local func,add
if float then func,add = burf32,4 else func,add = burf64,8 end
for i = 1, count do
ti(times, func(input, offset))
offset += add
end
for i = 1, count do
local col
if bytes then
local r = buru8(input, offset) offset += 1
local g = buru8(input, offset) offset += 1
local b = buru8(input, offset) offset += 1
col = C3r(r,g,b)
else
local r = func(input, offset) offset += add
local g = func(input, offset) offset += add
local b = func(input, offset) offset += add
col = C3n(r,g,b)
end
ti(keypoints, ColorSequenceKeypoint.new(times[i],col))
end
return ColorSequence.new(keypoints),offset
end,
function(input: buffer, offset:number) -- Vector2int16
local X = buru16(input, offset) offset += 2
local Y = buru16(input, offset) offset += 2
return Vector2int16.new(X-32768,Y-32768),offset
end,
function(input: buffer, offset:number) -- Vector3int16
local X = buru16(input, offset) offset += 2
local Y = buru16(input, offset) offset += 2
local Z = buru16(input, offset) offset += 2
return Vector3int16.new(X-32768,Y-32768,Z-32768),offset
end,
function(input: buffer, offset: number) -- EnumItem
local enumIdx = buru16(input, offset + 1)
local value = buru8(input, offset)
offset += 3
if not enumCache[enumIdx] then
local using = (enumMap or enumMapFallback)[enumIdx]
if using == "SERVER_ONLY_ENUM" then
return error("[NetShrink] Attempt to decode server-only Enum on client")
end
enumCache[enumIdx] = using:GetEnumItems()
--> special case: KeyCode enum has >256 enums, but 95 of those are 100% broken now
--> ^^ https://devforum.roblox.com/t/what-are-the-inputs-world0-to-world95-used-for/222976/8
local cache = enumCache[enumIdx]
if enumIdx == enumKeycode then
for i, en: EnumItem in enumKeycode:GetEnumItems() do
if en.Name:sub(1, 5) ~= "World" then continue end
table.remove(cache, table.find(cache, en)) -- die
end
end
end
value = enumCache[enumIdx][value].Value
return (enumMap or enumMapFallback)[enumIdx]:FromValue(value), offset
end,
function(input: buffer, offset: number) -- UDim2
local Xscale = burf32(input, offset)
local Xoffset = burf32(input, offset + 4)
local Yscale = burf32(input, offset + 8)
local Yoffset = burf32(input, offset + 12)
offset += 16
return UD2new(Xscale, Xoffset, Yscale, Yoffset), offset
end,
function(input: buffer, offset: number) -- UDim
local scale = burf32(input, offset)
local _offset = burf32(input, offset + 4)
offset += 8
return UDnew(scale, _offset), offset
end,
function(input: buffer, offset:number) -- NumberSequence
local count, off = module.DecodeVarLength(input, offset)
offset += off
local float = buru8(input, offset)==1 offset += 1
local times = {}
local keypoints = {}
local func,add
if float then func,add = burf32,4 else func,add = burf64,8 end
for i = 1, count do
ti(times, func(input, offset))
offset += add
end
for i = 1, count do
local value = func(input, offset) offset += add
local envelope = func(input, offset) offset += add
ti(keypoints, NumberSequenceKeypoint.new(times[i],value,envelope))
end
return NumberSequence.new(keypoints),offset
end,
function(input: buffer, offset:number) -- NumberRange
local float = buru8(input, offset)==1 offset += 1
local func,add
if float then func,add = burf32,4 else func,add = burf64,8 end
local min = func(input, offset) offset += add
local max = func(input, offset) offset += add
return NumberRange.new(min, max),offset
end,
nil -- RESERVED: Dictionary end marker for v1.6.2 fix
}
module.ReadType = function(input: buffer, offset: number, type: number)
return functions[type+1](input, offset)
end
return module