/[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/06/03 13:21:51	243
+++ 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;
@@ -33,7 +35,7 @@
             txtData.UseFixedBytesPerLine = true;
             txtData.StringViewVisible = true;
             ramScroll.Minimum = (int)MemoryStart;
-            for (uint i = MemoryStart; i < (MemoryStart + max_ram_view); 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;
 
@@ -51,13 +53,17 @@
 
         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
@@ -70,8 +76,10 @@
                 _AcceptedProcess = value;
                 update_timer.Enabled = (value != null);
                 UpdateEnabled = update_timer.Enabled;
+#if !DISABLE_GETFIRSTNONZEROBYTE_ONUPDATE_ACCEPTEDPROCESS
                 if (value != null)
                     GetFirstNonZeroByte();
+#endif
             }
         }
         #endregion
@@ -79,12 +87,12 @@
         public IConfigPlugin AcceptedPlugin { get; set; }
         #endregion
         #region IAcceptsMemoryRange members
-        public uint MemoryStart { get { return 0; } }
-        public uint MemorySize { get { return int.MaxValue; } }
+        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
         {
@@ -112,14 +120,14 @@
         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
-        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 { txthexGoto.Value = _CURRENT_TOP_ADDR = value; }
@@ -131,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)
@@ -190,25 +198,32 @@
         }
         private byte[] GetMemory()
         {
+            byte[] data = new byte[] { };
             try
             {
                 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
+                provider.OpenProvider();
                 int bytesReadSize;
-                byte[] data;
-                provider.ReadProcessMemory(CURRENT_TOP_ADDR, max_ram_view, out bytesReadSize, out data);
-                //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[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)
+        private void UpdateMemroyView(int address)
         {
             try
             {
@@ -224,19 +239,19 @@
                     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
                     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)
                     {
                         try
                         {
-                            for (uint j = 0; j < max_address_width; j++)
+                            for (int j = 0; j < max_address_width; j++)
                             {
-                                uint current_addr = i + j;
+                                int current_addr = i + j;
                                 if (current_addr >= MemorySize) break;
                                 byte ascii_value_raw = data[j];
                                 char ascii_value = (char)data[j];
@@ -273,7 +288,7 @@
         //{
         //    this.WriteCurrentBytes();
         //}
-        private void WriteCurrentBytes(uint start_address, byte[] data)
+        private void WriteCurrentBytes(int start_address, byte[] data)
         {
             try
             {
@@ -282,14 +297,16 @@
                 // Byte changed
                 //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 ++)
                 {
-                    uint addr = (uint)(start_address + i);
+                    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()); }
         }
@@ -318,7 +335,7 @@
             //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 = max_ram_view;
@@ -333,7 +350,7 @@
                             this.CURRENT_TOP_ADDR = 0; //NonHandledKeysAreBeingPressed = false; 
                             break;
                         case Keys.End:
-                            this.CURRENT_TOP_ADDR = (uint)((MemorySize - 1) - max_ram_view); //NonHandledKeysAreBeingPressed = false;
+                            this.CURRENT_TOP_ADDR = (int)((MemorySize - 1) - max_ram_view); //NonHandledKeysAreBeingPressed = false;
                             break;
                         default:
                             //NonHandledKeysAreBeingPressed = true;
@@ -353,11 +370,11 @@
                         }
                         else
                         {
-                            this.CURRENT_TOP_ADDR -= (uint)small_scroll_change; //NonHandledKeysAreBeingPressed = false;
+                            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:
                         if (this.CURRENT_TOP_ADDR == 0 && (this.CURRENT_TOP_ADDR - large_scroll_change > this.CURRENT_TOP_ADDR))
@@ -366,11 +383,11 @@
                         }
                         else
                         {
-                            this.CURRENT_TOP_ADDR -= (uint)(large_scroll_change); //NonHandledKeysAreBeingPressed = false;
+                            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;
@@ -436,7 +453,7 @@
             //this.UpdateEnabled = false;
 
             this.UpdateMaxRamView();
-            uint ORIGINAL_ADDR = this.CURRENT_TOP_ADDR;
+            int ORIGINAL_ADDR = this.CURRENT_TOP_ADDR;
             //uint size = MemorySize;
             if (e.Type == ScrollEventType.EndScroll) return;
 
@@ -473,7 +490,7 @@
                 //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;

 

  ViewVC Help
Powered by ViewVC 1.1.22