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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 892 - (show annotations) (download)
Wed Sep 17 05:56:41 2014 UTC (6 years, 10 months ago) by william
File size: 10433 byte(s)
+ alot of dang work to force an icon to be shown on the docked panels
  ** when it was as simple as setting: DockPanel.ShowDocumentIcon to true

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

  ViewVC Help
Powered by ViewVC 1.1.22