--- trunk/RomCheater/Docking/UI/UIMemoryViewer.cs 2012/05/31 07:20:43 199 +++ trunk/RomCheater/Docking/UI/UIMemoryViewer.cs 2012/06/03 16:09:05 249 @@ -1,4 +1,6 @@ -using System; +#define DISABLE_GETFIRSTNONZEROBYTE_ONUPDATE_ACCEPTEDPROCESS // when defined will not call GetFirstNonZeroByte() when AcceptedProcess is updated and is not null +#define DISABLE_GETFIRSTNONZERO_BYTE // when defined will make GetFirstNonZeroByte() an empty void method +using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; @@ -10,10 +12,14 @@ using RomCheater.Logging; using RomCheater.PluginFramework.Interfaces; using System.Diagnostics; +using Sojaner.MemoryScanner.MemoryProviers; namespace RomCheater.Docking.UI { - public partial class UIMemoryViewer : UserControl, IProcessConfig, IAcceptsPlugin, IAcceptsMemoryRange + public partial class UIMemoryViewer : UserControl, + IAcceptsPlugin, + IAcceptsProcess, + IAcceptsProcessAndConfig { public UIMemoryViewer() { @@ -29,7 +35,7 @@ txtData.UseFixedBytesPerLine = true; txtData.StringViewVisible = true; ramScroll.Minimum = (int)MemoryStart; - for (uint i = MemoryStart; i < (MemoryStart + MemorySize); i += max_address_width) { ramScroll.Maximum += (int)max_address_width; } + for (int i = MemoryStart; i < (MemoryStart + max_ram_view); i += max_address_width) { ramScroll.Maximum += (int)max_address_width; } ramScroll.Value = ramScroll.Minimum; this.CanChangeUpdateInterval = false; @@ -40,8 +46,27 @@ lblAddressMarker.Text = lblAddressMarker.Text + string.Format("{0:X2} ", i); } this.AcceptedPlugin = null; this.AcceptedProcess = null; + + txtAddresses.MouseWheel += new MouseEventHandler(txtAddresses_MouseWheel); + txtData.MouseWheel += new MouseEventHandler(txtData_MouseWheel); } - #region IProcessConfig Members + + private void GetFirstNonZeroByte() + { +#if !DISABLE_GETFIRSTNONZERO_BYTE + if (!DesignMode) + { + GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this); + provider.OpenProvider(); + uint addr = 0; + provider.ReadFirstNonZeroByte(MemoryStart, MemorySize, out addr); + provider.CloseProvider(); + GotoAddress(addr); + } +#endif + } + + #region IAcceptsProcess Members private Process _AcceptedProcess; public Process AcceptedProcess { @@ -51,15 +76,23 @@ _AcceptedProcess = value; update_timer.Enabled = (value != null); UpdateEnabled = update_timer.Enabled; +#if !DISABLE_GETFIRSTNONZEROBYTE_ONUPDATE_ACCEPTEDPROCESS + if (value != null) + GetFirstNonZeroByte(); +#endif } } #endregion #region IAcceptsPlugin Members public IConfigPlugin AcceptedPlugin { get; set; } #endregion + #region IAcceptsMemoryRange members + public int MemoryStart { get { return 0; } } + public int MemorySize { get { return int.MaxValue; } } + #endregion public void GotoTop() { this.CURRENT_TOP_ADDR = 0; } - public void GotoBottom() { uint size = MemorySize; this.CURRENT_TOP_ADDR = (uint)((size - 1) - max_ram_view); } - public void GotoAddress(uint addr) { this.CURRENT_TOP_ADDR = (uint)addr & 0xFFFFFFF0; } + public void GotoBottom() { uint size = (uint)MemorySize; this.CURRENT_TOP_ADDR = (int)((size - 1) - max_ram_view); } + public void GotoAddress(uint addr) { this.CURRENT_TOP_ADDR = (int)(addr & 0xFFFFFFF0); } private bool _UpdateEnabled; public bool UpdateEnabled { @@ -87,22 +120,17 @@ private string AsciiData = ""; private byte[] RamData = new byte[] { }; - const uint max_address_width = 16; - static uint max_ram_view = max_address_width * 27; + const int max_address_width = 16; + static int max_ram_view = max_address_width * 27; - static uint small_scroll_change = max_address_width * 1; // scrolls one line (when you clikc the up or down arrows) - static uint medium_scroll_change = max_ram_view / 2; // scrolls half a page - static uint large_scroll_change = max_ram_view; // scrolls a full page - - #region IAcceptsMemoryRange members - public uint MemoryStart { get; set; } - public uint MemorySize { get; set; } - #endregion - private uint _CURRENT_TOP_ADDR; - uint CURRENT_TOP_ADDR + static int small_scroll_change = max_address_width * 1; // scrolls one line (when you clikc the up or down arrows) + static int medium_scroll_change = max_ram_view / 2; // scrolls half a page + static int large_scroll_change = max_ram_view; // scrolls a full page + private int _CURRENT_TOP_ADDR; + int CURRENT_TOP_ADDR { get { return _CURRENT_TOP_ADDR; } - set { _CURRENT_TOP_ADDR = value; } + set { txthexGoto.Value = _CURRENT_TOP_ADDR = value; } } //uint CURRENT_BOITTOM_ADDR() { return CURRENT_TOP_ADDR + max_ram_view; } private void UpdateMaxRamView() @@ -111,8 +139,8 @@ Size size = g.MeasureString("00", txtData.Font).ToSize(); int ByteHeight = size.Height; int TotalHeight = txtData.Height; - uint NumberOfBytes = (uint)((TotalHeight / ByteHeight) * max_address_width); - uint byte_width = (max_address_width * 2); + int NumberOfBytes = (int)((TotalHeight / ByteHeight) * max_address_width); + int byte_width = (max_address_width * 2); max_ram_view = NumberOfBytes + (byte_width - 1); } private void btnGotoAddress_Click(object sender, EventArgs e) @@ -129,10 +157,10 @@ editor.ShowDialog(); if (editor.BytesEdited) { - DynamicByteProvider _DynamicByteProvider = new DynamicByteProvider(editor.AsBytes); - txtData.ByteProvider = _DynamicByteProvider; - _DynamicByteProvider.Changed += new EventHandler(HexResourceViewerBytes_Changed); - this.WriteCurrentBytes(); + //DynamicByteProvider _DynamicByteProvider = new DynamicByteProvider(editor.AsBytes); + //txtData.ByteProvider = _DynamicByteProvider; + //_DynamicByteProvider.Changed += new EventHandler(HexResourceViewerBytes_Changed); + this.WriteCurrentBytes(this.CURRENT_TOP_ADDR, editor.AsBytes); } this.UpdateEnabled = reenable; } @@ -170,26 +198,32 @@ } private byte[] GetMemory() { + byte[] data = new byte[] { }; try { - Sojaner.MemoryScanner.ProcessMemoryReader reader = new Sojaner.MemoryScanner.ProcessMemoryReader(); - reader.ReadProcess = this.AcceptedProcess; - reader.OpenProcess(); + GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this); + provider.OpenProvider(); int bytesReadSize; - byte[] data = reader.ReadProcessMemory(MemoryStart, MemorySize, out bytesReadSize); - //this.Logger.LogDebugMessage(string.Format("GetMemory() -> Memory Size: {0}0x{2:X8}{1}", "{", "}", data.Length)); - return data; + provider.ReadProcessMemory(CURRENT_TOP_ADDR, (uint)max_ram_view, out bytesReadSize, out data); + provider.CloseProvider(); + } catch (Exception ex) { logger.Error.WriteLine("{0}.GetMemory():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString()); - byte[] data = new byte[MemorySize]; - for (int i = 0; i < data.Length; i++) { data[i] = 0x0; } - return data; } + finally + { + if (data.Length == 0) + { + data = new byte[max_ram_view]; + for (int i = 0; i < data.Length; i++) { data[i] = 0x0; } + } + } + return data; } private void UpdateMemroyView() { this.UpdateMemroyView(this.CURRENT_TOP_ADDR); } - private void UpdateMemroyView(uint address) + private void UpdateMemroyView(int address) { try { @@ -197,46 +231,48 @@ if (AcceptedPlugin == null) { return; } byte[] data = GetMemory(); - - List ByteData = new List((int)(address + (max_ram_view - 0))); - try { - AddressList = ""; - AsciiData = ""; + RamData = data; - // create a byte array holding only the bytes that we need - for (uint i = address; i < (address + (max_ram_view + 1)); i += 1) { ByteData.Add(data[i]); } + AddressList = ""; + AsciiData = ""; // write the addreses out - for (uint i = address; i < (address + max_ram_view); i += max_address_width) + for (int i = address; i < (address + max_ram_view); i += max_address_width) { AddressList = AddressList + string.Format("{0:X8}:\n", i); } - // write out the ascii data + //// write out the ascii data StringBuilder builder = new StringBuilder(); - for (uint i = address; i < (address + max_ram_view); i += max_address_width) + for (int i = address; i < (address + max_ram_view); i += max_address_width) { - for (uint j = 0; j < max_address_width; j++) + try { - uint current_addr = i + j; - if (current_addr >= MemorySize) break; - byte ascii_value_raw = data[current_addr]; - char ascii_value = (char)data[current_addr]; - if (ascii_value_raw >= 0x20 && ascii_value_raw <= 0x7e) + for (int j = 0; j < max_address_width; j++) { - builder.Append(ascii_value.ToString()); - } - else - { - builder.Append("."); + int current_addr = i + j; + if (current_addr >= MemorySize) break; + byte ascii_value_raw = data[j]; + char ascii_value = (char)data[j]; + if (ascii_value_raw >= 0x20 && ascii_value_raw <= 0x7e) + { + builder.Append(ascii_value.ToString()); + } + else + { + builder.Append("."); + } } + builder.AppendLine(); + } + catch (Exception ex) + { + logger.Error.WriteLine("{0}.UpdateMemroyView().BuildingAsciiString:{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString()); + return; } - builder.AppendLine(); } AsciiData = builder.ToString(); - // write out the bytes - RamData = ByteData.ToArray(); } @@ -248,31 +284,29 @@ } catch (Exception ex) { logger.Error.WriteLine("{0}.UpdateMemroyView():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString()); } } - private void HexResourceViewerBytes_Changed(object sender, System.EventArgs e) - { - this.WriteCurrentBytes(); - } - private void WriteCurrentBytes() + //private void HexResourceViewerBytes_Changed(object sender, System.EventArgs e) + //{ + // this.WriteCurrentBytes(); + //} + private void WriteCurrentBytes(int start_address, byte[] data) { try { if (AcceptedProcess == null) { return; } if (AcceptedPlugin == null) { return; } // Byte changed - byte[] data = (txtData.ByteProvider as DynamicByteProvider).Bytes.ToArray(); - - - Sojaner.MemoryScanner.ProcessMemoryReader reader = new Sojaner.MemoryScanner.ProcessMemoryReader(); - reader.ReadProcess = this.AcceptedProcess; - reader.OpenProcess(); + //byte[] data = (txtData.ByteProvider as DynamicByteProvider).Bytes.ToArray(); + GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this); + provider.OpenProvider(); int bytesReadSize; - for (int i = 0; i < data.Length; i += sizeof(uint)) + for (int i = 0; i < data.Length; i ++) { - uint addr = (uint)(this.CURRENT_TOP_ADDR + i); - uint data_to_write = BitConverter.ToUInt32(data, i); - reader.WriteProcessMemory((UIntPtr)addr, data, out bytesReadSize); + int addr = (int)(start_address + i); + byte data_to_write = data[i]; + provider.WriteProcessMemory(addr, data_to_write, out bytesReadSize); } + provider.CloseProvider(); } catch (Exception ex) { logger.Error.WriteLine("{0}.WriteCurrentBytes():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString()); } } @@ -288,7 +322,7 @@ //this.Logger.LogDebugMessage(string.Format("RunWorkerCompeleted() -> Memory Size: {0}0x{2:X8}{1}", "{", "}", RamData.Length)); DynamicByteProvider _DynamicByteProvider = new DynamicByteProvider(RamData); txtData.ByteProvider = _DynamicByteProvider; - _DynamicByteProvider.Changed += new EventHandler(HexResourceViewerBytes_Changed); + //_DynamicByteProvider.Changed += new EventHandler(HexResourceViewerBytes_Changed); } catch (ObjectDisposedException) { } // ignore errors aobut disposed objects (usually only happens when the parent closes) catch (Exception ex) { logger.Error.WriteLine("{0}.ResultsUpdateWorkerThread_RunWorkerCompleted():{1}{2}", this.GetType().Name, System.Environment.NewLine, ex.ToString()); } @@ -301,10 +335,10 @@ //this.UpdateEnabled = false; this.UpdateMaxRamView(); - uint ORIGINAL_ADDR = this.CURRENT_TOP_ADDR; + int ORIGINAL_ADDR = this.CURRENT_TOP_ADDR; ////if (e.Type == ScrollEventType.EndScroll) return; - uint size = MemorySize; + //uint size = max_ram_view; bool haveModifier = false; switch (e.Modifiers) @@ -316,7 +350,7 @@ this.CURRENT_TOP_ADDR = 0; //NonHandledKeysAreBeingPressed = false; break; case Keys.End: - this.CURRENT_TOP_ADDR = (uint)((size - 1) - max_ram_view); //NonHandledKeysAreBeingPressed = false; + this.CURRENT_TOP_ADDR = (int)((MemorySize - 1) - max_ram_view); //NonHandledKeysAreBeingPressed = false; break; default: //NonHandledKeysAreBeingPressed = true; @@ -330,16 +364,30 @@ switch (e.KeyCode) { case Keys.Up: - this.CURRENT_TOP_ADDR -= (uint)small_scroll_change; //NonHandledKeysAreBeingPressed = false; + if (this.CURRENT_TOP_ADDR == 0 && (this.CURRENT_TOP_ADDR - small_scroll_change > this.CURRENT_TOP_ADDR)) + { + this.CURRENT_TOP_ADDR = ORIGINAL_ADDR; + } + else + { + this.CURRENT_TOP_ADDR -= (int)small_scroll_change; //NonHandledKeysAreBeingPressed = false; + } break; case Keys.Down: - this.CURRENT_TOP_ADDR += (uint)small_scroll_change; //NonHandledKeysAreBeingPressed = false; + this.CURRENT_TOP_ADDR += (int)small_scroll_change; //NonHandledKeysAreBeingPressed = false; break; case Keys.PageUp: - this.CURRENT_TOP_ADDR -= (uint)(large_scroll_change); //NonHandledKeysAreBeingPressed = false; + if (this.CURRENT_TOP_ADDR == 0 && (this.CURRENT_TOP_ADDR - large_scroll_change > this.CURRENT_TOP_ADDR)) + { + this.CURRENT_TOP_ADDR = ORIGINAL_ADDR; + } + else + { + this.CURRENT_TOP_ADDR -= (int)(large_scroll_change); //NonHandledKeysAreBeingPressed = false; + } break; case Keys.PageDown: - this.CURRENT_TOP_ADDR += (uint)(large_scroll_change); //NonHandledKeysAreBeingPressed = false; + this.CURRENT_TOP_ADDR += (int)(large_scroll_change); //NonHandledKeysAreBeingPressed = false; break; default: //NonHandledKeysAreBeingPressed = true; @@ -348,8 +396,8 @@ } if (this.CURRENT_TOP_ADDR < MemoryStart) this.CURRENT_TOP_ADDR = MemoryStart; //if (this.CURRENT_TOP_ADDR >= VTLB_VADDR_SIZE) this.CURRENT_TOP_ADDR = (size - 1) - max_ram_view; - if (this.CURRENT_TOP_ADDR + max_ram_view >= MemoryStart) this.CURRENT_TOP_ADDR = (size - 1) - max_ram_view; - + if (this.CURRENT_TOP_ADDR + max_ram_view >= MemorySize) this.CURRENT_TOP_ADDR = (MemorySize - max_ram_view); + //this.UpdateEnabled = reenable; } @@ -358,6 +406,45 @@ private void txtData_KeyDown(object sender, KeyEventArgs e) { this.Handle_KeyDown(sender, e); } private void ramScroll_Scroll(object sender, ScrollEventArgs e) { this.Handle_Scroll(sender, e); } + + private ScrollEventArgs GetMouseWheelScrollChange(int WheelDelta) + { + ScrollEventArgs args = new ScrollEventArgs(ScrollEventType.SmallIncrement,1); + if (WheelDelta < 0) + { + //// negative: scroll down + //// SmallDecrement -or- LargeDecrement + //if (WheelDelta <= small_scroll_change) + //{ + // args = new ScrollEventArgs(ScrollEventType.SmallDecrement,(int)small_scroll_change); + //} + //if (WheelDelta > small_scroll_change && WheelDelta <= large_scroll_change) + //{ + // args = new ScrollEventArgs(ScrollEventType.LargeDecrement, (int)large_scroll_change); + //} + args = new ScrollEventArgs(ScrollEventType.SmallIncrement, 1); + } + else + { + //// positive: scroll up + //// SmallIncrement -or- LargeIncrement + //if (WheelDelta <= small_scroll_change) + //{ + // args = new ScrollEventArgs(ScrollEventType.SmallIncrement, (int)small_scroll_change); + //} + //if (WheelDelta > small_scroll_change && WheelDelta <= large_scroll_change) + //{ + // args = new ScrollEventArgs(ScrollEventType.LargeIncrement, (int)large_scroll_change); + //} + args = new ScrollEventArgs(ScrollEventType.SmallDecrement, 1); + } + return args; + } + + void txtAddresses_MouseWheel(object sender, MouseEventArgs e) { this.Handle_Scroll(sender, GetMouseWheelScrollChange(e.Delta)); } + void txtData_MouseWheel(object sender, MouseEventArgs e) { this.Handle_Scroll(sender, GetMouseWheelScrollChange(e.Delta)); } + + private void Handle_Scroll(object sender, ScrollEventArgs e) { //isScrolling = true; @@ -366,21 +453,35 @@ //this.UpdateEnabled = false; this.UpdateMaxRamView(); - uint ORIGINAL_ADDR = this.CURRENT_TOP_ADDR; - uint size = MemorySize; + int ORIGINAL_ADDR = this.CURRENT_TOP_ADDR; + //uint size = MemorySize; if (e.Type == ScrollEventType.EndScroll) return; switch (e.Type) { case ScrollEventType.SmallDecrement: - this.CURRENT_TOP_ADDR -= (small_scroll_change); + if (this.CURRENT_TOP_ADDR == 0 && ((this.CURRENT_TOP_ADDR - small_scroll_change) > this.CURRENT_TOP_ADDR)) + { + this.CURRENT_TOP_ADDR = ORIGINAL_ADDR; + } + else + { + this.CURRENT_TOP_ADDR -= (small_scroll_change); + } break; case ScrollEventType.SmallIncrement: this.CURRENT_TOP_ADDR += (small_scroll_change); break; case ScrollEventType.LargeDecrement: - this.CURRENT_TOP_ADDR -= (large_scroll_change); + if (this.CURRENT_TOP_ADDR == 0 && ((this.CURRENT_TOP_ADDR - large_scroll_change) > this.CURRENT_TOP_ADDR)) + { + this.CURRENT_TOP_ADDR = ORIGINAL_ADDR; + } + else + { + this.CURRENT_TOP_ADDR -= (large_scroll_change); + } break; case ScrollEventType.LargeIncrement: this.CURRENT_TOP_ADDR += (large_scroll_change); @@ -389,15 +490,16 @@ //this.CURRENT_TOP_ADDR = (uint)e.NewValue; //break; default: - this.CURRENT_TOP_ADDR = (uint)((uint)e.NewValue & 0xFFFFFFF0); + this.CURRENT_TOP_ADDR = (int)(e.NewValue & 0xFFFFFFF0); break; } if (this.CURRENT_TOP_ADDR < 0) this.CURRENT_TOP_ADDR = 0; //if (this.CURRENT_TOP_ADDR >= VTLB_VADDR_SIZE) this.CURRENT_TOP_ADDR = VTLB_VADDR_SIZE - max_ram_view; //if (this.CURRENT_TOP_ADDR < 0 || this.CURRENT_TOP_ADDR >= VTLB_VADDR_SIZE) this.CURRENT_TOP_ADDR = ORIGINAL_ADDR; - if (this.CURRENT_TOP_ADDR + max_ram_view >= MemorySize) this.CURRENT_TOP_ADDR = (size - 1) - max_ram_view; + if (this.CURRENT_TOP_ADDR + max_ram_view >= MemorySize) this.CURRENT_TOP_ADDR = MemorySize - max_ram_view; //this.UpdateEnabled = reenable; //isScrolling = false; } + } }