From d004c6860e3d7fc9268d520cbd66a398777a37f2 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 17 Oct 2024 15:28:32 +0800 Subject: [PATCH] Add check update for sing-box --- v2rayN/ServiceLib/Common/FileManager.cs | 25 ++++++++-- v2rayN/ServiceLib/Services/UpdateService.cs | 2 +- .../ViewModels/CheckUpdateViewModel.cs | 48 ++++++++----------- 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/v2rayN/ServiceLib/Common/FileManager.cs b/v2rayN/ServiceLib/Common/FileManager.cs index 70b09314..b98b1655 100644 --- a/v2rayN/ServiceLib/Common/FileManager.cs +++ b/v2rayN/ServiceLib/Common/FileManager.cs @@ -1,4 +1,5 @@ -using System.IO.Compression; +using System.Formats.Tar; +using System.IO.Compression; using System.Text; namespace ServiceLib.Common @@ -19,7 +20,7 @@ namespace ServiceLib.Common return false; } - public static void UncompressedFile(string fileName, byte[] content) + public static void DecompressFile(string fileName, byte[] content) { try { @@ -33,7 +34,7 @@ namespace ServiceLib.Common } } - public static void UncompressedFile(string fileName, string toPath, string? toName) + public static void DecompressFile(string fileName, string toPath, string? toName) { try { @@ -49,6 +50,20 @@ namespace ServiceLib.Common } } + public static void DecompressTarFile(string fileName, string toPath) + { + try + { + using var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); + using var gz = new GZipStream(fs, CompressionMode.Decompress, leaveOpen: true); + TarFile.ExtractToDirectory(gz, toPath, overwriteFiles: true); + } + catch (Exception ex) + { + Logging.SaveLog(ex.Message, ex); + } + } + public static string NonExclusiveReadAllText(string path) { return NonExclusiveReadAllText(path, Encoding.Default); @@ -139,7 +154,7 @@ namespace ServiceLib.Common return true; } - public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive, string ignoredName) + public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive, string? ignoredName) { // Get information about the source directory var dir = new DirectoryInfo(sourceDir); @@ -166,7 +181,7 @@ namespace ServiceLib.Common continue; } var targetFilePath = Path.Combine(destinationDir, file.Name); - file.CopyTo(targetFilePath); + file.CopyTo(targetFilePath, true); } // If recursive and copying subdirectories, recursively call this method diff --git a/v2rayN/ServiceLib/Services/UpdateService.cs b/v2rayN/ServiceLib/Services/UpdateService.cs index f25548ce..09f103fa 100644 --- a/v2rayN/ServiceLib/Services/UpdateService.cs +++ b/v2rayN/ServiceLib/Services/UpdateService.cs @@ -107,7 +107,7 @@ namespace ServiceLib.Services _updateFunc?.Invoke(false, args.Msg); url = args.Url; - var ext = Path.GetExtension(url); + var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url); fileName = Utils.GetTempPath(Utils.GetGuid() + ext); await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); } diff --git a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs index aac02973..b314c79b 100644 --- a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs @@ -66,15 +66,12 @@ namespace ServiceLib.ViewModels CoreType = ECoreType.mihomo.ToString(), Remarks = ResUI.menuCheckUpdate, }); - if (Utils.IsWindows()) + _checkUpdateItem.Add(new CheckUpdateItem() { - _checkUpdateItem.Add(new CheckUpdateItem() - { - IsSelected = true, - CoreType = ECoreType.sing_box.ToString(), - Remarks = ResUI.menuCheckUpdate, - }); - } + IsSelected = true, + CoreType = ECoreType.sing_box.ToString(), + Remarks = ResUI.menuCheckUpdate, + }); _checkUpdateItem.Add(new CheckUpdateItem() { IsSelected = true, @@ -149,16 +146,6 @@ namespace ServiceLib.ViewModels private async Task CheckUpdateN(bool preRelease) { - ////Check for standalone windows .Net version - //if (Utils.IsWindows() - // && File.Exists(Path.Combine(Utils.StartupPath(), "wpfgfx_cor3.dll")) - // && File.Exists(Path.Combine(Utils.StartupPath(), "D3DCompiler_47_cor3.dll")) - // ) - //{ - // UpdateView(_v2rayN, ResUI.UpdateStandalonePackageTip); - // return; - //} - void _updateUI(bool success, string msg) { UpdateView(_v2rayN, msg); @@ -262,15 +249,24 @@ namespace ServiceLib.ViewModels { continue; } - string toPath = Utils.GetBinPath("", item.CoreType); + var toPath = Utils.GetBinPath("", item.CoreType); if (fileName.Contains(".tar.gz")) { - //It's too complicated to unzip. TODO + FileManager.DecompressTarFile(fileName, toPath); + var dir = new DirectoryInfo(toPath); + if (dir.Exists) + { + foreach (var subDir in dir.GetDirectories()) + { + FileManager.CopyDirectory(subDir.FullName, toPath, false, null); + subDir.Delete(true); + } + } } else if (fileName.Contains(".gz")) { - FileManager.UncompressedFile(fileName, toPath, item.CoreType); + FileManager.DecompressFile(fileName, toPath, item.CoreType); } else { @@ -299,12 +295,10 @@ namespace ServiceLib.ViewModels public void UpdateViewResult(CheckUpdateItem item) { var found = _checkUpdateItem.FirstOrDefault(t => t.CoreType == item.CoreType); - if (found != null) - { - var itemCopy = JsonUtils.DeepCopy(found); - itemCopy.Remarks = item.Remarks; - _checkUpdateItem.Replace(found, itemCopy); - } + if (found == null) return; + var itemCopy = JsonUtils.DeepCopy(found); + itemCopy.Remarks = item.Remarks; + _checkUpdateItem.Replace(found, itemCopy); } } } \ No newline at end of file