LANCommander/LANCommander.Launcher.Services/PowerShell/ElevatedScriptInterceptor.cs
Pat Hartl 6dfc0906ac Refactor launcher library importing, manifests
Switched the library importing in the launcher to work based on a queue. As a new game is added to the queue, it should find any related data and also add it to the queue. This should result in an easier to maintain importer while reducing the load on the launcher's DAL. This may result in more RAM usage while importing. Local manifests have also been refactored to match manifests in import/export LCX files, removing the old manifest format. This is a large breaking change that will probably break existing game installations. A migration will probably have to be created.
2025-11-29 18:24:27 -06:00

77 lines
No EOL
2.5 KiB
C#

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using CommandLine;
using LANCommander.Launcher.Models;
using LANCommander.SDK;
using LANCommander.SDK.Enums;
using LANCommander.SDK.PowerShell;
namespace LANCommander.Launcher.Services;
public class ElevatedScriptInterceptor : IScriptInterceptor
{
public async Task<bool> ExecuteAsync(PowerShellScript script)
{
try
{
bool isElevated = false;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
else
{
isElevated = Environment.UserName == "root";
}
if (script.RunAsAdmin && !isElevated)
{
var manifest = script.Variables.GetValue<SDK.Models.Manifest.Game>("GameManifest");
var options = new RunScriptCommandLineOptions
{
InstallDirectory = script.Variables.GetValue<string>("InstallDirectory"),
GameId = manifest.Id,
Type = script.Type,
};
if (script.Type == ScriptType.KeyChange)
options.AllocatedKey = script.Variables.GetValue<string>("AllocatedKey");
if (script.Type == ScriptType.NameChange)
{
options.OldPlayerAlias = script.Variables.GetValue<string>("OldPlayerAlias");
options.NewPlayerAlias = script.Variables.GetValue<string>("NewPlayerAlias");
}
var arguments = Parser.Default.FormatCommandLine(options);
var path = Process.GetCurrentProcess().MainModule!.FileName;
var process = new Process();
process.StartInfo.FileName = path;
process.StartInfo.Verb = "runas";
process.StartInfo.UseShellExecute = true;
process.StartInfo.WorkingDirectory = script.WorkingDirectory;
process.StartInfo.Arguments = arguments;
process.Start();
await process.WaitForExitAsync();
return true;
}
}
catch (Exception ex)
{
// Not running as admin
}
return false;
}
}