1 |
#region Logging Defines |
2 |
// include this any class or method that required logging, and comment-out what is not needed |
3 |
|
4 |
#region Enabled logging levels |
5 |
#define LOGGING_ENABLE_INFO |
6 |
#define LOGGING_ENABLE_WARN |
7 |
#define LOGGING_ENABLE_DEBUG |
8 |
#define LOGGING_ENABLE_VERBOSEDEBUG |
9 |
#define LOGGING_ENABLE_ERROR |
10 |
#define LOGGING_ENABLE_VERBOSEERROR |
11 |
#define LOGGING_ENABLE_PROFILER |
12 |
#endregion |
13 |
#endregion |
14 |
using System; |
15 |
using System.Collections.Generic; |
16 |
using System.ComponentModel; |
17 |
using System.Data; |
18 |
using System.Drawing; |
19 |
using System.Linq; |
20 |
using System.Text; |
21 |
using System.Windows.Forms; |
22 |
using WeifenLuo.WinFormsUI.Docking; |
23 |
using RomCheater.Logging; |
24 |
using System.IO; |
25 |
using System.Runtime.Serialization.Formatters.Binary; |
26 |
using RomCheater.PluginFramework.Core; |
27 |
using RomCheater.Core; |
28 |
|
29 |
namespace RomCheater.RVACalculator |
30 |
{ |
31 |
public partial class RVACalculatorDockControl : DockContent |
32 |
{ |
33 |
private UserControlPlugin plugin; |
34 |
public RVACalculatorDockControl(UserControlPlugin plugin) |
35 |
{ |
36 |
this.plugin = plugin; |
37 |
InitPluginFramework(); |
38 |
InitializeComponent(); |
39 |
} |
40 |
private void InitPluginFramework() |
41 |
{ |
42 |
if (this.plugin == null) { return; } |
43 |
this.plugin.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(plugin_OnSelectedProcessChanged); |
44 |
this.plugin.OnSelectedConfigChanged += new BaseEventHandler<ConfigChangedEventArgs>(plugin_OnSelectedConfigChanged); |
45 |
this.plugin.OnPEDataUpdated += new BaseEventHandler<PEViewerDataUpdatedEventArgs>(plugin_OnPEDataUpdated); |
46 |
} |
47 |
void plugin_OnPEDataUpdated(PEViewerDataUpdatedEventArgs e) |
48 |
{ |
49 |
logger.Warn.WriteLine("plugin_OnPEDataUpdated::has not been implemented!"); |
50 |
} |
51 |
void plugin_OnSelectedConfigChanged(ConfigChangedEventArgs e) |
52 |
{ |
53 |
logger.Warn.WriteLine("plugin_OnSelectedConfigChanged::has not been implemented!"); |
54 |
} |
55 |
void plugin_OnSelectedProcessChanged(ProcessChangedEventArgs e) |
56 |
{ |
57 |
logger.Warn.WriteLine("plugin_OnSelectedProcessChanged::has not been implemented!"); |
58 |
} |
59 |
/* |
60 |
* foreach (var col in Enumerable.Range(0, lstCheats.Columns.Count)) |
61 |
{ |
62 |
lstCheats.AutoResizeColumn(col, ColumnHeaderAutoResizeStyle.ColumnContent); |
63 |
} |
64 |
*/ |
65 |
private void btnAdd_Click(object sender, EventArgs e) |
66 |
{ |
67 |
string name = string.Empty; |
68 |
string address = string.Empty; |
69 |
|
70 |
name = Microsoft.VisualBasic.Interaction.InputBox("Input Dialog", "Enter the cheat name", string.Empty); |
71 |
address = Microsoft.VisualBasic.Interaction.InputBox("Input Dialog", "Enter the cheat address (in hex : 0xXXXXXXXX)", string.Empty); |
72 |
uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32(); |
73 |
ListViewItem li = new ListViewItem(name); |
74 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", Convert.ToUInt32(address, 16)))); |
75 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical))); |
76 |
lstCheats.Items.Add(li); |
77 |
} |
78 |
|
79 |
private void btnRemove_Click(object sender, EventArgs e) |
80 |
{ |
81 |
if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } |
82 |
int index = lstCheats.SelectedIndices[0]; |
83 |
lstCheats.Items.RemoveAt(index); |
84 |
} |
85 |
|
86 |
private void btnUpdate_Click(object sender, EventArgs e) |
87 |
{ |
88 |
if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } |
89 |
int index = lstCheats.SelectedIndices[0]; |
90 |
var li = lstCheats.SelectedItems[0]; |
91 |
string name = li.Text; |
92 |
string address = li.SubItems[1].Text; |
93 |
|
94 |
name = Microsoft.VisualBasic.Interaction.InputBox("Input Dialog", "Enter the cheat name", name); |
95 |
address = Microsoft.VisualBasic.Interaction.InputBox("Input Dialog", "Enter the cheat address (in hex : 0xXXXXXXXX)", address); |
96 |
uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32(); |
97 |
li.Text = name; |
98 |
li.SubItems[1].Text = string.Format("0x{0:x8}", Convert.ToUInt32(address, 16)); |
99 |
li.SubItems[2].Text = string.Format("0x{0:x8}", physical); |
100 |
lstCheats.Items[index] = li; |
101 |
} |
102 |
|
103 |
private void btnCopy_Click(object sender, EventArgs e) |
104 |
{ |
105 |
if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } |
106 |
var li = lstCheats.SelectedItems[0]; |
107 |
string physical = li.SubItems[2].Text; |
108 |
Clipboard.SetText(physical); |
109 |
} |
110 |
|
111 |
private void btnSave_Click(object sender, EventArgs e) |
112 |
{ |
113 |
DialogResult result = CheatSaver.ShowDialog(); |
114 |
if (result != DialogResult.OK) { return; } |
115 |
|
116 |
|
117 |
ICheatList2 list = new ICheatList2(); |
118 |
list.RVA = txtRVA.ToUInt32(); |
119 |
List<ICheatEntry2> cheats = new List<ICheatEntry2>(); |
120 |
foreach (ListViewItem li in lstCheats.Items) |
121 |
{ |
122 |
cheats.Add(new ICheatEntry2(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16), Convert.ToUInt32(li.SubItems[2].Text, 16))); |
123 |
} |
124 |
|
125 |
list.Cheats = cheats; |
126 |
|
127 |
try |
128 |
{ |
129 |
using (FileStream fs = new FileStream(CheatSaver.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) |
130 |
{ |
131 |
try |
132 |
{ |
133 |
fs.Seek(0, SeekOrigin.Begin); |
134 |
BinaryFormatter bin = new BinaryFormatter(); |
135 |
bin.Serialize(fs, list); |
136 |
} |
137 |
catch (Exception ex) |
138 |
{ |
139 |
logger.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName); |
140 |
logger.VerboseError.WriteLine(ex.ToString()); |
141 |
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
142 |
return; |
143 |
} |
144 |
} |
145 |
MessageBox.Show(string.Format("Successfully saved file: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
146 |
} |
147 |
catch (Exception ex) |
148 |
{ |
149 |
logger.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName); |
150 |
logger.VerboseError.WriteLine(ex.ToString()); |
151 |
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
152 |
} |
153 |
} |
154 |
|
155 |
private void btnLoad_Click(object sender, EventArgs e) |
156 |
{ |
157 |
DialogResult result = CheatLoader.ShowDialog(); |
158 |
if (result != DialogResult.OK) { return; } |
159 |
try |
160 |
{ |
161 |
using (FileStream fs = new FileStream(CheatLoader.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) |
162 |
{ |
163 |
try |
164 |
{ |
165 |
ICheatList2 list = new ICheatList2(); |
166 |
BinaryFormatter bin = new BinaryFormatter(); |
167 |
try |
168 |
{ |
169 |
fs.Seek(0, SeekOrigin.Begin); |
170 |
var t_list = (ICheatList2)bin.Deserialize(fs); |
171 |
list = t_list; |
172 |
} |
173 |
catch (Exception) |
174 |
{ |
175 |
fs.Seek(0, SeekOrigin.Begin); |
176 |
var t_list = (ICheatList)bin.Deserialize(fs); |
177 |
list = new ICheatList2(t_list); |
178 |
} |
179 |
|
180 |
txtRVA.Value = list.RVA; |
181 |
if (lstCheats.Items.Count > 0) |
182 |
{ |
183 |
result = MessageBox.Show("Clear existing Cheats?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); |
184 |
if (result == System.Windows.Forms.DialogResult.Cancel) |
185 |
{ |
186 |
// assume abort of load |
187 |
logger.Warn.WriteLine("Abored processing of file (by user request): {0}", CheatLoader.FileName); |
188 |
fs.Close(); |
189 |
return; |
190 |
} |
191 |
if (result == DialogResult.Yes) |
192 |
{ |
193 |
lstCheats.Items.Clear(); |
194 |
} |
195 |
} |
196 |
foreach (var cheat in list.Cheats) |
197 |
{ |
198 |
ListViewItem li = new ListViewItem(cheat.CheatName); |
199 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", cheat.CheatAddress))); |
200 |
uint physical = cheat.CheatAddress + list.RVA; |
201 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical))); |
202 |
lstCheats.Items.Add(li); |
203 |
} |
204 |
btnRefresh.PerformClick(); // do any needed refreshing |
205 |
} |
206 |
catch (Exception ex) |
207 |
{ |
208 |
logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName); |
209 |
logger.VerboseError.WriteLine(ex.ToString()); |
210 |
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
211 |
return; |
212 |
} |
213 |
} |
214 |
MessageBox.Show(string.Format("Successfully opened file: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
215 |
} |
216 |
catch (Exception ex) |
217 |
{ |
218 |
logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName); |
219 |
logger.VerboseError.WriteLine(ex.ToString()); |
220 |
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
221 |
} |
222 |
} |
223 |
|
224 |
private void btnRefresh_Click(object sender, EventArgs e) |
225 |
{ |
226 |
int index = 0; |
227 |
foreach (ListViewItem li in lstCheats.Items) |
228 |
{ |
229 |
string name = li.Text; |
230 |
string address = li.SubItems[1].Text; |
231 |
uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32(); |
232 |
li.SubItems[2].Text = string.Format("0x{0:x8}", physical); |
233 |
lstCheats.Items[index] = li; |
234 |
index++; |
235 |
} |
236 |
} |
237 |
private void btnCopyAll_Click(object sender, EventArgs e) |
238 |
{ |
239 |
ICheatList2 list = new ICheatList2(); |
240 |
list.RVA = txtRVA.ToUInt32(); |
241 |
List<ICheatEntry2> cheats = new List<ICheatEntry2>(); |
242 |
foreach (ListViewItem li in lstCheats.Items) |
243 |
{ |
244 |
cheats.Add(new ICheatEntry2(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16), Convert.ToUInt32(li.SubItems[2].Text, 16))); |
245 |
} |
246 |
list.Cheats = cheats; |
247 |
StringBuilder builder = new StringBuilder(); |
248 |
builder.AppendFormat("RVA: 0x{0:x8}", list.RVA); |
249 |
builder.AppendLine(); |
250 |
foreach (ColumnHeader t in lstCheats.Columns) |
251 |
{ |
252 |
builder.AppendFormat("{0}:\t\t", t.Text); |
253 |
} |
254 |
builder.AppendLine(); |
255 |
foreach (var cheat in list.Cheats) |
256 |
{ |
257 |
builder.AppendFormat("{0}\t\t0x{1:x8}\t\t0x{2:x8}", cheat.CheatName, cheat.CheatAddress, cheat.PhysicalAddress); |
258 |
builder.AppendLine(); |
259 |
} |
260 |
Clipboard.SetText(builder.ToString()); |
261 |
|
262 |
} |
263 |
private void RVACalculatorDockControl_Shown(object sender, EventArgs e) |
264 |
{ |
265 |
//const int t = 100; |
266 |
////txtRVA.SuspendLayout(); |
267 |
//logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width); |
268 |
//logger.Debug.WriteLine("increasing txtRva.Width to {0}", txtRVA.Width + t); |
269 |
//txtRVA.Width = txtRVA.Width + t; |
270 |
//logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width); |
271 |
////txtRVA.ResumeLayout(); |
272 |
} |
273 |
|
274 |
[Serializable] |
275 |
private struct ICheatEntry |
276 |
{ |
277 |
public ICheatEntry(string name, uint address) |
278 |
{ |
279 |
CheatName = name; |
280 |
CheatAddress = address; |
281 |
} |
282 |
public string CheatName; |
283 |
public uint CheatAddress; |
284 |
} |
285 |
[Serializable] |
286 |
private struct ICheatEntry2 |
287 |
{ |
288 |
public ICheatEntry2(string name, uint address, uint physical) |
289 |
{ |
290 |
CheatName = name; |
291 |
CheatAddress = address; |
292 |
PhysicalAddress = physical; |
293 |
} |
294 |
public string CheatName; |
295 |
public uint CheatAddress; |
296 |
public uint PhysicalAddress; |
297 |
} |
298 |
[Serializable] |
299 |
private struct ICheatList |
300 |
{ |
301 |
public ICheatList(ICheatList2 t) |
302 |
{ |
303 |
RVA = t.RVA; |
304 |
List<ICheatEntry> cheats = new List<ICheatEntry>(); |
305 |
t.Cheats.ForEach(c => cheats.Add(new ICheatEntry(c.CheatName,c.CheatAddress))); |
306 |
Cheats = cheats; |
307 |
} |
308 |
public ICheatList(uint rva, List<ICheatEntry> cheats) |
309 |
{ |
310 |
RVA = rva; |
311 |
Cheats = cheats; |
312 |
} |
313 |
public uint RVA; |
314 |
public List<ICheatEntry> Cheats; |
315 |
} |
316 |
[Serializable] |
317 |
private struct ICheatList2 |
318 |
{ |
319 |
public ICheatList2(ICheatList t) |
320 |
{ |
321 |
RVA = t.RVA; |
322 |
List<ICheatEntry2> cheats = new List<ICheatEntry2>(); |
323 |
t.Cheats.ForEach(c => cheats.Add(new ICheatEntry2(c.CheatName, c.CheatAddress, c.CheatAddress))); |
324 |
Cheats = cheats; |
325 |
} |
326 |
public ICheatList2(uint rva, List<ICheatEntry2> cheats) |
327 |
{ |
328 |
RVA = rva; |
329 |
Cheats = cheats; |
330 |
} |
331 |
public uint RVA; |
332 |
public List<ICheatEntry2> Cheats; |
333 |
} |
334 |
|
335 |
private void txtRVA_ValueChanged(object sender, ValueChangedEventArgs e) |
336 |
{ |
337 |
btnRefresh.PerformClick(); |
338 |
} |
339 |
|
340 |
|
341 |
} |
342 |
} |