1 | namespace Oni.Metadata
|
---|
2 | {
|
---|
3 | internal class CompareVisitor : MetaTypeVisitor
|
---|
4 | {
|
---|
5 | private readonly BinaryReader reader1;
|
---|
6 | private readonly BinaryReader reader2;
|
---|
7 | private bool equals = true;
|
---|
8 |
|
---|
9 | public CompareVisitor(BinaryReader reader1, BinaryReader reader2)
|
---|
10 | {
|
---|
11 | this.reader1 = reader1;
|
---|
12 | this.reader2 = reader2;
|
---|
13 | }
|
---|
14 |
|
---|
15 | public bool AreEquals => equals;
|
---|
16 |
|
---|
17 | public override void VisitByte(MetaByte type) => equals = (reader1.ReadByte() == reader2.ReadByte());
|
---|
18 | public override void VisitInt16(MetaInt16 type) => equals = (reader1.ReadInt16() == reader2.ReadInt16());
|
---|
19 | public override void VisitInt32(MetaInt32 type) => equals = (reader1.ReadInt32() == reader2.ReadInt32());
|
---|
20 | public override void VisitUInt32(MetaUInt32 type) => equals = (reader1.ReadUInt32() == reader2.ReadUInt32());
|
---|
21 | public override void VisitInt64(MetaInt64 type) => equals = (reader1.ReadInt64() == reader2.ReadInt64());
|
---|
22 | public override void VisitUInt64(MetaUInt64 type) => equals = (reader1.ReadUInt64() == reader2.ReadUInt64());
|
---|
23 | public override void VisitFloat(MetaFloat type) => equals = (reader1.ReadSingle() == reader2.ReadSingle());
|
---|
24 | public override void VisitColor(MetaColor type) => equals = (reader1.ReadInt32() == reader2.ReadInt32());
|
---|
25 | public override void VisitRawOffset(MetaRawOffset type) => equals = (reader1.ReadInt32() == reader2.ReadInt32());
|
---|
26 | public override void VisitSepOffset(MetaSepOffset type) => equals = (reader1.ReadInt32() == reader2.ReadInt32());
|
---|
27 | public override void VisitPointer(MetaPointer type) => equals = (reader1.ReadInt32() == reader2.ReadInt32());
|
---|
28 | public override void VisitString(MetaString type) => equals = (reader1.ReadString(type.Count) == reader2.ReadString(type.Count));
|
---|
29 |
|
---|
30 | public override void VisitPadding(MetaPadding type)
|
---|
31 | {
|
---|
32 | reader1.Skip(type.Count);
|
---|
33 | reader2.Skip(type.Count);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public override void VisitStruct(MetaStruct type)
|
---|
37 | {
|
---|
38 | foreach (var field in type.Fields)
|
---|
39 | {
|
---|
40 | field.Type.Accept(this);
|
---|
41 |
|
---|
42 | if (!equals)
|
---|
43 | break;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override void VisitArray(MetaArray type)
|
---|
48 | {
|
---|
49 | CompareArray(type.ElementType, type.Count);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override void VisitVarArray(MetaVarArray type)
|
---|
53 | {
|
---|
54 | int count1, count2;
|
---|
55 |
|
---|
56 | if (type.CountField.Type == MetaType.Int16)
|
---|
57 | {
|
---|
58 | count1 = reader1.ReadInt16();
|
---|
59 | count2 = reader2.ReadInt16();
|
---|
60 | }
|
---|
61 | else
|
---|
62 | {
|
---|
63 | count1 = reader1.ReadInt32();
|
---|
64 | count2 = reader2.ReadInt32();
|
---|
65 | }
|
---|
66 |
|
---|
67 | equals = (count1 == count2);
|
---|
68 |
|
---|
69 | if (!equals)
|
---|
70 | return;
|
---|
71 |
|
---|
72 | CompareArray(type.ElementType, count1);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void CompareArray(MetaType elementType, int count)
|
---|
76 | {
|
---|
77 | for (int i = 0; i < count; i++)
|
---|
78 | {
|
---|
79 | elementType.Accept(this);
|
---|
80 |
|
---|
81 | if (!equals)
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|