OSDN Git Service

艦これ統計データベースにデータを送信できるようにする
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Config.cs
1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 // \r
3 // This program is part of KancolleSniffer.\r
4 //\r
5 // KancolleSniffer is free software: you can redistribute it and/or modify\r
6 // it under the terms of the GNU General Public License as published by\r
7 // the Free Software Foundation, either version 3 of the License, or\r
8 // (at your option) any later version.\r
9 //\r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 //\r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.\r
17 \r
18 using System;\r
19 using System.Collections.Generic;\r
20 using System.Drawing;\r
21 using System.IO;\r
22 using Codeplex.Data;\r
23 \r
24 namespace KancolleSniffer\r
25 {\r
26     public class ProxyConfig\r
27     {\r
28         public const int DefaultListenPort = 8080;\r
29         public const string AutoConfigUrl = "https://kancollesniffer.osdn.jp/proxy.pac";\r
30         public const string AutoConfigUrlWithPort = "https://kancollesniffer.osdn.jp/proxy.php?port=";\r
31         public bool Auto { get; set; }\r
32         public int Listen { get; set; }\r
33         public bool UseUpstream { get; set; }\r
34         public int UpstreamPort { get; set; }\r
35 \r
36         public ProxyConfig()\r
37         {\r
38             Auto = true;\r
39             Listen = DefaultListenPort;\r
40             UseUpstream = false;\r
41             UpstreamPort = 8888;\r
42         }\r
43     }\r
44 \r
45     public class ShipListConfig\r
46     {\r
47         public Point Location { get; set; }\r
48         public Size Size { get; set; }\r
49         public bool ShipType { get; set; }\r
50         public List<int>[] ShipGroup { get; set; }\r
51 \r
52         public ShipListConfig()\r
53         {\r
54             Location = new Point(int.MinValue, int.MinValue);\r
55             ShipGroup = new List<int>[ShipListForm.GroupCount];\r
56             for (var i = 0; i < ShipGroup.Length; i++)\r
57                 ShipGroup[i] = new List<int>();\r
58         }\r
59     }\r
60 \r
61     public class LogConfig\r
62     {\r
63         public bool On { get; set; }\r
64         public string OutputDir { get; set; }\r
65         public int MaterialLogInterval { get; set; }\r
66         public bool ServerOn { get; set; }\r
67         public int Listen { get; set; }\r
68 \r
69         public LogConfig()\r
70         {\r
71             On = true;\r
72             OutputDir = "";\r
73             MaterialLogInterval = 10;\r
74             ServerOn = true;\r
75             Listen = 8008;\r
76         }\r
77     }\r
78 \r
79     public class KancolleDbConfig\r
80     {\r
81         public bool On { get; set; }\r
82         public string Token { get; set; } = "";\r
83     }\r
84 \r
85     public class Config\r
86     {\r
87         private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory;\r
88         private readonly string _configFileName;\r
89 \r
90         public Point Location { get; set; }\r
91         public bool TopMost { get; set; }\r
92         public bool HideOnMinimized { get; set; }\r
93         public bool FlashWindow { get; set; }\r
94         public bool ShowBaloonTip { get; set; }\r
95         public bool PlaySound { get; set; }\r
96         public int MarginShips { get; set; }\r
97         public int MarginEquips { get; set; }\r
98         public List<int> NotifyConditions { get; set; }\r
99         public List<int> ResetHours { get; set; }\r
100         public bool AlwaysShowResultRank { get; set; }\r
101         public bool UsePresetAkashi { get; set; }\r
102         public int SoundVolume { get; set; }\r
103         public string MissionSoundFile { get; set; }\r
104         public string NDockSoundFile { get; set; }\r
105         public string KDockSoundFile { get; set; }\r
106         public string MaxShipsSoundFile { get; set; }\r
107         public string MaxEquipsSoundFile { get; set; }\r
108         public string DamagedShipSoundFile { get; set; }\r
109         public string Akashi20MinSoundFile { get; set; }\r
110         public string AkashiProgressSoundFile { get; set; }\r
111         public string AkashiCompleteSoundFile { get; set; }\r
112         public string ConditionSoundFile { get; set; }\r
113         public bool DebugLogging { get; set; }\r
114         public string DebugLogFile { get; set; }\r
115         public ProxyConfig Proxy { get; set; }\r
116         public ShipListConfig ShipList { get; set; }\r
117         public LogConfig Log { get; set; }\r
118         public KancolleDbConfig KancolleDb { get; set; }\r
119 \r
120         public Config()\r
121         {\r
122             _configFileName = Path.Combine(_baseDir, "config.json");\r
123             Location = new Point(int.MinValue, int.MinValue);\r
124             FlashWindow = ShowBaloonTip = PlaySound = true;\r
125             MarginShips = 4;\r
126             MarginEquips = 10;\r
127             NotifyConditions = new List<int>(new[] {40, 49});\r
128             ResetHours = new List<int>(new[] {2});\r
129             AlwaysShowResultRank = false;\r
130             SoundVolume = 100;\r
131             MissionSoundFile = "ensei.mp3";\r
132             NDockSoundFile = "nyuukyo.mp3";\r
133             KDockSoundFile = "kenzou.mp3";\r
134             MaxShipsSoundFile = "kanmusu.mp3";\r
135             MaxEquipsSoundFile = "soubi.mp3";\r
136             DamagedShipSoundFile = "taiha.mp3";\r
137             Akashi20MinSoundFile = "20min.mp3";\r
138             AkashiProgressSoundFile = "syuuri.mp3";\r
139             AkashiCompleteSoundFile = "syuuri2.mp3";\r
140             ConditionSoundFile = "hirou.mp3";\r
141             DebugLogFile = "log.txt";\r
142             Proxy = new ProxyConfig();\r
143             ShipList = new ShipListConfig();\r
144             Log = new LogConfig();\r
145             KancolleDb = new KancolleDbConfig();\r
146             ConvertPath(PrependBaseDir);\r
147         }\r
148 \r
149         public void Load()\r
150         {\r
151             try\r
152             {\r
153                 var config = (Config)DynamicJson.Parse(File.ReadAllText(_configFileName));\r
154                 foreach (var property in GetType().GetProperties())\r
155                     property.SetValue(this, property.GetValue(config, null), null);\r
156                 ConvertPath(PrependBaseDir);\r
157             }\r
158             catch (FileNotFoundException)\r
159             {\r
160             }\r
161         }\r
162 \r
163         public void Save()\r
164         {\r
165             ConvertPath(StripBaseDir);\r
166             File.WriteAllText(_configFileName, DynamicJson.Serialize(this));\r
167         }\r
168 \r
169         private void ConvertPath(Func<string, string> func)\r
170         {\r
171             foreach (var property in GetType().GetProperties())\r
172             {\r
173                 if (!property.Name.EndsWith("File"))\r
174                     continue;\r
175                 property.SetValue(this, func((string)property.GetValue(this)));\r
176             }\r
177             Log.OutputDir = func(Log.OutputDir);\r
178         }\r
179 \r
180         private string StripBaseDir(string path)\r
181         {\r
182             if (!path.StartsWith(_baseDir))\r
183                 return path;\r
184             path = path.Substring(_baseDir.Length);\r
185             return path.StartsWith(Path.DirectorySeparatorChar.ToString()) ? path.Substring(1) : path;\r
186         }\r
187 \r
188         private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(_baseDir, path);\r
189     }\r
190 }