1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
7 // http://www.apache.org/licenses/LICENSE-2.0
\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
16 using System.Collections.Generic;
\r
17 using System.Drawing;
\r
20 using System.Xml.Serialization;
\r
22 namespace KancolleSniffer
\r
24 public class ProxyConfig
\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
32 public ProxyConfig()
\r
35 Listen = DefaultListenPort;
\r
36 UseUpstream = false;
\r
37 UpstreamPort = 8888;
\r
41 public class ShipListConfig
\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
52 public ShipListConfig()
\r
54 Location = new Point(int.MinValue, int.MinValue);
\r
55 ShipGroup = new List<List<int>>();
\r
59 public class LogConfig
\r
61 public bool On { get; set; }
\r
62 public string OutputDir { get; set; }
\r
63 public int MaterialLogInterval { get; set; }
\r
69 MaterialLogInterval = 10;
\r
73 public class KancolleDbConfig
\r
75 public bool On { get; set; }
\r
76 public string Token { get; set; } = "";
\r
79 public class PushbulletConfig
\r
81 public bool On { get; set; }
\r
82 public string Token { get; set; } = "";
\r
85 public class PushoverConfig
\r
87 public bool On { get; set; }
\r
88 public string ApiKey { get; set; } = "";
\r
89 public string UserKey { get; set; } = "";
\r
92 public class SoundConfig
\r
94 public int Volume { get; set; } = 100;
\r
96 public string[] Files { get; set; } =
\r
110 public string this[string name]
\r
112 get => Files[Config.NotificationIndex[name]];
\r
113 set => Files[Config.NotificationIndex[name]] = value;
\r
118 public enum NotificationType
\r
121 ShowBaloonTip = 1 << 1,
\r
122 PlaySound = 1 << 2,
\r
123 All = (1 << 3) - 1,
\r
124 Pushbullet = 1 << 3,
\r
128 Preliminary = 1 << 7
\r
132 public class NotificationSpec
\r
134 public string Name { get; set; }
\r
135 public NotificationType Flags { get; set; }
\r
136 public int RepeatInterval { get; set; }
\r
137 public int PreliminaryPeriod { get; set; }
\r
140 public class NotificationConfig
\r
142 public NotificationType[] Settings =
\r
143 Config.NotificationNames.Select(x => NotificationType.All).ToArray();
\r
145 public int[] RepeatIntervals =
\r
146 Config.NotificationNames.Select(x => 0).ToArray();
\r
148 public int[] PreliminaryPeriods =
\r
149 Config.NotificationNames.Select(x => 0).ToArray();
\r
151 public NotificationSpec this[string name]
\r
153 get => new NotificationSpec
\r
156 Flags = Settings[Config.NotificationIndex[name]],
\r
157 RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]],
\r
158 PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]]
\r
162 Settings[Config.NotificationIndex[name]] = value.Flags;
\r
163 RepeatIntervals[Config.NotificationIndex[name]] = value.RepeatInterval;
\r
164 PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod;
\r
169 public class LocationPerMachine
\r
171 public string MachineName { get; set; }
\r
172 public Point Location { get; set; }
\r
173 public int Zoom { get; set; } = 100;
\r
174 public Point ListLocation { get; set; }
\r
175 public Size ListSize { get; set; }
\r
179 public enum Spoiler
\r
182 AirBattleResult = 1 << 1,
\r
183 BattleResult = 1 << 2,
\r
188 public enum TimerKind
\r
194 public class Config
\r
196 private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory;
\r
197 private readonly string _configFileName;
\r
199 public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);
\r
200 public bool TopMost { get; set; }
\r
201 public bool HideOnMinimized { get; set; }
\r
202 public bool ExitSilently { get; set; }
\r
203 public int Zoom { get; set; } = 100;
\r
204 public bool SaveLocationPerMachine { get; set; }
\r
205 public List<LocationPerMachine> LocationList { get; set; } = new List<LocationPerMachine>();
\r
206 public bool ShowHpInPercent { get; set; }
\r
207 public TimerKind ShowEndTime { get; set; }
\r
208 public bool FlashWindow { get; set; } = true;
\r
209 public bool ShowBaloonTip { get; set; } = true;
\r
210 public bool PlaySound { get; set; } = true;
\r
211 public NotificationType NotificationFlags { get; set; } = NotificationType.All;
\r
212 public NotificationConfig Notifications { get; set; } = new NotificationConfig();
\r
213 public int MarginShips { get; set; } = 4;
\r
214 public int MarginEquips { get; set; } = 10;
\r
215 public List<int> NotifyConditions { get; set; }
\r
216 public List<int> ResetHours { get; set; }
\r
217 public bool AlwaysShowResultRank { get; set; }
\r
218 public Spoiler Spoilers { get; set; }
\r
219 public bool UsePresetAkashi { get; set; }
\r
220 public SoundConfig Sounds { get; set; } = new SoundConfig();
\r
221 public bool DebugLogging { get; set; }
\r
222 public string DebugLogFile { get; set; } = "log.txt";
\r
223 public ProxyConfig Proxy { get; set; } = new ProxyConfig();
\r
224 public ShipListConfig ShipList { get; set; } = new ShipListConfig();
\r
225 public LogConfig Log { get; set; } = new LogConfig();
\r
226 public KancolleDbConfig KancolleDb { get; set; } = new KancolleDbConfig();
\r
227 public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();
\r
228 public PushoverConfig Pushover { get; set; } = new PushoverConfig();
\r
230 public static readonly string[] NotificationNames =
\r
232 "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",
\r
233 "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復"
\r
236 public static readonly Dictionary<string, int> NotificationIndex =
\r
237 NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);
\r
241 _configFileName = Path.Combine(_baseDir, "config.xml");
\r
242 ConvertPath(PrependBaseDir);
\r
245 public void InitializeValues()
\r
247 NotifyConditions = new List<int>(new[] {40, 49});
\r
248 ResetHours = new List<int>(new[] {2});
\r
255 var serializer = new XmlSerializer(typeof(Config));
\r
257 using (var file = File.OpenText(_configFileName))
\r
258 config = (Config)serializer.Deserialize(file);
\r
259 foreach (var property in GetType().GetProperties())
\r
260 property.SetValue(this, property.GetValue(config, null), null);
\r
261 var ns = Notifications.Settings;
\r
262 for (var i = 0; i < ns.Length; i++)
\r
264 if ((ns[i] & NotificationType.Pushbullet) != 0)
\r
265 ns[i] = ns[i] ^ NotificationType.Pushbullet | NotificationType.Push;
\r
267 ComposeNotificationFlags();
\r
268 if (AlwaysShowResultRank)
\r
270 Spoilers = Spoiler.All;
\r
271 AlwaysShowResultRank = false;
\r
273 if (SaveLocationPerMachine)
\r
275 foreach (var l in LocationList)
\r
277 if (l.MachineName != Environment.MachineName)
\r
279 Location = l.Location;
\r
281 ShipList.Location = l.ListLocation;
\r
282 ShipList.Size = l.ListSize;
\r
286 catch (FileNotFoundException)
\r
288 InitializeValues();
\r
291 ConvertPath(PrependBaseDir);
\r
294 private void ComposeNotificationFlags()
\r
296 NotificationFlags = (NotificationFlags & ~NotificationType.All) |
\r
297 (FlashWindow ? NotificationType.FlashWindow : 0) |
\r
298 (ShowBaloonTip ? NotificationType.ShowBaloonTip : 0) |
\r
299 (PlaySound ? NotificationType.PlaySound : 0);
\r
304 if (SaveLocationPerMachine)
\r
306 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();
\r
307 LocationList.Add(new LocationPerMachine
\r
309 MachineName = Environment.MachineName,
\r
310 Location = Location,
\r
312 ListLocation = ShipList.Location,
\r
313 ListSize = ShipList.Size
\r
318 LocationList = new List<LocationPerMachine>();
\r
320 DecomposeNotificationFlags();
\r
321 ConvertPath(StripBaseDir);
\r
322 var serializer = new XmlSerializer(typeof(Config));
\r
323 using (var file = File.CreateText(_configFileName))
\r
324 serializer.Serialize(file, this);
\r
327 private void DecomposeNotificationFlags()
\r
329 FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0;
\r
330 ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0;
\r
331 PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0;
\r
334 private void ConvertPath(Func<string, string> func)
\r
336 DebugLogFile = func(DebugLogFile);
\r
337 Log.OutputDir = func(Log.OutputDir);
\r
338 for (var i = 0; i < Sounds.Files.Length; i++)
\r
339 Sounds.Files[i] = func(Sounds.Files[i]);
\r
342 private string StripBaseDir(string path)
\r
344 if (!path.StartsWith(_baseDir))
\r
346 path = path.Substring(_baseDir.Length);
\r
347 return path.StartsWith(Path.DirectorySeparatorChar.ToString()) ? path.Substring(1) : path;
\r
350 private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(_baseDir, path);
\r