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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 459 - (show annotations) (download)
Mon Jun 3 02:23:04 2013 UTC (8 years, 5 months ago) by william
File size: 11709 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 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())
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 [Obsolete("GetNextResult has been replaced by GetResults")]
129 public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible
130 {
131 return new ResultType<TValue>();
132 }
133 #endregion
134
135
136 public ResultItem[] GetResultItems(SearchType SearchArgs, IAcceptsProcessAndConfig iapc, ProgressBarWithPercentageLabel progress)
137 {
138 var arr = GetResults(SearchArgs, iapc, progress);
139 ResultItem[] items = new ResultItem[arr.Count()];
140 for (int i = 0; i < items.Count(); i++)
141 {
142 var v = arr[i];
143 switch (SearchArgs.DataType)
144 {
145 case SearchDataTypes._8bits:
146 if (SearchArgs.IsUnsignedDataType) { items[i] = new ResultItem(v.Address, false, Convert.ToByte(v.Value)); }
147 else { items[i] = new ResultItem(v.Address, false, Convert.ToSByte(v.Value)); }
148 break;
149 case SearchDataTypes._16bits:
150 if (SearchArgs.IsUnsignedDataType) { items[i] = new ResultItem(v.Address, false, Convert.ToUInt16(v.Value)); }
151 else { items[i] = new ResultItem(v.Address, false, Convert.ToInt16(v.Value)); }
152 break;
153 case SearchDataTypes._32bits:
154 if (SearchArgs.IsUnsignedDataType) { items[i] = new ResultItem(v.Address, false, Convert.ToUInt32(v.Value)); }
155 else { items[i] = new ResultItem(v.Address, false, Convert.ToInt32(v.Value)); }
156 break;
157 case SearchDataTypes._64bits:
158 if (SearchArgs.IsUnsignedDataType) { items[i] = new ResultItem(v.Address, false, Convert.ToUInt64(v.Value)); }
159 else { items[i] = new ResultItem(v.Address, false, Convert.ToInt64(v.Value)); }
160 break;
161 }
162 arr[i] = null; // free memory used by the data
163 }
164 return items;
165 }
166
167
168 public uint[] GetResultAddresses(SearchType SearchArgs, IAcceptsProcessAndConfig iapc, ProgressBarWithPercentageLabel progress)
169 {
170 var arr = GetResults(SearchArgs, iapc, progress);
171 uint[] items = new uint[arr.Count()];
172 for (int i = 0; i < items.Count(); i++)
173 {
174 var v = arr[i];
175 items[i] = v.Address;
176 arr[i] = null; // free memory used by the data
177 }
178 return items;
179 }
180 public ResultType<object>[] GetResults(SearchType SearchArgs, IAcceptsProcessAndConfig iapc, ProgressBarWithPercentageLabel progress)
181 {
182 return GetResultsInternal(SearchArgs, iapc, progress);
183 }
184
185 private ResultType<object>[] GetResultsInternal(SearchType SearchArgs, IAcceptsProcessAndConfig iapc, ProgressBarWithPercentageLabel progress)
186 {
187 progress.Value = 0;
188 progress.Message = string.Empty;
189
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 (SearchArgs.DataType)
204 {
205 case SearchDataTypes._8bits:
206 if (SearchArgs.IsUnsignedDataType) { Value = binReader.ReadByte(); } else { Value = binReader.ReadSByte(); }
207 break;
208 case SearchDataTypes._16bits:
209 if (SearchArgs.IsUnsignedDataType) { Value = binReader.ReadUInt16(); } else { Value = binReader.ReadInt16(); }
210 break;
211 case SearchDataTypes._32bits:
212 if (SearchArgs.IsUnsignedDataType) { Value = binReader.ReadUInt32(); } else { Value = binReader.ReadInt32(); }
213 break;
214 case SearchDataTypes._64bits:
215 if (SearchArgs.IsUnsignedDataType) { 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 progress.Value = int_percent_done;
227 progress.Message = string.Format(" -> Loading value for Address: 0x{0:x8}", Address);
228 Last_Whole_Percent_Done = int_percent_done;
229 }
230 }
231 }
232 }
233 }
234 progress.Value = 0;
235 progress.Message = string.Empty;
236 return results;
237 }
238 }
239 }

  ViewVC Help
Powered by ViewVC 1.1.22