OSDN Git Service

設定を保存するときにパス名を相対パスにする
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Thu, 3 Sep 2015 13:33:17 +0000 (22:33 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Sun, 6 Sep 2015 17:04:02 +0000 (02:04 +0900)
KancolleSniffer/Config.cs

index 30e0bc3..9398ab5 100644 (file)
@@ -15,6 +15,7 @@
 // You should have received a copy of the GNU General Public License\r
 // along with this program; if not, see <http://www.gnu.org/licenses/>.\r
 \r
+using System;\r
 using System.Collections.Generic;\r
 using System.Drawing;\r
 using System.IO;\r
@@ -70,7 +71,7 @@ namespace KancolleSniffer
         public LogConfig()\r
         {\r
             On = true;\r
-            OutputDir = Path.GetDirectoryName(Application.ExecutablePath);\r
+            OutputDir = "";\r
             MaterialLogInterval = 10;\r
             ServerOn = true;\r
             Listen = 8008;\r
@@ -79,8 +80,8 @@ namespace KancolleSniffer
 \r
     public class Config\r
     {\r
-        private readonly string _configFileName =\r
-            Path.Combine(Path.GetDirectoryName(Application.ExecutablePath) ?? "", "config.json");\r
+        private readonly string _baseDir = Path.GetDirectoryName(Application.ExecutablePath);\r
+        private readonly string _configFileName;\r
 \r
         public Point Location { get; set; }\r
         public bool TopMost { get; set; }\r
@@ -113,6 +114,7 @@ namespace KancolleSniffer
 \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
@@ -122,21 +124,21 @@ namespace KancolleSniffer
             AlwaysShowResultRank = false;\r
             WithAlvBonus = true;\r
             SoundVolume = 100;\r
-            var dir = Path.GetDirectoryName(Application.ExecutablePath) ?? "";\r
-            MissionSoundFile = Path.Combine(dir, "ensei.mp3");\r
-            NDockSoundFile = Path.Combine(dir, "nyuukyo.mp3");\r
-            KDockSoundFile = Path.Combine(dir, "kenzou.mp3");\r
-            MaxShipsSoundFile = Path.Combine(dir, "kanmusu.mp3");\r
-            MaxEquipsSoundFile = Path.Combine(dir, "soubi.mp3");\r
-            DamagedShipSoundFile = Path.Combine(dir, "taiha.mp3");\r
-            Akashi20MinSoundFile = Path.Combine(dir, "20min.mp3");\r
-            AkashiProgressSoundFile = Path.Combine(dir, "syuuri.mp3");\r
-            AkashiCompleteSoundFile = Path.Combine(dir, "syuuri2.mp3");\r
-            ConditionSoundFile = Path.Combine(dir, "hirou.mp3");\r
-            DebugLogFile = Path.Combine(dir, "log.txt");\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
+            ConvertPath(PrependBaseDir);\r
         }\r
 \r
         public void Load()\r
@@ -146,6 +148,7 @@ namespace KancolleSniffer
                 var config = (Config)DynamicJson.Parse(File.ReadAllText(_configFileName));\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
@@ -154,7 +157,38 @@ namespace KancolleSniffer
 \r
         public void Save()\r
         {\r
+            ConvertPath(StripBaseDir);\r
             File.WriteAllText(_configFileName, DynamicJson.Serialize(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
+            Log.OutputDir = func(Log.OutputDir);\r
+        }\r
+\r
+        private string StripBaseDir(string path)\r
+        {\r
+            if (_baseDir == null)\r
+                return path;\r
+            if (!path.StartsWith(_baseDir))\r
+                return path;\r
+            path = path.Substring(_baseDir.Length);\r
+            return path.StartsWith(Path.DirectorySeparatorChar.ToString()) ? path.Substring(1) : path;\r
+        }\r
+\r
+        private string PrependBaseDir(string path)\r
+        {\r
+            if (_baseDir == null)\r
+                return path;\r
+            if (Path.IsPathRooted(path))\r
+                return path;\r
+            return Path.Combine(_baseDir, path);\r
+        }\r
     }\r
 }
\ No newline at end of file