Skip to content

Commit

Permalink
Breaking changes to configs + various updates and bug fixes
Browse files Browse the repository at this point in the history
- Change RegConfs to have an off value as well as on value and update configs accordingly

- Use `sc` instead of `net` to start/stop services (speedup)

- Don't return false if service is already running (if trying to start) or stopped (if trying to stop)

- Remove "App" attribute from CommonConfig

- Fix CommonConfig being saved with incorrect Ver attribute, which caused it to be discarded on load

- Fix log level not being set by their respective menu

- Move various common code to Utils
  • Loading branch information
Sparronator9999 committed Jan 8, 2025
1 parent ec0d03f commit 3473aa1
Show file tree
Hide file tree
Showing 17 changed files with 181 additions and 119 deletions.
3 changes: 2 additions & 1 deletion Configs/Generic/MSI-10th-gen-or-newer-dualfan-nokeylight.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ If set to default fan curve, uses an alternative fan curve.</Desc>
<Desc>Sets fan mode to Advanced. Fixes fans not working at all with certain fan settings.
It is recommended you leave this enabled.</Desc>
<Reg>212</Reg>
<Value>77</Value>
<OnVal>77</OnVal>
<OffVal>13</OffVal>
</RegConf>
</RegConfs>
</YAMDCC_Config>
3 changes: 2 additions & 1 deletion Configs/Generic/MSI-10th-gen-or-newer-dualfan.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ If set to default fan curve, uses an alternative fan curve.</Desc>
<Desc>Sets fan mode to Advanced. Fixes fans not working at all with certain fan settings.
It is recommended you leave this enabled.</Desc>
<Reg>212</Reg>
<Value>77</Value>
<OnVal>77</OnVal>
<OffVal>13</OffVal>
</RegConf>
</RegConfs>
</YAMDCC_Config>
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ If set to default fan curve, uses an alternative fan curve.</Desc>
<Desc>Sets fan mode to Advanced. Fixes fans not working at all with certain fan settings.
It is recommended you leave this enabled.</Desc>
<Reg>212</Reg>
<Value>77</Value>
<OnVal>77</OnVal>
<OffVal>13</OffVal>
</RegConf>
</RegConfs>
</YAMDCC_Config>
3 changes: 2 additions & 1 deletion Configs/Generic/MSI-10th-gen-or-newer-singlefan.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ If set to default fan curve, uses an alternative fan curve.</Desc>
<Desc>Sets fan mode to Advanced. Fixes fans not working at all with certain fan settings.
It is recommended you leave this enabled.</Desc>
<Reg>212</Reg>
<Value>77</Value>
<OnVal>77</OnVal>
<OffVal>13</OffVal>
</RegConf>
</RegConfs>
</YAMDCC_Config>
3 changes: 2 additions & 1 deletion Configs/Generic/MSI-9th-gen-or-older-dualfan.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ If set to default fan curve, uses an alternative fan curve.</Desc>
<Desc>Sets fan mode to Advanced. Fixes fans not working at all with certain fan settings.
It is recommended you leave this enabled.</Desc>
<Reg>244</Reg>
<Value>141</Value>
<OnVal>141</OnVal>
<OffVal>13</OffVal>
</RegConf>
</RegConfs>
</YAMDCC_Config>
3 changes: 2 additions & 1 deletion Configs/Generic/MSI-9th-gen-or-older-singlefan.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ If set to default fan curve, uses an alternative fan curve.</Desc>
<Desc>Sets fan mode to Advanced. Fixes fans not working at all with certain fan settings.
It is recommended you leave this enabled.</Desc>
<Reg>244</Reg>
<Value>141</Value>
<OnVal>141</OnVal>
<OffVal>13</OffVal>
</RegConf>
</RegConfs>
</YAMDCC_Config>
3 changes: 2 additions & 1 deletion Configs/MSI-GF63-Thin-11SC.xml
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ If set to default fan curve, uses an alternative fan curve.</Desc>
<Desc>Sets fan mode to Advanced. Fixes fans not working at all with certain fan settings.
It is recommended you leave this enabled.</Desc>
<Reg>212</Reg>
<Value>77</Value>
<OnVal>77</OnVal>
<OffVal>13</OffVal>
</RegConf>
</RegConfs>
</YAMDCC_Config>
3 changes: 2 additions & 1 deletion Configs/MSI-Katana-GF66-12UG.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ If set to default fan curve, uses an alternative fan curve.</Desc>
<Desc>Set fan mode to Advanced. Fixes fans not working at all with certain fan settings.
It is recommended you leave this enabled.</Desc>
<Reg>212</Reg>
<Value>77</Value>
<OnVal>77</OnVal>
<OffVal>13</OffVal>
</RegConf>
</RegConfs>
</YAMDCC_Config>
3 changes: 2 additions & 1 deletion Configs/MSI-Modern-15-A5M.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ If set to default fan curve, uses an alternative fan curve.</Desc>
<Desc>Sets fan mode to Advanced. Fixes fans not working at all with certain fan settings.
It is recommended you leave this enabled.</Desc>
<Reg>244</Reg>
<Value>141</Value>
<OnVal>77</OnVal>
<OffVal>13</OffVal>
</RegConf>
</RegConfs>
</YAMDCC_Config>
8 changes: 1 addition & 7 deletions YAMDCC.Common/CommonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ public class CommonConfig
/// unless the config is newer or invalid.
/// </summary>
[XmlAttribute]
public int Ver { get; set; }

