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
47 AircraftCarrier = 1 << 1,
\r
48 HeavyCruiser = 1 << 2,
\r
49 LightCruiser = 1 << 3,
\r
54 // ReSharper restore UnusedMember.Global
\r
58 public class ShipListConfig
\r
60 public bool Visible { get; set; }
\r
61 public Point Location { get; set; }
\r
62 public Size Size { get; set; }
\r
63 public string Mode { get; set; }
\r
64 public ShipCategory ShipCategories { get; set; } = ShipCategory.All;
\r
65 public bool ShipType { get; set; }
\r
66 public bool ShowHpInPercent { get; set; }
\r
67 public ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext;
\r
68 public List<List<int>> ShipGroup { get; set; }
\r
69 public bool AutoBattleResult { get; set; } = true;
\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
155 public class NotificationSpec
\r
157 public string Name { get; set; }
\r
158 public NotificationType Flags { get; set; }
\r
159 public int RepeatInterval { get; set; }
\r
160 public int PreliminaryPeriod { get; set; }
\r
163 public class NotificationConfig
\r
165 public NotificationType[] Settings =
\r
166 Config.NotificationNames.Select(x => NotificationType.All).ToArray();
\r
168 public int[] RepeatIntervals =
\r
169 Config.NotificationNames.Select(x => 0).ToArray();
\r
171 public int[] PreliminaryPeriods =
\r
172 Config.NotificationNames.Select(x => 0).ToArray();
\r
174 public NotificationSpec this[string name]
\r
176 get => new NotificationSpec
\r
179 Flags = Settings[Config.NotificationIndex[name]],
\r
180 RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]],
\r
181 PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]]
\r
185 Settings[Config.NotificationIndex[name]] = value.Flags;
\r
186 RepeatIntervals[Config.NotificationIndex[name]] = value.RepeatInterval;
\r
187 PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod;
\r
191 public void Upgrade()
\r
193 UpgradeSettings(ref Settings);
\r
194 UpgradeParameterArray(ref RepeatIntervals);
\r
195 UpgradeParameterArray(ref PreliminaryPeriods);
\r
198 private void UpgradeSettings(ref NotificationType[] settings)
\r
200 for (var i = 0; i < settings.Length; i++)
\r
202 if ((settings[i] & NotificationType.Pushbullet) != 0)
\r
203 settings[i] = settings[i] ^ NotificationType.Pushbullet | NotificationType.Push;
\r
205 var expected = Config.NotificationNames.Length;
\r
206 if (expected == settings.Length)
\r
208 Array.Resize(ref settings, expected);
\r
209 settings[expected - 1] = NotificationType.All;
\r
212 private void UpgradeParameterArray(ref int[] array)
\r
214 Array.Resize(ref array, Config.NotificationNames.Length);
\r
215 for (var i = 0; i < array.Length; i++)
\r
223 public class LocationPerMachine
\r
225 public string MachineName { get; set; }
\r
226 public Point Location { get; set; }
\r
227 public int Zoom { get; set; } = 100;
\r
228 public Point ListLocation { get; set; }
\r
229 public Size ListSize { get; set; }
\r
233 public enum Spoiler
\r
236 AirBattleResult = 1 << 1,
\r
237 BattleResult = 1 << 2,
\r
243 public enum TimerKind
\r
249 public class Config
\r
251 public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);
\r
252 public bool TopMost { get; set; }
\r
253 public bool HideOnMinimized { get; set; }
\r
254 public bool ExitSilently { get; set; }
\r
255 public int Zoom { get; set; } = 100;
\r
256 public int QuestLines { get; set; } = 6;
\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 bool WarnBadDamageWithDameCon { get; set; }
\r
275 public SoundConfig Sounds { get; set; } = new SoundConfig();
\r
276 public bool DebugLogging { get; set; }
\r
277 public string DebugLogFile { get; set; } = "log.txt";
\r
278 public ProxyConfig Proxy { get; set; } = new ProxyConfig();
\r
279 public ShipListConfig ShipList { get; set; } = new ShipListConfig();
\r
280 public LogConfig Log { get; set; } = new LogConfig();
\r
281 public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();
\r
282 public PushoverConfig Pushover { get; set; } = new PushoverConfig();
\r
284 public static readonly string[] NotificationNames =
\r
286 "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",
\r
287 "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復", "任務達成"
\r
290 public static readonly Dictionary<string, int> NotificationIndex =
\r
291 NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);
\r
294 private const string FileName = "config.xml";
\r
295 public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
\r
296 private static readonly string ConfigFile = Path.Combine(BaseDir, FileName);
\r
300 ConvertPath(PrependBaseDir);
\r
303 public void InitializeValues()
\r
305 NotifyConditions = new List<int>(new[] {40, 49});
\r
306 ResetHours = new List<int>(new[] {2});
\r
313 var serializer = new XmlSerializer(typeof(Config));
\r
315 using (var file = File.OpenText(ConfigFile))
\r
316 config = (Config)serializer.Deserialize(file);
\r
317 foreach (var property in GetType().GetProperties())
\r
318 property.SetValue(this, property.GetValue(config, null), null);
\r
319 Notifications.Upgrade();
\r
320 ComposeNotificationFlags();
\r
322 if (AlwaysShowResultRank)
\r
324 Spoilers = Spoiler.All;
\r
325 AlwaysShowResultRank = false;
\r
327 if (SaveLocationPerMachine)
\r
329 foreach (var l in LocationList)
\r
331 if (l.MachineName != Environment.MachineName)
\r
333 Location = l.Location;
\r
335 ShipList.Location = l.ListLocation;
\r
336 ShipList.Size = l.ListSize;
\r
340 catch (FileNotFoundException)
\r
342 InitializeValues();
\r
345 catch (InvalidOperationException ex)
\r
347 throw new Exception(FileName + "が壊れています。", ex);
\r
349 ConvertPath(PrependBaseDir);
\r
352 private void ComposeNotificationFlags()
\r
354 NotificationFlags = (NotificationFlags & ~NotificationType.All) |
\r
355 (FlashWindow ? NotificationType.FlashWindow : 0) |
\r
356 (ShowBaloonTip ? NotificationType.ShowBaloonTip : 0) |
\r
357 (PlaySound ? NotificationType.PlaySound : 0);
\r
362 if (SaveLocationPerMachine)
\r
364 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();
\r
365 LocationList.Add(new LocationPerMachine
\r
367 MachineName = Environment.MachineName,
\r
368 Location = Location,
\r
370 ListLocation = ShipList.Location,
\r
371 ListSize = ShipList.Size
\r
376 LocationList = new List<LocationPerMachine>();
\r
378 DecomposeNotificationFlags();
\r
379 ConvertPath(StripBaseDir);
\r
380 var serializer = new XmlSerializer(typeof(Config));
\r
381 using (var file = File.CreateText(ConfigFile + ".tmp"))
\r
382 serializer.Serialize(file, this);
\r
383 File.Copy(ConfigFile + ".tmp", ConfigFile, true);
\r
384 File.Delete(ConfigFile + ".tmp");
\r
385 ConvertPath(PrependBaseDir);
\r
388 private void DecomposeNotificationFlags()
\r
390 FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0;
\r
391 ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0;
\r
392 PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0;
\r
395 private void ConvertPath(Func<string, string> func)
\r
397 DebugLogFile = func(DebugLogFile);
\r
398 Log.OutputDir = func(Log.OutputDir);
\r
399 for (var i = 0; i < Sounds.Files.Length; i++)
\r
400 Sounds.Files[i] = func(Sounds.Files[i]);
\r
403 private string StripBaseDir(string path)
\r
405 if (!path.StartsWith(BaseDir))
\r
407 path = path.Substring(BaseDir.Length);
\r
408 return path.TrimStart(Path.DirectorySeparatorChar);
\r
411 private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(BaseDir, path);
\r