forked from HearthSim/Hearthstone-Deck-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeckList.cs
170 lines (152 loc) · 4.2 KB
/
DeckList.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
#region
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.Utility;
using Hearthstone_Deck_Tracker.Utility.Logging;
#endregion
namespace Hearthstone_Deck_Tracker
{
[XmlRoot(ElementName = "Decks")]
public class DeckList
{
private static Lazy<DeckList> _instance = new Lazy<DeckList>(Load);
private Deck _activeDeck;
[XmlArray(ElementName = "Tags")]
[XmlArrayItem(ElementName = "Tag")]
public List<string> AllTags;
[XmlElement(ElementName = "Deck")]
public ObservableCollection<Deck> Decks;
public List<DeckInfo> LastDeckClass;
public DeckList()
{
Decks = new ObservableCollection<Deck>();
AllTags = new List<string>();
LastDeckClass = new List<DeckInfo>();
}
[XmlIgnore]
public Deck ActiveDeck
{
get { return _activeDeck; }
set
{
if(Equals(_activeDeck, value))
return;
var switchedDeck = _activeDeck != null;
_activeDeck = value;
Core.MainWindow.DeckPickerList.ActiveDeckChanged();
Core.MainWindow.DeckPickerList.RefreshDisplayedDecks();
Log.Info("Set active deck to: " + value);
Config.Instance.ActiveDeckId = value?.DeckId ?? Guid.Empty;
Config.Save();
Core.StatsOverview.ConstructedFilters.UpdateActiveDeckOnlyCheckBox();
Core.StatsOverview.ConstructedGames.UpdateAddGameButton();
if(switchedDeck)
Core.StatsOverview.ConstructedSummary.UpdateContent();
}
}
public Deck ActiveDeckVersion => ActiveDeck?.GetSelectedDeckVersion();
public static DeckList Instance => _instance.Value;
private void LoadActiveDeck()
{
var deck = Decks.FirstOrDefault(d => d.DeckId == Config.Instance.ActiveDeckId);
if(deck != null && deck.Archived)
deck = null;
_activeDeck = deck;
}
private static DeckList Load()
{
#if(!SQUIRREL)
SetupDeckListFile();
#endif
var file = Config.Instance.DataDir + "PlayerDecks.xml";
DeckList instance;
if(!File.Exists(file))
instance = new DeckList();
else
{
try
{
instance = XmlManager<DeckList>.Load(file);
}
catch(Exception ex)
{
Log.Error(ex);
try
{
File.Move(file, Helper.GetValidFilePath(Config.Instance.DataDir, "PlayerDecks_corrupted", "xml"));
}
catch(Exception ex1)
{
Log.Error(ex1);
}
instance = BackupManager.TryRestore<DeckList>("PlayerDecks.xml") ?? new DeckList();
}
}
var save = false;
if(!instance.AllTags.Contains("All"))
{
instance.AllTags.Add("All");
save = true;
}
if(!instance.AllTags.Contains("Favorite"))
{
if(instance.AllTags.Count > 1)
instance.AllTags.Insert(1, "Favorite");
else
instance.AllTags.Add("Favorite");
save = true;
}
if(!instance.AllTags.Contains("None"))
{
instance.AllTags.Add("None");
save = true;
}
if(save)
Save(instance);
instance.LoadActiveDeck();
return instance;
}
#if(!SQUIRREL)
internal static void SetupDeckListFile()
{
if(Config.Instance.SaveDataInAppData == null)
return;
var appDataPath = Path.Combine(Config.AppDataPath, "PlayerDecks.xml");
var dataDirPath = Path.Combine(Config.Instance.DataDirPath, "PlayerDecks.xml");
if(Config.Instance.SaveDataInAppData.Value)
{
if(File.Exists(dataDirPath))
{
if(File.Exists(appDataPath))
//backup in case the file already exists
File.Move(appDataPath, appDataPath + DateTime.Now.ToFileTime());
File.Move(dataDirPath, appDataPath);
Log.Info("Moved decks to appdata");
}
}
else if(File.Exists(appDataPath))
{
if(File.Exists(dataDirPath))
//backup in case the file already exists
File.Move(dataDirPath, dataDirPath + DateTime.Now.ToFileTime());
File.Move(appDataPath, dataDirPath);
Log.Info("Moved decks to local");
}
}
#endif
private static void Save(DeckList instance) => XmlManager<DeckList>.Save(Config.Instance.DataDir + "PlayerDecks.xml", instance);
public static void Save() => Save(Instance);
internal static void Reload() => _instance = new Lazy<DeckList>(Load);
}
public class DeckInfo
{
public string Class;
public Guid Id;
public string Name;
}
}