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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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
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 ResultsWritten++;
59 }
60 #endregion
61
62 private void WriteHeader(Guid guid)
63 {
64 // write magic: SRD
65 binWriter.Write(Encoding.UTF8.GetBytes("SRD"));
66
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 // write count (int)
80 binWriter.Write(ResultCount);
81 }
82
83 protected override void Dispose(bool disposing)
84 {
85 base.Dispose(disposing);
86 // ensure ResultCount and ResultsWritten are equal
87 if (ResultCount != ResultsWritten)
88 {
89 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 }
94 }
95 }

  ViewVC Help
Powered by ViewVC 1.1.22