/[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/02 20:38:58	450
+++ trunk/RomCheater/Serialization/SearchResultReader.cs	2013/06/02 21:37:09	451
@@ -4,6 +4,7 @@
 using System.Text;
 using RomCheater.Docking.MemorySearch;
 using RomCheater.Logging;
+using System.IO;
 
 namespace RomCheater.Serialization
 {
@@ -14,80 +15,93 @@
     }
     public class SearchResultReader : SerializationReader, ISearchResultReader
     {
+
+        private Guid _ResultGuid;
+        private Guid ResultGuid { get { return _ResultGuid; } set { _ResultGuid = value; } }
+
+        private long ResultDataOffset = 0;
+        private long CurrentResultOffset = 0;
         //public SearchResultReader() : base() { ReadHeader(); }
-        public SearchResultReader(Guid guid) : base(guid) { ReadHeader(guid); }
+        public SearchResultReader(Guid guid) : base(guid) { ResultGuid = guid; ReadHeader(); }
 
         protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
-        private void ReadHeader(Guid guid)
+        private void ReadHeader()
         {
             try
             {
-                //int ResultsRead = 0;
-                // SRD (string)
-                string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
-                string SRD = "SRD";
-                if (magic != SRD)
-                {
-                    throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
-                }
-                // version (int)
-                int version = binReader.ReadInt32();
-
-                if (version == 1)
+                using (FileStream fs = CreateReader(ResultGuid))
                 {
-                    // do nothing
-                }
-                else if (version == 2)
-                {
-                    int guid_array_length = binReader.ReadInt32();
-                    byte[] guid_array = new byte[guid_array_length];
-                    binReader.Read(guid_array, 0, guid_array_length);
-                    Guid g = new Guid(guid_array);
-                    if (g != guid)
+                    using (BinaryReader binReader = new BinaryReader(fs))
                     {
-                        throw new InvalidOperationException(string.Format("Encountered wrong search results guid: read '{1}' excpected '{2}'", g.ToString(), guid.ToString()));
+                        //int ResultsRead = 0;
+                        // SRD (string)
+                        string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
+                        string SRD = "SRD";
+                        if (magic != SRD)
+                        {
+                            throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
+                        }
+                        // version (int)
+                        int version = binReader.ReadInt32();
+
+                        if (version == 1)
+                        {
+                            // do nothing
+                        }
+                        else if (version == 2)
+                        {
+                            int guid_array_length = binReader.ReadInt32();
+                            byte[] guid_array = new byte[guid_array_length];
+                            binReader.Read(guid_array, 0, guid_array_length);
+                            Guid g = new Guid(guid_array);
+                            if (g != ResultGuid)
+                            {
+                                throw new InvalidOperationException(string.Format("Encountered wrong search results guid: read '{1}' excpected '{2}'", g.ToString(), ResultGuid.ToString()));
+                            }
+                        }
+                        else
+                        {
+                            throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1} or {2}", version, 1, 2));
+                        }
+                        // resultcount
+                        int resultcount = binReader.ReadInt32();
+                        if (resultcount == 0)
+                        {
+                            throw new InvalidOperationException(string.Format("Result Count is zero"));
+                        }
+                        ResultCount = resultcount;
+                        ResultDataOffset = binReader.BaseStream.Position;
+                        //for (int i = 0; i < ResultCount; i++)
+                        //{
+                        //    try
+                        //    {
+                        //        ResultsRead = i;
+                        //        uint address = 0;
+                        //        // assume uint for data type
+                        //        uint value = 0;
+                        //        address = binReader.ReadUInt32();
+                        //        value = binReader.ReadUInt32();
+                        //        //if (i % 100000 == 0)
+                        //        //    logger.VerboseDebug.WriteLine("Result: @0x{0:x8}=0x{1:x8}", address, value);
+                        //    }
+                        //    catch (Exception ex)
+                        //    {
+                        //        logger.VerboseError.WriteLine("SearchResultReader.ReadHeader():Consistency Check");
+                        //        //logger.VerboseError.WriteLine(ex.ToString());
+                        //        logger.VerboseError.WriteLine("Faied entry: {0}", ResultsRead);
+                        //        break;
+                        //    }
+                        //    ResultsRead++; // add 1
+                        //}
+                        ////throw new NotImplementedException("DEBUG: testing SearchResultReader consistency");
+                        //if (ResultCount != ResultsRead)
+                        //{
+                        //    throw new InvalidOperationException(string.Format("ResultCount does not match ResultsRead: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsRead));
+                        //} 
+                        binReader.Close();
                     }
+                    fs.Close();
                 }
-                else
-                {
-                    throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1} or {2}", version, 1, 2));
-                }
-                // resultcount
-                int resultcount = binReader.ReadInt32();
-                if (resultcount == 0)
-                {
-                    throw new InvalidOperationException(string.Format("Result Count is zero"));
-                }
-                ResultCount = resultcount;
-                
-                //for (int i = 0; i < ResultCount; i++)
-                //{
-                //    try
-                //    {
-                //        ResultsRead = i;
-                //        uint address = 0;
-                //        // assume uint for data type
-                //        uint value = 0;
-                //        address = binReader.ReadUInt32();
-                //        value = binReader.ReadUInt32();
-                //        //if (i % 100000 == 0)
-                //        //    logger.VerboseDebug.WriteLine("Result: @0x{0:x8}=0x{1:x8}", address, value);
-                //    }
-                //    catch (Exception ex)
-                //    {
-                //        logger.VerboseError.WriteLine("SearchResultReader.ReadHeader():Consistency Check");
-                //        //logger.VerboseError.WriteLine(ex.ToString());
-                //        logger.VerboseError.WriteLine("Faied entry: {0}", ResultsRead);
-                //        break;
-                //    }
-                //    ResultsRead++; // add 1
-                //}
-                ////throw new NotImplementedException("DEBUG: testing SearchResultReader consistency");
-                //if (ResultCount != ResultsRead)
-                //{
-                //    throw new InvalidOperationException(string.Format("ResultCount does not match ResultsRead: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsRead));
-                //}  
-
             }
             catch (System.IO.EndOfStreamException) { }
         }
