OSDN Git Service

各種報告書の表を列単位で検索可能にする
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Model / Status.cs
1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 // \r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 using System;\r
16 using System.Collections.Generic;\r
17 using System.IO;\r
18 using System.Xml.Serialization;\r
19 \r
20 namespace KancolleSniffer.Model\r
21 {\r
22     public interface IHaveState\r
23     {\r
24         bool NeedSave { get; }\r
25         void SaveState(Status status);\r
26         void LoadState(Status status);\r
27     }\r
28 \r
29     public class Status\r
30     {\r
31         public static bool Restoring { get; set; }\r
32         public Achievement Achievement { get; set; }\r
33         public List<MaterialCount> MaterialHistory { get; set; }\r
34         // ReSharper disable once IdentifierTypo\r
35         public double CondRegenTime { get; set; }\r
36         public ExMapInfo.ExMapState ExMapState { get; set; }\r
37         public QuestStatus[] QuestList { get; set; }\r
38         public QuestCount[] QuestCountList { get; set; }\r
39         public DateTime QuestLastReset { get; set; }\r
40 \r
41         private const string FileName = "status.xml";\r
42         private static readonly string StatusFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FileName);\r
43 \r
44         public Status()\r
45         {\r
46             CondRegenTime = double.MinValue;\r
47         }\r
48 \r
49         public void Load()\r
50         {\r
51             try\r
52             {\r
53                 Restoring = true;\r
54                 var serializer = new XmlSerializer(typeof(Status));\r
55                 Status status;\r
56                 using (var file = File.OpenText(StatusFile))\r
57                     status = (Status)serializer.Deserialize(file);\r
58                 foreach (var property in GetType().GetProperties())\r
59                     property.SetValue(this, property.GetValue(status, null), null);\r
60             }\r
61             catch (FileNotFoundException)\r
62             {\r
63             }\r
64             catch (InvalidOperationException ex)\r
65             {\r
66                 throw new Exception(FileName + "が壊れています。", ex);\r
67             }\r
68             finally\r
69             {\r
70                 Restoring = false;\r
71             }\r
72         }\r
73 \r
74         public void Save()\r
75         {\r
76             var serializer = new XmlSerializer(typeof(Status));\r
77             using (var file = File.CreateText(StatusFile + ".tmp"))\r
78                 serializer.Serialize(file, this);\r
79             File.Copy(StatusFile + ".tmp", StatusFile, true);\r
80             File.Delete(StatusFile + ".tmp");\r
81         }\r
82     }\r
83 }