/[RomCheater]/trunk/RomCheater/Docking/FloatingMemorySearcher.cs
ViewVC logotype

Diff of /trunk/RomCheater/Docking/FloatingMemorySearcher.cs

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

--- trunk/RomCheater/Docking/FloatingMemorySearcher.cs	2012/06/19 09:30:48	382
+++ trunk/RomCheater/Docking/FloatingMemorySearcher.cs	2012/06/19 10:15:53	384
@@ -994,59 +994,56 @@
             e.Result = st;
             //List<ResultType<object>> tmp_Results = new List<ResultType<object>>();
             List<ResultType<object>> second_tmp_Results = new List<ResultType<object>>();
-            const double _UPDATE_DELAY = 10240.0;
-            int UPDATE_DELAY = (int)(_UPDATE_DELAY / 1000);
-            //int UPDATE_DELAY = 1;
+            const int ElementsBeforeDisplay = 100;
+            //const double _UPDATE_DELAY = 1024.0;
+            //double UPDATE_DELAY = 1;         
             //tmp_Results = SearchArgs.Results.GetRange(0,SearchArgs.Results.Count);
             //SearchArgs.Results = null;
             //SearchArgs.Results.Clear();
             // log options
             SearchArgs.LogSearchOptions();
-            uint STEP_SIZE = (uint)SearchArgs.DataType / 8;
-
+            uint STEP_SIZE = (uint)SearchArgs.DataType / 8;            
             GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
             provider.OpenProvider();
             int bytes_read = 0;
 
-            byte[] buffered_mem = new byte[MemoryRangeSize]; // throws OutOfMemoryException if size is over 2G
-            provider.ReadProcessMemoryAtOnce(MemoryRangeStart, MemoryRangeSize, out bytes_read, out buffered_mem);         
+            byte[] buffered_mem = new byte[(MemoryRangeSize - MemoryRangeStart)]; // throws OutOfMemoryException if size is over 2G
+            provider.ReadProcessMemoryAtOnce(MemoryRangeStart, (MemoryRangeSize - MemoryRangeStart), out bytes_read, out buffered_mem);         
             provider.CloseProvider();
        
             if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
-            MemoryStream ms = new MemoryStream(buffered_mem);
-            BinaryReader r_ms = new BinaryReader(ms);
-            logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", r_ms.BaseStream.Length));
+            //MemoryStream ms = new MemoryStream(buffered_mem);
+            //BinaryReader r_ms = new BinaryReader(ms);
+            logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", buffered_mem.Length));
             int Last_Whole_Percent_Done = 0;
 
             #region First Search
             if (SearchArgs.IsFirstSearch)
             {
                 SearchArgs.Results.Clear();
-                r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
-                for (uint i = 0; i < r_ms.BaseStream.Length; i += STEP_SIZE)
+                //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
+                for (uint i = 0; i < buffered_mem.Length; i += STEP_SIZE)
                 {
                     ResultType<object> _tmp_result = new ResultType<object>();
-
-
                     switch (SearchArgs.DataType)
                     {
                         case SearchDataTypes._8bits:
-                            if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, r_ms.ReadByte()); }
-                            else { _tmp_result = new ResultType<object>(i, r_ms.ReadSByte()); } break;
+                            if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, buffered_mem[i]); }
+                            else { _tmp_result = new ResultType<object>(i, buffered_mem[i]); } break;
                         case SearchDataTypes._16bits:
-                            if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, r_ms.ReadUInt16()); }
-                            else { _tmp_result = new ResultType<object>(i, r_ms.ReadInt16()); } break;
+                            if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, BitConverter.ToUInt16(buffered_mem,(int)i)); }
+                            else { _tmp_result = new ResultType<object>(i,BitConverter.ToInt16(buffered_mem,(int)i)); } break;
                         case SearchDataTypes._32bits:
-                            if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, r_ms.ReadUInt32()); }
-                            else { _tmp_result = new ResultType<object>(i, r_ms.ReadInt32()); } break;
+                            if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, BitConverter.ToUInt32(buffered_mem,(int)i)); }
+                            else { _tmp_result = new ResultType<object>(i, BitConverter.ToInt32(buffered_mem,(int)i)); } break;
                         case SearchDataTypes._64bits:
-                            if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, r_ms.ReadUInt64()); }
-                            else { _tmp_result = new ResultType<object>(i, r_ms.ReadInt64()); } break;
+                            if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, BitConverter.ToUInt64(buffered_mem,(int)i)); }
+                            else { _tmp_result = new ResultType<object>(i, BitConverter.ToInt64(buffered_mem,(int)i)); } break;
                     }
                     SearchArgs.Results.Add(_tmp_result);
-                    double double_percent_done = 100.0 * (double)((double)i / (double)r_ms.BaseStream.Length);
+                    double double_percent_done = 100.0 * (double)((double)i / (double)buffered_mem.Length);
                     int int_percent_done = (int)double_percent_done;
-                    if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
+                    if (int_percent_done != Last_Whole_Percent_Done && i % ElementsBeforeDisplay == 0)
                     {
                         resultsprogress.Value = int_percent_done;
                         resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
@@ -1059,7 +1056,7 @@
                         e.Cancel = true;
                         return;
                     }
