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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 444 - (hide annotations) (download)
Sun Jun 2 18:56:41 2013 UTC (8 years, 1 month ago) by william
File size: 3668 byte(s)
+ update serialization reader/writer to include a search results guid

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    
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 william 444 private void WriteHeader(Guid guid)
63 william 401 {
64     // write magic: SRD
65     binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
66 william 444
67     if (guid == Guid.Empty)
68     {
69     binWriter.Write((int)1);
70     }
71     else
72     {
73     binWriter.Write((int)2);
74     // related guid - length
75     binWriter.Write(guid.ToByteArray().Length);
76     // related guid - data
77     binWriter.Write(guid.ToByteArray());
78     }
79 william 401 // write count (int)
80     binWriter.Write(ResultCount);
81     }
82 william 405
83     protected override void Dispose(bool disposing)
84 william 429 {
85 william 405 base.Dispose(disposing);
86 william 429 // ensure ResultCount and ResultsWritten are equal
87 william 405 if (ResultCount != ResultsWritten)
88     {
89 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);
90     Logging.logger.Error.WriteLine(message);
91     throw new InvalidOperationException(message);
92     }
93 william 405 }
94 william 401 }
95     }

  ViewVC Help
Powered by ViewVC 1.1.22