source: OniSplit/Metadata/MetaType.cs@ 1182

Last change on this file since 1182 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.3 KB
RevLine 
[1114]1using System;
2using System.IO;
3
4namespace Oni.Metadata
5{
6 internal abstract class MetaType
7 {
8 private string name;
9 private int size;
10 private bool isFixedSize;
11 private bool? isLeaf;
12
13 protected MetaType()
14 {
15 }
16
17 protected MetaType(string name, int size)
18 {
19 this.size = size;
20 this.name = name;
21 }
22
23 #region Primitive Types
24
25 public static readonly MetaChar Char = new MetaChar();
26 public static readonly MetaByte Byte = new MetaByte();
27 public static readonly MetaInt16 Int16 = new MetaInt16();
28 public static readonly MetaUInt16 UInt16 = new MetaUInt16();
29 public static readonly MetaInt32 Int32 = new MetaInt32();
30 public static readonly MetaUInt32 UInt32 = new MetaUInt32();
31 public static readonly MetaInt64 Int64 = new MetaInt64();
32 public static readonly MetaUInt64 UInt64 = new MetaUInt64();
33 public static readonly MetaColor Color = new MetaColor();
34 public static readonly MetaFloat Float = new MetaFloat();
35 public static readonly MetaVector2 Vector2 = new MetaVector2();
36 public static readonly MetaVector3 Vector3 = new MetaVector3();
37 public static readonly MetaQuaternion Quaternion = new MetaQuaternion();
38 public static readonly MetaPlane Plane = new MetaPlane();
39 public static readonly MetaBoundingBox BoundingBox = new MetaBoundingBox();
40 public static readonly MetaBoundingSphere BoundingSphere = new MetaBoundingSphere();
41 public static readonly MetaMatrix4x3 Matrix4x3 = new MetaMatrix4x3();
42 public static readonly MetaRawOffset RawOffset = new MetaRawOffset();
43 public static readonly MetaSepOffset SepOffset = new MetaSepOffset();
44 public static readonly MetaString String16 = new MetaString(16);
45 public static readonly MetaString String32 = new MetaString(32);
46 public static readonly MetaString String48 = new MetaString(48);
47 public static readonly MetaString String63 = new MetaString(63);
48 public static readonly MetaString String64 = new MetaString(64);
49 public static readonly MetaString String128 = new MetaString(128);
50 public static readonly MetaString String256 = new MetaString(256);
51
52 public static MetaPadding Padding(int length) => new MetaPadding(length);
53
54 public static MetaPadding Padding(int length, byte fillByte) => new MetaPadding(length, fillByte);
55
56 public static MetaArray Array(int length, MetaType elementType) => new MetaArray(elementType, length);
57
58 public static MetaVarArray ShortVarArray(MetaType elementType) => new MetaVarArray(Int16, elementType);
59
60 public static MetaVarArray VarArray(MetaType elementType) => new MetaVarArray(Int32, elementType);
61
62 public static MetaString String(int length)
63 {
64 switch (length)
65 {
66 case 16:
67 return String16;
68 case 32:
69 return String32;
70 case 64:
71 return String64;
72 case 128:
73 return String128;
74 case 256:
75 return String256;
76 default:
77 return new MetaString(length);
78 }
79 }
80
81 public static MetaPointer Pointer(TemplateTag tag) => new MetaPointer(tag);
82
83 public static MetaEnum Enum<T>()
84 {
85 Type underlyingType = System.Enum.GetUnderlyingType(typeof(T));
86
87 if (underlyingType == typeof(Byte))
88 return new MetaEnum(MetaType.Byte, typeof(T));
89 if (underlyingType == typeof(Int16))
90 return new MetaEnum(MetaType.Int16, typeof(T));
91 if (underlyingType == typeof(UInt16))
92 return new MetaEnum(MetaType.UInt16, typeof(T));
93 if (underlyingType == typeof(Int32))
94 return new MetaEnum(MetaType.Int32, typeof(T));
95 if (underlyingType == typeof(UInt32))
96 return new MetaEnum(MetaType.UInt32, typeof(T));
97 if (underlyingType == typeof(Int64))
98 return new MetaEnum(MetaType.Int64, typeof(T));
99 if (underlyingType == typeof(UInt64))
100 return new MetaEnum(MetaType.UInt64, typeof(T));
101
102 throw new InvalidOperationException(System.String.Format("Unsupported enum type {0}", underlyingType));
103 }
104
105 #endregion
106
107 public string Name
108 {
109 get { return name; }
110 protected set { name = value; }
111 }
112
113 public int Size
114 {
115 get { return size; }
116 protected set { size = value; }
117 }
118
119 public bool IsFixedSize
120 {
121 get { return isFixedSize; }
122 protected set { isFixedSize = value; }
123 }
124
125 public bool IsLeaf
126 {
127 get
128 {
129 if (!isLeaf.HasValue)
130 isLeaf = IsLeafImpl();
131
132 return isLeaf.Value;
133 }
134 }
135
136 protected abstract bool IsLeafImpl();
137
138 public bool IsBlittable
139 {
140 get
141 {
142 return (this == MetaType.Byte
143 || this == MetaType.Int16
144 || this == MetaType.UInt16
145 || this == MetaType.Int32
146 || this == MetaType.UInt32
147 || this == MetaType.Int64
148 || this == MetaType.UInt64
149 || this == MetaType.Float
150 || this == MetaType.Color
151 || this == MetaType.Matrix4x3
152 || this == MetaType.Plane
153 || this == MetaType.Quaternion
154 || this == MetaType.Vector2
155 || this == MetaType.Vector3);
156 }
157 }
158
159 public abstract void Accept(IMetaTypeVisitor visitor);
160
161 internal int Copy(BinaryReader input, BinaryWriter output, Action<CopyVisitor> callback)
162 {
163 var copyVisitor = new CopyVisitor(input, output, callback);
164 Accept(copyVisitor);
165 return copyVisitor.Position;
166 }
167 }
168}
Note: See TracBrowser for help on using the repository browser.