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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 447 - (hide annotations) (download)
Sun Jun 2 19:58:37 2013 UTC (8 years, 1 month ago) by william
File size: 4312 byte(s)

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 444 //public SearchResultWriter() : base() { }
20     public SearchResultWriter(int resultCount, Guid guid) : base(resultCount, guid) { WriteHeader(guid); }
21     //public SearchResultWriter(string filename, Guid guid) : base(CreateFileName(filename,guid), guid) { WriteHeader(guid); }
22     //public SearchResultWriter(string filename, int resultCount, Guid guid) : base(filename, resultCount, guid, false) { WriteHeader(guid); }
23     //public SearchResultWriter(string filename, int resultCount, Guid guid, bool delete) : base(filename, resultCount, guid, delete) { WriteHeader(guid); }
24 william 401 #region ISearchResultWriter members
25     public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible
26     {
27 william 445 try
28     {
29     Type t = typeof(TValue);
30 william 401
31 william 445 binWriter.Write(address);
32     switch (t.Name.ToLower())
33     {
34     case "byte":
35     binWriter.Write(Convert.ToByte(value));
36     break;
37     case "sbyte":
38     binWriter.Write(Convert.ToSByte(value));
39     break;
40     case "uint16":
41     binWriter.Write(Convert.ToUInt16(value));
42     break;
43     case "int16":
44     binWriter.Write(Convert.ToInt16(value));
45     break;
46     case "uint32":
47     binWriter.Write(Convert.ToUInt32(value));
48     break;
49     case "int32":
50     binWriter.Write(Convert.ToInt32(value));
51     break;
52     case "uint64":
53     binWriter.Write(Convert.ToUInt64(value));
54     break;
55     case "int64":
56     binWriter.Write(Convert.ToInt64(value));
57     break;
58     }
59     ResultsWritten++;
60     }
61     catch (Exception ex)
62 william 401 {
63 william 445 throw ex;
64 william 401 }
65     }
66     #endregion
67    
68 william 444 private void WriteHeader(Guid guid)
69 william 401 {
70 william 445 try
71     {
72     // write magic: SRD
73     binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
74 william 444
75 william 445 if (guid == Guid.Empty)
76     {
77     binWriter.Write((int)1);
78     }
79     else
80     {
81     binWriter.Write((int)2);
82     // related guid - length
83     binWriter.Write(guid.ToByteArray().Length);
84     // related guid - data
85     binWriter.Write(guid.ToByteArray());
86     }
87     // write count (int)
88     binWriter.Write(ResultCount);
89     }
90     catch (Exception ex)
91 william 444 {
92 william 445 throw ex;
93 william 444 }
94 william 401 }
95 william 405
96     protected override void Dispose(bool disposing)
97 william 445 {
98 william 447 if (Cancelled) { return; }
99 william 445 try
100 william 405 {
101 william 445 base.Dispose(disposing);
102     // ensure ResultCount and ResultsWritten are equal
103     if (ResultCount != ResultsWritten)
104     {
105     string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
106     Logging.logger.Error.WriteLine(message);
107     throw new InvalidOperationException(message);
108     }
109 william 429 }
110 william 445 catch (Exception ex)
111     {
112     throw ex;
113     }
114 william 405 }
115 william 401 }
116     }

  ViewVC Help
Powered by ViewVC 1.1.22