From cd315905e544e37fe2f4e26c15d51fea7456e441 Mon Sep 17 00:00:00 2001 From: yichengchen Date: Sat, 1 Sep 2018 14:19:53 +0800 Subject: [PATCH] Optimized: Backup Config file before override it. --- ClashX/AppDelegate.swift | 12 +++-------- ClashX/General/ConfigFileFactory.swift | 20 ++++++++++--------- .../PreferencesWindowController.swift | 1 + 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/ClashX/AppDelegate.swift b/ClashX/AppDelegate.swift index 3ba16e7b9..802d68702 100644 --- a/ClashX/AppDelegate.swift +++ b/ClashX/AppDelegate.swift @@ -135,17 +135,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { if let xx = x.object(forKey: "launch_fail_times") as? Int {launch_fail_times = xx } launch_fail_times += 1 x.set(launch_fail_times, forKey: "launch_fail_times") - if launch_fail_times > 2{ + if launch_fail_times > 2 { //发生连续崩溃 - let path = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/") - let documentDirectory = URL(fileURLWithPath: path) - let originPath = documentDirectory.appendingPathComponent("config.ini") - let destinationPath = documentDirectory.appendingPathComponent("config.ini.bak") - try? FileManager.default.removeItem(at:destinationPath) - try? FileManager.default.moveItem(at: originPath, to: destinationPath) - try? FileManager.default.removeItem(at: documentDirectory.appendingPathComponent("Country.mmdb")) + ConfigFileFactory.backupAndRemoveConfigFile() + try? FileManager.default.removeItem(atPath: kConfigFolderPath + "Country.mmdb") NSUserNotificationCenter.default.post(title: "Fail on launch protect", info: "You origin Config has been rename to config.ini.bak") - } DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double(Int64(1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: { x.set(0, forKey: "launch_fail_times") diff --git a/ClashX/General/ConfigFileFactory.swift b/ClashX/General/ConfigFileFactory.swift index cc5f5e576..f363333cc 100644 --- a/ClashX/General/ConfigFileFactory.swift +++ b/ClashX/General/ConfigFileFactory.swift @@ -65,21 +65,23 @@ class ConfigFileFactory { static func saveToClashConfigFile(str:String) { // save to ~/.config/clash/config.ini - let path = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/config.ini") - + self.backupAndRemoveConfigFile() + try? str.write(to: URL(fileURLWithPath: kConfigFilePath), atomically: true, encoding: .utf8) + } + + static func backupAndRemoveConfigFile() { + let path = kConfigFilePath + if (FileManager.default.fileExists(atPath: path)) { - try? FileManager.default.removeItem(at: URL(fileURLWithPath: path)) + let newPath = "\(kConfigFolderPath)config_\(Date().timeIntervalSince1970).ini" + try? FileManager.default.moveItem(atPath: path, toPath: newPath) } - try? str.write(to: URL(fileURLWithPath: path), atomically: true, encoding: .utf8) } static func copySimpleConfigFile() { let path = Bundle.main.path(forResource: "initConfig", ofType: "ini")! - let target = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/config.ini") - if (FileManager.default.fileExists(atPath: target)) { - try? FileManager.default.removeItem(at: URL(fileURLWithPath: target)) - } - try? FileManager.default.copyItem(atPath: path, toPath: target) + backupAndRemoveConfigFile() + try? FileManager.default.copyItem(atPath: path, toPath: kConfigFolderPath) NSUserNotificationCenter.default.postGenerateSimpleConfigNotice() } diff --git a/ClashX/ViewControllers/PreferencesWindowController.swift b/ClashX/ViewControllers/PreferencesWindowController.swift index cc4968477..9e544d26f 100755 --- a/ClashX/ViewControllers/PreferencesWindowController.swift +++ b/ClashX/ViewControllers/PreferencesWindowController.swift @@ -138,6 +138,7 @@ class PreferencesWindowController: NSWindowController let str = ConfigFileFactory.configFile(proxies: serverConfigs) ConfigFileFactory.saveToClashConfigFile(str: str) + NotificationCenter.default.post(Notification(name:kShouldUpDateConfig)) window?.performClose(nil) }