Implement Windows shortcut functionality

pull/35/head
Miepee 4 years ago
parent eba6628440
commit a92c0c56e4

@ -20,7 +20,7 @@ public static class LauncherUpdater
// Auto updating is fun! // Auto updating is fun!
/// <summary>The Version that identifies this current release.</summary> /// <summary>The Version that identifies this current release.</summary>
public const string VERSION = Core.Version; private const string VERSION = Core.Version;
/// <summary>The Path of the oldConfig. Only gets used Windows-only</summary> /// <summary>The Path of the oldConfig. Only gets used Windows-only</summary>
private static readonly string oldConfigPath = CrossPlatformOperations.CurrentPath + "/" + CrossPlatformOperations.LauncherName + ".oldCfg"; private static readonly string oldConfigPath = CrossPlatformOperations.CurrentPath + "/" + CrossPlatformOperations.LauncherName + ".oldCfg";

@ -10,6 +10,7 @@ using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -815,21 +816,34 @@ public partial class MainForm : Form
MessageBox.Show(this, Text.ShortcutWarning, Text.WarningWindowTitle, MessageBoxType.Warning); MessageBox.Show(this, Text.ShortcutWarning, Text.WarningWindowTitle, MessageBoxType.Warning);
}); });
} }
string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop, Environment.SpecialFolderOption.Create);
string shortcutFile = "";
try try
{ {
string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop, Environment.SpecialFolderOption.Create);
string shortcutFile;
string shortcutText;
if (OS.IsWindows) if (OS.IsWindows)
{ {
//TODO: implement this shortcutFile = $"{desktopFolder}/{profile.Name}.url";
const string shortcutTemplate =
"[InternetShortcut]\n" +
"IDList=\n" +
"IconIndex=0\n" +
"URL=EXECUTABLE\n" +
"IconFile=ICONPATH";
shortcutText = shortcutTemplate;
// When providing an exe, Windows uses its icon.
// And for the URL, we just use the file:// protocol
shortcutText = shortcutText.Replace("ICONPATH", $"{Core.ProfilesPath}/{profile.Name}/AM2R.exe");
shortcutText = shortcutText.Replace("EXECUTABLE", new Uri($"{Core.ProfilesPath}/{profile.Name}", UriKind.Absolute).AbsoluteUri);
} }
else if (OS.IsLinux) else if (OS.IsLinux)
{ {
shortcutFile = $"{desktopFolder}/{profile.Name}.desktop"; shortcutFile = $"{desktopFolder}/{profile.Name}.desktop";
const string desktopEntryTemplate = const string shortcutTemplate =
"[Desktop Entry]\n" + "[Desktop Entry]\n" +
"Type=Application\n" + "Type=Application\n" +
"Categories=Game\n" + "Categories=Game\n" +
@ -840,12 +854,12 @@ public partial class MainForm : Form
"Icon=ICONPATH\n" + "Icon=ICONPATH\n" +
"Terminal=false"; "Terminal=false";
string desktopEntryText = desktopEntryTemplate; shortcutText = shortcutTemplate;
// Replace values // Replace values
desktopEntryText = desktopEntryText.Replace("PROFILENAME", $"{profile.Name}"); shortcutText = shortcutText.Replace("PROFILENAME", $"{profile.Name}");
desktopEntryText = desktopEntryText.Replace("PROFILEDESCRIPTION", $"A shortcut for {profile.Name}."); shortcutText = shortcutText.Replace("PROFILEDESCRIPTION", $"A shortcut for {profile.Name}.");
desktopEntryText = desktopEntryText.Replace("ICONPATH", $"{Core.PatchDataPath}/data/files_to_copy/icon.png"); shortcutText = shortcutText.Replace("ICONPATH", $"{Core.PatchDataPath}/data/files_to_copy/icon.png");
string gameName; string gameName;
#if NOAPPIMAGE #if NOAPPIMAGE
@ -855,12 +869,9 @@ public partial class MainForm : Form
#endif #endif
log.Info($"Game name for shortcut is: {gameName}"); log.Info($"Game name for shortcut is: {gameName}");
if (OS.IsThisRunningFromFlatpak) if (OS.IsThisRunningFromFlatpak)
desktopEntryText = desktopEntryText.Replace("EXECUTABLE", $"flatpak run \"--command={Core.ProfilesPath}/{profile.Name}/{gameName}\" io.github.am2r_community_developers.AM2RLauncher"); shortcutText = shortcutText.Replace("EXECUTABLE", $"flatpak run \"--command={Core.ProfilesPath}/{profile.Name}/{gameName}\" io.github.am2r_community_developers.AM2RLauncher");
else else
desktopEntryText = desktopEntryText.Replace("EXECUTABLE", $"{Core.ProfilesPath}/{profile.Name}/{gameName}"); shortcutText = shortcutText.Replace("EXECUTABLE", $"{Core.ProfilesPath}/{profile.Name}/{gameName}");
File.WriteAllText(shortcutFile, desktopEntryText);
Process.Start("chmod", $"+x \"{shortcutFile}\"")?.WaitForExit();
} }
else if (OS.IsMac) else if (OS.IsMac)
{ {
@ -871,7 +882,8 @@ public partial class MainForm : Form
log.Error($"{OS.Name} has no way of creating shortcuts"); log.Error($"{OS.Name} has no way of creating shortcuts");
return; return;
} }
File.WriteAllText(shortcutFile, shortcutText);
if (OS.IsUnix) Process.Start("chmod", $"+x \"{shortcutFile}\"")?.WaitForExit();
CrossPlatformOperations.OpenFolderAndSelectFile(shortcutFile); CrossPlatformOperations.OpenFolderAndSelectFile(shortcutFile);
} }
// We only care about io exceptions (file not readable, drive not available etc.) The rest should throw normally // We only care about io exceptions (file not readable, drive not available etc.) The rest should throw normally

Loading…
Cancel
Save