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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 538 - (show annotations) (download)
Wed Jun 5 10:33:38 2013 UTC (8 years, 1 month ago) by william
File size: 15001 byte(s)

1 #region Logging Defines
2 // include this any class or method that required logging, and comment-out what is not needed
3
4 #region Enabled logging levels
5 #define LOGGING_ENABLE_INFO
6 #define LOGGING_ENABLE_WARN
7 #define LOGGING_ENABLE_DEBUG
8 #define LOGGING_ENABLE_VERBOSEDEBUG
9 #define LOGGING_ENABLE_ERROR
10 #define LOGGING_ENABLE_VERBOSEERROR
11 #define LOGGING_ENABLE_PROFILER
12 #endregion
13 #endregion
14 using System;
15 using System.Collections.Generic;
16 using System.Linq;
17 using System.Text;
18 using RomCheater.Docking.MemorySearch;
19 using RomCheater.Logging;
20 using System.IO;
21 using Utilities.TransparentControls;
22 using Sojaner.MemoryScanner.MemoryProviers;
23 using RomCheater.PluginFramework.Interfaces;
24 using System.Windows.Forms;
25 using RomCheater.Core;
26
27 namespace RomCheater.Serialization
28 {
29 public interface ISearchResultReader
30 {
31 ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible;
32 }
33
34 public interface ISerializedResult
35 {
36 SearchResultReader.ResultReaderCollection GetResults(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, Func<bool> cancelmethod);
37 bool ContainsAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, Func<bool> cancelmethod);
38 bool ContainsAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out int index, Func<bool> cancelmethod);
39 //void GetResultAtAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out StructResultType<ulong> result);
40 void GetResultAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out StructResultType<ulong> result, Func<bool> cancelmethod);
41 }
42
43
44
45 public class SearchResultReader : SerializationReader, ISearchResultReader, ISerializedResult
46 {
47 #region ISerializedResult members
48 public ResultReaderCollection GetResults(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, Func<bool> cancelmethod)
49 {
50 ResultReaderCollection collection = new ResultReaderCollection(this, unsigned, datatype, update_progress, cancelmethod);
51 return collection;
52 }
53 public bool ContainsAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, Func<bool> cancelmethod)
54 {
55 int index = 0;
56 return ContainsAddress(address, unsigned, datatype, update_progress, out index, cancelmethod);
57 }
58 public bool ContainsAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out int index, Func<bool> cancelmethod)
59 {
60 bool contains = false;
61 index = 0;
62 foreach (var result in GetResults(unsigned, datatype, update_progress, cancelmethod))
63 {
64 if (cancelmethod.Invoke()) { break; }
65 if (result.Address == address)
66 {
67 if (contains)
68 {
69 throw new ArgumentOutOfRangeException("address", string.Format("Found more than one result that matched address: 0x{0} - index={1}", address.ToString("X"), index));
70 }
71 contains = true;
72 break; // should never throw the exception, if we break here (only returning/checking the first found address
73 }
74 index++;
75 }
76 return contains;
77 }
78 public void GetResultAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out StructResultType<ulong> result, Func<bool> cancelmethod)
79 {
80 result = StructResultType<ulong>.Empty;
81 int count = 0;
82 foreach (var entry in GetResults(unsigned, datatype, update_progress, cancelmethod))
83 {
84 if (index == count)
85 {
86 result = entry;
87 break;
88 }
89 count++;
90 }
91 }
92 //public void GetResultAtAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out StructResultType<ulong> result)
93 //{
94 // result = StructResultType<ulong>.Empty;
95 // int index = 0;
96 // if (ContainsAddress(address, unsigned, datatype, update_progress, out index))
97 // {
98 // result = GetResultAtIndex(this,index, unsigned, datatype, update_progress);
99 // }
100 // else
101 // {
102 // throw new KeyNotFoundException(string.Format("Could not find result for address: 0x{0}", address.ToString("X")));
103 // }
104 //}
105 #endregion
106
107
108 #region sub-clases
109 #region public class ResultReaderEnumerator : IEnumerator<StructResultType<ulong>>
110 public class ResultReaderEnumerator : IEnumerator<StructResultType<ulong>>
111 {
112 //private IEnumerator<StructResultType<ulong>> _enumerator;
113
114 private int _index = 0;
115 private int index { get { return _index; } set { _index = value; } }
116 private SearchResultReader _reader;
117 private SearchResultReader reader { get { CheckReaderNull(); return _reader; } set { _reader = value; CheckReaderNull(); } }
118 private bool _unsigned;
119 private bool unsigned { get { return _unsigned; } set { _unsigned = value; } }
120 private SearchDataTypes _datatype;
121 private SearchDataTypes datatype { get { return _datatype; } set { _datatype = value; } }
122 private Action<int, string> _update_progress;
123 private Action<int, string> update_progress { get { return _update_progress; } set { _update_progress = value; } }
124
125
126 private Func<bool> _cancel_method;
127 private Func<bool> cancel_method { get { return _cancel_method; } set { _cancel_method = value; } }
128
129
130 private int MinimumIndex { get { return this.reader.MinimumIndex; } }
131 private int MaximumIndex { get { return this.reader.MaximumIndex; } }
132
133
134 #region Exceptions
135 private void CheckReaderNull()
136 {
137 if (_reader == null)
138 {
139 throw new ArgumentNullException("reader", "Reader cannot be null");
140 }
141 }
142 private void CheckIndexLessThanMin()
143 {
144 if (_index < MinimumIndex)
145 {
146 throw new ArgumentOutOfRangeException("index", string.Format("index cannot be less than {0}", MinimumIndex));
147 }
148 }
149 private void CheckIndexGreaterThanMax()
150 {
151 if (_index > MaximumIndex)
152 {
153 throw new ArgumentOutOfRangeException("index", string.Format("index cannot be greater than {0}", MaximumIndex));
154 }
155 }
156 #endregion
157
158 public ResultReaderEnumerator(SearchResultReader reader, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, Func<bool> cancelmethod)
159 {
160 this.reader = reader;
161 this.unsigned = unsigned;
162 this.datatype = datatype;
163 this.update_progress = update_progress;
164 this.cancel_method = cancelmethod;
165 this.index = -1;
166 }
167
168 public StructResultType<ulong> Current
169 {
170 get
171 {
172 // result the current entry (at position)
173 //throw new NotImplementedException();
174 CheckIndexLessThanMin();
175 CheckIndexGreaterThanMax();
176 return GetResultAtIndex(this.reader, this.index, this.unsigned, this.datatype, this.update_progress, this.cancel_method);
177 }
178 }
179
180 public void Dispose()
181 {
182 //throw new NotImplementedException();
183 }
184
185 object System.Collections.IEnumerator.Current
186 {
187 get { return this.Current; }
188 }
189
190 public bool MoveNext()
191 {
192 this.index++; // increment after processing index at MinimumIndex
193 if(this.index >= this.MinimumIndex && this.index <= this.MaximumIndex)
194 {
195 return true;
196 }
197 Reset();
198 return false;
199
200 }
201
202 public void Reset()
203 {
204 this.index = -1;
205 }
206 }
207 #endregion
208
209 #region public class ResultReaderCollection : IEnumerable<StructResultType<ulong>>
210 public class ResultReaderCollection : IEnumerable<StructResultType<ulong>>
211 {
212 private SearchResultReader _reader;
213 private SearchResultReader reader { get { CheckReaderNull(); return _reader; } set { _reader = value; CheckReaderNull(); } }
214 private bool _unsigned;
215 private bool unsigned { get { return _unsigned; } set { _unsigned = value; } }
216 private SearchDataTypes _datatype;
217 private SearchDataTypes datatype { get { return _datatype; } set { _datatype = value; } }
218 private Action<int, string> _update_progress;
219 private Action<int, string> update_progress { get { return _update_progress; } set { _update_progress = value; } }
220
221 private Func<bool> _cancel_method;
222 private Func<bool> cancel_method { get { return _cancel_method; } set { _cancel_method = value; } }
223
224 public ResultReaderCollection(SearchResultReader reader, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, Func<bool> cancelmethod)
225 {
226 this.reader = reader;
227 this.unsigned = unsigned;
228 this.datatype = datatype;
229 this.update_progress = update_progress;
230 this.cancel_method = cancelmethod;
231 }
232
233 #region Exceptions
234 private void CheckReaderNull()
235 {
236 if (_reader == null)
237 {
238 throw new ArgumentNullException("reader", "Reader cannot be null");
239 }
240 }
241 #endregion
242
243 public IEnumerator<StructResultType<ulong>> GetEnumerator()
244 {
245 return new ResultReaderEnumerator(_reader, _unsigned, _datatype, _update_progress, this.cancel_method);
246 }
247
248 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
249 {
250 return GetEnumerator();
251 }
252 }
253 #endregion
254
255 #endregion
256
257
258 public delegate void update_progress(int progress, string message);
259
260 private Guid _ResultGuid;
261 private Guid ResultGuid { get { return _ResultGuid; } set { _ResultGuid = value; } }
262
263
264
265
266
267 private int MinimumIndex { get { return 0; } }
268 private int MaximumIndex { get { return this.ResultCount - 1; } }
269
270 private long ResultDataOffset = 0;
271 //private long CurrentResultOffset = 0;
272 //public SearchResultReader() : base() { ReadHeader(); }
273 public SearchResultReader(Guid guid) : this(guid, false) { }
274 public SearchResultReader(Guid guid, bool delete) : base(guid, delete) { ResultGuid = guid; ReadHeader(); }
275
276 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
277 private void ReadHeader()
278 {
279 try
280 {
281 using (FileStream fs = CreateReader())
282 {
283 using (BinaryReader binReader = new BinaryReader(fs))
284 {
285 //int ResultsRead = 0;
286 // SRD (string)
287 string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
288 string SRD = "SRD";
289 if (magic != SRD)
290 {
291 throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
292 }
293 // version (int)
294 int version = binReader.ReadInt32();
295
296 if (version == 1)
297 {
298 // do nothing
299 }
300 else if (version == 2)
301 {
302 int guid_array_length = binReader.ReadInt32();
303 byte[] guid_array = new byte[guid_array_length];
304 binReader.Read(guid_array, 0, guid_array_length);
305 Guid g = new Guid(guid_array);
306 if (g != ResultGuid)
307 {
308 throw new InvalidOperationException(string.Format("Encountered wrong search results guid: read '{1}' excpected '{2}'", g.ToString(), ResultGuid.ToString()));
309 }
310 }
311 else
312 {
313 throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1} or {2}", version, 1, 2));
314 }
315 // resultcount
316 int resultcount = binReader.ReadInt32();
317 if (resultcount == 0)
318 {
319 throw new InvalidOperationException(string.Format("Result Count is zero"));
320 }
321 ResultCount = resultcount;
322 ResultDataOffset = binReader.BaseStream.Position;
323 binReader.Close();
324 }
325 fs.Close();
326 }
327 }
328 catch (System.IO.EndOfStreamException) { }
329 }
330 #region ISearchResultReader members
331 public bool ReadCurrentAddess { get; private set; }
332 public bool ReadCurrentValue { get; private set; }
333
334 [Obsolete("GetNextResult has been replaced by GetResults")]
335 public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible
336 {
337 return new ResultType<TValue>();
338 }
339 #endregion
340
341 }
342 }

  ViewVC Help
Powered by ViewVC 1.1.22