OSDN Git Service

一覧の分類で種別を表示してスクロールすると種別が残るのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Config.cs
index 6636163..fcd7e60 100644 (file)
@@ -38,13 +38,29 @@ namespace KancolleSniffer
         }\r
     }\r
 \r
+    [Flags]\r
+    public enum ShipCategory\r
+    {\r
+        None = 0,\r
+        BattleShip = 1 << 0,\r
+        AircraftCarrier = 1 << 1,\r
+        HeavyCruiser = 1 << 2,\r
+        LightCruiser = 1 << 3,\r
+        Destroyer = 1 << 4,\r
+        Escort = 1 << 5,\r
+        Submarine = 1 << 6,\r
+        Assistant = 1 << 7,\r
+        All = (1 << 8) - 1\r
+    }\r
+\r
     public class ShipListConfig\r
     {\r
         public bool Visible { get; set; }\r
         public Point Location { get; set; }\r
         public Size Size { get; set; }\r
         public string Mode { get; set; }\r
-        public bool ShipType { get; set; }\r
+        public ShipCategory ShipCategories { get; set; } = ShipCategory.All;\r
+        public bool ShipType;\r
         public bool ShowHpInPercent { get; set; }\r
         public ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext;\r
         public List<List<int>> ShipGroup { get; set; }\r
@@ -112,6 +128,7 @@ namespace KancolleSniffer
     public enum NotificationType\r
     {\r
         FlashWindow = 1,\r
+        // ReSharper disable once IdentifierTypo\r
         ShowBaloonTip = 1 << 1,\r
         PlaySound = 1 << 2,\r
         All = (1 << 3) - 1,\r
@@ -158,6 +175,17 @@ namespace KancolleSniffer
                 PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod;\r
             }\r
         }\r
+\r
+        public void Normalization()\r
+        {\r
+            Settings = Settings.Select(s =>\r
+                    (s & NotificationType.Pushbullet) != 0\r
+                        ? s ^ NotificationType.Pushbullet | NotificationType.Push\r
+                        : s)\r
+                .ToArray();\r
+            RepeatIntervals = RepeatIntervals.Select(v => v < 0 ? 0 : v).ToArray();\r
+            PreliminaryPeriods = PreliminaryPeriods.Select(v => v < 0 ? 0 : v).ToArray();\r
+        }\r
     }\r
 \r
     public class LocationPerMachine\r
@@ -175,7 +203,8 @@ namespace KancolleSniffer
         ResultRank = 1,\r
         AirBattleResult = 1 << 1,\r
         BattleResult = 1 << 2,\r
-        All = (1 << 3) - 1\r
+        NextCell = 1 << 3,\r
+        All = (1 << 4) - 1\r
     }\r
 \r
     [Flags]\r
@@ -187,9 +216,6 @@ namespace KancolleSniffer
 \r
     public class Config\r
     {\r
-        private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar);\r
-        private readonly string _configFileName;\r
-\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
@@ -200,12 +226,13 @@ namespace KancolleSniffer
         public bool ShowHpInPercent { get; set; }\r
         public TimerKind ShowEndTime { get; set; }\r
         public bool FlashWindow { get; set; } = true;\r
+        // ReSharper disable once IdentifierTypo\r
         public bool ShowBaloonTip { get; set; } = true;\r
         public bool PlaySound { get; set; } = true;\r
         public NotificationType NotificationFlags { get; set; } = NotificationType.All;\r
         public NotificationConfig Notifications { get; set; } = new NotificationConfig();\r
-        public int MarginShips { get; set; } = 4;\r
-        public int MarginEquips { get; set; } = 10;\r
+        public int MarginShips { get; set; } = 5;\r
+        public int MarginEquips { get; set; } = 5;\r
         public List<int> NotifyConditions { get; set; }\r
         public List<int> ResetHours { get; set; }\r
         public bool AlwaysShowResultRank { get; set; }\r
@@ -229,9 +256,13 @@ namespace KancolleSniffer
         public static readonly Dictionary<string, int> NotificationIndex =\r
             NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i);\r
 \r
+\r
+        private const string FileName = "config.xml";\r
+        public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;\r
+        private static readonly string ConfigFile = Path.Combine(BaseDir, FileName);\r
+\r
         public Config()\r
         {\r
-            _configFileName = Path.Combine(_baseDir, "config.xml");\r
             ConvertPath(PrependBaseDir);\r
         }\r
 \r
@@ -247,16 +278,11 @@ namespace KancolleSniffer
             {\r
                 var serializer = new XmlSerializer(typeof(Config));\r
                 Config config;\r
-                using (var file = File.OpenText(_configFileName))\r
+                using (var file = File.OpenText(ConfigFile))\r
                     config = (Config)serializer.Deserialize(file);\r
                 foreach (var property in GetType().GetProperties())\r
                     property.SetValue(this, property.GetValue(config, null), null);\r
-                var ns = Notifications.Settings;\r
-                for (var i = 0; i < ns.Length; i++)\r
-                {\r
-                    if ((ns[i] & NotificationType.Pushbullet) != 0)\r
-                        ns[i] = ns[i] ^ NotificationType.Pushbullet | NotificationType.Push;\r
-                }\r
+                Notifications.Normalization();\r
                 ComposeNotificationFlags();\r
                 if (AlwaysShowResultRank)\r
                 {\r
@@ -281,6 +307,10 @@ namespace KancolleSniffer
                 InitializeValues();\r
                 Save();\r
             }\r
+            catch (InvalidOperationException ex)\r
+            {\r
+                throw new Exception(FileName + "が壊れています。", ex);\r
+            }\r
             ConvertPath(PrependBaseDir);\r
         }\r
 \r
@@ -313,8 +343,11 @@ namespace KancolleSniffer
             DecomposeNotificationFlags();\r
             ConvertPath(StripBaseDir);\r
             var serializer = new XmlSerializer(typeof(Config));\r
-            using (var file = File.CreateText(_configFileName))\r
+            using (var file = File.CreateText(ConfigFile + ".tmp"))\r
                 serializer.Serialize(file, this);\r
+            File.Copy(ConfigFile + ".tmp", ConfigFile, true);\r
+            File.Delete(ConfigFile + ".tmp");\r
+            ConvertPath(PrependBaseDir);\r
         }\r
 \r
         private void DecomposeNotificationFlags()\r
@@ -334,12 +367,12 @@ namespace KancolleSniffer
 \r
         private string StripBaseDir(string path)\r
         {\r
-            if (!path.StartsWith(_baseDir))\r
+            if (!path.StartsWith(BaseDir))\r
                 return path;\r
-            path = path.Substring(_baseDir.Length);\r
+            path = path.Substring(BaseDir.Length);\r
             return path.TrimStart(Path.DirectorySeparatorChar);\r
         }\r
 \r
-        private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(_baseDir, path);\r
+        private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(BaseDir, path);\r
     }\r
 }
\ No newline at end of file