-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrmLoadPgnGames.xaml.cs
204 lines (187 loc) · 8.13 KB
/
FrmLoadPgnGames.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using SrcChess2.PgnParsing;
namespace SrcChess2 {
/// <summary>
/// Interaction logic for frmLoadPGNGames.xaml
/// </summary>
public partial class FrmLoadPgnGames : Window {
/// <summary>Processed file name</summary>
private readonly string m_fileName;
/// <summary>Task used to process the file</summary>
private Task<bool>? m_task;
/// <summary>Actual phase</summary>
private PgnParser.ParsingPhase m_phase;
/// <summary>PGN parsing result</summary>
private bool m_result;
/// <summary>Private delegate</summary>
private delegate void DelProgressCallBack(PgnParser.ParsingPhase phase, int fileIndex, int fileCount, string? fileName, int gameDone, int gameCount);
/// <summary>
/// Ctor
/// </summary>
public FrmLoadPgnGames() {
InitializeComponent();
m_fileName = "";
}
/// <summary>
/// Ctor
/// </summary>
/// <param name="fileName"> File name to be loaded</param>
public FrmLoadPgnGames(string fileName) : this() {
InitializeComponent();
m_fileName = fileName;
Loaded += PgnParsing_Loaded;
Unloaded += PgnParsing_Unloaded;
}
/// <summary>
/// Called when the windows is loaded
/// </summary>
/// <param name="sender"> Sender object</param>
/// <param name="e"> Event arguments</param>
private void PgnParsing_Loaded(object sender, RoutedEventArgs e) {
ProgressBar.Start();
StartProcessing();
}
/// <summary>
/// Called when the windows is closing
/// </summary>
/// <param name="sender"> Sender object</param>
/// <param name="e"> Event arguments</param>
private void PgnParsing_Unloaded(object sender, RoutedEventArgs e) => ProgressBar.Stop();
/// <summary>
/// List of PGN games read from the file
/// </summary>
public List<PgnGame>? PgnGames { get; private set; }
/// <summary>
/// PGN Parser
/// </summary>
public PgnParser? PgnParser { get; private set; }
/// <summary>
/// Total number of games skipped
/// </summary>
public int TotalSkipped { get; private set; }
/// <summary>
/// Total number of games truncated
/// </summary>
public int TotalTruncated { get; private set; }
/// <summary>
/// Error if any
/// </summary>
public string? Error { get; private set; }
/// <summary>
/// Cancel the parsing job
/// </summary>
/// <param name="sender"> Sender object</param>
/// <param name="e"> Event arguments</param>
private void ButCancel_Click(object sender, RoutedEventArgs e) {
butCancel.IsEnabled = false;
PgnParser.CancelParsingJob();
}
/// <summary>
/// Progress bar
/// </summary>
/// <param name="phase"> Phase</param>
/// <param name="fileIndex"> File index</param>
/// <param name="fileCount"> File count</param>
/// <param name="fileName"> File name</param>
/// <param name="gameDone"> Games processed since the last call</param>
/// <param name="gameCount"> Game count</param>
private void WndCallBack(PgnParser.ParsingPhase phase, int fileIndex, int fileCount, string? fileName, int gameDone, int gameCount) {
if (m_phase != phase) {
switch (phase) {
case PgnParser.ParsingPhase.OpeningFile:
ctlPhase.Content = "Openning the file";
ctlFileBeingProcessed.Content = System.IO.Path.GetFileName(fileName ?? throw new ArgumentNullException(nameof(fileName)));
ctlStep.Content = "";
break;
case PgnParser.ParsingPhase.ReadingFile:
ctlPhase.Content = "Reading the file content into memory";
ctlStep.Content = "";
break;
case PgnParser.ParsingPhase.RawParsing:
ctlPhase.Content = "Parsing the PGN";
ctlStep.Content = $"0 / {gameCount} mb";
break;
case PgnParser.ParsingPhase.Finished:
ctlPhase.Content = "Done";
break;
default:
break;
}
m_phase = phase;
}
switch (phase) {
case PgnParser.ParsingPhase.OpeningFile:
break;
case PgnParser.ParsingPhase.ReadingFile:
ctlPhase.Content = "Reading the file content into memory";
break;
case PgnParser.ParsingPhase.RawParsing:
ctlStep.Content = $"{gameDone} / {gameCount} mb";
break;
case PgnParser.ParsingPhase.Finished:
if (PgnParser.IsJobCancelled) {
DialogResult = false;
} else {
DialogResult = m_result;
}
break;
default:
break;
}
}
/// <summary>
/// Progress bar
/// </summary>
/// <param name="cookie"> Cookie</param>
/// <param name="phase"> Phase</param>
/// <param name="fileIndex"> File index</param>
/// <param name="fileCount"> File count</param>
/// <param name="fileName"> File name</param>
/// <param name="gameProcessed"> Games processed since the last call</param>
/// <param name="gameCount"> Game count</param>
static void ProgressCallBack(object? cookie, PgnParser.ParsingPhase phase, int fileIndex, int fileCount, string? fileName, int gameProcessed, int gameCount) {
FrmLoadPgnGames frm;
DelProgressCallBack del;
frm = (FrmLoadPgnGames)cookie!;
del = frm.WndCallBack;
frm.Dispatcher.Invoke(del, System.Windows.Threading.DispatcherPriority.Normal, new object[] { phase, fileIndex, fileCount, fileName! /*can be null, compiler complain*/, gameProcessed, gameCount });
}
/// <summary>
/// Load the PGN games from the specified file
/// </summary>
/// <returns></returns>
private bool LoadPgn() {
bool retVal;
int totalSkipped = 0;
try {
TotalTruncated = 0;
Error = null;
m_phase = PgnParser.ParsingPhase.None;
PgnParser = new PgnParser(isDiagnoseOn: false);
retVal = PgnParser.InitFromFile(m_fileName);
if (retVal) {
PgnGames = PgnParser.GetAllRawPgn(getAttrList: true,
getMoveList: false,
out totalSkipped,
(cookie, phase, fileIndex, fileCount, fileName, gameProcessed, gameCount) => { ProgressCallBack(cookie, phase, fileIndex, fileCount, fileName, gameProcessed, gameCount); },
this);
retVal = PgnGames != null;
}
TotalSkipped = totalSkipped;
} catch(Exception ex) {
MessageBox.Show(ex.Message);
retVal = false;
}
m_result = retVal;
ProgressCallBack(this, PgnParser.ParsingPhase.Finished, fileIndex: 0, fileCount: 0, fileName: null, gameProcessed: 0, gameCount: 0);
return retVal;
}
/// <summary>
/// Start the job
/// </summary>
private void StartProcessing() => m_task = Task<bool>.Factory.StartNew(() => { return LoadPgn(); });
}
}