From a4cd1fb69e223d202ca10f04bb74ee756213d6fa Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Fri, 28 Jul 2023 02:41:27 +0800 Subject: [PATCH] Repo // Dump wrecked UOM data on crash when debug mode is ON. --- Source/Modules/AppDelegate.swift | 26 +++++++++++++++----------- Source/Modules/LMMgr_Core.swift | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Source/Modules/AppDelegate.swift b/Source/Modules/AppDelegate.swift index 43752052..d1cbc1ae 100644 --- a/Source/Modules/AppDelegate.swift +++ b/Source/Modules/AppDelegate.swift @@ -66,21 +66,25 @@ public extension AppDelegate { } } - // 一旦發現與使用者半衰模組的觀察行為有關的崩潰標記被開啟,就清空既有的半衰記憶資料檔案。 + // 一旦發現與使用者半衰模組的觀察行為有關的崩潰標記被開啟: + // 如果有開啟 Debug 模式的話,就將既有的半衰記憶資料檔案更名+打上當時的時間戳。 + // 如果沒有開啟 Debug 模式的話,則將半衰記憶資料直接清空。 if PrefMgr.shared.failureFlagForUOMObservation { - DispatchQueue.main.async { + if PrefMgr.shared.isDebugModeEnabled { + LMMgr.relocateWreckedUOMData() + } else { LMMgr.clearUserOverrideModelData(.imeModeCHS) LMMgr.clearUserOverrideModelData(.imeModeCHT) - PrefMgr.shared.failureFlagForUOMObservation = false - let userNotification = NSUserNotification() - userNotification.title = NSLocalizedString("vChewing", comment: "") - userNotification.informativeText = NSLocalizedString( - "vChewing crashed while handling previously loaded UOM observation data. These data files are cleaned now to ensure the usability.", - comment: "" - ) - userNotification.soundName = NSUserNotificationDefaultSoundName - NSUserNotificationCenter.default.deliver(userNotification) } + PrefMgr.shared.failureFlagForUOMObservation = false + let userNotification = NSUserNotification() + userNotification.title = NSLocalizedString("vChewing", comment: "") + userNotification.informativeText = NSLocalizedString( + "vChewing crashed while handling previously loaded UOM observation data. These data files are cleaned now to ensure the usability.", + comment: "" + ) + userNotification.soundName = NSUserNotificationDefaultSoundName + NSUserNotificationCenter.default.deliver(userNotification) } if !PrefMgr.shared.onlyLoadFactoryLangModelsIfNeeded { LMMgr.loadDataModelsOnAppDelegate() } diff --git a/Source/Modules/LMMgr_Core.swift b/Source/Modules/LMMgr_Core.swift index 04c9cc93..7c8fc07e 100644 --- a/Source/Modules/LMMgr_Core.swift +++ b/Source/Modules/LMMgr_Core.swift @@ -382,6 +382,24 @@ public class LMMgr { } } + public static func relocateWreckedUOMData() { + func dateStringTag(date givenDate: Date) -> String { + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyyMMdd-HHmm" + dateFormatter.timeZone = .current + let strDate = dateFormatter.string(from: givenDate) + return strDate + } + + let urls: [URL] = [userOverrideModelDataURL(.imeModeCHS), userOverrideModelDataURL(.imeModeCHT)] + let folderURL = URL(fileURLWithPath: dataFolderPath(isDefaultFolder: true)).deletingLastPathComponent() + urls.forEach { oldURL in + let newFileName = "[UOM-CRASH][\(dateStringTag(date: .init()))]\(oldURL.lastPathComponent)" + let newURL = folderURL.appendingPathComponent(newFileName) + try? FileManager.default.moveItem(at: oldURL, to: newURL) + } + } + public static func clearUserOverrideModelData(_ mode: Shared.InputMode = .imeModeNULL) { switch mode { case .imeModeCHS: