using DiscordRPC.Logging;
using System;
using System.Diagnostics;
namespace DiscordRPC.Registry
{
internal class UriSchemeRegister
{
///
/// The ID of the Discord App to register
///
public string ApplicationID { get; set; }
///
/// Optional Steam App ID to register. If given a value, then the game will launch through steam instead of Discord.
///
public string SteamAppID { get; set; }
///
/// Is this register using steam?
///
public bool UsingSteamApp { get { return !string.IsNullOrEmpty(SteamAppID) && SteamAppID != ""; } }
///
/// The full executable path of the application.
///
public string ExecutablePath { get; set; }
private ILogger _logger;
public UriSchemeRegister(ILogger logger, string applicationID, string steamAppID = null, string executable = null)
{
_logger = logger;
ApplicationID = applicationID.Trim();
SteamAppID = steamAppID != null ? steamAppID.Trim() : null;
ExecutablePath = executable ?? GetApplicationLocation();
}
///
/// Registers the URI scheme, using the correct creator for the correct platform
///
public bool RegisterUriScheme()
{
//Get the creator
IUriSchemeCreator creator = null;
switch(Environment.OSVersion.Platform)
{
case PlatformID.Win32Windows:
case PlatformID.Win32S:
case PlatformID.Win32NT:
case PlatformID.WinCE:
_logger.Trace("Creating Windows Scheme Creator");
creator = new WindowsUriSchemeCreator(_logger);
break;
case PlatformID.Unix:
_logger.Trace("Creating Unix Scheme Creator");
creator = new UnixUriSchemeCreator(_logger);
break;
case PlatformID.MacOSX:
_logger.Trace("Creating MacOSX Scheme Creator");
creator = new MacUriSchemeCreator(_logger);
break;
default:
_logger.Error("Unkown Platform: " + Environment.OSVersion.Platform);
throw new PlatformNotSupportedException("Platform does not support registration.");
}
//Regiser the app
if (creator.RegisterUriScheme(this))
{
_logger.Info("URI scheme registered.");
return true;
}
return false;
}
///
/// Gets the FileName for the currently executing application
///
///
public static string GetApplicationLocation()
{
return Process.GetCurrentProcess().MainModule.FileName;
}
}
}