Skip to content

Commit

Permalink
Detect SPT-AKI install locations from MUI cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sailro committed Sep 17, 2024
1 parent de3c698 commit 8d12206
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
19 changes: 7 additions & 12 deletions Installer/Installation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Linq;
using System.Runtime.Versioning;
using System.Text;
using Microsoft.Win32;
using Spectre.Console;

namespace Installer;
Expand Down Expand Up @@ -95,18 +94,14 @@ private static IEnumerable<Installation> DiscoverInstallations()
if (TryDiscoverInstallation(Path.Combine(Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System))!, "SPT"), out installation))
yield return installation;

using var hive = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
using var eft = hive.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\EscapeFromTarkov", false);

if (eft == null)
yield break;

var exe = eft.GetValue("DisplayIcon") as string;
if (string.IsNullOrEmpty(exe) || !File.Exists(exe))
yield break;
// SPT-AKI locations from MUI cache
foreach (var sptpath in Registry.GetSptAkiInstallationsFromMuiCache())
{
if (TryDiscoverInstallation(sptpath, out installation))
yield return installation;
}

var path = Path.GetDirectoryName(exe);
if (string.IsNullOrEmpty(path) || !Directory.Exists(path))
if (!Registry.TryGetEscapeFromTarkovInstallationPath(out var path))
yield break;

if (TryDiscoverInstallation(path, out installation))
Expand Down
68 changes: 68 additions & 0 deletions Installer/Registry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using Microsoft.Win32;

namespace Installer
{
[SupportedOSPlatform("windows")]
internal class Registry
{
public static bool TryGetEscapeFromTarkovInstallationPath([NotNullWhen(true)] out string? installationPath)
{
installationPath = null;

try
{
using var hive = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
using var eft = hive.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\EscapeFromTarkov", false);

if (eft == null)
return false;

var exe = eft.GetValue("DisplayIcon") as string;
if (string.IsNullOrEmpty(exe) || !File.Exists(exe))
return false;

var path = Path.GetDirectoryName(exe);
if (string.IsNullOrEmpty(path) || !Directory.Exists(path))
return false;

installationPath = path;
return true;
}
catch
{
return false;
}
}

public static IEnumerable<string?> GetSptAkiInstallationsFromMuiCache()
{
try
{
using var hive = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
using var mui = hive.OpenSubKey(@"Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache", false);

if (mui == null)
return [];

const string attribute = ".FriendlyAppName";
string[] candidates = ["SPT.Launcher.exe", "SPT.Server.exe", "Aki.Launcher.exe"];

return mui
.GetValueNames()
.Where(v => candidates.Any(c => v.Contains($"{c}{attribute}", StringComparison.OrdinalIgnoreCase)))
.Select(v => Path.GetDirectoryName(v.Replace(attribute, string.Empty)))
.Distinct();
}
catch
{
return [];
}
}
}
}

0 comments on commit 8d12206

Please sign in to comment.