OSDN Git Service

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