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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22