-
Notifications
You must be signed in to change notification settings - Fork 8
/
uReader.cs
154 lines (125 loc) · 4.38 KB
/
uReader.cs
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
using UnityEngine;
using System.Runtime.InteropServices;
using System.IO;
using System;
using System.Text;
namespace uSource
{
public class uReader : BinaryReader
{
public Stream InputStream;
public uReader(Stream InputStream)
: base(InputStream)
{
this.InputStream = InputStream;
if (!InputStream.CanRead)
throw new InvalidDataException("Stream unreadable!");
}
public byte[] GetBytes(int Count, long Offset)
{
if (!Offset.Equals(0) && !Offset.Equals(InputStream.Position))
InputStream.Seek(Offset, SeekOrigin.Begin);
byte[] Buffer = new byte[Count];
InputStream.Read(Buffer, 0, Buffer.Length);
return Buffer;
}
public void ReadType<T>(ref T Variable, long? Offset = null)
{
if (Offset.HasValue)
InputStream.Seek(Offset.Value, SeekOrigin.Begin);
Byte[] Buffer = new byte[Marshal.SizeOf(typeof(T))];
InputStream.Read(Buffer, 0, Buffer.Length);
GCHandle Handle = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
try
{
Variable = (T)Marshal.PtrToStructure(Handle.AddrOfPinnedObject(), typeof(T));
}
finally
{
Handle.Free();
}
}
public void ReadArray<T>(ref T[] Array, long? Offset = null)
{
if (Offset.HasValue)
InputStream.Seek(Offset.Value, SeekOrigin.Begin);
for (Int32 i = 0; i < Array.Length; i++)
ReadType(ref Array[i]);
}
public void ReadTypeFixed<T>(ref T Variable, Int32 TypeSizeOf, long? Offset = null)
{
if (Offset.HasValue)
InputStream.Seek(Offset.Value, SeekOrigin.Begin);
Byte[] Buffer = new byte[TypeSizeOf];
InputStream.Read(Buffer, 0, Buffer.Length);
GCHandle Handle = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
try
{
Variable = (T)Marshal.PtrToStructure(Handle.AddrOfPinnedObject(), typeof(T));
}
finally
{
Handle.Free();
}
}
public void ReadArrayFixed<T>(ref T[] Array, Int32 TypeSizeOf, long? Offset = null)
{
if (Offset.HasValue)
InputStream.Seek(Offset.Value, SeekOrigin.Begin);
for (Int32 i = 0; i < Array.Length; i++)
ReadTypeFixed(ref Array[i], TypeSizeOf);
}
[ThreadStatic]
private static StringBuilder _sBuilder;
public String ReadNullTerminatedString(long? Offset = null)
{
if (Offset.HasValue && !Offset.Value.Equals(InputStream.Position))
InputStream.Seek(Offset.Value, SeekOrigin.Begin);
if (_sBuilder == null)
_sBuilder = new StringBuilder();
else
_sBuilder.Remove(0, _sBuilder.Length);
while (true)
{
Char c = (char)InputStream.ReadByte();
if (c == 0)
return _sBuilder.ToString();
_sBuilder.Append(c);
}
}
public Vector3 ReadVector2D()
{
Vector2 Vector2D;// = new Vector2(ReadSingle(), ReadSingle());
Vector2D.x = ReadSingle();
Vector2D.y = ReadSingle();
return Vector2D;
}
public Vector3 ReadVector3D(bool SwapZY = true)
{
Vector3 Vector3D;// = new Vector3(ReadSingle(), ReadSingle(), ReadSingle());
Vector3D.x = ReadSingle();
Vector3D.y = ReadSingle();
Vector3D.z = ReadSingle();
if (SwapZY)
{
//float x = Vector3D.x;
//float y = Vector3D.y;
//float z = Vector3D.z;
Single tempX = Vector3D.x;
Vector3D.x = -Vector3D.y;
Vector3D.y = Vector3D.z;
Vector3D.z = tempX;
}
return Vector3D;
}
public Vector3 ReadVector4D()
{
Vector4 Vector4D;
Vector4D.x = ReadSingle();
Vector4D.y = ReadSingle();
Vector4D.z = ReadSingle();
Vector4D.w = ReadSingle();
return Vector4D;
}
}
}