From 452dbd23a59fe9f9ebbec95fb3963dc5c8bdb779 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 30 May 2024 14:41:50 +1000 Subject: [PATCH] [Windows] Improve embedded resource extraction behaviour (#478) --- .../QMK Toolbox/Helpers/DriverInstaller.cs | 5 ++-- .../Helpers/EmbeddedResourceHelper.cs | 23 ++++++++++++++++--- windows/QMK Toolbox/MainWindow.Designer.cs | 11 ++++++++- windows/QMK Toolbox/MainWindow.cs | 8 +++++-- .../Usb/Bootloader/BootloaderDevice.cs | 9 ++++---- 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/windows/QMK Toolbox/Helpers/DriverInstaller.cs b/windows/QMK Toolbox/Helpers/DriverInstaller.cs index ad4a14169c..b34460c52c 100644 --- a/windows/QMK Toolbox/Helpers/DriverInstaller.cs +++ b/windows/QMK Toolbox/Helpers/DriverInstaller.cs @@ -26,8 +26,9 @@ public static bool DisplayPrompt() private static bool InstallDrivers() { - var driversPath = Path.Combine(Application.LocalUserAppDataPath, DriversListFilename); - var installerPath = Path.Combine(Application.LocalUserAppDataPath, InstallerFilename); + string toolboxData = EmbeddedResourceHelper.GetResourceFolder(); + string driversPath = Path.Combine(toolboxData, DriversListFilename); + string installerPath = Path.Combine(toolboxData, InstallerFilename); if (!File.Exists(driversPath)) { diff --git a/windows/QMK Toolbox/Helpers/EmbeddedResourceHelper.cs b/windows/QMK Toolbox/Helpers/EmbeddedResourceHelper.cs index bf605e8e7e..9985a15117 100644 --- a/windows/QMK Toolbox/Helpers/EmbeddedResourceHelper.cs +++ b/windows/QMK Toolbox/Helpers/EmbeddedResourceHelper.cs @@ -1,7 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Reflection; -using System.Windows.Forms; namespace QMK_Toolbox.Helpers { @@ -28,9 +28,26 @@ public static class EmbeddedResourceHelper "libwinpthread-1.dll" }; + public static string GetResourceFolder() + { + string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + return Path.Combine(appData, "QMK", "Toolbox"); + } + + public static void InitResourceFolder() + { + string toolboxData = GetResourceFolder(); + if (Directory.Exists(toolboxData)) + { + Directory.Delete(toolboxData, true); + } + Directory.CreateDirectory(toolboxData); + ExtractResources(Resources); + } + public static void ExtractResource(string file) { - var destPath = Path.Combine(Application.LocalUserAppDataPath, file); + string destPath = Path.Combine(GetResourceFolder(), file); if (!File.Exists(destPath)) { diff --git a/windows/QMK Toolbox/MainWindow.Designer.cs b/windows/QMK Toolbox/MainWindow.Designer.cs index a6d47b01af..f0854798b0 100644 --- a/windows/QMK Toolbox/MainWindow.Designer.cs +++ b/windows/QMK Toolbox/MainWindow.Designer.cs @@ -73,6 +73,7 @@ private void InitializeComponent() checkForUpdatesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); helpToolStripMenuSep = new System.Windows.Forms.ToolStripSeparator(); aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + clearResourcesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)windowStateBindingSource).BeginInit(); fileGroupBox.SuspendLayout(); logContextMenu.SuspendLayout(); @@ -338,7 +339,7 @@ private void InitializeComponent() // // toolsToolStripMenuItem // - toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { flashToolStripMenuItem, eepromToolStripMenuItem, exitDFUToolStripMenuItem, toolsToolStripMenuSep1, autoFlashToolStripMenuItem, showAllDevicesToolStripMenuItem, toolsToolStripMenuSep2, keyTesterToolStripMenuItem, hidConsoleToolStripMenuItem, installDriversToolStripMenuItem, toolsToolStripMenuSep3, optionsToolStripMenuItem }); + toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { flashToolStripMenuItem, eepromToolStripMenuItem, exitDFUToolStripMenuItem, toolsToolStripMenuSep1, autoFlashToolStripMenuItem, showAllDevicesToolStripMenuItem, toolsToolStripMenuSep2, keyTesterToolStripMenuItem, hidConsoleToolStripMenuItem, installDriversToolStripMenuItem, clearResourcesToolStripMenuItem, toolsToolStripMenuSep3, optionsToolStripMenuItem }); toolsToolStripMenuItem.Name = "toolsToolStripMenuItem"; toolsToolStripMenuItem.Size = new System.Drawing.Size(46, 20); toolsToolStripMenuItem.Text = "&Tools"; @@ -490,6 +491,13 @@ private void InitializeComponent() aboutToolStripMenuItem.Text = "&About"; aboutToolStripMenuItem.Click += AboutMenuItem_Click; // + // clearResourcesToolStripMenuItem + // + clearResourcesToolStripMenuItem.Name = "clearResourcesToolStripMenuItem"; + clearResourcesToolStripMenuItem.Size = new System.Drawing.Size(196, 22); + clearResourcesToolStripMenuItem.Text = "Clear Resources"; + clearResourcesToolStripMenuItem.Click += ClearResourcesMenuItem_Click; + // // MainWindow // AllowDrop = true; @@ -572,5 +580,6 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripSeparator toolsToolStripMenuSep3; private QMK_Toolbox.BindableToolStripMenuItem showAllDevicesToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem hidConsoleToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem clearResourcesToolStripMenuItem; } } diff --git a/windows/QMK Toolbox/MainWindow.cs b/windows/QMK Toolbox/MainWindow.cs index 53c113609e..4ca7b4361f 100644 --- a/windows/QMK Toolbox/MainWindow.cs +++ b/windows/QMK Toolbox/MainWindow.cs @@ -59,8 +59,6 @@ private void MainWindow_Load(object sender, EventArgs e) mcuBox.SelectedValue = Settings.Default.targetSetting; - EmbeddedResourceHelper.ExtractResources(EmbeddedResourceHelper.Resources); - logTextBox.LogInfo($"QMK Toolbox {Application.ProductVersion} (https://qmk.fm/toolbox)"); logTextBox.LogInfo("Supported bootloaders:"); logTextBox.LogInfo(" - ARM DFU (APM32, Kiibohd, STM32, STM32duino) and RISC-V DFU (GD32V) via dfu-util (http://dfu-util.sourceforge.net/)"); @@ -105,6 +103,7 @@ private void MainWindow_Shown(object sender, EventArgs e) { if (Settings.Default.firstStart) { + EmbeddedResourceHelper.InitResourceFolder(); Settings.Default.Upgrade(); } @@ -507,6 +506,11 @@ private void InstallDriversMenuItem_Click(object sender, EventArgs e) DriverInstaller.DisplayPrompt(); } + private void ClearResourcesMenuItem_Click(object sender, EventArgs e) + { + EmbeddedResourceHelper.InitResourceFolder(); + } + private void KeyTesterToolStripMenuItem_Click(object sender, EventArgs e) { KeyTesterWindow.GetInstance().Show(this); diff --git a/windows/QMK Toolbox/Usb/Bootloader/BootloaderDevice.cs b/windows/QMK Toolbox/Usb/Bootloader/BootloaderDevice.cs index 96fbc1e935..93b1d29e93 100644 --- a/windows/QMK Toolbox/Usb/Bootloader/BootloaderDevice.cs +++ b/windows/QMK Toolbox/Usb/Bootloader/BootloaderDevice.cs @@ -1,9 +1,9 @@ -using System; +using QMK_Toolbox.Helpers; +using System; using System.Diagnostics; using System.IO; using System.Management; using System.Threading.Tasks; -using System.Windows.Forms; namespace QMK_Toolbox.Usb.Bootloader { @@ -61,14 +61,15 @@ public virtual Task Reset(string mcu) protected async Task RunProcessAsync(string command, string args) { PrintMessage($"{command} {args}", MessageType.Command); + string toolboxData = EmbeddedResourceHelper.GetResourceFolder(); using var process = new Process { StartInfo = { - FileName = Path.Combine(Application.LocalUserAppDataPath, command), + FileName = Path.Combine(toolboxData, command), Arguments = args, - WorkingDirectory = Application.LocalUserAppDataPath, + WorkingDirectory = toolboxData, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true,