OSDN Git Service

設定ファイルの形式をXMLに変更する
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Wed, 25 Nov 2015 12:10:44 +0000 (21:10 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Tue, 29 Dec 2015 12:20:41 +0000 (21:20 +0900)
KancolleSniffer/Config.cs
KancolleSniffer/ConfigDialog.cs
KancolleSniffer/MainForm.cs
KancolleSniffer/ShipListForm.cs

index ba8085e..ef8f74c 100644 (file)
@@ -16,7 +16,8 @@ using System;
 using System.Collections.Generic;\r
 using System.Drawing;\r
 using System.IO;\r
-using Codeplex.Data;\r
+using System.Linq;\r
+using System.Xml.Serialization;\r
 \r
 namespace KancolleSniffer\r
 {\r
@@ -44,14 +45,12 @@ namespace KancolleSniffer
         public Point Location { get; set; }\r
         public Size Size { get; set; }\r
         public bool ShipType { get; set; }\r
-        public List<int>[] ShipGroup { get; set; }\r
+        public List<List<int>> ShipGroup { get; set; }\r
 \r
         public ShipListConfig()\r
         {\r
             Location = new Point(int.MinValue, int.MinValue);\r
-            ShipGroup = new List<int>[ShipListForm.GroupCount];\r
-            for (var i = 0; i < ShipGroup.Length; i++)\r
-                ShipGroup[i] = new List<int>();\r
+            ShipGroup = new List<List<int>>();\r
         }\r
     }\r
 \r
@@ -79,67 +78,73 @@ namespace KancolleSniffer
         public string Token { get; set; } = "";\r
     }\r
 \r
