/[RomCheater]/trunk/RomCheater/Docking/UI/UIMemoryViewer.cs
ViewVC logotype

Contents of /trunk/RomCheater/Docking/UI/UIMemoryViewer.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 238 - (show annotations) (download)
Sun Jun 3 12:48:38 2012 UTC (8 years, 4 months ago) by william
File size: 22442 byte(s)
+ change inteface implementation
+ implement GenericMemoryProvider

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using Be.Windows.Forms;
10 using RomCheater.Logging;
11 using RomCheater.PluginFramework.Interfaces;
12 using System.Diagnostics;
13 using Sojaner.MemoryScanner.MemoryProviers;
14
15 namespace RomCheater.Docking.UI
16 {
17 public partial class UIMemoryViewer : UserControl, IAcceptsProcess<Process>, IAcceptsPlugin<IConfigPlugin>//, IAcceptsMemoryRange
18 {
19 public UIMemoryViewer()
20 {
21 InitializeComponent();
22 SetStyle(ControlStyles.UserPaint, true);
23 SetStyle(ControlStyles.DoubleBuffer, true);
24 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
25 SetStyle(ControlStyles.ResizeRedraw, true);
26 //this.OnPCSX2ProcessCallback = new UIEvents.PCSX2ProcessCallback(this.PCSX2ProcessCallback);
27 //if (this.OnPCSX2ProcessCallback != null) { this.OnPCSX2ProcessCallback.Invoke(out procdata); }
28 this.UpdateEnabled = false;
29 txtData.BytesPerLine = (int)max_address_width;
30 txtData.UseFixedBytesPerLine = true;
31 txtData.StringViewVisible = true;
32 ramScroll.Minimum = (int)MemoryStart;
33 for (uint i = MemoryStart; i < (MemoryStart + max_ram_view); i += max_address_width) { ramScroll.Maximum += (int)max_address_width; }
34 ramScroll.Value = ramScroll.Minimum;
35 this.CanChangeUpdateInterval = false;
36
37
38 lblAddressMarker.Text = "";
39 for (uint i = 0; i < max_address_width; i++)
40 {
41 lblAddressMarker.Text = lblAddressMarker.Text + string.Format("{0:X2} ", i);
42 }
43 this.AcceptedPlugin = null; this.AcceptedProcess = null;
44
45 txtAddresses.MouseWheel += new MouseEventHandler(txtAddresses_MouseWheel);
46 txtData.MouseWheel += new MouseEventHandler(txtData_MouseWheel);
47 }
48
49 private void GetFirstNonZeroByte()
50 {
51 if (!DesignMode)
52 {
53 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
54 uint addr = 0;
55 provider.ReadFirstNonZeroByte(MemoryStart, MemorySize, out addr);
56 GotoAddress(addr);
57 }
58 }
59
60 #region IAcceptsProcess<Process> Members
61 private Process _AcceptedProcess;
62 public Process AcceptedProcess
63 {
64 get { return _AcceptedProcess; }
65 set
66 {
67 _AcceptedProcess = value;
68 update_timer.Enabled = (value != null);
69 UpdateEnabled = update_timer.Enabled;
70 if (value != null)
71 GetFirstNonZeroByte();
72 }
73 }
74 #endregion
75 #region IAcceptsPlugin<IConfigPlugin> Members
76 public IConfigPlugin AcceptedPlugin { get; set; }
77 #endregion
78 #region IAcceptsMemoryRange members
79 public uint MemoryStart { get { return 0; } }
80 public uint MemorySize { get { return int.MaxValue; } }
81 #endregion
82 public void GotoTop() { this.CURRENT_TOP_ADDR = 0; }
83 public void GotoBottom() { uint size = MemorySize; this.CURRENT_TOP_ADDR = (uint)((size - 1) - max_ram_view); }
84 public void GotoAddress(uint addr) { this.CURRENT_TOP_ADDR = (uint)addr & 0xFFFFFFF0; }
85 private bool _UpdateEnabled;
86 public bool UpdateEnabled
87 {
88 get { return _UpdateEnabled; }
89 set
90 {
91 _UpdateEnabled = value;
92 if (value) { this.update_timer.Enabled = true; }
93 else { this.update_timer.Enabled = false; }
94 }
95 }
96 private bool _CanChangeUpdateInterval;
97 public bool CanChangeUpdateInterval
98 {
99 get { return _CanChangeUpdateInterval; }
100 set { _CanChangeUpdateInterval = value; }
101 }
102
103 public int UpdateInterval
104 {
105 get { return this.update_timer.Interval; }
106 set { if (CanChangeUpdateInterval) this.update_timer.Interval = value; }
107 }
108 private string AddressList = "";
109 private string AsciiData = "";
110 private byte[] RamData = new byte[] { };
111
112 const uint max_address_width = 16;
113 static uint max_ram_view = max_address_width * 27;
114
115 static uint small_scroll_change = max_address_width * 1; // scrolls one line (when you clikc the up or down arrows)
116 static uint medium_scroll_change = max_ram_view / 2; // scrolls half a page
117 static uint large_scroll_change = max_ram_view; // scrolls a full page
118 private uint _CURRENT_TOP_ADDR;
119 uint CURRENT_TOP_ADDR
120 {
121 get { return _CURRENT_TOP_ADDR; }
122 set { txthexGoto.Value = _CURRENT_TOP_ADDR = value; }
123 }
124 //uint CURRENT_BOITTOM_ADDR() { return CURRENT_TOP_ADDR + max_ram_view; }
125 private void UpdateMaxRamView()
126 {
127 Graphics g = this.CreateGraphics();
128 Size size = g.MeasureString("00", txtData.Font).ToSize();
129 int ByteHeight = size.Height;
130 int TotalHeight = txtData.Height;
131 uint NumberOfBytes = (uint)((TotalHeight / ByteHeight) * max_address_width);
132 uint byte_width = (max_address_width * 2);
133 max_ram_view = NumberOfBytes + (byte_width - 1);
134 }
135 private void btnGotoAddress_Click(object sender, EventArgs e)
136 {
137 this.GotoAddress(txthexGoto.ToUInt32());
138 }
139
140 private void btnEditBytes_Click(object sender, EventArgs e)
141 {
142 bool reenable = false;
143 if (this.UpdateEnabled) reenable = true;
144 this.UpdateEnabled = false;
145 ByteEditor editor = new ByteEditor((txtData.ByteProvider as DynamicByteProvider).Bytes.ToArray(), this.CURRENT_TOP_ADDR);
146 editor.ShowDialog();
147 if (editor.BytesEdited)
148 {
149 //DynamicByteProvider _DynamicByteProvider = new DynamicByteProvider(editor.AsBytes);
150 //txtData.ByteProvider = _DynamicByteProvider;
151 //_DynamicByteProvider.Changed += new EventHandler(HexResourceViewerBytes_Changed);
152 this.WriteCurrentBytes(this.CURRENT_TOP_ADDR, editor.AsBytes);
153 }
154 this.UpdateEnabled = reenable;
155 }
156 private bool ForceUpdate = false;
157 private bool ShouldUpdateResults()
158 {
159 //if (ResultsUpdateWorkerThread.IsBusy) { return false; }
160 if (ForceUpdate) return true;
161 //else if (TextIsBeingSelected) return false;
162 //else if (NonHandledKeysAreBeingPressed) return false;
163 else if (this.UpdateEnabled) return true;
164 else { return false; }
165 }
166 private void UpdateGui(bool force)
167 {
168 if (AcceptedProcess == null) { return; }
169 if (AcceptedPlugin == null) { return; }
170
171 if (!this.ShouldUpdateResults()) { return; }// this.Logger.LogDebugMessage(string.Format("ShouldUpdateResults() -> returning false")); return; }
172 this.UpdateMaxRamView();
173 //if (!force)
174 //{
175 if (!ResultsUpdateWorkerThread.IsBusy)
176 ResultsUpdateWorkerThread.RunWorkerAsync();
177 //}
178 //else
179 //{
180 // while (!ResultsUpdateWorkerThread.IsBusy) { ResultsUpdateWorkerThread.CancelAsync(); Application.DoEvents(); }
181 // ResultsUpdateWorkerThread.RunWorkerAsync();
182 //}
183 }
184 private void UpdateGui()
185 {
186 this.UpdateGui(false);
187 }
188 private byte[] GetMemory()
189 {
190 try
191 {
192 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
193 int bytesReadSize;
194 byte[] data;
195 provider.ReadProcessMemory(CURRENT_TOP_ADDR, max_ram_view, out bytesReadSize, out data);
196 //this.Logger.LogDebugMessage(string.Format("GetMemory() -> Memory Size: {0}0x{2:X8}{1}", "{", "}", data.Length));
197 return data;
198 }
199 catch (Exception ex)
200 {
201 logger.Error.WriteLine("{0}.GetMemory():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString());
202 byte[] data = new byte[max_ram_view];
203 for (int i = 0; i < data.Length; i++) { data[i] = 0x0; }
204 return data;
205 }
206 }
207 private void UpdateMemroyView() { this.UpdateMemroyView(this.CURRENT_TOP_ADDR); }
208 private void UpdateMemroyView(uint address)
209 {
210 try
211 {
212 if (AcceptedProcess == null) { return; }
213 if (AcceptedPlugin == null) { return; }
214
215 byte[] data = GetMemory();
216 try
217 {
218 RamData = data;
219
220
221 AddressList = "";
222 AsciiData = "";
223 // write the addreses out
224 for (uint i = address; i < (address + max_ram_view); i += max_address_width)
225 {
226 AddressList = AddressList + string.Format("{0:X8}:\n", i);
227 }
228 //// write out the ascii data
229 StringBuilder builder = new StringBuilder();
230 for (uint i = address; i < (address + max_ram_view); i += max_address_width)
231 {
232 try
233 {
234 for (uint j = 0; j < max_address_width; j++)
235 {
236 uint current_addr = i + j;
237 if (current_addr >= MemorySize) break;
238 byte ascii_value_raw = data[j];
239 char ascii_value = (char)data[j];
240 if (ascii_value_raw >= 0x20 && ascii_value_raw <= 0x7e)
241 {
242 builder.Append(ascii_value.ToString());
243 }
244 else
245 {
246 builder.Append(".");
247 }
248 }
249 builder.AppendLine();
250 }
251 catch (Exception ex)
252 {
253 logger.Error.WriteLine("{0}.UpdateMemroyView().BuildingAsciiString:{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString());
254 return;
255 }
256 }
257 AsciiData = builder.ToString();
258
259
260 }
261 catch (Exception ex)
262 {
263 logger.Error.WriteLine("{0}.UpdateMemroyView():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString());
264 return;
265 }
266 }
267 catch (Exception ex) { logger.Error.WriteLine("{0}.UpdateMemroyView():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString()); }
268 }
269 //private void HexResourceViewerBytes_Changed(object sender, System.EventArgs e)
270 //{
271 // this.WriteCurrentBytes();
272 //}
273 private void WriteCurrentBytes(uint start_address, byte[] data)
274 {
275 try
276 {
277 if (AcceptedProcess == null) { return; }
278 if (AcceptedPlugin == null) { return; }
279 // Byte changed
280 //byte[] data = (txtData.ByteProvider as DynamicByteProvider).Bytes.ToArray();
281 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
282 int bytesReadSize;
283
284 for (int i = 0; i < data.Length; i ++)
285 {
286 uint addr = (uint)(start_address + i);
287 byte data_to_write = data[i];
288 provider.WriteProcessMemory(addr, data_to_write, out bytesReadSize);
289 }
290 }
291 catch (Exception ex) { logger.Error.WriteLine("{0}.WriteCurrentBytes():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString()); }
292 }
293
294 private void update_timer_Tick(object sender, EventArgs e) { this.UpdateGui(); }
295 private void ResultsUpdateWorkerThread_DoWork(object sender, DoWorkEventArgs e) { try { this.UpdateMemroyView(); } catch (Exception ex) { logger.Error.WriteLine("{0}.ResultsUpdateWorkerThread_DoWork():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString()); } }
296 private void ResultsUpdateWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
297 {
298 try
299 {
300 txtAddresses.Clear(); txtAddresses.Text = AddressList;
301 //txtAscii.Clear(); txtAscii.Text = AsciiData;
302 //this.Logger.LogDebugMessage(string.Format("RunWorkerCompeleted() -> Memory Size: {0}0x{2:X8}{1}", "{", "}", RamData.Length));
303 DynamicByteProvider _DynamicByteProvider = new DynamicByteProvider(RamData);
304 txtData.ByteProvider = _DynamicByteProvider;
305 //_DynamicByteProvider.Changed += new EventHandler(HexResourceViewerBytes_Changed);
306 }
307 catch (ObjectDisposedException) { } // ignore errors aobut disposed objects (usually only happens when the parent closes)
308 catch (Exception ex) { logger.Error.WriteLine("{0}.ResultsUpdateWorkerThread_RunWorkerCompleted():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString()); }
309 }
310 private void Handle_KeyDown(object sender, KeyEventArgs e)
311 {
312 //isScrolling = false;
313 //bool reenable = false;
314 //if (this.UpdateEnabled) reenable = true;
315 //this.UpdateEnabled = false;
316
317 this.UpdateMaxRamView();
318 uint ORIGINAL_ADDR = this.CURRENT_TOP_ADDR;
319
320 ////if (e.Type == ScrollEventType.EndScroll) return;
321 //uint size = max_ram_view;
322
323 bool haveModifier = false;
324 switch (e.Modifiers)
325 {
326 case Keys.Control:
327 switch (e.KeyCode)
328 {
329 case Keys.Home:
330 this.CURRENT_TOP_ADDR = 0; //NonHandledKeysAreBeingPressed = false;
331 break;
332 case Keys.End:
333 this.CURRENT_TOP_ADDR = (uint)((MemorySize - 1) - max_ram_view); //NonHandledKeysAreBeingPressed = false;
334 break;
335 default:
336 //NonHandledKeysAreBeingPressed = true;
337 break;
338 }
339 break;
340 }
341
342 if (!haveModifier)
343 {
344 switch (e.KeyCode)
345 {
346 case Keys.Up:
347 if (this.CURRENT_TOP_ADDR == 0 && (this.CURRENT_TOP_ADDR - small_scroll_change > this.CURRENT_TOP_ADDR))
348 {
349 this.CURRENT_TOP_ADDR = ORIGINAL_ADDR;
350 }
351 else
352 {
353 this.CURRENT_TOP_ADDR -= (uint)small_scroll_change; //NonHandledKeysAreBeingPressed = false;
354 }
355 break;
356 case Keys.Down:
357 this.CURRENT_TOP_ADDR += (uint)small_scroll_change; //NonHandledKeysAreBeingPressed = false;
358 break;
359 case Keys.PageUp:
360 if (this.CURRENT_TOP_ADDR == 0 && (this.CURRENT_TOP_ADDR - large_scroll_change > this.CURRENT_TOP_ADDR))
361 {
362 this.CURRENT_TOP_ADDR = ORIGINAL_ADDR;
363 }
364 else
365 {
366 this.CURRENT_TOP_ADDR -= (uint)(large_scroll_change); //NonHandledKeysAreBeingPressed = false;
367 }
368 break;
369 case Keys.PageDown:
370 this.CURRENT_TOP_ADDR += (uint)(large_scroll_change); //NonHandledKeysAreBeingPressed = false;
371 break;
372 default:
373 //NonHandledKeysAreBeingPressed = true;
374 break;
375 }
376 }
377 if (this.CURRENT_TOP_ADDR < MemoryStart) this.CURRENT_TOP_ADDR = MemoryStart;
378 //if (this.CURRENT_TOP_ADDR >= VTLB_VADDR_SIZE) this.CURRENT_TOP_ADDR = (size - 1) - max_ram_view;
379 if (this.CURRENT_TOP_ADDR + max_ram_view >= MemorySize) this.CURRENT_TOP_ADDR = (MemorySize - max_ram_view);
380
381 //this.UpdateEnabled = reenable;
382 }
383
384 private void UIMemoryViewer_KeyDown(object sender, KeyEventArgs e) { this.Handle_KeyDown(sender, e); }
385 private void txtAddresses_KeyDown(object sender, KeyEventArgs e) { this.Handle_KeyDown(sender, e); }
386 private void txtData_KeyDown(object sender, KeyEventArgs e) { this.Handle_KeyDown(sender, e); }
387
388 private void ramScroll_Scroll(object sender, ScrollEventArgs e) { this.Handle_Scroll(sender, e); }
389
390 private ScrollEventArgs GetMouseWheelScrollChange(int WheelDelta)
391 {
392 ScrollEventArgs args = new ScrollEventArgs(ScrollEventType.SmallIncrement,1);
393 if (WheelDelta < 0)
394 {
395 //// negative: scroll down
396 //// SmallDecrement -or- LargeDecrement
397 //if (WheelDelta <= small_scroll_change)
398 //{
399 // args = new ScrollEventArgs(ScrollEventType.SmallDecrement,(int)small_scroll_change);
400 //}
401 //if (WheelDelta > small_scroll_change && WheelDelta <= large_scroll_change)
402 //{
403 // args = new ScrollEventArgs(ScrollEventType.LargeDecrement, (int)large_scroll_change);
404 //}
405 args = new ScrollEventArgs(ScrollEventType.SmallIncrement, 1);
406 }
407 else
408 {
409 //// positive: scroll up
410 //// SmallIncrement -or- LargeIncrement
411 //if (WheelDelta <= small_scroll_change)
412 //{
413 // args = new ScrollEventArgs(ScrollEventType.SmallIncrement, (int)small_scroll_change);
414 //}
415 //if (WheelDelta > small_scroll_change && WheelDelta <= large_scroll_change)
416 //{
417 // args = new ScrollEventArgs(ScrollEventType.LargeIncrement, (int)large_scroll_change);
418 //}
419 args = new ScrollEventArgs(ScrollEventType.SmallDecrement, 1);
420 }
421 return args;
422 }
423
424 void txtAddresses_MouseWheel(object sender, MouseEventArgs e) { this.Handle_Scroll(sender, GetMouseWheelScrollChange(e.Delta)); }
425 void txtData_MouseWheel(object sender, MouseEventArgs e) { this.Handle_Scroll(sender, GetMouseWheelScrollChange(e.Delta)); }
426
427
428 private void Handle_Scroll(object sender, ScrollEventArgs e)
429 {
430 //isScrolling = true;
431 //bool reenable = false;
432 //if (this.UpdateEnabled) reenable = true;
433 //this.UpdateEnabled = false;
434
435 this.UpdateMaxRamView();
436 uint ORIGINAL_ADDR = this.CURRENT_TOP_ADDR;
437 //uint size = MemorySize;
438 if (e.Type == ScrollEventType.EndScroll) return;
439
440 switch (e.Type)
441 {
442 case ScrollEventType.SmallDecrement:
443 if (this.CURRENT_TOP_ADDR == 0 && ((this.CURRENT_TOP_ADDR - small_scroll_change) > this.CURRENT_TOP_ADDR))
444 {
445 this.CURRENT_TOP_ADDR = ORIGINAL_ADDR;
446 }
447 else
448 {
449 this.CURRENT_TOP_ADDR -= (small_scroll_change);
450 }
451 break;
452 case ScrollEventType.SmallIncrement:
453 this.CURRENT_TOP_ADDR += (small_scroll_change);
454 break;
455
456 case ScrollEventType.LargeDecrement:
457 if (this.CURRENT_TOP_ADDR == 0 && ((this.CURRENT_TOP_ADDR - large_scroll_change) > this.CURRENT_TOP_ADDR))
458 {
459 this.CURRENT_TOP_ADDR = ORIGINAL_ADDR;
460 }
461 else
462 {
463 this.CURRENT_TOP_ADDR -= (large_scroll_change);
464 }
465 break;
466 case ScrollEventType.LargeIncrement:
467 this.CURRENT_TOP_ADDR += (large_scroll_change);
468 break;
469 case ScrollEventType.ThumbPosition:
470 //this.CURRENT_TOP_ADDR = (uint)e.NewValue;
471 //break;
472 default:
473 this.CURRENT_TOP_ADDR = (uint)((uint)e.NewValue & 0xFFFFFFF0);
474 break;
475 }
476 if (this.CURRENT_TOP_ADDR < 0) this.CURRENT_TOP_ADDR = 0;
477 //if (this.CURRENT_TOP_ADDR >= VTLB_VADDR_SIZE) this.CURRENT_TOP_ADDR = VTLB_VADDR_SIZE - max_ram_view;
478 //if (this.CURRENT_TOP_ADDR < 0 || this.CURRENT_TOP_ADDR >= VTLB_VADDR_SIZE) this.CURRENT_TOP_ADDR = ORIGINAL_ADDR;
479 if (this.CURRENT_TOP_ADDR + max_ram_view >= MemorySize) this.CURRENT_TOP_ADDR = MemorySize - max_ram_view;
480 //this.UpdateEnabled = reenable;
481 //isScrolling = false;
482 }
483
484 }
485 }

  ViewVC Help
Powered by ViewVC 1.1.22