2023-01-15 10:29:47 +00:00
|
|
|
|
using LANCommander.SDK.Enums;
|
|
|
|
|
using Playnite.SDK.Models;
|
2023-01-08 18:09:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-01-15 07:10:36 +00:00
|
|
|
|
using System.Diagnostics;
|
2023-01-08 18:09:57 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Management.Automation;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.PlaynitePlugin
|
|
|
|
|
{
|
|
|
|
|
internal class PowerShellRuntime
|
|
|
|
|
{
|
2023-01-15 10:29:47 +00:00
|
|
|
|
public void RunScript(string path)
|
2023-01-08 18:09:57 +00:00
|
|
|
|
{
|
2023-01-15 07:10:36 +00:00
|
|
|
|
var process = new Process();
|
|
|
|
|
process.StartInfo.FileName = "powershell.exe";
|
2023-01-15 10:29:47 +00:00
|
|
|
|
process.StartInfo.Arguments = $@"-File ""{path}""";
|
2023-01-15 07:10:36 +00:00
|
|
|
|
process.Start();
|
|
|
|
|
process.WaitForExit();
|
2023-01-08 18:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-15 10:29:47 +00:00
|
|
|
|
public void RunScriptAsAdmin(string path)
|
2023-01-08 18:09:57 +00:00
|
|
|
|
{
|
2023-01-15 10:29:47 +00:00
|
|
|
|
var process = new Process();
|
|
|
|
|
process.StartInfo.FileName = "powershell.exe";
|
|
|
|
|
process.StartInfo.UseShellExecute = true;
|
|
|
|
|
process.StartInfo.Verb = "runas";
|
|
|
|
|
process.StartInfo.Arguments = $@"-ExecutionPolicy Unrestricted -File ""{path}""";
|
|
|
|
|
process.Start();
|
|
|
|
|
process.WaitForExit();
|
2023-01-15 07:28:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-15 10:29:47 +00:00
|
|
|
|
public void RunScript(Game game, ScriptType type)
|
2023-01-15 07:28:05 +00:00
|
|
|
|
{
|
2023-01-15 10:29:47 +00:00
|
|
|
|
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 path = Path.Combine(game.InstallDirectory, filenames[type]);
|
2023-01-15 07:28:05 +00:00
|
|
|
|
|
2023-01-15 10:29:47 +00:00
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
var contents = File.ReadAllText(path);
|
2023-01-08 18:09:57 +00:00
|
|
|
|
|
2023-01-15 10:29:47 +00:00
|
|
|
|
if (contents.StartsWith("# Requires Admin"))
|
|
|
|
|
RunScriptAsAdmin(path);
|
|
|
|
|
else
|
|
|
|
|
RunScript(path);
|
|
|
|
|
}
|
2023-01-08 18:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|