Files
wiicompiled/Launcher/WiiCompiled.Setup.Windows/RunningProductGuard.cs
T
theofficialgman c0ed2bfbeb refactor WiiCompiled.Setup into WiiCompiled.Setup.Windows and add WiiCompiled.Setup.Common
the idea behind this is C# code that is OS agnostic can go in WiiCompiled.Setup.Common to be shared by any OS specific code (eg: WiiCompiled.Setup.Windows and WiiCompiled.Setup.Linux).
2026-08-29 12:06:58 -04:00

49 lines
1.7 KiB
C#

using System.Diagnostics;
using WiiCompiled.Setup.Common;
namespace WiiCompiled.Setup.Windows;
/// <summary>
/// Refuses to replace installed products while one of them is running: publishing renames the
/// product directories, which Windows rejects while a process runs from them.
/// </summary>
internal static class RunningProductGuard
{
public static void EnsureProductsNotRunning(string installDirectory)
{
var installation = new Installation(installDirectory);
var running = FindProcessesUnder(installation.BaseDirectory, installation.RetroDirectory);
if (running.Count == 0) return;
throw new InvalidOperationException(
$"Mario Kart Wii is still running ({string.Join(", ", running)}). " +
"Close the game, then retry the update.");
}
private static List<string> FindProcessesUnder(params string[] roots)
{
var names = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var process in Process.GetProcesses())
{
try
{
if (process.Id == Environment.ProcessId) continue;
var path = process.MainModule?.FileName;
if (path is null) continue;
if (roots.Any(root => FileSystemUtilities.PathContains(root, Path.GetFullPath(path))))
names.Add(Path.GetFileName(path));
}
catch
{
// Inaccessible processes (elevated, exited, protected) cannot run our products' exes
// from a user-writable install directory in any case that matters here.
}
finally
{
process.Dispose();
}
}
return names.ToList();
}
}