-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
206 lines (172 loc) · 6.91 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
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
namespace ReplaysOfTheVoid
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ReplayReader m_replayreader;
private ConcurrentQueue<Replay> m_currentReplayList = null;
private ConcurrentQueue<Tuple<string, string>> m_failedToLoadReplays = null;
private DispatcherTimer m_uiTimer;
private Task[] m_tasks = null;
public MainWindow()
{
InitializeComponent();
m_uiTimer = new DispatcherTimer();
m_uiTimer.Interval = new TimeSpan(100);
m_uiTimer.Tick += UiTimer_Tick;
// by default fill in user/docs/sc2beta
edReplayPath.Text =
System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") +
"\\StarCraft II Beta\\";
m_replayreader = new ReplayReader();
}
private void UiTimer_Tick(object sender, EventArgs e)
{
if ((m_currentReplayList != null) && (m_tasks != null))
{
// update progressbar
pbMain.Value = m_currentReplayList.Count();
// check if all tasks are completed
if (Task.WaitAll(m_tasks, 10))
{
m_uiTimer.Stop();
pbMain.Value = pbMain.Maximum;
// sort replays by date and show them in the UI
gridReplays.ItemsSource = m_currentReplayList.OrderBy(o => o.ReplayDate).ToList();
// we can now rename then
btnRenameAll.IsEnabled = true;
btnInvertCheckboxes.IsEnabled = true;
btnParse.IsEnabled = true;
if (m_failedToLoadReplays.Count() > 0)
{
MessageBox.Show(m_failedToLoadReplays.Count() + " replays failed to load");
}
// todo: do something with the information in FailedToLoadReplays
}
}
}
/// <summary>
/// Parse button click implementation
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnParse_Click(object sender, RoutedEventArgs e)
{
btnParse.IsEnabled = false;
pbMain.Value = 0;
m_currentReplayList = new ConcurrentQueue<Replay>();
m_failedToLoadReplays = new ConcurrentQueue<Tuple<string, string>>();
string replaypath = edReplayPath.Text;
// list all files recursively from replaypath
var dirinfo = new DirectoryInfo(replaypath);
var files =
dirinfo.EnumerateFiles(
"*.SC2Replay",
SearchOption.AllDirectories
);
pbMain.Maximum = files.Count();
var format = edFormat.Text;
var i = 0;
m_tasks = new Task[files.Count()];
foreach (var file in files)
{
// load replays as threaded tasks
m_tasks[i] = Task.Factory.StartNew(() =>
{
// parse replay file and add to list
try
{
var replay = m_replayreader.ParseReplay(file.DirectoryName + "\\", file.Name, file.CreationTime, format);
m_currentReplayList.Enqueue(replay);
}
catch (Exception except)
{
// ignore exception, just don't add to list
m_failedToLoadReplays.Enqueue(
new Tuple<string, string>(file.DirectoryName + "\\" + file.Name, except.Message)
);
}
});
i++;
}
// start timer that checks status of all the running tasks
m_uiTimer.Start();
}
/// <summary>
/// Rename button click implementation
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRenameAll_Click(object sender, RoutedEventArgs e)
{
int renamecount = 0;
// todo: can we run this threaded like we did with parsing all the replays
foreach (var replay in m_currentReplayList)
{
// if user checked the box to rename
if (replay.DoRename)
{
var renameto = replay.RenameTo;
// if it isn't already the same name
if (replay.Filename != renameto)
{
// if filename is a duplicate, try a different name (by appending a count)
var duplicatecount = 1;
while (File.Exists(replay.Directory + renameto) && (replay.Filename != renameto))
{
duplicatecount++;
renameto = replay.GetAlternateRename(duplicatecount);
}
// rename
if (replay.Filename != renameto)
{
try
{
File.Move(
replay.Directory + replay.Filename,
replay.Directory + renameto
);
renamecount++;
}
catch (Exception)
{
// ignore
// todo: what to do with this?
}
}
}
}
}
MessageBox.Show("Replays renamed: " + renamecount.ToString());
}
private void btnInvertCheckboxes_Click(object sender, RoutedEventArgs e)
{
gridReplays.ItemsSource = null;
foreach (var replay in m_currentReplayList)
{
replay.DoRename = !replay.DoRename;
}
gridReplays.ItemsSource = m_currentReplayList.OrderBy(o => o.ReplayDate).ToList();
}
}
}