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

  ViewVC Help
Powered by ViewVC 1.1.22