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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/RomCheater/Docking/MemorySearch/SearchDataTypes.cs	2013/06/05 04:50:44	520
+++ trunk/RomCheater/Docking/MemorySearch/SearchDataTypes.cs	2013/06/05 07:06:53	521
@@ -2,6 +2,8 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Runtime.InteropServices;
+using System.IO;
 
 namespace RomCheater.Docking.MemorySearch
 {
@@ -15,6 +17,43 @@
     public static class BitTools
     {
         private const byte BitsPerByte = 8;
+
+        #region SizeOf Members
+        public static T SizeOf<T>() where T : IConvertible
+        {
+            Type t = typeof(T);
+            string name = t.Name.ToLower();
+            switch (name)
+            {
+                case "byte":  // unsigned byte
+                    return (T)Convert.ChangeType(sizeof(byte), typeof(T));
+                case "sbyte": // signed byte
+                    return (T)Convert.ChangeType(sizeof(sbyte), typeof(T));
+                case "int16": // short
+                    return (T)Convert.ChangeType(sizeof(short), typeof(T));
+                case "uint16": // ushort
+                    return (T)Convert.ChangeType(sizeof(ushort), typeof(T));
+                case "int32": // int
+                    return (T)Convert.ChangeType(sizeof(int), typeof(T));
+                case "uint32": // uint
+                    return (T)Convert.ChangeType(sizeof(uint), typeof(T));
+                case "int64": // long
+                    return (T)Convert.ChangeType(sizeof(long), typeof(T));
+                case "uint64": // ulong
+                    return (T)Convert.ChangeType(sizeof(ulong), typeof(T));
+                default:
+                    throw new InvalidOperationException("Encountered invalid type when converting byte array", new TypeLoadException(string.Format("Type: '{0}' was unexpected.", name)));
+            }
+        }
+        public static T SizeOf<T>(SearchDataTypes sdt) where T: IConvertible
+        {
+            if (!Enum.IsDefined(typeof(SearchDataTypes), sdt))
+            {
+                throw new ArgumentException(string.Format("DataType: 0x{0} has not been defined", ((int)sdt).ToString("X")), "sdt");
+            }
+            return (T)Convert.ChangeType(((int)sdt / BitsPerByte), typeof(T));
+        }
+
         #region sbyte/byte output
         public static void SizeOf(SearchDataTypes sdt, out sbyte size)
         {
@@ -98,5 +137,92 @@
             size = Convert.ToUInt64((uint)sdt / BitsPerByte);
         }
         #endregion
+        #endregion
+
+
+
+
+        #region Structure Support
+
+        public static T[] ConvertByteArray<T>(byte[] data) where T: IConvertible
+        {
+            Type t = typeof(T);
+            string name = t.Name.ToLower();
+
+            List<T> list = new List<T>();
+
+            switch(name)
+            {
+                case "byte":  // unsigned byte
+                case "sbyte": // signed byte
+                    list = new List<T>();
+                    data.ToList().ForEach(s => list.Add((T)Convert.ChangeType(s, typeof(T))));
+                    break;
+                case "int16": // short  
+                    list = new List<T>();
+                    using (MemoryStream ms = new MemoryStream(data))
+                    {
+                        using (BinaryReader br = new BinaryReader(ms))
+                        {
+                            while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt16(), typeof(T))); }
+                        }
+                    }
+                    break;
+                case "uint16": // ushort
+                    list = new List<T>();
+                    using (MemoryStream ms = new MemoryStream(data))
+                    {
+                        using (BinaryReader br = new BinaryReader(ms))
+                        {
+                            while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt16(), typeof(T))); }
+                        }
+                    }
+                    break;
+                case "int32": // int
+                    list = new List<T>();
+                    using (MemoryStream ms = new MemoryStream(data))
+                    {
+                        using (BinaryReader br = new BinaryReader(ms))
+                        {
+                            while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt32(), typeof(T))); }
+                        }
+                    }
+                    break;
+                case "uint32": // uint
+                    list = new List<T>();
+                    using (MemoryStream ms = new MemoryStream(data))
+                    {
+                        using (BinaryReader br = new BinaryReader(ms))
+                        {
+                            while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt32(), typeof(T))); }
+                        }
+                    }
+                    break;
+                case "int64": // long
+                    list = new List<T>();
+                    using (MemoryStream ms = new MemoryStream(data))
+                    {
+                        using (BinaryReader br = new BinaryReader(ms))
+                        {
+                            while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadInt64(), typeof(T))); }
+                        }
+                    }
+                    break;
+                case "uint64": // ulong
+                    list = new List<T>();
+                    using (MemoryStream ms = new MemoryStream(data))
+                    {
+                        using (BinaryReader br = new BinaryReader(ms))
+                        {
+                            while (br.BaseStream.Position < br.BaseStream.Length) { list.Add((T)Convert.ChangeType(br.ReadUInt64(), typeof(T))); }
+                        }
+                    }
+                    break;
+                default:
+                    throw new InvalidOperationException("Encountered invalid type when converting byte array",new TypeLoadException(string.Format("Type: '{0}' was unexpected.", name)));
+            }
+            return list.ToArray();
+        }
+        #endregion
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22