/
DeckPackageProcessor.cs
314 lines (300 loc) · 14.7 KB
/
DeckPackageProcessor.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Windows.Storage;
namespace QuizCards
{
class DeckPackageProcessor
{
public String packagePath;
public Deck deck;
private String deckXml;
private bool deckDescriptionFound = false;
public DeckPackageProcessor()
{
this.deck = new Deck();
this.packagePath = null;
}
public DeckPackageProcessor(String p)
{
this.deck = new Deck();
this.packagePath = p;
}
public async Task<bool> readPackageAsync(StorageFile file)
{
this.deckDescriptionFound = false;
this.deck.filename = file.Name;
bool result = false;
//Locate Archive based on this.packagePath
var folder = ApplicationData.Current.TemporaryFolder;
//Open Archive
Stream stream = await file.OpenStreamForReadAsync();
using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry en in archive.Entries)
{ //Iterate through Entries
if (en.FullName.Contains("deckdescription.xml"))
{
char[] output = new char[en.Length];
this.deckDescriptionFound = true;
//Open deckdescription.xml and save it into deckXml
using (StreamReader sr = new StreamReader(en.Open()))
{
await sr.ReadAsync(output, 0, (int)en.Length);
//There was some weirdness here, which I'm documenting for the sake of my Memento-esque memory.
//We were hitting all kinds of weird XMLExceptions at the end of certain files (XMLExceptions around the Null character 0x00).
//The UTF-8 files effected always involved some characters that required two bytes. We'd hit this exception after the closing tag ("</deck>")
//When I inspected these files in a Hex editor, there were never any null characters at the end of the file, which was weird.
//My hypothesis was that when we built the output char array using en.Length, that length was returning the length in Bytes, which
//was not the same as the number of characters (since some required 2 bytes). The result was that the output array was longer
//than the string that it was representing, and those extra slots were NULL (excess length was a function of the number of characters
//requiring 2 bytes). Thus, I am trimming the null chars off the end.
//tl;dr: ZipArchiveEntry.Length doesn't actually return the number of characters in the file. This seems obvious in retrospect.
this.deckXml = new String(output).TrimEnd('\0');
}
}
if (en.FullName.EndsWith(".jpg") || en.FullName.EndsWith(".jpeg") || en.FullName.EndsWith(".png"))
{
//Copy Images to LocalStorage - Tmp Folder
using (Stream picdata = en.Open())
{
StorageFile outfile = await folder.CreateFileAsync(en.Name, CreationCollisionOption.ReplaceExisting);
using (Stream outputfilestream = await outfile.OpenStreamForWriteAsync())
{
await picdata.CopyToAsync(outputfilestream);
await outputfilestream.FlushAsync(); //flush makes sure all the bits are written
}
}
}
}
}
//kick off processXml, return result of that.
if (this.deckDescriptionFound)
{
try
{
result = processXml();
}
catch (XmlException e)
{
result = false;
}
}
else
{
result = false;
}
return result;
}
public bool processXml()
{
//Process this.deckXml and:
//Populate Deck [title, sideA and sideB names, description]
//Populate Cards, linking to bitmaps that are loaded in localstorage.
//return true if succeeded
using (XmlReader reader = XmlReader.Create(new StringReader(this.deckXml)))
{
Card currentCard = null;
int tabDepth = 0;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if ((tabDepth == 0))
{
if (reader.Name.Equals("deck"))
{
tabDepth++;
}
else
{
return false;
}
}
else
{
tabDepth++;
if (reader.Name.Equals("title"))
{
this.deck.setTitle(System.Net.WebUtility.HtmlDecode(reader.ReadElementContentAsString()));
Debug.WriteLine("Set deck title to " + this.deck.getTitle());
}
else if (reader.Name.Equals("description"))
{
//SET DESCRIPTION!
Debug.WriteLine("Deck Description: " + System.Net.WebUtility.HtmlDecode(reader.ReadElementContentAsString()));
}
else if (reader.Name.Equals("sideaname"))
{
this.deck.setSideAName(System.Net.WebUtility.HtmlDecode(reader.ReadElementContentAsString()));
}
else if (reader.Name.Equals("sidebname"))
{
this.deck.setSideBName(System.Net.WebUtility.HtmlDecode(reader.ReadElementContentAsString()));
}
else if (reader.Name.Equals("cards"))
{
tabDepth++;
}
else if (reader.Name.Equals("card"))
{
tabDepth++;
currentCard = new Card();
}
else if (reader.Name.Equals("sidealabel"))
{
currentCard.sideALabel = System.Net.WebUtility.HtmlDecode(reader.ReadElementContentAsString());
}
else if (reader.Name.Equals("sideblabel"))
{
currentCard.sideBLabel = System.Net.WebUtility.HtmlDecode(reader.ReadElementContentAsString());
}
else if (reader.Name.Equals("sideaimage"))
{
currentCard.setSideAImage("ms-appdata:///temp/" + System.Net.WebUtility.HtmlDecode(reader.ReadElementContentAsString()));
}
else if (reader.Name.Equals("sidebimage"))
{
currentCard.setSideBImage("ms-appdata:///temp/" + System.Net.WebUtility.HtmlDecode(reader.ReadElementContentAsString()));
}
}
break;
case XmlNodeType.Comment:
break;
case XmlNodeType.Text:
break;
case XmlNodeType.XmlDeclaration:
break;
case XmlNodeType.EndElement:
if (reader.Name.Equals("card") || reader.Name.Equals("cards") || reader.Name.Equals("deck"))
{
tabDepth--;
}
if (reader.Name.Equals("card"))
{
//Card info is ready, add to deck!
this.deck.addCard(currentCard);
}
break;
}
}
Debug.WriteLine("Deck '" + this.deck.getTitle() + "' has " + this.deck.getLength() + " cards.");
}
return true;
}
public async Task<bool> writePackageAsync(StorageFile file, Deck outDeck)
{
this.deck = outDeck;
StorageFolder tmpFolder = ApplicationData.Current.TemporaryFolder;
//create a MemoryStream to build ZipArchive in Memory
using (MemoryStream zipStream = new MemoryStream())
{
//Create ZipArchive using MemoryStream
using (ZipArchive archive = new ZipArchive(zipStream,ZipArchiveMode.Create,true)) { //leave zipstream open so we can copy from it later.
//Write File(s) to ZipArchive as entries
//for every image referenced by a card, we'll need to copy them over
String[] segments;
ZipArchiveEntry en;
foreach (Card c in this.deck.cards)
{
if (c.hasSideAImage())
{
segments = c.sideAImage.UriSource.Segments;
StorageFile inFile = await tmpFolder.GetFileAsync(segments[segments.Count() - 1]); //to be copied
using (Stream s = await inFile.OpenStreamForReadAsync()) //incoming stream
{
en = archive.CreateEntry(segments[segments.Count() - 1]);
using (Stream sout = en.Open()) //outgoing stream. Can't use en.Open() directly because it won't get properly disposed.
{
await s.CopyToAsync(sout);
}
}
}
if (c.hasSideBImage())
{
segments = c.sideBImage.UriSource.Segments;
StorageFile inFile = await tmpFolder.GetFileAsync(segments[segments.Count() - 1]); //to be copied
using (Stream s = await inFile.OpenStreamForReadAsync()) //incoming stream
{
en = archive.CreateEntry(segments[segments.Count() - 1]);
using (Stream sout = en.Open()) //outgoing stream. Can't use en.Open() directly because it won't get properly disposed.
{
await s.CopyToAsync(sout);
}
}
}
}
//Then we write the deck description file
deckToXml();
en = archive.CreateEntry("deckdescription.xml");
using (StreamWriter sw = new StreamWriter(en.Open()))
{
sw.Write(this.deckXml);
}
}
//Copy MemoryStream (entire Zip Archive) to file specified
zipStream.Position = 0;
using (Stream s = await file.OpenStreamForWriteAsync())
{
zipStream.CopyTo(s);
}
}
return true;
}
public bool deckToXml()
{
String[] segments;
if (this.deck != null)
{
StringBuilder s = new StringBuilder();
s.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
s.AppendLine("<deck>");
s.Append("<title>");
s.Append(System.Net.WebUtility.HtmlEncode(this.deck.title));
s.AppendLine("</title>");
s.Append("<description>");
s.Append("");
s.AppendLine("</description>");
s.Append("<sideaname>");
s.Append(System.Net.WebUtility.HtmlEncode(this.deck.sideAName));
s.AppendLine("</sideaname>");
s.Append("<sidebname>");
s.Append(System.Net.WebUtility.HtmlEncode(this.deck.sideBName));
s.AppendLine("</sidebname>");
s.AppendLine("<cards>");
foreach (Card c in this.deck.cards)
{
s.AppendLine("<card>");
s.AppendLine("<sidealabel>" + System.Net.WebUtility.HtmlEncode(c.sideALabel) + "</sidealabel>");
s.AppendLine("<sideblabel>" + System.Net.WebUtility.HtmlEncode(c.sideBLabel) + "</sideblabel>");
if (c.hasSideAImage())
{
segments = c.sideAImage.UriSource.Segments;
s.AppendLine("<sideaimage>"+ System.Net.WebUtility.HtmlEncode(segments[segments.Count()-1]) +"</sideaimage>");
}
if (c.hasSideBImage())
{
segments = c.sideBImage.UriSource.Segments;
s.AppendLine("<sidebimage>" + System.Net.WebUtility.HtmlEncode(segments[segments.Count() - 1]) + "</sidebimage>");
}
s.AppendLine("</card>");
}
s.AppendLine("</cards>");
s.AppendLine("</deck>");
this.deckXml = s.ToString();
return true;
}
else
{
return false;
}
}
}
}