-
+                    Application.DoEvents();
                 }
                 resultsprogress.Value = 100;
                 resultsprogress.Message = "";
@@ -1069,7 +1066,7 @@
             #endregion
 
             #region Subsequent Searches
-            r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
+            //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
 
 
             // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
@@ -1091,15 +1088,15 @@
                     ////second_tmp_Results.c
                     for (int i = 0; i < SearchArgs.Results.Count; i += 1)
                     {
-                        r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
-
+                        //r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
+                        uint address = SearchArgs.Results[i].Address;
                         switch (SearchArgs.DataType)
                         {
                             #region Comparer Support
                             case SearchDataTypes._8bits:
                                 if (SearchArgs.IsUnsignedDataType)
                                 {
-                                    byte lookup_value = r_ms.ReadByte();
+                                    byte lookup_value = buffered_mem[address];
                                     _8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
                                     byte value = 0;
                                     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
@@ -1125,7 +1122,7 @@
                                 }
                                 else
                                 {
-                                    sbyte lookup_value = r_ms.ReadSByte();
+                                    sbyte lookup_value = (sbyte)buffered_mem[address];
                                     _8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
                                     sbyte value = 0;
                                     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
@@ -1151,7 +1148,7 @@
                             case SearchDataTypes._16bits:
                                 if (SearchArgs.IsUnsignedDataType)
                                 {
-                                    ushort lookup_value = r_ms.ReadUInt16();
+                                    ushort lookup_value = BitConverter.ToUInt16(buffered_mem,(int)address);
                                     _16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
                                     ushort value = 0;
                                     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
@@ -1177,7 +1174,7 @@
                                 }
                                 else
                                 {
-                                    short lookup_value = r_ms.ReadInt16();
+                                    short lookup_value = BitConverter.ToInt16(buffered_mem,(int)address);
                                     _16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
                                     short value = 0;
                                     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
@@ -1203,7 +1200,7 @@
                             case SearchDataTypes._32bits:
                                 if (SearchArgs.IsUnsignedDataType)
                                 {
-                                    uint lookup_value = r_ms.ReadUInt32();
+                                    uint lookup_value = BitConverter.ToUInt32(buffered_mem,(int)address);
                                     _32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
                                     uint value = 0;
                                     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
@@ -1229,7 +1226,7 @@
                                 }
                                 else
                                 {
-                                    int lookup_value = r_ms.ReadInt32();
+                                    int lookup_value =BitConverter.ToInt32(buffered_mem,(int)address);
                                     _32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
                                     int value = 0;
                                     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
@@ -1255,7 +1252,7 @@
                             case SearchDataTypes._64bits:
                                 if (SearchArgs.IsUnsignedDataType)
                                 {
-                                    ulong lookup_value = r_ms.ReadUInt64();
+                                    ulong lookup_value = BitConverter.ToUInt64(buffered_mem,(int)address);
                                     _64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
                                     ulong value = 0;
                                     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
@@ -1281,7 +1278,7 @@
                                 }
                                 else
                                 {
-                                    long lookup_value = r_ms.ReadInt64();
+                                    long lookup_value = BitConverter.ToInt64(buffered_mem,(int)address);
                                     _64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
                                     long value = 0;
                                     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
@@ -1309,7 +1306,7 @@
 
                         double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
                         int int_percent_done = (int)double_percent_done;
-                        if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
+                        if (int_percent_done != Last_Whole_Percent_Done && i % ElementsBeforeDisplay == 0)
                         {
                             resultsprogress.Value = int_percent_done;
                             resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
@@ -1330,11 +1327,12 @@
                     end = SearchArgs.CompareEndValue;
                     for (int i = 0; i < SearchArgs.Results.Count; i += 1)
                     {
-                        r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
+                        uint address = SearchArgs.Results[i].Address;
+                        //r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
                         if (SearchArgs.CompareType == SearchCompareTypes.Between)
                         {
                             InRangeComparer comparer = new InRangeComparer(SearchArgs.Results[i].Address, 0);
-                            if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, r_ms))
+                            if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, buffered_mem))
                             {
                                 SearchArgs.Results[i].Value = comparer.Value;
                                 second_tmp_Results.Add(SearchArgs.Results[i]);
@@ -1344,7 +1342,7 @@
                         else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
                         {
                             NotInRangeComparer comparer = new NotInRangeComparer(SearchArgs.Results[i].Address, 0);
-                            if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, r_ms))
+                            if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, buffered_mem))
                             {
                                 SearchArgs.Results[i].Value = comparer.Value;
                                 second_tmp_Results.Add(SearchArgs.Results[i]);
@@ -1357,7 +1355,7 @@
                         }
                         double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
                         int int_percent_done = (int)double_percent_done;
-                        if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
+                        if (int_percent_done != Last_Whole_Percent_Done && i % ElementsBeforeDisplay == 0)
                         {
                             resultsprogress.Value = int_percent_done;
                             resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
@@ -1378,7 +1376,7 @@
                 second_tmp_Results = null; // free memory
             }
 
-            r_ms.Close();
+            //r_ms.Close();
         }
 
         private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)

 

  ViewVC Help
Powered by ViewVC 1.1.22