-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
706 lines (548 loc) · 18.7 KB
/
main.lua
File metadata and controls
706 lines (548 loc) · 18.7 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
-- local Log = LibDataPacker_Logger()
local BinaryBuffer = LibDataPacker_BinaryBuffer
local floor = math.floor
local ceil = math.ceil
local log = math.log
local char = string.char
local lib = {}
-- ----------------------------------------------------------------------------
local function maxBitLengthFor(number)
return ceil(log(number + 1) / log(2))
end
local function getMaxValueForBitLength(number)
return 2^number - 1
end
-- ----------------------------------------------------------------------------
local EMPTY = 0
local NUMERIC = 1
local TABLE = 2
local ARRAY = 3
local BOOLEAN = 4
local STRING = 5
local ENUM = 6
local VARIABLE_LENGTH_ARRAY = 7
local OPTIONAL = 8
-- ----------------------------------------------------------------------------
--- @class Field
--- @field __index Field
--- @field name string|nil The field name
--- @field fieldType integer The field type (EMPTY, NUMERIC, TABLE, ARRAY)
local Field = {}
Field.__index = Field
--- Creates a new Field object
--- @param name string|nil The field name
--- @param fieldType integer The field type
--- @return Field @The new Field object
function Field.New(name, fieldType)
--- @class (partial) Field
local o = setmetatable({}, Field)
o.name = name
o.fieldType = fieldType
return o
end
function Field:Serialize(data, buffer)
assert(false, 'Must be overridden')
end
function Field:Unserialize(buffer)
assert(false, 'Must be overridden')
end
function Field:GetMaxBitLength()
assert(false, 'Must be overridden')
end
function Field:Skip(buffer)
assert(false, 'Must be overridden')
end
-- ----------------------------------------------------------------------------
--- @class Numeric : Field
--- @field __index Numeric
--- @field bitLength integer The bit length for the numeric field
local Numeric = setmetatable({}, { __index = Field })
Numeric.__index = Numeric
--- @class NumericWithPrecision : Field
--- @field __index NumericWithPrecision
--- @field bitLength integer The bit length for the numeric field
--- @field mult number Multiplier
local NumericWithPrecision = setmetatable({}, { __index = Field })
NumericWithPrecision.__index = NumericWithPrecision
--- Creates a new Numeric field
--- @param name string|nil The field name
--- @param bitLength integer The bit length for the numeric field
--- @param precision integer|nil
--- @return Numeric|NumericWithPrecision @The new Numeric field
function Numeric.New(name, bitLength, precision)
--- @class (partial) Numeric
local o = Field.New(name, NUMERIC)
o.bitLength = bitLength
if precision and type(precision) == 'number' and precision ~= 0 then
setmetatable(o, NumericWithPrecision)
o.mult = 10^precision
else
setmetatable(o, Numeric)
end
return o
end
--- Handles a numeric value
--- @param data number The numeric data to handle
--- @return string|nil The binary string representation, or nil on error
function Numeric:Serialize(data, binaryBuffer)
if type(data) ~= 'number' then
error(('Value must be a number, got %s: %s'):format(type(data), tostring(data)))
end
binaryBuffer:Write(data, self.bitLength)
end
function Numeric:Unserialize(binaryBuffer)
-- TODO: length check
return binaryBuffer:Read(self.bitLength)
end
function Numeric:Skip(binaryBuffer)
binaryBuffer:Skip(self.bitLength)
end
function Numeric:GetMaxBitLength()
return self.bitLength
end
local function transformForward(number, mult)
return floor(number * mult + 0.5)
end
--- Handles a numeric value
--- @param data number The numeric data to handle
--- @return string|nil The binary string representation, or nil on error
function NumericWithPrecision:Serialize(data, binaryBuffer)
if type(data) ~= 'number' then
error(('Value must be a number, got %s: %s'):format(type(data), tostring(data)))
end
binaryBuffer:Write(transformForward(data, self.mult), self.bitLength)
end
local function transformBackward(number, mult)
return number / mult
end
function NumericWithPrecision:Unserialize(binaryBuffer)
-- TODO: length check
return transformBackward(binaryBuffer:Read(self.bitLength), self.mult)
end
-- TODO: inherit from Numeric then?
NumericWithPrecision.GetMaxBitLength = Numeric.GetMaxBitLength
NumericWithPrecision.Skip = Numeric.Skip
-- ----------------------------------------------------------------------------
--- @class Array : Field
--- @field __index Array
--- @field length integer The array length
--- @field subType Field The field type for array elements
local Array = setmetatable({}, { __index = Field })
Array.__index = Array
--- Creates a new Array field
--- @param name string|nil The field name
--- @param length integer The array length
--- @param subtype Field The field type for array elements
--- @return Array The new Array field
function Array.New(name, length, subtype)
--- @class (partial) Array
local o = setmetatable(Field.New(name, ARRAY), Array)
o.length = length
o.subType = subtype
return o
end
--- Handles an array of values
--- @param data table The array data to handle
--- @return string|nil The binary string representation, or nil on error
function Array:Serialize(data, binaryBuffer)
if type(data) ~= 'table' then error('Value must be a table') end
for _, datum in ipairs(data) do
self.subType:Serialize(datum, binaryBuffer)
end
end
function Array:Unserialize(dataBuffer)
local result = {}
-- TODO: length check
for i = 1, self.length do
result[i] = self.subType:Unserialize(dataBuffer)
end
return result
end
function Array:Skip(binaryBuffer)
for _ = 1, self.length do
self.subType:Skip(binaryBuffer)
end
end
function Array:GetMaxBitLength()
return self.length * self.subType:GetMaxBitLength()
end
-- ----------------------------------------------------------------------------
--- @class VLArray : Field
--- @field __index VLArray
--- @field length Numeric The array max length
--- @field subType Field The field type for array elements
local VLArray = setmetatable({}, { __index = Field })
VLArray.__index = VLArray
--- Creates a new VLArray field
--- @param name string|nil The field name
--- @param maxLength integer The array max length
--- @param subtype Field The field type for array elements
--- @return VLArray The new Array field
function VLArray.New(name, maxLength, subtype)
--- @class (partial) VLArray
local o = setmetatable(Field.New(name, VARIABLE_LENGTH_ARRAY), VLArray)
local bitLength = maxBitLengthFor(maxLength)
o.length = Numeric.New(nil, bitLength) --[[@as Numeric]]
o.subType = subtype
return o
end
--- Handles an array of values
--- @param data table The array data to handle
--- @return string|nil The binary string representation, or nil on error
function VLArray:Serialize(data, binaryBuffer)
if type(data) ~= 'table' then error('Value must be a table') end
self.length:Serialize(#data, binaryBuffer)
for _, datum in ipairs(data) do
self.subType:Serialize(datum, binaryBuffer)
end
end
function VLArray:Unserialize(binaryBuffer)
local result = {}
-- TODO: length check
local length = self.length:Unserialize(binaryBuffer)
local subType = self.subType
for i = 1, length do
result[i] = subType:Unserialize(binaryBuffer)
end
return result
end
function VLArray:Skip(binaryBuffer)
local length = self.length:Unserialize(binaryBuffer)
local subType = self.subType
for _ = 1, length do
subType:Skip(binaryBuffer)
end
end
function VLArray:GetMaxBitLength()
local lenghtMaxBitLength = self.length:GetMaxBitLength()
return lenghtMaxBitLength + getMaxValueForBitLength(lenghtMaxBitLength) * self.subType:GetMaxBitLength()
end
-- ----------------------------------------------------------------------------
local function isSequential(tbl)
if next(tbl) ~= 1 then
return false
else
return next(tbl, #tbl) == nil
end
end
--- @class Table : Field
--- @field __index Table
--- @field _fields Field[] The fields contained in the table
local Table = setmetatable({}, { __index = Field })
Table.__index = Table
--- Creates a new Table field
--- @param name string|nil The field name
--- @param fields Field[] The fields contained in the table
--- @return Table @The new Table field
function Table.New(name, fields, ignoreNames)
--- @class (partial) Table
local o = setmetatable(Field.New(name, TABLE), Table)
o._fields = fields
o._ignoreNames = ignoreNames
assert(isSequential(fields), 'Fields table must be a simple array!')
o._indicies = {}
for i = 1, #fields do
if fields[i].name then
o._indicies[fields[i].name] = i
else
o._indicies[#o._indicies+1] = i
end
end
return o
end
--- Handles a table of values
--- @param data table The table data to handle
--- @return string|nil @The binary string representation, or nil on error
function Table:Serialize(data, binaryBuffer)
if type(data) ~= 'table' then error('Value must be a table') end
if self._ignoreNames then
for i = 1, #self._fields do
local field = self._fields[i]
local datum = data[i]
field:Serialize(datum, binaryBuffer)
end
else
for i = 1, #self._fields do
local field = self._fields[i]
local datum = data[field.name]
field:Serialize(datum, binaryBuffer)
end
end
end
function Table:Unserialize(dataBuffer)
local result = {}
local l = 1
for i = 1, #self._fields do
local field = self._fields[i]
local unserializedData = field:Unserialize(dataBuffer)
if self._ignoreNames or not field.name then
result[l] = unserializedData
l = l + 1
else
result[field.name] = unserializedData
end
end
return result
end
function Table:Skip(binaryBuffer)
for i = 1, #self._fields do
self._fields[i]:Skip(binaryBuffer)
end
end
function Table:GetMaxBitLength()
local totalBitLength = 0
for i = 1, #self._fields do
totalBitLength = totalBitLength + self._fields[i]:GetMaxBitLength()
end
return totalBitLength
end
function Table:Replace(name, substitute)
self._fields[self._indicies[name]] = substitute
end
function Table:GetFields()
return self._fields
end
function Table:ShallowCopy(fieldsToCopy)
local newFieldsArray = {}
if fieldsToCopy then
for i = 1, #fieldsToCopy do
local fieldIndex = self._indicies[fieldsToCopy[i]]
newFieldsArray[i] = self._fields[fieldIndex]
end
else
ZO_ShallowTableCopy(self._fields, newFieldsArray)
end
return Table.New(self.name, newFieldsArray, self._ignoreNames)
end
function Table:Remove(index)
table.remove(self._fields, self._indicies[index])
end
-- ----------------------------------------------------------------------------
--- @class Boolean : Field
--- @field __index Boolean
local Boolean = setmetatable({}, { __index = Field })
Boolean.__index = Boolean
--- Creates a new Boolean field
--- @param name string|nil The field name
--- @return Boolean @The new Boolean field
function Boolean.New(name)
--- @class (partial) Boolean
local o = setmetatable(Field.New(name, BOOLEAN), Boolean)
return o
end
--- Handles a boolean value
--- @param data boolean The boolean data to handle
--- @return string|nil The binary string representation, or nil on error
function Boolean:Serialize(data, binaryBuffer)
if type(data) ~= 'boolean' then error('Value must be a boolean') end
binaryBuffer:WriteBit(data and 1 or 0)
end
function Boolean:Unserialize(dataBuffer)
return dataBuffer:Read(1) == 1
end
function Boolean:Skip(binaryBuffer)
binaryBuffer:Skip(1)
end
function Boolean:GetMaxBitLength()
return 1
end
-- ----------------------------------------------------------------------------
--- @class String : Field
--- @field __index String
local String = setmetatable({}, { __index = Field })
String.__index = String
--- Creates a new Boolean field
--- @param name string|nil The field name
--- @return String @The new String field
function String.New(name, maxLength)
--- @class (partial) String
local o = setmetatable(Field.New(name, STRING), String)
local lengthBits = maxBitLengthFor(maxLength)
o.length = Numeric.New(nil, lengthBits) --[[@as Numeric]]
return o
end
--- Handles a string value
--- @param data string The string data to handle
--- @return string|nil The binary string representation, or nil on error
function String:Serialize(data, binaryBuffer)
if type(data) ~= 'string' then error('Value must be a string') end
self.length:Serialize(#data, binaryBuffer)
for i = 1, #data do
local byte = data:byte(i)
binaryBuffer:Write(byte, 8)
end
end
local precomputedPointerChange = {}
do
for i = 0, 5 do
precomputedPointerChange[i] = floor((i + 8) / 6)
end
end
local function read8(buffer)
local startPointer, startShift = buffer.chunkPointer, buffer.shift
buffer.shift = (startShift + 8) % 6
buffer.chunkPointer = buffer.chunkPointer + precomputedPointerChange[startShift]
local decimal = buffer[buffer.chunkPointer]
for i = buffer.chunkPointer-1, startPointer, -1 do
decimal = BitOr(BitLShift(decimal, 6), buffer[i])
end
return BitAnd(0xFF, BitRShift(decimal, startShift))
end
function String:Unserialize(dataBuffer)
local bytesTotal = self.length:Unserialize(dataBuffer)
local bytes = {}
if dataBuffer.chunkSize == 6 then
local p = floor(bytesTotal / 6) * 6
local reminder = bytesTotal % 6
for i = 1, p, 6 do
local decimal = dataBuffer:Read(48) -- TODO: optimizable
for k = 0, 4 do
bytes[i+k] = BitAnd(decimal, 0xFF); decimal = BitRShift(decimal, 8)
end
bytes[i+5] = decimal
end
for i = p+1, p+reminder do
bytes[i] = read8(dataBuffer)
end
else
for i = 1, bytesTotal do
bytes[i] = dataBuffer:Read(8)
end
end
return char(unpack(bytes))
end
function String:Skip(binaryBuffer)
local bytesTotal = self.length:Unserialize(binaryBuffer)
binaryBuffer:Skip(bytesTotal * 8)
end
function String:GetMaxBitLength()
local lenghtMaxBitLength = self.length:GetMaxBitLength()
return lenghtMaxBitLength + getMaxValueForBitLength(lenghtMaxBitLength) * 8
end
-- ----------------------------------------------------------------------------
--- @class Enum : Field
--- @field __index Enum
local Enum = setmetatable({}, { __index = Field })
Enum.__index = Enum
--- Creates a new Enum field
--- @param name string|nil The field name
--- @param enumTable table The table of values
--- @param inverted boolean|nil If the table is lookup table instead
--- @param bitLength number|nil Amount of reserved bits for Enum
--- @return Enum @The new Enum field
function Enum.New(name, enumTable, inverted, bitLength)
--- @class (partial) Enum
local o = setmetatable(Field.New(name, ENUM), Enum)
o.forward = enumTable
o.backward = {}
for k, v in pairs(enumTable) do
o.backward[v] = k
end
if inverted then
o.forward, o.backward = o.backward, o.forward
end
local calculatedBitLength = maxBitLengthFor(#o.backward)
if bitLength then
if calculatedBitLength > bitLength then
error(('Enum `%s` requires more bits (%d) than reserved (%d)'):format(string(o.name), calculatedBitLength, bitLength))
end
o.bitLength = bitLength
else
o.bitLength = calculatedBitLength
end
o.subType = Numeric.New(nil, o.bitLength) --[[@as Numeric]]
return o
end
--- Handles a boolean value
--- @param data any The data to handle
--- @return string|nil The binary string representation, or nil on error
function Enum:Serialize(data, binaryBuffer)
local newValue = self.forward[data]
if newValue == nil then
error(('Value %s is not found in enum table'):format(tostring(data)))
end
self.subType:Serialize(newValue, binaryBuffer)
end
function Enum:Unserialize(dataBuffer)
local result = self.subType:Unserialize(dataBuffer)
local newResult = self.backward[result]
if newResult == nil then
error(('Value %s not found in lookup table'):format(tostring(result)))
end
return newResult
end
function Enum:Skip(binaryBuffer)
self.subType:Skip(binaryBuffer)
end
function Enum:GetMaxBitLength()
return self.subType:GetMaxBitLength()
end
-- ----------------------------------------------------------------------------
--- @class Optional : Field
--- @field __index Optional
--- @field subfield Field Subfield
local Optional = setmetatable({}, { __index = Field })
Optional.__index = Optional
--- Creates a new Optional field
--- @param subfield Field The subfield
--- @return Optional @The new Optional field
function Optional.New(subfield)
--- @class (partial) Optional
local o = setmetatable(Field.New(subfield.name, OPTIONAL), Optional)
o.subfield = subfield
return o
end
--- Handles a data
--- @param data any The field data to handle
--- @return string|nil The binary string representation, or nil on error
function Optional:Serialize(data, binaryBuffer)
binaryBuffer:WriteBit(data == nil and 0 or 1)
if data ~= nil then
self.subfield:Serialize(data, binaryBuffer)
end
end
function Optional:Unserialize(dataBuffer)
if dataBuffer:Read(1) == 0 then return end
return self.subfield:Unserialize(dataBuffer)
end
function Optional:Skip(binaryBuffer)
if binaryBuffer:Read(1) == 0 then return end
self.subfield:Skip(binaryBuffer)
end
function Optional:GetMaxBitLength()
return self.subfield:GetMaxBitLength() + 1
end
-- ----------------------------------------------------------------------------
lib.BaseField = Field
lib.Field = {
Number = Numeric.New,
Array = Array.New,
VLArray = VLArray.New,
Table = Table.New,
Bool = Boolean.New,
String = String.New,
Enum = Enum.New,
Optional = Optional.New,
}
lib.Diagnostics = {
MaxLength = function(schema, base)
return ceil(schema:GetMaxBitLength() / base.bitLength)
end,
}
function lib.Pack(data, schema, base)
base = base or lib.Base.Base64RCF4648
local binaryBuffer = BinaryBuffer.New(nil, base.bitLength)
schema:Serialize(data, binaryBuffer)
return base:Encode(binaryBuffer)
end
function lib.Unpack(data, schema, base)
base = base or lib.Base.Base64RCF4648
return schema:Unserialize(base:Decode(data))
end
function lib.Repack(data, oldSchema, newSchema, oldBase, newBase)
newSchema = newSchema or oldSchema
newBase = newBase or oldBase
return lib.Pack(lib.Unpack(data, oldSchema, oldBase), newSchema, newBase)
end
-- ----------------------------------------------------------------------------
LibDataPacker = lib