Skip to content

Commit

Permalink
Optimized: Backup Config file before override it.
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed Sep 1, 2018
1 parent 9dc4b9b commit cd31590
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
12 changes: 3 additions & 9 deletions ClashX/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 11 additions & 9 deletions ClashX/General/ConfigFileFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
1 change: 1 addition & 0 deletions ClashX/ViewControllers/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit cd31590

Please sign in to comment.