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 List<ShipListConfig> ListFormGroup { get; set; } = new List<ShipListConfig>();\r
281         public LogConfig Log { get; set; } = new LogConfig();\r
282         public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();\r
283         public PushoverConfig Pushover { get; set; } = new PushoverConfig();\r
284 \r
285         public static readonly string[] NotificationNames =\r
286         {\r
287             "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
288             "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復", "任務達成"\r
289         };\r
290 \r
291         public static readonly Dictionary<string, int> NotificationIndex =\r
292             NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);\r
293 \r
294 \r
295         private const string FileName = "config.xml";\r
296         public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;\r
297         private static readonly string ConfigFile = Path.Combine(BaseDir, FileName);\r
298 \r
299         public Config()\r
300         {\r
301             ConvertPath(PrependBaseDir);\r
302         }\r
303 \r
304         public void InitializeValues()\r
305         {\r
306             NotifyConditions = new List<int>(new[] {40, 49});\r
307             ResetHours = new List<int>(new[] {2});\r
308         }\r
309 \r
310         public void Load()\r
311         {\r
312             try\r
313             {\r
314                 var serializer = new XmlSerializer(typeof(Config));\r
315                 Config config;\r
316                 using (var file = File.OpenText(ConfigFile))\r
317                     config = (Config)serializer.Deserialize(file);\r
318                 foreach (var property in GetType().GetProperties())\r
319                     property.SetValue(this, property.GetValue(config, null), null);\r
320                 Notifications.Upgrade();\r
321                 ComposeNotificationFlags();\r
322                 Sounds.Upgrade();\r
323                 if (AlwaysShowResultRank)\r
324                 {\r
325                     Spoilers = Spoiler.All;\r
326                     AlwaysShowResultRank = false;\r
327                 }\r
328                 if (SaveLocationPerMachine)\r
329                 {\r
330                     foreach (var l in LocationList)\r
331                     {\r
332                         if (l.MachineName != Environment.MachineName)\r
333                             continue;\r
334                         Location = l.Location;\r
335                         Zoom = l.Zoom;\r
336                         ShipList.Location = l.ListLocation;\r
337                         ShipList.Size = l.ListSize;\r
338                     }\r
339                 }\r
340             }\r
341             catch (FileNotFoundException)\r
342             {\r
343                 InitializeValues();\r
344                 Save();\r
345             }\r
346             catch (InvalidOperationException ex)\r
347             {\r
348                 File.Delete(ConfigFile);\r
349                 throw new Exception(FileName + "が壊れています。", ex);\r
350             }\r
351             ConvertPath(PrependBaseDir);\r
352         }\r
353 \r
354         private void ComposeNotificationFlags()\r
355         {\r
356             NotificationFlags = (NotificationFlags & ~NotificationType.All) |\r
357                                 (FlashWindow ? NotificationType.FlashWindow : 0) |\r
358                                 (ShowBaloonTip ? NotificationType.ShowBaloonTip : 0) |\r
359                                 (PlaySound ? NotificationType.PlaySound : 0);\r
360         }\r
361 \r
362         public void Save()\r
363         {\r
364             if (SaveLocationPerMachine)\r
365             {\r
366                 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();\r
367                 LocationList.Add(new LocationPerMachine\r
368                 {\r
369                     MachineName = Environment.MachineName,\r
370                     Location = Location,\r
371                     Zoom = Zoom,\r
372                     ListLocation = ShipList.Location,\r
373                     ListSize = ShipList.Size\r
374                 });\r
375             }\r
376             else\r
377             {\r
378                 LocationList = new List<LocationPerMachine>();\r
379             }\r
380             DecomposeNotificationFlags();\r
381             ConvertPath(StripBaseDir);\r
382             var serializer = new XmlSerializer(typeof(Config));\r
383             using (var file = File.CreateText(ConfigFile + ".tmp"))\r
384                 serializer.Serialize(file, this);\r
385             File.Copy(ConfigFile + ".tmp", ConfigFile, true);\r
386             File.Delete(ConfigFile + ".tmp");\r
387             ConvertPath(PrependBaseDir);\r
388         }\r
389 \r
390         private void DecomposeNotificationFlags()\r
391         {\r
392             FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0;\r
393             ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0;\r
394             PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0;\r
395         }\r
396 \r
397         private void ConvertPath(Func<string, string> func)\r
398         {\r
399             DebugLogFile = func(DebugLogFile);\r
400             Log.OutputDir = func(Log.OutputDir);\r
401             for (var i = 0; i < Sounds.Files.Length; i++)\r
402                 Sounds.Files[i] = func(Sounds.Files[i]);\r
403         }\r
404 \r
405         private string StripBaseDir(string path)\r
406         {\r
407             if (!path.StartsWith(BaseDir))\r
408                 return path;\r
409             path = path.Substring(BaseDir.Length);\r
410             return path.TrimStart(Path.DirectorySeparatorChar);\r
411         }\r
412 \r
413         private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(BaseDir, path);\r
414     }\r
415 }