-
Notifications
You must be signed in to change notification settings - Fork 16
/
frmMain.cs
455 lines (414 loc) · 17.5 KB
/
frmMain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using EOLib.IO;
using EOLib.IO.Pub;
using EOLib.IO.Services;
using EOLib.IO.Services.Serializers;
namespace BatchPub
{
public partial class frmMain : Form
{
private IPubFile<EIFRecord> eif;
private string _fname;
private bool changes;
private enum CompareOperator
{
EQ,
GT,
LT,
GTE,
LTE,
NE,
REGEX
}
public frmMain()
{
InitializeComponent();
InitializeMore();
}
private void btnReset_Click(object sender, EventArgs e)
{
SuspendLayout();
Controls.Clear();
ResumeLayout();
InitializeComponent();
InitializeMore();
eif = null;
}
private void InitializeMore()
{
cmbStepTwoField.Items.Clear();
cmbStepThreeField.Items.Clear();
var eifType = typeof (EIFRecord);
foreach(System.Reflection.PropertyInfo prop in eifType.GetProperties())
{
cmbStepTwoField.Items.Add(new PropInfo(prop));
cmbStepThreeField.Items.Add(new PropInfo(prop));
}
}
private void btnProcess_Click(object sender, EventArgs e)
{
//process everything
System.Reflection.PropertyInfo pi = (cmbStepTwoField.SelectedItem as PropInfo).PropertyInfo;
object newValue;
switch ((cmbStepTwoField.SelectedItem as PropInfo).PropertyInfo.Name)
{
case "Type":
case "SubType":
case "Special":
case "Size":
newValue = cmbStepTwoValue.SelectedIndex;
break;
case "Name":
newValue = txtStepTwoValue.Text;
break;
default:
int dummy;
newValue = txtStepTwoValue.Text.ToString();
if (!int.TryParse(newValue.ToString(), out dummy))
{
MessageBox.Show("Only integer values are supported for the value field in step two.");
return;
}
newValue = dummy;
break;
}
if(!chkFilterOn.Checked)
{ //process the change for EVERY item record. save changes immediately.
if (MessageBox.Show(
"This change will be processed for every item immediately. The change is irreversible. Are you sure you want to continue? You may specify a filter by checking the filter option.", "No filter selected",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) == DialogResult.No
)
{
return;
}
rtfOutput.Text += "Processing change: set " + pi.Name + "(" + pi.PropertyType.ToString() + ")=" + newValue.ToString() + " for all items...";
foreach (var rec in eif)
{
System.Reflection.PropertyInfo prop = rec.GetType().GetProperty(pi.Name);
prop.SetValue(rec, Convert.ChangeType(newValue, pi.PropertyType));
}
rtfOutput.Text += "done.\n\n";
}
else
{
object compareValue;
CompareOperator op;
try
{
op = (CompareOperator)cmbCompareType.SelectedIndex;
if(op == CompareOperator.REGEX && cmbCompareValue.Enabled)
{
MessageBox.Show("You can't use a regex to parse enumerated types");
return;
}
}
catch
{
MessageBox.Show("Invalid comparison operator specified.");
return;
}
Regex comparer = null;
if (op == CompareOperator.REGEX)
{
try
{
compareValue = txtCompareValue.Text;
comparer = new Regex(compareValue.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Unable to parse regular expression:\n " + ex.Message, "Error!");
return;
}
}
else
{
switch ((cmbStepThreeField.SelectedItem as PropInfo).PropertyInfo.Name)
{
case "Type":
compareValue = (ItemType)cmbCompareValue.SelectedIndex;
break;
case "SubType":
compareValue = (ItemSubType)cmbCompareValue.SelectedIndex;
break;
case "Special":
compareValue = (ItemSpecial)cmbCompareValue.SelectedIndex;
break;
case "Size":
compareValue = (ItemSize)cmbCompareValue.SelectedIndex;
break;
case "Name":
compareValue = txtCompareValue.Text;
break;
default:
compareValue = txtCompareValue.Text;
int dummy;
if (!int.TryParse(compareValue.ToString(), out dummy))
{
MessageBox.Show("Only integer values are allowed for this comparison type.", "Error parsing");
return;
}
compareValue = dummy;
break;
}
}
List<EIFRecord> filtered = eif.Where(record =>
{
EIFRecord rec = (EIFRecord)record;
if (rec == null || rec.ID == 0) return false;
System.Reflection.PropertyInfo comparePropertyInfo = (cmbStepThreeField.SelectedItem as PropInfo).PropertyInfo;
System.Reflection.PropertyInfo currentInfo = rec.GetType().GetProperty(comparePropertyInfo.Name);
switch (op)
{
case CompareOperator.EQ:
return compareValue.ToString() == currentInfo.GetValue(rec).ToString();
case CompareOperator.GT:
return compareValue.ToString().CompareTo(currentInfo.GetValue(rec).ToString()) > 0;
case CompareOperator.GTE:
return compareValue.ToString().CompareTo(currentInfo.GetValue(rec).ToString()) >= 0;
case CompareOperator.LT:
return compareValue.ToString().CompareTo(currentInfo.GetValue(rec).ToString()) < 0;
case CompareOperator.LTE:
return compareValue.ToString().CompareTo(currentInfo.GetValue(rec).ToString()) <= 0;
case CompareOperator.NE:
return compareValue.ToString() != currentInfo.GetValue(rec).ToString();
case CompareOperator.REGEX:
object curValAsString = currentInfo.GetValue(rec);
if (curValAsString == null)
return false;
return comparer.IsMatch(curValAsString.ToString());
default:
return false;
}
}).ToList();
filtered.ForEach(rec =>
{
if (!changes)
changes = true;
int index = rec.ID - 1;
rtfOutput.Text += "Found matching item " + rec.Name + " (" + rec.ID + ")\n";
rtfOutput.Text += " replacing " + pi.Name + " (currently " + pi.GetValue(rec).ToString() + ") with new value " + newValue.ToString() + "\n";
rtfOutput.SelectionStart = rtfOutput.TextLength;
rtfOutput.ScrollToCaret();
object setter;
//enums are special: convert them to object
if (pi.PropertyType == typeof(ItemType) ||
pi.PropertyType == typeof(ItemSubType) ||
pi.PropertyType == typeof(ItemSpecial) ||
pi.PropertyType == typeof(ItemSize))
setter = Convert.ChangeType(Enum.ToObject(pi.PropertyType, newValue), pi.PropertyType);
else
setter = Convert.ChangeType(newValue, pi.PropertyType);
pi.SetValue(rec, setter);
//eif.ReplaceRecordAt(index, rec); //todo: way to modify pub files
});
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
using(OpenFileDialog ofd = new OpenFileDialog())
{
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
ofd.AddExtension = true;
ofd.Filter = "Endless Online Item Data File|*.eif";
ofd.Multiselect = false;
ofd.InitialDirectory = Application.ExecutablePath;
if (ofd.ShowDialog() == DialogResult.Cancel)
return;
txtFileName.Text = ofd.FileName;
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
var deserializer = new PubFileSerializer(new NumberEncoderService(), new PubRecordSerializer(new NumberEncoderService()));
_fname = "";
try
{
var fileBytes = File.ReadAllBytes(_fname = string.IsNullOrEmpty(txtFileName.Text) ? PubFileNameConstants.PathToEIFFile : txtFileName.Text);
eif = deserializer.DeserializeFromByteArray(fileBytes, () => new EIFFile());
lblFileName.Text = "Loaded file: " + _fname;
grpStepTwo.Enabled = true;
btnReset.Enabled = true;
}
catch(Exception ex)
{
eif = null;
MessageBox.Show("Error loading " + _fname + ":\n" + ex.Message, "Error!");
}
}
private void cmbStepTwoField_SelectedIndexChanged(object sender, EventArgs e)
{
cmbStepTwoValue.Items.Clear();
txtStepTwoValue.Enabled = false;
cmbStepTwoValue.Enabled = true;
switch ((cmbStepTwoField.SelectedItem as PropInfo).DisplayName)
{
case "Type":
foreach (string val in Enum.GetNames(typeof(ItemType)))
cmbStepTwoValue.Items.Add(val);
break;
case "SubType":
foreach (string val in Enum.GetNames(typeof(ItemSubType)))
cmbStepTwoValue.Items.Add(val);
break;
case "Special":
foreach (string val in Enum.GetNames(typeof(ItemSpecial)))
cmbStepTwoValue.Items.Add(val);
break;
case "Size":
foreach (string val in Enum.GetNames(typeof(ItemSize)))
cmbStepTwoValue.Items.Add(val);
break;
default:
txtStepTwoValue.Enabled = true;
cmbStepTwoValue.Enabled = false;
break;
}
}
private void LeaveStepTwo(object sender, EventArgs e)
{
if(cmbStepTwoField.SelectedIndex >= 0 &&
((txtStepTwoValue.Enabled && txtStepTwoValue.TextLength > 0) || (cmbStepTwoValue.Enabled && cmbStepTwoValue.SelectedIndex >= 0))
)
{
grpStepThree.Enabled = true;
btnProcess.Enabled = true;
}
else
{
grpStepThree.Enabled = false;
btnProcess.Enabled = false;
}
}
private void chkFilterOn_CheckedChanged(object sender, EventArgs e)
{
if(chkFilterOn.Checked)
{
cmbStepThreeField.Enabled = true;
txtCompareValue.Enabled = true;
cmbCompareType.Enabled = true;
cmbCompareType.SelectedIndex = 0;
cmbStepThreeField.SelectedIndex = 0;
}
else
{
cmbStepThreeField.Enabled = false;
txtCompareValue.Enabled = false;
cmbCompareType.Enabled = false;
cmbCompareType.SelectedIndex = -1;
cmbStepThreeField.SelectedIndex = -1;
}
}
private void frmMain_Load(object sender, EventArgs e)
{
MessageBox.Show("It is highly recommended that you back up your pub files before using this utility. Changes are irreversable.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void cmbStepThreeField_SelectedIndexChanged(object sender, EventArgs e)
{
SuspendLayout();
cmbCompareValue.Items.Clear();
cmbCompareValue.Enabled = true;
cmbCompareValue.Visible = true;
txtCompareValue.Enabled = false;
txtCompareValue.Visible = false;
if (cmbStepThreeField.SelectedItem == null)
{
ResumeLayout();
return;
}
switch ((cmbStepThreeField.SelectedItem as PropInfo).PropertyInfo.Name)
{
case "Type":
foreach (string val in Enum.GetNames(typeof(ItemType)))
cmbCompareValue.Items.Add(val);
break;
case "SubType":
foreach (string val in Enum.GetNames(typeof(ItemSubType)))
cmbCompareValue.Items.Add(val);
break;
case "Special":
foreach (string val in Enum.GetNames(typeof(ItemSpecial)))
cmbCompareValue.Items.Add(val);
break;
case "Size":
foreach (string val in Enum.GetNames(typeof(ItemSize)))
cmbCompareValue.Items.Add(val);
break;
default:
cmbCompareValue.Enabled = false;
cmbCompareValue.Visible = false;
txtCompareValue.Enabled = true;
txtCompareValue.Visible = true;
break;
}
ResumeLayout();
}
private void rtfOutput_TextChanged(object sender, EventArgs e)
{
if (rtfOutput.TextLength > 0)
btnClear.Enabled = true;
else
btnClear.Enabled = false;
}
private void btnClear_Click(object sender, EventArgs e)
{
rtfOutput.Text = "";
}
private void btnSave_Click(object sender, EventArgs e)
{
if(changes)
{
DialogResult dr = MessageBox.Show("WARNING: This will update the pub file you currently have open with any batch changes that have been made. Confirm that this behavior is desired.", "Confirm changes?", MessageBoxButtons.YesNo);
if (dr == DialogResult.No)
return;
//var version = eif.Version;
//if (eif.Version == 0)
//{
// version = 1;
// MessageBox.Show("Saving using this tool will update the version of the EIF file.");
//}
try
{
var serializer = new PubFileSerializer(new NumberEncoderService(), new PubRecordSerializer(new NumberEncoderService()));
var bytes = serializer.SerializeToByteArray(eif);
}
catch(Exception ex)
{
MessageBox.Show("Error saving changes to the file:\n" + ex.Message);
return;
}
MessageBox.Show("Changes saved.");
}
else
{
MessageBox.Show("Changes to the pub file were not detected. No changes have been processed.");
}
}
}
public class PropInfo
{
public System.Reflection.PropertyInfo PropertyInfo { get; set; }
public string DisplayName => PropertyInfo.Name;
public PropInfo(System.Reflection.PropertyInfo info)
{
if (info == null)
throw new ArgumentNullException("Info parameter may not be null.");
PropertyInfo = info;
}
public override string ToString()
{
return DisplayName;
}
}
}