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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 679 - (show annotations) (download)
Mon Jun 17 07:31:15 2013 UTC (7 years, 10 months ago) by william
File size: 10510 byte(s)
+ move some data into pluginframework

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

  ViewVC Help
Powered by ViewVC 1.1.22