OSDN Git Service

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