OSDN Git Service

2016夏イベのE-1のボス戦でエラーになるのを直す
[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                 ReadOldStatus();\r
60             }\r
61             finally\r
62             {\r
63                 Restoring = false;\r
64             }\r
65         }\r
66 \r
67         public void Save()\r
68         {\r
69             var serializer = new XmlSerializer(typeof(Status));\r
70             using (var file = File.CreateText(_statusFileName))\r
71                 serializer.Serialize(file, this);\r
72         }\r
73 \r
74         public void ReadOldStatus()\r
75         {\r
76             var old = Path.Combine(_baseDir, "status.json");\r
77             dynamic json;\r
78             try\r
79             {\r
80                 json = JsonParser.Parse(File.ReadAllText(old));\r
81             }\r
82             catch (FileNotFoundException)\r
83             {\r
84                 return;\r
85             }\r
86             var ac = json.Achievement;\r
87             Achievement = new Achievement\r
88             {\r
89                 Start = (int)ac.Start,\r
90                 StartOfMonth = (int)ac.StartOfMonth,\r
91                 LastReset = DateTime.Parse(ac.LastReset),\r
92                 LastResetOfMonth = DateTime.Parse(ac.LastResetOfMonth),\r
93                 ResetHours = new List<int>((int[])ac.ResetHours),\r
94             };\r
95             var history = new List<MaterialCount>();\r
96             foreach (var h in json.MatreialHistory)\r
97             {\r
98                 history.Add(new MaterialCount\r
99                 {\r
100                     BegOfDay = (int)h.BegOfDay,\r
101                     BegOfWeek = (int)h.BegOfWeek,\r
102                     Now = (int)h.Now,\r
103                     LastSet = DateTime.Parse(h.LastSet)\r
104                 });\r
105             }\r
106             MaterialHistory = history;\r
107             CondRegenTime = json.CondRegenTime;\r
108             ExMapState = new ExMapInfo.ExMapState();\r
109             var clear = new List<ExMapInfo.ClearStatus>();\r
110             foreach (var cs in json.ExMapState.ClearStatusList)\r
111             {\r
112                 clear.Add(new ExMapInfo.ClearStatus\r
113                 {\r
114                     Map = (int)cs.Map,\r
115                     Cleared = cs.Cleared,\r
116                     Rate = (int)cs.Rate,\r
117                 });\r
118             }\r
119             ExMapState.ClearStatusList = clear;\r
120             Save();\r
121         }\r
122     }\r
123 }