/[RomCheater]/trunk/RomCheater/Docking/FloatingMemorySectionViewer.cs
ViewVC logotype

Contents of /trunk/RomCheater/Docking/FloatingMemorySectionViewer.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 686 - (show annotations) (download)
Mon Jun 17 09:31:52 2013 UTC (8 years, 1 month ago) by william
File size: 10464 byte(s)
+ massive move of source files

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using WeifenLuo.WinFormsUI.Docking;
10 using System.Diagnostics;
11 using RomCheater.PluginFramework.Events;
12 using Sojaner.MemoryScanner;
13 using Sojaner.MemoryScanner.MemoryProviers;
14 using ManagedWinapi;
15 using RomCheater.Core;
16
17 namespace RomCheater.Docking
18 {
19 public partial class FloatingMemorySectionViewer : DockContent,
20 IAcceptsPlugin<IConfigPlugin>,
21 IAcceptsProcess<Process>,
22 IAcceptsProcessAndConfig,
23 IAcceptPEData,
24 IAcceptsBrowseMemoryRegion
25 {
26 ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter();
27 public FloatingMemorySectionViewer()
28 {
29 InitializeComponent();
30 this.lstMemoryRegions.ListViewItemSorter = lvwColumnSorter;
31 }
32
33 public FloatingMemorySectionViewer(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
34 public FloatingMemorySectionViewer(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
35
36 #region IAcceptsPlugin<IConfigPlugin> Members
37 private IConfigPlugin _AcceptedPlugin;
38 public IConfigPlugin AcceptedPlugin { get { return _AcceptedPlugin; } set { _AcceptedPlugin = value; UpdateAcceptedPlugin(value); } }
39 private void UpdateAcceptedPlugin(IConfigPlugin config)
40 {
41 if (config == null) { return; }
42 // perform processing when config plugin is updated
43 }
44 #endregion
45 #region IAcceptsProcess<Process> Members
46 private Process _AcceptedProcess;
47 public Process AcceptedProcess { get { return _AcceptedProcess; } set { _AcceptedProcess = value; UpdateAcceptedProcess(value); } }
48 private void UpdateAcceptedProcess(Process process)
49 {
50 if (process == null) { return; }
51 // perform processing when process is updated
52 List<MEMORY_REGION_INFORMATION> regions = new List<MEMORY_REGION_INFORMATION>();
53 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
54 {
55 provider.OpenProvider();
56 if (this.PEData != null)
57 {
58 if (this.PEData.Is32bitAssembly())
59 {
60 regions = provider.QueryMemoryRegions(MemorySizeConstants.MinimumAddress, MemorySizeConstants.MaximumAddressSize_x86);
61 }
62 else
63 {
64 regions = provider.QueryMemoryRegions(MemorySizeConstants.MinimumAddress, MemorySizeConstants.MaximumAddressSize_x64);
65 }
66 }
67 else
68 {
69 regions = provider.QueryMemoryRegions(MemorySizeConstants.MinimumAddress, MemorySizeConstants.MaximumAddressSize_x86);
70 }
71
72 provider.CloseProvider();
73 }
74 PopulateMemoryRegions(regions);
75 }
76 #endregion
77
78 #region IAcceptPEData Members
79 private IPEDData _PEData;
80 private IPEDData PEData { get { return _PEData; } set { _PEData = value; } }
81 public void SetPEViewerData(IPEDData peData)
82 {
83 this.PEData = peData;
84 }
85 #endregion
86
87
88 #region IAcceptsBrowseMemoryRegion members
89 public event BaseEventHandler<BrowseMemoryRegionEvent> OnBrowseMemoryRegion;
90 #endregion
91 private void PopulateMemoryRegions(List<MEMORY_REGION_INFORMATION> regions)
92 {
93 lstMemoryRegions.Items.Clear();
94 if (regions.Count == 0) { return; }
95
96 List<ListViewItem> items = new List<ListViewItem>();
97
98 foreach (var region in regions)
99 {
100 ListViewItem li = new ListViewItem();
101
102 if (this.PEData != null)
103 {
104 if (this.PEData.Is32bitAssembly())
105 {
106 li.Text = string.Format("0x{0}", region.StartAddress.ToString("X").PadLeft(sizeof(uint) * 2, '0'));
107 li.SubItems.Add(string.Format("0x{0}", region.EndAddress.ToString("X").PadLeft(sizeof(uint) * 2, '0')));
108 li.SubItems.Add(string.Format("0x{0}", region.Size.ToString("X").PadLeft(sizeof(uint) * 2, '0')));
109 }
110 else
111 {
112 li.Text = string.Format("0x{0}", region.StartAddress.ToString("X").PadLeft(sizeof(ulong) * 2, '0'));
113 li.SubItems.Add(string.Format("0x{0}", region.EndAddress.ToString("X").PadLeft(sizeof(ulong) * 2, '0')));
114 li.SubItems.Add(string.Format("0x{0}", region.Size.ToString("X").PadLeft(sizeof(ulong) * 2, '0')));
115 }
116 }
117 else
118 {
119 li.Text = string.Format("0x{0}", region.StartAddress.ToString("X").PadLeft(sizeof(ulong) * 2, '0'));
120 li.SubItems.Add(string.Format("0x{0}", region.EndAddress.ToString("X").PadLeft(sizeof(ulong) * 2, '0')));
121 li.SubItems.Add(string.Format("0x{0}", region.Size.ToString("X").PadLeft(sizeof(ulong) * 2, '0')));
122 }
123
124 li.SubItems.Add(string.Format("{0}", region.RegionInfo.Protect.ToString()));
125 li.SubItems.Add(string.Format("{0}", region.RegionInfo.State.ToString()));
126 li.SubItems.Add(string.Format("{0}", region.RegionInfo.Type.ToString()));
127
128
129 if(region.RegionInfo.Protect == AllocationProtect.PAGE_NOACCESS ||
130 region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_NOACCESS))
131 {
132 li.ForeColor = Color.Red;
133 }
134
135 else if (region.RegionInfo.Protect == AllocationProtect.PAGE_READONLY ||
136 region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_READONLY) ||
137 region.RegionInfo.Protect == AllocationProtect.PAGE_EXECUTE_READ ||
138 region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_EXECUTE_READ))
139 {
140 li.ForeColor = Color.RoyalBlue;
141 }
142 else if (region.RegionInfo.Protect == AllocationProtect.PAGE_READWRITE ||
143 region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_READWRITE) ||
144 region.RegionInfo.Protect == AllocationProtect.PAGE_EXECUTE_READWRITE ||
145 region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_EXECUTE_READWRITE))
146 {
147 li.ForeColor = Color.Green;
148 }
149 else if (region.RegionInfo.Protect == AllocationProtect.NONE)
150 {
151 li.ForeColor = Color.Orange;
152 }
153 else
154 {
155 li.ForeColor = Color.Black;
156 }
157
158 items.Add(li);
159 }
160
161 if (items.Count == 0) { return; }
162
163 lstMemoryRegions.BeginUpdate();
164 lstMemoryRegions.Items.AddRange(items.ToArray());
165 lstMemoryRegions.EndUpdate();
166
167
168 foreach (var col in Enumerable.Range(0, lstMemoryRegions.Columns.Count))
169 {
170 lstMemoryRegions.AutoResizeColumn(col, ColumnHeaderAutoResizeStyle.ColumnContent);
171 }
172
173 }
174
175
176 private void lstMemoryRegions_ColumnClick(object sender, ColumnClickEventArgs e)
177 {
178 // Determine if clicked column is already the column that is being sorted.
179 if (e.Column == lvwColumnSorter.SortColumn)
180 {
181 // Reverse the current sort direction for this column.
182 if (lvwColumnSorter.Order == SortOrder.Ascending)
183 {
184 lvwColumnSorter.Order = SortOrder.Descending;
185 }
186 else
187 {
188 lvwColumnSorter.Order = SortOrder.Ascending;
189 }
190 }
191 else
192 {
193 // Set the column number that is to be sorted; default to ascending.
194 lvwColumnSorter.SortColumn = e.Column;
195 lvwColumnSorter.Order = SortOrder.Ascending;
196 }
197
198 // Perform the sort with these new sort options.
199 this.lstMemoryRegions.Sort();
200 }
201
202 private void mnuItemCopy_Click(object sender, EventArgs e)
203 {
204 StringBuilder builder = new StringBuilder();
205
206 if (lstMemoryRegions.SelectedItems.Count == 0) { return; }
207
208
209 foreach (ColumnHeader col in lstMemoryRegions.Columns)
210 {
211 builder.AppendFormat("{0}\t", col.Text);
212 }
213 builder.AppendLine();
214
215
216
217 foreach (ListViewItem li in lstMemoryRegions.SelectedItems)
218 {
219 //builder.AppendFormat("{0} ", li.Text);
220 foreach (ListViewItem.ListViewSubItem item in li.SubItems)
221 {
222 builder.AppendFormat("{0}\t", item.Text);
223 }
224 if (lstMemoryRegions.SelectedItems.Count > 1)
225 {
226 builder.AppendLine();
227 }
228 }
229 Clipboard.SetText(builder.ToString().Trim());
230 }
231
232 private void contextmnuRegion_Opening(object sender, CancelEventArgs e)
233 {
234 if (lstMemoryRegions.Items.Count == 0) { e.Cancel = true; }
235 }
236
237 private void mnuItemBrowseFirstSelectedRegion_Click(object sender, EventArgs e)
238 {
239 if (lstMemoryRegions.SelectedItems.Count == 0) { return; }
240
241 ListViewItem li = lstMemoryRegions.SelectedItems[0];
242
243 ulong address = Convert.ToUInt64(li.Text, 16);
244
245 if (OnBrowseMemoryRegion != null)
246 OnBrowseMemoryRegion(new BrowseMemoryRegionEvent(this, address));
247 }
248
249 }
250 }

  ViewVC Help
Powered by ViewVC 1.1.22