Skip to content

Commit

Permalink
Added: Attempt to auto-create .desktop file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Sep 20, 2024
1 parent 411d0c9 commit 70c60f3
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions source/Reloaded.Mod.Installer.Lib/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ public class MainWindowViewModel : ObservableObject

public async Task InstallReloadedAsync(Settings settings)
{
// Note: All of this code is terrible, I don't have the time to make it good.

// ReSharper disable InconsistentNaming
const uint MB_OK = 0x0;
const uint MB_ICONINFORMATION = 0x40;
// ReSharper restore InconsistentNaming

// Add suffix if needed
// Handle Proton specific customizations.
var protonTricksSuffix = GetProtontricksSuffix();
var isProton = !string.IsNullOrEmpty(protonTricksSuffix);
OverrideInstallLocationForProton(settings, protonTricksSuffix, out var nativePath);
OverrideInstallLocationForProton(settings, protonTricksSuffix, out var nativeInstallFolder, out var userName);

// Check for existing installation
if (Directory.Exists(settings.InstallLocation) && Directory.GetFiles(settings.InstallLocation).Length > 0)
Expand Down Expand Up @@ -87,6 +89,7 @@ await CheckAndInstallMissingRuntimesAsync(settings.InstallLocation, tempDownload

var executableName = IntPtr.Size == 8 ? "Reloaded-II.exe" : "Reloaded-II32.exe";
var executablePath = Path.Combine(settings.InstallLocation, executableName);
var nativeExecutablePath = Path.Combine(nativeInstallFolder, executableName);
var shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Reloaded-II.lnk");

// For Proton, create a shortcut up one folder above install location.
Expand All @@ -96,7 +99,10 @@ await CheckAndInstallMissingRuntimesAsync(settings.InstallLocation, tempDownload
if (settings.CreateShortcut)
{
CurrentStepDescription = "Creating Shortcut";
NativeShellLink.MakeShortcut(shortcutPath, executablePath);
if (!isProton)
NativeShellLink.MakeShortcut(shortcutPath, executablePath);
else
MakeProtonShortcut(userName, protonTricksSuffix, shortcutPath, nativeExecutablePath);
}

CurrentStepDescription = "All Set";
Expand All @@ -111,7 +117,7 @@ await CheckAndInstallMissingRuntimesAsync(settings.InstallLocation, tempDownload
}

if (!settings.HideNonErrorGuiMessages && isProton)
Native.MessageBox(IntPtr.Zero, $"Reloaded was installed via Proton to your Desktop.\nYou can find it at: {nativePath}", "Installation Complete", MB_OK | MB_ICONINFORMATION);
Native.MessageBox(IntPtr.Zero, $"Reloaded was installed via Proton to your Desktop.\nYou can find it at: {nativeInstallFolder}", "Installation Complete", MB_OK | MB_ICONINFORMATION);

if (settings.StartReloaded)
{
Expand All @@ -133,12 +139,45 @@ await CheckAndInstallMissingRuntimesAsync(settings.InstallLocation, tempDownload
$"Stack Trace: {e.StackTrace}", "Error in Installing Reloaded", MB_OK);
}
}
private static void OverrideInstallLocationForProton(Settings settings, string protonTricksSuffix, out string nativeInstallPath)
private void MakeProtonShortcut(string? userName, string protonTricksSuffix, string shortcutPath, string nativeExecutablePath)
{
nativeExecutablePath = nativeExecutablePath.Replace('\\', '/');
var desktopFile =
"""
[Desktop Entry]
Name=Reloaded-II ({SUFFIX})
Exec=protontricks-launch --appid {APPID} "{NATIVEPATH}"
Type=Application
StartupNotify=true
Comment=Reloaded II installation for {SUFFIX}
Path={RELOADEDFOLDER}
Icon={RELOADEDFOLDER}/Mods/reloaded.sharedlib.hooks/Preview.png
StartupWMClass=reloaded-ii.exe
""";
// reloaded.sharedlib.hooks is present in all Reloaded installs after boot, so we can use that... for now.

desktopFile = desktopFile.Replace("{USER}", userName);
desktopFile = desktopFile.Replace("{APPID}", Environment.GetEnvironmentVariable("STEAM_APPID"));
desktopFile = desktopFile.Replace("{SUFFIX}", protonTricksSuffix);
desktopFile = desktopFile.Replace("{RELOADEDFOLDER}", Path.GetDirectoryName(nativeExecutablePath)!.Replace('\\', '/'));
desktopFile = desktopFile.Replace("{NATIVEPATH}", nativeExecutablePath);
shortcutPath = shortcutPath.Replace(".lnk", ".desktop");

File.WriteAllText(shortcutPath, desktopFile);
}

private static void OverrideInstallLocationForProton(Settings settings, string protonTricksSuffix, out string nativeInstallFolder, out string? userName)
{
nativeInstallPath = "";
nativeInstallFolder = "";
userName = "";
if (settings.IsManuallyOverwrittenLocation) return;
if (string.IsNullOrEmpty(protonTricksSuffix)) return;
settings.InstallLocation = Path.Combine(GetHomeDesktopDirectoryOnProton(out nativeInstallPath), $"Reloaded-II - {protonTricksSuffix}");

var desktopDir = GetHomeDesktopDirectoryOnProton(out nativeInstallFolder, out userName);
var folderName = $"Reloaded-II - {protonTricksSuffix}";

settings.InstallLocation = Path.Combine(desktopDir, folderName);
nativeInstallFolder = Path.Combine(nativeInstallFolder, folderName);
}

private static async Task DownloadReloadedAsync(string downloadLocation, IProgress<double> downloadProgress)
Expand Down Expand Up @@ -266,14 +305,14 @@ private static string GetProtontricksSuffix()
/// <summary>
/// This suffix is appended to shortcut name and install folder.
/// </summary>
private static string GetHomeDesktopDirectoryOnProton(out string linuxPath)
private static string GetHomeDesktopDirectoryOnProton(out string linuxPath, out string? userName)
{
var user = Environment.GetEnvironmentVariable("LOGNAME");
if (user != null)
userName = Environment.GetEnvironmentVariable("LOGNAME");
if (userName != null)
{
// TODO: This is a terrible hack.
linuxPath = $"/home/{user}/Desktop";
return @$"Z:\home\{user}\Desktop";
linuxPath = $"/home/{userName}/Desktop";
return @$"Z:\home\{userName}\Desktop";
}

Native.MessageBox(IntPtr.Zero, "Cannot determine username for proton installation.\n" +
Expand Down

0 comments on commit 70c60f3

Please sign in to comment.