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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 448 - (show annotations) (download)
Sun Jun 2 20:22:04 2013 UTC (8 years, 3 months ago) by william
File size: 6234 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22