source: OniSplit/Metadata/LinkVisitor.cs@ 1139

Last change on this file since 1139 was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 2.7 KB
RevLine 
[1114]1using System;
2using System.Collections.Generic;
3
4namespace Oni.Metadata
5{
6 internal class LinkVisitor : MetaTypeVisitor
7 {
8 private readonly List<int> links = new List<int>();
9 private readonly BinaryReader reader;
10
11 public LinkVisitor(BinaryReader reader)
12 {
13 this.reader = reader;
14 }
15
16 public ICollection<int> Links => links;
17
18 public override void VisitByte(MetaByte type) => reader.Position += type.Size;
19 public override void VisitInt16(MetaInt16 type) => reader.Position += type.Size;
20 public override void VisitUInt16(MetaUInt16 type) => reader.Position += type.Size;
21 public override void VisitInt32(MetaInt32 type) => reader.Position += type.Size;
22 public override void VisitUInt32(MetaUInt32 type) => reader.Position += type.Size;
23 public override void VisitInt64(MetaInt64 type) => reader.Position += type.Size;
24 public override void VisitUInt64(MetaUInt64 type) => reader.Position += type.Size;
25 public override void VisitFloat(MetaFloat type) => reader.Position += type.Size;
26 public override void VisitColor(MetaColor type) => reader.Position += type.Size;
27 public override void VisitRawOffset(MetaRawOffset type) => reader.Position += type.Size;
28 public override void VisitSepOffset(MetaSepOffset type) => reader.Position += type.Size;
29
30 public override void VisitPointer(MetaPointer type)
31 {
32 int id = reader.ReadInt32();
33
34 if (id != 0)
35 links.Add(id);
36 }
37
38 public override void VisitString(MetaString type) => reader.Position += type.Size;
39 public override void VisitPadding(MetaPadding type) => reader.Position += type.Size;
40
41 public override void VisitStruct(MetaStruct type)
42 {
43 foreach (Field field in type.Fields)
44 field.Type.Accept(this);
45 }
46
47 public override void VisitArray(MetaArray type) => VisitArray(type.ElementType, type.Count);
48
49 public override void VisitVarArray(MetaVarArray type)
50 {
51 int count;
52
53 if (type.CountField.Type == MetaType.Int16)
54 count = reader.ReadUInt16();
55 else
56 count = reader.ReadInt32();
57
58 VisitArray(type.ElementType, count);
59 }
60
61 private void VisitArray(MetaType elementType, int count)
62 {
63 if (elementType is MetaPointer || elementType is MetaStruct)
64 {
65 for (int i = 0; i < count; i++)
66 elementType.Accept(this);
67 }
68 else
69 {
70 reader.Position += elementType.Size * count;
71 }
72 }
73 }
74}
Note: See TracBrowser for help on using the repository browser.