/[RomCheater]/trunk/RomCheater.Core/BitTools.cs
ViewVC logotype

Contents of /trunk/RomCheater.Core/BitTools.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (show annotations) (download)
Fri Jun 7 12:27:51 2013 UTC (7 years, 4 months ago) by william
File size: 10849 byte(s)
remove private modifer from: const byte BitsPerByte=8  (it is redundant to declare it private, because no access modifier is automatically marked private by default)

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 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, Func<bool> cancelmethod) 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 //list = new List<T>();
147 //using (MemoryStream ms = new MemoryStream(data))
148 //{
149 // using (BinaryReader br = new BinaryReader(ms))
150 // {
151 // while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadByte(), typeof(T))); if (cancelmethod.Invoke()) { break; } }
152 // }
153 //}
154 //break;
155 throw new NotSupportedException("Conversion from byte[] to byte[] is not supported because the data can be used without conversion.");
156 case "sbyte": // signed byte
157 //list = new List<T>();
158 //using (MemoryStream ms = new MemoryStream(data))
159 //{
160 // using (BinaryReader br = new BinaryReader(ms))
161 // {
162 // while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadSByte(), typeof(T))); if (cancelmethod.Invoke()) { break; } }
163 // }
164 //}
165 //break;
166 throw new NotSupportedException("Conversion from byte[] to sbyte[] is not supported because the data can be used without conversion.");
167 case "int16": // short
168 list = new List<T>();
169 using (MemoryStream ms = new MemoryStream(data))
170 {
171 using (BinaryReader br = new BinaryReader(ms))
172 {
173 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt16(), typeof(T))); if (cancelmethod.Invoke()) { break; } }
174 }
175 }
176 break;
177 case "uint16": // ushort
178 list = new List<T>();
179 using (MemoryStream ms = new MemoryStream(data))
180 {
181 using (BinaryReader br = new BinaryReader(ms))
182 {
183 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt16(), typeof(T))); if (cancelmethod.Invoke()) { break; } }
184 }
185 }
186 break;
187 case "int32": // int
188 list = new List<T>();
189 using (MemoryStream ms = new MemoryStream(data))
190 {
191 using (BinaryReader br = new BinaryReader(ms))
192 {
193 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt32(), typeof(T))); if (cancelmethod.Invoke()) { break; } }
194 }
195 }
196 break;
197 case "uint32": // uint
198 list = new List<T>();
199 using (MemoryStream ms = new MemoryStream(data))
200 {
201 using (BinaryReader br = new BinaryReader(ms))
202 {
203 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt32(), typeof(T))); if (cancelmethod.Invoke()) { break; } }
204 }
205 }
206 break;
207 case "int64": // long
208 list = new List<T>();
209 using (MemoryStream ms = new MemoryStream(data))
210 {
211 using (BinaryReader br = new BinaryReader(ms))
212 {
213 while (br.BaseStream.Position < br.BaseStream.Length) {list.Add((T)Convert.ChangeType(br.ReadInt64(), typeof(T))); if (cancelmethod.Invoke()) { break; } }
214 }
215 }
216 break;
217 case "uint64": // ulong
218 list = new List<T>();
219 using (MemoryStream ms = new MemoryStream(data))
220 {
221 using (BinaryReader br = new BinaryReader(ms))
222 {
223 while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt64(), typeof(T))); if (cancelmethod.Invoke()) { break; } }
224 }
225 }
226 break;
227 default:
228 throw new InvalidOperationException("Encountered invalid type when converting byte array", new TypeLoadException(string.Format("Type: '{0}' was unexpected.", name)));
229 }
230 return list.ToArray();
231 }
232 #endregion
233 }
234 }

  ViewVC Help
Powered by ViewVC 1.1.22