Skip to content

Commit

Permalink
修复命令行参数不生效问题
Browse files Browse the repository at this point in the history
针对未来可能的跨平台使用进行优化
修改错误信息收集内容范围
  • Loading branch information
dawn-lc committed Oct 3, 2022
1 parent 170c2d4 commit ef31db5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
56 changes: 36 additions & 20 deletions ArchivePasswordTestTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -28,6 +28,7 @@ public class Lib
public class Config
{
public DateTime CheckUpgrade { get; set; } = new();
public bool IsLatestVersion { get; set; } = false;
public List<Lib> Libs { get; set; } = new();
public string Dictionary { get; set; } = "PasswordDictionary.txt";
}
Expand All @@ -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<string, IEnumerable<string>>() { ["user-agent"] = new List<string>() { 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<Upgrade.ReleasesInfo>(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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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 =>
Expand All @@ -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<string, IEnumerable<string>>() { ["user-agent"] = new List<string>() { 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();
Expand All @@ -168,7 +174,7 @@ await AnsiConsole.Progress().AutoClear(true).HideCompleted(true).StartAsync(asyn
{
if (StartupParametersCheck(args, "F"))
{
ArchiveFile = GetParameter(args, "F", AnsiConsole.Ask<string>("请输入需要进行测试的压缩包路径[[或将压缩包拖至本窗口]]:")).Replace("\"", "");
ArchiveFile = GetParameter(args, "F", "").Replace("\"", "");
}
else
{
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions ArchivePasswordTestTool/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"ArchivePasswordTestTool": {
"commandName": "Project",
"commandLineArgs": ""
}
}
}
40 changes: 23 additions & 17 deletions ArchivePasswordTestTool/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> Parameters, string Flag)
{
Expand Down Expand Up @@ -293,7 +293,6 @@ public static bool CheckUpgrade(ReleasesInfo LatestInfo, int[] version, string v
case -1:
return true;
}
Warn("当前版本不是最新的,请前往下载最新版本。");
return false;
}
catch (Exception)
Expand Down Expand Up @@ -357,23 +356,30 @@ public static async Task<HttpResponseMessage> 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} 下载完成");
}
}
}
Expand Down

0 comments on commit ef31db5

Please sign in to comment.