OSDN Git Service

ファイルのヘッダをApache Licenseのものにする
[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 Codeplex.Data;\r
20 \r
21 namespace KancolleSniffer\r
22 {\r
23     public class ProxyConfig\r
24     {\r
25         public const int DefaultListenPort = 8080;\r
26         public const string AutoConfigUrl = "https://kancollesniffer.osdn.jp/proxy.pac";\r
27         public const string AutoConfigUrlWithPort = "https://kancollesniffer.osdn.jp/proxy.php?port=";\r
28         public bool Auto { get; set; }\r
29         public int Listen { get; set; }\r
30         public bool UseUpstream { get; set; }\r
31         public int UpstreamPort { get; set; }\r
32 \r
33         public ProxyConfig()\r
34         {\r
35             Auto = true;\r
36             Listen = DefaultListenPort;\r
37             UseUpstream = false;\r
38             UpstreamPort = 8888;\r
39         }\r
40     }\r
41 \r
42     public class ShipListConfig\r
43     {\r
44         public Point Location { get; set; }\r
45         public Size Size { get; set; }\r
46         public bool ShipType { get; set; }\r
47         public 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<int>[ShipListForm.GroupCount];\r
53             for (var i = 0; i < ShipGroup.Length; i++)\r
54                 ShipGroup[i] = new 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 Config\r
83     {\r
84         private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory;\r
85         private readonly string _configFileName;\r
86 \r
87         public Point Location { get; set; }\r
88         public bool TopMost { get; set; }\r
89         public bool HideOnMinimized { get; set; }\r
90         public bool FlashWindow { get; set; }\r
91         public bool ShowBaloonTip { get; set; }\r
92         public bool PlaySound { get; set; }\r
93         public int MarginShips { get; set; }\r
94         public int MarginEquips { get; set; }\r
95         public List<int> NotifyConditions { get; set; }\r
96         public List<int> ResetHours { get; set; }\r
97         public bool AlwaysShowResultRank { get; set; }\r
98         public bool UsePresetAkashi { get; set; }\r
99         public int SoundVolume { get; set; }\r
100         public string MissionSoundFile { get; set; }\r
101         public string NDockSoundFile { get; set; }\r
102         public string KDockSoundFile { get; set; }\r
103         public string MaxShipsSoundFile { get; set; }\r
104         public string MaxEquipsSoundFile { get; set; }\r
105         public string DamagedShipSoundFile { get; set; }\r
106         public string Akashi20MinSoundFile { get; set; }\r
107         public string AkashiProgressSoundFile { get; set; }\r
108         public string AkashiCompleteSoundFile { get; set; }\r
109         public string ConditionSoundFile { get; set; }\r
110         public bool DebugLogging { get; set; }\r
111         public string DebugLogFile { get; set; }\r
112         public ProxyConfig Proxy { get; set; }\r
113         public ShipListConfig ShipList { get; set; }\r
114         public LogConfig Log { get; set; }\r
115         public KancolleDbConfig KancolleDb { get; set; }\r
116 \r
117         public Config()\r
118         {\r
119             _configFileName = Path.Combine(_baseDir, "config.json");\r
120             Location = new Point(int.MinValue, int.MinValue);\r
121             FlashWindow = ShowBaloonTip = PlaySound = true;\r
122             MarginShips = 4;\r
123             MarginEquips = 10;\r
124             NotifyConditions = new List<int>(new[] {40, 49});\r
125             ResetHours = new List<int>(new[] {2});\r
126             AlwaysShowResultRank = false;\r
127             SoundVolume = 100;\r
128             MissionSoundFile = "ensei.mp3";\r
129             NDockSoundFile = "nyuukyo.mp3";\r
130             KDockSoundFile = "kenzou.mp3";\r
131             MaxShipsSoundFile = "kanmusu.mp3";\r
132             MaxEquipsSoundFile = "soubi.mp3";\r
133             DamagedShipSoundFile = "taiha.mp3";\r
134             Akashi20MinSoundFile = "20min.mp3";\r
135             AkashiProgressSoundFile = "syuuri.mp3";\r
136             AkashiCompleteSoundFile = "syuuri2.mp3";\r
137             ConditionSoundFile = "hirou.mp3";\r
138             DebugLogFile = "log.txt";\r
139             Proxy = new ProxyConfig();\r
140             ShipList = new ShipListConfig();\r
141             Log = new LogConfig();\r
142             KancolleDb = new KancolleDbConfig();\r
143             ConvertPath(PrependBaseDir);\r
144         }\r
145 \r
146         public void Load()\r
147         {\r
148             try\r
149             {\r
150                 var config = (Config)DynamicJson.Parse(File.ReadAllText(_configFileName));\r
151                 foreach (var property in GetType().GetProperties())\r
152                     property.SetValue(this, property.GetValue(config, null), null);\r
153                 ConvertPath(PrependBaseDir);\r
154             }\r
155             catch (FileNotFoundException)\r
156             {\r
157             }\r
158         }\r
159 \r
160         public void Save()\r
161         {\r
162             ConvertPath(StripBaseDir);\r
163             File.WriteAllText(_configFileName, DynamicJson.Serialize(this));\r
164         }\r
165 \r
166         private void ConvertPath(Func<string, string> func)\r
167         {\r
168             foreach (var property in GetType().GetProperties())\r
169             {\r
170                 if (!property.Name.EndsWith("File"))\r
171                     continue;\r
172                 property.SetValue(this, func((string)property.GetValue(this)));\r
173             }\r
174             Log.OutputDir = func(Log.OutputDir);\r
175         }\r
176 \r
177         private string StripBaseDir(string path)\r
178         {\r
179             if (!path.StartsWith(_baseDir))\r
180                 return path;\r
181             path = path.Substring(_baseDir.Length);\r
182             return path.StartsWith(Path.DirectorySeparatorChar.ToString()) ? path.Substring(1) : path;\r
183         }\r
184 \r
185         private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(_baseDir, path);\r
186     }\r
187 }