ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater/Serialization/SearchResultReader.cs
Revision: 403
Committed: Thu Jun 21 07:13:04 2012 UTC (11 years, 3 months ago) by william
File size: 4948 byte(s)
Log Message:

File Contents

# Content
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using RomCheater.Docking.MemorySearch;
6
7 namespace RomCheater.Serialization
8 {
9 public interface ISearchResultReader
10 {
11 void CurrentAddress(out uint Address);
12 uint CurrentAddress();
13 void CurrentResult<TValue>(out TValue Value);
14 TValue CurrentResult<TValue>();
15
16 bool ReadCurrentAddess { get; }
17 bool ReadCurrentValue { get; }
18
19 }
20 public class SearchResultReader : SerializationReader, ISearchResultReader
21 {
22 public SearchResultReader() : base() { ReadHeader(); }
23
24 private void ReadHeader()
25 {
26 try
27 {
28 // SRD (string)
29 string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
30 string SRD = "SRD";
31 if (magic != SRD)
32 {
33 throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
34 }
35 // version (int)
36 int version = binReader.ReadInt32();
37 if (version != 1)
38 {
39 throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1}", version, 1));
40 }
41 // resultcount
42 int resultcount = binReader.ReadInt32();
43 if (resultcount == 0)
44 {
45 throw new InvalidOperationException(string.Format("Result Count is zero"));
46 }
47 ResultCount = resultcount;
48 }
49 catch (System.IO.EndOfStreamException) { }
50 }
51 #region ISearchResultReader members
52 public bool ReadCurrentAddess { get; private set; }
53 public bool ReadCurrentValue { get; private set; }
54 public uint CurrentAddress()
55 {
56 uint Address = 0;
57 CurrentAddress(out Address);
58 return Address;
59 }
60 public void CurrentAddress(out uint Address)
61 {
62 Address = 0;
63 try
64 {
65 if (ReadCurrentAddess && !ReadCurrentValue)
66 {
67 throw new InvalidOperationException("Cannot read Current Address because the current Value has not been read. Please call CurrentResult<TValue>() first.");
68 }
69 Address = binReader.ReadUInt32();
70 ReadCurrentAddess = true;
71 ReadCurrentValue = false;
72 }
73 catch (System.IO.EndOfStreamException) { }
74 }
75 public TValue CurrentResult<TValue>()
76 {
77 TValue Value = default(TValue);
78 CurrentResult<TValue>(out Value);
79 return Value;
80 }
81 public void CurrentResult<TValue>(out TValue Value)
82 {
83 Value = default(TValue);
84 if (!ReadCurrentAddess)
85 {
86 throw new InvalidOperationException("Cannot read Current Value because the current Address has not been read. Please call CurrentAddress() first.");
87 }
88 try
89 {
90 Type t = typeof(TValue);
91 switch (t.Name.ToLower())
92 {
93 case "byte":
94 Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue));
95 break;
96 case "sbyte":
97 Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue));
98 break;
99 case "uint16":
100 Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue));
101 break;
102 case "int16":
103 Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue));
104 break;
105 case "uint32":
106 Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue));
107 break;
108 case "int32":
109 Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue));
110 break;
111 case "uint64":
112 Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue));
113 break;
114 case "int64":
115 Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue));
116 break;
117 }
118 ReadCurrentValue = true;
119 ReadCurrentAddess = false;
120 }
121 catch (System.IO.EndOfStreamException) { }
122 }
123 #endregion
124 }
125 }