1 |
william |
401 |
using System; |
2 |
|
|
using System.Collections.Generic; |
3 |
|
|
using System.Linq; |
4 |
|
|
using System.Text; |
5 |
william |
402 |
using RomCheater.Docking.MemorySearch; |
6 |
william |
405 |
using RomCheater.Logging; |
7 |
william |
401 |
|
8 |
|
|
namespace RomCheater.Serialization |
9 |
|
|
{ |
10 |
william |
402 |
public interface ISearchResultReader |
11 |
william |
401 |
{ |
12 |
william |
409 |
ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible; |
13 |
william |
403 |
|
14 |
william |
401 |
} |
15 |
william |
402 |
public class SearchResultReader : SerializationReader, ISearchResultReader |
16 |
|
|
{ |
17 |
william |
444 |
//public SearchResultReader() : base() { ReadHeader(); } |
18 |
|
|
public SearchResultReader(Guid guid) : base(guid) { ReadHeader(guid); } |
19 |
william |
402 |
|
20 |
william |
444 |
private void ReadHeader(Guid guid) |
21 |
william |
402 |
{ |
22 |
|
|
try |
23 |
|
|
{ |
24 |
william |
408 |
//int ResultsRead = 0; |
25 |
william |
402 |
// 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 |
william |
444 |
|
35 |
|
|
if (version == 1) |
36 |
william |
402 |
{ |
37 |
william |
444 |
// do nothing |
38 |
william |
402 |
} |
39 |
william |
444 |
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 |
william |
402 |
// 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 |
william |
405 |
|
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 |
william |
402 |
} |
91 |
|
|
catch (System.IO.EndOfStreamException) { } |
92 |
|
|
} |
93 |
william |
403 |
#region ISearchResultReader members |
94 |
|
|
public bool ReadCurrentAddess { get; private set; } |
95 |
|
|
public bool ReadCurrentValue { get; private set; } |
96 |
william |
409 |
|
97 |
|
|
public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible |
98 |
william |
402 |
{ |
99 |
william |
410 |
|
100 |
william |
409 |
ResultType<TValue> result = new ResultType<TValue>(); |
101 |
william |
410 |
try |
102 |
|
|
{ |
103 |
william |
409 |
|
104 |
william |
410 |
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 |
william |
403 |
} |
137 |
william |
410 |
catch (System.IO.EndOfStreamException) |
138 |
|
|
{ return result; } |
139 |
william |
402 |
} |
140 |
|
|
#endregion |
141 |
|
|
} |
142 |
william |
401 |
} |