From 7884853098a0c5642433672f5b368affd18072ee Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:36:26 +0800 Subject: [PATCH] Improved backup and restore Overwrite the configuration file directly in the main program --- v2rayN/ServiceLib/Common/SqliteHelper.cs | 14 ++++++++++++++ .../ViewModels/BackupAndRestoreViewModel.cs | 16 +++++++++++++++- .../ServiceLib/ViewModels/MainWindowViewModel.cs | 9 +++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/v2rayN/ServiceLib/Common/SqliteHelper.cs b/v2rayN/ServiceLib/Common/SqliteHelper.cs index 3fc84e6a..3a8946bf 100644 --- a/v2rayN/ServiceLib/Common/SqliteHelper.cs +++ b/v2rayN/ServiceLib/Common/SqliteHelper.cs @@ -73,5 +73,19 @@ namespace ServiceLib.Common { return _dbAsync.Table(); } + + public async Task DisposeDbConnectionAsync() + { + await Task.Factory.StartNew(() => + { + _db?.Close(); + _db?.Dispose(); + _db = null; + + _dbAsync?.GetConnection()?.Close(); + _dbAsync?.GetConnection()?.Dispose(); + _dbAsync = null; + }); + } } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs b/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs index 804d46eb..77230770 100644 --- a/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs @@ -141,7 +141,21 @@ namespace ServiceLib.ViewModels var result = await CreateZipFileFromDirectory(fileBackup); if (result) { - Locator.Current.GetService()?.UpgradeApp(fileName); + var service = Locator.Current.GetService(); + await service?.MyAppExitAsync(true); + await SQLiteHelper.Instance.DisposeDbConnectionAsync(); + + var toPath = Utils.GetConfigPath(); + FileManager.ZipExtractToFile(fileName, toPath, ""); + + if (Utils.IsWindows()) + { + service?.RebootAsAdmin(false); + } + else + { + service?.Shutdown(); + } } else { diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 4645200d..cd1c2d13 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -343,6 +343,11 @@ namespace ServiceLib.ViewModels _updateView?.Invoke(EViewAction.ShowHideWindow, blShow); } + public void Shutdown() + { + _updateView?.Invoke(EViewAction.Shutdown, null); + } + #endregion Actions #region Servers && Groups @@ -509,7 +514,7 @@ namespace ServiceLib.ViewModels } } - public async Task RebootAsAdmin() + public async Task RebootAsAdmin(bool blAdmin = true) { try { @@ -519,7 +524,7 @@ namespace ServiceLib.ViewModels Arguments = Global.RebootAs, WorkingDirectory = Utils.StartupPath(), FileName = Utils.GetExePath().AppendQuotes(), - Verb = "runas", + Verb = blAdmin ? "runas" : null, }; Process.Start(startInfo); await MyAppExitAsync(false);