OSDN Git Service

Pushbulletを利用したスマホへのプッシュ通知を実装する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Config.cs
index ba8085e..469f9ea 100644 (file)
@@ -16,15 +16,14 @@ 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
     public class ProxyConfig\r
     {\r
         public const int DefaultListenPort = 8080;\r
-        public const string AutoConfigUrl = "https://kancollesniffer.osdn.jp/proxy.pac";\r
-        public const string AutoConfigUrlWithPort = "https://kancollesniffer.osdn.jp/proxy.php?port=";\r
         public bool Auto { get; set; }\r
         public int Listen { get; set; }\r
         public bool UseUpstream { get; set; }\r
@@ -44,14 +43,13 @@ 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 ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext;\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
@@ -60,16 +58,12 @@ namespace KancolleSniffer
         public bool On { get; set; }\r
         public string OutputDir { get; set; }\r
         public int MaterialLogInterval { get; set; }\r
-        public bool ServerOn { get; set; }\r
-        public int Listen { get; set; }\r
 \r
         public LogConfig()\r
         {\r
             On = true;\r
             OutputDir = "";\r
             MaterialLogInterval = 10;\r
-            ServerOn = true;\r
-            Listen = 8008;\r
         }\r
     }\r
 \r
@@ -79,99 +73,141 @@ namespace KancolleSniffer
         public string Token { get; set; } = "";\r
     }\r
 \r
+    public class PushbulletConfig\r
+    {\r
+        public bool On { get; set; }\r
+        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 string this[string name]\r
+        {\r
+            get { return Files[Config.NotificationIndex[name]]; }\r
+            set { Files[Config.NotificationIndex[name]] = value; }\r
+        }\r
+    }\r
+\r
+    [Flags]\r
+    public enum NotificationType\r
+    {\r
+        FlashWindow = 1,\r
+        ShowBaloonTip = 1 << 1,\r
+        PlaySound = 1 << 2,\r
+        Pushbullet = 1 << 3,\r
+        All = (1 << 3) - 1 // Pushbullet以外\r
+    }\r
+\r
+    public class NotificationConfig\r
+    {\r
+        public NotificationType[] Settings =\r
+            Config.NotificationNames.Select(x => NotificationType.All).ToArray();\r
+\r
+        public NotificationType this[string name]\r
+        {\r
+            get { return Settings[Config.NotificationIndex[name]]; }\r
+            set { Settings[Config.NotificationIndex[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 int Zoom { get; set; } = 100;\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 bool PlaySound { get; set; } = true;\r
+        public NotificationConfig Notifications { get; set; } = new NotificationConfig();\r
+        public int MarginShips { get; set; } = 4;\r
+        public int MarginEquips { get; set; } = 10;\r
         public List<int> NotifyConditions { get; set; }\r
         public List<int> ResetHours { get; set; }\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
+        public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();\r
+\r
+        public static readonly string[] NotificationNames =\r
+        {\r
+            "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
+            "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復"\r
+        };\r
+\r
+        public static readonly Dictionary<string, int> NotificationIndex =\r
+            NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);\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
+            _configFileName = Path.Combine(_baseDir, "config.xml");\r
+            ConvertPath(PrependBaseDir);\r
+        }\r
+\r
+        public void InitializeValues()\r
+        {\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
-            ConvertPath(PrependBaseDir);\r
         }\r
 \r
         public void Load()\r
         {\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
+                InitializeValues();\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 +219,59 @@ 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
+            dynamic json;\r
+            try\r
+            {\r
+                json = JsonParser.Parse(File.ReadAllText(old));\r
+            }\r
+            catch (FileNotFoundException)\r
+            {\r
+                return;\r
+            }\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
+        }\r
     }\r
 }
\ No newline at end of file