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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (hide annotations) (download)
Wed Jun 5 09:01:44 2013 UTC (7 years, 4 months ago) by william
File size: 10507 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22