/[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 521 - (show annotations) (download)
Wed Jun 5 07:06:53 2013 UTC (7 years, 1 month ago) by william
File size: 9625 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 = new List<T>();
159 data.ToList().ForEach(s => list.Add((T)Convert.ChangeType(s, typeof(T))));
160 break;
161 case "int16": // short
162 list = new List<T>();
163 using (MemoryStream ms = new MemoryStream(data))
164 {
165 using (BinaryReader br = new BinaryReader(ms))
166 {
167 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt16(), typeof(T))); }
168 }
169 }
170 break;
171 case "uint16": // ushort
172 list = new List<T>();
173 using (MemoryStream ms = new MemoryStream(data))
174 {
175 using (BinaryReader br = new BinaryReader(ms))
176 {
177 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt16(), typeof(T))); }
178 }
179 }
180 break;
181 case "int32": // int
182 list = new List<T>();
183 using (MemoryStream ms = new MemoryStream(data))
184 {
185 using (BinaryReader br = new BinaryReader(ms))
186 {
187 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt32(), typeof(T))); }
188 }
189 }
190 break;
191 case "uint32": // uint
192 list = new List<T>();
193 using (MemoryStream ms = new MemoryStream(data))
194 {
195 using (BinaryReader br = new BinaryReader(ms))
196 {
197 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt32(), typeof(T))); }
198 }
199 }
200 break;
201 case "int64": // long
202 list = new List<T>();
203 using (MemoryStream ms = new MemoryStream(data))
204 {
205 using (BinaryReader br = new BinaryReader(ms))
206 {
207 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt64(), typeof(T))); }
208 }
209 }
210 break;
211 case "uint64": // ulong
212 list = new List<T>();
213 using (MemoryStream ms = new MemoryStream(data))
214 {
215 using (BinaryReader br = new BinaryReader(ms))
216 {
217 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt64(), typeof(T))); }
218 }
219 }
220 break;
221 default:
222 throw new InvalidOperationException("Encountered invalid type when converting byte array",new TypeLoadException(string.Format("Type: '{0}' was unexpected.", name)));
223 }
224 return list.ToArray();
225 }
226 #endregion
227 }
228 }

  ViewVC Help
Powered by ViewVC 1.1.22