ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater/Docking/UI/UIMemoryViewer.cs
Revision: 201
Committed: Thu May 31 08:10:25 2012 UTC (11 years, 6 months ago) by william
File size: 18653 byte(s)
Log Message:
+ get memoryview working

File Contents

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