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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 455 - (show annotations) (download)
Mon Jun 3 00:37:29 2013 UTC (8 years, 2 months ago) by william
File size: 13923 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
25 namespace RomCheater.Serialization
26 {
27 public interface ISearchResultReader
28 {
29 ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible;
30
31 }
32 public class SearchResultReader : SerializationReader, ISearchResultReader
33 {
34
35 private Guid _ResultGuid;
36 private Guid ResultGuid { get { return _ResultGuid; } set { _ResultGuid = value; } }
37
38 private long ResultDataOffset = 0;
39 private long CurrentResultOffset = 0;
40 //public SearchResultReader() : base() { ReadHeader(); }
41 public SearchResultReader(Guid guid) : base(guid) { ResultGuid = guid; ReadHeader(); }
42
43 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
44 private void ReadHeader()
45 {
46 try
47 {
48 using (FileStream fs = CreateReader(ResultGuid))
49 {
50 using (BinaryReader binReader = new BinaryReader(fs))
51 {
52 //int ResultsRead = 0;
53 // SRD (string)
54 string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
55 string SRD = "SRD";
56 if (magic != SRD)
57 {
58 throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
59 }
60 // version (int)
61 int version = binReader.ReadInt32();
62
63 if (version == 1)
64 {
65 // do nothing
66 }
67 else if (version == 2)
68 {
69 int guid_array_length = binReader.ReadInt32();
70 byte[] guid_array = new byte[guid_array_length];
71 binReader.Read(guid_array, 0, guid_array_length);
72 Guid g = new Guid(guid_array);
73 if (g != ResultGuid)
74 {
75 throw new InvalidOperationException(string.Format("Encountered wrong search results guid: read '{1}' excpected '{2}'", g.ToString(), ResultGuid.ToString()));
76 }
77 }
78 else
79 {
80 throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1} or {2}", version, 1, 2));
81 }
82 // resultcount
83 int resultcount = binReader.ReadInt32();
84 if (resultcount == 0)
85 {
86 throw new InvalidOperationException(string.Format("Result Count is zero"));
87 }
88 ResultCount = resultcount;
89 ResultDataOffset = binReader.BaseStream.Position;
90 //for (int i = 0; i < ResultCount; i++)
91 //{
92 // try
93 // {
94 // ResultsRead = i;
95 // uint address = 0;
96 // // assume uint for data type
97 // uint value = 0;
98 // address = binReader.ReadUInt32();
99 // value = binReader.ReadUInt32();
100 // //if (i % 100000 == 0)
101 // // logger.VerboseDebug.WriteLine("Result: @0x{0:x8}=0x{1:x8}", address, value);
102 // }
103 // catch (Exception ex)
104 // {
105 // logger.VerboseError.WriteLine("SearchResultReader.ReadHeader():Consistency Check");
106 // //logger.VerboseError.WriteLine(ex.ToString());
107 // logger.VerboseError.WriteLine("Faied entry: {0}", ResultsRead);
108 // break;
109 // }
110 // ResultsRead++; // add 1
111 //}
112 ////throw new NotImplementedException("DEBUG: testing SearchResultReader consistency");
113 //if (ResultCount != ResultsRead)
114 //{
115 // throw new InvalidOperationException(string.Format("ResultCount does not match ResultsRead: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsRead));
116 //}
117 binReader.Close();
118 }
119 fs.Close();
120 }
121 }
122 catch (System.IO.EndOfStreamException) { }
123 }
124 #region ISearchResultReader members
125 public bool ReadCurrentAddess { get; private set; }
126 public bool ReadCurrentValue { get; private set; }
127
128
129 public List<ResultType<TValue>> GetResults<TValue>(ProgressBarWithPercentageLabel progress) where TValue : IConvertible
130 {
131 List<ResultType<TValue>> results = new List<ResultType<TValue>>();
132 using (FileStream fs = CreateReader(ResultGuid))
133 {
134 using (BinaryReader binReader = new BinaryReader(fs))
135 {
136 binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
137 for (int i = 0; i < ResultCount; i++ )
138 {
139 uint Address = binReader.ReadUInt32();
140 TValue Value = default(TValue);
141 Type t = typeof(TValue);
142 switch (t.Name.ToLower())
143 {
144 case "byte":
145 Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue));
146 break;
147 case "sbyte":
148 Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue));
149 break;
150 case "uint16":
151 Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue));
152 break;
153 case "int16":
154 Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue));
155 break;
156 case "uint32":
157 Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue));
158 break;
159 case "int32":
160 Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue));
161 break;
162 case "uint64":
163 Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue));
164 break;
165 case "int64":
166 Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue));
167 break;
168 }
169 results.Add(new ResultType<TValue>(Address, Value));
170 progress.Value = (int)(100.0 * (double)i / (double)ResultCount);
171 progress.Message = string.Format("Preloading Results ... 0x{0:x8}", Address);
172 }
173 }
174 }
175 return results;
176 }
177
178 public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible
179 {
180 ResultType<TValue> result = new ResultType<TValue>();
181 using (FileStream fs = CreateReader(ResultGuid))
182 {
183 using (BinaryReader binReader = new BinaryReader(fs))
184 {
185 if (CurrentResultOffset == 0)
186 {
187 binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
188 }
189 else
190 {
191 binReader.BaseStream.Seek(CurrentResultOffset, SeekOrigin.Begin); // seek to current entry
192 }
193
194 try
195 {
196 uint Address = binReader.ReadUInt32();
197 TValue Value = default(TValue);
198 Type t = typeof(TValue);
199 switch (t.Name.ToLower())
200 {
201 case "byte":
202 Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue));
203 break;
204 case "sbyte":
205 Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue));
206 break;
207 case "uint16":
208 Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue));
209 break;
210 case "int16":
211 Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue));
212 break;
213 case "uint32":
214 Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue));
215 break;
216 case "int32":
217 Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue));
218 break;
219 case "uint64":
220 Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue));
221 break;
222 case "int64":
223 Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue));
224 break;
225 }
226 result = new ResultType<TValue>(Address, Value);
227 CurrentResultOffset = binReader.BaseStream.Position;
228 }
229 catch (System.IO.EndOfStreamException) { }
230 }
231 }
232 return result;
233 }
234 #endregion
235
236 public List<ResultType<object>> GetResults(SearchType SearchArgs, IAcceptsProcessAndConfig iapc, ProgressBarWithPercentageLabel progress)
237 {
238 return GetResultsInternal(SearchArgs, iapc, progress);
239 }
240
241 private List<ResultType<object>> GetResultsInternal(SearchType SearchArgs, IAcceptsProcessAndConfig iapc, ProgressBarWithPercentageLabel progress)
242 {
243 progress.Value = 0;
244 progress.Message = string.Empty;
245
246
247 List<ResultType<object>> second_tmp_Results = new List<ResultType<object>>();
248 using (FileStream fs = CreateReader(ResultGuid))
249 {
250 using (BinaryReader binReader = new BinaryReader(fs))
251 {
252
253 binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
254 for (int i = 0; i < ResultCount; i++)
255 {
256 uint Address = binReader.ReadUInt32();
257 object Value = 0;
258 switch (SearchArgs.DataType)
259 {
260 case SearchDataTypes._8bits:
261 if (SearchArgs.IsUnsignedDataType) { Value = binReader.ReadByte(); } else { Value = binReader.ReadSByte(); }
262 break;
263 case SearchDataTypes._16bits:
264 if (SearchArgs.IsUnsignedDataType) { Value = binReader.ReadUInt16(); } else { Value = binReader.ReadInt16(); }
265 break;
266 case SearchDataTypes._32bits:
267 if (SearchArgs.IsUnsignedDataType) { Value = binReader.ReadUInt32(); } else { Value = binReader.ReadInt32(); }
268 break;
269 case SearchDataTypes._64bits:
270 if (SearchArgs.IsUnsignedDataType) { Value = binReader.ReadUInt64(); } else { Value = binReader.ReadInt64(); }
271 break;
272 }
273 }
274 }
275 }
276
277 return second_tmp_Results;
278 }
279 }
280 }

  ViewVC Help
Powered by ViewVC 1.1.22