1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.IO; |
6 |
|
7 |
namespace RomCheater.Core |
8 |
{ |
9 |
public static class BitTools |
10 |
{ |
11 |
private const byte BitsPerByte = 8; |
12 |
|
13 |
#region SizeOf Members |
14 |
public static T SizeOf<T>() where T : IConvertible |
15 |
{ |
16 |
Type t = typeof(T); |
17 |
string name = t.Name.ToLower(); |
18 |
switch (name) |
19 |
{ |
20 |
case "byte": // unsigned byte |
21 |
return (T)Convert.ChangeType(sizeof(byte), typeof(T)); |
22 |
case "sbyte": // signed byte |
23 |
return (T)Convert.ChangeType(sizeof(sbyte), typeof(T)); |
24 |
case "int16": // short |
25 |
return (T)Convert.ChangeType(sizeof(short), typeof(T)); |
26 |
case "uint16": // ushort |
27 |
return (T)Convert.ChangeType(sizeof(ushort), typeof(T)); |
28 |
case "int32": // int |
29 |
return (T)Convert.ChangeType(sizeof(int), typeof(T)); |
30 |
case "uint32": // uint |
31 |
return (T)Convert.ChangeType(sizeof(uint), typeof(T)); |
32 |
case "int64": // long |
33 |
return (T)Convert.ChangeType(sizeof(long), typeof(T)); |
34 |
case "uint64": // ulong |
35 |
return (T)Convert.ChangeType(sizeof(ulong), typeof(T)); |
36 |
default: |
37 |
throw new InvalidOperationException("Encountered invalid type when converting byte array", new TypeLoadException(string.Format("Type: '{0}' was unexpected.", name))); |
38 |
} |
39 |
} |
40 |
public static T SizeOf<T>(SearchDataTypes sdt) where T : IConvertible |
41 |
{ |
42 |
if (!Enum.IsDefined(typeof(SearchDataTypes), sdt)) |
43 |
{ |
44 |
throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt"); |
45 |
} |
46 |
return (T)Convert.ChangeType(((int)sdt / BitsPerByte), typeof(T)); |
47 |
} |
48 |
|
49 |
#region sbyte/byte output |
50 |
public static void SizeOf(SearchDataTypes sdt, out sbyte size) |
51 |
{ |
52 |
size = 0; |
53 |
if (!Enum.IsDefined(typeof(SearchDataTypes), sdt)) |
54 |
{ |
55 |
throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt"); |
56 |
} |
57 |
size = Convert.ToSByte((sbyte)sdt / BitsPerByte); |
58 |
} |
59 |
public static void SizeOf(SearchDataTypes sdt, out byte size) |
60 |
{ |
61 |
size = 0; |
62 |
if (!Enum.IsDefined(typeof(SearchDataTypes), sdt)) |
63 |
{ |
64 |
throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt"); |
65 |
} |
66 |
size = Convert.ToByte((byte)sdt / BitsPerByte); |
67 |
} |
68 |
#endregion |
69 |
|
70 |
#region short/ushort output |
71 |
public static void SizeOf(SearchDataTypes sdt, out short size) |
72 |
{ |
73 |
size = 0; |
74 |
if (!Enum.IsDefined(typeof(SearchDataTypes), sdt)) |
75 |
{ |
76 |
throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt"); |
77 |
} |
78 |
size = Convert.ToInt16((short)sdt / BitsPerByte); |
79 |
} |
80 |
public static void SizeOf(SearchDataTypes sdt, out ushort size) |
81 |
{ |
82 |
size = 0; |
83 |
if (!Enum.IsDefined(typeof(SearchDataTypes), sdt)) |
84 |
{ |
85 |
throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt"); |
86 |
} |
87 |
size = Convert.ToUInt16((ushort)sdt / BitsPerByte); |
88 |
} |
89 |
#endregion |
90 |
|
91 |
#region int/uint output |
92 |
public static void SizeOf(SearchDataTypes sdt, out int size) |
93 |
{ |
94 |
size = 0; |
95 |
if (!Enum.IsDefined(typeof(SearchDataTypes), sdt)) |
96 |
{ |
97 |
throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt"); |
98 |
} |
99 |
size = Convert.ToInt32((int)sdt / BitsPerByte); |
100 |
} |
101 |
public static void SizeOf(SearchDataTypes sdt, out uint size) |
102 |
{ |
103 |
size = 0; |
104 |
if (!Enum.IsDefined(typeof(SearchDataTypes), sdt)) |
105 |
{ |
106 |
throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt"); |
107 |
} |
108 |
size = Convert.ToUInt32((uint)sdt / BitsPerByte); |
109 |
} |
110 |
#endregion |
111 |
|
112 |
#region long/ulong output |
113 |
public static void SizeOf(SearchDataTypes sdt, out long size) |
114 |
{ |
115 |
size = 0; |
116 |
if (!Enum.IsDefined(typeof(SearchDataTypes), sdt)) |
117 |
{ |
118 |
throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt"); |
119 |
} |
120 |
size = Convert.ToInt64((uint)sdt / BitsPerByte); |
121 |
} |
122 |
public static void SizeOf(SearchDataTypes sdt, out ulong size) |
123 |
{ |
124 |
size = 0; |
125 |
if (!Enum.IsDefined(typeof(SearchDataTypes), sdt)) |
126 |
{ |
127 |
throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt"); |
128 |
} |
129 |
size = Convert.ToUInt64((uint)sdt / BitsPerByte); |
130 |
} |
131 |
#endregion |
132 |
#endregion |
133 |
|
134 |
#region Structure Support |
135 |
|
136 |
public static T[] ConvertByteArray<T>(byte[] data) where T : IConvertible |
137 |
{ |
138 |
Type t = typeof(T); |
139 |
string name = t.Name.ToLower(); |
140 |
|
141 |
List<T> list = new List<T>(); |
142 |
|
143 |
switch (name) |
144 |
{ |
145 |
case "byte": // unsigned byte |
146 |
case "sbyte": // signed byte |
147 |
list = data.Cast<T>().ToList(); |
148 |
break; |
149 |
case "int16": // short |
150 |
list = new List<T>(); |
151 |
using (MemoryStream ms = new MemoryStream(data)) |
152 |
{ |
153 |
using (BinaryReader br = new BinaryReader(ms)) |
154 |
{ |
155 |
while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt16(), typeof(T))); } |
156 |
} |
157 |
} |
158 |
break; |
159 |
case "uint16": // ushort |
160 |
list = new List<T>(); |
161 |
using (MemoryStream ms = new MemoryStream(data)) |
162 |
{ |
163 |
using (BinaryReader br = new BinaryReader(ms)) |
164 |
{ |
165 |
while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt16(), typeof(T))); } |
166 |
} |
167 |
} |
168 |
break; |
169 |
case "int32": // int |
170 |
list = new List<T>(); |
171 |
using (MemoryStream ms = new MemoryStream(data)) |
172 |
{ |
173 |
using (BinaryReader br = new BinaryReader(ms)) |
174 |
{ |
175 |
while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt32(), typeof(T))); } |
176 |
} |
177 |
} |
178 |
break; |
179 |
case "uint32": // uint |
180 |
list = new List<T>(); |
181 |
using (MemoryStream ms = new MemoryStream(data)) |
182 |
{ |
183 |
using (BinaryReader br = new BinaryReader(ms)) |
184 |
{ |
185 |
while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt32(), typeof(T))); } |
186 |
} |
187 |
} |
188 |
break; |
189 |
case "int64": // long |
190 |
list = new List<T>(); |
191 |
using (MemoryStream ms = new MemoryStream(data)) |
192 |
{ |
193 |
using (BinaryReader br = new BinaryReader(ms)) |
194 |
{ |
195 |
while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt64(), typeof(T))); } |
196 |
} |
197 |
} |
198 |
break; |
199 |
case "uint64": // ulong |
200 |
list = new List<T>(); |
201 |
using (MemoryStream ms = new MemoryStream(data)) |
202 |
{ |
203 |
using (BinaryReader br = new BinaryReader(ms)) |
204 |
{ |
205 |
while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt64(), typeof(T))); } |
206 |
} |
207 |
} |
208 |
break; |
209 |
default: |
210 |
throw new InvalidOperationException("Encountered invalid type when converting byte array", new TypeLoadException(string.Format("Type: '{0}' was unexpected.", name))); |
211 |
} |
212 |
return list.ToArray(); |
213 |
} |
214 |
#endregion |
215 |
} |
216 |
} |