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     [Flags]\r
42     public enum ShipCategory\r
43     {\r
44         None = 0,\r
45         // ReSharper disable UnusedMember.Global\r
46         BattleShip = 1,\r
47         AircraftCarrier = 1 << 1,\r
48         HeavyCruiser = 1 << 2,\r
49         LightCruiser = 1 << 3,\r
50         Destroyer = 1 << 4,\r
51         Escort = 1 << 5,\r
52         Submarine = 1 << 6,\r
53         Assistant = 1 << 7,\r
54         // ReSharper restore UnusedMember.Global\r
55         All = (1 << 8) - 1\r
56     }\r
57 \r
58     public class ShipListConfig\r
59     {\r
60         public bool Visible { get; set; }\r
61         public Point Location { get; set; }\r
62         public Size Size { get; set; }\r
63         public string Mode { get; set; }\r
64         public ShipCategory ShipCategories { get; set; } = ShipCategory.All;\r
65         public bool ShipType { get; set; }\r
66         public bool ShowHpInPercent { get; set; }\r
67         public ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext;\r
68         public List<List<int>> ShipGroup { get; set; }\r
69         public bool AutoBattleResult { get; set; } = true;\r
70 \r
71         public ShipListConfig()\r
72         {\r
73             Location = new Point(int.MinValue, int.MinValue);\r
74             ShipGroup = new List<List<int>>();\r
75         }\r
76     }\r
77 \r
78     public class LogConfig\r
79     {\r
80         public bool On { get; set; }\r
81         public string OutputDir { get; set; }\r
82         public int MaterialLogInterval { get; set; }\r
83 \r
84         public LogConfig()\r
85         {\r
86             On = true;\r
87             OutputDir = "";\r
88             MaterialLogInterval = 10;\r
89         }\r
90     }\r
91 \r
92     public class PushbulletConfig\r
93     {\r
94         public bool On { get; set; }\r
95         public string Token { get; set; } = "";\r
96     }\r
97 \r
98     public class PushoverConfig\r
99     {\r
100         public bool On { get; set; }\r
101         public string ApiKey { get; set; } = "";\r
102         public string UserKey { get; set; } = "";\r
103     }\r
104 \r
105     public class SoundConfig\r
106     {\r
107         public int Volume { get; set; } = 100;\r
108 \r
109         public string[] Files =\r
110         {\r
111             "ensei.mp3",\r
112             "nyuukyo.mp3",\r
113             "kenzou.mp3",\r
114             "kanmusu.mp3",\r
115             "soubi.mp3",\r
116             "taiha.mp3",\r
117             "20min.mp3",\r
118             "syuuri.mp3",\r
119             "syuuri2.mp3",\r
120             "hirou.mp3",\r
121             "ninmu.mp3"\r
122         };\r
123 \r
124         public string this[string name]\r
125         {\r
126             get => Files[Config.NotificationIndex[name]];\r
127             set => Files[Config.NotificationIndex[name]] = value;\r
128         }\r
129 \r
130         public void Upgrade()\r
131         {\r
132             var expected = Config.NotificationNames.Length;\r
133             if (Files.Length == expected)\r
134                 return;\r
135             Array.Resize(ref Files, expected);\r
136             Files[expected - 1] = "ninmu.mp3";\r
137         }\r
138     }\r
139 \r
140     [Flags]\r
141     public enum NotificationType\r
142     {\r
143         FlashWindow = 1,\r
144         // ReSharper disable once IdentifierTypo\r
145         ShowBaloonTip = 1 << 1,\r
146         PlaySound = 1 << 2,\r
147         All = (1 << 3) - 1,\r
148         Pushbullet = 1 << 3,\r
149         Push = 1 << 4,\r
150         Repeat = 1 << 5,\r
151         Cont = 1 << 6,\r
152         Preliminary = 1 << 7\r
153     }\r
154 \r
155     public class NotificationSpec\r
156     {\r
157         public string Name { get; set; }\r
158         public NotificationType Flags { get; set; }\r
159         public int RepeatInterval { get; set; }\r
160         public int PreliminaryPeriod { get; set; }\r
161     }\r
162 \r
163     public class NotificationConfig\r
164     {\r
165         public NotificationType[] Settings =\r
166             Config.NotificationNames.Select(x => NotificationType.All).ToArray();\r
167 \r
168         public int[] RepeatIntervals =\r
169             Config.NotificationNames.Select(x => 0).ToArray();\r
170 \r
171         public int[] PreliminaryPeriods =\r
172             Config.NotificationNames.Select(x => 0).ToArray();\r
173 \r
174         public NotificationSpec this[string name]\r
175         {\r
176             get => new NotificationSpec\r
177             {\r
178                 Name = name,\r
179                 Flags = Settings[Config.NotificationIndex[name]],\r
180                 RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]],\r
181                 PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]]\r
182             };\r
183             set\r
184             {\r
185                 Settings[Config.NotificationIndex[name]] = value.Flags;\r
186                 RepeatIntervals[Config.NotificationIndex[name]] = value.RepeatInterval;\r
187                 PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod;\r
188             }\r
189         }\r
190 \r
191         public void Upgrade()\r
192         {\r
193             UpgradeSettings(ref Settings);\r
194             UpgradeParameterArray(ref RepeatIntervals);\r
195             UpgradeParameterArray(ref PreliminaryPeriods);\r
196         }\r
197 \r
198         private void UpgradeSettings(ref NotificationType[] settings)\r
199         {\r
200             for (var i = 0; i < settings.Length; i++)\r
201             {\r
202                 if ((settings[i] & NotificationType.Pushbullet) != 0)\r
203                     settings[i] = settings[i] ^ NotificationType.Pushbullet | NotificationType.Push;\r
204             }\r
205             var expected = Config.NotificationNames.Length;\r
206             if (expected == settings.Length)\r
207                 return;\r
208             Array.Resize(ref settings, expected);\r
209             settings[expected - 1] = NotificationType.All;\r
210         }\r
211 \r
212         private void UpgradeParameterArray(ref int[] array)\r
213         {\r
214             Array.Resize(ref array, Config.NotificationNames.Length);\r
215             for (var i = 0; i < array.Length; i++)\r
216             {\r
217                 if (array[i] < 0)\r
218                     array[i] = 0;\r
219             }\r
220         }\r
221     }\r
222 \r
223     public class LocationPerMachine\r
224     {\r
225         public string MachineName { get; set; }\r
226         public Point Location { get; set; }\r
227         public int Zoom { get; set; } = 100;\r
228         public Point ListLocation { get; set; }\r
229         public Size ListSize { get; set; }\r
230     }\r
231 \r
232     [Flags]\r
233     public enum Spoiler\r
234     {\r
235         ResultRank = 1,\r
236         AirBattleResult = 1 << 1,\r
237         BattleResult = 1 << 2,\r
238         NextCell = 1 << 3,\r
239         All = (1 << 4) - 1\r
240     }\r
241 \r
242     [Flags]\r
243     public enum TimerKind\r
244     {\r
245         Mission = 1,\r
246         NDock = 1 << 1\r
247     }\r
248 \r
249     public class Config\r
250     {\r
251         public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);\r
252         public bool TopMost { get; set; }\r
253         public bool HideOnMinimized { get; set; }\r
254         public bool ExitSilently { get; set; }\r
255         public int Zoom { get; set; } = 100;\r
256         public int QuestLines { get; set; } = 6;\r
257         public bool SaveLocationPerMachine { get; set; }\r
258         public List<LocationPerMachine> LocationList { get; set; } = new List<LocationPerMachine>();\r
259         public bool ShowHpInPercent { get; set; }\r
260         public TimerKind ShowEndTime { get; set; }\r
261         public bool FlashWindow { get; set; } = true;\r
262         // ReSharper disable once IdentifierTypo\r
263         public bool ShowBaloonTip { get; set; } = true;\r
264         public bool PlaySound { get; set; } = true;\r
265         public NotificationType NotificationFlags { get; set; } = NotificationType.All;\r
266         public NotificationConfig Notifications { get; set; } = new NotificationConfig();\r
267         public int MarginShips { get; set; } = 5;\r
268         public int MarginEquips { get; set; } = 5;\r
269         public List<int> NotifyConditions { get; set; }\r
270         public List<int> ResetHours { get; set; }\r
271         public bool AlwaysShowResultRank { get; set; }\r
272         public Spoiler Spoilers { get; set; }\r
273         public bool UsePresetAkashi { get; set; }\r
274         public bool WarnBadDamageWithDameCon { get; set; }\r
275         public SoundConfig Sounds { get; set; } = new SoundConfig();\r
276         public bool DebugLogging { get; set; }\r
277         public string DebugLogFile { get; set; } = "log.txt";\r
278         public ProxyConfig Proxy { get; set; } = new ProxyConfig();\r
279         public ShipListConfig ShipList { get; set; } = new ShipListConfig();\r
280         public LogConfig Log { get; set; } = new LogConfig();\r
281         public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();\r
282         public PushoverConfig Pushover { get; set; } = new PushoverConfig();\r
283 \r
284         public static readonly string[] NotificationNames =\r
285         {\r
286             "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
287             "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復", "任務達成"\r
288         };\r
289 \r
290         public static readonly Dictionary<string, int> NotificationIndex =\r
291             NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);\r
292 \r
293 \r
294         private const string FileName = "config.xml";\r
295         public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;\r
296         private static readonly string ConfigFile = Path.Combine(BaseDir, FileName);\r
297 \r
298         public Config()\r
299         {\r
300             ConvertPath(PrependBaseDir);\r
301         }\r
302 \r
303         public void InitializeValues()\r
304         {\r
305             NotifyConditions = new List<int>(new[] {40, 49});\r
306             ResetHours = new List<int>(new[] {2});\r
307         }\r
308 \r
309         public void Load()\r
310         {\r
311             try\r
312             {\r
313                 var serializer = new XmlSerializer(typeof(Config));\r
314                 Config config;\r
315                 using (var file = File.OpenText(ConfigFile))\r
316                     config = (Config)serializer.Deserialize(file);\r
317                 foreach (var property in GetType().GetProperties())\r
318                     property.SetValue(this, property.GetValue(config, null), null);\r
319                 Notifications.Upgrade();\r
320                 ComposeNotificationFlags();\r
321                 Sounds.Upgrade();\r
322                 if (AlwaysShowResultRank)\r
323                 {\r
324                     Spoilers = Spoiler.All;\r
325                     AlwaysShowResultRank = false;\r
326                 }\r
327                 if (SaveLocationPerMachine)\r
328                 {\r
329                     foreach (var l in LocationList)\r
330                     {\r
331                         if (l.MachineName != Environment.MachineName)\r
332                             continue;\r
333                         Location = l.Location;\r
334                         Zoom = l.Zoom;\r
335                         ShipList.Location = l.ListLocation;\r
336                         ShipList.Size = l.ListSize;\r
337                     }\r
338                 }\r
339             }\r
340             catch (FileNotFoundException)\r
341             {\r
342                 InitializeValues();\r
343                 Save();\r
344             }\r
345             catch (InvalidOperationException ex)\r
346             {\r
347                 throw new Exception(FileName + "が壊れています。", ex);\r
348             }\r
349             ConvertPath(PrependBaseDir);\r
350         }\r
351 \r
352         private void ComposeNotificationFlags()\r
353         {\r
354             NotificationFlags = (NotificationFlags & ~NotificationType.All) |\r
355                                 (FlashWindow ? NotificationType.FlashWindow : 0) |\r
356                                 (ShowBaloonTip ? NotificationType.ShowBaloonTip : 0) |\r
357                                 (PlaySound ? NotificationType.PlaySound : 0);\r
358         }\r
359 \r
360         public void Save()\r
361         {\r
362             if (SaveLocationPerMachine)\r
363             {\r
364                 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();\r
365                 LocationList.Add(new LocationPerMachine\r
366                 {\r
367                     MachineName = Environment.MachineName,\r
368                     Location = Location,\r
369                     Zoom = Zoom,\r
370                     ListLocation = ShipList.Location,\r
371                     ListSize = ShipList.Size\r
372                 });\r
373             }\r
374             else\r
375             {\r
376                 LocationList = new List<LocationPerMachine>();\r
377             }\r
378             DecomposeNotificationFlags();\r
379             ConvertPath(StripBaseDir);\r
380             var serializer = new XmlSerializer(typeof(Config));\r
381             using (var file = File.CreateText(ConfigFile + ".tmp"))\r
382                 serializer.Serialize(file, this);\r
383             File.Copy(ConfigFile + ".tmp", ConfigFile, true);\r
384             File.Delete(ConfigFile + ".tmp");\r
385             ConvertPath(PrependBaseDir);\r
386         }\r
387 \r
388         private void DecomposeNotificationFlags()\r
389         {\r
390             FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0;\r
391             ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0;\r
392             PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0;\r
393         }\r
394 \r
395         private void ConvertPath(Func<string, string> func)\r
396         {\r
397             DebugLogFile = func(DebugLogFile);\r
398             Log.OutputDir = func(Log.OutputDir);\r
399             for (var i = 0; i < Sounds.Files.Length; i++)\r
400                 Sounds.Files[i] = func(Sounds.Files[i]);\r
401         }\r
402 \r
403         private string StripBaseDir(string path)\r
404         {\r
405             if (!path.StartsWith(BaseDir))\r
406                 return path;\r
407             path = path.Substring(BaseDir.Length);\r
408             return path.TrimStart(Path.DirectorySeparatorChar);\r
409         }\r
410 \r
411         private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(BaseDir, path);\r
412     }\r
413 }