diff --git a/YAMDCC.ECAccess/Driver.cs b/YAMDCC.ECAccess/Driver.cs index ef30d44..ee07674 100644 --- a/YAMDCC.ECAccess/Driver.cs +++ b/YAMDCC.ECAccess/Driver.cs @@ -18,6 +18,7 @@ using System.IO; using System.Runtime.InteropServices; using System.Security.AccessControl; +using YAMDCC.ECAccess.Win32; namespace YAMDCC.ECAccess { diff --git a/YAMDCC.ECAccess/Win32/AdvApi32.cs b/YAMDCC.ECAccess/Win32/AdvApi32.cs index d5fe899..cf86461 100644 --- a/YAMDCC.ECAccess/Win32/AdvApi32.cs +++ b/YAMDCC.ECAccess/Win32/AdvApi32.cs @@ -17,7 +17,7 @@ using System; using System.Runtime.InteropServices; -namespace YAMDCC.ECAccess +namespace YAMDCC.ECAccess.Win32 { /// /// Wraps native Win32 functions from advapi32.dll. diff --git a/YAMDCC.ECAccess/Win32/Kernel32.cs b/YAMDCC.ECAccess/Win32/Kernel32.cs index c83e32e..f47f3e6 100644 --- a/YAMDCC.ECAccess/Win32/Kernel32.cs +++ b/YAMDCC.ECAccess/Win32/Kernel32.cs @@ -19,7 +19,7 @@ using System.Runtime.InteropServices; using System.Threading; -namespace YAMDCC.ECAccess +namespace YAMDCC.ECAccess.Win32 { /// /// Wraps native Win32 functions from kernel32.dll. diff --git a/YAMDCC.GUI/Constants.cs b/YAMDCC.GUI/Constants.cs index 575cfd5..81d38dc 100644 --- a/YAMDCC.GUI/Constants.cs +++ b/YAMDCC.GUI/Constants.cs @@ -30,5 +30,30 @@ internal static class Constants public static readonly string DataPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Sparronator9999", "YAMDCC"); + + /// + /// The path where YAMDCC service logs are saved. + /// + /// + /// (C:\ProgramData\Sparronator9999\YAMDCC\Logs on Windows) + /// + public static readonly string LogPath = Path.Combine(DataPath, "Logs"); + + /// + /// The path where the currently applied YAMDCC config is saved. + /// + /// + /// (C:\ProgramData\Sparronator9999\YAMDCC\CurrentConfig.xml on Windows) + /// + public static readonly string CurrentConfigPath = Path.Combine(DataPath, "CurrentConfig.xml"); + + /// + /// The path where the path to the last saved YAMDCC config is saved. + /// + /// + /// (C:\ProgramData\Sparronator9999\YAMDCC\CurrentConfig.xml on Windows) + /// + public static readonly string LastConfigPath = Path.Combine(DataPath, "LastConfig"); + } } diff --git a/YAMDCC.GUI/MainWindow.cs b/YAMDCC.GUI/MainWindow.cs index 6de55cb..2ce4dfa 100644 --- a/YAMDCC.GUI/MainWindow.cs +++ b/YAMDCC.GUI/MainWindow.cs @@ -18,7 +18,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Drawing; -using System.Globalization; using System.IO; using System.Reflection; using System.Text; @@ -196,14 +195,12 @@ private void MainWindow_Load(object sender, EventArgs e) } catch (Exception ex) { - MessageBox.Show(string.Format(CultureInfo.InvariantCulture, - Strings.GetString("svcErrorConnect"), ex), "Error", - MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("svcErrorConnect", ex)); Application.Exit(); return; } - LoadConf(Path.Combine(Constants.DataPath, "CurrentConfig.xml")); + LoadConf(Constants.CurrentConfigPath); if (Config is not null && Config.KeyLightConf is not null) { @@ -212,8 +209,7 @@ private void MainWindow_Load(object sender, EventArgs e) if (File.Exists(Path.Combine(Constants.DataPath, "ECToConfFail"))) { - MessageBox.Show(Strings.GetString("dlgECtoConfError", Path.Combine(Constants.DataPath, "Logs")), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("dlgECtoConfError", Constants.LogPath)); } else if (File.Exists(Path.Combine(Constants.DataPath, "ECToConfSuccess"))) { @@ -498,10 +494,9 @@ private void tsiStopSvc_Click(object sender, EventArgs e) IPCClient.Stop(); Close(); - if (!ServiceUtils.StopService("yamdccsvc")) + if (!Utils.StopService("yamdccsvc")) { - MessageBox.Show(Strings.GetString("dlgSvcStopError"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("dlgSvcStopError")); } } } @@ -522,9 +517,9 @@ private void tsiUninstall_Click(object sender, EventArgs e) // Apparently this fixes the YAMDCC service not uninstalling // when YAMDCC is launched by certain means - if (ServiceUtils.StopService("yamdccsvc")) + if (Utils.StopService("yamdccsvc")) { - if (ServiceUtils.UninstallService("yamdccsvc")) + if (Utils.UninstallService("yamdccsvc")) { // Only delete service data if the // service uninstalled successfully @@ -535,14 +530,12 @@ private void tsiUninstall_Click(object sender, EventArgs e) } else { - MessageBox.Show(Strings.GetString("dlgSvcUninstallError"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("dlgSvcUninstallError")); } } else { - MessageBox.Show(Strings.GetString("dlgSvcStopError"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("dlgSvcStopError")); } } } @@ -870,7 +863,7 @@ private void LoadConf(YAMDCC_Config config) private void ApplyConf() { // Save the updated config - Config.Save(Path.Combine(Constants.DataPath, "CurrentConfig.xml")); + Config.Save(Constants.CurrentConfigPath); // Tell the service to reload and apply the updated config SendServiceMessage(new ServiceCommand(Command.ApplyConfig, null)); @@ -896,13 +889,11 @@ private void RevertConf() { if (ex is FileNotFoundException) { - MessageBox.Show(Strings.GetString("dlgOldConfMissing"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("dlgOldConfMissing")); } else if (ex is InvalidConfigException or InvalidOperationException) { - MessageBox.Show(Strings.GetString("dlgOldConfInvalid"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("dlgOldConfInvalid")); } else { @@ -973,7 +964,7 @@ private void DelFanProfile() private static string GetLastConfPath() { - StreamReader sr = new(Path.Combine(Constants.DataPath, "LastConfig"), Encoding.UTF8); + StreamReader sr = new(Constants.LastConfigPath, Encoding.UTF8); try { string path = sr.ReadLine(); @@ -987,7 +978,7 @@ private static string GetLastConfPath() private static void SetLastConfPath(string path) { - StreamWriter sw = new(Path.Combine(Constants.DataPath, "LastConfig"), false, Encoding.UTF8); + StreamWriter sw = new(Constants.LastConfigPath, false, Encoding.UTF8); try { sw.WriteLine(path); @@ -1124,9 +1115,9 @@ private void UpdateStatus(StatusCode status, int data = 0) lblStatus.Invoke(() => lblStatus.Text += $" (x{AppStatus.RepeatCount + 1})"); } + tmrStatusReset.Stop(); if (!persist) { - tmrStatusReset.Stop(); tmrStatusReset.Start(); } } diff --git a/YAMDCC.GUI/Program.cs b/YAMDCC.GUI/Program.cs index dd47c06..c26ccd6 100644 --- a/YAMDCC.GUI/Program.cs +++ b/YAMDCC.GUI/Program.cs @@ -19,6 +19,7 @@ using System.IO; using System.Security.Principal; using System.ServiceProcess; +using System.Threading; using System.Windows.Forms; using YAMDCC.GUI.Dialogs; @@ -42,18 +43,15 @@ private static void Main() // Make sure the application data directory structure is set up // because apparently windows services don't know how to create // directories: - Directory.CreateDirectory(Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), - "Sparronator9999", "YAMDCC", "Logs")); + Directory.CreateDirectory(Constants.LogPath); if (!IsAdmin()) { - MessageBox.Show(Strings.GetString("dlgNoAdmin"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("dlgNoAdmin")); return; } - if (!ServiceUtils.ServiceExists("yamdccsvc")) + if (!Utils.ServiceExists("yamdccsvc")) { if (File.Exists("yamdccsvc.exe")) { @@ -63,31 +61,28 @@ private static void Main() MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { - if (ServiceUtils.InstallService("yamdccsvc")) + if (Utils.InstallService("yamdccsvc")) { - if (ServiceUtils.StartService("yamdccsvc")) + if (Utils.StartService("yamdccsvc")) { // Start the program when the service finishes starting: Start(); } else { - MessageBox.Show(Strings.GetString("svcErrorCrash"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("svcErrorCrash")); } } else { - MessageBox.Show(Strings.GetString("svcInstallFail"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("svcInstallFail")); } } return; } else { - MessageBox.Show(Strings.GetString("svcNotFound"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("svcNotFound")); return; } } @@ -106,10 +101,9 @@ private static void Main() "Service not running", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { - if (!ServiceUtils.StartService("yamdccsvc")) + if (!Utils.StartService("yamdccsvc")) { - MessageBox.Show(Strings.GetString("svcErrorCrash"), - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("svcErrorCrash")); return; } } @@ -121,9 +115,7 @@ private static void Main() } catch (Exception ex) { - MessageBox.Show( - Strings.GetString("svcErrorStart", ex), "Error", - MessageBoxButtons.OK, MessageBoxIcon.Error); + Utils.ShowError(Strings.GetString("svcErrorStart", ex)); return; } finally @@ -142,47 +134,33 @@ private static void Start() { StreamReader sr = new(Path.Combine(Constants.DataPath, "ECToConfPending")); if (int.TryParse(sr.ReadToEnd(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)) - rebootFlag = value; - sr.Close(); - - if (rebootFlag == 1) { - if (MessageBox.Show(Strings.GetString("dlgECtoConfRebootPending"), - "Reboot pending", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, - MessageBoxDefaultButton.Button2) == DialogResult.Yes) - { - try - { - File.Delete(Path.Combine(Constants.DataPath, "ECToConfPending")); - } - catch (FileNotFoundException) { } - catch (DirectoryNotFoundException) { } - } - else - { - return; - } + rebootFlag = value; } + sr.Close(); } catch (FileNotFoundException) { } catch (DirectoryNotFoundException) { } - Application.Run(new MainWindow()); - } - #region Global exception handlers - private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) - { - if (e.ExceptionObject is Exception ex) + if (rebootFlag == 1) { - CrashDialog dlg = new(ex); - dlg.ShowDialog(); + if (MessageBox.Show(Strings.GetString("dlgECtoConfRebootPending"), + "Reboot pending", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, + MessageBoxDefaultButton.Button2) == DialogResult.Yes) + { + try + { + File.Delete(Path.Combine(Constants.DataPath, "ECToConfPending")); + } + catch (DirectoryNotFoundException) { } + } + else + { + return; + } } - } - private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) - { - CrashDialog dlg = new(e.Exception); - dlg.ShowDialog(); + Application.Run(new MainWindow()); } private static bool IsAdmin() @@ -202,6 +180,22 @@ private static bool IsAdmin() identity.Dispose(); } } + + #region Global exception handlers + private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + if (e.ExceptionObject is Exception ex) + { + CrashDialog dlg = new(ex); + dlg.ShowDialog(); + } + } + + private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) + { + CrashDialog dlg = new(e.Exception); + dlg.ShowDialog(); + } #endregion } } diff --git a/YAMDCC.GUI/Status.cs b/YAMDCC.GUI/Status.cs index d7f41cb..d738209 100644 --- a/YAMDCC.GUI/Status.cs +++ b/YAMDCC.GUI/Status.cs @@ -16,7 +16,7 @@ namespace YAMDCC.GUI { - internal class Status + internal sealed class Status { internal StatusCode Code; internal int RepeatCount; @@ -26,12 +26,6 @@ internal Status() Code = StatusCode.None; RepeatCount = 0; } - - internal Status(StatusCode code, int repeatCount) - { - Code = code; - RepeatCount = repeatCount; - } } internal enum StatusCode diff --git a/YAMDCC.GUI/ServiceUtils.cs b/YAMDCC.GUI/Utils.cs similarity index 90% rename from YAMDCC.GUI/ServiceUtils.cs rename to YAMDCC.GUI/Utils.cs index f6ca433..712b433 100644 --- a/YAMDCC.GUI/ServiceUtils.cs +++ b/YAMDCC.GUI/Utils.cs @@ -19,14 +19,30 @@ using System.Linq; using System.Runtime.InteropServices; using System.ServiceProcess; +using System.Windows.Forms; namespace YAMDCC.GUI { /// /// A collection of miscellaneous useful utilities /// - internal static class ServiceUtils + internal static class Utils { + /// + /// Shows an error dialog. + /// + /// + /// The message to show in the error dialog. + /// + /// + /// One of the values. + /// + internal static DialogResult ShowError(string message) + { + return MessageBox.Show(message, "Error", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + /// /// Installs the specified .NET Framework /// service to the local computer. diff --git a/YAMDCC.Service/Constants.cs b/YAMDCC.Service/Constants.cs new file mode 100644 index 0000000..d7c7828 --- /dev/null +++ b/YAMDCC.Service/Constants.cs @@ -0,0 +1,52 @@ +// This file is part of YAMDCC (Yet Another MSI Dragon Center Clone). +// Copyright © Sparronator9999 2023-2024. +// +// YAMDCC is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) +// any later version. +// +// YAMDCC is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// YAMDCC. If not, see . + +using System; +using System.IO; + +namespace YAMDCC.Service +{ + internal static class Constants + { + /// + /// The path where program data is stored. + /// + /// + /// (C:\ProgramData\Sparronator9999\YAMDCC on Windows) + /// + public static readonly string DataPath = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), + "Sparronator9999", "YAMDCC"); + + /// + /// The path where YAMDCC service logs are saved. + /// + /// + /// (C:\ProgramData\Sparronator9999\YAMDCC\Logs on Windows) + /// + public static readonly string LogPath = Path.Combine(DataPath, "Logs"); + + /// + /// The path where the currently applied YAMDCC config is saved. + /// + /// + /// (C:\ProgramData\Sparronator9999\YAMDCC\CurrentConfig.xml on Windows) + /// + public static readonly string CurrentConfigPath = Path.Combine(DataPath, "CurrentConfig.xml"); + + public static readonly string ECtoConfPendingPath = Path.Combine(DataPath, "ECToConfPending"); + } +} diff --git a/YAMDCC.Service/FanControlService.cs b/YAMDCC.Service/FanControlService.cs index e7737a4..8875139 100644 --- a/YAMDCC.Service/FanControlService.cs +++ b/YAMDCC.Service/FanControlService.cs @@ -32,10 +32,6 @@ internal sealed class FanControlService : ServiceBase { #region Fields - private static readonly string DataPath = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), - "Sparronator9999", "YAMDCC"); - /// /// The currently loaded MSI Fan Control config. /// @@ -123,7 +119,7 @@ protected override void OnStart(string[] args) int rebootFlag = -1; try { - StreamReader sr = new(Path.Combine(DataPath, "ECToConfPending")); + StreamReader sr = new(Constants.ECtoConfPendingPath); if (int.TryParse(sr.ReadToEnd(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)) { rebootFlag = value; @@ -133,7 +129,7 @@ protected override void OnStart(string[] args) if (rebootFlag == 0) { FanCurveECToConf(); - File.Delete(Path.Combine(DataPath, "ECToConfPending")); + File.Delete(Constants.ECtoConfPendingPath); } } catch (FileNotFoundException) { } @@ -159,7 +155,7 @@ protected override void OnShutdown() int rebootFlag = -1; try { - StreamReader sr = new(Path.Combine(DataPath, "ECToConfPending")); + StreamReader sr = new(Constants.ECtoConfPendingPath); if (int.TryParse(sr.ReadToEnd(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)) { rebootFlag = value; @@ -168,7 +164,7 @@ protected override void OnShutdown() if (rebootFlag == 1) { - StreamWriter sw = new(Path.Combine(DataPath, "ECToConfPending")); + StreamWriter sw = new(Constants.ECtoConfPendingPath); try { sw.Write(0); @@ -354,7 +350,7 @@ private void LoadConf() { Log.Info(Strings.GetString("cfgLoading")); - string confPath = Path.Combine(DataPath, "CurrentConfig.xml"); + string confPath = Constants.CurrentConfigPath; try { @@ -790,14 +786,14 @@ private void FanCurveECToConf() } Log.Debug("Saving config..."); - Config.Save(Path.Combine(DataPath, "CurrentConfig.xml")); + Config.Save(Constants.CurrentConfigPath); - FileStream fs = File.Create(Path.Combine(DataPath, "ECToConfSuccess")); + FileStream fs = File.Create(Path.Combine(Constants.DataPath, "ECToConfSuccess")); fs.Close(); } catch { - FileStream fs = File.Create(Path.Combine(DataPath, "ECToConfFail")); + FileStream fs = File.Create(Path.Combine(Constants.DataPath, "ECToConfFail")); fs.Close(); } } diff --git a/YAMDCC.Service/Program.cs b/YAMDCC.Service/Program.cs index 8d8c7e4..6adca2c 100644 --- a/YAMDCC.Service/Program.cs +++ b/YAMDCC.Service/Program.cs @@ -16,6 +16,7 @@ using System; using System.ServiceProcess; +using System.Windows.Forms; using YAMDCC.Logs; namespace YAMDCC.Service @@ -37,7 +38,15 @@ internal static class Program private static void Main() { AppDomain.CurrentDomain.UnhandledException += LogUnhandledException; - ServiceBase.Run(new FanControlService(Log)); + if (Environment.UserInteractive) + { + MessageBox.Show(Strings.GetString("errDirectRun"), + "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + else + { + ServiceBase.Run(new FanControlService(Log)); + } } private static void LogUnhandledException(object sender, UnhandledExceptionEventArgs e) diff --git a/YAMDCC.Service/Strings.resx b/YAMDCC.Service/Strings.resx index 1f2b3fb..13c20fe 100644 --- a/YAMDCC.Service/Strings.resx +++ b/YAMDCC.Service/Strings.resx @@ -237,4 +237,9 @@ Args: {1} IPC error occurred (connection to ID: {0}): {1} - \ No newline at end of file + + This program is not intended to be run directly. + +Please run 'YAMDCC.exe' instead. + + diff --git a/YAMDCC.Service/YAMDCC.Service.csproj b/YAMDCC.Service/YAMDCC.Service.csproj index f7cb564..990bba0 100644 --- a/YAMDCC.Service/YAMDCC.Service.csproj +++ b/YAMDCC.Service/YAMDCC.Service.csproj @@ -28,6 +28,7 @@ +