DisplayMagician/HeliosPlus.Shared/Helios.cs

137 lines
3.6 KiB
C#
Raw Normal View History

2017-02-26 19:23:31 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
namespace HeliosPlus.Shared
2017-02-26 19:23:31 +00:00
{
public class Helios
{
public static string Address
{
get
{
try
{
using (
var key =
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\HeliosPlus", false))
2017-02-26 19:23:31 +00:00
{
var executableAddress = key?.GetValue(@"ExecutableAddress", null) as string;
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (!string.IsNullOrWhiteSpace(executableAddress) && File.Exists(executableAddress))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return executableAddress;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
}
catch
{
// ignored
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return null;
}
}
2018-10-20 00:27:25 +00:00
public static bool IsInstalled
{
get => !string.IsNullOrWhiteSpace(Address);
}
2017-02-26 19:23:31 +00:00
2018-10-20 00:08:30 +00:00
// ReSharper disable once MethodTooLong
// ReSharper disable once TooManyArguments
2018-10-20 00:27:25 +00:00
public static void Open(
HeliosStartupAction action = HeliosStartupAction.None,
ProfileItem profile = null,
2018-10-20 00:27:25 +00:00
string programAddress = null,
bool asAdmin = false)
2017-02-26 19:23:31 +00:00
{
try
{
if (!IsInstalled)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
var args = new List<string> {$"-a {action}"};
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (profile != null)
2018-10-20 00:27:25 +00:00
{
args.Add($"-p \"{profile.Id}\"");
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
if (!string.IsNullOrWhiteSpace(programAddress))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
args.Add($"-e \"{programAddress}\"");
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
var processInfo = new ProcessStartInfo(Address, string.Join(" ", args))
{
UseShellExecute = true
};
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (asAdmin)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
processInfo.Verb = @"runas";
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
Process.Start(processInfo);
}
catch (Exception e)
{
// Check if operation canceled by user
if ((e as Win32Exception)?.NativeErrorCode == 1223)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
throw;
}
}
public static void OpenSteamGame(
2018-10-20 00:27:25 +00:00
HeliosStartupAction action = HeliosStartupAction.None,
ProfileItem profile = null,
2017-02-26 19:23:31 +00:00
uint steamAppId = 0)
{
try
{
if (!IsInstalled)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
var args = new List<string> {$@"-a {action}"};
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (profile != null)
2018-10-20 00:27:25 +00:00
{
args.Add($"-p \"{profile.Id}\"");
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
if (steamAppId > 0)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
args.Add($"-s \"{steamAppId}\"");
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
var processInfo = new ProcessStartInfo(Address, string.Join(" ", args))
{
UseShellExecute = true
};
Process.Start(processInfo);
}
catch (Exception e)
{
// Check if operation canceled by user
if ((e as Win32Exception)?.NativeErrorCode == 1223)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
throw;
}
}
}
}