-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSerialize.bf
More file actions
688 lines (591 loc) · 20.3 KB
/
Serialize.bf
File metadata and controls
688 lines (591 loc) · 20.3 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
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
namespace Bon.Integrated
{
struct SerializeValueState
{
public bool doOneLine, arrayKeepUnlessSet;
}
static class Serialize
{
public static mixin CompNoReflectionError(String type, String example)
{
scope:mixin $"No reflection data for {type}!\n(for example: [BonTarget] extension {example} {{}} or through build settings)"
}
static mixin DoAlwaysInclude(Type type)
{
type.IsStruct || type.IsSizedArray
}
static mixin DoInclude(ValueView val, BonSerializeFlags flags)
{
flags.HasFlag(.IncludeDefault) || DoAlwaysInclude!(val.type) || !ValueDataIsZero!(val)
}
static mixin DoTypeOneLine(Type type, BonSerializeFlags flags)
{
(type.IsPrimitive || (type.IsTypedPrimitive && (!flags.HasFlag(.Verbose) || !type.IsEnum)))
}
// These two are for integrated use!
[Inline]
public static int Start(BonWriter writer)
{
writer.Start();
return writer.outStr.Length;
}
[Inline]
public static void End(BonWriter writer, int lengthReturnedFromStartCall)
{
if (writer.outStr.Length == lengthReturnedFromStartCall)
Irrelevant(writer);
writer.End();
}
public static void Entry(BonWriter writer, ValueView val, BonEnvironment env)
{
let startLen = Start(writer);
if (DoInclude!(val, env.serializeFlags))
Value(writer, val, env);
End(writer, startLen);
}
public static void Value(BonWriter writer, ValueView val, BonEnvironment env, SerializeValueState state = default)
{
let valType = val.type;
// Make sure that doOneLineVal is only passed when valid
Debug.Assert(!state.doOneLine || DoTypeOneLine!(valType, env.serializeFlags));
writer.EntryStart(state.doOneLine);
if (valType.IsPrimitive)
{
if (valType.IsInteger)
Integer(valType, writer, val);
else if (valType.IsFloatingPoint)
Float(valType, writer, val);
else if (valType.IsChar)
Char(valType, writer, val);
else if (valType == typeof(bool))
Bool(writer, val, env.serializeFlags);
else Debug.FatalError(); // Should be unreachable
}
else if (valType.IsTypedPrimitive)
{
// Is used to change what will be printed if the enum has a remainder
// to be printed as a literal
int64 enumRemainderVal = 0;
var printVal = val;
bool doPrintLiteral = true;
if (valType.IsEnum && env.serializeFlags.HasFlag(.Verbose))
{
doPrintLiteral = false;
int64 valueData = 0;
Span<uint8>((uint8*)val.dataPtr, valType.Size).CopyTo(Span<uint8>((uint8*)&valueData, valType.Size));
bool found = false;
for (var field in valType.GetFields())
{
if (field.[Friend]mFieldData.mFlags.HasFlag(.EnumCase) &&
*(int64*)&field.[Friend]mFieldData.[Friend]mData == valueData)
{
writer.Enum(field.Name);
found = true;
break;
}
}
if (!found)
{
// There is no exact named value here, but maybe multiple!
// We only try once, but that's better than none. If you were
// to have this enum { A = 0b0011, B = 0b0111, C = 0b1100 }
// and run this on 0b1111, this algorithm would fail to
// identify .A | .C, but rather .B | 0b1000 because it takes
// the largest match first and never looks back if it doesn't
// work out. The easiest way to make something more complicated
// work would probably be recursion... maybe in the future
enumRemainderVal = valueData;
String bestValName = scope .();
bool foundAny = false;
while (enumRemainderVal != 0)
{
// Go through all values and find best match in therms of bits
int64 bestVal = 0;
var bestValBits = 0;
for (var field in valType.GetFields())
{
if (field.[Friend]mFieldData.mFlags.HasFlag(.EnumCase))
{
let fieldVal = *(int64*)&field.[Friend]mFieldData.[Friend]mData;
if (fieldVal == 0 || (fieldVal & ~enumRemainderVal) != 0)
continue; // fieldVal contains bits that valueLeft doesn't have
var bits = 0;
for (let i < sizeof(int64) * 8)
if (((fieldVal >> i) & 0b1) != 0)
bits++;
if (bits > bestValBits)
{
bestVal = fieldVal;
bestValName.Set(field.Name);
bestValBits = bits;
}
}
}
if (bestValBits > 0)
{
enumRemainderVal &= ~bestVal; // Remove all bits it shares with this
writer.Enum(bestValName);
foundAny = true;
}
else
{
if (foundAny)
writer.EnumAdd();
// Print remainder literal
doPrintLiteral = true;
printVal = .(val.type, &enumRemainderVal);
break;
}
if (enumRemainderVal == 0)
break;
}
}
}
// Print on non-enum types, and if the enum need to append the leftover value as literal
// or in any case if we're not printing verbose
if (doPrintLiteral)
{
if (valType.UnderlyingType.IsInteger)
Integer(valType.UnderlyingType, writer, printVal);
else if (valType.UnderlyingType.IsFloatingPoint)
Float(valType.UnderlyingType, writer, printVal);
else if (valType.UnderlyingType.IsChar)
Char(valType.UnderlyingType, writer, printVal);
else if (valType.UnderlyingType == typeof(bool))
Bool(writer, printVal, env.serializeFlags);
else Debug.FatalError(); // Should be unreachable
}
}
else if (valType.IsStruct)
{
if (valType == typeof(StringView))
{
let view = *(StringView*)val.dataPtr;
if (view.Ptr == null)
writer.Null();
else writer.String(view);
}
else if (valType.IsEnum && valType.IsUnion)
{
let payloadVal = GetPayloadValueFromEnumUnion(val, let caseName, ?);
if (payloadVal != default)
{
writer.Enum(caseName);
Struct(writer, payloadVal, env);
}
else
{
writer.outStr.Append('?');
if (env.serializeFlags.HasFlag(.Verbose) && !ValueDataIsZero!(val))
{
if (valType.FieldCount <= 2) // Always has two fields $discriminator & $payload
writer.outStr.Append(scope $"/* No reflection data for {valType}. Add [BonTarget] or force it */");
else writer.outStr.Append("/* Enum has corrupted value */");
}
}
}
else if (GetCustomHandler(valType, env, let func))
func(writer, val, env, state);
else
{
if (valType.IsUnion && env.serializeFlags.HasFlag(.Verbose))
writer.outStr.Append("/* Union struct! Fields influence each other */");
Struct(writer, val, env);
}
}
else if (valType.IsSizedArray)
{
let t = (SizedArrayType)valType;
let count = t.ElementCount;
// Since this is a fixed-size array, this info is not necessary to
// deserialize in any case. But it's nice for manual editing to know how
// much the array can hold
if (env.serializeFlags.HasFlag(.Verbose))
writer.Sizer((.)count, true);
Array(writer, t.UnderlyingType, val.dataPtr, count, env, state.arrayKeepUnlessSet);
}
else if (TypeHoldsObject!(valType))
{
if (*(void**)val.dataPtr == null)
writer.Null();
else
{
// We may modify type with polytype for further operations
var val;
let polyType = (*(Object*)val.dataPtr).GetType();
if (polyType != valType)
{
Debug.Assert(!polyType.IsObject || polyType.IsSubtypeOf(valType) || (valType.IsInterface && polyType.HasInterface(valType)));
// Change type of pointer to actual type
val.type = polyType;
Type(writer, polyType);
}
else Debug.Assert(!valType.IsInterface);
if (!polyType.IsObject)
{
// Throw together the pointer to the box payload
// in the corlib approved way. (See Variant.CreateFromBoxed)
let boxType = (*(Object*)val.dataPtr).[Friend]RawGetType();
let boxedPtr = (uint8*)*(void**)val.dataPtr + boxType.[Friend]mMemberDataOffset;
Debug.Assert(!polyType.IsObject);
// polyType already is the type in the box
Value(writer, ValueView(polyType, boxedPtr), env);
// Don't call EndEntry twice
return;
}
else if (polyType.IsArray)
{
Debug.Assert(polyType != typeof(Array) && polyType is ArrayType);
let t = polyType as ArrayType;
Debug.Assert(t.GetField("mFirstElement") case .Ok, CompNoReflectionError!("array type", "Array1<T>"));
let arrType = t.GetGenericArg(0); // T
var arrPtr = GetValFieldPtr!(val, "mFirstElement"); // T*
var count = GetValField!<int_arsize>(val, "mLength");
bool includeAllValues = state.arrayKeepUnlessSet;
switch (t.UnspecializedType)
{
case typeof(Array1<>):
if (!includeAllValues && !Serialize.IsArrayFilled(arrType, arrPtr, count, env))
writer.Sizer((.)count);
Array(writer, arrType, arrPtr, count, env, includeAllValues);
case typeof(Array2<>):
let count1 = GetValField!<int_cosize>(val, "mLength1");
count /= count1;
writer.MultiSizer((.)count,(.)count1);
MultiDimensionalArray(writer, arrType, arrPtr, env, includeAllValues, count, count1);
case typeof(Array3<>):
let count2 = GetValField!<int_cosize>(val, "mLength2");
let count1 = GetValField!<int_cosize>(val, "mLength1");
count /= (count1 * count2);
writer.MultiSizer((.)count,(.)count1,(.)count2);
MultiDimensionalArray(writer, arrType, arrPtr, env, includeAllValues, count, count1, count2);
case typeof(Array4<>):
let count1 = GetValField!<int_cosize>(val, "mLength1");
let count2 = GetValField!<int_cosize>(val, "mLength2");
let count3 = GetValField!<int_cosize>(val, "mLength3");
count /= (count1 * count2 * count3);
writer.MultiSizer((.)count,(.)count1,(.)count2,(.)count3);
MultiDimensionalArray(writer, arrType, arrPtr, env, includeAllValues, count, count1, count2, count3);
default:
Debug.FatalError();
}
}
else if (GetCustomHandler(polyType, env, let func))
func(writer, val, env, state);
else Class(writer, val, env);
}
}
else if (valType.IsPointer)
{
if (env.serializeFlags.HasFlag(.Verbose))
writer.outStr.Append("/* Cannot handle pointer values. Put [BonIgnore] on this field */");
}
else
{
writer.outStr.Append("/* Unhandled. Please report this! */");
Debug.FatalError();
}
writer.EntryEnd(state.doOneLine);
}
static bool GetCustomHandler(Type type, BonEnvironment env, out HandleSerializeFunc func)
{
if (env.typeHandlers.TryGetValue(type, let val) && val.serialize != null)
{
func = val.serialize;
return true;
}
else if (type is SpecializedGenericType && env.typeHandlers.TryGetValue(((SpecializedGenericType)type).UnspecializedType, let gVal)
&& gVal.serialize != null)
{
func = gVal.serialize;
return true;
}
func = null;
return false;
}
public static void Class(BonWriter writer, ValueView classVal, BonEnvironment env)
{
let classType = classVal.type;
Debug.Assert(classType.IsObject);
Struct(writer, ValueView(classType, *(void**)classVal.dataPtr), env);
}
static mixin GetStateFromValueField(FieldInfo fieldInfo)
{
SerializeValueState state = default;
if (fieldInfo.HasCustomAttribute<BonArrayKeepUnlessSetAttribute>())
state.arrayKeepUnlessSet = true;
state
}
public static void Struct(BonWriter writer, ValueView structVal, BonEnvironment env)
{
let structType = structVal.type;
let flags = env.serializeFlags;
bool hasUnnamedMembers = false, membersKeepUnlessSet = structType.HasCustomAttribute<BonKeepMembersUnlessSetAttribute>();
if (structType.FieldCount > 0)
{
using (writer.ObjectBlock())
{
for (let f in structType.GetFields(.Instance))
{
if ((flags & .IgnorePermissions) != .IgnorePermissions
&& (f.GetCustomAttribute<BonIgnoreAttribute>() case .Ok // check hidden
|| (!flags.HasFlag(.IncludeNonPublic) && (f.[Friend]mFieldData.mFlags & .Public == 0) // check protection level
&& f.GetCustomAttribute<BonIncludeAttribute>() case .Err))) // check if we still include it anyway)
continue;
var val = ValueView(f.FieldType, ((uint8*)structVal.dataPtr) + f.MemberOffset);
if (!membersKeepUnlessSet && !DoInclude!(val, flags) && !f.HasCustomAttribute<BonKeepUnlessSetAttribute>())
continue;
if (flags.HasFlag(.Verbose) && uint64.Parse(f.Name) case .Ok)
hasUnnamedMembers = true;
writer.Identifier(f.Name);
Value(writer, val, env, GetStateFromValueField!(f));
}
}
}
else writer.outStr.Append("{}");
if (env.serializeFlags.HasFlag(.Verbose))
{
// Just add this as a comment in case anyone wonders...
if (!ValueDataIsZero!(structVal) && structType.FieldCount == 0)
writer.outStr.Append(scope $"/* No reflection data for {structType}. Add [BonTarget] or force it */");
else if (hasUnnamedMembers)
writer.outStr.Append("/* Type has unnamed members */");
}
}
public static bool IsArrayFilled(Type arrType, void* arrPtr, int64 count, BonEnvironment env)
{
if (DoInclude!(ValueView(arrType, (uint8*)arrPtr + arrType.Stride * (count - 1)), env.serializeFlags))
return true;
return false;
}
public static void Array(BonWriter writer, Type arrType, void* arrPtr, int64 count, BonEnvironment env, bool includeAllValues = false)
{
SerializeValueState state = .{
doOneLine = DoTypeOneLine!(arrType, env.serializeFlags)
};
using (writer.ArrayBlock(state.doOneLine))
{
if (count > 0)
{
var includeCount = count;
if (!includeAllValues && !env.serializeFlags.HasFlag(.IncludeDefault) && !DoAlwaysInclude!(arrType)) // DoInclude! would return true on anything anyway
{
var ptr = (uint8*)arrPtr + arrType.Stride * (count - 1);
for (var i = count - 1; i >= 0; i--)
{
// If this gets included, we'll have to include everything until here!
if (DoInclude!(ValueView(arrType, ptr), env.serializeFlags))
{
includeCount = i + 1;
break;
}
ptr -= arrType.Stride;
}
}
var ptr = (uint8*)arrPtr;
for (let i < includeCount)
{
var arrVal = ValueView(arrType, ptr);
if (includeAllValues || DoInclude!(arrVal, env.serializeFlags))
Value(writer, arrVal, env, state);
else Irrelevant(writer, state.doOneLine);
ptr += arrType.Stride;
}
}
}
}
public static void MultiDimensionalArray(BonWriter writer, Type arrType, void* arrPtr, BonEnvironment env, bool includeAllValues = false, params int64[] counts)
{
Debug.Assert(counts.Count > 1); // Must be multi-dimensional!
let count = counts[0];
var stride = counts[1];
if (counts.Count > 2)
for (let i < counts.Count - 2)
stride *= counts[i + 2];
stride *= arrType.Stride;
using (writer.ArrayBlock())
{
if (count > 0)
{
var includeCount = count;
if (!includeAllValues && !env.serializeFlags.HasFlag(.IncludeDefault) && !DoAlwaysInclude!(arrType))
{
var ptr = (uint8*)arrPtr + stride * (count - 1);
DEFCHECK:for (var i = count - 1; i >= 0; i--)
{
// If this gets included, we'll have to include everything until here!
for (var j < stride)
if (ptr[j] != 0)
break DEFCHECK;
includeCount = i + 1;
ptr -= stride;
}
}
var ptr = (uint8*)arrPtr;
for (let i < includeCount)
{
bool isZero = true;
for (var j < stride)
if (ptr[j] != 0)
{
isZero = false;
break;
}
if (!isZero || includeAllValues || env.serializeFlags.HasFlag(.IncludeDefault) || DoAlwaysInclude!(arrType))
{
let inner = counts.Count - 1;
if (inner > 1)
{
int64[] innerCounts = scope .[inner];
for (let j < inner)
innerCounts[j] = counts[j + 1];
MultiDimensionalArray(writer, arrType, ptr, env, includeAllValues, params innerCounts);
}
else Array(writer, arrType, ptr, counts[1], env, includeAllValues);
writer.EntryEnd();
}
else Irrelevant(writer);
ptr += stride;
}
}
}
}
public static void GetPolyTypeName(Type type, String into)
{
let res = type.GetCustomAttribute<BonPolyNameAttribute>();
if (res case .Ok(let attr))
{
Debug.Assert(attr.name != null);
into.Append(attr.name);
}
else type.GetFullName(.. into);
}
[Inline]
public static void Type(BonWriter writer, Type type)
{
writer.EntryStart();
writer.Type(GetPolyTypeName(type, .. scope .(256)));
}
[Inline]
public static void Irrelevant(BonWriter writer, bool doOneLine = false)
{
writer.EntryStart(doOneLine);
writer.IrrelevantEntry();
writer.EntryEnd(doOneLine);
}
static mixin AsThingToString<T>(BonWriter writer, ValueView val)
{
T thing = *(T*)val.dataPtr;
thing.ToString(writer.outStr);
}
[Inline]
static void Integer(Type type, BonWriter writer, ValueView val)
{
switch (type)
{
case typeof(int8): AsThingToString!<int8>(writer, val);
case typeof(int16): AsThingToString!<int16>(writer, val);
case typeof(int32): AsThingToString!<int32>(writer, val);
case typeof(int64): AsThingToString!<int64>(writer, val);
case typeof(int): AsThingToString!<int>(writer, val);
case typeof(uint8): AsThingToString!<uint8>(writer, val);
case typeof(uint16): AsThingToString!<uint16>(writer, val);
case typeof(uint32): AsThingToString!<uint32>(writer, val);
case typeof(uint64): AsThingToString!<uint64>(writer, val);
case typeof(uint): AsThingToString!<uint>(writer, val);
default: Debug.FatalError(); // Should be unreachable
}
}
[Inline]
static void Char(Type type, BonWriter writer, ValueView val)
{
char32 char = 0;
switch (type)
{
case typeof(char8): char = (.)*(char8*)val.dataPtr;
case typeof(char16): char = (.)*(char16*)val.dataPtr;
case typeof(char32): char = *(char32*)val.dataPtr;
}
writer.Char(char);
}
[Inline]
static void Float(Type type, BonWriter writer, ValueView val)
{
switch (type)
{
case typeof(float):
float thing = *(float*)val.dataPtr;
thing.ToString(writer.outStr, "R", null); // Produce a string we can recreate... MS says this should be G9 when supported
case typeof(double):
double thing = *(double*)val.dataPtr;
thing.ToString(writer.outStr, "R", null); // Produce a string we can recreate... MS says this should be G17 when supported
default: Debug.FatalError(); // Should be unreachable
}
}
[Inline]
static void Bool(BonWriter writer, ValueView val, BonSerializeFlags flags)
{
bool boolean = *(bool*)val.dataPtr;
if (flags.HasFlag(.Verbose))
writer.Bool(boolean);
else (boolean ? 1 : 0).ToString(writer.outStr);
}
internal static ValueView GetPayloadValueFromEnumUnion(ValueView enumVal, out StringView caseName, out uint64 caseIndex)
{
// Enum union in memory:
// {<payload>|<discriminator>}
bool foundCase = false;
uint64 unionCaseIndex = 0;
uint64 currCaseIndex = 0;
for (var enumField in enumVal.type.GetFields())
{
if (enumField.[Friend]mFieldData.mFlags.HasFlag(.EnumDiscriminator))
{
let discrType = enumField.FieldType;
var discrVal = ValueView(discrType, (uint8*)enumVal.dataPtr + enumField.[Friend]mFieldData.mData);
Debug.Assert(discrType.IsInteger);
mixin GetVal<T>() where T : var
{
T thing = *(T*)discrVal.dataPtr;
unionCaseIndex = (uint64)thing;
}
switch (discrType)
{
case typeof(int8): GetVal!<int8>();
case typeof(int16): GetVal!<int16>();
case typeof(int32): GetVal!<int32>();
case typeof(int64): GetVal!<int64>();
case typeof(int): GetVal!<int>();
case typeof(uint8): GetVal!<uint8>();
case typeof(uint16): GetVal!<uint16>();
case typeof(uint32): GetVal!<uint32>();
case typeof(uint64): GetVal!<uint64>();
case typeof(uint): GetVal!<uint>();
default: Debug.FatalError(); // Should be unreachable
}
foundCase = true;
}
else if (enumField.[Friend]mFieldData.mFlags.HasFlag(.EnumCase)) // Filter through unioncaseIndex
{
Debug.Assert(foundCase);
// Skip enum cases until we get to the selected one
if (currCaseIndex != unionCaseIndex)
{
currCaseIndex++;
continue;
}
caseIndex = currCaseIndex;
caseName = enumField.Name;
return ValueView(enumField.FieldType, enumVal.dataPtr);
}
}
caseIndex = 0;
caseName = default;
return default;
}
}
}