Skip to content

Commit

Permalink
Refactors and minor bug fix
Browse files Browse the repository at this point in the history
- Fix persistent status messages potentially not being persistent

- Move some commonly-used file paths to their own class (Constants.cs)

Move some redundant code into their own functions
Sparronator9999 committed Dec 5, 2024
1 parent bfa0203 commit 1ad9a1f
Showing 13 changed files with 184 additions and 100 deletions.
1 change: 1 addition & 0 deletions YAMDCC.ECAccess/Driver.cs
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
using System.IO;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using YAMDCC.ECAccess.Win32;

namespace YAMDCC.ECAccess
{
2 changes: 1 addition & 1 deletion YAMDCC.ECAccess/Win32/AdvApi32.cs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
using System;
using System.Runtime.InteropServices;

namespace YAMDCC.ECAccess
namespace YAMDCC.ECAccess.Win32
{
/// <summary>
/// Wraps native Win32 functions from <c>advapi32.dll</c>.
2 changes: 1 addition & 1 deletion YAMDCC.ECAccess/Win32/Kernel32.cs
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
using System.Runtime.InteropServices;
using System.Threading;

namespace YAMDCC.ECAccess
namespace YAMDCC.ECAccess.Win32
{
/// <summary>
/// Wraps native Win32 functions from <c>kernel32.dll</c>.
25 changes: 25 additions & 0 deletions YAMDCC.GUI/Constants.cs
Original file line number Diff line number Diff line change
@@ -30,5 +30,30 @@ internal static class Constants
public static readonly string DataPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"Sparronator9999", "YAMDCC");

/// <summary>
/// The path where YAMDCC service logs are saved.
/// </summary>
/// <remarks>
/// (C:\ProgramData\Sparronator9999\YAMDCC\Logs on Windows)
/// </remarks>
public static readonly string LogPath = Path.Combine(DataPath, "Logs");

/// <summary>
/// The path where the currently applied YAMDCC config is saved.
/// </summary>
/// <remarks>
/// (C:\ProgramData\Sparronator9999\YAMDCC\CurrentConfig.xml on Windows)
/// </remarks>
public static readonly string CurrentConfigPath = Path.Combine(DataPath, "CurrentConfig.xml");

/// <summary>
/// The path where the path to the last saved YAMDCC config is saved.
/// </summary>
/// <remarks>
/// (C:\ProgramData\Sparronator9999\YAMDCC\CurrentConfig.xml on Windows)
/// </remarks>
public static readonly string LastConfigPath = Path.Combine(DataPath, "LastConfig");

}
}
39 changes: 15 additions & 24 deletions YAMDCC.GUI/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
98 changes: 46 additions & 52 deletions YAMDCC.GUI/Program.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 1 addition & 7 deletions YAMDCC.GUI/Status.cs
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 1ad9a1f

Please sign in to comment.