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
41 public class ShipListConfig
\r
43 public Point Location { get; set; }
\r
44 public Size Size { get; set; }
\r
45 public bool ShipType { get; set; }
\r
46 public ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext;
\r
47 public List<List<int>> ShipGroup { get; set; }
\r
49 public ShipListConfig()
\r
51 Location = new Point(int.MinValue, int.MinValue);
\r
52 ShipGroup = new List<List<int>>();
\r
56 public class LogConfig
\r
58 public bool On { get; set; }
\r
59 public string OutputDir { get; set; }
\r
60 public int MaterialLogInterval { get; set; }
\r
66 MaterialLogInterval = 10;
\r
70 public class KancolleDbConfig
\r
72 public bool On { get; set; }
\r
73 public string Token { get; set; } = "";
\r
76 public class PushbulletConfig
\r
78 public bool On { get; set; }
\r
79 public string Token { get; set; } = "";
\r
82 public class SoundConfig
\r
84 public int Volume { get; set; } = 100;
\r
86 public string[] Files { get; set; } = {
\r
99 public string this[string name]
\r
101 get => Files[Config.NotificationIndex[name]];
\r
102 set => Files[Config.NotificationIndex[name]] = value;
\r
107 public enum NotificationType
\r
110 ShowBaloonTip = 1 << 1,
\r
111 PlaySound = 1 << 2,
\r
112 Pushbullet = 1 << 3,
\r
113 All = (1 << 3) - 1 // Pushbullet以外
\r
116 public class NotificationConfig
\r
118 public NotificationType[] Settings =
\r
119 Config.NotificationNames.Select(x => NotificationType.All).ToArray();
\r
121 public NotificationType this[string name]
\r
123 get => Settings[Config.NotificationIndex[name]];
\r
124 set => Settings[Config.NotificationIndex[name]] = value;
\r
128 public class Config
\r
130 private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory;
\r
131 private readonly string _configFileName;
\r
133 public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);
\r
134 public bool TopMost { get; set; }
\r
135 public bool HideOnMinimized { get; set; }
\r
136 public int Zoom { get; set; } = 100;
\r
137 public bool FlashWindow { get; set; } = true;
\r
138 public bool ShowBaloonTip { get; set; }
\r
139 public bool PlaySound { get; set; } = true;
\r
140 public NotificationConfig Notifications { get; set; } = new NotificationConfig();
\r
141 public int MarginShips { get; set; } = 4;
\r
142 public int MarginEquips { get; set; } = 10;
\r
143 public List<int> NotifyConditions { get; set; }
\r
144 public List<int> ResetHours { get; set; }
\r
145 public bool AlwaysShowResultRank { get; set; }
\r
146 public bool UsePresetAkashi { get; set; }
\r
147 public SoundConfig Sounds { get; set; } = new SoundConfig();
\r
148 public bool DebugLogging { get; set; }
\r
149 public string DebugLogFile { get; set; } = "log.txt";
\r
150 public ProxyConfig Proxy { get; set; } = new ProxyConfig();
\r
151 public ShipListConfig ShipList { get; set; } = new ShipListConfig();
\r
152 public LogConfig Log { get; set; } = new LogConfig();
\r
153 public KancolleDbConfig KancolleDb { get; set; } = new KancolleDbConfig();
\r
154 public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();
\r
156 public static readonly string[] NotificationNames =
\r
158 "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",
\r
159 "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復"
\r
162 public static readonly Dictionary<string, int> NotificationIndex =
\r
163 NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);
\r
167 _configFileName = Path.Combine(_baseDir, "config.xml");
\r
168 ConvertPath(PrependBaseDir);
\r
171 public void InitializeValues()
\r
173 NotifyConditions = new List<int>(new[] {40, 49});
\r
174 ResetHours = new List<int>(new[] {2});
\r
181 var serializer = new XmlSerializer(typeof(Config));
\r
183 using (var file = File.OpenText(_configFileName))
\r
184 config = (Config)serializer.Deserialize(file);
\r
185 foreach (var property in GetType().GetProperties())
\r
186 property.SetValue(this, property.GetValue(config, null), null);
\r
188 catch (FileNotFoundException)
\r
190 InitializeValues();
\r
194 ConvertPath(PrependBaseDir);
\r
199 ConvertPath(StripBaseDir);
\r
200 var serializer = new XmlSerializer(typeof(Config));
\r
201 using (var file = File.CreateText(_configFileName))
\r
202 serializer.Serialize(file, this);
\r
205 private void ConvertPath(Func<string, string> func)
\r
207 DebugLogFile = func(DebugLogFile);
\r
208 Log.OutputDir = func(Log.OutputDir);
\r
209 for (var i = 0; i < Sounds.Files.Length; i++)
\r
210 Sounds.Files[i] = func(Sounds.Files[i]);
\r
213 private string StripBaseDir(string path)
\r
215 if (!path.StartsWith(_baseDir))
\r
217 path = path.Substring(_baseDir.Length);
\r
218 return path.StartsWith(Path.DirectorySeparatorChar.ToString()) ? path.Substring(1) : path;
\r
221 private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(_baseDir, path);
\r
223 private void ReadOldConfig()
\r
225 var old = Path.Combine(_baseDir, "config.json");
\r
229 json = JsonParser.Parse(File.ReadAllText(old));
\r
231 catch (FileNotFoundException)
\r
235 Location = new Point((int)json.Location.X, (int)json.Location.Y);
\r
236 foreach (var property in (from prop in GetType().GetProperties()
\r
237 let type = prop.PropertyType
\r
238 where type == typeof(bool) || type == typeof(int) || type == typeof(string)
\r
241 if (!json.IsDefined(property.Name))
\r
243 var v = json[property.Name];
\r
244 property.SetValue(this, property.PropertyType == typeof(int) ? (int)v : v);
\r
246 NotifyConditions = new List<int>((int[])json.NotifyConditions);
\r
247 ResetHours = new List<int>((int[])json.ResetHours);
\r
248 Sounds.Volume = (int)json.SoundVolume;
\r
250 foreach (var name in new[]
\r
252 "Mission", "NDock", "KDock", "MaxShips", "MaxEquips",
\r
253 "DamagedShip", "Akashi20Min", "AkashiProgress", "AkashiComplete", "Condition"
\r
256 if (json.IsDefined(name + "SoundFile"))
\r
257 Sounds.Files[idx] = json[name + "SoundFile"];
\r
260 Proxy.Auto = json.Proxy.Auto;
\r
261 Proxy.Listen = (int)json.Proxy.Listen;
\r
262 Proxy.UseUpstream = json.Proxy.UseUpstream;
\r
263 Proxy.UpstreamPort = (int)json.Proxy.UpstreamPort;
\r
264 var sl = json.ShipList;
\r
265 ShipList.Location = new Point((int)sl.Location.X, (int)sl.Location.Y);
\r
266 ShipList.Size = new Size((int)sl.Size.Width, (int)sl.Size.Height);
\r
267 ShipList.ShipType = sl.ShipType;
\r
268 var sg = (int[][])sl.ShipGroup;
\r
269 ShipList.ShipGroup = new List<List<int>>();
\r
270 foreach (var g in sg)
\r
271 ShipList.ShipGroup.Add(new List<int>(g));
\r
272 Log.On = json.Log.On;
\r
273 Log.OutputDir = json.Log.OutputDir;
\r
274 Log.MaterialLogInterval = (int)json.Log.MaterialLogInterval;
\r