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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 429 - (hide annotations) (download)
Tue May 28 15:16:05 2013 UTC (8 years, 1 month ago) by william
File size: 3204 byte(s)
+ fix search so that the result count(s) match

1 william 401 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.IO;
6 william 405 using System.Diagnostics;
7 william 401
8     namespace RomCheater.Serialization
9     {
10     public interface ISearchResultWriter
11     {
12     void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible;
13     }
14     public class SearchResultWriter : SerializationWriter, ISearchResultWriter
15     {
16 william 405
17     private int ResultsWritten = 0;
18    
19 william 401 public SearchResultWriter() : base() { }
20     public SearchResultWriter(int resultCount) : base(resultCount) { WriteHeader(); }
21 william 402 public SearchResultWriter(string filename) : base(filename) { WriteHeader(); }
22     public SearchResultWriter(string filename, int resultCount) : base(filename, resultCount, false) { WriteHeader(); }
23     public SearchResultWriter(string filename, int resultCount, bool delete) : base(filename, resultCount, delete) { WriteHeader(); }
24 william 401 #region ISearchResultWriter members
25     public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible
26     {
27    
28     Type t = typeof(TValue);
29    
30     binWriter.Write(address);
31     switch (t.Name.ToLower())
32     {
33     case "byte":
34     binWriter.Write(Convert.ToByte(value));
35     break;
36     case "sbyte":
37     binWriter.Write(Convert.ToSByte(value));
38     break;
39     case "uint16":
40     binWriter.Write(Convert.ToUInt16(value));
41     break;
42     case "int16":
43     binWriter.Write(Convert.ToInt16(value));
44     break;
45     case "uint32":
46     binWriter.Write(Convert.ToUInt32(value));
47     break;
48     case "int32":
49     binWriter.Write(Convert.ToInt32(value));
50     break;
51     case "uint64":
52     binWriter.Write(Convert.ToUInt64(value));
53     break;
54     case "int64":
55     binWriter.Write(Convert.ToInt64(value));
56     break;
57     }
58 william 405 ResultsWritten++;
59 william 401 }
60     #endregion
61    
62     private void WriteHeader()
63     {
64     // write magic: SRD
65     binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
66     // write version (int)
67     binWriter.Write((int)1);
68     // write count (int)
69     binWriter.Write(ResultCount);
70     }
71 william 405
72     protected override void Dispose(bool disposing)
73 william 429 {
74 william 405 base.Dispose(disposing);
75 william 429 // ensure ResultCount and ResultsWritten are equal
76 william 405 if (ResultCount != ResultsWritten)
77     {
78 william 429 string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
79     Logging.logger.Error.WriteLine(message);
80     throw new InvalidOperationException(message);
81     }
82 william 405 }
83 william 401 }
84     }

  ViewVC Help
Powered by ViewVC 1.1.22