+    public class SoundConfig\r
+    {\r
+        public int Volume { get; set; } = 100;\r
+\r
+        public string[] Files { get; set; } = {\r
+            "ensei.mp3",\r
+            "nyuukyo.mp3",\r
+            "kenzou.mp3",\r
+            "kanmusu.mp3",\r
+            "soubi.mp3",\r
+            "taiha.mp3",\r
+            "20min.mp3",\r
+            "syuuri.mp3",\r
+            "syuuri2.mp3",\r
+            "hirou.mp3"\r
+        };\r
+\r
+        public readonly string[] SoundNames =\r
+        {\r
+            "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
+            "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復"\r
+        };\r
+\r
+        private readonly Dictionary<string, int> _names = new Dictionary<string, int>();\r
+\r
+        public SoundConfig()\r
+        {\r
+            var idx = 0;\r
+            foreach (var name in SoundNames)\r
+                _names[name] = idx++;\r
+        }\r
+\r
+        public string this[string name]\r
+        {\r
+            get { return Files[_names[name]]; }\r
+            set { Files[_names[name]] = value; }\r
+        }\r
+    }\r
+\r
     public class Config\r
     {\r
         private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory;\r
         private readonly string _configFileName;\r
 \r
-        public Point Location { get; set; }\r
+        public Point Location { get; set; } = new Point(int.MinValue, int.MinValue);\r
         public bool TopMost { get; set; }\r
         public bool HideOnMinimized { get; set; }\r
-        public bool FlashWindow { get; set; }\r
+        public bool FlashWindow { get; set; } = true;\r
         public bool ShowBaloonTip { get; set; }\r
-        public bool PlaySound { get; set; }\r
-        public int MarginShips { get; set; }\r
-        public int MarginEquips { get; set; }\r
-        public List<int> NotifyConditions { get; set; }\r
-        public List<int> ResetHours { get; set; }\r
+        public bool PlaySound { get; set; } = true;\r
+        public int MarginShips { get; set; } = 4;\r
+        public int MarginEquips { get; set; } = 10;\r
+        public List<int> NotifyConditions { get; set; } = new List<int>(new[] {40, 49});\r
+        public List<int> ResetHours { get; set; } = new List<int>(new[] {2});\r
         public bool AlwaysShowResultRank { get; set; }\r
         public bool UsePresetAkashi { get; set; }\r
-        public int SoundVolume { get; set; }\r
-        public string MissionSoundFile { get; set; }\r
-        public string NDockSoundFile { get; set; }\r
-        public string KDockSoundFile { get; set; }\r
-        public string MaxShipsSoundFile { get; set; }\r
-        public string MaxEquipsSoundFile { get; set; }\r
-        public string DamagedShipSoundFile { get; set; }\r
-        public string Akashi20MinSoundFile { get; set; }\r
-        public string AkashiProgressSoundFile { get; set; }\r
-        public string AkashiCompleteSoundFile { get; set; }\r
-        public string ConditionSoundFile { get; set; }\r
+        public SoundConfig Sounds { get; set; } = new SoundConfig();\r
         public bool DebugLogging { get; set; }\r
-        public string DebugLogFile { get; set; }\r
-        public ProxyConfig Proxy { get; set; }\r
-        public ShipListConfig ShipList { get; set; }\r
-        public LogConfig Log { get; set; }\r
-        public KancolleDbConfig KancolleDb { get; set; }\r
+        public string DebugLogFile { get; set; } = "log.txt";\r
+        public ProxyConfig Proxy { get; set; } = new ProxyConfig();\r
+        public ShipListConfig ShipList { get; set; } = new ShipListConfig();\r
+        public LogConfig Log { get; set; } = new LogConfig();\r
+        public KancolleDbConfig KancolleDb { get; set; } = new KancolleDbConfig();\r
 \r
         public Config()\r
         {\r
-            _configFileName = Path.Combine(_baseDir, "config.json");\r
-            Location = new Point(int.MinValue, int.MinValue);\r
-            FlashWindow = ShowBaloonTip = PlaySound = true;\r
-            MarginShips = 4;\r
-            MarginEquips = 10;\r
-            NotifyConditions = new List<int>(new[] {40, 49});\r
-            ResetHours = new List<int>(new[] {2});\r
-            AlwaysShowResultRank = false;\r
-            SoundVolume = 100;\r
-            MissionSoundFile = "ensei.mp3";\r
-            NDockSoundFile = "nyuukyo.mp3";\r
-            KDockSoundFile = "kenzou.mp3";\r
-            MaxShipsSoundFile = "kanmusu.mp3";\r
-            MaxEquipsSoundFile = "soubi.mp3";\r
-            DamagedShipSoundFile = "taiha.mp3";\r
-            Akashi20MinSoundFile = "20min.mp3";\r
-            AkashiProgressSoundFile = "syuuri.mp3";\r
-            AkashiCompleteSoundFile = "syuuri2.mp3";\r
-            ConditionSoundFile = "hirou.mp3";\r
-            DebugLogFile = "log.txt";\r
-            Proxy = new ProxyConfig();\r
-            ShipList = new ShipListConfig();\r
-            Log = new LogConfig();\r
-            KancolleDb = new KancolleDbConfig();\r
+            _configFileName = Path.Combine(_baseDir, "config.xml");\r
             ConvertPath(PrependBaseDir);\r
         }\r
 \r
@@ -147,31 +152,35 @@ namespace KancolleSniffer
         {\r
             try\r
             {\r
-                var config = (Config)DynamicJson.Parse(File.ReadAllText(_configFileName));\r
+                var serializer = new XmlSerializer(typeof(Config));\r
+                Config config;\r
+                using (var file = File.OpenText(_configFileName))\r
+                    config = (Config)serializer.Deserialize(file);\r
                 foreach (var property in GetType().GetProperties())\r
                     property.SetValue(this, property.GetValue(config, null), null);\r
-                ConvertPath(PrependBaseDir);\r
             }\r
             catch (FileNotFoundException)\r
             {\r
+                ReadOldConfig();\r
+                Save();\r
             }\r
+            ConvertPath(PrependBaseDir);\r
         }\r
 \r
         public void Save()\r
         {\r
             ConvertPath(StripBaseDir);\r
-            File.WriteAllText(_configFileName, DynamicJson.Serialize(this));\r
+            var serializer = new XmlSerializer(typeof(Config));\r
+            using (var file = File.CreateText(_configFileName))\r
+                serializer.Serialize(file, this);\r
         }\r
 \r
         private void ConvertPath(Func<string, string> func)\r
         {\r
-            foreach (var property in GetType().GetProperties())\r
-            {\r
-                if (!property.Name.EndsWith("File"))\r
-                    continue;\r
-                property.SetValue(this, func((string)property.GetValue(this)));\r
-            }\r
+            DebugLogFile = func(DebugLogFile);\r
             Log.OutputDir = func(Log.OutputDir);\r
+            for (var i = 0; i < Sounds.Files.Length; i++)\r
+                Sounds.Files[i] = func(Sounds.Files[i]);\r
         }\r
 \r
         private string StripBaseDir(string path)\r
@@ -183,5 +192,53 @@ namespace KancolleSniffer
         }\r
 \r
         private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(_baseDir, path);\r
