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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 486 - (show annotations) (download)
Mon Jun 3 20:11:43 2013 UTC (7 years, 3 months ago) by william
File size: 5444 byte(s)
+ allow the delete of result files, when reading (if delete is true)

1 #region Logging Defines
2 // include this any class or method that required logging, and comment-out what is not needed
3
4 #region Enabled logging levels
5 #define LOGGING_ENABLE_INFO
6 #define LOGGING_ENABLE_WARN
7 #define LOGGING_ENABLE_DEBUG
8 #define LOGGING_ENABLE_VERBOSEDEBUG
9 #define LOGGING_ENABLE_ERROR
10 #define LOGGING_ENABLE_VERBOSEERROR
11 #define LOGGING_ENABLE_PROFILER
12 #endregion
13 #endregion
14 using System;
15 using System.Collections.Generic;
16 using System.Linq;
17 using System.Text;
18 using System.IO;
19 using System.Diagnostics;
20
21 namespace RomCheater.Serialization
22 {
23 public interface ISearchResultWriter
24 {
25 void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible;
26 }
27 public class SearchResultWriter : SerializationWriter, ISearchResultWriter
28 {
29
30 private int ResultsWritten = 0;
31 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
32 //public SearchResultWriter() : base() { }
33 public SearchResultWriter(int resultCount, Guid guid) : this(resultCount, guid,false) { WriteHeader(guid); }
34 public SearchResultWriter(int resultCount, Guid guid, bool delete) : base(resultCount, guid,delete) { WriteHeader(guid); }
35 //public SearchResultWriter(string filename, Guid guid) : base(CreateFileName(filename,guid), guid) { WriteHeader(guid); }
36 //public SearchResultWriter(string filename, int resultCount, Guid guid) : base(filename, resultCount, guid, false) { WriteHeader(guid); }
37 //public SearchResultWriter(string filename, int resultCount, Guid guid, bool delete) : base(filename, resultCount, guid, delete) { WriteHeader(guid); }
38 #region ISearchResultWriter members
39 public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible
40 {
41 try
42 {
43 Type t = typeof(TValue);
44
45 binWriter.Write(address);
46 switch (t.Name.ToLower())
47 {
48 case "byte":
49 binWriter.Write(Convert.ToByte(value));
50 break;
51 case "sbyte":
52 binWriter.Write(Convert.ToSByte(value));
53 break;
54 case "uint16":
55 binWriter.Write(Convert.ToUInt16(value));
56 break;
57 case "int16":
58 binWriter.Write(Convert.ToInt16(value));
59 break;
60 case "uint32":
61 binWriter.Write(Convert.ToUInt32(value));
62 break;
63 case "int32":
64 binWriter.Write(Convert.ToInt32(value));
65 break;
66 case "uint64":
67 binWriter.Write(Convert.ToUInt64(value));
68 break;
69 case "int64":
70 binWriter.Write(Convert.ToInt64(value));
71 break;
72 }
73 ResultsWritten++;
74 }
75 catch (Exception ex)
76 {
77 throw ex;
78 }
79 }
80 #endregion
81
82 long ResultCountOffset = 0;
83 private void WriteHeader(Guid guid)
84 {
85 try
86 {
87 // write magic: SRD
88 binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
89
90 if (guid == Guid.Empty)
91 {
92 binWriter.Write((int)1);
93 }
94 else
95 {
96 binWriter.Write((int)2);
97 // related guid - length
98 binWriter.Write(guid.ToByteArray().Length);
99 // related guid - data
100 binWriter.Write(guid.ToByteArray());
101 }
102 // write count (int)
103 binWriter.Write(ResultCount);
104 ResultCountOffset = binWriter.BaseStream.Position - sizeof(int);
105 }
106 catch (Exception ex)
107 {
108 throw ex;
109 }
110 }
111
112 protected override void Dispose(bool disposing)
113 {
114 if (Cancelled) { binWriter.Close(); fileStream.Close(); return; }
115 try
116 {
117
118 //// ensure ResultCount and ResultsWritten are equal
119 //if (ResultCount != ResultsWritten)
120 //{
121 // string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
122 // Logging.logger.Error.WriteLine(message);
123 // throw new InvalidOperationException(message);
124 //}
125 if (ResultCount != ResultsWritten)
126 {
127 // update the number of results written
128 binWriter.BaseStream.Seek(ResultCountOffset, SeekOrigin.Begin);
129 binWriter.Write(ResultsWritten);
130 }
131 base.Dispose(disposing);
132 }
133 catch (Exception ex)
134 {
135 throw ex;
136 }
137 }
138 }
139 }

  ViewVC Help
Powered by ViewVC 1.1.22