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 |
|
27 |
namespace RomCheater.RVACalculator |
28 |
{ |
29 |
public partial class RVACalculatorDockControl : DockContent |
30 |
{ |
31 |
public RVACalculatorDockControl() |
32 |
{ |
33 |
InitializeComponent(); |
34 |
} |
35 |
|
36 |
/* |
37 |
* foreach (var col in Enumerable.Range(0, lstCheats.Columns.Count)) |
38 |
{ |
39 |
lstCheats.AutoResizeColumn(col, ColumnHeaderAutoResizeStyle.ColumnContent); |
40 |
} |
41 |
*/ |
42 |
private void btnAdd_Click(object sender, EventArgs e) |
43 |
{ |
44 |
string name = string.Empty; |
45 |
string address = string.Empty; |
46 |
|
47 |
name = Microsoft.VisualBasic.Interaction.InputBox("Input Dialog", "Enter the cheat name", string.Empty); |
48 |
address = Microsoft.VisualBasic.Interaction.InputBox("Input Dialog", "Enter the cheat address (in hex : 0xXXXXXXXX)", string.Empty); |
49 |
uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32(); |
50 |
ListViewItem li = new ListViewItem(name); |
51 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", Convert.ToUInt32(address, 16)))); |
52 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical))); |
53 |
lstCheats.Items.Add(li); |
54 |
} |
55 |
|
56 |
private void btnRemove_Click(object sender, EventArgs e) |
57 |
{ |
58 |
if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } |
59 |
int index = lstCheats.SelectedIndices[0]; |
60 |
lstCheats.Items.RemoveAt(index); |
61 |
} |
62 |
|
63 |
private void btnUpdate_Click(object sender, EventArgs e) |
64 |
{ |
65 |
if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } |
66 |
int index = lstCheats.SelectedIndices[0]; |
67 |
var li = lstCheats.SelectedItems[0]; |
68 |
string name = li.Text; |
69 |
string address = li.SubItems[1].Text; |
70 |
|
71 |
name = Microsoft.VisualBasic.Interaction.InputBox("Input Dialog", "Enter the cheat name", name); |
72 |
address = Microsoft.VisualBasic.Interaction.InputBox("Input Dialog", "Enter the cheat address (in hex : 0xXXXXXXXX)", address); |
73 |
uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32(); |
74 |
li.Text = name; |
75 |
li.SubItems[1].Text = string.Format("0x{0:x8}", Convert.ToUInt32(address, 16)); |
76 |
li.SubItems[2].Text = string.Format("0x{0:x8}", physical); |
77 |
lstCheats.Items[index] = li; |
78 |
} |
79 |
|
80 |
private void btnCopy_Click(object sender, EventArgs e) |
81 |
{ |
82 |
if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } |
83 |
var li = lstCheats.SelectedItems[0]; |
84 |
string physical = li.SubItems[2].Text; |
85 |
Clipboard.SetText(physical); |
86 |
} |
87 |
|
88 |
private void btnSave_Click(object sender, EventArgs e) |
89 |
{ |
90 |
DialogResult result = CheatSaver.ShowDialog(); |
91 |
if (result != DialogResult.OK) { return; } |
92 |
|
93 |
|
94 |
ICheatList list = new ICheatList(); |
95 |
list.RVA = txtRVA.ToUInt32(); |
96 |
List<ICheatEntry> cheats = new List<ICheatEntry>(); |
97 |
foreach (ListViewItem li in lstCheats.Items) |
98 |
{ |
99 |
cheats.Add(new ICheatEntry(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16))); |
100 |
} |
101 |
|
102 |
list.Cheats = cheats; |
103 |
|
104 |
try |
105 |
{ |
106 |
using (FileStream fs = new FileStream(CheatSaver.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) |
107 |
{ |
108 |
try |
109 |
{ |
110 |
BinaryFormatter bin = new BinaryFormatter(); |
111 |
bin.Serialize(fs, list); |
112 |
} |
113 |
catch (Exception ex) |
114 |
{ |
115 |
logger.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName); |
116 |
logger.VerboseError.WriteLine(ex.ToString()); |
117 |
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
118 |
} |
119 |
} |
120 |
MessageBox.Show(string.Format("Successfully saved file: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
121 |
} |
122 |
catch (Exception ex) |
123 |
{ |
124 |
logger.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName); |
125 |
logger.VerboseError.WriteLine(ex.ToString()); |
126 |
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
127 |
} |
128 |
} |
129 |
|
130 |
private void btnLoad_Click(object sender, EventArgs e) |
131 |
{ |
132 |
DialogResult result = CheatLoader.ShowDialog(); |
133 |
if (result != DialogResult.OK) { return; } |
134 |
try |
135 |
{ |
136 |
using (FileStream fs = new FileStream(CheatLoader.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) |
137 |
{ |
138 |
try |
139 |
{ |
140 |
BinaryFormatter bin = new BinaryFormatter(); |
141 |
var list = (ICheatList)bin.Deserialize(fs); |
142 |
|
143 |
txtRVA.Value = list.RVA; |
144 |
result = MessageBox.Show("Clear existing Cheats?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); |
145 |
if (result == System.Windows.Forms.DialogResult.Cancel) |
146 |
{ |
147 |
// assume abort of load |
148 |
logger.Warn.WriteLine("Abored processing of file (by user request): {0}", CheatLoader.FileName); |
149 |
fs.Close(); |
150 |
return; |
151 |
} |
152 |
if (result == DialogResult.Yes) |
153 |
{ |
154 |
lstCheats.Items.Clear(); |
155 |
} |
156 |
foreach (var cheat in list.Cheats) |
157 |
{ |
158 |
ListViewItem li = new ListViewItem(cheat.CheatName); |
159 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", cheat.CheatAddress))); |
160 |
uint physical = cheat.CheatAddress + list.RVA; |
161 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical))); |
162 |
lstCheats.Items.Add(li); |
163 |
} |
164 |
btnRefresh.PerformClick(); // do any needed refreshing |
165 |
} |
166 |
catch (Exception ex) |
167 |
{ |
168 |
logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName); |
169 |
logger.VerboseError.WriteLine(ex.ToString()); |
170 |
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
171 |
} |
172 |
} |
173 |
MessageBox.Show(string.Format("Successfully opened file: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
174 |
} |
175 |
catch (Exception ex) |
176 |
{ |
177 |
logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName); |
178 |
logger.VerboseError.WriteLine(ex.ToString()); |
179 |
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
180 |
} |
181 |
} |
182 |
|
183 |
private void btnRefresh_Click(object sender, EventArgs e) |
184 |
{ |
185 |
int index = 0; |
186 |
foreach (ListViewItem li in lstCheats.Items) |
187 |
{ |
188 |
string name = li.Text; |
189 |
string address = li.SubItems[1].Text; |
190 |
uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32(); |
191 |
li.SubItems[2].Text = string.Format("0x{0:x8}", physical); |
192 |
lstCheats.Items[index] = li; |
193 |
index++; |
194 |
} |
195 |
} |
196 |
|
197 |
private void RVACalculatorDockControl_Shown(object sender, EventArgs e) |
198 |
{ |
199 |
//const int t = 100; |
200 |
////txtRVA.SuspendLayout(); |
201 |
//logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width); |
202 |
//logger.Debug.WriteLine("increasing txtRva.Width to {0}", txtRVA.Width + t); |
203 |
//txtRVA.Width = txtRVA.Width + t; |
204 |
//logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width); |
205 |
////txtRVA.ResumeLayout(); |
206 |
} |
207 |
|
208 |
[Serializable] |
209 |
private struct ICheatEntry |
210 |
{ |
211 |
public ICheatEntry(string name, uint address) |
212 |
{ |
213 |
CheatName = name; |
214 |
CheatAddress = address; |
215 |
} |
216 |
public string CheatName; |
217 |
public uint CheatAddress; |
218 |
} |
219 |
[Serializable] |
220 |
private struct ICheatList |
221 |
{ |
222 |
public uint RVA; |
223 |
public List<ICheatEntry> Cheats; |
224 |
} |
225 |
|
226 |
} |
227 |
} |