+\r
+        private void ReadOldConfig()\r
+        {\r
+            var old = Path.Combine(_baseDir, "config.json");\r
+            var json = (dynamic)JsonParser.Parse(File.ReadAllText(old));\r
+            Location = new Point((int)json.Location.X, (int)json.Location.Y);\r
+            foreach (var property in (from prop in GetType().GetProperties()\r
+                let type = prop.PropertyType\r
+                where type == typeof(bool) || type == typeof(int) || type == typeof(string)\r
+                select prop))\r
+            {\r
+                if (!json.IsDefined(property.Name))\r
+                    continue;\r
+                var v = json[property.Name];\r
+                property.SetValue(this, property.PropertyType == typeof(int) ? (int)v : v);\r
+            }\r
+            NotifyConditions = new List<int>((int[])json.NotifyConditions);\r
+            ResetHours = new List<int>((int[])json.ResetHours);\r
+            Sounds.Volume = (int)json.SoundVolume;\r
+            var idx = 0;\r
+            foreach (var name in new[]\r
+            {\r
+                "Mission", "NDock", "KDock", "MaxShips", "MaxEquips",\r
+                "DamagedShip", "Akashi20Min", "AkashiProgress", "AkashiComplete", "Condition"\r
+            })\r
+            {\r
+                if (json.IsDefined(name + "SoundFile"))\r
+                    Sounds.Files[idx] = json[name + "SoundFile"];\r
+                idx++;\r
+            }\r
+            Proxy.Auto = json.Proxy.Auto;\r
+            Proxy.Listen = (int)json.Proxy.Listen;\r
+            Proxy.UseUpstream = json.Proxy.UseUpstream;\r
+            Proxy.UpstreamPort = (int)json.Proxy.UpstreamPort;\r
+            var sl = json.ShipList;\r
+            ShipList.Location = new Point((int)sl.Location.X, (int)sl.Location.Y);\r
+            ShipList.Size = new Size((int)sl.Size.Width, (int)sl.Size.Height);\r
+            ShipList.ShipType = sl.ShipType;\r
+            var sg = (int[][])sl.ShipGroup;\r
+            ShipList.ShipGroup = new List<List<int>>();\r
+            foreach (var g in sg)\r
+                ShipList.ShipGroup.Add(new List<int>(g));\r
+            Log.On = json.Log.On;\r
+            Log.OutputDir = json.Log.OutputDir;\r
+            Log.MaterialLogInterval = (int)json.Log.MaterialLogInterval;\r
+            Log.ServerOn = json.Log.ServerOn;\r
+            Log.Listen = (int)json.Log.Listen;\r
+        }\r
     }\r
 }
\ No newline at end of file
index 0e854ee..feb6fb7 100644 (file)
@@ -34,11 +34,8 @@ namespace KancolleSniffer
             InitializeComponent();\r
             _config = config;\r
             _main = main;\r
