forked from neophoenix236/INVedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
447 lines (410 loc) · 14.4 KB
/
MainForm.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using System.Net;
using System.ComponentModel;
using System.Diagnostics;
using NBT;
namespace INVedit
{
public partial class MainForm : Form
{
static string appdata;
static MainForm() {
if (Platform.Current == Platform.Windows)
appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+"/.minecraft";
else if (Platform.Current == Platform.Mac)
appdata = Environment.GetEnvironmentVariable("HOME")+"/Library/Application Support/minecraft";
else
appdata = Environment.GetEnvironmentVariable("HOME")+"/.minecraft";
}
List<CheckBox> groups = new List<CheckBox>();
string url = "http://copy.mcft.net/mc/INVedit";
WebClient client = new WebClient();
List<string> download;
List<byte[]> files;
int current;
public MainForm(string[] files)
{
InitializeComponent();
Data.Init("items.txt");
client.DownloadStringCompleted += VersionCompleted;
client.DownloadDataCompleted += FileCompleted;
client.DownloadProgressChanged += FileProgress;
boxItems.LargeImageList = Data.list;
boxItems.ItemDrag += ItemDrag;
foreach (Data.Group group in Data.groups.Values) {
CheckBox box = new CheckBox();
box.Size = new Size(26, 26);
box.Location = new Point(Width-189 + (groups.Count / 12) * 27, 29 + (groups.Count % 12) * 27);
box.ImageList = Data.list;
box.ImageIndex = group.imageIndex;
box.Appearance = Appearance.Button;
box.Checked = true;
box.Tag = group;
box.MouseDown += ItemMouseDown;
Controls.Add(box);
groups.Add(box);
}
Width += ((groups.Count-1) / 12) * 27;
UpdateItems();
foreach (string file in files)
if (File.Exists(file)) Open(file);
}
void Open(string file)
{
Page page = new Page();
Open(page,file);
tabControl.TabPages.Add(page);
tabControl.SelectedTab = page;
}
void Open(Page page, string file)
{
try {
FileInfo info = new FileInfo(file);
page.file = info.FullName;
if (info.Name == "level.dat") { page.Text = info.Directory.Name; }
else { page.Text = info.Name; }
Text = "INVedit - "+page.Text;
page.changed = false;
btnSave.Enabled = true;
btnCloseTab.Enabled = true;
btnReload.Enabled = true;
Tag tag = NBT.Tag.Load(file);
if (tag.Type==TagType.Compound && tag.Contains("Data")) { tag = tag["Data"]; }
if (tag.Type==TagType.Compound && tag.Contains("Player")) { tag = tag["Player"]; }
if (tag.Type==TagType.Compound && tag.Contains("Inventory")) { tag = tag["Inventory"]; }
if (tag.Name != "Inventory") { throw new Exception("Can't find Inventory tag."); }
Inventory.Load(tag, page.slots);
} catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
void Save(Page page, string file)
{
try {
FileInfo info = new FileInfo(file);
if (info.Exists && page.file != info.FullName) {
string str;
if (info.Name == "level.dat")
str = "Are you sure you want to overwrite "+info.Directory.Name+"?";
else str = "Are you sure you want to overwrite '"+info.Name+"'?";
DialogResult result = MessageBox.Show(str, "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes) return;
}
page.file = info.FullName;
Tag root,tag;
if (info.Exists) {
root = NBT.Tag.Load(page.file);
tag = root;
} else {
if (info.Extension.ToLower() == ".dat") {
MessageBox.Show("You can't create a new Minecraft level file.\n"+
"Select an existing one instead.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
} root = NBT.Tag.Create("Inventory");
tag = root;
} if (tag.Type==TagType.Compound && tag.Contains("Data")) { tag = tag["Data"]; }
if (tag.Type==TagType.Compound && tag.Contains("Player")) { tag = tag["Player"]; }
if (root.Name!="Inventory" && (tag.Type!=TagType.Compound || !tag.Contains("Inventory"))) { throw new Exception("Can't find Inventory tag."); }
Inventory.Save(tag, page.slots);
root.Save(page.file);
if (info.Name == "level.dat") { page.Text = info.Directory.Name; }
else { page.Text = info.Name; }
Text = "INVedit - "+page.Text;
page.changed = false;
btnReload.Enabled = true;
} catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
protected override void OnDragEnter(DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
string[] files = ((string[])e.Data.GetData(DataFormats.FileDrop));
foreach (string file in files) {
FileInfo info = new FileInfo(file);
if (info.Extension.ToLower() == ".inv" || info.Extension.ToLower() == ".dat")
e.Effect = DragDropEffects.Copy;
}
}
}
protected override void OnDragDrop(DragEventArgs e) {
OnDragEnter(e);
BringToFront();
if (e.Effect == DragDropEffects.None) return;
string[] files = ((string[])e.Data.GetData(DataFormats.FileDrop));
foreach (string file in files)
if (File.Exists(file)) Open(file);
}
void UpdateItems()
{
boxItems.BeginUpdate();
boxItems.Clear();
if (boxSearch.Text == "" || boxSearch.Font.Italic) {
foreach (CheckBox box in groups) if (box.Checked)
foreach (Data.Item item in ((Data.Group)box.Tag).items)
boxItems.Items.Add(new ListViewItem(item.name, item.imageIndex){ Tag = new Item(item.id, 0, 0, item.damage) });
} else {
short id;
if (short.TryParse(boxSearch.Text, out id)) {
if (Data.items.ContainsKey(id))
foreach (Data.Item item in Data.items[id].Values)
boxItems.Items.Add(new ListViewItem(item.name, item.imageIndex){ Tag = new Item(item.id, 0, 0, item.damage) });
else boxItems.Items.Add(new ListViewItem("Unknown item "+id, 0){ Tag = new Item(id) });
} else foreach (CheckBox box in groups) if (box.Checked)
foreach (Data.Item item in ((Data.Group)box.Tag).items)
if (item.name.IndexOf(boxSearch.Text, StringComparison.InvariantCultureIgnoreCase) >= 0)
boxItems.Items.Add(new ListViewItem(item.name, item.imageIndex){ Tag = new Item(item.id, 0, 0, item.damage) });
}
boxItems.EndUpdate();
}
void ItemMouseDown(object sender, MouseEventArgs e)
{
CheckBox self = (CheckBox)sender;
bool changed = false;
if (e.Button == MouseButtons.Left) {
bool other = true;
foreach (CheckBox box in groups)
if (box.Checked == (self!=box))
other = false;
foreach (CheckBox box in groups)
if (box.Checked == (self!=box) || other) {
changed = true;
box.Checked = (self==box) || other;
}
} else if (e.Button == MouseButtons.Right) {
self.Checked = !self.Checked;
changed = true;
} else return;
self.Select();
if (changed) UpdateItems();
if (e.Button == MouseButtons.Left)
self.Checked = !self.Checked;
}
void BoxSearchEnter(object sender, EventArgs e)
{
if (!boxSearch.Font.Italic) return;
boxSearch.Font = new Font(boxSearch.Font, FontStyle.Regular);
boxSearch.ForeColor = SystemColors.ControlText;
boxSearch.Text = "";
}
void BoxSearchLeave(object sender, EventArgs e)
{
if (boxSearch.Text != "") return;
boxSearch.Font = new Font(boxSearch.Font, FontStyle.Italic);
boxSearch.ForeColor = Color.Gray;
boxSearch.Text = "Search...";
}
void BoxSearchTextChanged(object sender, EventArgs e)
{
UpdateItems();
}
void ItemDrag(object sender, ItemDragEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
Item item = (Item)((ListViewItem)e.Item).Tag;
item = new Item(item.ID, item.Stack, 0, item.Damage);
DoDragDrop(item, DragDropEffects.Copy | DragDropEffects.Move);
}
void BtnNewClick(object sender, EventArgs e)
{
Page page = new Page();
page.Text = "unnamed.inv";
Text = "INVedit - unnamed.inv";
tabControl.TabPages.Add(page);
tabControl.SelectedTab = page;
btnSave.Enabled = true;
btnCloseTab.Enabled = true;
btnReload.Enabled = false;
}
void BtnOpenClick(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
Open(openFileDialog.FileName);
}
void BtnSaveClick(object sender, EventArgs e)
{
Page page = (Page)tabControl.SelectedTab;
saveFileDialog.FileName = page.file;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
Save(page, saveFileDialog.FileName);
}
void BtnReloadClick(object sender, EventArgs e)
{
if (((Page)tabControl.SelectedTab).changed) {
DialogResult result = MessageBox.Show(
"Are you sure you want to reload this tab?\n"+
"All unsaved changes will be lost.", "Question",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes) return;
}
try {
Page page = (Page)tabControl.SelectedTab;
Open(page, page.file);
} catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
void BtnCloseTabClick(object sender, EventArgs e)
{
if (((Page)tabControl.SelectedTab).changed) {
DialogResult result = MessageBox.Show(
"Are you sure you want to close this tab?\n"+
"All unsaved changes will be lost.", "Question",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes) return;
}
tabControl.TabPages.Remove(tabControl.SelectedTab);
if (tabControl.TabPages.Count == 0) {
btnSave.Enabled = false;
btnCloseTab.Enabled = false;
}
}
void BtnAboutClick(object sender, EventArgs e)
{
new AboutForm().ShowDialog();
}
void MainFormFormClosing(object sender, FormClosingEventArgs e)
{
foreach (TabPage page in tabControl.TabPages)
if (((Page)page).changed) {
DialogResult result = MessageBox.Show(
"Are you sure you want to close INVedit?\n"+
"There still are unsaved inventory tabs.", "Question",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes) e.Cancel = true;
break;
}
}
void TabControlDragOver(object sender, DragEventArgs e)
{
Point point = tabControl.PointToClient(new Point(e.X, e.Y));
TabPage hover = null;
for (int i = 0; i < tabControl.TabPages.Count; ++i)
if (tabControl.GetTabRect(i).Contains(point)) {
hover = tabControl.TabPages[i];
break;
}
if (hover == null) return;
if (!e.Data.GetDataPresent(typeof(Item))) return;
tabControl.SelectedTab = hover;
}
void TabControlSelected(object sender, TabControlEventArgs e)
{
if (e.TabPage != null) {
Text = "INVedit - "+e.TabPage.Text;
btnReload.Enabled = (((Page)e.TabPage).file != null);
} else {
Text = "INVedit - Minecraft Inventory Editor";
btnReload.Enabled = false;
}
}
void BtnOpenDropDownOpening(object sender, EventArgs e)
{
btnOpen.DropDownItems.Clear();
ResourceManager resources = new ResourceManager("INVedit.Resources", GetType().Assembly);
Image world = (Image)resources.GetObject("world");
DirectoryInfo dirs = new DirectoryInfo(appdata+"/saves");
if (dirs.Exists) foreach (DirectoryInfo dir in dirs.GetDirectories()) {
if (dir.GetFiles("level.dat").Length > 0) {
ToolStripItem item = btnOpen.DropDownItems.Add("Open from „"+dir.Name+"“", world);
item.Tag = dir.FullName+"/level.dat";
}
}
}
void BtnOpenDropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
Open((string)e.ClickedItem.Tag);
}
void BtnSaveDropDownOpening(object sender, EventArgs e)
{
btnSave.DropDownItems.Clear();
ResourceManager resources = new ResourceManager("INVedit.Resources", GetType().Assembly);
Image world = (Image)resources.GetObject("world");
DirectoryInfo dirs = new DirectoryInfo(appdata+"/saves");
if (dirs.Exists) foreach (DirectoryInfo dir in dirs.GetDirectories()) {
if (dir.GetFiles("level.dat").Length > 0) {
ToolStripItem item = btnSave.DropDownItems.Add("Save to „"+dir.Name+"“", world);
item.Tag = dir.FullName+"/level.dat";
}
}
}
void BtnSaveDropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
Page page = (Page)tabControl.SelectedTab;
Save(page, (string)e.ClickedItem.Tag);
}
void BtnUpdateClick(object sender, EventArgs e)
{
switch (btnUpdate.Text) {
case "Check for updates":
btnUpdate.Text = "Checking ...";
btnUpdate.Enabled = false;
client.DownloadStringAsync(new Uri(url+"/version"));
break;
case "Download":
btnUpdate.Text = "";
btnUpdate.Enabled = false;
barUpdate.Visible = true;
barUpdate.Value = 0;
barUpdate.Maximum = download.Count*100;
int current = 0;
files = new List<byte[]>();
Uri uri = new Uri(url+"/"+download[current]);
client.DownloadDataAsync(uri);
break;
case "Restart":
for (int i=0;i<download.Count;++i) {
string name = download[i];
byte[] data = files[i];
if (name == "INVedit.exe" ||
name == "NBT.dll") { name = "_"+name; }
File.WriteAllBytes(name,data);
} if (File.Exists("_INVedit.exe")) {
Process.Start("_INVedit.exe","-update");
} else { Process.Start("INVedit.exe","-update"); }
Application.Exit();
break;
}
}
void VersionCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error!=null) {
btnUpdate.Text = "Error while checking for updates";
return;
} int version = 1;
string[] lines = e.Result.Split(new string[]{ "\r\n" }, StringSplitOptions.None);
download = new List<string>();
foreach (string line in lines) {
if (line == "") ++version;
else if (version > Data.version &&
!download.Contains(line)) download.Add(line);
} if (download.Count > 0) {
btnUpdate.Text = "Download";
btnUpdate.Enabled = true;
} else {
btnUpdate.Text = "No update available";
}
}
void FileProgress(object sender, DownloadProgressChangedEventArgs e)
{
barUpdate.Value = current*100 + e.ProgressPercentage;
}
void FileCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (e.Error!=null) {
btnUpdate.Text = "Download";
btnUpdate.Enabled = true;
barUpdate.Visible = false;
throw e.Error;
} files.Add(e.Result);
++current;
if (current == download.Count) {
btnUpdate.Text = "Restart";
btnUpdate.Enabled = true;
barUpdate.Visible = false;
} else {
Uri uri = new Uri(url+"/"+download[current]);
client.DownloadDataAsync(uri);
}
}
}
}