source: OniSplit/Metadata/DumpVisitor.cs@ 1133

Last change on this file since 1133 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: 6.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4
5namespace Oni.Metadata
6{
7 internal class DumpVisitor : MetaTypeVisitor
8 {
9 #region Private data
10 private readonly InstanceMetadata metadata;
11 private readonly TextWriter writer;
12 private Stack<ActiveField> activeFields;
13 private MetaType topLevelType;
14 private MetaType currentType;
15 private int fieldSize;
16 private int position;
17 private string indent = "";
18 private Field field;
19 #endregion
20
21 #region private class ActiveField
22
23 private class ActiveField
24 {
25 public Field Field;
26 public int Index;
27
28 public ActiveField(Field field)
29 {
30 this.Field = field;
31 this.Index = -1;
32 }
33 }
34
35 #endregion
36
37 public DumpVisitor(TextWriter writer, InstanceMetadata metadata)
38 {
39 this.writer = writer;
40 this.metadata = metadata;
41 }
42
43 public MetaType TopLevelType => topLevelType;
44 public MetaType Type => currentType;
45
46 public Field Field
47 {
48 get
49 {
50 if (activeFields.Count == 0)
51 return null;
52
53 return activeFields.Peek().Field;
54 }
55 }
56
57 public int Position => position;
58
59 public string GetCurrentFieldName()
60 {
61 if (activeFields.Count == 0)
62 return null;
63
64 List<string> names = new List<string>();
65
66 foreach (ActiveField state in activeFields)
67 {
68 if (string.IsNullOrEmpty(state.Field.Name))
69 return null;
70
71 if (state.Index >= 0)
72 names.Add(string.Format("{0}[{1}]", state.Field.Name, state.Index));
73 else
74 names.Add(state.Field.Name);
75 }
76
77 names.Add(topLevelType.Name);
78 names.Reverse();
79
80 return string.Join(".", names.ToArray());
81 }
82
83 public string GetParentFieldName()
84 {
85 if (activeFields.Count == 0)
86 return null;
87
88 var names = new List<string>();
89
90 foreach (ActiveField state in activeFields)
91 {
92 if (string.IsNullOrEmpty(state.Field.Name))
93 return null;
94
95 if (state.Index >= 0)
96 names.Add(string.Format("{0}[{1}]", state.Field.Name, state.Index));
97 else
98 names.Add(state.Field.Name);
99 }
100
101 names.Add(topLevelType.Name);
102 names.Reverse();
103 names.RemoveAt(names.Count - 1);
104
105 return string.Join(".", names.ToArray());
106 }
107
108 public override void VisitByte(MetaByte type)
109 {
110 writer.Write(indent);
111 writer.Write("uint8_t");
112 }
113
114 public override void VisitInt16(MetaInt16 type)
115 {
116 writer.Write(indent);
117 writer.Write("int16_t");
118 }
119
120 public override void VisitUInt16(MetaUInt16 type)
121 {
122 writer.Write(indent);
123 writer.Write("uint16_t");
124 }
125
126 public override void VisitInt32(MetaInt32 type)
127 {
128 writer.Write(indent);
129 writer.Write("int32_t");
130 }
131
132 public override void VisitUInt32(MetaUInt32 type)
133 {
134 writer.Write(indent);
135 writer.Write("uint32_t");
136 }
137
138 public override void VisitInt64(MetaInt64 type)
139 {
140 writer.Write(indent);
141 writer.Write("int64_t");
142 }
143
144 public override void VisitUInt64(MetaUInt64 type)
145 {
146 writer.Write(indent);
147 writer.Write("uint64_t");
148 }
149
150 public override void VisitFloat(MetaFloat type)
151 {
152 writer.Write(indent);
153 writer.Write("float");
154 }
155
156 public override void VisitColor(MetaColor type)
157 {
158 writer.Write(indent);
159 writer.Write("uint32_t");
160 }
161
162 public override void VisitRawOffset(MetaRawOffset type)
163 {
164 writer.Write(indent);
165 writer.Write("void*");
166 }
167
168 public override void VisitSepOffset(MetaSepOffset type)
169 {
170 writer.Write(indent);
171 writer.Write("void*");
172 }
173
174 public override void VisitPointer(MetaPointer type)
175 {
176 writer.Write(indent);
177 writer.Write("{0}*", metadata.GetTemplate(type.Tag).Type.Name);
178 }
179
180 public override void VisitBoundingBox(MetaBoundingBox type)
181 {
182 writer.Write(indent);
183 writer.Write("bbox");
184 }
185
186 public override void VisitBoundingSphere(MetaBoundingSphere type)
187 {
188 writer.Write(indent);
189 writer.Write("bsphere");
190 }
191
192 public override void VisitMatrix4x3(MetaMatrix4x3 type)
193 {
194 writer.Write(indent);
195 writer.Write("matrix43");
196 }
197
198 public override void VisitPlane(MetaPlane type)
199 {
200 writer.Write(indent);
201 writer.Write("plane");
202 }
203
204 public override void VisitQuaternion(MetaQuaternion type)
205 {
206 writer.Write(indent);
207 writer.Write("quat");
208 }
209
210 public override void VisitVector2(MetaVector2 type)
211 {
212 writer.Write(indent);
213 writer.Write("vec2");
214 }
215
216 public override void VisitVector3(MetaVector3 type)
217 {
218 writer.Write(indent);
219 writer.Write("vec3");
220 }
221
222 public override void VisitString(MetaString type)
223 {
224 writer.Write(indent);
225 writer.Write("const char*");
226 }
227
228 public override void VisitPadding(MetaPadding type)
229 {
230 writer.Write(indent);
231 writer.Write("std::array<{0}>", type.Size);
232 }
233
234 public override void VisitStruct(MetaStruct type)
235 {
236 if (topLevelType == null)
237 topLevelType = type;
238
239 writer.WriteLine();
240 writer.WriteLine("struct {0} {{", type.Name);
241 indent += "\t";
242
243 foreach (Field field in type.Fields)
244 {
245 field.Type.Accept(this);
246 writer.WriteLine(" {0};", field.Name);
247 }
248
249 indent = indent.Substring(0, indent.Length - 1);
250 writer.WriteLine("};");
251 }
252
253 public override void VisitArray(MetaArray type)
254 {
255 writer.Write(indent);
256 }
257
258 public override void VisitVarArray(MetaVarArray type)
259 {
260 }
261 }
262}
Note: See TracBrowser for help on using the repository browser.