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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 409 - (show annotations) (download)
Thu Jun 21 20:02:40 2012 UTC (8 years, 5 months ago) by william
File size: 5062 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
19 private void ReadHeader()
20 {
21 try
22 {
23 //int ResultsRead = 0;
24 // SRD (string)
25 string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
26 string SRD = "SRD";
27 if (magic != SRD)
28 {
29 throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
30 }
31 // version (int)
32 int version = binReader.ReadInt32();
33 if (version != 1)
34 {
35 throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1}", version, 1));
36 }
37 // resultcount
38 int resultcount = binReader.ReadInt32();
39 if (resultcount == 0)
40 {
41 throw new InvalidOperationException(string.Format("Result Count is zero"));
42 }
43 ResultCount = resultcount;
44
45 //for (int i = 0; i < ResultCount; i++)
46 //{
47 // try
48 // {
49 // ResultsRead = i;
50 // uint address = 0;
51 // // assume uint for data type
52 // uint value = 0;
53 // address = binReader.ReadUInt32();
54 // value = binReader.ReadUInt32();
55 // //if (i % 100000 == 0)
56 // // logger.VerboseDebug.WriteLine("Result: @0x{0:x8}=0x{1:x8}", address, value);
57 // }
58 // catch (Exception ex)
59 // {
60 // logger.VerboseError.WriteLine("SearchResultReader.ReadHeader():Consistency Check");
61 // //logger.VerboseError.WriteLine(ex.ToString());
62 // logger.VerboseError.WriteLine("Faied entry: {0}", ResultsRead);
63 // break;
64 // }
65 // ResultsRead++; // add 1
66 //}
67 ////throw new NotImplementedException("DEBUG: testing SearchResultReader consistency");
68 //if (ResultCount != ResultsRead)
69 //{
70 // throw new InvalidOperationException(string.Format("ResultCount does not match ResultsRead: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsRead));
71 //}
72
73 }
74 catch (System.IO.EndOfStreamException) { }
75 }
76 #region ISearchResultReader members
77 public bool ReadCurrentAddess { get; private set; }
78 public bool ReadCurrentValue { get; private set; }
79
80 public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible
81 {
82 ResultType<TValue> result = new ResultType<TValue>();
83
84 uint Address = binReader.ReadUInt32();
85 TValue Value = default(TValue);
86 Type t = typeof(TValue);
87 switch (t.Name.ToLower())
88 {
89 case "byte":
90 Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue));
91 break;
92 case "sbyte":
93 Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue));
94 break;
95 case "uint16":
96 Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue));
97 break;
98 case "int16":
99 Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue));
100 break;
101 case "uint32":
102 Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue));
103 break;
104 case "int32":
105 Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue));
106 break;
107 case "uint64":
108 Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue));
109 break;
110 case "int64":
111 Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue));
112 break;
113 }
114 result = new ResultType<TValue>(Address, Value);
115 return result;
116 }
117 #endregion
118 }
119 }

  ViewVC Help
Powered by ViewVC 1.1.22