-            listBoxSoundFile.Items.AddRange(new object[]\r
-            {\r
-                "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
-                "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復"\r
-            });\r
+            // ReSharper disable once CoVariantArrayConversion\r
+            listBoxSoundFile.Items.AddRange(_config.Sounds.SoundNames);\r
             numericUpDownMaterialLogInterval.Maximum = 1440;\r
         }\r
 \r
@@ -63,19 +60,9 @@ namespace KancolleSniffer
             radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;\r
             checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;\r
 \r
-            numericUpDownSoundVolume.Value = _config.SoundVolume;\r
-\r
-            _soundSetting["遠征終了"] = _config.MissionSoundFile;\r
-            _soundSetting["入渠終了"] = _config.NDockSoundFile;\r
-            _soundSetting["建造完了"] = _config.KDockSoundFile;\r
-            _soundSetting["艦娘数超過"] = _config.MaxShipsSoundFile;\r
-            _soundSetting["装備数超過"] = _config.MaxEquipsSoundFile;\r
-            _soundSetting["大破警告"] = _config.DamagedShipSoundFile;\r
-            _soundSetting["泊地修理20分経過"] = _config.Akashi20MinSoundFile;\r
-            _soundSetting["泊地修理進行"] = _config.AkashiProgressSoundFile;\r
-            _soundSetting["泊地修理完了"] = _config.AkashiCompleteSoundFile;\r
-            _soundSetting["疲労回復"] = _config.ConditionSoundFile;\r
-\r
+            numericUpDownSoundVolume.Value = _config.Sounds.Volume;\r
+            foreach (var name in _config.Sounds.SoundNames)\r
+                _soundSetting[name] = _config.Sounds[name];\r
             listBoxSoundFile.SelectedIndex = -1;\r
             listBoxSoundFile.SelectedIndex = 0;\r
 \r
@@ -173,18 +160,9 @@ namespace KancolleSniffer
             _main.UpdateFighterPower();\r
             _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
 \r
-            _config.SoundVolume = (int)numericUpDownSoundVolume.Value;\r
-\r
-            _config.MissionSoundFile = _soundSetting["遠征終了"];\r
-            _config.NDockSoundFile = _soundSetting["入渠終了"];\r
-            _config.KDockSoundFile = _soundSetting["建造完了"];\r
-            _config.MaxShipsSoundFile = _soundSetting["艦娘数超過"];\r
-            _config.MaxEquipsSoundFile = _soundSetting["装備数超過"];\r
-            _config.DamagedShipSoundFile = _soundSetting["大破警告"];\r
-            _config.Akashi20MinSoundFile = _soundSetting["泊地修理20分経過"];\r
-            _config.AkashiProgressSoundFile = _soundSetting["泊地修理進行"];\r
-            _config.AkashiCompleteSoundFile = _soundSetting["泊地修理完了"];\r
-            _config.ConditionSoundFile = _soundSetting["疲労回復"];\r
+            _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
+            foreach (var name in _config.Sounds.SoundNames)\r
+                _config.Sounds[name] = _soundSetting[name];\r
         }\r
 \r
         private bool ValidatePorts(out int listen, out int outbound, out int server)\r
index 184d437..aa087bb 100644 (file)
@@ -83,6 +83,7 @@ namespace KancolleSniffer
             labelPresetAkashiTimer.BackColor = ShipLabels.ColumnColors[1];\r
             _shipListForm = new ShipListForm(_sniffer, _config) {Owner = this};\r
             _noticeQueue = new NoticeQueue(Ring);\r
+            _config.Load();\r
         }\r
 \r
         private void HttpProxy_AfterSessionComplete(HttpProxy.Session session)\r