/// <summary>
/// The product this <see cref="CommonConfig"/> was made for.
/// </summary>
[XmlAttribute]
public string App { get; set; }
public int Ver { get; set; } = 1;

/// <summary>
/// How verbose logs should be.
Expand Down
4 changes: 2 additions & 2 deletions YAMDCC.Common/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public static class Paths
/// <summary>
/// The GitHub home page.
/// </summary>
public const string GitHubHome = "https://github.com";
public static readonly string GitHubHome = "https://github.com";

/// <summary>
/// The project repository.
/// </summary>
public const string ProjectRepo = "Sparronator9999/YAMDCC";
public static readonly string ProjectRepo = "Sparronator9999/YAMDCC";

/// <summary>
/// The URL to this project's GitHub page.
Expand Down
131 changes: 100 additions & 31 deletions YAMDCC.Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
// YAMDCC. If not, see <https://www.gnu.org/licenses/>.

using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.ServiceProcess;
Expand All @@ -29,6 +31,51 @@ namespace YAMDCC.Common;
/// </summary>
public static class Utils
{
/// <summary>
/// Shows an information dialog.
/// </summary>
/// <param name="message">
/// The message to show in the info dialog.
/// </param>
/// <param name="title">
/// The text to show in the title bar of the dialog.
/// </param>
/// <param name="buttons">
/// One of the <see cref="MessageBoxButtons"/> values
/// that specifies which buttons to display in the dialog.
/// </param>
/// <returns>
/// One of the <see cref="DialogResult"/> values.
/// </returns>
public static DialogResult ShowInfo(string message, string title,
MessageBoxButtons buttons = MessageBoxButtons.OK)
{
return MessageBox.Show(message, title, buttons, MessageBoxIcon.Asterisk);
}

/// <summary>
/// Shows a warning dialog.
/// </summary>
/// <param name="message">
/// The message to show in the warning dialog.
/// </param>
/// <param name="title">
/// The text to show in the title bar of the dialog.
/// </param>
/// <param name="button">
/// One of the <see cref="MessageBoxDefaultButton"/> values
/// that specifies the default button for the dialog.
/// </param>
/// <returns>
/// One of the <see cref="DialogResult"/> values.
/// </returns>
public static DialogResult ShowWarning(string message, string title,
MessageBoxDefaultButton button = MessageBoxDefaultButton.Button1)
{
return MessageBox.Show(message, title, MessageBoxButtons.YesNo,
MessageBoxIcon.Warning, button);
}

/// <summary>
/// Shows an error dialog.
/// </summary>
Expand All @@ -41,10 +88,10 @@ public static class Utils
public static DialogResult ShowError(string message)
{
return MessageBox.Show(message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}

public static string GetVerString()
public static string GetVerSuffix()
{
// format: X.Y.Z-SUFFIX[.W]+REVISION,
// where W is a beta/release candidate version if applicable
Expand All @@ -67,13 +114,21 @@ public static string GetVerString()
// suffix probably doesn't exist...
suffix = string.Empty;
}
return suffix.ToLowerInvariant();
}

public static string GetVerString()
{
// format: X.Y.Z-SUFFIX[.W]+REVISION,
// where W is a beta/release candidate version if applicable
string prodVer = Application.ProductVersion;

return suffix.ToLowerInvariant() switch
return GetVerSuffix() switch
{
// only show the version number (e.g. X.Y.Z):
"release" => prodVer.Remove(prodVer.IndexOf('-')),
"dev" => prodVer.Contains("+")
// probably a snapshot release (e.g. X.Y.Z-SNAPSHOT+REVISION);
// probably a development release (e.g. X.Y.Z-dev+REVISION);
// show shortened Git commit hash if it exists:
? prodVer.Remove(prodVer.IndexOf('+') + 8)
// Return the product version if not in expected format
Expand All @@ -83,7 +138,7 @@ public static string GetVerString()
// Beta releases should be in format X.Y.Z-beta.W+REVISION.
// Remove the revision (i.e. only show X.Y.Z-beta.W):
? prodVer.Remove(prodVer.IndexOf('+'))
// Return the product version if not in expected format
// Just return the product version if not in expected format
: prodVer,
};
}
Expand All @@ -104,22 +159,32 @@ public static string GetRevision()
: string.Empty;
}

public static Icon GetEntryAssemblyIcon()
{
return Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location);
}

