From 85174515e8e7b93c2559f8d8ab5906a3f64a0f27 Mon Sep 17 00:00:00 2001 From: yanue Date: Wed, 6 Mar 2019 22:43:27 +0800 Subject: [PATCH] save user-rules into UserDefaults --- .../xcdebugger/Breakpoints_v2.xcbkptlist | 4 ++-- V2rayU/PacUtils.swift | 7 ++++++- V2rayU/UserRulesWindow.swift | 13 +++++++++++-- V2rayU/Util.swift | 2 ++ V2rayUTool/main.swift | 2 +- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/V2rayU.xcworkspace/xcuserdata/yanue.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/V2rayU.xcworkspace/xcuserdata/yanue.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index d94ebbb9..fd09ad87 100644 --- a/V2rayU.xcworkspace/xcuserdata/yanue.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/V2rayU.xcworkspace/xcuserdata/yanue.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -10,7 +10,7 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "V2rayU/ConfigWindow.swift" - timestampString = "573574739.490474" + timestampString = "573575018.389261" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "260" @@ -26,7 +26,7 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "V2rayU/ConfigWindow.swift" - timestampString = "573574739.491349" + timestampString = "573575018.390221" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "264" diff --git a/V2rayU/PacUtils.swift b/V2rayU/PacUtils.swift index 14be00f6..e0c57d15 100644 --- a/V2rayU/PacUtils.swift +++ b/V2rayU/PacUtils.swift @@ -39,6 +39,12 @@ func GeneratePACFile() -> Bool { let str = String(data: data, encoding: String.Encoding.utf8) var lines = str!.components(separatedBy: CharacterSet.newlines) + // read userRules from UserDefaults + let userRules = UserDefaults.get(forKey: .userRules) + if userRules != nil { + try userRules!.data(using: String.Encoding.utf8)?.write(to: URL(fileURLWithPath: PACUserRuleFilePath), options: .atomic) + } + do { let userRuleStr = try String(contentsOfFile: PACUserRuleFilePath, encoding: String.Encoding.utf8) let userRuleLines = userRuleStr.components(separatedBy: CharacterSet.newlines) @@ -47,7 +53,6 @@ func GeneratePACFile() -> Bool { } catch { NSLog("Not found user-rule.txt") } - // Filter empty and comment lines lines = lines.filter({ (s: String) -> Bool in if s.isEmpty { diff --git a/V2rayU/UserRulesWindow.swift b/V2rayU/UserRulesWindow.swift index 5c2be787..655a725c 100644 --- a/V2rayU/UserRulesWindow.swift +++ b/V2rayU/UserRulesWindow.swift @@ -18,8 +18,14 @@ class UserRulesWindowController: NSWindowController { override func windowDidLoad() { super.windowDidLoad() - let str = try? String(contentsOfFile: PACUserRuleFilePath, encoding: String.Encoding.utf8) - userRulesView.string = str! + // read userRules from UserDefaults + let txt = UserDefaults.get(forKey: .userRules) + if txt != nil { + userRulesView.string = txt! + } else { + let str = try? String(contentsOfFile: PACUserRuleFilePath, encoding: String.Encoding.utf8) + userRulesView.string = str! + } } @IBAction func didCancel(_ sender: AnyObject) { @@ -29,6 +35,9 @@ class UserRulesWindowController: NSWindowController { @IBAction func didOK(_ sender: AnyObject) { if let str = userRulesView?.string { do { + // save user rules into UserDefaults + UserDefaults.set(forKey: .userRules, value: str) + try str.data(using: String.Encoding.utf8)?.write(to: URL(fileURLWithPath: PACUserRuleFilePath), options: .atomic) if GeneratePACFile() { diff --git a/V2rayU/Util.swift b/V2rayU/Util.swift index 4cc86b0f..d0c4c993 100644 --- a/V2rayU/Util.swift +++ b/V2rayU/Util.swift @@ -27,6 +27,8 @@ extension UserDefaults { case autoLaunch // pac|manual|global case runMode + // use rules + case userRules } static func setBool(forKey key: KEY, value: Bool) { diff --git a/V2rayUTool/main.swift b/V2rayUTool/main.swift index 8f5168a3..ce1fc655 100644 --- a/V2rayUTool/main.swift +++ b/V2rayUTool/main.swift @@ -126,7 +126,7 @@ class V2rayUTool: NSObject { let commitRet = SCPreferencesCommitChanges(prefRef) let applyRet = SCPreferencesApplyChanges(prefRef) SCPreferencesSynchronize(prefRef) - // AuthorizationFree(authRef, kAuthorizationFlagDefaults) + // AuthorizationFree(authRef, kAuthorizationFlagDefaults) Swift.print("after SCPreferencesCommitChanges: commitRet = \(commitRet), applyRet = \(applyRet)") } }