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