OSDN Git Service

母港にいるときは戦況を隠す機能を止める
[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         public static bool Restoring { get; set; }\r
32         public Achievement Achievement { get; set; }\r
33         public List<MaterialCount> MaterialHistory { get; set; }\r
34         public double CondRegenTime { get; set; }\r
35         public ExMapInfo.ExMapState ExMapState { get; set; }\r
36         public QuestStatus[] QuestList { get; set; }\r
37         public QuestCount[] QuestCountList { get; set; }\r
38         public DateTime QuestLastReset { get; set; }\r
39 \r
40         private const string FileName = "status.xml";\r
41         private static readonly string StatusFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FileName);\r
42 \r
43         public Status()\r
44         {\r
45             CondRegenTime = double.MinValue;\r
46         }\r
47 \r
48         public void Load()\r
49         {\r
50             try\r
51             {\r
52                 Restoring = true;\r
53                 var serializer = new XmlSerializer(typeof(Status));\r
54                 Status status;\r
55                 using (var file = File.OpenText(StatusFile))\r
56                     status = (Status)serializer.Deserialize(file);\r
57                 foreach (var property in GetType().GetProperties())\r
58                     property.SetValue(this, property.GetValue(status, null), null);\r
59             }\r
60             catch (FileNotFoundException)\r
61             {\r
62             }\r
63             catch (InvalidOperationException ex)\r
64             {\r
65                 throw new Exception(FileName + "が壊れています。", ex);\r
66             }\r
67             finally\r
68             {\r
69                 Restoring = false;\r
70             }\r
71         }\r
72 \r
73         public void Save()\r
74         {\r
75             var serializer = new XmlSerializer(typeof(Status));\r
76             using (var file = File.CreateText(StatusFile + ".tmp"))\r
77                 serializer.Serialize(file, this);\r
78             if (File.Exists(StatusFile))\r
79                 File.Delete(StatusFile);\r
80             File.Move(StatusFile + ".tmp", StatusFile);\r
81         }\r
82     }\r
83 }