From bfbe7be4b000326115c4585b5e2135127e4a2e66 Mon Sep 17 00:00:00 2001 From: X9VoiD Date: Thu, 10 Dec 2020 04:42:08 +0800 Subject: [PATCH 01/13] Create shortcuts after install, and fix progress bar --- InstallerLib.Tests/InstallerLib.Tests.csproj | 2 +- InstallerLib/Installer.cs | 60 +++++++++++++++++-- InstallerLib/InstallerLib.csproj | 2 +- .../OpenTabletDriver.Installer.Gtk.csproj | 2 +- .../OpenTabletDriver.Installer.Wpf.csproj | 2 +- OpenTabletDriver.Installer/MainForm.cs | 6 +- .../OpenTabletDriver.Installer.csproj | 2 +- 7 files changed, 63 insertions(+), 13 deletions(-) diff --git a/InstallerLib.Tests/InstallerLib.Tests.csproj b/InstallerLib.Tests/InstallerLib.Tests.csproj index d4311fa..8d2fdff 100644 --- a/InstallerLib.Tests/InstallerLib.Tests.csproj +++ b/InstallerLib.Tests/InstallerLib.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1;net5 + net5 false diff --git a/InstallerLib/Installer.cs b/InstallerLib/Installer.cs index 9f28c19..c3bc0f2 100644 --- a/InstallerLib/Installer.cs +++ b/InstallerLib/Installer.cs @@ -3,6 +3,8 @@ using System.IO; using System.IO.Compression; using System.Linq; +using System.Runtime.InteropServices; +using System.Text; using System.Threading.Tasks; using ICSharpCode.SharpZipLib.Tar; using InstallerLib.Info; @@ -38,7 +40,7 @@ public async Task Install() var release = await Downloader.GetLatestRelease(repo); var asset = await Downloader.GetCurrentPlatformAsset(repo, release); var extension = asset.Name.Split('.').Last(); - + using (var httpStream = await Downloader.GetAssetStream(asset)) { if (extension == "zip") @@ -60,7 +62,7 @@ public async Task Install() { await CopyStreamWithProgress(asset.Size, httpStream, memoryStream); using (var decompressionStream = new GZipStream(memoryStream, CompressionMode.Decompress)) - using (var tar = TarArchive.CreateInputTarArchive(decompressionStream)) + using (var tar = TarArchive.CreateInputTarArchive(decompressionStream, Encoding.Unicode)) { // Extract to directory tar.ExtractContents(InstallationDirectory.FullName); @@ -109,6 +111,22 @@ public async Task Install() using (var fs = VersionInfoFile.OpenWrite()) versionInfo.Serialize(fs); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + var target = Path.Join(InstallationDirectory.FullName, Launcher.AppName + ".exe"); + var shortcut = "OpenTabletDriver.lnk"; + var desktop = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), shortcut); + var startMenu = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), shortcut); + var startup = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.Startup), shortcut); + + // If this is the second install and user deleted any of these shortcuts then left some, don't create shortcuts + if (!File.Exists(desktop) && !File.Exists(startMenu) && !File.Exists(startup)) + { + CreateShortcut(desktop, target); + CreateShortcut(startMenu, target); + CreateShortcut(startup, target); + } + } return true; } return false; @@ -122,6 +140,7 @@ public void Uninstall() base.OnReport(0); if (InstallationDirectory.Exists) InstallationDirectory.Delete(true); + CleanShortcuts(); base.OnReport(1); } @@ -173,6 +192,20 @@ private void CleanDirectory(DirectoryInfo directory) directory.Create(); } + private void CleanShortcuts() + { + var shortcut = "OpenTabletDriver.lnk"; + var desktop = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), shortcut); + var startMenu = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), shortcut); + var startup = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.Startup), shortcut); + if (File.Exists(desktop)) + File.Delete(desktop); + if (File.Exists(startMenu)) + File.Delete(startMenu); + if (File.Exists(startup)) + File.Delete(startup); + } + private async Task CopyStreamWithProgress(int length, Stream source, Stream target) { var buffer = new byte[BufferSize]; @@ -181,12 +214,12 @@ private async Task CopyStreamWithProgress(int length, Stream source, Stream targ { base.OnReport((float)i / (float)length); - int bytesRead = await source.ReadAsync(buffer, 0, BufferSize); + int bytesRead = await source.ReadAsync(buffer.AsMemory(0, BufferSize)); if (bytesRead == 0) break; - await target.WriteAsync(buffer[0..bytesRead], 0, bytesRead); - i += BufferSize; + await target.WriteAsync(buffer[0..bytesRead].AsMemory(0, bytesRead)); + i += bytesRead; } // Reset stream position @@ -195,5 +228,22 @@ private async Task CopyStreamWithProgress(int length, Stream source, Stream targ // Report copy complete base.OnReport(1); } + + private void CreateShortcut(string shortcutPath, string targetPath) + { + string arg = "-NoProfile -Command " + + "$wShell = New-Object -ComObject WScript.Shell; " + + $"$Shortcut = $wShell.CreateShortcut('{shortcutPath}'); " + + $"$Shortcut.TargetPath = '{targetPath}'; " + + "$Shortcut.Save();"; + + var startInfo = new ProcessStartInfo("powershell", arg) + { + UseShellExecute = false, + CreateNoWindow = true + }; + + Process.Start(startInfo); + } } } \ No newline at end of file diff --git a/InstallerLib/InstallerLib.csproj b/InstallerLib/InstallerLib.csproj index 7571df5..c0c10cb 100644 --- a/InstallerLib/InstallerLib.csproj +++ b/InstallerLib/InstallerLib.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1;net5 + net5 diff --git a/OpenTabletDriver.Installer.Gtk/OpenTabletDriver.Installer.Gtk.csproj b/OpenTabletDriver.Installer.Gtk/OpenTabletDriver.Installer.Gtk.csproj index c747dea..9c522cc 100644 --- a/OpenTabletDriver.Installer.Gtk/OpenTabletDriver.Installer.Gtk.csproj +++ b/OpenTabletDriver.Installer.Gtk/OpenTabletDriver.Installer.Gtk.csproj @@ -2,7 +2,7 @@ WinExe - netcoreapp3.1;net5 + net5 0.4.1 diff --git a/OpenTabletDriver.Installer.Wpf/OpenTabletDriver.Installer.Wpf.csproj b/OpenTabletDriver.Installer.Wpf/OpenTabletDriver.Installer.Wpf.csproj index c384612..c1ddc32 100644 --- a/OpenTabletDriver.Installer.Wpf/OpenTabletDriver.Installer.Wpf.csproj +++ b/OpenTabletDriver.Installer.Wpf/OpenTabletDriver.Installer.Wpf.csproj @@ -2,7 +2,7 @@ WinExe - netcoreapp3.1;net5-windows + net5-windows ../OpenTabletDriver.Installer/Assets/otd.ico 0.4.1 diff --git a/OpenTabletDriver.Installer/MainForm.cs b/OpenTabletDriver.Installer/MainForm.cs index 8a1091e..f8f8912 100644 --- a/OpenTabletDriver.Installer/MainForm.cs +++ b/OpenTabletDriver.Installer/MainForm.cs @@ -192,8 +192,8 @@ public async Task Install(bool isUpdate = false) MinValue = 0, MaxValue = 100 }; - App.Current.Installer.ProgressChanged += (sender, progress) => installProgress.Value = (int)(100 * progress); - SetStatus("Installing...", installProgress); + App.Current.Installer.ProgressChanged += (sender, progress) => Application.Instance.AsyncInvoke(() => installProgress.Value = (int)(100 * progress)); + SetStatus("Installing...", installProgress); if (!await App.Current.Installer.Install()) { @@ -214,7 +214,7 @@ public void Uninstall() MinValue = 0, MaxValue = 100 }; - App.Current.Installer.ProgressChanged += (sender, progress) => uninstallProgress.Value = (int)(100 * progress); + App.Current.Installer.ProgressChanged += (sender, progress) => Application.Instance.AsyncInvoke(() => uninstallProgress.Value = (int)(100 * progress)); SetStatus("Uninstalling...", uninstallProgress); App.Current.Installer.Uninstall(); diff --git a/OpenTabletDriver.Installer/OpenTabletDriver.Installer.csproj b/OpenTabletDriver.Installer/OpenTabletDriver.Installer.csproj index 7fb9458..0fa94ba 100644 --- a/OpenTabletDriver.Installer/OpenTabletDriver.Installer.csproj +++ b/OpenTabletDriver.Installer/OpenTabletDriver.Installer.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;net5 + net5 From 60343181798d5b9f1c75f672ec88d0009f710bb1 Mon Sep 17 00:00:00 2001 From: X9VoiD Date: Thu, 10 Dec 2020 04:51:32 +0800 Subject: [PATCH 02/13] Convert indentation to spaces --- OpenTabletDriver.Installer/MainForm.cs | 474 ++++++++++++------------- 1 file changed, 237 insertions(+), 237 deletions(-) diff --git a/OpenTabletDriver.Installer/MainForm.cs b/OpenTabletDriver.Installer/MainForm.cs index f8f8912..94e48b8 100644 --- a/OpenTabletDriver.Installer/MainForm.cs +++ b/OpenTabletDriver.Installer/MainForm.cs @@ -12,269 +12,269 @@ namespace OpenTabletDriver.Installer { public partial class MainForm : Form - { - public MainForm() - { - Title = "OpenTabletDriver Updater"; - ClientSize = new Size(400, 350); - Icon = App.Logo.WithSize(App.Logo.Size); + { + public MainForm() + { + Title = "OpenTabletDriver Updater"; + ClientSize = new Size(400, 350); + Icon = App.Logo.WithSize(App.Logo.Size); - this.status = new StackLayout() - { - Orientation = Orientation.Vertical, - VerticalContentAlignment = VerticalAlignment.Center, - HorizontalContentAlignment = HorizontalAlignment.Center, - Padding = 10, - Spacing = 5, - }; + this.status = new StackLayout() + { + Orientation = Orientation.Vertical, + VerticalContentAlignment = VerticalAlignment.Center, + HorizontalContentAlignment = HorizontalAlignment.Center, + Padding = 10, + Spacing = 5, + }; - var showFolder = new Command { MenuText = "Show install folder...", ToolBarText = "Show install folder" }; - showFolder.Executed += (sender, e) => SystemInfo.Open(InstallationInfo.Current.InstallationDirectory); + var showFolder = new Command { MenuText = "Show install folder...", ToolBarText = "Show install folder" }; + showFolder.Executed += (sender, e) => SystemInfo.Open(InstallationInfo.Current.InstallationDirectory); - var quitCommand = new Command { MenuText = "Quit", Shortcut = Application.Instance.CommonModifier | Keys.Q }; - quitCommand.Executed += (sender, e) => Application.Instance.Quit(); + var quitCommand = new Command { MenuText = "Quit", Shortcut = Application.Instance.CommonModifier | Keys.Q }; + quitCommand.Executed += (sender, e) => Application.Instance.Quit(); - this.startButton = new Button((sender, e) => Start()) - { - Text = "Start" - }; + this.startButton = new Button((sender, e) => Start()) + { + Text = "Start" + }; - this.installButton = new Button(async (sender, e) => await Install()) - { - Text = "Install" - }; + this.installButton = new Button(async (sender, e) => await Install()) + { + Text = "Install" + }; - this.uninstallButton = new Button((sender, e) => Uninstall()) - { - Text = "Uninstall" - }; - - this.updateButton = new Button(async (sender, e) => await Install(isUpdate: true)) - { - Text = "Update" - }; + this.uninstallButton = new Button((sender, e) => Uninstall()) + { + Text = "Uninstall" + }; + + this.updateButton = new Button(async (sender, e) => await Install(isUpdate: true)) + { + Text = "Update" + }; - // create menu - Menu = new MenuBar - { - Items = - { - // File submenu - new ButtonMenuItem - { - Text = "&File", - Items = - { - showFolder - } - }, - }, - ApplicationItems = - { - // application (OS X) or file menu (others) - }, - QuitItem = quitCommand - }; + // create menu + Menu = new MenuBar + { + Items = + { + // File submenu + new ButtonMenuItem + { + Text = "&File", + Items = + { + showFolder + } + }, + }, + ApplicationItems = + { + // application (OS X) or file menu (others) + }, + QuitItem = quitCommand + }; - PerformMigration(); - UpdateControls(autostart: true); - } + PerformMigration(); + UpdateControls(autostart: true); + } - public async void UpdateControls(bool autostart = false) - { - if (!shownInstallerUpdate && await InstallerUpdater.CheckForUpdate()) - { - var result = MessageBox.Show( - "An update is available for the installer." + Environment.NewLine + - "Do you wish to be directed to the latest release?", - "Installer Update", - MessageBoxButtons.YesNo, - MessageBoxType.Information - ); - switch (result) - { - case DialogResult.Yes: + public async void UpdateControls(bool autostart = false) + { + if (!shownInstallerUpdate && await InstallerUpdater.CheckForUpdate()) + { + var result = MessageBox.Show( + "An update is available for the installer." + Environment.NewLine + + "Do you wish to be directed to the latest release?", + "Installer Update", + MessageBoxButtons.YesNo, + MessageBoxType.Information + ); + switch (result) + { + case DialogResult.Yes: SystemInfo.Open(GitHubInfo.InstallerReleaseUrl); - Application.Instance.Quit(); - break; - } - autostart = false; - shownInstallerUpdate = true; - } + Application.Instance.Quit(); + break; + } + autostart = false; + shownInstallerUpdate = true; + } - bool installed = App.Current.Installer.IsInstalled; - bool update = await App.Current.Installer.CheckForUpdate(); - - var buttons = new List