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 |
{ |
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(Sojaner.MemoryScanner.IPEDData peData) |
82 |
{ |
83 |
this.PEData = peData; |
84 |
} |
85 |
#endregion |
86 |
|
87 |
|
88 |
|
89 |
private void PopulateMemoryRegions(List<MEMORY_REGION_INFORMATION> regions) |
90 |
{ |
91 |
lstMemoryRegions.Items.Clear(); |
92 |
if (regions.Count == 0) { return; } |
93 |
|
94 |
List<ListViewItem> items = new List<ListViewItem>(); |
95 |
|
96 |
foreach (var region in regions) |
97 |
{ |
98 |
ListViewItem li = new ListViewItem(); |
99 |
|
100 |
if (this.PEData != null) |
101 |
{ |
102 |
if (this.PEData.Is32bitAssembly()) |
103 |
{ |
104 |
li.Text = string.Format("0x{0}", region.StartAddress.ToString("X").PadLeft(sizeof(uint) * 2, '0')); |
105 |
li.SubItems.Add(string.Format("0x{0}", region.EndAddress.ToString("X").PadLeft(sizeof(uint) * 2, '0'))); |
106 |
li.SubItems.Add(string.Format("0x{0}", region.Size.ToString("X").PadLeft(sizeof(uint) * 2, '0'))); |
107 |
} |
108 |
else |
109 |
{ |
110 |
li.Text = string.Format("0x{0}", region.StartAddress.ToString("X").PadLeft(sizeof(ulong) * 2, '0')); |
111 |
li.SubItems.Add(string.Format("0x{0}", region.EndAddress.ToString("X").PadLeft(sizeof(ulong) * 2, '0'))); |
112 |
li.SubItems.Add(string.Format("0x{0}", region.Size.ToString("X").PadLeft(sizeof(ulong) * 2, '0'))); |
113 |
} |
114 |
} |
115 |
else |
116 |
{ |
117 |
li.Text = string.Format("0x{0}", region.StartAddress.ToString("X").PadLeft(sizeof(ulong) * 2, '0')); |
118 |
li.SubItems.Add(string.Format("0x{0}", region.EndAddress.ToString("X").PadLeft(sizeof(ulong) * 2, '0'))); |
119 |
li.SubItems.Add(string.Format("0x{0}", region.Size.ToString("X").PadLeft(sizeof(ulong) * 2, '0'))); |
120 |
} |
121 |
|
122 |
li.SubItems.Add(string.Format("{0}", region.RegionInfo.Protect.ToString())); |
123 |
li.SubItems.Add(string.Format("{0}", region.RegionInfo.State.ToString())); |
124 |
li.SubItems.Add(string.Format("{0}", region.RegionInfo.Type.ToString())); |
125 |
|
126 |
|
127 |
if(region.RegionInfo.Protect == AllocationProtect.PAGE_NOACCESS || |
128 |
region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_NOACCESS)) |
129 |
{ |
130 |
li.ForeColor = Color.Red; |
131 |
} |
132 |
|
133 |
else if (region.RegionInfo.Protect == AllocationProtect.PAGE_READONLY || |
134 |
region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_READONLY) || |
135 |
region.RegionInfo.Protect == AllocationProtect.PAGE_EXECUTE_READ || |
136 |
region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_EXECUTE_READ)) |
137 |
{ |
138 |
li.ForeColor = Color.RoyalBlue; |
139 |
} |
140 |
else if (region.RegionInfo.Protect == AllocationProtect.PAGE_READWRITE || |
141 |
region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_READWRITE) || |
142 |
region.RegionInfo.Protect == AllocationProtect.PAGE_EXECUTE_READWRITE || |
143 |
region.RegionInfo.Protect.HasFlag(AllocationProtect.PAGE_EXECUTE_READWRITE)) |
144 |
{ |
145 |
li.ForeColor = Color.Green; |
146 |
} |
147 |
else if (region.RegionInfo.Protect == AllocationProtect.NONE) |
148 |
{ |
149 |
li.ForeColor = Color.Orange; |
150 |
} |
151 |
else |
152 |
{ |
153 |
li.ForeColor = Color.Black; |
154 |
} |
155 |
|
156 |
items.Add(li); |
157 |
} |
158 |
|
159 |
if (items.Count == 0) { return; } |
160 |
|
161 |
lstMemoryRegions.BeginUpdate(); |
162 |
lstMemoryRegions.Items.AddRange(items.ToArray()); |
163 |
lstMemoryRegions.EndUpdate(); |
164 |
|
165 |
|
166 |
foreach (var col in Enumerable.Range(0, lstMemoryRegions.Columns.Count)) |
167 |
{ |
168 |
lstMemoryRegions.AutoResizeColumn(col, ColumnHeaderAutoResizeStyle.ColumnContent); |
169 |
} |
170 |
|
171 |
} |
172 |
|
173 |
|
174 |
private void lstMemoryRegions_ColumnClick(object sender, ColumnClickEventArgs e) |
175 |
{ |
176 |
// Determine if clicked column is already the column that is being sorted. |
177 |
if (e.Column == lvwColumnSorter.SortColumn) |
178 |
{ |
179 |
// Reverse the current sort direction for this column. |
180 |
if (lvwColumnSorter.Order == SortOrder.Ascending) |
181 |
{ |
182 |
lvwColumnSorter.Order = SortOrder.Descending; |
183 |
} |
184 |
else |
185 |
{ |
186 |
lvwColumnSorter.Order = SortOrder.Ascending; |
187 |
} |
188 |
} |
189 |
else |
190 |
{ |
191 |
// Set the column number that is to be sorted; default to ascending. |
192 |
lvwColumnSorter.SortColumn = e.Column; |
193 |
lvwColumnSorter.Order = SortOrder.Ascending; |
194 |
} |
195 |
|
196 |
// Perform the sort with these new sort options. |
197 |
this.lstMemoryRegions.Sort(); |
198 |
} |
199 |
|
200 |
} |
201 |
} |