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 { get; set; } =
\r
120 public string this[string name]
\r
122 get => Files[Config.NotificationIndex[name]];
\r
123 set => Files[Config.NotificationIndex[name]] = value;
\r
128 public enum NotificationType
\r
131 // ReSharper disable once IdentifierTypo
\r
132 ShowBaloonTip = 1 << 1,
\r
133 PlaySound = 1 << 2,
\r
134 All = (1 << 3) - 1,
\r
135 Pushbullet = 1 << 3,
\r
139 Preliminary = 1 << 7
\r
143 public class NotificationSpec
\r
145 public string Name { get; set; }
\r
146 public NotificationType Flags { get; set; }
\r
147 public int RepeatInterval { get; set; }
\r
148 public int PreliminaryPeriod { get; set; }
\r
151 public class NotificationConfig
\r
153 public NotificationType[] Settings =
\r
154 Config.NotificationNames.Select(x => NotificationType.All).ToArray();
\r
156 public int[] RepeatIntervals =
\r
157 Config.NotificationNames.Select(x => 0).ToArray();
\r
159 public int[] PreliminaryPeriods =
\r
160 Config.NotificationNames.Select(x => 0).ToArray();
\r
162 public NotificationSpec this[string name]
\r
164 get => new NotificationSpec
\r
167 Flags = Settings[Config.NotificationIndex[name]],
\r
168 RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]],
\r
169 PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]]
\r
173 Settings[Config.NotificationIndex[name]] = value.Flags;
\r
174 RepeatIntervals[Config.NotificationIndex[name]] = value.RepeatInterval;
\r
175 PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod;
\r
179 public void Normalization()
\r
181 Settings = Settings.Select(s =>
\r
182 (s & NotificationType.Pushbullet) != 0
\r
183 ? s ^ NotificationType.Pushbullet | NotificationType.Push
\r
186 RepeatIntervals = RepeatIntervals.Select(v => v < 0 ? 0 : v).ToArray();
\r
187 PreliminaryPeriods = PreliminaryPeriods.Select(v => v < 0 ? 0 : v).ToArray();
\r
191 public class LocationPerMachine
\r
193 public string MachineName { get; set; }
\r
194 public Point Location { get; set; }
\r
195 public int Zoom { get; set; } = 100;
\r
196 public Point ListLocation { get; set; }
\r
197 public Size ListSize { get; set; }
\r
201 public enum Spoiler
\r
204 AirBattleResult = 1 << 1,
\r
205 BattleResult = 1 << 2,
\r
211 public enum TimerKind
\r
217 public class Config
\r
219 public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);
\r
220 public bool TopMost { get; set; }
\r
221 public bool HideOnMinimized { get; set; }
\r
222 public bool ExitSilently { get; set; }
\r
223 public int Zoom { get; set; } = 100;
\r
224 public bool SaveLocationPerMachine { get; set; }
\r
225 public List<LocationPerMachine> LocationList { get; set; } = new List<LocationPerMachine>();
\r
226 public bool ShowHpInPercent { get; set; }
\r
227 public TimerKind ShowEndTime { get; set; }
\r
228 public bool FlashWindow { get; set; } = true;
\r
229 // ReSharper disable once IdentifierTypo
\r
230 public bool ShowBaloonTip { get; set; } = true;
\r
231 public bool PlaySound { get; set; } = true;
\r
232 public NotificationType NotificationFlags { get; set; } = NotificationType.All;
\r
233 public NotificationConfig Notifications { get; set; } = new NotificationConfig();
\r
234 public int MarginShips { get; set; } = 5;
\r
235 public int MarginEquips { get; set; } = 5;
\r
236 public List<int> NotifyConditions { get; set; }
\r
237 public List<int> ResetHours { get; set; }
\r
238 public bool AlwaysShowResultRank { get; set; }
\r
239 public Spoiler Spoilers { get; set; }
\r
240 public bool UsePresetAkashi { get; set; }
\r
241 public SoundConfig Sounds { get; set; } = new SoundConfig();
\r
242 public bool DebugLogging { get; set; }
\r
243 public string DebugLogFile { get; set; } = "log.txt";
\r
244 public ProxyConfig Proxy { get; set; } = new ProxyConfig();
\r
245 public ShipListConfig ShipList { get; set; } = new ShipListConfig();
\r
246 public LogConfig Log { get; set; } = new LogConfig();
\r
247 public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();
\r
248 public PushoverConfig Pushover { get; set; } = new PushoverConfig();
\r
250 public static readonly string[] NotificationNames =
\r
252 "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",
\r
253 "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復"
\r
256 public static readonly Dictionary<string, int> NotificationIndex =
\r
257 NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);
\r
260 private const string FileName = "config.xml";
\r
261 public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
\r
262 private static readonly string ConfigFile = Path.Combine(BaseDir, FileName);
\r
266 ConvertPath(PrependBaseDir);
\r
269 public void InitializeValues()
\r
271 NotifyConditions = new List<int>(new[] {40, 49});
\r
272 ResetHours = new List<int>(new[] {2});
\r
279 var serializer = new XmlSerializer(typeof(Config));
\r
281 using (var file = File.OpenText(ConfigFile))
\r
282 config = (Config)serializer.Deserialize(file);
\r
283 foreach (var property in GetType().GetProperties())
\r
284 property.SetValue(this, property.GetValue(config, null), null);
\r
285 Notifications.Normalization();
\r
286 ComposeNotificationFlags();
\r
287 if (AlwaysShowResultRank)
\r
289 Spoilers = Spoiler.All;
\r
290 AlwaysShowResultRank = false;
\r
292 if (SaveLocationPerMachine)
\r
294 foreach (var l in LocationList)
\r
296 if (l.MachineName != Environment.MachineName)
\r
298 Location = l.Location;
\r
300 ShipList.Location = l.ListLocation;
\r
301 ShipList.Size = l.ListSize;
\r
305 catch (FileNotFoundException)
\r
307 InitializeValues();
\r
310 catch (InvalidOperationException ex)
\r
312 throw new Exception(FileName + "が壊れています。", ex);
\r
314 ConvertPath(PrependBaseDir);
\r
317 private void ComposeNotificationFlags()
\r
319 NotificationFlags = (NotificationFlags & ~NotificationType.All) |
\r
320 (FlashWindow ? NotificationType.FlashWindow : 0) |
\r
321 (ShowBaloonTip ? NotificationType.ShowBaloonTip : 0) |
\r
322 (PlaySound ? NotificationType.PlaySound : 0);
\r
327 if (SaveLocationPerMachine)
\r
329 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();
\r
330 LocationList.Add(new LocationPerMachine
\r
332 MachineName = Environment.MachineName,
\r
333 Location = Location,
\r
335 ListLocation = ShipList.Location,
\r
336 ListSize = ShipList.Size
\r
341 LocationList = new List<LocationPerMachine>();
\r
343 DecomposeNotificationFlags();
\r
344 ConvertPath(StripBaseDir);
\r
345 var serializer = new XmlSerializer(typeof(Config));
\r
346 using (var file = File.CreateText(ConfigFile + ".tmp"))
\r
347 serializer.Serialize(file, this);
\r
348 File.Copy(ConfigFile + ".tmp", ConfigFile, true);
\r
349 File.Delete(ConfigFile + ".tmp");
\r
350 ConvertPath(PrependBaseDir);
\r
353 private void DecomposeNotificationFlags()
\r
355 FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0;
\r
356 ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0;
\r
357 PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0;
\r
360 private void ConvertPath(Func<string, string> func)
\r
362 DebugLogFile = func(DebugLogFile);
\r
363 Log.OutputDir = func(Log.OutputDir);
\r
364 for (var i = 0; i < Sounds.Files.Length; i++)
\r
365 Sounds.Files[i] = func(Sounds.Files[i]);
\r
368 private string StripBaseDir(string path)
\r
370 if (!path.StartsWith(BaseDir))
\r
372 path = path.Substring(BaseDir.Length);
\r
373 return path.TrimStart(Path.DirectorySeparatorChar);
\r
376 private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(BaseDir, path);
\r