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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 446 - (show annotations) (download)
Sun Jun 2 19:52:03 2013 UTC (7 years, 5 months ago) by william
File size: 4316 byte(s)
+ fix search cancel/reset

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Diagnostics;
7
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
17 private int ResultsWritten = 0;
18
19 //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 #region ISearchResultWriter members
25 public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible
26 {
27 try
28 {
29 Type t = typeof(TValue);
30
31 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 {
63 throw ex;
64 }
65 }
66 #endregion
67
68 private void WriteHeader(Guid guid)
69 {
70 try
71 {
72 // write magic: SRD
73 binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
74
75 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 {
92 throw ex;
93 }
94 }
95
96 protected override void Dispose(bool disposing)
97 {
98 try
99 {
100 base.Dispose(disposing);
101 if (Cancelled) { return; }
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 }
110 catch (Exception ex)
111 {
112 throw ex;
113 }
114 }
115 }
116 }

  ViewVC Help
Powered by ViewVC 1.1.22