OSDN Git Service

Pushoverによるプッシュ通知に対応する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / 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\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         private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory;\r
32         private readonly string _statusFileName;\r
33         public static bool Restoring { get; set; }\r
34         public Achievement Achievement { get; set; }\r
35         public List<MaterialCount> MaterialHistory { get; set; }\r
36         public double CondRegenTime { get; set; }\r
37         public ExMapInfo.ExMapState ExMapState { get; set; }\r
38 \r
39         public Status()\r
40         {\r
41             _statusFileName = Path.Combine(_baseDir, "status.xml");\r
42             CondRegenTime = double.MinValue;\r
43         }\r
44 \r
45         public void Load()\r
46         {\r
47             try\r
48             {\r
49                 Restoring = true;\r
50                 var serializer = new XmlSerializer(typeof(Status));\r
51                 Status status;\r
52                 using (var file = File.OpenText(_statusFileName))\r
53                     status = (Status)serializer.Deserialize(file);\r
54                 foreach (var property in GetType().GetProperties())\r
55                     property.SetValue(this, property.GetValue(status, null), null);\r
56             }\r
57             catch (FileNotFoundException)\r
58             {\r
59             }\r
60             finally\r
61             {\r
62                 Restoring = false;\r
63             }\r
64         }\r
65 \r
66         public void Save()\r
67         {\r
68             var serializer = new XmlSerializer(typeof(Status));\r
69             using (var file = File.CreateText(_statusFileName))\r
70                 serializer.Serialize(file, this);\r
71         }\r
72     }\r
73 }