OSDN Git Service

List<T>型の設定が空のときに設定が初期値に戻るのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Config.cs
1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 // \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
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\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
14 \r
15 using System;\r
16 using System.Collections.Generic;\r
17 using System.Drawing;\r
18 using System.IO;\r
19 using System.Linq;\r
20 using System.Xml.Serialization;\r
21 \r
22 namespace KancolleSniffer\r
23 {\r
24     public class ProxyConfig\r
25     {\r
26         public const int DefaultListenPort = 8080;\r
27         public const string AutoConfigUrl = "https://kancollesniffer.osdn.jp/proxy.pac";\r
28         public const string AutoConfigUrlWithPort = "https://kancollesniffer.osdn.jp/proxy.php?port=";\r
29         public bool Auto { get; set; }\r
30         public int Listen { get; set; }\r
31         public bool UseUpstream { get; set; }\r
32         public int UpstreamPort { get; set; }\r
33 \r
34         public ProxyConfig()\r
35         {\r
36             Auto = true;\r
37             Listen = DefaultListenPort;\r
38             UseUpstream = false;\r
39             UpstreamPort = 8888;\r
40         }\r
41     }\r
42 \r
43     public class ShipListConfig\r
44     {\r
45         public Point Location { get; set; }\r
46         public Size Size { get; set; }\r
47         public bool ShipType { get; set; }\r
48         public List<List<int>> ShipGroup { get; set; }\r
49 \r
50         public ShipListConfig()\r
51         {\r
52             Location = new Point(int.MinValue, int.MinValue);\r
53             ShipGroup = new List<List<int>>();\r
54         }\r
55     }\r
56 \r
57     public class LogConfig\r
58     {\r
59         public bool On { get; set; }\r
60         public string OutputDir { get; set; }\r
61         public int MaterialLogInterval { get; set; }\r
62         public bool ServerOn { get; set; }\r
63         public int Listen { get; set; }\r
64 \r
65         public LogConfig()\r
66         {\r
67             On = true;\r
68             OutputDir = "";\r
69             MaterialLogInterval = 10;\r
70             ServerOn = true;\r
71             Listen = 8008;\r
72         }\r
73     }\r
74 \r
75     public class KancolleDbConfig\r
76     {\r
77         public bool On { get; set; }\r
78         public string Token { get; set; } = "";\r
79     }\r
80 \r
81     public class SoundConfig\r
82     {\r
83         public int Volume { get; set; } = 100;\r
84 \r
85         public string[] Files { get; set; } = {\r
86             "ensei.mp3",\r
87             "nyuukyo.mp3",\r
88             "kenzou.mp3",\r
89             "kanmusu.mp3",\r
90             "soubi.mp3",\r
91             "taiha.mp3",\r
92             "20min.mp3",\r
93             "syuuri.mp3",\r
94             "syuuri2.mp3",\r
95             "hirou.mp3"\r
96         };\r
97 \r
98         public readonly string[] SoundNames =\r
99         {\r
100             "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
101             "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復"\r
102         };\r
103 \r
104         private readonly Dictionary<string, int> _names = new Dictionary<string, int>();\r
105 \r
106         public SoundConfig()\r
107         {\r
108             var idx = 0;\r
109             foreach (var name in SoundNames)\r
110                 _names[name] = idx++;\r
111         }\r
112 \r
113         public string this[string name]\r
114         {\r
115             get { return Files[_names[name]]; }\r
116             set { Files[_names[name]] = value; }\r
117         }\r
118     }\r
119 \r
120     public class Config\r
121     {\r
122         private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory;\r
123         private readonly string _configFileName;\r
124 \r
125         public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);\r
126         public bool TopMost { get; set; }\r
127         public bool HideOnMinimized { get; set; }\r
128         public bool FlashWindow { get; set; } = true;\r
129         public bool ShowBaloonTip { get; set; }\r
130         public bool PlaySound { get; set; } = true;\r
131         public int MarginShips { get; set; } = 4;\r
132         public int MarginEquips { get; set; } = 10;\r
133         public List<int> NotifyConditions { get; set; }\r
134         public List<int> ResetHours { get; set; }\r
135         public bool AlwaysShowResultRank { get; set; }\r
136         public bool UsePresetAkashi { get; set; }\r
137         public SoundConfig Sounds { get; set; } = new SoundConfig();\r
138         public bool DebugLogging { get; set; }\r
139         public string DebugLogFile { get; set; } = "log.txt";\r
140         public ProxyConfig Proxy { get; set; } = new ProxyConfig();\r
141         public ShipListConfig ShipList { get; set; } = new ShipListConfig();\r
142         public LogConfig Log { get; set; } = new LogConfig();\r
143         public KancolleDbConfig KancolleDb { get; set; } = new KancolleDbConfig();\r
144 \r
145         public Config()\r
146         {\r
147             _configFileName = Path.Combine(_baseDir, "config.xml");\r
148             ConvertPath(PrependBaseDir);\r
149         }\r
150 \r
151         public void InitializeValues()\r
152         {\r
153             NotifyConditions = new List<int>(new[] {40, 49});\r
154             ResetHours = new List<int>(new int[] {2});\r
155         }\r
156 \r
157         public void Load()\r
158         {\r
159             try\r
160             {\r
161                 var serializer = new XmlSerializer(typeof(Config));\r
162                 Config config;\r
163                 using (var file = File.OpenText(_configFileName))\r
164                     config = (Config)serializer.Deserialize(file);\r
165                 foreach (var property in GetType().GetProperties())\r
166                     property.SetValue(this, property.GetValue(config, null), null);\r
167             }\r
168             catch (FileNotFoundException)\r
169             {\r
170                 InitializeValues();\r
171                 ReadOldConfig();\r
172                 Save();\r
173             }\r
174             ConvertPath(PrependBaseDir);\r
175         }\r
176 \r
177         public void Save()\r
178         {\r
179             ConvertPath(StripBaseDir);\r
180             var serializer = new XmlSerializer(typeof(Config));\r
181             using (var file = File.CreateText(_configFileName))\r
182                 serializer.Serialize(file, this);\r
183         }\r
184 \r
185         private void ConvertPath(Func<string, string> func)\r
186         {\r
187             DebugLogFile = func(DebugLogFile);\r
188             Log.OutputDir = func(Log.OutputDir);\r
189             for (var i = 0; i < Sounds.Files.Length; i++)\r
190                 Sounds.Files[i] = func(Sounds.Files[i]);\r
191         }\r
192 \r
193         private string StripBaseDir(string path)\r
194         {\r
195             if (!path.StartsWith(_baseDir))\r
196                 return path;\r
197             path = path.Substring(_baseDir.Length);\r
198             return path.StartsWith(Path.DirectorySeparatorChar.ToString()) ? path.Substring(1) : path;\r
199         }\r
200 \r
201         private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(_baseDir, path);\r
202 \r
203         private void ReadOldConfig()\r
204         {\r
205             var old = Path.Combine(_baseDir, "config.json");\r
206             dynamic json;\r
207             try\r
208             {\r
209                 json = JsonParser.Parse(File.ReadAllText(old));\r
210             }\r
211             catch (FileNotFoundException)\r
212             {\r
213                 return;\r
214             }\r
215             Location = new Point((int)json.Location.X, (int)json.Location.Y);\r
216             foreach (var property in (from prop in GetType().GetProperties()\r
217                 let type = prop.PropertyType\r
218                 where type == typeof(bool) || type == typeof(int) || type == typeof(string)\r
219                 select prop))\r
220             {\r
221                 if (!json.IsDefined(property.Name))\r
222                     continue;\r
223                 var v = json[property.Name];\r
224                 property.SetValue(this, property.PropertyType == typeof(int) ? (int)v : v);\r
225             }\r
226             NotifyConditions = new List<int>((int[])json.NotifyConditions);\r
227             ResetHours = new List<int>((int[])json.ResetHours);\r
228             Sounds.Volume = (int)json.SoundVolume;\r
229             var idx = 0;\r
230             foreach (var name in new[]\r
231             {\r
232                 "Mission", "NDock", "KDock", "MaxShips", "MaxEquips",\r
233                 "DamagedShip", "Akashi20Min", "AkashiProgress", "AkashiComplete", "Condition"\r
234             })\r
235             {\r
236                 if (json.IsDefined(name + "SoundFile"))\r
237                     Sounds.Files[idx] = json[name + "SoundFile"];\r
238                 idx++;\r
239             }\r
240             Proxy.Auto = json.Proxy.Auto;\r
241             Proxy.Listen = (int)json.Proxy.Listen;\r
242             Proxy.UseUpstream = json.Proxy.UseUpstream;\r
243             Proxy.UpstreamPort = (int)json.Proxy.UpstreamPort;\r
244             var sl = json.ShipList;\r
245             ShipList.Location = new Point((int)sl.Location.X, (int)sl.Location.Y);\r
246             ShipList.Size = new Size((int)sl.Size.Width, (int)sl.Size.Height);\r
247             ShipList.ShipType = sl.ShipType;\r
248             var sg = (int[][])sl.ShipGroup;\r
249             ShipList.ShipGroup = new List<List<int>>();\r
250             foreach (var g in sg)\r
251                 ShipList.ShipGroup.Add(new List<int>(g));\r
252             Log.On = json.Log.On;\r
253             Log.OutputDir = json.Log.OutputDir;\r
254             Log.MaterialLogInterval = (int)json.Log.MaterialLogInterval;\r
255             Log.ServerOn = json.Log.ServerOn;\r
256             Log.Listen = (int)json.Log.Listen;\r
257         }\r
258     }\r
259 }