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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 456 - (show annotations) (download)
Mon Jun 3 01:45:40 2013 UTC (8 years, 1 month ago) by william
File size: 4903 byte(s)

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 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
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 long ResultCountOffset = 0;
69 private void WriteHeader(Guid guid)
70 {
71 try
72 {
73 // write magic: SRD
74 binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
75
76 if (guid == Guid.Empty)
77 {
78 binWriter.Write((int)1);
79 }
80 else
81 {
82 binWriter.Write((int)2);
83 // related guid - length
84 binWriter.Write(guid.ToByteArray().Length);
85 // related guid - data
86 binWriter.Write(guid.ToByteArray());
87 }
88 // write count (int)
89 binWriter.Write(ResultCount);
90 ResultCountOffset = binWriter.BaseStream.Position - sizeof(int);
91 }
92 catch (Exception ex)
93 {
94 throw ex;
95 }
96 }
97
98 protected override void Dispose(bool disposing)
99 {
100 if (Cancelled) { binWriter.Close(); fileStream.Close(); return; }
101 try
102 {
103
104 //// ensure ResultCount and ResultsWritten are equal
105 //if (ResultCount != ResultsWritten)
106 //{
107 // string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
108 // Logging.logger.Error.WriteLine(message);
109 // throw new InvalidOperationException(message);
110 //}
111 if (ResultCount != ResultsWritten)
112 {
113 // update the number of results written
114 binWriter.BaseStream.Seek(ResultCountOffset, SeekOrigin.Begin);
115 binWriter.Write(ResultsWritten);
116 }
117 base.Dispose(disposing);
118 }
119 catch (Exception ex)
120 {
121 throw ex;
122 }
123 }
124 }
125 }

  ViewVC Help
Powered by ViewVC 1.1.22