OSDN Git Service

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