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 |
public class SearchResultReader : SerializationReader, ISearchResultReader |
17 |
{ |
18 |
public SearchResultReader() : base() { ReadHeader(); } |
19 |
|
20 |
private void ReadHeader() |
21 |
{ |
22 |
try |
23 |
{ |
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 |
catch (System.IO.EndOfStreamException) { } |
46 |
} |
47 |
#region ISearchResultReader members |
48 |
public uint CurrentAddress() |
49 |
{ |
50 |
uint Address = 0; |
51 |
CurrentAddress(out Address); |
52 |
return Address; |
53 |
} |
54 |
public void CurrentAddress(out uint Address) |
55 |
{ |
56 |
Address = 0; |
57 |
try |
58 |
{ |
59 |
Address = binReader.ReadUInt32(); |
60 |
} |
61 |
catch (System.IO.EndOfStreamException) { } |
62 |
} |
63 |
public TValue CurrentResult<TValue>() |
64 |
{ |
65 |
TValue Value = default(TValue); |
66 |
CurrentResult<TValue>(out Value); |
67 |
return Value; |
68 |
} |
69 |
public void CurrentResult<TValue>(out TValue Value) |
70 |
{ |
71 |
Value = default(TValue); |
72 |
try |
73 |
{ |
74 |
Type t = typeof(TValue); |
75 |
switch (t.Name.ToLower()) |
76 |
{ |
77 |
case "byte": |
78 |
Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue)); |
79 |
break; |
80 |
case "sbyte": |
81 |
Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue)); |
82 |
break; |
83 |
case "uint16": |
84 |
Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue)); |
85 |
break; |
86 |
case "int16": |
87 |
Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue)); |
88 |
break; |
89 |
case "uint32": |
90 |
Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue)); |
91 |
break; |
92 |
case "int32": |
93 |
Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue)); |
94 |
break; |
95 |
case "uint64": |
96 |
Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue)); |
97 |
break; |
98 |
case "int64": |
99 |
Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue)); |
100 |
break; |
101 |
} |
102 |
} |
103 |
catch (System.IO.EndOfStreamException) { } |
104 |
} |
105 |
#endregion |
106 |
} |
107 |
} |