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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 451 - (show annotations) (download)
Sun Jun 2 21:37:09 2013 UTC (8 years, 4 months ago) by william
File size: 8304 byte(s)
attempt to reduce memory usage by:
1) opening the serialized results
2) seeking to the current result entry
3) read the data
4) close the data stream
5) return the result

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 using System.IO;
8
9 namespace RomCheater.Serialization
10 {
11 public interface ISearchResultReader
12 {
13 ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible;
14
15 }
16 public class SearchResultReader : SerializationReader, ISearchResultReader
17 {
18
19 private Guid _ResultGuid;
20 private Guid ResultGuid { get { return _ResultGuid; } set { _ResultGuid = value; } }
21
22 private long ResultDataOffset = 0;
23 private long CurrentResultOffset = 0;
24 //public SearchResultReader() : base() { ReadHeader(); }
25 public SearchResultReader(Guid guid) : base(guid) { ResultGuid = guid; ReadHeader(); }
26
27 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
28 private void ReadHeader()
29 {
30 try
31 {
32 using (FileStream fs = CreateReader(ResultGuid))
33 {
34 using (BinaryReader binReader = new BinaryReader(fs))
35 {
36 //int ResultsRead = 0;
37 // SRD (string)
38 string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
39 string SRD = "SRD";
40 if (magic != SRD)
41 {
42 throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
43 }
44 // version (int)
45 int version = binReader.ReadInt32();
46
47 if (version == 1)
48 {
49 // do nothing
50 }
51 else if (version == 2)
52 {
53 int guid_array_length = binReader.ReadInt32();
54 byte[] guid_array = new byte[guid_array_length];
55 binReader.Read(guid_array, 0, guid_array_length);
56 Guid g = new Guid(guid_array);
57 if (g != ResultGuid)
58 {
59 throw new InvalidOperationException(string.Format("Encountered wrong search results guid: read '{1}' excpected '{2}'", g.ToString(), ResultGuid.ToString()));
60 }
61 }
62 else
63 {
64 throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1} or {2}", version, 1, 2));
65 }
66 // resultcount
67 int resultcount = binReader.ReadInt32();
68 if (resultcount == 0)
69 {
70 throw new InvalidOperationException(string.Format("Result Count is zero"));
71 }
72 ResultCount = resultcount;
73 ResultDataOffset = binReader.BaseStream.Position;
74 //for (int i = 0; i < ResultCount; i++)
75 //{
76 // try
77 // {
78 // ResultsRead = i;
79 // uint address = 0;
80 // // assume uint for data type
81 // uint value = 0;
82 // address = binReader.ReadUInt32();
83 // value = binReader.ReadUInt32();
84 // //if (i % 100000 == 0)
85 // // logger.VerboseDebug.WriteLine("Result: @0x{0:x8}=0x{1:x8}", address, value);
86 // }
87 // catch (Exception ex)
88 // {
89 // logger.VerboseError.WriteLine("SearchResultReader.ReadHeader():Consistency Check");
90 // //logger.VerboseError.WriteLine(ex.ToString());
91 // logger.VerboseError.WriteLine("Faied entry: {0}", ResultsRead);
92 // break;
93 // }
94 // ResultsRead++; // add 1
95 //}
96 ////throw new NotImplementedException("DEBUG: testing SearchResultReader consistency");
97 //if (ResultCount != ResultsRead)
98 //{
99 // throw new InvalidOperationException(string.Format("ResultCount does not match ResultsRead: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsRead));
100 //}
101 binReader.Close();
102 }
103 fs.Close();
104 }
105 }
106 catch (System.IO.EndOfStreamException) { }
107 }
108 #region ISearchResultReader members
109 public bool ReadCurrentAddess { get; private set; }
110 public bool ReadCurrentValue { get; private set; }
111
112 public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible
113 {
114 ResultType<TValue> result = new ResultType<TValue>();
115 using (FileStream fs = CreateReader(ResultGuid))
116 {
117 using (BinaryReader binReader = new BinaryReader(fs))
118 {
119 if (CurrentResultOffset == 0)
120 {
121 binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
122 }
123 else
124 {
125 binReader.BaseStream.Seek(CurrentResultOffset, SeekOrigin.Begin); // seek to current entry
126 }
127
128 try
129 {
130 uint Address = binReader.ReadUInt32();
131 TValue Value = default(TValue);
132 Type t = typeof(TValue);
133 switch (t.Name.ToLower())
134 {
135 case "byte":
136 Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue));
137 break;
138 case "sbyte":
139 Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue));
140 break;
141 case "uint16":
142 Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue));
143 break;
144 case "int16":
145 Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue));
146 break;
147 case "uint32":
148 Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue));
149 break;
150 case "int32":
151 Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue));
152 break;
153 case "uint64":
154 Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue));
155 break;
156 case "int64":
157 Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue));
158 break;
159 }
160 result = new ResultType<TValue>(Address, Value);
161 CurrentResultOffset = binReader.BaseStream.Position;
162 }
163 catch (System.IO.EndOfStreamException) { }
164 }
165 }
166 return result;
167 }
168 #endregion
169 }
170 }

  ViewVC Help
Powered by ViewVC 1.1.22