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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 408 - (show annotations) (download)
Thu Jun 21 18:10:21 2012 UTC (8 years, 5 months ago) by william
File size: 6496 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 void CurrentAddress(out uint Address);
13 uint CurrentAddress();
14 void CurrentResult<TValue>(out TValue Value);
15 TValue CurrentResult<TValue>();
16
17 bool ReadCurrentAddess { get; }
18 bool ReadCurrentValue { get; }
19
20 }
21 public class SearchResultReader : SerializationReader, ISearchResultReader
22 {
23 public SearchResultReader() : base() { ReadHeader(); }
24
25 private void ReadHeader()
26 {
27 try
28 {
29 //int ResultsRead = 0;
30 // SRD (string)
31 string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
32 string SRD = "SRD";
33 if (magic != SRD)
34 {
35 throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
36 }
37 // version (int)
38 int version = binReader.ReadInt32();
39 if (version != 1)
40 {
41 throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1}", version, 1));
42 }
43 // resultcount
44 int resultcount = binReader.ReadInt32();
45 if (resultcount == 0)
46 {
47 throw new InvalidOperationException(string.Format("Result Count is zero"));
48 }
49 ResultCount = resultcount;
50
51 //for (int i = 0; i < ResultCount; i++)
52 //{
53 // try
54 // {
55 // ResultsRead = i;
56 // uint address = 0;
57 // // assume uint for data type
58 // uint value = 0;
59 // address = binReader.ReadUInt32();
60 // value = binReader.ReadUInt32();
61 // //if (i % 100000 == 0)
62 // // logger.VerboseDebug.WriteLine("Result: @0x{0:x8}=0x{1:x8}", address, value);
63 // }
64 // catch (Exception ex)
65 // {
66 // logger.VerboseError.WriteLine("SearchResultReader.ReadHeader():Consistency Check");
67 // //logger.VerboseError.WriteLine(ex.ToString());
68 // logger.VerboseError.WriteLine("Faied entry: {0}", ResultsRead);
69 // break;
70 // }
71 // ResultsRead++; // add 1
72 //}
73 ////throw new NotImplementedException("DEBUG: testing SearchResultReader consistency");
74 //if (ResultCount != ResultsRead)
75 //{
76 // throw new InvalidOperationException(string.Format("ResultCount does not match ResultsRead: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsRead));
77 //}
78
79 }
80 catch (System.IO.EndOfStreamException) { }
81 }
82 #region ISearchResultReader members
83 public bool ReadCurrentAddess { get; private set; }
84 public bool ReadCurrentValue { get; private set; }
85 public uint CurrentAddress()
86 {
87 uint Address = 0;
88 CurrentAddress(out Address);
89 return Address;
90 }
91 public void CurrentAddress(out uint Address)
92 {
93 Address = 0;
94 try
95 {
96 if (ReadCurrentAddess && !ReadCurrentValue)
97 {
98 throw new InvalidOperationException("Cannot read Current Address because the current Value has not been read. Please call CurrentResult<TValue>() first.");
99 }
100 Address = binReader.ReadUInt32();
101 ReadCurrentAddess = true;
102 ReadCurrentValue = false;
103 }
104 catch (System.IO.EndOfStreamException) { }
105 }
106 public TValue CurrentResult<TValue>()
107 {
108 TValue Value = default(TValue);
109 CurrentResult<TValue>(out Value);
110 return Value;
111 }
112 public void CurrentResult<TValue>(out TValue Value)
113 {
114 Value = default(TValue);
115 if (!ReadCurrentAddess)
116 {
117 throw new InvalidOperationException("Cannot read Current Value because the current Address has not been read. Please call CurrentAddress() first.");
118 }
119 try
120 {
121 Type t = typeof(TValue);
122 switch (t.Name.ToLower())
123 {
124 case "byte":
125 Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue));
126 break;
127 case "sbyte":
128 Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue));
129 break;
130 case "uint16":
131 Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue));
132 break;
133 case "int16":
134 Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue));
135 break;
136 case "uint32":
137 Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue));
138 break;
139 case "int32":
140 Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue));
141 break;
142 case "uint64":
143 Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue));
144 break;
145 case "int64":
146 Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue));
147 break;
148 }
149 ReadCurrentValue = true;
150 ReadCurrentAddess = false;
151 }
152 catch (System.IO.EndOfStreamException) { }
153 }
154 #endregion
155 }
156 }

  ViewVC Help
Powered by ViewVC 1.1.22