From b2669103dc9c6a1a7479c56da8df87e3ce41415f Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:03:15 +0800 Subject: [PATCH] Refactor Upgrade --- v2rayN/v2rayUpgrade/Program.cs | 153 ++------------------------------- v2rayN/v2rayUpgrade/Upgrade.cs | 141 ++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 147 deletions(-) create mode 100644 v2rayN/v2rayUpgrade/Upgrade.cs diff --git a/v2rayN/v2rayUpgrade/Program.cs b/v2rayN/v2rayUpgrade/Program.cs index 0d057a96..8b9dfe4c 100644 --- a/v2rayN/v2rayUpgrade/Program.cs +++ b/v2rayN/v2rayUpgrade/Program.cs @@ -1,163 +1,22 @@ -using System.Diagnostics; -using System.IO.Compression; -using System.Runtime.InteropServices; -using System.Text; - -namespace v2rayUpgrade +namespace v2rayUpgrade { internal static class Program { - private static readonly string defaultFilename = "v2rayN.zip_temp"; - private static string? fileName; - /// /// 应用程序的主入口点。 /// [STAThread] private static void Main(string[] args) { - if (args.Length > 0) - { - fileName = Uri.UnescapeDataString(string.Join(" ", args)); - } - else - { - fileName = defaultFilename; - } - Console.WriteLine(fileName); - Console.WriteLine("In progress, please wait...(正在进行中,请等待)"); - - Thread.Sleep(5000); - - try - { - Process[] existing = Process.GetProcessesByName(V2rayN()); - foreach (Process p in existing) - { - var path = p.MainModule?.FileName ?? ""; - if (path.StartsWith(GetPath(V2rayN()))) - { - p.Kill(); - p.WaitForExit(100); - } - } - } - catch (Exception ex) - { - // Access may be denied without admin right. The user may not be an administrator. - Console.WriteLine("Failed to close v2rayN(关闭v2rayN失败).\n" + - "Close it manually, or the upgrade may fail.(请手动关闭正在运行的v2rayN,否则可能升级失败。\n\n" + ex.StackTrace); - } - - if (!File.Exists(fileName)) - { - if (File.Exists(defaultFilename)) - { - fileName = defaultFilename; - } - else - { - Console.WriteLine("Upgrade Failed, File Not Exist(升级失败,文件不存在)."); - return; - } - } - - StringBuilder sb = new(); - try - { - string thisAppOldFile = $"{GetExePath()}.tmp"; - File.Delete(thisAppOldFile); - string splitKey = "/"; - - using ZipArchive archive = ZipFile.OpenRead(fileName); - foreach (ZipArchiveEntry entry in archive.Entries) - { - try - { - if (entry.Length == 0) - { - continue; - } - - var lst = entry.FullName.Split(splitKey); - if (lst.Length == 1) continue; - string fullName = string.Join(splitKey, lst[1..lst.Length]); - - if (string.Equals(GetExePath(), GetPath(fullName), StringComparison.OrdinalIgnoreCase)) - { - File.Move(GetExePath(), thisAppOldFile); - } - - string entryOutputPath = GetPath(fullName); - Directory.CreateDirectory(Path.GetDirectoryName(entryOutputPath)!); - entry.ExtractToFile(entryOutputPath, true); - - Console.WriteLine(entryOutputPath); - } - catch (Exception ex) - { - sb.Append(ex.StackTrace); - } - } - } - catch (Exception ex) + if (args.Length == 0) { - Console.WriteLine("Upgrade Failed(升级失败)." + ex.StackTrace); + Console.WriteLine("Please run it from the main application.(请从主应用运行)"); + Thread.Sleep(5000); return; } - if (sb.Length > 0) - { - Console.WriteLine("Upgrade Failed.\n" + - "(升级失败)." + sb.ToString()); - return; - } - - Console.WriteLine("Start v2rayN, please wait...(正在重启,请等待)"); - Thread.Sleep(3000); - Process process = new() - { - StartInfo = new() - { - FileName = V2rayN(), - WorkingDirectory = StartupPath() - } - }; - process.Start(); - } - - public static string GetExePath() - { - return Environment.ProcessPath ?? string.Empty; - } - - public static string StartupPath() - { - return AppDomain.CurrentDomain.BaseDirectory; - } - - public static string GetPath(string fileName) - { - string startupPath = StartupPath(); - if (string.IsNullOrEmpty(fileName)) - { - return startupPath; - } - return Path.Combine(startupPath, fileName); - } - private static string V2rayN() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - if (File.Exists(GetPath("v2rayN.exe"))) - return "v2rayN"; - else - return "v2rayN.Desktop"; - } - else - { - return "v2rayN.Desktop"; - } + var fileName = Uri.UnescapeDataString(string.Join(" ", args)); + Upgrade.UpgradeApp(fileName); } } } \ No newline at end of file diff --git a/v2rayN/v2rayUpgrade/Upgrade.cs b/v2rayN/v2rayUpgrade/Upgrade.cs new file mode 100644 index 00000000..ef627f10 --- /dev/null +++ b/v2rayN/v2rayUpgrade/Upgrade.cs @@ -0,0 +1,141 @@ +using System.Diagnostics; +using System.IO.Compression; +using System.Runtime.InteropServices; +using System.Text; + +namespace v2rayUpgrade +{ + internal class Upgrade + { + public static void UpgradeApp(string fileName) + { + Console.WriteLine(fileName); + Console.WriteLine("In progress, please wait...(正在进行中,请等待)"); + + Thread.Sleep(5000); + + if (!File.Exists(fileName)) + { + Console.WriteLine("Upgrade Failed, File Not Exist(升级失败,文件不存在)."); + return; + } + + try + { + Process[] existing = Process.GetProcessesByName(V2rayN()); + foreach (Process p in existing) + { + var path = p.MainModule?.FileName ?? ""; + if (path.StartsWith(GetPath(V2rayN()))) + { + p.Kill(); + p.WaitForExit(100); + } + } + } + catch (Exception ex) + { + // Access may be denied without admin right. The user may not be an administrator. + Console.WriteLine("Failed to close v2rayN(关闭v2rayN失败).\n" + + "Close it manually, or the upgrade may fail.(请手动关闭正在运行的v2rayN,否则可能升级失败。\n\n" + ex.StackTrace); + } + + StringBuilder sb = new(); + try + { + string thisAppOldFile = $"{GetExePath()}.tmp"; + File.Delete(thisAppOldFile); + string splitKey = "/"; + + using ZipArchive archive = ZipFile.OpenRead(fileName); + foreach (ZipArchiveEntry entry in archive.Entries) + { + try + { + if (entry.Length == 0) + { + continue; + } + + var lst = entry.FullName.Split(splitKey); + if (lst.Length == 1) continue; + string fullName = string.Join(splitKey, lst[1..lst.Length]); + + if (string.Equals(GetExePath(), GetPath(fullName), StringComparison.OrdinalIgnoreCase)) + { + File.Move(GetExePath(), thisAppOldFile); + } + + string entryOutputPath = GetPath(fullName); + Directory.CreateDirectory(Path.GetDirectoryName(entryOutputPath)!); + entry.ExtractToFile(entryOutputPath, true); + + Console.WriteLine(entryOutputPath); + } + catch (Exception ex) + { + sb.Append(ex.StackTrace); + } + } + } + catch (Exception ex) + { + Console.WriteLine("Upgrade Failed(升级失败)." + ex.StackTrace); + return; + } + if (sb.Length > 0) + { + Console.WriteLine("Upgrade Failed.\n" + + "(升级失败)." + sb.ToString()); + return; + } + + Console.WriteLine("Start v2rayN, please wait...(正在重启,请等待)"); + Thread.Sleep(3000); + Process process = new() + { + StartInfo = new() + { + FileName = V2rayN(), + WorkingDirectory = StartupPath() + } + }; + process.Start(); + } + + private static string GetExePath() + { + return Environment.ProcessPath ?? string.Empty; + } + + private static string StartupPath() + { + return AppDomain.CurrentDomain.BaseDirectory; + } + + private static string GetPath(string fileName) + { + string startupPath = StartupPath(); + if (string.IsNullOrEmpty(fileName)) + { + return startupPath; + } + return Path.Combine(startupPath, fileName); + } + + private static string V2rayN() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + if (File.Exists(GetPath("v2rayN.exe"))) + return "v2rayN"; + else + return "v2rayN.Desktop"; + } + else + { + return "v2rayN.Desktop"; + } + } + } +} \ No newline at end of file