/// <summary>
/// Gets whether the application is running with administrator privileges.
/// </summary>
/// <returns>
/// <see langword="true"/> if the application is running as
/// an administrator, otherwise <see langword="false"/>.
/// </returns>
public static bool IsAdmin()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
try
{
WindowsPrincipal principal = new(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
catch
{
return false;
}
finally
{
identity.Dispose();
}
}

/// <summary>
Expand All @@ -134,13 +199,13 @@ public static bool IsAdmin()
/// The path to the service executable.
/// </param>
/// <returns>
/// <c>true</c> if the service installation
/// was successful, otherwise <c>false</c>.
/// <see langword="true"/> if the service installation
/// was successful, otherwise <see langword="false"/>.
/// </returns>
public static bool InstallService(string svcExe)
{
string runtimePath = RuntimeEnvironment.GetRuntimeDirectory();
int exitCode = RunCmd($"{runtimePath}\\installutil.exe", $"{svcExe}.exe");
int exitCode = RunCmd($"{runtimePath}\\installutil.exe", $"\"{svcExe}.exe\"");
DeleteInstallUtilLogs();
return exitCode == 0;
}
Expand All @@ -153,13 +218,13 @@ public static bool InstallService(string svcExe)
/// The path to the service executable.
/// </param>
/// <returns>
/// <c>true</c> if the service uninstallation
/// was successful, otherwise <c>false</c>.
/// <see langword="true"/> if the service uninstallation
/// was successful, otherwise <see langword="false"/>.
/// </returns>
public static bool UninstallService(string svcExe)
{
string runtimePath = RuntimeEnvironment.GetRuntimeDirectory();
int exitCode = RunCmd($"{runtimePath}\\installutil.exe", $"/u {svcExe}.exe");
int exitCode = RunCmd($"{runtimePath}\\installutil.exe", $"/u \"{svcExe}.exe\"");
DeleteInstallUtilLogs();
return exitCode == 0;
}
Expand All @@ -172,12 +237,12 @@ public static bool UninstallService(string svcExe)
/// (NOT to be confused with its display name).
/// </param>
/// <returns>
/// <c>true</c> if the service started
/// successfully, otherwise <c>false</c>.
/// <see langword="true"/> if the service started successfully
/// (or is already running), otherwise <see langword="false"/>.
/// </returns>
public static bool StartService(string svcName)
{
return RunCmd("net.exe", $"start {svcName}") == 0;
return RunCmd("sc.exe", $"start {svcName}") is 0 or 1056;
}

/// <summary>
Expand All @@ -188,12 +253,12 @@ public static bool StartService(string svcName)
/// (NOT to be confused with its display name).
/// </param>
/// <returns>
/// <c>true</c> if the service was stopped
/// successfully, otherwise <c>false</c>.
/// <see langword="true"/> if the service was stopped successfully
/// (or is already stopped), otherwise <see langword="false"/>.
/// </returns>
public static bool StopService(string svcName)
{
return RunCmd("net.exe", $"stop {svcName}") == 0;
return RunCmd("sc.exe", $"stop {svcName}") is 0 or 1062;
}

/// <summary>
Expand All @@ -205,8 +270,8 @@ public static bool StopService(string svcName)
/// (NOT to be confused with its display name).
/// </param>
/// <returns>
/// <c>true</c> if the service was
/// found, otherwise <c>false</c>.
/// <see langword="true"/> if the service was
/// found, otherwise <see langword="false"/>
/// </returns>
public static bool ServiceExists(string svcName)
{
Expand All @@ -225,10 +290,10 @@ private static void DeleteInstallUtilLogs()
}
}

public static int RunCmd(string exe, string args, bool admin = true)
public static int RunCmd(string exe, string args, bool waitExit = true)
{
bool shellExecute = false;
if (admin && !IsAdmin())
if (!IsAdmin())
{
// if running unprivileged, we can't create an admin process
// directly, so use shell execute (creating new cmd window) instead
Expand All @@ -241,14 +306,18 @@ public static int RunCmd(string exe, string args, bool admin = true)
{
CreateNoWindow = true,
UseShellExecute = shellExecute,
Verb = admin ? "runas" : string.Empty,
Verb = "runas",
Arguments = args,
},
};

p.Start();
p.WaitForExit();
if (waitExit)
{
p.WaitForExit();
return p.ExitCode;
}

return p.ExitCode;
return 0;
}
}
12 changes: 10 additions & 2 deletions YAMDCC.Config/RegConf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ public sealed class RegConf
public byte Reg { get; set; }

/// <summary>
/// The value to write to the register.
/// The value to write to the register when
/// this <see cref="RegConf"/> is enabled.
/// </summary>
[XmlElement]
public byte Value { get; set; }
public byte OnVal { get; set; }

/// <summary>
/// The value to write to the register when
/// this <see cref="RegConf"/> is disabled.
/// </summary>
[XmlElement]
public byte OffVal { get; set; }
}
Loading

0 comments on commit 3473aa1

Please sign in to comment.