DisplayMagician/HeliosDisplayManagement/UIForms/ShortcutForm.cs

676 lines
23 KiB
C#
Raw Normal View History

2017-02-26 19:23:31 +00:00
using System;
using System.Collections.Generic;
using System.Drawing.IconLib;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HeliosDisplayManagement.Resources;
using HeliosDisplayManagement.Shared;
using HeliosDisplayManagement.Steam;
using NvAPIWrapper.Native.GPU;
2017-02-26 19:23:31 +00:00
namespace HeliosDisplayManagement.UIForms
{
public partial class ShortcutForm : Form
{
public ShortcutForm()
{
InitializeComponent();
}
public ShortcutForm(Profile profile) : this()
{
Profile = profile;
}
public static string IconCache
{
get => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Assembly.GetExecutingAssembly().GetName().Name, @"IconCache");
}
public string ProcessNameToMonitor
2017-02-26 19:23:31 +00:00
{
get
{
if (rb_switch_temp.Checked && rb_standalone.Checked) {
if (rb_wait_executable.Checked)
{
return txt_process_name.Text;
}
}
return string.Empty;
}
2017-08-07 16:38:48 +00:00
set
{
if (File.Exists(txt_executable.Text))
{
txt_process_name.Text = value;
rb_wait_executable.Checked = true;
}
2017-08-07 16:38:48 +00:00
}
2017-02-26 19:23:31 +00:00
}
public Profile Profile
{
get => dv_profile.Profile;
set => dv_profile.Profile = value;
}
public string ExecutableNameAndPath
2017-02-26 19:23:31 +00:00
{
get => rb_switch_temp.Checked && rb_launcher.Checked ? txt_executable.Text : string.Empty;
2017-02-26 19:23:31 +00:00
set
{
if (File.Exists(txt_executable.Text))
2017-02-26 19:23:31 +00:00
{
rb_switch_temp.Checked = true;
rb_launcher.Checked = true;
2017-02-26 19:23:31 +00:00
txt_executable.Text = value;
}
}
}
public uint ExecutableTimeout
2018-10-20 00:27:25 +00:00
{
get
{
if (rb_wait_executable.Checked)
{
return (uint)nud_timeout_executable.Value;
}
return 0;
}
set
{
nud_timeout_executable.Value = value;
}
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
public string ExecutableArguments
2017-02-26 19:23:31 +00:00
{
get => cb_args_executable.Checked ? txt_args_executable.Text : string.Empty;
2017-02-26 19:23:31 +00:00
set
{
txt_args_executable.Text = value;
cb_args_executable.Checked = true;
2017-02-26 19:23:31 +00:00
}
}
public uint GameAppId
2017-08-07 16:38:48 +00:00
{
get => rb_switch_temp.Checked && rb_launcher.Checked ? (uint) nud_game_appid.Value : 0;
set
{
rb_switch_temp.Checked = true;
rb_launcher.Checked = true;
nud_game_appid.Value = value;
}
2017-08-07 16:38:48 +00:00
}
public string GameName
2017-02-26 19:23:31 +00:00
{
get => rb_switch_temp.Checked && rb_launcher.Checked ? txt_game_name.Text : string.Empty;
2017-02-26 19:23:31 +00:00
set
{
rb_switch_temp.Checked = true;
rb_launcher.Checked = true;
txt_game_name.Text = value;
2017-02-26 19:23:31 +00:00
}
}
public SupportedGameLibrary GameLibrary
2017-02-26 19:23:31 +00:00
{
get => rb_switch_temp.Checked && rb_launcher.Checked ? (SupportedGameLibrary) nud_game_appid.Value : SupportedGameLibrary.Unknown;
set
2017-02-26 19:23:31 +00:00
{
rb_switch_temp.Checked = true;
rb_launcher.Checked = true;
switch (value)
2018-10-20 00:27:25 +00:00
{
case SupportedGameLibrary.Steam:
break;
case SupportedGameLibrary.Uplay:
break;
2018-10-20 00:27:25 +00:00
}
// TODO - If SupportedGameLibrary.Unknown; then we need to show an error message.
}
}
2018-10-20 00:27:25 +00:00
public uint GameTimeout
{
get
{
if (rb_switch_temp.Checked && rb_launcher.Checked)
2018-10-20 00:27:25 +00:00
{
return (uint)nud_timeout_game.Value;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
return 0;
}
set
{
nud_timeout_game.Value = value;
2017-02-26 19:23:31 +00:00
}
}
public string GameArguments
{
get => cb_args_game.Checked ? txt_args_game.Text : string.Empty;
set
{
txt_args_game.Text = value;
cb_args_game.Checked = true;
}
}
2017-02-26 19:23:31 +00:00
private void btn_app_executable_Click(object sender, EventArgs e)
{
if (dialog_open.ShowDialog(this) == DialogResult.OK)
2018-10-20 00:27:25 +00:00
{
if (File.Exists(dialog_open.FileName) && Path.GetExtension(dialog_open.FileName) == @".exe")
2017-02-26 19:23:31 +00:00
{
txt_executable.Text = dialog_open.FileName;
dialog_open.FileName = string.Empty;
}
else
{
MessageBox.Show(
Language.Selected_file_is_not_a_valid_executable_file,
Language.Executable,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
2017-08-07 16:38:48 +00:00
private void btn_save_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.None;
2018-10-20 00:27:25 +00:00
2017-08-07 16:38:48 +00:00
try
{
if (dialog_save.ShowDialog(this) == DialogResult.OK)
{
if (CreateShortcut(dialog_save.FileName))
2018-10-20 00:27:25 +00:00
{
2017-08-07 16:38:48 +00:00
MessageBox.Show(
Language.Shortcut_place_successfully,
Language.Shortcut,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
2018-10-20 00:27:25 +00:00
}
2017-08-07 16:38:48 +00:00
else
2018-10-20 00:27:25 +00:00
{
2017-08-07 16:38:48 +00:00
MessageBox.Show(
Language.Failed_to_create_the_shortcut_Unexpected_exception_occurred,
Language.Shortcut,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
2018-10-20 00:27:25 +00:00
}
2017-08-07 16:38:48 +00:00
dialog_save.FileName = string.Empty;
DialogResult = DialogResult.OK;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Language.Shortcut, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
2017-02-26 19:23:31 +00:00
// ReSharper disable once FunctionComplexityOverflow
2017-08-07 16:38:48 +00:00
// ReSharper disable once CyclomaticComplexity
2017-02-26 19:23:31 +00:00
private bool CreateShortcut(string fileName)
{
var programName = Path.GetFileNameWithoutExtension(txt_executable.Text);
var description = string.Empty;
var icon = string.Empty;
2017-02-26 19:23:31 +00:00
var args = new List<string>
{
// Add the SwitchProfile command as the first argument to start to switch to another profile
$"{HeliosStartupAction.SwitchProfile}",
// Add the Profile Name as the second argument (use that rather than ID - though ID still will work!)
$"--profile \"{dv_profile.Profile.Name}\""
2017-02-26 19:23:31 +00:00
};
2018-10-20 00:27:25 +00:00
if (!Directory.Exists(IconCache))
2018-10-20 00:27:25 +00:00
{
try
{
Directory.CreateDirectory(IconCache);
}
catch
{
// ignored
}
2018-10-20 00:27:25 +00:00
}
// Only add the rest of the options if the temporary switch radio button is set
if (rb_switch_temp.Checked)
2017-02-26 19:23:31 +00:00
{
// Only add this set of options if the standalone programme radio button is set
2017-02-26 19:23:31 +00:00
if (rb_standalone.Checked)
{
// Doublecheck the Executable text field is filled in
2017-02-26 19:23:31 +00:00
if (string.IsNullOrWhiteSpace(txt_executable.Text))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
throw new Exception(Language.Executable_address_can_not_be_empty);
2018-10-20 00:27:25 +00:00
}
// Doublecheck the Executable text field is a path to a real file
2017-02-26 19:23:31 +00:00
if (!File.Exists(txt_executable.Text))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
throw new Exception(Language.Executable_file_not_found);
2018-10-20 00:27:25 +00:00
}
// Add the executable command and the executable name to the shortcut arguments
args.Add($"execute \"{txt_executable.Text.Trim()}\"");
2018-10-20 00:27:25 +00:00
// Check that the wait for executable radiobutton is on
if (rb_wait_executable.Checked)
2017-02-26 19:23:31 +00:00
{
// Doublecheck the process name has text in it
if (!string.IsNullOrWhiteSpace(txt_process_name.Text))
{
// Add the waitfor argument and the process name to the shortcut arguments
args.Add($"--waitfor \"{txt_process_name.Text.Trim()}\"");
}
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
// Add the timeout argument and the timeout duration in seconds to the shortcut arguments
args.Add($"--timeout {(int)nud_timeout_executable.Value}");
if (cb_args_executable.Checked && !string.IsNullOrWhiteSpace(txt_args_executable.Text))
{
args.Add($"--arguments \"{txt_args_executable.Text.Trim()}\"");
}
// Prepare text for the shortcut description field
2017-02-26 19:23:31 +00:00
description = string.Format(Language.Executing_application_with_profile, programName, Profile.Name);
2018-10-20 00:27:25 +00:00
// Grab an icon for the selected executable
2017-02-26 19:23:31 +00:00
try
{
icon = Path.Combine(IconCache, Guid.NewGuid() + ".ico");
new ProfileIcon(Profile).ToIconOverly(txt_executable.Text)
.Save(icon, MultiIconFormat.ICO);
}
2017-08-07 16:38:48 +00:00
catch (Exception)
2017-02-26 19:23:31 +00:00
{
icon = $"{txt_executable.Text.Trim()},0";
}
}
// Only add the rest of the options if the temporary switch radio button is set
// and if the game launching radio button is set
else if (rb_launcher.Checked)
2017-02-26 19:23:31 +00:00
{
// TODO need to make this work so at least one game library is installed
// i.e. if (!SteamGame.SteamInstalled && !UplayGame.UplayInstalled )
2017-02-26 19:23:31 +00:00
if (!SteamGame.SteamInstalled)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
throw new Exception(Language.Steam_is_not_installed);
2018-10-20 00:27:25 +00:00
}
// TODO - Add in Uplay game as well depending on which one was requested
// Add the Steam Game ID to the shortcut arguments
var steamGame = new SteamGame((uint) nud_game_appid.Value);
args.Add($"--steam {(int) nud_game_appid.Value}");
// Add the game timeout argument and the timeout duration in seconds to the shortcut arguments
args.Add($"--timeout {(int) nud_timeout_game.Value}");
if (cb_args_game.Checked && !string.IsNullOrWhiteSpace(txt_args_game.Text))
{
args.Add($"--arguments \"{txt_args_game.Text.Trim()}\"");
}
// Prepare text for the shortcut description field
2017-02-26 19:23:31 +00:00
description = string.Format(Language.Executing_application_with_profile, steamGame.Name,
Profile.Name);
var steamIcon = steamGame.GetIcon().Result;
2018-10-20 00:27:25 +00:00
// Grab an icon for the selected game
2017-02-26 19:23:31 +00:00
if (!string.IsNullOrWhiteSpace(steamIcon))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
try
{
icon = Path.Combine(IconCache, Guid.NewGuid() + ".ico");
new ProfileIcon(Profile).ToIconOverly(steamIcon)
.Save(icon, MultiIconFormat.ICO);
}
2017-08-07 16:38:48 +00:00
catch (Exception)
2017-02-26 19:23:31 +00:00
{
icon = steamIcon;
}
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
else
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
icon = $"{SteamGame.SteamAddress},0";
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
}
// Only add the rest of the options if the permanent switch radio button is set
2017-02-26 19:23:31 +00:00
else
{
// Prepare text for the shortcut description field
2017-02-26 19:23:31 +00:00
description = string.Format(Language.Switching_display_profile_to_profile, Profile.Name);
2018-10-20 00:27:25 +00:00
// Grab an icon for the selected profile
2017-02-26 19:23:31 +00:00
try
{
icon = Path.Combine(IconCache, Guid.NewGuid() + ".ico");
new ProfileIcon(Profile).ToIcon().Save(icon, MultiIconFormat.ICO);
}
catch
{
icon = string.Empty;
}
}
// TODO - Make this semi-automatic if its a game to be launched. Maybe still show
// a save prompt, but suggest the name of the game extracted from the game launcher
// or the executable itself.
// Now we are ready to create a shortcut based on the filename the user gave us
2017-02-26 19:23:31 +00:00
fileName = Path.ChangeExtension(fileName, @"lnk");
2018-10-20 00:27:25 +00:00
// If the user supplied a file
2017-02-26 19:23:31 +00:00
if (fileName != null)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
try
{
// Remove the old file to replace it
if (File.Exists(fileName))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
File.Delete(fileName);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
// Actually create the shortcut!
2017-02-26 19:23:31 +00:00
var wshShellType = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8"));
dynamic wshShell = Activator.CreateInstance(wshShellType);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
try
{
var shortcut = wshShell.CreateShortcut(fileName);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
try
{
shortcut.TargetPath = Application.ExecutablePath;
shortcut.Arguments = string.Join(" ", args);
shortcut.Description = description;
shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath) ??
string.Empty;
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (!string.IsNullOrWhiteSpace(icon))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
shortcut.IconLocation = icon;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
shortcut.Save();
}
finally
{
Marshal.FinalReleaseComObject(shortcut);
}
}
finally
{
Marshal.FinalReleaseComObject(wshShell);
}
}
catch
{
// Clean up a failed attempt
if (File.Exists(fileName))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
File.Delete(fileName);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
}
// Return a status on how it went
// true if it was a success or false if it was not
2018-10-20 00:27:25 +00:00
return fileName != null && File.Exists(fileName);
2017-02-26 19:23:31 +00:00
}
private void nud_game_appid_ValueChanged(object sender, EventArgs e)
2017-02-26 19:23:31 +00:00
{
//lbl_steamname.Text = new SteamGame((uint) nud_game_appid.Value).ToString();
2017-08-07 16:38:48 +00:00
}
/*private void nud_steamapps_Click(object sender, EventArgs e)
2017-08-07 16:38:48 +00:00
{
var steamGamesForm = new SteamGamesForm();
2018-10-20 00:27:25 +00:00
if (steamGamesForm.ShowDialog(this) == DialogResult.OK && steamGamesForm.SteamGame != null)
{
nud_game_appid.Value = steamGamesForm.SteamGame.AppId;
}
}*/
/* private void rb_wait_process_CheckedChanged(object sender, EventArgs e)
{
if (rb_wait_process.Checked)
{
// Enable the Process Name Text field
txt_process_name.Enabled = true;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
*/
2017-02-26 19:23:31 +00:00
private void txt_executable_TextChanged(object sender, EventArgs e)
{
try
{
txt_process_name.Text = Path.GetFileNameWithoutExtension(txt_executable.Text)?.ToLower() ?? txt_process_name.Text;
2017-02-26 19:23:31 +00:00
}
catch
{
// ignored
}
}
private void rb_switch_perm_CheckedChanged(object sender, EventArgs e)
{
if (rb_switch_perm.Checked)
{
// Disable the Temporary Group
g_temporary.Enabled = false;
}
}
private void rb_switch_temp_CheckedChanged(object sender, EventArgs e)
{
if (rb_switch_temp.Checked)
{
// Enable the Temporary Group
g_temporary.Enabled = true;
}
/*g_temporary.Enabled = rb_switch_temp.Checked;
p_standalone.Enabled = rb_standalone.Checked;
txt_process_name.Enabled = cb_process.Checked;
nud_timeout.Enabled = cb_process.Checked;
p_game.Enabled = rb_launcher.Checked;
txt_args_executable.Enabled = cb_args.Checked;
if (rb_launcher.Checked)
{
nud_steamappid_ValueChanged(rb_launcher, e);
}*/
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void rb_standalone_CheckedChanged(object sender, EventArgs e)
{
if (rb_standalone.Checked)
{
// Enable the Standalone Panel
p_standalone.Enabled = true;
// Disable the Game Panel
p_game.Enabled = false;
}
}
private void rb_launcher_CheckedChanged(object sender, EventArgs e)
{
if (rb_launcher.Checked)
{
// Enable the Game Panel
p_game.Enabled = true;
// Disable the Standalone Panel
p_standalone.Enabled = false;
}
}
private void cb_args_executable_CheckedChanged(object sender, EventArgs e)
{
// Disable the Process Name Text field
if (cb_args_executable.Checked)
{
// Enable the Executable Arguments Text field
txt_args_executable.Enabled = true;
}
else
{
// Disable the Executable Arguments Text field
txt_args_executable.Enabled = false;
}
}
private void ShortcutForm_Load(object sender, EventArgs e)
{
// Make the game launcher selector read only.
cmb_game_launcher.DropDownStyle = ComboBoxStyle.DropDownList;
// Fill the list of supported game libraries
foreach (var gameLibirary in Enum.GetNames(typeof(SupportedGameLibrary))) {
cmb_game_launcher.Items.Add(gameLibirary);
}
// Start finding the games and loading the tree_games
/*foreach (var game in SteamGame.GetAllOwnedGames().OrderByDescending(game => game.IsInstalled).ThenBy(game => game.Name))
{
var iconAddress = await game.GetIcon();
if (!string.IsNullOrWhiteSpace(iconAddress))
{
try
{
using (var fileReader = File.OpenRead(iconAddress))
{
var icon = new Icon(fileReader, il_games.ImageSize);
il_games.Images.Add(icon);
}
}
catch
{
il_games.Images.Add(Properties.Resources.SteamIcon);
}
}
else
{
il_games.Images.Add(Properties.Resources.SteamIcon);
}
if (!Visible)
{
return;
}
lv_games.Items.Add(new ListViewItem
{
Text = game.Name,
Tag = game,
ImageIndex = il_games.Images.Count - 1
});
}*/
}
private void rb_wait_process_CheckedChanged(object sender, EventArgs e)
{
if (rb_wait_process.Checked)
{
// Enable the Process Name Text field
txt_process_name.Enabled = true;
}
}
private void rb_wait_executable_CheckedChanged(object sender, EventArgs e)
{
if (rb_wait_executable.Checked)
{
// Disable the Process Name Text field
txt_process_name.Enabled = false;
}
}
private void nud_timeout_executable_ValueChanged(object sender, EventArgs e)
{
}
private void nud_timeout_game_ValueChanged(object sender, EventArgs e)
{
}
private void txt_args_executable_TextChanged(object sender, EventArgs e)
{
}
private void txt_game_name_TextChanged(object sender, EventArgs e)
{
}
private void btn_app_process_Click(object sender, EventArgs e)
{
if (dialog_open.ShowDialog(this) == DialogResult.OK)
{
if (File.Exists(dialog_open.FileName) && Path.GetExtension(dialog_open.FileName) == @".exe")
{
txt_process_name.Text = dialog_open.FileName;
dialog_open.FileName = string.Empty;
}
else
{
MessageBox.Show(
Language.Selected_file_is_not_a_valid_executable_file,
Language.Executable,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
}
2017-02-26 19:23:31 +00:00
}
}