@@ -198,7 +199,7 @@ namespace KancolleSniffer
 \r
         private void MainForm_Load(object sender, EventArgs e)\r
         {\r
-            _config.Load();\r
+\r
             RestoreLocation();\r
             if (_config.HideOnMinimized && WindowState == FormWindowState.Minimized)\r
                 ShowInTaskbar = false;\r
@@ -499,7 +500,7 @@ namespace KancolleSniffer
             if (item.RingShips)\r
             {\r
                 var message = $"残り{_sniffer.Item.MaxShips - _sniffer.Item.NowShips:D}隻";\r
-                _noticeQueue.Enqueue("艦娘が多すぎます", message, _config.MaxShipsSoundFile);\r
+                _noticeQueue.Enqueue("艦娘が多すぎます", message, _config.Sounds["艦娘数超過"]);\r
                 item.RingShips = false;\r
             }\r
         }\r
@@ -512,7 +513,7 @@ namespace KancolleSniffer
             if (item.RingEquips)\r
             {\r
                 var message = $"残り{_sniffer.Item.MaxEquips - _sniffer.Item.NowEquips:D}個";\r
-                _noticeQueue.Enqueue("装備が多すぎます", message, _config.MaxEquipsSoundFile);\r
+                _noticeQueue.Enqueue("装備が多すぎます", message, _config.Sounds["装備数超過"]);\r
                 item.RingEquips = false;\r
             }\r
         }\r
@@ -579,7 +580,7 @@ namespace KancolleSniffer
         {\r
             if (_sniffer.BadlyDamagedShips.Any())\r
                 _noticeQueue.Enqueue("大破した艦娘がいます", string.Join(" ", _sniffer.BadlyDamagedShips),\r
-                    _config.DamagedShipSoundFile);\r
+                    _config.Sounds["大破警告"]);\r
         }\r
 \r
         private void NotifyAkashiTimer()\r
@@ -592,7 +593,7 @@ namespace KancolleSniffer
                 return;\r
             if (msgs[0].Proceeded == "20分経過しました。")\r
             {\r
-                _noticeQueue.Enqueue("泊地修理", msgs[0].Proceeded, _config.Akashi20MinSoundFile);\r
+                _noticeQueue.Enqueue("泊地修理", msgs[0].Proceeded, _config.Sounds["泊地修理20分経過"]);\r
                 msgs[0].Proceeded = "";\r
                 // 修理完了がいるかもしれないので続ける\r
             }\r
@@ -600,9 +601,9 @@ namespace KancolleSniffer
             for (var i = 0; i < fn.Length; i++)\r
             {\r
                 if (msgs[i].Proceeded != "")\r
-                    _noticeQueue.Enqueue("泊地修理 " + fn[i], "修理進行:" + msgs[i].Proceeded, _config.AkashiProgressSoundFile);\r
+                    _noticeQueue.Enqueue("泊地修理 " + fn[i], "修理進行:" + msgs[i].Proceeded, _config.Sounds["泊地修理進行"]);\r
                 if (msgs[i].Completed != "")\r
-                    _noticeQueue.Enqueue("泊地修理 " + fn[i], "修理完了:" + msgs[i].Completed, _config.AkashiCompleteSoundFile);\r
+                    _noticeQueue.Enqueue("泊地修理 " + fn[i], "修理完了:" + msgs[i].Completed, _config.Sounds["泊地修理完了"]);\r
             }\r
         }\r
 \r
@@ -700,7 +701,7 @@ namespace KancolleSniffer
                             : rest.ToString(@"hh\:mm\:ss");\r
                 if (!entry.Timer.NeedRing)\r
                     continue;\r
-                _noticeQueue.Enqueue("遠征が終わりました", entry.Name, _config.MissionSoundFile);\r
+                _noticeQueue.Enqueue("遠征が終わりました", entry.Name, _config.Sounds["遠征終了"]);\r
                 entry.Timer.NeedRing = false;\r
             }\r
             for (var i = 0; i < _sniffer.NDock.Length; i++)\r
@@ -710,7 +711,7 @@ namespace KancolleSniffer
                 _shipLabels.SetNDockTimer(i, entry.Timer, _ndockFinishTimeMode);\r
                 if (!entry.Timer.NeedRing)\r
                     continue;\r
