/[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 245 - (show annotations) (download)
Sun Jun 3 14:50:09 2012 UTC (8 years, 4 months ago) by william
File size: 22706 byte(s)
+ add exception logic to ensure provider is open

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

  ViewVC Help
Powered by ViewVC 1.1.22