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 // ReSharper disable UnusedMember.Global
\r
46 BattleShip = 1 << 0,
\r
48 AircraftCarrier = 1 << 1,
\r
49 HeavyCruiser = 1 << 2,
\r
50 LightCruiser = 1 << 3,
\r
55 // ReSharper restore UnusedMember.Global
\r
59 public class ShipListConfig
\r
61 public bool Visible { get; set; }
\r
62 public Point Location { get; set; }
\r
63 public Size Size { get; set; }
\r
64 public string Mode { get; set; }
\r
65 public ShipCategory ShipCategories { get; set; } = ShipCategory.All;
\r
66 public bool ShipType;
\r
67 public bool ShowHpInPercent { get; set; }
\r
68 public ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext;
\r
69 public List<List<int>> ShipGroup { get; set; }
\r
71 public ShipListConfig()
\r
73 Location = new Point(int.MinValue, int.MinValue);
\r
74 ShipGroup = new List<List<int>>();
\r
78 public class LogConfig
\r
80 public bool On { get; set; }
\r
81 public string OutputDir { get; set; }
\r
82 public int MaterialLogInterval { get; set; }
\r
88 MaterialLogInterval = 10;
\r
92 public class PushbulletConfig
\r
94 public bool On { get; set; }
\r
95 public string Token { get; set; } = "";
\r
98 public class PushoverConfig
\r
100 public bool On { get; set; }
\r
101 public string ApiKey { get; set; } = "";
\r
102 public string UserKey { get; set; } = "";
\r
105 public class SoundConfig
\r
107 public int Volume { get; set; } = 100;
\r
109 public string[] Files =
\r
124 public string this[string name]
\r
126 get => Files[Config.NotificationIndex[name]];
\r
127 set => Files[Config.NotificationIndex[name]] = value;
\r
130 public void Upgrade()
\r
132 var expected = Config.NotificationNames.Length;
\r
133 if (Files.Length == expected)
\r
135 Array.Resize(ref Files, expected);
\r
136 Files[expected - 1] = "ninmu.mp3";
\r
141 public enum NotificationType
\r
144 // ReSharper disable once IdentifierTypo
\r
145 ShowBaloonTip = 1 << 1,
\r
146 PlaySound = 1 << 2,
\r
147 All = (1 << 3) - 1,
\r
148 Pushbullet = 1 << 3,
\r
152 Preliminary = 1 << 7
\r
156 public class NotificationSpec
\r
158 public string Name { get; set; }
\r
159 public NotificationType Flags { get; set; }
\r
160 public int RepeatInterval { get; set; }
\r
161 public int PreliminaryPeriod { get; set; }
\r
164 public class NotificationConfig
\r
166 public NotificationType[] Settings =
\r
167 Config.NotificationNames.Select(x => NotificationType.All).ToArray();
\r
169 public int[] RepeatIntervals =
\r
170 Config.NotificationNames.Select(x => 0).ToArray();
\r
172 public int[] PreliminaryPeriods =
\r
173 Config.NotificationNames.Select(x => 0).ToArray();
\r
175 public NotificationSpec this[string name]
\r
177 get => new NotificationSpec
\r
180 Flags = Settings[Config.NotificationIndex[name]],
\r
181 RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]],
\r
182 PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]]
\r
186 Settings[Config.NotificationIndex[name]] = value.Flags;
\r
187 RepeatIntervals[Config.NotificationIndex[name]] = value.RepeatInterval;
\r
188 PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod;
\r
192 public void Upgrade()
\r
194 UpgradeSettings(ref Settings);
\r
195 UpgradeParameterArray(ref RepeatIntervals);
\r
196 UpgradeParameterArray(ref PreliminaryPeriods);
\r
199 private void UpgradeSettings(ref NotificationType[] settings)
\r
201 for (var i = 0; i < settings.Length; i++)
\r
203 if ((settings[i] & NotificationType.Pushbullet) != 0)
\r
204 settings[i] = settings[i] ^ NotificationType.Pushbullet | NotificationType.Push;
\r
206 var expected = Config.NotificationNames.Length;
\r
207 if (expected == settings.Length)
\r
209 Array.Resize(ref settings, expected);
\r
210 settings[expected - 1] = NotificationType.All;
\r
213 private void UpgradeParameterArray(ref int[] array)
\r
215 Array.Resize(ref array, Config.NotificationNames.Length);
\r
216 for (var i = 0; i < array.Length; i++)
\r
224 public class LocationPerMachine
\r
226 public string MachineName { get; set; }
\r
227 public Point Location { get; set; }
\r
228 public int Zoom { get; set; } = 100;
\r
229 public Point ListLocation { get; set; }
\r
230 public Size ListSize { get; set; }
\r
234 public enum Spoiler
\r
237 AirBattleResult = 1 << 1,
\r
238 BattleResult = 1 << 2,
\r
244 public enum TimerKind
\r
250 public class Config
\r
252 public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);
\r
253 public bool TopMost { get; set; }
\r
254 public bool HideOnMinimized { get; set; }
\r
255 public bool ExitSilently { get; set; }
\r
256 public int Zoom { get; set; } = 100;
\r
257 public bool SaveLocationPerMachine { get; set; }
\r
258 public List<LocationPerMachine> LocationList { get; set; } = new List<LocationPerMachine>();
\r
259 public bool ShowHpInPercent { get; set; }
\r
260 public TimerKind ShowEndTime { get; set; }
\r
261 public bool FlashWindow { get; set; } = true;
\r
262 // ReSharper disable once IdentifierTypo
\r
263 public bool ShowBaloonTip { get; set; } = true;
\r
264 public bool PlaySound { get; set; } = true;
\r
265 public NotificationType NotificationFlags { get; set; } = NotificationType.All;
\r
266 public NotificationConfig Notifications { get; set; } = new NotificationConfig();
\r
267 public int MarginShips { get; set; } = 5;
\r
268 public int MarginEquips { get; set; } = 5;
\r
269 public List<int> NotifyConditions { get; set; }
\r
270 public List<int> ResetHours { get; set; }
\r
271 public bool AlwaysShowResultRank { get; set; }
\r
272 public Spoiler Spoilers { get; set; }
\r
273 public bool UsePresetAkashi { get; set; }
\r
274 public SoundConfig Sounds { get; set; } = new SoundConfig();
\r
275 public bool DebugLogging { get; set; }
\r
276 public string DebugLogFile { get; set; } = "log.txt";
\r
277 public ProxyConfig Proxy { get; set; } = new ProxyConfig();
\r
278 public ShipListConfig ShipList { get; set; } = new ShipListConfig();
\r
279 public LogConfig Log { get; set; } = new LogConfig();
\r
280 public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();
\r
281 public PushoverConfig Pushover { get; set; } = new PushoverConfig();
\r
283 public static readonly string[] NotificationNames =
\r
285 "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",
\r
286 "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復", "任務達成"
\r
289 public static readonly Dictionary<string, int> NotificationIndex =
\r
290 NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);
\r
293 private const string FileName = "config.xml";
\r
294 public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
\r
295 private static readonly string ConfigFile = Path.Combine(BaseDir, FileName);
\r
299 ConvertPath(PrependBaseDir);
\r
302 public void InitializeValues()
\r
304 NotifyConditions = new List<int>(new[] {40, 49});
\r
305 ResetHours = new List<int>(new[] {2});
\r
312 var serializer = new XmlSerializer(typeof(Config));
\r
314 using (var file = File.OpenText(ConfigFile))
\r
315 config = (Config)serializer.Deserialize(file);
\r
316 foreach (var property in GetType().GetProperties())
\r
317 property.SetValue(this, property.GetValue(config, null), null);
\r
318 Notifications.Upgrade();
\r
319 ComposeNotificationFlags();
\r
321 if (AlwaysShowResultRank)
\r
323 Spoilers = Spoiler.All;
\r
324 AlwaysShowResultRank = false;
\r
326 if (SaveLocationPerMachine)
\r
328 foreach (var l in LocationList)
\r
330 if (l.MachineName != Environment.MachineName)
\r
332 Location = l.Location;
\r
334 ShipList.Location = l.ListLocation;
\r
335 ShipList.Size = l.ListSize;
\r
339 catch (FileNotFoundException)
\r
341 InitializeValues();
\r
344 catch (InvalidOperationException ex)
\r
346 throw new Exception(FileName + "が壊れています。", ex);
\r
348 ConvertPath(PrependBaseDir);
\r
351 private void ComposeNotificationFlags()
\r
353 NotificationFlags = (NotificationFlags & ~NotificationType.All) |
\r
354 (FlashWindow ? NotificationType.FlashWindow : 0) |
\r
355 (ShowBaloonTip ? NotificationType.ShowBaloonTip : 0) |
\r
356 (PlaySound ? NotificationType.PlaySound : 0);
\r
361 if (SaveLocationPerMachine)
\r
363 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();
\r
364 LocationList.Add(new LocationPerMachine
\r
366 MachineName = Environment.MachineName,
\r
367 Location = Location,
\r
369 ListLocation = ShipList.Location,
\r
370 ListSize = ShipList.Size
\r
375 LocationList = new List<LocationPerMachine>();
\r
377 DecomposeNotificationFlags();
\r
378 ConvertPath(StripBaseDir);
\r
379 var serializer = new XmlSerializer(typeof(Config));
\r
380 using (var file = File.CreateText(ConfigFile + ".tmp"))
\r
381 serializer.Serialize(file, this);
\r
382 File.Copy(ConfigFile + ".tmp", ConfigFile, true);
\r
383 File.Delete(ConfigFile + ".tmp");
\r
384 ConvertPath(PrependBaseDir);
\r
387 private void DecomposeNotificationFlags()
\r
389 FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0;
\r
390 ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0;
\r
391 PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0;
\r
394 private void ConvertPath(Func<string, string> func)
\r
396 DebugLogFile = func(DebugLogFile);
\r
397 Log.OutputDir = func(Log.OutputDir);
\r
398 for (var i = 0; i < Sounds.Files.Length; i++)
\r
399 Sounds.Files[i] = func(Sounds.Files[i]);
\r
402 private string StripBaseDir(string path)
\r
404 if (!path.StartsWith(BaseDir))
\r
406 path = path.Substring(BaseDir.Length);
\r
407 return path.TrimStart(Path.DirectorySeparatorChar);
\r
410 private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(BaseDir, path);
\r