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
21 // ReSharper disable MemberCanBePrivate.Global
\r
22 // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
\r
23 // ReSharper disable FieldCanBeMadeReadOnly.Global
\r
25 namespace KancolleSniffer
\r
27 public class ProxyConfig
\r
29 private const int DefaultListenPort = 8080;
\r
30 public bool Auto { get; set; }
\r
31 public int Listen { get; set; }
\r
32 public bool UseUpstream { get; set; }
\r
33 public int UpstreamPort { get; set; }
\r
35 public ProxyConfig()
\r
38 Listen = DefaultListenPort;
\r
39 UseUpstream = false;
\r
40 UpstreamPort = 8888;
\r
45 public enum ShipCategory
\r
47 // ReSharper disable UnusedMember.Global
\r
50 AircraftCarrier = 1 << 1,
\r
51 HeavyCruiser = 1 << 2,
\r
52 LightCruiser = 1 << 3,
\r
57 // ReSharper restore UnusedMember.Global
\r
61 public class ShipListConfig
\r
63 public bool Visible { get; set; }
\r
64 public Point Location { get; set; }
\r
65 public Size Size { get; set; }
\r
66 public string Mode { get; set; }
\r
67 public ShipCategory ShipCategories { get; set; } = ShipCategory.All;
\r
68 public bool ShipType { get; set; }
\r
69 public bool ShowHpInPercent { get; set; }
\r
70 public ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext;
\r
71 public List<List<int>> ShipGroup { get; set; }
\r
72 public bool AutoBattleResult { get; set; } = true;
\r
74 public ShipListConfig()
\r
76 Location = new Point(int.MinValue, int.MinValue);
\r
77 ShipGroup = new List<List<int>>();
\r
81 public class LogConfig
\r
83 public bool On { get; set; }
\r
84 public string OutputDir { get; set; }
\r
85 public int MaterialLogInterval { get; set; }
\r
91 MaterialLogInterval = 10;
\r
95 public class PushbulletConfig
\r
97 public bool On { get; set; }
\r
98 public string Token { get; set; } = "";
\r
101 public class PushoverConfig
\r
103 public bool On { get; set; }
\r
104 public string ApiKey { get; set; } = "";
\r
105 public string UserKey { get; set; } = "";
\r
108 public class SoundConfig
\r
110 public int Volume { get; set; } = 100;
\r
112 public string[] Files =
\r
127 public string this[string name]
\r
129 get => Files[Config.NotificationIndex[name]];
\r
130 set => Files[Config.NotificationIndex[name]] = value;
\r
135 public enum NotificationType
\r
138 // ReSharper disable once IdentifierTypo
\r
139 ShowBaloonTip = 1 << 1,
\r
140 PlaySound = 1 << 2,
\r
141 All = (1 << 3) - 1,
\r
145 Preliminary = 1 << 7
\r
148 public class NotificationSpec
\r
150 public string Name { get; set; }
\r
151 public NotificationType Flags { get; set; }
\r
152 public int RepeatInterval { get; set; }
\r
153 public int PreliminaryPeriod { get; set; }
\r
156 public class NotificationConfig
\r
158 public NotificationType[] Settings =
\r
159 Config.NotificationNames.Select(x => NotificationType.All).ToArray();
\r
161 public int[] RepeatIntervals =
\r
162 Config.NotificationNames.Select(x => 0).ToArray();
\r
164 public int[] PreliminaryPeriods =
\r
165 Config.NotificationNames.Select(x => 0).ToArray();
\r
167 public NotificationSpec this[string name]
\r
169 get => new NotificationSpec
\r
172 Flags = Settings[Config.NotificationIndex[name]],
\r
173 RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]],
\r
174 PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]]
\r
178 Settings[Config.NotificationIndex[name]] = value.Flags;
\r
179 RepeatIntervals[Config.NotificationIndex[name]] = value.RepeatInterval;
\r
180 PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod;
\r
185 public class LocationPerMachine
\r
187 public string MachineName { get; set; }
\r
188 public Point Location { get; set; }
\r
189 public int Zoom { get; set; } = 100;
\r
190 public Point ListLocation { get; set; }
\r
191 public Size ListSize { get; set; }
\r
192 public List<ShipListConfig> ListFromGroup { get; set; }
\r
196 public enum Spoiler
\r
199 AirBattleResult = 1 << 1,
\r
200 BattleResult = 1 << 2,
\r
202 // ReSharper disable once UnusedMember.Global
\r
207 public enum TimerKind
\r
214 public class Config
\r
216 public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);
\r
217 public bool TopMost { get; set; }
\r
218 public bool HideOnMinimized { get; set; }
\r
219 public bool ExitSilently { get; set; }
\r
220 public int Zoom { get; set; } = 100;
\r
221 public int QuestLines { get; set; } = 6;
\r
222 public bool SaveLocationPerMachine { get; set; }
\r
223 public List<LocationPerMachine> LocationList { get; set; } = new List<LocationPerMachine>();
\r
224 public bool ShowHpInPercent { get; set; }
\r
225 public TimerKind ShowEndTime { get; set; }
\r
226 public NotificationType NotificationFlags { get; set; } = NotificationType.All;
\r
227 public NotificationConfig Notifications { get; set; } = new NotificationConfig();
\r
228 public int MarginShips { get; set; } = 5;
\r
229 public int MarginEquips { get; set; } = 5;
\r
230 public List<int> NotifyConditions { get; set; }
\r
231 public List<int> ResetHours { get; set; }
\r
232 public Spoiler Spoilers { get; set; }
\r
233 public bool UsePresetAkashi { get; set; }
\r
234 public bool WarnBadDamageWithDameCon { get; set; }
\r
235 public SoundConfig Sounds { get; set; } = new SoundConfig();
\r
236 public bool DebugLogging { get; set; }
\r
237 public string DebugLogFile { get; set; } = "log.txt";
\r
238 public ProxyConfig Proxy { get; set; } = new ProxyConfig();
\r
239 public ShipListConfig ShipList { get; set; } = new ShipListConfig();
\r
240 public List<ShipListConfig> ListFormGroup { get; set; } = new List<ShipListConfig>();
\r
241 public LogConfig Log { get; set; } = new LogConfig();
\r
242 public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();
\r
243 public PushoverConfig Pushover { get; set; } = new PushoverConfig();
\r
245 public static readonly string[] NotificationNames =
\r
247 "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",
\r
248 "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復", "任務達成"
\r
251 public static readonly Dictionary<string, int> NotificationIndex =
\r
252 NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);
\r
255 private const string FileName = "config.xml";
\r
256 public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
\r
257 private static readonly string ConfigFile = Path.Combine(BaseDir, FileName);
\r
261 ConvertPath(PrependBaseDir);
\r
264 private void InitializeValues()
\r
266 NotifyConditions = new List<int>(new[] {40, 49});
\r
267 ResetHours = new List<int>(new[] {2});
\r
274 var serializer = new XmlSerializer(typeof(Config));
\r
276 using (var file = File.OpenText(ConfigFile))
\r
277 config = (Config)serializer.Deserialize(file);
\r
278 foreach (var property in GetType().GetProperties())
\r
279 property.SetValue(this, property.GetValue(config, null), null);
\r
280 if (SaveLocationPerMachine)
\r
282 foreach (var l in LocationList)
\r
284 if (l.MachineName != Environment.MachineName)
\r
286 Location = l.Location;
\r
288 ShipList.Location = l.ListLocation;
\r
289 ShipList.Size = l.ListSize;
\r
290 ListFormGroup = l.ListFromGroup;
\r
294 catch (FileNotFoundException)
\r
296 InitializeValues();
\r
299 catch (InvalidOperationException ex)
\r
301 File.Delete(ConfigFile);
\r
302 throw new Exception(FileName + "が壊れています。", ex);
\r
304 ConvertPath(PrependBaseDir);
\r
309 if (SaveLocationPerMachine)
\r
311 LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList();
\r
312 LocationList.Add(new LocationPerMachine
\r
314 MachineName = Environment.MachineName,
\r
315 Location = Location,
\r
317 ListLocation = ShipList.Location,
\r
318 ListSize = ShipList.Size,
\r
319 ListFromGroup = ListFormGroup
\r
324 LocationList = new List<LocationPerMachine>();
\r
326 ConvertPath(StripBaseDir);
\r
327 var serializer = new XmlSerializer(typeof(Config));
\r
328 using (var file = File.CreateText(ConfigFile + ".tmp"))
\r
329 serializer.Serialize(file, this);
\r
330 File.Copy(ConfigFile + ".tmp", ConfigFile, true);
\r
331 File.Delete(ConfigFile + ".tmp");
\r
332 ConvertPath(PrependBaseDir);
\r
335 private void ConvertPath(Func<string, string> func)
\r
337 DebugLogFile = func(DebugLogFile);
\r
338 Log.OutputDir = func(Log.OutputDir);
\r
339 for (var i = 0; i < Sounds.Files.Length; i++)
\r
340 Sounds.Files[i] = func(Sounds.Files[i]);
\r
343 private string StripBaseDir(string path)
\r
345 if (!path.StartsWith(BaseDir))
\r
347 path = path.Substring(BaseDir.Length);
\r
348 return path.TrimStart(Path.DirectorySeparatorChar);
\r
351 private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(BaseDir, path);
\r