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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 444 - (show annotations) (download)
Sun Jun 2 18:56:41 2013 UTC (8 years, 6 months ago) by william
File size: 6120 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 RomCheater.Docking.MemorySearch;
6 using RomCheater.Logging;
7
8 namespace RomCheater.Serialization
9 {
10 public interface ISearchResultReader
11 {
12 ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible;
13
14 }
15 public class SearchResultReader : SerializationReader, ISearchResultReader
16 {
17 //public SearchResultReader() : base() { ReadHeader(); }
18 public SearchResultReader(Guid guid) : base(guid) { ReadHeader(guid); }
19
20 private void ReadHeader(Guid guid)
21 {
22 try
23 {
24 //int ResultsRead = 0;
25 // SRD (string)
26 string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
27 string SRD = "SRD";
28 if (magic != SRD)
29 {
30 throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
31 }
32 // version (int)
33 int version = binReader.ReadInt32();
34
35 if (version == 1)
36 {
37 // do nothing
38 }
39 else if (version == 2)
40 {
41 int guid_array_length = binReader.ReadInt32();
42 byte[] guid_array = new byte[guid_array_length];
43 binReader.Read(guid_array, 0, guid_array_length);
44 Guid g = new Guid(guid_array);
45 if (g != guid)
46 {
47 throw new InvalidOperationException(string.Format("Encountered wrong search results guid: read '{1}' excpected '{2}'", g.ToString(), guid.ToString()));
48 }
49 }
50 else
51 {
52 throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1} or {2}", version, 1, 2));
53 }
54 // resultcount
55 int resultcount = binReader.ReadInt32();
56 if (resultcount == 0)
57 {
58 throw new InvalidOperationException(string.Format("Result Count is zero"));
59 }
60 ResultCount = resultcount;
61
62 //for (int i = 0; i < ResultCount; i++)
63 //{
64 // try
65 // {
66 // ResultsRead = i;
67 // uint address = 0;
68 // // assume uint for data type
69 // uint value = 0;
70 // address = binReader.ReadUInt32();
71 // value = binReader.ReadUInt32();
72 // //if (i % 100000 == 0)
73 // // logger.VerboseDebug.WriteLine("Result: @0x{0:x8}=0x{1:x8}", address, value);
74 // }
75 // catch (Exception ex)
76 // {
77 // logger.VerboseError.WriteLine("SearchResultReader.ReadHeader():Consistency Check");
78 // //logger.VerboseError.WriteLine(ex.ToString());
79 // logger.VerboseError.WriteLine("Faied entry: {0}", ResultsRead);
80 // break;
81 // }
82 // ResultsRead++; // add 1
83 //}
84 ////throw new NotImplementedException("DEBUG: testing SearchResultReader consistency");
85 //if (ResultCount != ResultsRead)
86 //{
87 // throw new InvalidOperationException(string.Format("ResultCount does not match ResultsRead: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsRead));
88 //}
89
90 }
91 catch (System.IO.EndOfStreamException) { }
92 }
93 #region ISearchResultReader members
94 public bool ReadCurrentAddess { get; private set; }
95 public bool ReadCurrentValue { get; private set; }
96
97 public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible
98 {
99
100 ResultType<TValue> result = new ResultType<TValue>();
101 try
102 {
103
104 uint Address = binReader.ReadUInt32();
105 TValue Value = default(TValue);
106 Type t = typeof(TValue);
107 switch (t.Name.ToLower())
108 {
109 case "byte":
110 Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue));
111 break;
112 case "sbyte":
113 Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue));
114 break;
115 case "uint16":
116 Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue));
117 break;
118 case "int16":
119 Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue));
120 break;
121 case "uint32":
122 Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue));
123 break;
124 case "int32":
125 Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue));
126 break;
127 case "uint64":
128 Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue));
129 break;
130 case "int64":
131 Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue));
132 break;
133 }
134 result = new ResultType<TValue>(Address, Value);
135 return result;
136 }
137 catch (System.IO.EndOfStreamException)
138 { return result; }
139 }
140 #endregion
141 }
142 }

  ViewVC Help
Powered by ViewVC 1.1.22