/[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 199 - (hide annotations) (download)
Thu May 31 07:20:43 2012 UTC (8 years, 4 months ago) by william
File size: 18542 byte(s)
+ add logic to enable update

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

  ViewVC Help
Powered by ViewVC 1.1.22