-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
105 lines (93 loc) · 3.36 KB
/
MainWindow.xaml.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
using fNbt;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace NBTViewer
{
public partial class MainWindow : Window
{
private readonly ObservableCollection<FileTab> tabList = new ObservableCollection<FileTab>();
private static readonly string[] regionFileExtensions = new string[] { ".mca", ".mcapm", ".mcr" };
public MainWindow()
{
InitializeComponent();
tabControl.ItemsSource = tabList;
}
private void NBTPanel_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
openNbtFileInViewer(files[0]);
}
}
private void openNbtFileInViewer(string path)
{
NbtCompound rootTag = null;
try
{
if (Array.Exists(regionFileExtensions, e => e == Path.GetExtension(path)))
{
rootTag = NbtTreeContainer.loadRegionFile(path);
}
else
{
rootTag = NbtTreeContainer.loadNbtFile(path);
}
}
catch (Exception ex)
{
MessageBox.Show("An error occured: " + ex.ToString() + "\n" + ex.StackTrace, "Error parsing NBT", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
}
if (rootTag != null)
{
TreeViewItem treeRoot = new TreeViewItem();
foreach (NbtTag tag in rootTag)
{
if (tag is ICollection<NbtTag>)
{
ThreadPool.QueueUserWorkItem(delegate
{
Dispatcher.BeginInvoke(DispatcherPriority.Background, (ThreadStart)delegate
{
treeRoot.Items.Add(NbtTreeContainer.collectionItem(tag));
});
});
}
else
{
treeRoot.Items.Add(NbtTreeContainer.valueItem(tag));
}
}
FileTab tab = new FileTab(treeRoot,
String.Format("{0} ({1} {2})", Path.GetFileName(path), rootTag.Count, rootTag.Count == 1 ? "tag" : "tags"));
tabList.Add(tab);
tabControl.SelectedItem = tab;
}
}
private RelayCommand closeTabCommand;
public RelayCommand CloseTabCommand
{
get
{
if (closeTabCommand == null)
{
closeTabCommand = new RelayCommand(w => CloseCommandHandler(w), null);
}
return closeTabCommand;
}
}
private void CloseCommandHandler(object parameter)
{
tabControl.ClearValue(ItemsControl.ItemsSourceProperty);
tabList.Remove(tabList.Where(w => w.header.Equals(parameter as string)).Single());
tabControl.ItemsSource = tabList;
}
}
}