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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations) (download)
Thu Jun 6 07:56:04 2013 UTC (8 years, 1 month ago) by william
File size: 5587 byte(s)

1 william 486 #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 william 401 using System.Collections.Generic;
16     using System.Linq;
17     using System.Text;
18     using System.IO;
19 william 405 using System.Diagnostics;
20 william 401
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 william 405
30 william 577 private ulong ResultsWritten = 0;
31 william 448 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
32 william 444 //public SearchResultWriter() : base() { }
33 william 528 public SearchResultWriter(Guid guid) : this(0, guid) { }
34     public SearchResultWriter(Guid guid, bool delete) : this(0, guid, delete) { }
35 william 577 public SearchResultWriter(ulong resultCount, Guid guid) : this(resultCount, guid, false) { }
36     public SearchResultWriter(ulong resultCount, Guid guid, bool delete) : base(resultCount, guid, delete) { WriteHeader(guid); }
37 william 444 //public SearchResultWriter(string filename, Guid guid) : base(CreateFileName(filename,guid), guid) { WriteHeader(guid); }
38     //public SearchResultWriter(string filename, int resultCount, Guid guid) : base(filename, resultCount, guid, false) { WriteHeader(guid); }
39     //public SearchResultWriter(string filename, int resultCount, Guid guid, bool delete) : base(filename, resultCount, guid, delete) { WriteHeader(guid); }
40 william 401 #region ISearchResultWriter members
41 william 577 public void WriteResult<TValue>(ulong address, TValue value) where TValue : IConvertible
42 william 401 {
43 william 445 try
44     {
45     Type t = typeof(TValue);
46 william 401
47 william 445 binWriter.Write(address);
48     switch (t.Name.ToLower())
49     {
50     case "byte":
51     binWriter.Write(Convert.ToByte(value));
52     break;
53     case "sbyte":
54     binWriter.Write(Convert.ToSByte(value));
55     break;
56     case "uint16":
57     binWriter.Write(Convert.ToUInt16(value));
58     break;
59     case "int16":
60     binWriter.Write(Convert.ToInt16(value));
61     break;
62     case "uint32":
63     binWriter.Write(Convert.ToUInt32(value));
64     break;
65     case "int32":
66     binWriter.Write(Convert.ToInt32(value));
67     break;
68     case "uint64":
69     binWriter.Write(Convert.ToUInt64(value));
70     break;
71     case "int64":
72     binWriter.Write(Convert.ToInt64(value));
73     break;
74     }
75     ResultsWritten++;
76     }
77     catch (Exception ex)
78 william 401 {
79 william 445 throw ex;
80 william 401 }
81     }
82     #endregion
83    
84 william 456 long ResultCountOffset = 0;
85 william 444 private void WriteHeader(Guid guid)
86 william 401 {
87 william 445 try
88     {
89     // write magic: SRD
90     binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
91 william 444
92 william 445 if (guid == Guid.Empty)
93     {
94     binWriter.Write((int)1);
95     }
96     else
97     {
98     binWriter.Write((int)2);
99     // related guid - length
100     binWriter.Write(guid.ToByteArray().Length);
101     // related guid - data
102     binWriter.Write(guid.ToByteArray());
103     }
104     // write count (int)
105     binWriter.Write(ResultCount);
106 william 577 ResultCountOffset = binWriter.BaseStream.Position - sizeof(ulong);
107 william 445 }
108     catch (Exception ex)
109 william 444 {
110 william 445 throw ex;
111 william 444 }
112 william 401 }
113 william 405
114     protected override void Dispose(bool disposing)
115 william 445 {
116 william 448 if (Cancelled) { binWriter.Close(); fileStream.Close(); return; }
117 william 445 try
118 william 405 {
119 william 456
120     //// ensure ResultCount and ResultsWritten are equal
121     //if (ResultCount != ResultsWritten)
122     //{
123     // string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten);
124     // Logging.logger.Error.WriteLine(message);
125     // throw new InvalidOperationException(message);
126     //}
127 william 445 if (ResultCount != ResultsWritten)
128     {
129 william 456 // update the number of results written
130     binWriter.BaseStream.Seek(ResultCountOffset, SeekOrigin.Begin);
131     binWriter.Write(ResultsWritten);
132 william 445 }
133 william 456 base.Dispose(disposing);
134 william 429 }
135 william 445 catch (Exception ex)
136     {
137     throw ex;
138     }
139 william 405 }
140 william 401 }
141 william 528 }

  ViewVC Help
Powered by ViewVC 1.1.22