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