diff --git a/YAMDCC.Common/Dialogs/ProgressDialog.cs b/YAMDCC.Common/Dialogs/ProgressDialog.cs index dbf0c18..fb16b45 100644 --- a/YAMDCC.Common/Dialogs/ProgressDialog.cs +++ b/YAMDCC.Common/Dialogs/ProgressDialog.cs @@ -34,11 +34,11 @@ protected override CreateParams CreateParams } #endregion - public TResult Result { get; set; } + private readonly Timer DisplayTimer = new(); - private readonly Func DoWork; + public TResult Result { get; set; } - private readonly Timer DisplayTimer = new(); + public Func DoWork { get; set; } public string Caption { @@ -49,30 +49,13 @@ public string Caption } /// - /// Initialises a new instance of the class. + /// Initialises a new instance of the class. /// - /// - /// The window caption to use. - /// - /// - /// The to run when showing this window. - /// - /// - public ProgressDialog(string caption, Func doWork) + public ProgressDialog() { Opacity = 0; InitializeComponent(); - // sanity check - if (doWork is null) - { - throw new ArgumentNullException(nameof(doWork), "The doWork parameter was null."); - } - DoWork = doWork; - - // set title text - Caption = caption; - pbProgress.Style = ProgressBarStyle.Marquee; DisplayTimer.Interval = 1000; @@ -81,6 +64,11 @@ public ProgressDialog(string caption, Func doWork) private async void OnLoad(object sender, EventArgs e) { + // sanity check + if (DoWork is null) + { + throw new InvalidOperationException("The DoWork property is null."); + } DisplayTimer.Start(); Result = await Task.Run(DoWork); Close(); diff --git a/YAMDCC.ConfigEditor/MainForm.cs b/YAMDCC.ConfigEditor/MainForm.cs index ef5e9ad..8a11e7e 100644 --- a/YAMDCC.ConfigEditor/MainForm.cs +++ b/YAMDCC.ConfigEditor/MainForm.cs @@ -161,8 +161,11 @@ private void MainWindow_Load(object sender, EventArgs e) IPCClient.Error += new EventHandler>(IPCError); IPCClient.Start(); - ProgressDialog dlg = new("Connecting to YAMDCC service...", - () => !IPCClient.WaitForConnection(5000)); + ProgressDialog dlg = new() + { + Caption = "Connecting to YAMDCC service...", + DoWork = () => !IPCClient.WaitForConnection(5000) + }; dlg.ShowDialog(); if (dlg.Result) @@ -527,8 +530,10 @@ private void tsiStopSvc_Click(object sender, EventArgs e) IPCClient.Stop(); Hide(); - ProgressDialog dlg = new(Strings.GetString("dlgSvcStopping"), - static () => + ProgressDialog dlg = new() + { + Caption = Strings.GetString("dlgSvcStopping"), + DoWork = static () => { if (!Utils.StopService("yamdccsvc")) { @@ -537,7 +542,8 @@ private void tsiStopSvc_Click(object sender, EventArgs e) } Utils.ShowInfo(Strings.GetString("dlgSvcStopped"), "Success"); return true; - }); + } + }; dlg.ShowDialog(); Close(); @@ -558,32 +564,36 @@ private void tsiUninstall_Click(object sender, EventArgs e) IPCClient.Stop(); Hide(); - ProgressDialog dlg = new(Strings.GetString("dlgSvcUninstalling"), () => + ProgressDialog dlg = new() { - // Apparently this fixes the YAMDCC service not uninstalling - // when YAMDCC is launched by certain means - if (Utils.StopService("yamdccsvc")) + Caption = Strings.GetString("dlgSvcUninstalling"), + DoWork = () => { - if (Utils.UninstallService("yamdccsvc")) + // Apparently this fixes the YAMDCC service not uninstalling + // when YAMDCC is launched by certain means + if (Utils.StopService("yamdccsvc")) { - // Delete the auto-update scheduled task - Utils.RunCmd("updater", "--setautoupdate false"); - - // Only delete service data if the - // service uninstalled successfully - if (delData) + if (Utils.UninstallService("yamdccsvc")) { - Directory.Delete(Paths.Data, true); + // Delete the auto-update scheduled task + Utils.RunCmd("updater", "--setautoupdate false"); + + // Only delete service data if the + // service uninstalled successfully + if (delData) + { + Directory.Delete(Paths.Data, true); + } + Utils.ShowInfo(Strings.GetString("dlgSvcUninstalled"), "Success"); + return true; } - Utils.ShowInfo(Strings.GetString("dlgSvcUninstalled"), "Success"); - return true; + Utils.ShowError(Strings.GetString("dlgUninstallErr")); + return false; } - Utils.ShowError(Strings.GetString("dlgUninstallErr")); + Utils.ShowError(Strings.GetString("dlgSvcStopErr")); return false; } - Utils.ShowError(Strings.GetString("dlgSvcStopErr")); - return false; - }); + }; dlg.ShowDialog(); Close(); } diff --git a/YAMDCC.ConfigEditor/Program.cs b/YAMDCC.ConfigEditor/Program.cs index 054dc9b..ad0bea5 100644 --- a/YAMDCC.ConfigEditor/Program.cs +++ b/YAMDCC.ConfigEditor/Program.cs @@ -68,25 +68,29 @@ private static void Main() Strings.GetString("dlgSvcNotInstalled"), "Service not installed", MessageBoxButtons.YesNo) == DialogResult.Yes) { - ProgressDialog dlg = new(Strings.GetString("dlgSvcInstalling"), () => + ProgressDialog dlg = new() { - if (Utils.InstallService("yamdccsvc")) + Caption = Strings.GetString("dlgSvcInstalling"), + DoWork = () => { - if (Utils.StartService("yamdccsvc")) + if (Utils.InstallService("yamdccsvc")) { - return true; + if (Utils.StartService("yamdccsvc")) + { + return true; + } + else + { + Utils.ShowError(Strings.GetString("dlgSvcStartCrash")); + } } else { - Utils.ShowError(Strings.GetString("dlgSvcStartCrash")); + Utils.ShowError(Strings.GetString("dlgSvcInstallFail")); } + return false; } - else - { - Utils.ShowError(Strings.GetString("dlgSvcInstallFail")); - } - return false; - }); + }; dlg.ShowDialog(); if (dlg.Result) @@ -114,21 +118,25 @@ private static void Main() Strings.GetString("dlgSvcNotRunning"), "Service not running", MessageBoxButtons.YesNo) == DialogResult.Yes) { - ProgressDialog dlg = new(Strings.GetString("dlgSvcStarting"), () => - { - if (Utils.StartService("yamdccsvc")) - { - return false; - } - else + ProgressDialog dlg = new() { - Utils.ShowError(Strings.GetString("dlgSvcStartCrash")); - return true; - } - }); + Caption = Strings.GetString("dlgSvcStarting"), + DoWork = () => + { + if (Utils.StartService("yamdccsvc")) + { + return false; + } + else + { + Utils.ShowError(Strings.GetString("dlgSvcStartCrash")); + return true; + } + } + }; dlg.ShowDialog(); - if ((bool)dlg.Result) + if (dlg.Result) { return; } diff --git a/YAMDCC.ECAccess/Driver.cs b/YAMDCC.ECAccess/Driver.cs index 835d2f0..f0cbb74 100644 --- a/YAMDCC.ECAccess/Driver.cs +++ b/YAMDCC.ECAccess/Driver.cs @@ -113,7 +113,7 @@ public bool Install() AdvApi32.ServiceType.KernelDriver, AdvApi32.ServiceStartType.DemandStart, AdvApi32.ServiceError.Normal, - DriverPath, null, null, null, null, null); + fullPath, null, null, null, null, null); if (hSvc == IntPtr.Zero) { diff --git a/YAMDCC.ECAccess/YAMDCC.ECAccess.csproj b/YAMDCC.ECAccess/YAMDCC.ECAccess.csproj index 7aae7f5..37217d3 100644 --- a/YAMDCC.ECAccess/YAMDCC.ECAccess.csproj +++ b/YAMDCC.ECAccess/YAMDCC.ECAccess.csproj @@ -19,6 +19,9 @@ none + + + PreserveNewest diff --git a/YAMDCC.Service/FanControlService.cs b/YAMDCC.Service/FanControlService.cs index dc6be10..c87a97d 100644 --- a/YAMDCC.Service/FanControlService.cs +++ b/YAMDCC.Service/FanControlService.cs @@ -86,45 +86,53 @@ public FanControlService(Logger logger) #region Events protected override void OnStart(string[] args) { - Log.Info(Strings.GetString("svcStarting")); - - // Install WinRing0 to get EC access try { - Log.Info(Strings.GetString("drvLoad")); - if (!_EC.LoadDriver()) + Log.Info(Strings.GetString("svcStarting")); + + // Install WinRing0 to get EC access + try { - throw new Win32Exception(_EC.GetDriverError()); + Log.Info(Strings.GetString("drvLoad")); + if (!_EC.LoadDriver()) + { + throw new Win32Exception(_EC.GetDriverError()); + } } - } - catch (Win32Exception) - { - Log.Fatal(Strings.GetString("drvLoadFail")); - _EC.UnloadDriver(); - ExitCode = 1; - throw; - } - Log.Info(Strings.GetString("drvLoadSuccess")); + catch (Win32Exception) + { + Log.Fatal(Strings.GetString("drvLoadFail")); + _EC.UnloadDriver(); + ExitCode = 1; + throw; + } + Log.Info(Strings.GetString("drvLoadSuccess")); - // Load the last applied YAMDCC config. - bool confLoaded = LoadConf(); + // Load the last applied YAMDCC config. + bool confLoaded = LoadConf(); - // Set up IPC server - Log.Info("Starting IPC server..."); - IPCServer.Start(); + // Set up IPC server + Log.Info("Starting IPC server..."); + IPCServer.Start(); - Log.Info(Strings.GetString("svcStarted")); + Log.Info(Strings.GetString("svcStarted")); - // Attempt to read default fan curve if it's pending: - if (CommonConfig.GetECtoConfState() == ECtoConfState.PostReboot) - { - ECtoConf(); - } + // Attempt to read default fan curve if it's pending: + if (CommonConfig.GetECtoConfState() == ECtoConfState.PostReboot) + { + ECtoConf(); + } - // Apply the fan curve and charging threshold: - if (confLoaded) + // Apply the fan curve and charging threshold: + if (confLoaded) + { + ApplyConf(); + } + } + catch (Exception ex) { - ApplyConf(); + Log.Fatal(Strings.GetString("svcException", ex)); + throw; } } diff --git a/YAMDCC.Updater/Program.cs b/YAMDCC.Updater/Program.cs index 1d0ca33..21a0d7a 100644 --- a/YAMDCC.Updater/Program.cs +++ b/YAMDCC.Updater/Program.cs @@ -84,9 +84,12 @@ private static int Main(string[] args) private static void InstallUpdate( string oldPath, string updatePath, string destPath, string confPath) { - ProgressDialog dlg = new("Installing YAMDCC update...", () => + ProgressDialog dlg = new(); + + dlg.DoWork = () => { // kill all running instances of YAMDCC + dlg.Caption = "Closing all YAMDCC instances..."; string[] names = [ "ConfigEditor", @@ -94,15 +97,18 @@ private static void InstallUpdate( // yamdccsvc is stopped in next section ]; - foreach (string name in names) + foreach (Process p in Process.GetProcesses()) { - foreach (Process process in Process.GetProcessesByName(name)) + foreach (string name in names) { - // try to close the main window. - // If that doesn't work, kill the process instead - if (!process.CloseMainWindow() || !process.WaitForExit(3000)) + if (p.ProcessName == name) { - process.Kill(); + // try to close the main window. + // If that doesn't work, kill the process instead + if (!p.CloseMainWindow() || !p.WaitForExit(3000)) + { + p.Kill(); + } } } } @@ -110,12 +116,17 @@ private static void InstallUpdate( bool svcRunning = Utils.ServiceRunning("yamdccsvc"); // stop the YAMDCC service if it's running - if (svcRunning && !Utils.StopService("yamdccsvc")) + if (svcRunning) { - Utils.ShowError("Failed to stop YAMDCC service!"); + dlg.Caption = "Stopping YAMDCC service..."; + if (!Utils.StopService("yamdccsvc")) + { + Utils.ShowError("Failed to stop YAMDCC service!"); + } } // delete the old YAMDCC installation + dlg.Caption = "Installing YAMDCC update..."; DirectoryInfo di = new(destPath); foreach (FileInfo fi in di.GetFiles()) { @@ -157,15 +168,17 @@ private static void InstallUpdate( // restart the YAMDCC service if it was running before the update if (svcRunning) { + dlg.Caption = "Starting YAMDCC service..."; Utils.StartService("yamdccsvc"); } // cleanup :) // (note: does not delete "Old" folder that we should be running from) + dlg.Caption = "Cleaning up..."; Directory.Delete(updatePath, true); return true; - }); + }; dlg.ShowDialog(); if (Utils.ShowInfo( diff --git a/YAMDCC.Updater/UpdateForm.cs b/YAMDCC.Updater/UpdateForm.cs index dbf3e38..9535af5 100644 --- a/YAMDCC.Updater/UpdateForm.cs +++ b/YAMDCC.Updater/UpdateForm.cs @@ -149,21 +149,33 @@ await Updater.DownloadUpdateAsync( // backup existing YAMDCC configs so they don't // get deleted when installing the new update - DirectoryInfo confDI = new(ConfPath); - foreach (FileInfo fi in confDI.GetFiles()) + try { - string name = Path.GetFileNameWithoutExtension(fi.Name); - - // config is a backup from previous update; ignore - if (name.Contains("-backup-")) + DirectoryInfo confDI = new(ConfPath); + foreach (FileInfo fi in confDI.GetFiles()) { - continue; + string name = Path.GetFileNameWithoutExtension(fi.Name); + + // config is a backup from previous update; ignore + if (name.Contains("-backup-")) + { + continue; + } + + // backup the config, just in case :) + string path = Path.Combine(fi.DirectoryName, name); + string date = $"{DateTime.Now:s}".Replace(':', '-'); + fi.MoveTo($"{path}-backup-{date}{fi.Extension}"); + } + } + catch (Exception ex) + { + // catch exception that occurs if there is no Configs + // folder in the same location as YAMDCC's executables + if (ex is not FileNotFoundException and not DirectoryNotFoundException) + { + throw; } - - // backup the config, just in case :) - string path = Path.Combine(fi.DirectoryName, name); - string date = $"{DateTime.Now:s}".Replace(':', '-'); - fi.MoveTo($"{path}-backup-{date}{fi.Extension}"); } // actually install the update