From ef31db54b43b316ca40ae927afefa2cc5d8769d3 Mon Sep 17 00:00:00 2001 From: dawn-lc Date: Tue, 4 Oct 2022 01:30:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20=E9=92=88=E5=AF=B9=E6=9C=AA=E6=9D=A5=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E7=9A=84=E8=B7=A8=E5=B9=B3=E5=8F=B0=E4=BD=BF=E7=94=A8=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E4=BC=98=E5=8C=96=20=E4=BF=AE=E6=94=B9=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=A1=E6=81=AF=E6=94=B6=E9=9B=86=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArchivePasswordTestTool/Program.cs | 56 ++++++++++++------- .../Properties/launchSettings.json | 8 +++ ArchivePasswordTestTool/Utils.cs | 40 +++++++------ 3 files changed, 67 insertions(+), 37 deletions(-) create mode 100644 ArchivePasswordTestTool/Properties/launchSettings.json diff --git a/ArchivePasswordTestTool/Program.cs b/ArchivePasswordTestTool/Program.cs index 1f8ae68..01e6db6 100644 --- a/ArchivePasswordTestTool/Program.cs +++ b/ArchivePasswordTestTool/Program.cs @@ -12,7 +12,7 @@ namespace ArchivePasswordTestTool public class Program { public static readonly string AppName = "ArchivePasswordTestTool"; - public static readonly int[] Version = new int[] { 1, 5, 5 }; + public static readonly int[] Version = new int[] { 1, 5, 6 }; public static readonly string VersionType = "Release"; public static readonly string AppHomePage = "https://www.bilibili.com/read/cv6101558"; public static readonly string Developer = "dawn-lc"; @@ -28,6 +28,7 @@ public class Lib public class Config { public DateTime CheckUpgrade { get; set; } = new(); + public bool IsLatestVersion { get; set; } = false; public List Libs { get; set; } = new(); public string Dictionary { get; set; } = "PasswordDictionary.txt"; } @@ -43,26 +44,21 @@ private static async Task Initialization(StatusContext ctx) try { HttpResponseMessage Info = await HTTP.GetAsync(new Uri($"https://api.github.com/repos/{Developer}/{AppName}/releases/latest"), new Dictionary>() { ["user-agent"] = new List() { AppName + " " + string.Join(".", Version) } }); - if (Info.StatusCode != HttpStatusCode.OK) - { - Error("检查更新失败!请检查您的网络情况。"); - throw new Exception($"检查更新失败!\r\n{ Info.StatusCode }\r\n{ await Info.Content.ReadAsStringAsync() }"); - } - else + if (Info.StatusCode == HttpStatusCode.OK) { Upgrade.ReleasesInfo? LatestInfo = JsonSerializer.Deserialize(await Info.Content.ReadAsStringAsync()); if (LatestInfo != null) { + config.Libs.Clear(); if (Upgrade.CheckUpgrade(LatestInfo, Version, VersionType)) { - Log("当前本地程序已是最新版本。"); + config.IsLatestVersion = true; config.CheckUpgrade = DateTime.Now; - config.Libs.Clear(); foreach (var item in (LatestInfo.Body ?? "").Split("\r\n")) { if (!string.IsNullOrEmpty(item) && item[0..4] == "lib." && LatestInfo.Assets.Any(i => i.Name == item.Split(":")[0])) { - LatestInfo.Assets.Find(i => i.Name == item.Split(":")[0]).Label = item.Split(":")[1]; + LatestInfo.Assets.Find(i => i.Name == item.Split(":")[0])!.Label = item.Split(":")[1]; } } foreach (var item in LatestInfo.Assets) @@ -74,15 +70,13 @@ private static async Task Initialization(StatusContext ctx) } File.WriteAllText($"config.json", JsonSerializer.Serialize(config)); } - else - { - Warn("版本不是最新的, 请下载最新版。"); - Process.Start("explorer.exe", AppHomePage); - throw new Exception("版本过低!"); - } } } - + else + { + Error("检查更新失败!请检查您的网络情况。"); + throw new Exception($"检查更新失败!\r\n{ Info.StatusCode }\r\n{ await Info.Content.ReadAsStringAsync() }"); + } } catch (Exception ex) { @@ -130,6 +124,15 @@ static async Task Main(string[] args) await AnsiConsole.Status().StartAsync("初始化...", async ctx => { await Initialization(ctx); }); + if (!config.IsLatestVersion) + { + Warn("当前版本不是最新的,请前往下载最新版本。"); + if (AnsiConsole.Confirm("是否打开软件发布页?", true)) + { + Process.Start(new ProcessStartInfo(AppHomePage) { UseShellExecute = true }); + } + Environment.Exit(0); + } if (config.Libs.Any(i => !i.Exists)) { await AnsiConsole.Progress().AutoClear(true).HideCompleted(true).StartAsync(async ctx => @@ -143,7 +146,10 @@ await AnsiConsole.Progress().AutoClear(true).HideCompleted(true).StartAsync(asyn await HTTP.DownloadAsync(await HTTP.GetAsync(new Uri("https://raw.githubusercontent.com/baidusama/EroPassword/main/PasswordDictionary.txt"), new Dictionary>() { ["user-agent"] = new List() { AppName + " " + string.Join(".", Version) } }), "PasswordDictionary.txt", ctx.AddTask($"下载 PasswordDictionary.txt"), "PasswordDictionary.txt"); } }); - AnsiConsole.Confirm("下载完成,请重启软件以完成更新。", true); + if (AnsiConsole.Confirm("下载完成,请重启软件以完成更新。", true)) + { + Process.Start(Environment.ProcessPath!); + } Environment.Exit(0); } AnsiConsole.Clear(); @@ -168,7 +174,7 @@ await AnsiConsole.Progress().AutoClear(true).HideCompleted(true).StartAsync(asyn { if (StartupParametersCheck(args, "F")) { - ArchiveFile = GetParameter(args, "F", AnsiConsole.Ask("请输入需要进行测试的压缩包路径[[或将压缩包拖至本窗口]]:")).Replace("\"", ""); + ArchiveFile = GetParameter(args, "F", "").Replace("\"", ""); } else { @@ -224,7 +230,17 @@ await AnsiConsole.Progress().AutoClear(true).HideCompleted(true).StartAsync(asyn } file.Close(); } - Process.Start("Explorer.exe", $"/select, \"{ArchiveFile}[测试报告].txt\""); + switch (Environment.OSVersion.Platform) + { + case PlatformID.Win32S: + case PlatformID.Win32Windows: + case PlatformID.Win32NT: + case PlatformID.WinCE: + Process.Start("explorer.exe", $"/select, \"{ArchiveFile}[测试报告].txt\""); + break; + default: + break; + } } } catch (Exception ex) diff --git a/ArchivePasswordTestTool/Properties/launchSettings.json b/ArchivePasswordTestTool/Properties/launchSettings.json new file mode 100644 index 0000000..bcfae4b --- /dev/null +++ b/ArchivePasswordTestTool/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "ArchivePasswordTestTool": { + "commandName": "Project", + "commandLineArgs": "" + } + } +} \ No newline at end of file diff --git a/ArchivePasswordTestTool/Utils.cs b/ArchivePasswordTestTool/Utils.cs index 9182eae..06f3923 100644 --- a/ArchivePasswordTestTool/Utils.cs +++ b/ArchivePasswordTestTool/Utils.cs @@ -90,15 +90,15 @@ public static bool ComparisonFile(Stream FileA, Stream FileB) } public static void Log(string value) { - AnsiConsole.MarkupLine($"[bold][[{DateTime.Now}]] [lime]Info[/][/] {value}"); + AnsiConsole.MarkupLine($"[bold][[{DateTime.Now}]] [lime]I[/][/] {value}"); } public static void Warn(string value) { - AnsiConsole.MarkupLine($"[bold][[{DateTime.Now}]] [orangered1]Warning[/][/] {value}"); + AnsiConsole.MarkupLine($"[bold][[{DateTime.Now}]] [orangered1]W[/][/] {value}"); } public static void Error(string value) { - AnsiConsole.MarkupLine($"[bold][[{DateTime.Now}]] [red]Error[/][/] {value}"); + AnsiConsole.MarkupLine($"[bold][[{DateTime.Now}]] [red]E[/][/] {value}"); } public static bool StartupParametersCheck(List Parameters, string Flag) { @@ -293,7 +293,6 @@ public static bool CheckUpgrade(ReleasesInfo LatestInfo, int[] version, string v case -1: return true; } - Warn("当前版本不是最新的,请前往下载最新版本。"); return false; } catch (Exception) @@ -357,23 +356,30 @@ public static async Task PutAsync(Uri url, HttpContent cont } public static async Task DownloadAsync(HttpResponseMessage response, string path, ProgressTask task, string? name) { - if (File.Exists(path)) + try { - File.Delete(path); + if (File.Exists(path)) + { + File.Delete(path); + } + byte[] buffer = new byte[8192]; + Stream ResponseStream = await response.Content.ReadAsStreamAsync(); + using var destination = new FileStream(path, FileMode.OpenOrCreate); + long contentLength = response.Content.Headers.ContentLength ?? 0; + int bytesRead; + while ((bytesRead = await ResponseStream.ReadAsync(buffer).ConfigureAwait(false)) != 0) + { + await destination.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false); + task.Increment((double)bytesRead / contentLength * 100); + } + task.Increment(100); + task.StopTask(); + Log($"{name} 下载完成"); } - byte[] buffer = new byte[8192]; - Stream ResponseStream = await response.Content.ReadAsStreamAsync(); - using var destination = new FileStream(path, FileMode.OpenOrCreate); - long contentLength = response.Content.Headers.ContentLength ?? 0; - int bytesRead; - while ((bytesRead = await ResponseStream.ReadAsync(buffer).ConfigureAwait(false)) != 0) + catch (Exception ex) { - await destination.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false); - task.Increment((double)bytesRead / contentLength * 100); + throw new($"Downloading {name ?? response.RequestMessage?.RequestUri?.ToString() ?? path} \r\n {ex}"); } - task.Increment(100); - task.StopTask(); - Log($"{name} 下载完成"); } } }