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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/RomCheater/Docking/UI/UIMemoryViewer.cs	2012/05/31 08:38:16	202
+++ trunk/RomCheater/Docking/UI/UIMemoryViewer.cs	2012/06/03 15:15:51	246
@@ -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<IConfigPlugin>//, IAcceptsMemoryRange
+    public partial class UIMemoryViewer : UserControl, 
+        IAcceptsPlugin<IConfigPlugin>,
+        IAcceptsProcess<Process>,
+        IAcceptsProcessAndConfig
     {
         public UIMemoryViewer()
         {
@@ -43,8 +49,24 @@
 
             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<Process> Members
         private Process _AcceptedProcess;
         public Process AcceptedProcess
         {
@@ -54,6 +76,10 @@
                 _AcceptedProcess = value;
                 update_timer.Enabled = (value != null);
                 UpdateEnabled = update_timer.Enabled;
+#if !DISABLE_GETFIRSTNONZEROBYTE_ONUPDATE_ACCEPTEDPROCESS
+                if (value != null)
+                    GetFirstNonZeroByte();
+#endif
             }
         }
         #endregion
@@ -61,8 +87,8 @@
         public IConfigPlugin AcceptedPlugin { get; set; }
         #endregion
         #region IAcceptsMemoryRange members
-        private uint MemoryStart { get { return 0; } }
-        private uint MemorySize { get { return int.MaxValue; } }
+        public uint MemoryStart { get { return 0; } }
+        public uint 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); }
@@ -104,7 +130,7 @@
         uint 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()
@@ -131,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;
         }
@@ -172,23 +198,29 @@
         }
         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(CURRENT_TOP_ADDR, max_ram_view, out bytesReadSize);
-                //this.Logger.LogDebugMessage(string.Format("GetMemory() -> Memory Size: {0}0x{2:X8}{1}", "{", "}", data.Length));
-                return data;
+                provider.ReadProcessMemory(CURRENT_TOP_ADDR, 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[max_ram_view];
-                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)
@@ -252,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(uint 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);
+                    uint addr = (uint)(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()); }
         }
@@ -292,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()); }
@@ -308,7 +338,7 @@
             uint ORIGINAL_ADDR = this.CURRENT_TOP_ADDR;
 
             ////if (e.Type == ScrollEventType.EndScroll) return;
-            uint size = max_ram_view;
+            //uint size = max_ram_view;
 
             bool haveModifier = false;
             switch (e.Modifiers)
@@ -320,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 = (uint)((MemorySize - 1) - max_ram_view); //NonHandledKeysAreBeingPressed = false;
                             break;
                         default:
                             //NonHandledKeysAreBeingPressed = true;
@@ -334,13 +364,27 @@
                 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 -= (uint)small_scroll_change; //NonHandledKeysAreBeingPressed = false;
+                        }
                         break;
                     case Keys.Down:
                         this.CURRENT_TOP_ADDR += (uint)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 -= (uint)(large_scroll_change); //NonHandledKeysAreBeingPressed = false;
+                        }
                         break;
                     case Keys.PageDown:
                         this.CURRENT_TOP_ADDR += (uint)(large_scroll_change); //NonHandledKeysAreBeingPressed = false;
@@ -352,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 >= MemorySize) this.CURRENT_TOP_ADDR = (size - 1);
-
+            if (this.CURRENT_TOP_ADDR + max_ram_view >= MemorySize) this.CURRENT_TOP_ADDR = (MemorySize - max_ram_view);
+            
             //this.UpdateEnabled = reenable;
         }
 
@@ -410,7 +454,7 @@
 
             this.UpdateMaxRamView();
             uint ORIGINAL_ADDR = this.CURRENT_TOP_ADDR;
-            uint size = MemorySize;
+            //uint size = MemorySize;
             if (e.Type == ScrollEventType.EndScroll) return;
 
             switch (e.Type)
@@ -452,9 +496,10 @@
             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;
         }
+
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22