@@ -97,46 +111,59 @@
 
         public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible
         {
-
             ResultType<TValue> result = new ResultType<TValue>();
-            try
+            using (FileStream fs = CreateReader(ResultGuid))
             {
-
-                uint Address = binReader.ReadUInt32();
-                TValue Value = default(TValue);
-                Type t = typeof(TValue);
-                switch (t.Name.ToLower())
+                using (BinaryReader binReader = new BinaryReader(fs))
                 {
-                    case "byte":
-                        Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue));
-                        break;
-                    case "sbyte":
-                        Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue));
-                        break;
-                    case "uint16":
-                        Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue));
-                        break;
-                    case "int16":
-                        Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue));
-                        break;
-                    case "uint32":
-                        Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue));
-                        break;
-                    case "int32":
-                        Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue));
-                        break;
-                    case "uint64":
-                        Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue));
-                        break;
-                    case "int64":
-                        Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue));
-                        break;
+                    if (CurrentResultOffset == 0)
+                    {
+                        binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
+                    }
+                    else
+                    {
+                        binReader.BaseStream.Seek(CurrentResultOffset, SeekOrigin.Begin); // seek to current entry
+                    }
+                    
+                    try
+                    {
+                        uint Address = binReader.ReadUInt32();
+                        TValue Value = default(TValue);
+                        Type t = typeof(TValue);
+                        switch (t.Name.ToLower())
+                        {
+                            case "byte":
+                                Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue));
+                                break;
+                            case "sbyte":
+                                Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue));
+                                break;
+                            case "uint16":
+                                Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue));
+                                break;
+                            case "int16":
+                                Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue));
+                                break;
+                            case "uint32":
+                                Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue));
+                                break;
+                            case "int32":
+                                Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue));
+                                break;
+                            case "uint64":
+                                Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue));
+                                break;
+                            case "int64":
+                                Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue));
+                                break;
+                        }
+                        result = new ResultType<TValue>(Address, Value);
+                        CurrentResultOffset = binReader.BaseStream.Position;
+                    }
+                    catch (System.IO.EndOfStreamException) { }
                 }
-                result = new ResultType<TValue>(Address, Value);
-                return result;
             }
-            catch (System.IO.EndOfStreamException)
-            { return result; }
+            return result;
         }
         #endregion
     }

 

  ViewVC Help
Powered by ViewVC 1.1.22