From 7458b7b4cd04ef21219e9c2cd5d05089647b9b49 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 4 Sep 2020 15:19:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9B=B4=E6=96=B0=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E5=AF=B9=E9=9D=9E.zip=E5=8E=8B=E7=BC=A9=E5=8C=85?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Netch/Forms/MainForm.Misc.cs | 31 +++++------ NetchUpdater/Program.cs | 100 ++++++++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 37 deletions(-) diff --git a/Netch/Forms/MainForm.Misc.cs b/Netch/Forms/MainForm.Misc.cs index c7b7ab18..b504f5f9 100644 --- a/Netch/Forms/MainForm.Misc.cs +++ b/Netch/Forms/MainForm.Misc.cs @@ -31,40 +31,35 @@ namespace Netch.Forms { _updater.NewVersionFound += (o, args) => { - if (_updater.LatestVersionDownloadUrl.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) - { - NotifyTip($"{i18N.Translate(@"New version available", ": ")}{_updater.LatestVersionNumber}"); - NewVersionLabel.Visible = true; - } + NotifyTip($"{i18N.Translate(@"New version available", ": ")}{_updater.LatestVersionNumber}"); + NewVersionLabel.Visible = true; }; _updater.Check(Global.Settings.CheckBetaUpdate); } private async void NewVersionLabel_Click(object sender, EventArgs e) { + if (!_updater.LatestVersionDownloadUrl.Contains("Netch")) + { + Utils.Utils.Open(_updater.LatestVersionUrl); + return; + } + if (MessageBoxX.Show(i18N.Translate("Download and install now?"), confirm: true) != DialogResult.OK) return; NotifyTip(i18N.Translate("Start downloading new version")); - var fileName = $"Netch{_updater.LatestVersionNumber}.zip"; + var fileName = Path.GetFileName(new Uri(_updater.LatestVersionDownloadUrl).LocalPath); + fileName = fileName.Insert(fileName.LastIndexOf('.'), _updater.LatestVersionNumber); var fileFullPath = Path.Combine(Global.NetchDir, "data", fileName); - var updateFileValid = false; try { - if (File.Exists(fileFullPath)) + if (!File.Exists(fileFullPath)) { - if (!(updateFileValid = Utils.Utils.IsZipValid(fileFullPath))) - { - File.Delete(fileFullPath); - } + await WebUtil.DownloadFileAsync(WebUtil.CreateRequest(_updater.LatestVersionDownloadUrl), fileFullPath); } - if (!File.Exists(fileFullPath)) - await WebUtil.DownloadFileAsync(WebUtil.CreateRequest(_updater.LatestVersionDownloadUrl), fileFullPath); - if (updateFileValid || Utils.Utils.IsZipValid(fileFullPath)) - RunUpdater(); - else - throw new InvalidDataException($"{fileFullPath} invalid"); + RunUpdater(); } catch (Exception exception) { diff --git a/NetchUpdater/Program.cs b/NetchUpdater/Program.cs index a94580f7..4172f5b2 100644 --- a/NetchUpdater/Program.cs +++ b/NetchUpdater/Program.cs @@ -12,10 +12,10 @@ namespace NetchUpdater { internal class Program { - static string UpdaterFullName; - static string UpdaterDirectory; - static string UpdaterFriendlyName; - static Process CurrentProcess; + private static readonly string UpdaterFullName; + private static readonly string UpdaterDirectory; + private static readonly string UpdaterFriendlyName; + private static readonly Process CurrentProcess; static Program() { @@ -153,10 +153,33 @@ namespace NetchUpdater #region Update + string extractPath = null; if (!updateExtracted) - Extract(updatePath, targetPath, true); - else - MoveDirectory(updatePath, targetPath, true); + { + extractPath = Path.Combine(UpdaterDirectory, "extract"); + Extract(updatePath, extractPath, true); + + var netchExeFileInfo = FindFile("Netch.exe", extractPath); + + if (netchExeFileInfo == null) + { + throw new Exception("Netch.exe not found in archive!"); + } + + updatePath = netchExeFileInfo.Directory.FullName; + } + + MoveDirectory(updatePath, targetPath, true); + + try + { + if (extractPath != null) + Directory.Delete(extractPath, true); + } + catch + { + // ignored + } #endregion @@ -211,6 +234,45 @@ namespace NetchUpdater })?.WaitForExit(); } + public static FileInfo FindFile(string filename, string directory) + { + var DirStack = new Stack(); + DirStack.Push(directory); + + while (DirStack.Count > 0) + { + var DirInfo = new DirectoryInfo(DirStack.Pop()); + try + { + foreach (var DirChildInfo in DirInfo.GetDirectories()) + { + DirStack.Push(DirChildInfo.FullName); + } + } + catch + { + // ignored + } + + try + { + foreach (var FileChildInfo in DirInfo.GetFiles()) + { + if (FileChildInfo.Name == filename) + { + return FileChildInfo; + } + } + } + catch + { + // ignored + } + } + + return null; + } + private static void MoveDirectory(string sourceDirName, string destDirName, bool overwrite) { sourceDirName = Path.GetFullPath(sourceDirName); @@ -221,19 +283,19 @@ namespace NetchUpdater } else { - var DirStack = new Stack(); - DirStack.Push(sourceDirName); + var dirStack = new Stack(); + dirStack.Push(sourceDirName); - while (DirStack.Count > 0) + while (dirStack.Count > 0) { - var DirInfo = new DirectoryInfo(DirStack.Pop()); + var dirInfo = new DirectoryInfo(dirStack.Pop()); try { - foreach (var DirChildInfo in DirInfo.GetDirectories()) + foreach (var dirChildInfo in dirInfo.GetDirectories()) { + var destPath = dirChildInfo.FullName.Replace(sourceDirName, destDirName); try { - var destPath = DirChildInfo.ToString().Replace(sourceDirName, destDirName); if (!Directory.Exists(destPath)) { Directory.CreateDirectory(destPath); @@ -241,27 +303,27 @@ namespace NetchUpdater } catch (Exception e) { - Console.WriteLine($"Create {DirChildInfo} Folder Error: {e.Message}"); + Console.WriteLine($"Create {destPath} Folder Error: {e.Message}"); } - DirStack.Push(DirChildInfo.FullName); + dirStack.Push(dirChildInfo.FullName); } - foreach (var FileChildInfo in DirInfo.GetFiles()) + foreach (var fileChildInfo in dirInfo.GetFiles()) { + var destPath = fileChildInfo.FullName.Replace(sourceDirName, destDirName); try { - var destPath = FileChildInfo.ToString().Replace(sourceDirName, destDirName); if (File.Exists(destPath)) { File.Delete(destPath); } - FileChildInfo.MoveTo(destDirName); + fileChildInfo.MoveTo(destPath); } catch (Exception e) { - Console.WriteLine($"Move {FileChildInfo} Error: {e.Message}"); + Console.WriteLine($"Move {fileChildInfo.FullName} To {destPath} Error: {e.Message}"); } } }