OSDN Git Service

BaseAirCorpsをAirBaseにリネームする
[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 << 0,\r
47 \r
48         AircraftCarrier = 1 << 1,\r
49         HeavyCruiser = 1 << 2,\r
50         LightCruiser = 1 << 3,\r
51         Destroyer = 1 << 4,\r
52         Escort = 1 << 5,\r
53         Submarine = 1 << 6,\r
54         Assistant = 1 << 7,\r
55         // ReSharper restore UnusedMember.Global\r
56         All = (1 << 8) - 1\r
57     }\r
58 \r
59     public class ShipListConfig\r
60     {\r
61         public bool Visible { get; set; }\r
62         public Point Location { get; set; }\r
63         public Size Size { get; set; }\r
64         public string Mode { get; set; }\r
65         public ShipCategory ShipCategories { get; set; } = ShipCategory.All;\r
66         public bool ShipType;\r
67         public bool ShowHpInPercent { get; set; }\r
68         public ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext;\r
69         public List<List<int>> ShipGroup { get; set; }\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 \r
156     public class NotificationSpec\r
157     {\r
158         public string Name { get; set; }\r
159         public NotificationType Flags { get; set; }\r
160         public int RepeatInterval { get; set; }\r
161         public int PreliminaryPeriod { get; set; }\r
162     }\r
163 \r
164     public class NotificationConfig\r
165     {\r
166         public NotificationType[] Settings =\r
167             Config.NotificationNames.Select(x => NotificationType.All).ToArray();\r
168 \r
169         public int[] RepeatIntervals =\r
170             Config.NotificationNames.Select(x => 0).ToArray();\r
171 \r
172         public int[] PreliminaryPeriods =\r
173             Config.NotificationNames.Select(x => 0).ToArray();\r
174 \r
175         public NotificationSpec this[string name]\r
176         {\r
177             get => new NotificationSpec\r
178             {\r
179                 Name = name,\r
180                 Flags = Settings[Config.NotificationIndex[name]],\r
181                 RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]],\r
182                 PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]]\r
183             };\r
184             set\r
185             {\r
186                 Settings[Config.NotificationIndex[name]] = value.Flags;\r
187                 RepeatIntervals[Config.NotificationIndex[name]] = value.RepeatInterval;\r
188                 PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod;\r
189             }\r
190         }\r
191 \r
192         public void Upgrade()\r
193         {\r
194             UpgradeSettings(ref Settings);\r
195             UpgradeParameterArray(ref RepeatIntervals);\r
196             UpgradeParameterArray(ref PreliminaryPeriods);\r
197         }\r
198 \r
199         private void UpgradeSettings(ref NotificationType[] settings)\r
200         {\r
201             for (var i = 0; i < settings.Length; i++)\r
202             {\r
203                 if ((settings[i] & NotificationType.Pushbullet) != 0)\r
204                     settings[i] = settings[i] ^ NotificationType.Pushbullet | NotificationType.Push;\r
205             }\r
206             var expected = Config.NotificationNames.Length;\r
207             if (expected == settings.Length)\r
208                 return;\r
209             Array.Resize(ref settings, expected);\r
210             settings[expected - 1] = NotificationType.All;\r
211         }\r
212 \r
213         private void UpgradeParameterArray(ref int[] array)\r
214         {\r
215             Array.Resize(ref array, Config.NotificationNames.Length);\r
216             for (var i = 0; i < array.Length; i++)\r
217             {\r
218                 if (array[i] < 0)\r
219                     array[i] = 0;\r
220             }\r
221         }\r
222     }\r
223 \r
224     public class LocationPerMachine\r
225     {\r
226         public string MachineName { get; set; }\r
227         public Point Location { get; set; }\r
228         public int Zoom { get; set; } = 100;\r
229         public Point ListLocation { get; set; }\r
230         public Size ListSize { get; set; }\r
231     }\r
232 \r
233     [Flags]\r
234     public enum Spoiler\r
235     {\r
236         ResultRank = 1,\r
237         AirBattleResult = 1 << 1,\r
238         BattleResult = 1 << 2,\r
239         NextCell = 1 << 3,\r
240         All = (1 << 4) - 1\r
241     }\r
242 \r
243     [Flags]\r
244     public enum TimerKind\r
245     {\r
246         Mission = 1,\r
247         NDock = 1 << 1\r
248     }\r
249 \r
250     public class Config\r
251     {\r
252         public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);\r
253         public bool TopMost { get; set; }\r
254         public bool HideOnMinimized { get; set; }\r
255         public bool ExitSilently { get; set; }\r
256         public int Zoom { get; set; } = 100;\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 SoundConfig Sounds { get; set; } = new SoundConfig();\r
275         public bool DebugLogging { get; set; }\r
276         public string DebugLogFile { get; set; } = "log.txt";\r
277         public ProxyConfig Proxy { get; set; } = new ProxyConfig();\r
278         public ShipListConfig ShipList { get; set; } = new ShipListConfig();\r
279         public LogConfig Log { get; set; } = new LogConfig();\r
280         public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();\r
281         public PushoverConfig Pushover { get; set; } = new PushoverConfig();\r
282 \r
283         public static readonly string[] NotificationNames =\r
284         {\r
285             "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
286             "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復", "任務達成"\r
287         };\r
288 \r
289         public static readonly Dictionary<string, int> NotificationIndex =\r
290             NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);\r
291 \r
292 \r
293         private const string FileName = "config.xml";\r
294         public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;\r
295         private static readonly string ConfigFile = Path.Combine(BaseDir, FileName);\r
296 \r
297         public Config()\r
298         {\r
299             ConvertPath(PrependBaseDir);\r
300         }\r
301 \r
302         public void InitializeValues()\r
303         {\r
304             NotifyConditions = new List<int>(new[] {40, 49});\r
305             ResetHours = new List<int>(new[] {2});\r
306         }\r
307 \r
308         public void Load()\r
309         {\r
310             try\r
311             {\r
312                 var serializer = new XmlSerializer(typeof(Config));\r
313                 Config config;\r
314                 using (var file = File.OpenText(ConfigFile))\r
315                     config = (Config)serializer.Deserialize(file);\r
316                 foreach (var property in GetType().GetProperties())\r
317                     property.SetValue(this, property.GetValue(config, null), null);\r
318                 Notifications.Upgrade();\r
319                 ComposeNotificationFlags();\r
320                 Sounds.Upgrade();\r
321                 if (AlwaysShowResultRank)\r
322                 {\r
323                     Spoilers = Spoiler.All;\r
324                     AlwaysShowResultRank = false;\r
325                 }\r
326                 if (SaveLocationPerMachine)\r
327                 {\r
328                     foreach (var l in LocationList)\r
329                     {\r
330                         if (l.MachineName != Environment.MachineName)\r
331                             continue;\r
332                         Location = l.Location;\r
333                         Zoom = l.Zoom;\r
334                         ShipList.Location = l.ListLocation;\r
335                         ShipList.Size = l.ListSize;\r
336                     }\r
337                 }\r
338             }\r
339             catch (FileNotFoundException)\r
340             {\r
341                 InitializeValues();\r
342                 Save();\r
343             }\r
344             catch (InvalidOperationException ex)\r
345             {\r
346                 throw new Exception(FileName + "が壊れています。", ex);\r
347             }\r
348             ConvertPath(PrependBaseDir);\r
349         }\r
350 \r
351         private void ComposeNotificationFlags()\r
352         {\r
353             NotificationFlags = (NotificationFlags & ~NotificationType.All) |\r
354                                 (FlashWindow ? NotificationType.FlashWindow : 0) |\r
355                                 (ShowBaloonTip ? NotificationType.ShowBaloonTip : 0) |\r
356                                 (PlaySound ? NotificationType.PlaySound : 0);\r
357         }\r
358 \r
359         public void Save()\r
360         {\r
361             if (SaveLocationPerMachine)\r
362             {\r
363                 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();\r
364                 LocationList.Add(new LocationPerMachine\r
365                 {\r
366                     MachineName = Environment.MachineName,\r
367                     Location = Location,\r
368                     Zoom = Zoom,\r
369                     ListLocation = ShipList.Location,\r
370                     ListSize = ShipList.Size\r
371                 });\r
372             }\r
373             else\r
374             {\r
375                 LocationList = new List<LocationPerMachine>();\r
376             }\r
377             DecomposeNotificationFlags();\r
378             ConvertPath(StripBaseDir);\r
379             var serializer = new XmlSerializer(typeof(Config));\r
380             using (var file = File.CreateText(ConfigFile + ".tmp"))\r
381                 serializer.Serialize(file, this);\r
382             File.Copy(ConfigFile + ".tmp", ConfigFile, true);\r
383             File.Delete(ConfigFile + ".tmp");\r
384             ConvertPath(PrependBaseDir);\r
385         }\r
386 \r
387         private void DecomposeNotificationFlags()\r
388         {\r
389             FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0;\r
390             ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0;\r
391             PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0;\r
392         }\r
393 \r
394         private void ConvertPath(Func<string, string> func)\r
395         {\r
396             DebugLogFile = func(DebugLogFile);\r
397             Log.OutputDir = func(Log.OutputDir);\r
398             for (var i = 0; i < Sounds.Files.Length; i++)\r
399                 Sounds.Files[i] = func(Sounds.Files[i]);\r
400         }\r
401 \r
402         private string StripBaseDir(string path)\r
403         {\r
404             if (!path.StartsWith(BaseDir))\r
405                 return path;\r
406             path = path.Substring(BaseDir.Length);\r
407             return path.TrimStart(Path.DirectorySeparatorChar);\r
408         }\r
409 \r
410         private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(BaseDir, path);\r
411     }\r
412 }