OSDN Git Service

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