-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlReader.cs
85 lines (75 loc) · 2.91 KB
/
XmlReader.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;
namespace ArenaTracker {
class XmlReader {
public Dictionary<string, List<Card>> loadedCards;
public List<Arena> arenaHistory;
public XmlReader() {
this.loadedCards = new Dictionary<string,List<Card>>();
this.loadedCards.Add("Neutral", loadCards("neutral"));
this.loadedCards.Add("Druid", loadCards("druid"));
this.loadedCards.Add("Hunter", loadCards("hunter"));
this.loadedCards.Add("Mage", loadCards("mage"));
this.loadedCards.Add("Paladin", loadCards("paladin"));
this.loadedCards.Add("Priest", loadCards("priest"));
this.loadedCards.Add("Rogue", loadCards("rogue"));
this.loadedCards.Add("Shaman", loadCards("shaman"));
this.loadedCards.Add("Warlock", loadCards("warlock"));
this.loadedCards.Add("Warrior", loadCards("warrior"));
}
public List<Card> loadCards(string hero) {
try {
string filename = "cards/" + hero + ".xml";
XmlSerializer xs = new XmlSerializer(typeof(Collection));
FileStream read = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
Collection coll = (Collection)xs.Deserialize(read);
return coll.Cards.ToList();
} catch (Exception ex) {
MessageBox.Show(ex.Message);
throw;
}
}
public Arena LoadArena(string fileName) {
XmlSerializer deserializer = new XmlSerializer(typeof(Arena));
try {
TextReader tr = new StreamReader(fileName);
Arena arena = (Arena)deserializer.Deserialize(tr);
tr.Close();
return arena;
} catch (Exception ex) {
return null;
}
}
public void SaveArena(Arena arena, string fileName) {
XmlSerializer xs = new XmlSerializer(typeof(Arena));
TextWriter tw = new StreamWriter(fileName);
try {
xs.Serialize(tw, arena);
} catch (Exception e) {
MessageBox.Show(e.Message);
} finally {
tw.Close();
}
}
public void LoadHistory() {
string[] files = Directory.GetFiles("playedArenas/");
foreach (string file in files) {
this.arenaHistory.Add(LoadArena(file));
}
}
}
//helper class for card loading
public class Collection {
public ObservableCollection<Card> Cards { get; set; }
public Collection() {
Cards = new ObservableCollection<Card>();
}
}
}