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
42 public enum ShipCategory
\r
45 BattleShip = 1 << 0,
\r
46 AircraftCarrier = 1 << 1,
\r
47 HeavyCruiser = 1 << 2,
\r
48 LightCruiser = 1 << 3,
\r
56 public class ShipListConfig
\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
68 public ShipListConfig()
\r
70 Location = new Point(int.MinValue, int.MinValue);
\r
71 ShipGroup = new List<List<int>>();
\r
75 public class LogConfig
\r
77 public bool On { get; set; }
\r
78 public string OutputDir { get; set; }
\r
79 public int MaterialLogInterval { get; set; }
\r
85 MaterialLogInterval = 10;
\r
89 public class PushbulletConfig
\r
91 public bool On { get; set; }
\r
92 public string Token { get; set; } = "";
\r
95 public class PushoverConfig
\r
97 public bool On { get; set; }
\r
98 public string ApiKey { get; set; } = "";
\r
99 public string UserKey { get; set; } = "";
\r
102 public class SoundConfig
\r
104 public int Volume { get; set; } = 100;
\r
106 public string[] Files =
\r
121 public string this[string name]
\r
123 get => Files[Config.NotificationIndex[name]];
\r
124 set => Files[Config.NotificationIndex[name]] = value;
\r
127 public void Upgrade()
\r
129 var expected = Config.NotificationNames.Length;
\r
130 if (Files.Length == expected)
\r
132 Array.Resize(ref Files, expected);
\r
133 Files[expected - 1] = "ninmu.mp3";
\r
138 public enum NotificationType
\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
149 Preliminary = 1 << 7
\r
153 public class NotificationSpec
\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
161 public class NotificationConfig
\r
163 public NotificationType[] Settings =
\r
164 Config.NotificationNames.Select(x => NotificationType.All).ToArray();
\r
166 public int[] RepeatIntervals =
\r
167 Config.NotificationNames.Select(x => 0).ToArray();
\r
169 public int[] PreliminaryPeriods =
\r
170 Config.NotificationNames.Select(x => 0).ToArray();
\r
172 public NotificationSpec this[string name]
\r
174 get => new NotificationSpec
\r
177 Flags = Settings[Config.NotificationIndex[name]],
\r
178 RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]],
\r
179 PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]]
\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
189 public void Upgrade()
\r
191 UpgradeSettings(ref Settings);
\r
192 UpgradeParameterArray(ref RepeatIntervals);
\r
193 UpgradeParameterArray(ref PreliminaryPeriods);
\r
196 private void UpgradeSettings(ref NotificationType[] settings)
\r
198 for (var i = 0; i < settings.Length; i++)
\r
200 if ((settings[i] & NotificationType.Pushbullet) != 0)
\r
201 settings[i] = settings[i] ^ NotificationType.Pushbullet | NotificationType.Push;
\r
203 var expected = Config.NotificationNames.Length;
\r
204 if (expected == settings.Length)
\r
206 Array.Resize(ref settings, expected);
\r
207 settings[expected - 1] = NotificationType.All;
\r
210 private void UpgradeParameterArray(ref int[] array)
\r
212 Array.Resize(ref array, Config.NotificationNames.Length);
\r
213 for (var i = 0; i < array.Length; i++)
\r
221 public class LocationPerMachine
\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
231 public enum Spoiler
\r
234 AirBattleResult = 1 << 1,
\r
235 BattleResult = 1 << 2,
\r
241 public enum TimerKind
\r
247 public class Config
\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
280 public static readonly string[] NotificationNames =
\r
282 "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",
\r
283 "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復", "任務達成"
\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
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
296 ConvertPath(PrependBaseDir);
\r
299 public void InitializeValues()
\r
301 NotifyConditions = new List<int>(new[] {40, 49});
\r
302 ResetHours = new List<int>(new[] {2});
\r
309 var serializer = new XmlSerializer(typeof(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
318 if (AlwaysShowResultRank)
\r
320 Spoilers = Spoiler.All;
\r
321 AlwaysShowResultRank = false;
\r
323 if (SaveLocationPerMachine)
\r
325 foreach (var l in LocationList)
\r
327 if (l.MachineName != Environment.MachineName)
\r
329 Location = l.Location;
\r
331 ShipList.Location = l.ListLocation;
\r
332 ShipList.Size = l.ListSize;
\r
336 catch (FileNotFoundException)
\r
338 InitializeValues();
\r
341 catch (InvalidOperationException ex)
\r
343 throw new Exception(FileName + "が壊れています。", ex);
\r
345 ConvertPath(PrependBaseDir);
\r
348 private void ComposeNotificationFlags()
\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
358 if (SaveLocationPerMachine)
\r
360 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();
\r
361 LocationList.Add(new LocationPerMachine
\r
363 MachineName = Environment.MachineName,
\r
364 Location = Location,
\r
366 ListLocation = ShipList.Location,
\r
367 ListSize = ShipList.Size
\r
372 LocationList = new List<LocationPerMachine>();
\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
384 private void DecomposeNotificationFlags()
\r
386 FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0;
\r
387 ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0;
\r
388 PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0;
\r
391 private void ConvertPath(Func<string, string> func)
\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
399 private string StripBaseDir(string path)
\r
401 if (!path.StartsWith(BaseDir))
\r
403 path = path.Substring(BaseDir.Length);
\r
404 return path.TrimStart(Path.DirectorySeparatorChar);
\r
407 private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(BaseDir, path);
\r