-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEncode.lua
More file actions
367 lines (348 loc) · 7.62 KB
/
Encode.lua
File metadata and controls
367 lines (348 loc) · 7.62 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
local module = {}
local p = pairs
local ti = table.insert
local b3ba = bit32.band
local b3rs = bit32.rshift
local b3bo = bit32.bor
local bucr = buffer.create
local buwu8 = buffer.writeu8
local buwu16 = buffer.writeu16
local buwi16 = buffer.writei16
local buwu32 = buffer.writeu32
local buwf32 = buffer.writef32
local buwf64 = buffer.writef64
local buws = buffer.writestring
local bule = buffer.len
local buco = buffer.copy
local tbfFunctions = {
function(input)
local dat = input.Data
local size = #dat
local buf = bucr(size)
buws(buf,0,dat,size)
return buf
end,
}
local function ToBuffer(input)
return tbfFunctions[input.DataType+1](input)
end
local function toI16(num: number)
return math.floor(num * 32767 + 0.5)
end
local function BufByte(input)
local buf = bucr(1)
buwu8(buf,0,input)
return buf
end
local function MergeBuffers(...)
local offset = 0
local totalSize = 0
local list = {...}
for _,v in p(list) do
totalSize += bule(v)
end
local buf = bucr(totalSize)
for _,v in p(list) do
local size = bule(v)
buco(buf,offset,v,0,size)
offset+=size
end
return buf
end
module.EncodeVarLength = function(input: number)
local bytes = {}
while true do
local x = b3ba(input, 0x7F)
input = b3rs(input, 7)
if input == 0 then
ti(bytes, b3bo(0x80, x))
break
end
ti(bytes,x)
input-=1
end
local buf = bucr(#bytes)
for i,v in p(bytes) do
buwu8(buf,i-1,v)
end
return buf
end
local function OutlineMoment(v)
local buf
local func
local off
local dat = v.Data
if v.comp then
buf = bucr(#dat*4+1)
func = buwf32
off = 4
else
buf = bucr(#dat*8+1)
func = buwf64
off = 8
end
buwu8(buf,0,(if v.comp then 1 else 0))
for i,d in pairs(dat) do
func(buf,(i-1)*off+1,d)
end
return buf
end
local mf = math.floor
local mc = math.clamp
local function toByte(num)
return mc(mf(num*255),0,255)
end
local functions
functions = {
function(v) -- String
local varlen = module.EncodeVarLength(v.CompressMode==3 and buffer.len(v.Data) or #v.Data)
local buf = MergeBuffers(varlen,BufByte(v.CompressMode),(v.CompressMode==3 and v.Data or ToBuffer(v)))
return buf
end,
function(v) -- Boolean5
local buf = bucr(1)
buwu8(buf,0,v.Value)
return buf
end,
function(v) -- UInt8
local buf = bucr(1)
buwu8(buf,0,v.Value)
return buf
end,
function(v) -- UInt16
local buf = bucr(2)
buwu16(buf,0,v.Value)
return buf
end,
function(v) -- UInt32
local buf = bucr(4)
buwu32(buf,0,v.Value)
return buf
end,
function(v) -- float
local buf = bucr(4)
buwf32(buf,0,v.Value)
return buf
end,
function(v) -- double
local buf = bucr(8)
buwf64(buf,0,v.Value)
return buf
end,
function(v) -- Vector2
return OutlineMoment(v)
end,
function(v) -- Vector3
return OutlineMoment(v)
end,
function(v) -- 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, r00, r01, r02, r10, r11, r12, _, _, _ = unpack(v.Data)
local buf = bucr(24)
--> position
buwf32(buf, 0, x); buwf32(buf, 4, y); buwf32(buf, 8, z)
--> rotation vector 1
buwi16(buf, 12, toI16(r00)); buwi16(buf, 14, toI16(r01)); buwi16(buf, 16, toI16(r02))
--> rotation vector 2
buwi16(buf, 18, toI16(r10)); buwi16(buf, 20, toI16(r11)); buwi16(buf, 22, toI16(r12))
return buf
end,
function(v) -- CFrameEuler
return OutlineMoment(v)
end,
function(v , ident) -- Color3
local buf
local func
local off
local dat = v.Data
if v.comp then
buf = bucr(#dat*4+2)
func = buwf32
off = 4
else
buf = bucr(#dat*8+2)
func = buwf64
off = 8
end
local o = 1
if ident == false then
o = 0
else
buwu8(buf,0,(if v.Brick then 1 else 0))
end
buwu8(buf,o,(if v.comp then 1 else 0))
for i,d in pairs(dat) do
func(buf,(i-1)*off+(o+1),d)
end
return buf
end,
function(v, ident) -- Color3b
local buf = bucr(4)
local o = 1
if ident == false then
o = 0
else
buwu8(buf,0,(if v.Brick then 1 else 0))
end
buwu8(buf,o,v.R)
buwu8(buf,o+1,v.G)
buwu8(buf,o+2,v.B)
return buf
end,
function(v) -- Table
local objs = {}
local total = 0
for _,a in p(v.Value) do
local buf = functions[a.DataType+1](a)
total += bule(buf)
ti(objs, buf)
end
local out = bucr(total)
total = 0
for _,v in p(objs) do
local len = bule(v)
buco(out,total,v,0,len)
total += len
end
return out
end,
nil, -- DO NOT USE: End marker for tables.
nil, -- DO NOT USE: Handled elsewhere, begin marker for dictionaries.
function(v) -- nil
return bucr(0)
end,
function(v) -- ColorSequence
local func
local off
local dat = v.Data
local kp = v.Value.Keypoints
local count = #kp
if v.comp1 then
func = buwf32
off = 4
else
func = buwf64
off = 8
end
local sz = (if v.comp2 then 3 else off*3)*count+2
local varlen = module.EncodeVarLength(count)
local lensz = bule(varlen)
local buf = bucr(count*off+sz+lensz)
buco(buf,0,varlen,0,lensz)
buwu8(buf,lensz,(if v.comp1 then 1 else 0))
buwu8(buf,lensz+1,(if v.comp2 then 1 else 0))
local pos = lensz+2
for _,k in kp do
func(buf,pos,k.Time)
pos += off
end
for _,k in kp do
local c = k.Value
if v.comp2 then
buwu8(buf,pos,toByte(c.R))
buwu8(buf,pos+1,toByte(c.G))
buwu8(buf,pos+2,toByte(c.B))
pos += 3
else
-- looks dumb but should technically be faster
func(buf,pos,c.R)
pos += off
func(buf,pos,c.G)
pos += off
func(buf,pos,c.B)
pos += off
end
end
return buf
end,
function(v) -- Vector2int16
local buf = bucr(4)
buwu16(buf,0,v.Data[1]+32768)
buwu16(buf,2,v.Data[2]+32768)
return buf
end,
function(v) -- Vector3int16
local buf = bucr(6)
buwu16(buf,0,v.Data[1]+32768)
buwu16(buf,2,v.Data[2]+32768)
buwu16(buf,4,v.Data[3]+32768)
return buf
end,
function(v) -- EnumItem
local buf = bucr(3)
buwu8(buf, 0, v.Data[1]) -- value
buwu16(buf, 1, v.Data[2]) -- enum idx
return buf
end,
function(v) -- UDim2
local dat = v.Data
local buf = bucr(#dat*4)
for i,d in pairs(dat) do
buwf32(buf,(i-1)*4,d)
end
return buf
end,
function(v) -- UDim
local dat = v.Data
local buf = bucr(#dat*4)
for i,d in pairs(dat) do
buwf32(buf,(i-1)*4,d)
end
return buf
end,
function(v) -- NumberSequence
local func
local off
local dat = v.Data
local kp = v.Value.Keypoints
local count = #kp
if v.comp1 then
func = buwf32
off = 4
else
func = buwf64
off = 8
end
local varlen = module.EncodeVarLength(count)
local lensz = bule(varlen)
local buf = bucr((count*off*3)+lensz+1)
buco(buf,0,varlen,0,lensz)
buwu8(buf,lensz,(if v.comp1 then 1 else 0))
local pos = lensz+1
for _,k in kp do
func(buf,pos,k.Time)
pos += off
end
for _,k in kp do
func(buf,pos,k.Value)
pos += off
func(buf,pos,k.Envelope)
pos += off
end
return buf
end,
function(v) -- NumberRange
local func
local off
local data = v.Value
if v.comp1 then
func = buwf32
off = 4
else
func = buwf64
off = 8
end
local buf = bucr(off*2+1)
buwu8(buf,0,(if v.comp1 then 1 else 0))
func(buf,1,data.Min)
func(buf,off+1,data.Max)
return buf
end,
nil -- RESERVED: Dictionary end marker for v1.6.2 fix
}
module.Convert = function(v)
return functions[v.DataType+1](v)
end
return module