/[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 16:48:28	474
+++ trunk/RomCheater/Serialization/SearchResultReader.cs	2013/06/03 17:43:12	475
@@ -234,6 +234,19 @@
             return results;
         }
 
+        public uint[] GetAddressRangeAtIndex(int index, int count, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
+        {
+            uint[] results = new uint[index + count];
+            for (int i = 0; i < count; i++)
+            {
+                uint result = GetAddressAtIndex(i + index, unsigned, datatype, update_progress);
+                results[i] = result;
+                //double percent = 100.0 * ((double)i / (double)count);
+                //update_progress.Invoke((int)percent, string.Format("-> Reading Address: 0x{1:x8} from index: 0x{1:x8}", results[i].Address, i + index));
+            }
+            return results;
+        }
+
         public ResultType<object>[] GetResultRangeAtIndex(int index, int count, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
         {
             ResultType<object>[] results = new ResultType<object>[index+count];
@@ -247,7 +260,41 @@
             }
             return results;
         }
-
+        public uint GetAddressAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
+        {
+            //update_progress.Invoke(0, string.Empty);
+            int data_size = sizeof(uint); // address size ... should be 4 bytes (could be 8, if we have a 64-bit address)
+            uint result = 0;
+            using (FileStream fs = CreateReader())
+            {
+                using (BinaryReader binReader = new BinaryReader(fs))
+                {
+                    binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data 
+                    data_size += (int)datatype / 8;
+                    long offset = data_size * index;
+                    binReader.BaseStream.Seek(offset, SeekOrigin.Current);
+                    uint Address = binReader.ReadUInt32();
+                    object Value = 0;
+                    switch (datatype)
+                    {
+                        case SearchDataTypes._8bits:
+                            if (unsigned) { Value = binReader.ReadByte(); } else { Value = binReader.ReadSByte(); }
+                            break;
+                        case SearchDataTypes._16bits:
+                            if (unsigned) { Value = binReader.ReadUInt16(); } else { Value = binReader.ReadInt16(); }
+                            break;
+                        case SearchDataTypes._32bits:
+                            if (unsigned) { Value = binReader.ReadUInt32(); } else { Value = binReader.ReadInt32(); }
+                            break;
+                        case SearchDataTypes._64bits:
+                            if (unsigned) { Value = binReader.ReadUInt64(); } else { Value = binReader.ReadInt64(); }
+                            break;
+                    }
+                    result = Address;
+                }
+            }
+            return result;
+        }
         public ResultType<object> GetResultAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
         {
             //update_progress.Invoke(0, string.Empty);
@@ -284,5 +331,353 @@
             return result;
         }
 
+        public ResultType<object>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
+        {
+            int Last_Whole_Percent_Done = 0;
+            bool unsigned = SearchArgs.IsUnsignedDataType;
+            SearchDataTypes sdt = SearchArgs.DataType;
+            var addresses = this.GetAddressRangeAtIndex(0, this.ResultCount, unsigned, sdt, update_progress);
+
+
+            uint STEP_SIZE = (uint)sdt/8;
+
+            List<ResultType<object>> foundAddresses = new List<ResultType<object>>();
+
+            int count = 0;
+            int totalcount = addresses.Length;
+            foreach (var address in addresses)
+            {
+                byte[] data = new byte[0];
+                using (GenericMemoryProvider provider = new GenericMemoryProvider(iapc))
+                {
+                    int bytesRead=0;
+                    provider.OpenProvider();
+                    provider.ReadProcessMemory(address, STEP_SIZE, out bytesRead, out data);
+                    provider.CloseProvider();
+                }
+                using (MemoryStream ms = new MemoryStream(data))
+                {
+                    using (BinaryReader br = new BinaryReader(ms))
+                    {
+                        #region comparison
+                        switch (sdt)
+                        {
+                            case SearchDataTypes._8bits:
+                                if (unsigned)
+                                {
+                                    #region 8bits - unsigned
+                                    var Value = br.ReadByte();
+                                    using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address))
+                                    {
+                                        byte value = 0;
+                                        if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
+                                        {
+                                            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;
+                                        }
+                                        else
+                                        {
+                                            value = Convert.ToByte(SearchArgs.CompareStartValue);
+                                            comparer.Value = value;
+                                        }
+                                        if (comparer.Compare(Convert.ToByte(Value), value))
+                                        {
+                                            foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
+                                        }
+                                    }
+                                    #endregion
+                                }
+                                else
+                                {
+                                    #region 8bits - signed
+                                    var Value = br.ReadSByte();
+                                    using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address))
+                                    {
+                                        sbyte value = 0;
+                                        if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
+                                        {
+                                            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;
+                                        }
+                                        else
+                                        {
+                                            value = Convert.ToSByte(SearchArgs.CompareStartValue);
+                                            comparer.Value = value;
+                                        }
+                                        if (comparer.Compare(Convert.ToSByte(Value), value))
+                                        {
+                                            foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
+                                        }
+                                    }
+                                    #endregion
+                                }
+                                break;
+                            case SearchDataTypes._16bits:
+                                if (unsigned)
+                                {
+                                    #region 16bits - unsigned
+                                    var Value = br.ReadUInt16();
+                                    using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address))
+                                    {
+                                        ushort value = 0;
+                                        if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
+                                        {
+                                            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;
+                                        }
+                                        else
+                                        {
+                                            value = Convert.ToUInt16(SearchArgs.CompareStartValue);
+                                            comparer.Value = value;
+                                        }
+                                        if (comparer.Compare(Convert.ToUInt16(Value), value))
+                                        {
+                                            foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
+                                        }
+                                    }
+                                    #endregion
+                                }
+                                else
+                                {
+                                    #region 16bits - signed
+                                    var Value = br.ReadInt16();
+                                    using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address))
+                                    {
+                                        short value = 0;
+                                        if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
+                                        {
+                                            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;
+                                        }
+                                        else
+                                        {
+                                            value = Convert.ToInt16(SearchArgs.CompareStartValue);
+                                            comparer.Value = value;
+                                        }
+                                        if (comparer.Compare(Convert.ToInt16(Value), value))
+                                        {
+                                            foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
+                                        }
+                                    }
+                                    #endregion
+                                }
+                                break;
+                            case SearchDataTypes._32bits:
+                                if (unsigned)
+                                {
+                                    #region 32bits - unsigned
+                                    var Value = br.ReadUInt32();
+                                    using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address))
+                                    {
+                                        uint value = 0;
+                                        if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
+                                        {
+                                            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;
+                                        }
+                                        else
+                                        {
+                                            value = Convert.ToUInt32(SearchArgs.CompareStartValue);
+                                            comparer.Value = value;
+                                        }
+                                        if (comparer.Compare(Convert.ToUInt32(Value), value))
+                                        {
+                                            foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
+                                        }
+                                    }
+                                    #endregion
+                                }
+                                else
+                                {
+                                    #region 32bits - signed
+                                    var Value = br.ReadInt32();
+                                    using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address))
+                                    {
+                                        int value = 0;
+                                        if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
+                                        {
+                                            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;
+                                        }
+                                        else
+                                        {
+                                            value = Convert.ToInt32(SearchArgs.CompareStartValue);
+                                            comparer.Value = value;
+                                        }
+                                        if (comparer.Compare(Convert.ToInt32(Value), value))
+                                        {
+                                            foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
+                                        }
+                                    }
+                                    #endregion
+                                }
+                                break;
+                            case SearchDataTypes._64bits:
+                                if (unsigned)
+                                {
+                                    #region 64bits - unsigned
+                                    var Value = br.ReadUInt64();
+                                    using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address))
+                                    {
+                                        ulong value = 0;
+                                        if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
+                                        {
+                                            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;
+                                        }
+                                        else
+                                        {
+                                            value = Convert.ToUInt64(SearchArgs.CompareStartValue);
+                                            comparer.Value = value;
+                                        }
+                                        if (comparer.Compare(Convert.ToUInt64(Value), value))
+                                        {
+                                            foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
+                                        }
+                                    }
+                                    #endregion
+                                }
+                                else
+                                {
+                                    #region 64bits - signed
+                                    var Value = br.ReadInt64();
+                                    using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address))
+                                    {
+                                        long value = 0;
+                                        if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
+                                        {
+                                            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;
+                                        }
+                                        else
+                                        {
+                                            value = Convert.ToInt64(SearchArgs.CompareStartValue);
+                                            comparer.Value = value;
+                                        }
+                                        if (comparer.Compare(Convert.ToInt64(Value), value))
+                                        {
+                                            foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
+                                        }
+                                    }
+                                    #endregion
+                                }
+                                break;
+                        }
+                        #endregion
+                    }
+                }
+
+                double double_percent_done = 100.0 * (double)((double)count / (double)totalcount);
+                int int_percent_done = (int)double_percent_done;
+                if (int_percent_done != Last_Whole_Percent_Done && count % 100000 == 0)
+                {
+                    if (int_percent_done <= 100)
+                    {
+                        update_progress.Invoke(int_percent_done, string.Format(" -> Reading Address: 0x{0:x8}", address));
+                        Last_Whole_Percent_Done = int_percent_done;
+                    }
+                }
+
+                count++;
+            }
+            return foundAddresses.ToArray();
+        }
+
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22