/[RomCheater]/trunk/RomCheater/Docking/MemorySearch/SearchDataTypes.cs
ViewVC logotype

Contents of /trunk/RomCheater/Docking/MemorySearch/SearchDataTypes.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 524 - (show annotations) (download)
Wed Jun 5 07:32:57 2013 UTC (7 years, 3 months ago) by william
File size: 9559 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22