-                _noticeQueue.Enqueue("入渠が終わりました", entry.Name, _config.NDockSoundFile);\r
+                _noticeQueue.Enqueue("入渠が終わりました", entry.Name, _config.Sounds["入渠終了"]);\r
                 entry.Timer.NeedRing = false;\r
             }\r
             var kdock = new[] {labelConstruct1, labelConstruct2, labelConstruct3, labelConstruct4};\r
@@ -723,7 +724,7 @@ namespace KancolleSniffer
                 kdock[i].Text = timer.EndTime == DateTime.MinValue ? "" : timer.Rest.ToString(@"hh\:mm\:ss");\r
                 if (!timer.NeedRing)\r
                     continue;\r
-                _noticeQueue.Enqueue("建造が終わりました", $"第{i + 1:D}ドック", _config.KDockSoundFile);\r
+                _noticeQueue.Enqueue("建造が終わりました", $"第{i + 1:D}ドック", _config.Sounds["建造完了"]);\r
                 timer.NeedRing = false;\r
             }\r
             UpdateCondTimers();\r
@@ -774,7 +775,7 @@ namespace KancolleSniffer
             {\r
                 if (!_config.NotifyConditions.Contains(notice[i]))\r
                     return;\r
-                _noticeQueue.Enqueue("疲労が回復しました", fn[i] + " cond" + notice[i].ToString("D"), _config.ConditionSoundFile);\r
+                _noticeQueue.Enqueue("疲労が回復しました", fn[i] + " cond" + notice[i].ToString("D"), _config.Sounds["疲労回復"]);\r
             }\r
         }\r
 \r
@@ -890,7 +891,7 @@ namespace KancolleSniffer
             if (_config.ShowBaloonTip)\r
                 notifyIconMain.ShowBalloonTip(20000, baloonTitle, baloonMessage, ToolTipIcon.Info);\r
             if (_config.PlaySound)\r
-                PlaySound(soundFile, _config.SoundVolume);\r
+                PlaySound(soundFile, _config.Sounds.Volume);\r
         }\r
 \r
         [DllImport("winmm.dll")]\r
index c0a513c..a8e60af 100644 (file)
@@ -420,7 +420,9 @@ namespace KancolleSniffer
             checkBoxShipType.Checked = config.ShipType;\r
             ActiveControl = panelShipList;\r
             for (var i = 0; i < GroupCount; i++)\r
-                _groupSettings[i] = new HashSet<int>(config.ShipGroup[i]);\r
+                _groupSettings[i] = config.ShipGroup.Count == 0\r
+                    ? new HashSet<int>()\r
+                    : new HashSet<int>(config.ShipGroup[i]);\r
             comboBoxGroup.SelectedIndex = 0;\r
             if (config.Location.X == int.MinValue)\r
                 return;\r
@@ -432,19 +434,18 @@ namespace KancolleSniffer
 \r
         private void ShipListForm_FormClosing(object sender, FormClosingEventArgs e)\r
         {\r
+            e.Cancel = true;\r
+            if (!Visible)\r
+                return;\r
             var config = _config.ShipList;\r
             var all = _sniffer.ShipList.Select(s => s.Id).ToArray();\r
+            config.ShipGroup.Clear();\r
             for (var i = 0; i < GroupCount; i++)\r
             {\r
-                if (_groupSettings[i] == null)\r
-                    break;\r
                 if (all.Length > 0)\r
                     _groupSettings[i].IntersectWith(all);\r
-                config.ShipGroup[i] = _groupSettings[i].ToList();\r
+                config.ShipGroup.Add(_groupSettings[i].ToList());\r
             }\r
-            e.Cancel = true;\r
-            if (!Visible)\r
-                return;\r
             var bounds = WindowState == FormWindowState.Normal ? Bounds : RestoreBounds;\r
             config.Location = bounds.Location;\r
             config.Size = bounds.Size;\r