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 System.IO; |
13 |
|
14 |
namespace RomCheater.Docking |
15 |
{ |
16 |
public partial class FloatingRamDumperDialog : DockContent, IProcessConfig |
17 |
{ |
18 |
#region sub-classes |
19 |
private const int BYTE_CORRECTION_VALUE = 23; |
20 |
public enum DumpSize |
21 |
{ |
22 |
Bytes, |
23 |
KiloBytes, |
24 |
MegaBytes, |
25 |
GigaBytes, |
26 |
} |
27 |
#endregion |
28 |
private DumpSize dumpSize = DumpSize.Bytes; |
29 |
|
30 |
public FloatingRamDumperDialog() { InitializeComponent(); this.AcceptedPlugin = null; this.AcceptedProcess = null; } |
31 |
public FloatingRamDumperDialog(IConfigPlugin config) : this() { this.AcceptedPlugin = config; } |
32 |
public FloatingRamDumperDialog(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; } |
33 |
|
34 |
private void FloatingRamDumperDialog_Load(object sender, EventArgs e) |
35 |
{ |
36 |
txtStart.Value = 0; |
37 |
txtEnd.Value = int.MaxValue; |
38 |
} |
39 |
|
40 |
|
41 |
#region IProcessConfig Members' |
42 |
public Process AcceptedProcess { get; set; } |
43 |
#endregion |
44 |
#region IAcceptsPlugin<IConfigPlugin> Members |
45 |
public IConfigPlugin AcceptedPlugin { get; set; } |
46 |
#endregion |
47 |
|
48 |
private void radioBTNBytes_CheckedChanged(object sender, EventArgs e) |
49 |
{ |
50 |
dumpSize = DumpSize.Bytes; |
51 |
} |
52 |
|
53 |
private void radioBTNKiloBytes_CheckedChanged(object sender, EventArgs e) |
54 |
{ |
55 |
dumpSize = DumpSize.KiloBytes; |
56 |
} |
57 |
|
58 |
private void radioBTNMegaBytes_CheckedChanged(object sender, EventArgs e) |
59 |
{ |
60 |
dumpSize = DumpSize.MegaBytes; |
61 |
} |
62 |
|
63 |
private void radioBTNGigaBytes_CheckedChanged(object sender, EventArgs e) |
64 |
{ |
65 |
dumpSize = DumpSize.GigaBytes; |
66 |
} |
67 |
|
68 |
private void btnCalcEndAddr_Click(object sender, EventArgs e) |
69 |
{ |
70 |
ulong start = 0; |
71 |
ulong end = 0; |
72 |
start = txtStart.Value; |
73 |
switch (dumpSize) |
74 |
{ |
75 |
case DumpSize.Bytes: |
76 |
end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1.0 + start) - BYTE_CORRECTION_VALUE; |
77 |
txtEnd.Value = end; |
78 |
break; |
79 |
case DumpSize.KiloBytes: |
80 |
end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1000.0 + start) - BYTE_CORRECTION_VALUE; |
81 |
txtEnd.Value = end; |
82 |
break; |
83 |
case DumpSize.MegaBytes: |
84 |
end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1000000.0 + start) - BYTE_CORRECTION_VALUE; |
85 |
txtEnd.Value = end; |
86 |
break; |
87 |
case DumpSize.GigaBytes: |
88 |
end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1000000000.0 + start) - BYTE_CORRECTION_VALUE; |
89 |
txtEnd.Value = end; |
90 |
break; |
91 |
} |
92 |
} |
93 |
|
94 |
private void btnCalcStartAddr_Click(object sender, EventArgs e) |
95 |
{ |
96 |
long start = 0; |
97 |
long end = 0; |
98 |
end = txtEnd.ToInt64(); |
99 |
switch (dumpSize) |
100 |
{ |
101 |
case DumpSize.Bytes: |
102 |
start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1.0)) + BYTE_CORRECTION_VALUE; |
103 |
txtStart.Value = (ulong)start;; |
104 |
break; |
105 |
case DumpSize.KiloBytes: |
106 |
start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1000.0)) + BYTE_CORRECTION_VALUE; |
107 |
txtStart.Value = (ulong)start;; |
108 |
break; |
109 |
case DumpSize.MegaBytes: |
110 |
start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1000000.0)) + BYTE_CORRECTION_VALUE; |
111 |
txtStart.Value = (ulong)start;; |
112 |
break; |
113 |
case DumpSize.GigaBytes: |
114 |
start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1000000000.0)) + BYTE_CORRECTION_VALUE; |
115 |
txtStart.Value = (ulong)start; |
116 |
break; |
117 |
} |
118 |
} |
119 |
|
120 |
private void btnCalcDumpSize_Click(object sender, EventArgs e) |
121 |
{ |
122 |
ulong start = txtStart.Value; |
123 |
ulong end = txtEnd.Value; |
124 |
ulong byte_diff = (end - start) + BYTE_CORRECTION_VALUE; |
125 |
switch (dumpSize) |
126 |
{ |
127 |
case DumpSize.Bytes: |
128 |
txtDumpSize.Text = string.Format("{0:n2}", (double)byte_diff); |
129 |
break; |
130 |
case DumpSize.KiloBytes: |
131 |
txtDumpSize.Text = string.Format("{0:n3}", (double)byte_diff / 1000.0); |
132 |
break; |
133 |
case DumpSize.MegaBytes: |
134 |
txtDumpSize.Text = string.Format("{0:n6}", (double)byte_diff / 1000000.0); |
135 |
break; |
136 |
case DumpSize.GigaBytes: |
137 |
txtDumpSize.Text = string.Format("{0:n9}", (double)byte_diff / 1000000000.0); |
138 |
break; |
139 |
} |
140 |
} |
141 |
|
142 |
private void btnDumpRam_Click(object sender, EventArgs e) |
143 |
{ |
144 |
if (this.AcceptedProcess == null) |
145 |
{ |
146 |
MessageBox.Show("Please select a process to dump memory from", "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
147 |
return; |
148 |
} |
149 |
DialogResult result = dumpsaver.ShowDialog(); |
150 |
if (result != DialogResult.OK) return; |
151 |
DumpRam(txtStart.Value, txtEnd.Value, dumpsaver.FileName); |
152 |
} |
153 |
|
154 |
|
155 |
#region memory support |
156 |
private void DumpRam(ulong start, ulong end, string filename) |
157 |
{ |
158 |
uint byte_count = (uint)(end - start); |
159 |
DumpRam(start, byte_count, filename); |
160 |
} |
161 |
private void DumpRam(ulong start, uint count, string filename) |
162 |
{ |
163 |
if (this.AcceptedProcess == null) return; |
164 |
Sojaner.MemoryScanner.ProcessMemoryReader reader = new Sojaner.MemoryScanner.ProcessMemoryReader(); |
165 |
reader.ReadProcess = this.AcceptedProcess; |
166 |
reader.OpenProcess(); |
167 |
int bytesReadSize; |
168 |
byte[] data = reader.ReadProcessMemory((IntPtr)(uint)start, count, out bytesReadSize); |
169 |
reader.CloseHandle(); |
170 |
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) |
171 |
{ |
172 |
BinaryWriter bw = new BinaryWriter(fs); |
173 |
foreach (byte b in data) { bw.Write(b); } |
174 |
bw.Flush(); |
175 |
bw.Close(); |
176 |
} |
177 |
} |
178 |
#endregion |
179 |
} |
180 |
} |