/[RomCheater]/trunk/RomCheater/Serialization/SearchResultReader.cs
ViewVC logotype

Diff of /trunk/RomCheater/Serialization/SearchResultReader.cs

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

--- trunk/RomCheater/Serialization/SearchResultReader.cs	2013/06/03 22:18:34	493
+++ trunk/RomCheater/Serialization/SearchResultReader.cs	2013/06/03 22:50:27	494
@@ -335,7 +335,7 @@
         #region old-code: public StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
         //public StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
         //{
-            
+
         //    //int Last_Whole_Percent_Done = 0;
         //    bool unsigned = SearchArgs.IsUnsignedDataType;
         //    SearchDataTypes sdt = SearchArgs.DataType;
@@ -709,60 +709,123 @@
 
         public StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
         {
-            
-            bool unsigned = SearchArgs.IsUnsignedDataType;
-            SearchDataTypes sdt = SearchArgs.DataType;
-            uint STEP_SIZE = (uint)sdt / 8;
             List<StructResultType<ulong>> results = new List<StructResultType<ulong>>();
-
-            ulong compare_value = Convert.ToUInt64(SearchArgs.CompareStartValue);
-            update_progress.Invoke(0, string.Format("-> Checking value(s) for 0x{0:x8} addresses ... Please Wait ...", this.ResultCount));
-            using (FileStream fs = CreateReader())
+            using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
             {
-                using (BinaryReader br = new BinaryReader(fs))
+                try
                 {
-                    br.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data 
-                    for (int i = 0; i < this.ResultCount; i++)
-                    {
-                        uint address = br.ReadUInt32();
-                        ulong value = 0;
-                        switch (sdt)
-                        {
-                            case SearchDataTypes._8bits:
-                                if (unsigned) { value = br.ReadByte(); } else { value = (ulong)br.ReadSByte(); }
-                                break;
-                            case SearchDataTypes._16bits:
-                                if (unsigned) { value = br.ReadUInt16(); } else { value = (ulong)br.ReadInt16(); }
-                                break;
-                            case SearchDataTypes._32bits:
-                                if (unsigned) { value = br.ReadUInt32(); } else { value = (ulong)br.ReadInt32(); }
-                                break;
-                            case SearchDataTypes._64bits:
-                                if (unsigned) { value = br.ReadUInt64(); } else { value = (ulong)br.ReadInt64(); }
-                                break;
-                        }
+                    gmp.OpenProvider();
+                    bool unsigned = SearchArgs.IsUnsignedDataType;
+                    SearchDataTypes sdt = SearchArgs.DataType;
+                    uint STEP_SIZE = (uint)sdt / 8;
+                    
 
-                        StructResultType<ulong> result = StructResultType<ulong>.Empty;
-                        if (CheckResult<ulong>(iapc, SearchArgs, update_progress, address, compare_value, out result))
+                    ulong compare_value = Convert.ToUInt64(SearchArgs.CompareStartValue);
+                    update_progress.Invoke(0, string.Format("-> Checking value(s) for 0x{0:x8} addresses ... Please Wait ...", this.ResultCount));
+                    using (FileStream fs = CreateReader())
+                    {
+                        using (BinaryReader br = new BinaryReader(fs))
                         {
-                            results.Add(result);
+                            br.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data 
+                            StructResultType<ulong> result = StructResultType<ulong>.Empty;
+                            for (int i = 0; i < this.ResultCount; i++)
+                            {
+                                uint address = br.ReadUInt32();
+                                ulong read_value = 0;
+                                br.BaseStream.Seek(STEP_SIZE, SeekOrigin.Current); // seek STEP_SIZE bytes from address, to next entry
+                                switch (sdt)
+                                {
+                                    case SearchDataTypes._8bits:
+                                        if (unsigned)
+                                        {
+                                            byte v = 0;
+                                            gmp.ReadMemory(address, out v);
+                                            read_value = (ulong)v;
+                                        }
+                                        else
+                                        {
+                                            sbyte v = 0;
+                                            gmp.ReadMemory(address, out v);
+                                            read_value = (ulong)v;
+                                        }
+                                        break;
+                                    case SearchDataTypes._16bits:
+                                        if (unsigned)
+                                        {
+                                            ushort v = 0;
+                                            gmp.ReadMemory(address, out v);
+                                            read_value = (ulong)v;
+                                        }
+                                        else
+                                        {
+                                            short v = 0;
+                                            gmp.ReadMemory(address, out v);
+                                            read_value = (ulong)v;
+                                        }
+                                        break;
+                                    case SearchDataTypes._32bits:
+                                        if (unsigned)
+                                        {
+                                            uint v = 0;
+                                            gmp.ReadMemory(address, out v);
+                                            read_value = (ulong)v;
+                                        }
+                                        else
+                                        {
+                                            int v = 0;
+                                            gmp.ReadMemory(address, out v);
+                                            read_value = (ulong)v;
+                                        }
+                                        break;
+                                    case SearchDataTypes._64bits:
+                                        if (unsigned)
+                                        {
+                                            ulong v = 0;
+                                            gmp.ReadMemory(address, out v);
+                                            read_value = (ulong)v;
+                                        }
+                                        else
+                                        {
+                                            long v = 0;
+                                            gmp.ReadMemory(address, out v);
+                                            read_value = (ulong)v;
+                                        }
+                                        break;
+                                }
+                                if (CheckResult(iapc, SearchArgs, update_progress, address, compare_value, read_value, out result))
+                                {
+                                    results.Add(result);
+                                }
+                                //double percent = 100.0 * ((double)i / (double)this.ResultCount);
+                                //update_progress.Invoke((int)percent, string.Format("-> Checking value for Address: 0x{0:x8} from index: 0x{1:x8}", address, i));
+                            }
                         }
-                        //double percent = 100.0 * ((double)i / (double)this.ResultCount);
-                        //update_progress.Invoke((int)percent, string.Format("-> Checking value for Address: 0x{0:x8} from index: 0x{1:x8}", address, i));
                     }
+                    gmp.CloseProvider();
+                }
+                catch (Exception ex)
+                {
+                    logger.VerboseError.WriteLine(ex.ToString());
                 }
             }
             return results.ToArray();
         }
 
+        private TValue ConvertValue<TValue>(ulong value)
+            where TValue : IConvertible
+        {
+            TValue converted_value = (TValue)Convert.ChangeType(value, typeof(TValue));
+            return converted_value;
+        }
 
         #region private bool CheckResult<T>(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, uint address, T compare_value, out StructResultType<ulong> result)
-        private bool CheckResult<T>(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, uint address, T compare_value, out StructResultType<ulong> result)
+        private bool CheckResult(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, uint address, ulong compare_value, ulong read_value, out StructResultType<ulong> result)
         {
             result = StructResultType<ulong>.Empty;
             bool valid_result = false;
             bool unsigned = SearchArgs.IsUnsignedDataType;
             SearchDataTypes sdt = SearchArgs.DataType;
+
             switch (sdt)
             {
                 case SearchDataTypes._8bits:
@@ -771,22 +834,8 @@
                         #region 8bits - unsigned
                         using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address))
                         {
-                            byte value = 0;
-                            using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
-                            {
-                                try
-                                {
-                                    gmp.OpenProvider();
-                                    gmp.ReadMemory(address, out value);
-                                    gmp.CloseProvider();
-                                }
-                                catch (Exception ex)
-                                {
-                                    logger.VerboseError.WriteLine(ex.ToString());
-                                }
-                            }
-                            comparer.Value = value;
-                            if (comparer.Compare(Convert.ToByte(compare_value), value))
+                            comparer.Value = ConvertValue<byte>(read_value);
+                            if (comparer.Compare(Convert.ToByte(compare_value), comparer.Value))
                             {
                                 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
                                 valid_result = true;
@@ -799,22 +848,8 @@
                         #region 8bits - signed
                         using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address))
                         {
-                            sbyte value = 0;
-                            using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
-                            {
-                                try
-                                {
-                                    gmp.OpenProvider();
-                                    gmp.ReadMemory(address, out value);
-                                    gmp.CloseProvider();
-                                }
-                                catch (Exception ex)
-                                {
-                                    logger.VerboseError.WriteLine(ex.ToString());
-                                }
-                            }
-                            comparer.Value = value;
-                            if (comparer.Compare(Convert.ToSByte(compare_value), value))
+                            comparer.Value = ConvertValue<sbyte>(read_value);
+                            if (comparer.Compare(Convert.ToSByte(compare_value), comparer.Value))
                             {
                                 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
                                 valid_result = true;
@@ -828,22 +863,8 @@
                         #region 16bits - unsigned
                         using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address))
                         {
-                            ushort value = 0;
-                            using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
-                            {
-                                try
-                                {
-                                    gmp.OpenProvider();
-                                    gmp.ReadMemory(address, out value);
-                                    gmp.CloseProvider();
-                                }
-                                catch (Exception ex)
-                                {
-                                    logger.VerboseError.WriteLine(ex.ToString());
-                                }
-                            }
-                            comparer.Value = value;
-                            if (comparer.Compare(Convert.ToUInt16(compare_value), value))
+                            comparer.Value = ConvertValue<ushort>(read_value);
+                            if (comparer.Compare(Convert.ToUInt16(compare_value), comparer.Value))
                             {
                                 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
                                 valid_result = true;
@@ -856,22 +877,8 @@
                         #region 16bits - signed
                         using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address))
                         {
-                            short value = 0;
-                            using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
-                            {
-                                try
-                                {
-                                    gmp.OpenProvider();
-                                    gmp.ReadMemory(address, out value);
-                                    gmp.CloseProvider();
-                                }
-                                catch (Exception ex)
-                                {
-                                    logger.VerboseError.WriteLine(ex.ToString());
-                                }
-                            }
-                            comparer.Value = value;
-                            if (comparer.Compare(Convert.ToSByte(compare_value), value))
+                            comparer.Value = ConvertValue<short>(read_value);
+                            if (comparer.Compare(Convert.ToSByte(compare_value), comparer.Value))
                             {
                                 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
                                 valid_result = true;
@@ -885,22 +892,8 @@
                         #region 32bits - unsigned
                         using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address))
                         {
-                            uint value = 0;
-                            using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
-                            {
-                                try
-                                {
-                                    gmp.OpenProvider();
-                                    gmp.ReadMemory(address, out value);
-                                    gmp.CloseProvider();
-                                }
-                                catch (Exception ex)
-                                {
-                                    logger.VerboseError.WriteLine(ex.ToString());
-                                }
-                            }
-                            comparer.Value = value;
-                            if (comparer.Compare(Convert.ToUInt32(compare_value), value))
+                            comparer.Value = ConvertValue<uint>(read_value);
+                            if (comparer.Compare(Convert.ToUInt32(compare_value), comparer.Value))
                             {
                                 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
                                 valid_result = true;
@@ -913,22 +906,8 @@
                         #region 32bits - signed
                         using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address))
                         {
-                            int value = 0;
-                            using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
-                            {
-                                try
-                                {
-                                    gmp.OpenProvider();
-                                    gmp.ReadMemory(address, out value);
-                                    gmp.CloseProvider();
-                                }
-                                catch (Exception ex)
-                                {
-                                    logger.VerboseError.WriteLine(ex.ToString());
-                                }
-                            }
-                            comparer.Value = value;
-                            if (comparer.Compare(Convert.ToInt32(compare_value), value))
+                            comparer.Value = ConvertValue<int>(read_value);
+                            if (comparer.Compare(Convert.ToInt32(compare_value), comparer.Value))
                             {
                                 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
                                 valid_result = true;
@@ -942,22 +921,8 @@
                         #region 64bits - unsigned
                         using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address))
                         {
-                            ulong value = 0;
-                            using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
-                            {
-                                try
-                                {
-                                    gmp.OpenProvider();
-                                    gmp.ReadMemory(address, out value);
-                                    gmp.CloseProvider();
-                                }
-                                catch (Exception ex)
-                                {
-                                    logger.VerboseError.WriteLine(ex.ToString());
-                                }
-                            }
-                            comparer.Value = value;
-                            if (comparer.Compare(Convert.ToUInt64(compare_value), value))
+                            comparer.Value = ConvertValue<ulong>(read_value);
+                            if (comparer.Compare(Convert.ToUInt64(compare_value), comparer.Value))
                             {
                                 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
                                 valid_result = true;
@@ -970,22 +935,8 @@
                         #region 64bits - signed
                         using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address))
                         {
-                            long value = 0;
-                            using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
-                            {
-                                try
-                                {
-                                    gmp.OpenProvider();
-                                    gmp.ReadMemory(address, out value);
-                                    gmp.CloseProvider();
-                                }
-                                catch (Exception ex)
-                                {
-                                    logger.VerboseError.WriteLine(ex.ToString());
-                                }
-                            }
-                            comparer.Value = value;
-                            if (comparer.Compare(Convert.ToInt64(compare_value), value))
+                            comparer.Value = ConvertValue<long>(read_value);
+                            if (comparer.Compare(Convert.ToInt64(compare_value), comparer.Value))
                             {
                                 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
                                 valid_result = true;

 

  ViewVC Help
Powered by ViewVC 1.1.22