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

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

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

--- trunk/RomCheater/Serialization/SearchResultWriter.cs	2012/06/21 06:30:33	402
+++ trunk/RomCheater/Serialization/SearchResultWriter.cs	2013/06/03 01:45:40	456
@@ -3,6 +3,7 @@
 using System.Linq;
 using System.Text;
 using System.IO;
+using System.Diagnostics;
 
 namespace RomCheater.Serialization
 {
@@ -12,56 +13,113 @@
     }
     public class SearchResultWriter : SerializationWriter, ISearchResultWriter
     {
-        public SearchResultWriter() : base() { }
-        public SearchResultWriter(int resultCount) : base(resultCount) { WriteHeader(); }
-        public SearchResultWriter(string filename) : base(filename) { WriteHeader(); }
-        public SearchResultWriter(string filename, int resultCount) : base(filename, resultCount, false) { WriteHeader(); }
-        public SearchResultWriter(string filename, int resultCount, bool delete) : base(filename, resultCount, delete) { WriteHeader(); }
+
+        private int ResultsWritten = 0;
+        protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
+        //public SearchResultWriter() : base() { }
+        public SearchResultWriter(int resultCount, Guid guid) : base(resultCount, guid) { WriteHeader(guid); }
+        //public SearchResultWriter(string filename, Guid guid) : base(CreateFileName(filename,guid), guid) { WriteHeader(guid); }
+        //public SearchResultWriter(string filename, int resultCount, Guid guid) : base(filename, resultCount, guid, false) { WriteHeader(guid); }
+        //public SearchResultWriter(string filename, int resultCount, Guid guid, bool delete) : base(filename, resultCount, guid, delete) { WriteHeader(guid); }      
         #region ISearchResultWriter members
         public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible
         {
+            try
+            {
+                Type t = typeof(TValue);
 
-            Type t = typeof(TValue);
-
-            binWriter.Write(address);
-            switch (t.Name.ToLower())
+                binWriter.Write(address);
+                switch (t.Name.ToLower())
+                {
+                    case "byte":
+                        binWriter.Write(Convert.ToByte(value));
+                        break;
+                    case "sbyte":
+                        binWriter.Write(Convert.ToSByte(value));
+                        break;
+                    case "uint16":
+                        binWriter.Write(Convert.ToUInt16(value));
+                        break;
+                    case "int16":
+                        binWriter.Write(Convert.ToInt16(value));
+                        break;
+                    case "uint32":
+                        binWriter.Write(Convert.ToUInt32(value));
+                        break;
+                    case "int32":
+                        binWriter.Write(Convert.ToInt32(value));
+                        break;
+                    case "uint64":
+                        binWriter.Write(Convert.ToUInt64(value));
+                        break;
+                    case "int64":
+                        binWriter.Write(Convert.ToInt64(value));
+                        break;
+                }
+                ResultsWritten++;
+            }
+            catch (Exception ex)
             {
-                case "byte":
-                    binWriter.Write(Convert.ToByte(value));
-                    break;
-                case "sbyte":
-                    binWriter.Write(Convert.ToSByte(value));
-                    break;
-                case "uint16":
-                    binWriter.Write(Convert.ToUInt16(value));
-                    break;
-                case "int16":
-                    binWriter.Write(Convert.ToInt16(value));
-                    break;
-                case "uint32":
-                    binWriter.Write(Convert.ToUInt32(value));
-                    break;
-                case "int32":
-                    binWriter.Write(Convert.ToInt32(value));
-                    break;
-                case "uint64":
-                    binWriter.Write(Convert.ToUInt64(value));
-                    break;
-                case "int64":
-                    binWriter.Write(Convert.ToInt64(value));
-                    break;
+                throw ex;
             }
         }
         #endregion
 
-        private void WriteHeader()
+        long ResultCountOffset = 0;
+        private void WriteHeader(Guid guid)
+        {
+            try
+            {
+                // write magic: SRD
+                binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
+
+                if (guid == Guid.Empty)
+                {
+                    binWriter.Write((int)1);
+                }
+                else
+                {
+                    binWriter.Write((int)2);
+                    // related guid - length
+                    binWriter.Write(guid.ToByteArray().Length);
+                    // related guid - data
+                    binWriter.Write(guid.ToByteArray());
+                }
+                // write count (int)
+                binWriter.Write(ResultCount);
+                ResultCountOffset = binWriter.BaseStream.Position - sizeof(int);
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+        }
+
+        protected override void Dispose(bool disposing)
         {
-            // write magic: SRD
-            binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
-            // write version (int)
-            binWriter.Write((int)1);
-            // write count (int)
-            binWriter.Write(ResultCount);
+            if (Cancelled) { binWriter.Close(); fileStream.Close(); return; }
+            try
+            {
+               
+                //// ensure ResultCount and ResultsWritten are equal            
+                //if (ResultCount != ResultsWritten)
+                //{
+                //    string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
+                //    Logging.logger.Error.WriteLine(message);
+                //    throw new InvalidOperationException(message);
+                //}
+                if (ResultCount != ResultsWritten)
+                {
+                    // update the number of results written
+                    binWriter.BaseStream.Seek(ResultCountOffset, SeekOrigin.Begin);
+                    binWriter.Write(ResultsWritten);
+                }
+                base.Dispose(disposing);
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
         }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22