LANCommander/LANCommander.Playnite.Exten.../PowerShellRuntime.cs

74 lines
2.3 KiB
C#
Raw Normal View History

using LANCommander.SDK.Enums;
using Playnite.SDK.Models;
using System;
using System.Collections.Generic;
2023-01-15 07:10:36 +00:00
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
namespace LANCommander.PlaynitePlugin
{
internal class PowerShellRuntime
{
public void RunScript(string path, string arguments = null)
{
2023-01-15 07:10:36 +00:00
var process = new Process();
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.Arguments = $@"-File ""{path}""";
if (arguments != null)
process.StartInfo.Arguments += " " + arguments;
2023-01-15 07:10:36 +00:00
process.Start();
process.WaitForExit();
}
public void RunScriptAsAdmin(string path, string arguments = null)
{
var process = new Process();
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.UseShellExecute = true;
process.StartInfo.Verb = "runas";
process.StartInfo.Arguments = $@"-ExecutionPolicy Unrestricted -File ""{path}""";
if (arguments != null)
process.StartInfo.Arguments += " " + arguments;
process.Start();
process.WaitForExit();
}
public void RunScript(Game game, ScriptType type, string arguments = null)
{
var path = GetScriptFilePath(game, type);
if (File.Exists(path))
{
var contents = File.ReadAllText(path);
if (contents.StartsWith("# Requires Admin"))
RunScriptAsAdmin(path, arguments);
else
RunScript(path, arguments);
}
}
public static string GetScriptFilePath(Game game, ScriptType type)
{
Dictionary<ScriptType, string> filenames = new Dictionary<ScriptType, string>() {
{ ScriptType.Install, "_install.ps1" },
{ ScriptType.Uninstall, "_uninstall.ps1" },
{ ScriptType.NameChange, "_changename.ps1" },
{ ScriptType.KeyChange, "_changekey.ps1" }
};
var filename = filenames[type];
return Path.Combine(game.InstallDirectory, filename);
}
}
}