/[RomCheater]/trunk/RomCheater/Docking/UI/UIMemoryViewer.cs
ViewVC logotype

Annotation of /trunk/RomCheater/Docking/UI/UIMemoryViewer.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 198 - (hide annotations) (download)
Thu May 31 07:13:43 2012 UTC (8 years, 4 months ago) by william
File size: 18243 byte(s)
+ initial support for memoryview and getting ram from process for display

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

  ViewVC Help
Powered by ViewVC 1.1.22