/[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 243 - (show annotations) (download)
Sun Jun 3 13:21:51 2012 UTC (8 years, 5 months ago) by william
File size: 22473 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22