Radeon Reset Bug fix service
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RadeonResetBugFix/RadeonResetBugFixService/Program.cs

155 lines
5.7 KiB

namespace RadeonResetBugFixService
{
using System;
using System.ServiceProcess;
using Tasks.ComplexTasks;
using ThirdParty.ServiceHelpers;
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
public static int Main(string[] args)
{
5 years ago
if (args == null)
{
throw new ArgumentNullException(nameof(args));
}
if (!EnvironmentHelper.IsVistaOrNewer())
{
Console.Error.WriteLine("This program only runs on Windows Vista or newer");
return -1;
}
if (EnvironmentHelper.IsConsoleVisibleOnWindows())
{
if (!EnvironmentHelper.HasAdministratorPrivileges())
{
Console.Error.WriteLine("Access Denied.");
4 years ago
Console.Error.WriteLine("Administrator permissions are needed to use this tool.");
Console.Error.WriteLine("Run the command again from an administrator command prompt.");
return 740; // ERROR_ELEVATION_REQUIRED
}
MainConsole(args);
return 0;
}
else
{
return MainService();
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "Windows service initialization")]
private static int MainService()
{
ServiceBase.Run(new RadeonResetBugFixService());
return 0;
}
private static void MainConsole(string[] args)
{
Console.WriteLine($"Build date: {EnvironmentHelper.GetServiceBuildDate()}");
4 years ago
var command = args.Length == 1 ? args[0] : string.Empty;
5 years ago
if (command.Equals("install", StringComparison.OrdinalIgnoreCase)) {
DoInstall();
}
else if (command.Equals("uninstall", StringComparison.OrdinalIgnoreCase))
{
DoUninstall();
}
4 years ago
else if (command.Equals("reinstall", StringComparison.OrdinalIgnoreCase))
{
DoReinstall();
}
5 years ago
else if (command.Equals("startup", StringComparison.OrdinalIgnoreCase))
{
5 years ago
DoStartup();
}
else if (command.Equals("shutdown", StringComparison.OrdinalIgnoreCase))
{
DoShutdown();
}
else if (command.Equals("diagnose", StringComparison.OrdinalIgnoreCase))
{
DoDiagnose();
}
4 years ago
else
{
ShowHelp();
}
}
private static void ShowHelp()
{
var exeName = Environment.GetCommandLineArgs()[0];
Console.WriteLine("Usage:");
Console.WriteLine($"\t{exeName} install");
Console.WriteLine("\t\tInstalls service");
Console.WriteLine($"\t{exeName} uninstall");
Console.WriteLine("\t\tUninstalls service");
4 years ago
Console.WriteLine($"\t{exeName} reinstall");
Console.WriteLine("\t\tReinstalls service (might be useful for some upgrades)");
Console.WriteLine($"\t{exeName} startup");
4 years ago
Console.WriteLine("\t\tPerforms startup sequence (development command, does not affect services)");
Console.WriteLine($"\t{exeName} shutdown");
4 years ago
Console.WriteLine("\t\tPerforms shutdown sequence (development command, does not affect services)");
Console.WriteLine($"\t{exeName} diagnose");
Console.WriteLine("\t\tPerforms diagnose (see result file in logs folder)");
}
private static void DoInstall()
{
Console.WriteLine("Setting registry values...");
Console.WriteLine("Installing service...");
4 years ago
ServiceHelpers.InstallService(Constants.ServiceName, typeof(RadeonResetBugFixService));
Console.WriteLine("Starting service...");
4 years ago
ServiceHelpers.StartService(Constants.ServiceName);
Console.WriteLine("Should restart service now; stopping service...");
ServiceHelpers.StopService(Constants.ServiceName);
Console.WriteLine("Starting service...");
ServiceHelpers.StartService(Constants.ServiceName);
Console.WriteLine("Service started");
}
private static void DoUninstall()
{
Console.WriteLine("Stopping service...");
4 years ago
ServiceHelpers.StopService(Constants.ServiceName);
Console.WriteLine("Uninstalling service...");
4 years ago
ServiceHelpers.UninstallService(Constants.ServiceName, typeof(RadeonResetBugFixService));
Console.WriteLine("Uninstalled");
}
4 years ago
private static void DoReinstall()
{
Console.WriteLine("Attempting to uninstall...");
DoUninstall();
Console.WriteLine("Attempting to install...");
DoInstall();
}
private static void DoStartup() => new MainHandler().HandleEntryPoint(
"Program.DoStartup",
(logger) => TasksProcessor.ProcessTask(logger, new StartupTask())
);
private static void DoShutdown() => new MainHandler().HandleEntryPoint(
"Program.DoShutdown",
(logger) => TasksProcessor.ProcessTask(logger, new ShutdownTask())
);
private static void DoDiagnose() => new MainHandler().HandleEntryPoint(
"Program.DoDiagnose",
(logger) => TasksProcessor.ProcessTask(logger, new DiagnoseTask())
);
}
}