/[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 205 - (show annotations) (download)
Thu May 31 09:19:16 2012 UTC (8 years, 4 months ago) by william
File size: 22665 byte(s)
+ add/change more logic to fix keydown events

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 private bool first_load = true;
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 Sojaner.MemoryScanner.ProcessMemoryReader reader = new Sojaner.MemoryScanner.ProcessMemoryReader();
54 reader.ReadProcess = this.AcceptedProcess;
55 reader.OpenProcess();
56 uint addr = 0;
57 reader.ReadFirstNonZeroByte(MemoryStart, MemorySize, out addr);
58 GotoAddress(addr);
59 }
60 }
61
62 #region IProcessConfig Members
63 private Process _AcceptedProcess;
64 public Process AcceptedProcess
65 {
66 get { return _AcceptedProcess; }
67 set
68 {
69 _AcceptedProcess = value;
70 update_timer.Enabled = (value != null);
71 UpdateEnabled = update_timer.Enabled;
72 if (value != null)
73 GetFirstNonZeroByte();
74 }
75 }
76 #endregion
77 #region IAcceptsPlugin<IConfigPlugin> Members
78 public IConfigPlugin AcceptedPlugin { get; set; }
79 #endregion
80 #region IAcceptsMemoryRange members
81 private uint MemoryStart { get { return 0; } }
82 private uint MemorySize { get { return int.MaxValue; } }
83 #endregion
84 public void GotoTop() { this.CURRENT_TOP_ADDR = 0; }
85 public void GotoBottom() { uint size = MemorySize; this.CURRENT_TOP_ADDR = (uint)((size - 1) - max_ram_view); }
86 public void GotoAddress(uint addr) { this.CURRENT_TOP_ADDR = (uint)addr & 0xFFFFFFF0; }
87 private bool _UpdateEnabled;
88 public bool UpdateEnabled
89 {
90 get { return _UpdateEnabled; }
91 set
92 {
93 _UpdateEnabled = value;
94 if (value) { this.update_timer.Enabled = true; }
95 else { this.update_timer.Enabled = false; }
96 }
97 }
98 private bool _CanChangeUpdateInterval;
99 public bool CanChangeUpdateInterval
100 {
101 get { return _CanChangeUpdateInterval; }
102 set { _CanChangeUpdateInterval = value; }
103 }
104
105 public int UpdateInterval
106 {
107 get { return this.update_timer.Interval; }
108 set { if (CanChangeUpdateInterval) this.update_timer.Interval = value; }
109 }
110 private string AddressList = "";
111 private string AsciiData = "";
112 private byte[] RamData = new byte[] { };
113
114 const uint max_address_width = 16;
115 static uint max_ram_view = max_address_width * 27;
116
117 static uint small_scroll_change = max_address_width * 1; // scrolls one line (when you clikc the up or down arrows)
118 static uint medium_scroll_change = max_ram_view / 2; // scrolls half a page
119 static uint large_scroll_change = max_ram_view; // scrolls a full page
120 private uint _CURRENT_TOP_ADDR;
121 uint CURRENT_TOP_ADDR
122 {
123 get { return _CURRENT_TOP_ADDR; }
124 set { _CURRENT_TOP_ADDR = value; }
125 }
126 //uint CURRENT_BOITTOM_ADDR() { return CURRENT_TOP_ADDR + max_ram_view; }
127 private void UpdateMaxRamView()
128 {
129 Graphics g = this.CreateGraphics();
130 Size size = g.MeasureString("00", txtData.Font).ToSize();
131 int ByteHeight = size.Height;
132 int TotalHeight = txtData.Height;
133 uint NumberOfBytes = (uint)((TotalHeight / ByteHeight) * max_address_width);
134 uint byte_width = (max_address_width * 2);
135 max_ram_view = NumberOfBytes + (byte_width - 1);
136 }
137 private void btnGotoAddress_Click(object sender, EventArgs e)
138 {
139 this.GotoAddress(txthexGoto.ToUInt32());
140 }
141
142 private void btnEditBytes_Click(object sender, EventArgs e)
143 {
144 bool reenable = false;
145 if (this.UpdateEnabled) reenable = true;
146 this.UpdateEnabled = false;
147 ByteEditor editor = new ByteEditor((txtData.ByteProvider as DynamicByteProvider).Bytes.ToArray(), this.CURRENT_TOP_ADDR);
148 editor.ShowDialog();
149 if (editor.BytesEdited)
150 {
151 DynamicByteProvider _DynamicByteProvider = new DynamicByteProvider(editor.AsBytes);
152 txtData.ByteProvider = _DynamicByteProvider;
153 _DynamicByteProvider.Changed += new EventHandler(HexResourceViewerBytes_Changed);
154 this.WriteCurrentBytes();
155 }
156 this.UpdateEnabled = reenable;
157 }
158 private bool ForceUpdate = false;
159 private bool ShouldUpdateResults()
160 {
161 //if (ResultsUpdateWorkerThread.IsBusy) { return false; }
162 if (ForceUpdate) return true;
163 //else if (TextIsBeingSelected) return false;
164 //else if (NonHandledKeysAreBeingPressed) return false;
165 else if (this.UpdateEnabled) return true;
166 else { return false; }
167 }
168 private void UpdateGui(bool force)
169 {
170 if (AcceptedProcess == null) { return; }
171 if (AcceptedPlugin == null) { return; }
172
173 if (!this.ShouldUpdateResults()) { return; }// this.Logger.LogDebugMessage(string.Format("ShouldUpdateResults() -> returning false")); return; }
174 this.UpdateMaxRamView();
175 //if (!force)
176 //{
177 if (!ResultsUpdateWorkerThread.IsBusy)
178 ResultsUpdateWorkerThread.RunWorkerAsync();
179 //}
180 //else
181 //{
182 // while (!ResultsUpdateWorkerThread.IsBusy) { ResultsUpdateWorkerThread.CancelAsync(); Application.DoEvents(); }
183 // ResultsUpdateWorkerThread.RunWorkerAsync();
184 //}
185 }
186 private void UpdateGui()
187 {
188 this.UpdateGui(false);
189 }
190 private byte[] GetMemory()
191 {
192 try
193 {
194 Sojaner.MemoryScanner.ProcessMemoryReader reader = new Sojaner.MemoryScanner.ProcessMemoryReader();
195 reader.ReadProcess = this.AcceptedProcess;
196 reader.OpenProcess();
197 int bytesReadSize;
198 byte[] data = reader.ReadProcessMemory(CURRENT_TOP_ADDR, max_ram_view, out bytesReadSize);
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()
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 += sizeof(uint))
292 {
293 uint addr = (uint)(this.CURRENT_TOP_ADDR + i);
294 uint data_to_write = BitConverter.ToUInt32(data, i);
295 reader.WriteProcessMemory((UIntPtr)addr, data, 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 }

  ViewVC Help
Powered by ViewVC 1.1.22