OSDN Git Service

タイマーが残り時間と終了時間のどちらを表示するかを保存する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Config.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.Drawing;\r
18 using System.IO;\r
19 using System.Linq;\r
20 using System.Xml.Serialization;\r
21 \r
22 namespace KancolleSniffer\r
23 {\r
24     public class ProxyConfig\r
25     {\r
26         public const int DefaultListenPort = 8080;\r
27         public bool Auto { get; set; }\r
28         public int Listen { get; set; }\r
29         public bool UseUpstream { get; set; }\r
30         public int UpstreamPort { get; set; }\r
31 \r
32         public ProxyConfig()\r
33         {\r
34             Auto = true;\r
35             Listen = DefaultListenPort;\r
36             UseUpstream = false;\r
37             UpstreamPort = 8888;\r
38         }\r
39     }\r
40 \r
41     public class ShipListConfig\r
42     {\r
43         public bool Visible { get; set; }\r
44         public Point Location { get; set; }\r
45         public Size Size { get; set; }\r
46         public string Mode { get; set; }\r
47         public bool ShipType { get; set; }\r
48         public bool ShowHpInPercent { get; set; }\r
49         public ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext;\r
50         public List<List<int>> ShipGroup { get; set; }\r
51 \r
52         public ShipListConfig()\r
53         {\r
54             Location = new Point(int.MinValue, int.MinValue);\r
55             ShipGroup = new List<List<int>>();\r
56         }\r
57     }\r
58 \r
59     public class LogConfig\r
60     {\r
61         public bool On { get; set; }\r
62         public string OutputDir { get; set; }\r
63         public int MaterialLogInterval { get; set; }\r
64 \r
65         public LogConfig()\r
66         {\r
67             On = true;\r
68             OutputDir = "";\r
69             MaterialLogInterval = 10;\r
70         }\r
71     }\r
72 \r
73     public class KancolleDbConfig\r
74     {\r
75         public bool On { get; set; }\r
76         public string Token { get; set; } = "";\r
77     }\r
78 \r
79     public class PushbulletConfig\r
80     {\r
81         public bool On { get; set; }\r
82         public string Token { get; set; } = "";\r
83     }\r
84 \r
85     public class PushoverConfig\r
86     {\r
87         public bool On { get; set; }\r
88         public string ApiKey { get; set; } = "";\r
89         public string UserKey { get; set; } = "";\r
90     }\r
91 \r
92     public class SoundConfig\r
93     {\r
94         public int Volume { get; set; } = 100;\r
95 \r
96         public string[] Files { get; set; } =\r
97         {\r
98             "ensei.mp3",\r
99             "nyuukyo.mp3",\r
100             "kenzou.mp3",\r
101             "kanmusu.mp3",\r
102             "soubi.mp3",\r
103             "taiha.mp3",\r
104             "20min.mp3",\r
105             "syuuri.mp3",\r
106             "syuuri2.mp3",\r
107             "hirou.mp3"\r
108         };\r
109 \r
110         public string this[string name]\r
111         {\r
112             get => Files[Config.NotificationIndex[name]];\r
113             set => Files[Config.NotificationIndex[name]] = value;\r
114         }\r
115     }\r
116 \r
117     [Flags]\r
118     public enum NotificationType\r
119     {\r
120         FlashWindow = 1,\r
121         ShowBaloonTip = 1 << 1,\r
122         PlaySound = 1 << 2,\r
123         All = (1 << 3) - 1,\r
124         Pushbullet = 1 << 3,\r
125         Push = 1 << 4,\r
126         Repeat = 1 << 5,\r
127         Cont = 1 << 6\r
128     }\r
129 \r
130 \r
131     public class NotificationSpec\r
132     {\r
133         public string Name { get; set; }\r
134         public NotificationType Flags { get; set; }\r
135         public int RepeatInterval { get; set; }\r
136         public int PreliminaryPeriod { get; set; }\r
137     }\r
138 \r
139     public class NotificationConfig\r
140     {\r
141         public NotificationType[] Settings =\r
142             Config.NotificationNames.Select(x => NotificationType.All).ToArray();\r
143 \r
144         public int[] RepeatIntervals =\r
145             Config.NotificationNames.Select(x => 0).ToArray();\r
146 \r
147         public int[] PreliminaryPeriods =\r
148             Config.NotificationNames.Select(x => 0).ToArray();\r
149 \r
150         public NotificationSpec this[string name]\r
151         {\r
152             get => new NotificationSpec\r
153             {\r
154                 Name = name,\r
155                 Flags = Settings[Config.NotificationIndex[name]],\r
156                 RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]],\r
157                 PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]]\r
158             };\r
159             set\r
160             {\r
161                 Settings[Config.NotificationIndex[name]] = value.Flags;\r
162                 RepeatIntervals[Config.NotificationIndex[name]] = value.RepeatInterval;\r
163                 PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod;\r
164             }\r
165         }\r
166     }\r
167 \r
168     public class LocationPerMachine\r
169     {\r
170         public string MachineName { get; set; }\r
171         public Point Location { get; set; }\r
172         public int Zoom { get; set; } = 100;\r
173         public Point ListLocation { get; set; }\r
174         public Size ListSize { get; set; }\r
175     }\r
176 \r
177     [Flags]\r
178     public enum Spoiler\r
179     {\r
180         ResultRank = 1,\r
181         AirBattleResult = 1 << 1,\r
182         BattleResult = 1 << 2,\r
183         All = (1 << 3) - 1\r
184     }\r
185 \r
186     [Flags]\r
187     public enum TimerKind\r
188     {\r
189         Mission = 1,\r
190         NDock = 1 << 1\r
191     }\r
192 \r
193     public class Config\r
194     {\r
195         private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory;\r
196         private readonly string _configFileName;\r
197 \r
198         public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);\r
199         public bool TopMost { get; set; }\r
200         public bool HideOnMinimized { get; set; }\r
201         public bool ExitSilently { get; set; }\r
202         public int Zoom { get; set; } = 100;\r
203         public bool SaveLocationPerMachine { get; set; }\r
204         public List<LocationPerMachine> LocationList { get; set; } = new List<LocationPerMachine>();\r
205         public bool ShowHpInPercent { get; set; }\r
206         public TimerKind ShowEndTime { get; set; }\r
207         public bool FlashWindow { get; set; } = true;\r
208         public bool ShowBaloonTip { get; set; } = true;\r
209         public bool PlaySound { get; set; } = true;\r
210         public NotificationType NotificationFlags { get; set; } = NotificationType.All;\r
211         public NotificationConfig Notifications { get; set; } = new NotificationConfig();\r
212         public int MarginShips { get; set; } = 4;\r
213         public int MarginEquips { get; set; } = 10;\r
214         public List<int> NotifyConditions { get; set; }\r
215         public List<int> ResetHours { get; set; }\r
216         public bool AlwaysShowResultRank { get; set; }\r
217         public Spoiler Spoilers { get; set; }\r
218         public bool UsePresetAkashi { get; set; }\r
219         public SoundConfig Sounds { get; set; } = new SoundConfig();\r
220         public bool DebugLogging { get; set; }\r
221         public string DebugLogFile { get; set; } = "log.txt";\r
222         public ProxyConfig Proxy { get; set; } = new ProxyConfig();\r
223         public ShipListConfig ShipList { get; set; } = new ShipListConfig();\r
224         public LogConfig Log { get; set; } = new LogConfig();\r
225         public KancolleDbConfig KancolleDb { get; set; } = new KancolleDbConfig();\r
226         public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();\r
227         public PushoverConfig Pushover { get; set; } = new PushoverConfig();\r
228 \r
229         public static readonly string[] NotificationNames =\r
230         {\r
231             "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
232             "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復"\r
233         };\r
234 \r
235         public static readonly Dictionary<string, int> NotificationIndex =\r
236             NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);\r
237 \r
238         public Config()\r
239         {\r
240             _configFileName = Path.Combine(_baseDir, "config.xml");\r
241             ConvertPath(PrependBaseDir);\r
242         }\r
243 \r
244         public void InitializeValues()\r
245         {\r
246             NotifyConditions = new List<int>(new[] {40, 49});\r
247             ResetHours = new List<int>(new[] {2});\r
248         }\r
249 \r
250         public void Load()\r
251         {\r
252             try\r
253             {\r
254                 var serializer = new XmlSerializer(typeof(Config));\r
255                 Config config;\r
256                 using (var file = File.OpenText(_configFileName))\r
257                     config = (Config)serializer.Deserialize(file);\r
258                 foreach (var property in GetType().GetProperties())\r
259                     property.SetValue(this, property.GetValue(config, null), null);\r
260                 var ns = Notifications.Settings;\r
261                 for (var i = 0; i < ns.Length; i++)\r
262                 {\r
263                     if ((ns[i] & NotificationType.Pushbullet) != 0)\r
264                         ns[i] = ns[i] ^ NotificationType.Pushbullet | NotificationType.Push;\r
265                 }\r
266                 ComposeNotificationFlags();\r
267                 if (AlwaysShowResultRank)\r
268                 {\r
269                     Spoilers = Spoiler.All;\r
270                     AlwaysShowResultRank = false;\r
271                 }\r
272                 if (SaveLocationPerMachine)\r
273                 {\r
274                     foreach (var l in LocationList)\r
275                     {\r
276                         if (l.MachineName != Environment.MachineName)\r
277                             continue;\r
278                         Location = l.Location;\r
279                         Zoom = l.Zoom;\r
280                         ShipList.Location = l.ListLocation;\r
281                         ShipList.Size = l.ListSize;\r
282                     }\r
283                 }\r
284             }\r
285             catch (FileNotFoundException)\r
286             {\r
287                 InitializeValues();\r
288                 Save();\r
289             }\r
290             ConvertPath(PrependBaseDir);\r
291         }\r
292 \r
293         private void ComposeNotificationFlags()\r
294         {\r
295             NotificationFlags = (NotificationFlags & ~NotificationType.All) |\r
296                                 (FlashWindow ? NotificationType.FlashWindow : 0) |\r
297                                 (ShowBaloonTip ? NotificationType.ShowBaloonTip : 0) |\r
298                                 (PlaySound ? NotificationType.PlaySound : 0);\r
299         }\r
300 \r
301         public void Save()\r
302         {\r
303             if (SaveLocationPerMachine)\r
304             {\r
305                 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();\r
306                 LocationList.Add(new LocationPerMachine\r
307                 {\r
308                     MachineName = Environment.MachineName,\r
309                     Location = Location,\r
310                     Zoom = Zoom,\r
311                     ListLocation = ShipList.Location,\r
312                     ListSize = ShipList.Size\r
313                 });\r
314             }\r
315             else\r
316             {\r
317                 LocationList = new List<LocationPerMachine>();\r
318             }\r
319             DecomposeNotificationFlags();\r
320             ConvertPath(StripBaseDir);\r
321             var serializer = new XmlSerializer(typeof(Config));\r
322             using (var file = File.CreateText(_configFileName))\r
323                 serializer.Serialize(file, this);\r
324         }\r
325 \r
326         private void DecomposeNotificationFlags()\r
327         {\r
328             FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0;\r
329             ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0;\r
330             PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0;\r
331         }\r
332 \r
333         private void ConvertPath(Func<string, string> func)\r
334         {\r
335             DebugLogFile = func(DebugLogFile);\r
336             Log.OutputDir = func(Log.OutputDir);\r
337             for (var i = 0; i < Sounds.Files.Length; i++)\r
338                 Sounds.Files[i] = func(Sounds.Files[i]);\r
339         }\r
340 \r
341         private string StripBaseDir(string path)\r
342         {\r
343             if (!path.StartsWith(_baseDir))\r
344                 return path;\r
345             path = path.Substring(_baseDir.Length);\r
346             return path.StartsWith(Path.DirectorySeparatorChar.ToString()) ? path.Substring(1) : path;\r
347         }\r
348 \r
349         private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(_baseDir, path);\r
350     }\r
351 }