diff --git a/LANCommander.Launcher/UI/Components/PowerShellConsole.razor b/LANCommander.Launcher/UI/Components/PowerShellConsole.razor index 67fa1bfa..c2c78cca 100644 --- a/LANCommander.Launcher/UI/Components/PowerShellConsole.razor +++ b/LANCommander.Launcher/UI/Components/PowerShellConsole.razor @@ -2,9 +2,12 @@ @using LogLevel = Microsoft.Extensions.Logging.LogLevel @inject SDK.Client Client - +
+ +
@code { + private bool Visible { get; set; } = false; private Xterm Terminal; private TaskCompletionSource InputTaskCompletionSource; @@ -22,6 +25,15 @@ protected override async Task OnInitializedAsync() { + Client.Scripts.OnDebugStart = async (ps) => + { + Terminal?.Clear(); + Visible = true; + await InvokeAsync(StateHasChanged); + + await Terminal.Addon("addon-fit").InvokeVoidAsync("fit"); + }; + Client.Scripts.OnOutput = async (level, message) => { switch (level) @@ -48,7 +60,7 @@ } }; - Client.Scripts.OnDebug = async (ps) => + Client.Scripts.OnDebugBreak = async (ps) => { while (true) { @@ -62,6 +74,9 @@ await ps.InvokeAsync(); } + + Visible = false; + await InvokeAsync(StateHasChanged); }; } diff --git a/LANCommander.SDK/PowerShell/PowerShellScript.cs b/LANCommander.SDK/PowerShell/PowerShellScript.cs index 941eb280..af0d731a 100644 --- a/LANCommander.SDK/PowerShell/PowerShellScript.cs +++ b/LANCommander.SDK/PowerShell/PowerShellScript.cs @@ -34,9 +34,18 @@ namespace LANCommander.SDK.PowerShell private TaskCompletionSource Input { get; set; } - public Func OnDebug; + public Func OnDebugStart; + public Func OnDebugBreak; public Func OnOutput; + private const string Logo = @" + __ ___ _ _______ __ + / / / _ | / |/ / ___/__ __ _ __ _ ___ ____ ___/ /__ ____ + / /__/ __ |/ / /__/ _ \/ ' \/ ' \/ _ `/ _ \/ _ / -_) __/ +/____/_/ |_/_/|_/\___/\___/_/_/_/_/_/_/\_,_/_//_/\_,_/\__/_/ + +"; + public PowerShellScript(ScriptType type) { Type = type; @@ -144,32 +153,8 @@ namespace LANCommander.SDK.PowerShell public async Task ExecuteAsync() { - var scriptBuilder = new StringBuilder(); - var wow64Value = IntPtr.Zero; - foreach (var variable in Variables) - { - scriptBuilder.AppendLine($"${variable.Name} = ConvertFrom-SerializedBase64 \"{Serialize(variable.Value)}\""); - } - - scriptBuilder.AppendLine(Contents); - - if (Debug) - { - scriptBuilder.AppendLine("Write-Host '----- DEBUG -----'"); - scriptBuilder.AppendLine("Write-Host 'Variables:'"); - - foreach (var variable in Variables) - { - scriptBuilder.AppendLine($"Write-Host ' ${variable.Name}'"); - } - - scriptBuilder.AppendLine("Write-Host ''"); - - // Process.StartInfo.Arguments += " -NoExit"; - } - if (IgnoreWow64) Wow64DisableWow64FsRedirection(ref wow64Value); @@ -179,14 +164,41 @@ namespace LANCommander.SDK.PowerShell runspace.SessionStateProxy.Path.SetLocation(WorkingDirectory); + foreach (var variable in Variables) + { + runspace.SessionStateProxy.SetVariable(variable.Name, variable.Value); + } + + runspace.SessionStateProxy.SetVariable("Logo", Logo); + runspace.SessionStateProxy.SetVariable("ScriptType", Type); + runspace.SessionStateProxy.SetVariable("WorkingDirectory", WorkingDirectory); + using (var ps = System.Management.Automation.PowerShell.Create()) { ps.Runspace = runspace; - ps.AddScript(scriptBuilder.ToString()); + if (Debug) + OnDebugStart?.Invoke(ps); + + ps.AddScript("Write-Host $Logo"); + + ps.AddScript(Contents); if (Debug) { + ps.AddScript("Write-Host '--------- DEBUG ---------'"); + ps.AddScript("Write-Host \"Script Type: $ScriptType\""); + ps.AddScript("Write-Host \"Working Directory: $WorkingDirectory\""); + ps.AddScript("Write-Host 'Variables:'"); + + foreach (var variable in Variables) + { + ps.AddScript($"Write-Host ' ${variable.Name}'"); + } + + ps.AddScript("Write-Host ''"); + ps.AddScript("Write-Host 'Enter \"exit\" to continue'"); + if (OnOutput != null) { ps.Streams.Information.DataAdded += Information_DataAdded; @@ -200,9 +212,7 @@ namespace LANCommander.SDK.PowerShell var results = await ps.InvokeAsync(); if (Debug) - { - await OnDebug?.Invoke(ps); - } + await OnDebugBreak?.Invoke(ps); } } diff --git a/LANCommander.SDK/ScriptService.cs b/LANCommander.SDK/ScriptService.cs index 0f406fbb..eaf39b15 100644 --- a/LANCommander.SDK/ScriptService.cs +++ b/LANCommander.SDK/ScriptService.cs @@ -23,7 +23,8 @@ namespace LANCommander.SDK public delegate Task ExternalScriptRunnerHandler(PowerShellScript script); public event ExternalScriptRunnerHandler ExternalScriptRunner; - public Func OnDebug; + public Func OnDebugStart; + public Func OnDebugBreak; public Func OnOutput; public ScriptService(Client client) @@ -50,6 +51,9 @@ namespace LANCommander.SDK var script = new PowerShellScript(Enums.ScriptType.Install); + if (debug) + script.OnDebugStart = OnDebugStart; + script.AddVariable("InstallDirectory", installDirectory); script.AddVariable("GameManifest", manifest); script.AddVariable("DefaultInstallDirectory", Client.DefaultInstallDirectory); @@ -60,7 +64,7 @@ namespace LANCommander.SDK if (debug) { script.EnableDebug(); - script.OnDebug = OnDebug; + script.OnDebugBreak = OnDebugBreak; script.OnOutput = OnOutput; } @@ -96,6 +100,9 @@ namespace LANCommander.SDK { var script = new PowerShellScript(Enums.ScriptType.Uninstall); + if (debug) + script.OnDebugStart = OnDebugStart; + script.AddVariable("InstallDirectory", installDirectory); script.AddVariable("GameManifest", manifest); script.AddVariable("DefaultInstallDirectory", Client.DefaultInstallDirectory); @@ -105,7 +112,7 @@ namespace LANCommander.SDK if (debug) { script.EnableDebug(); - script.OnDebug = OnDebug; + script.OnDebugBreak = OnDebugBreak; script.OnOutput = OnOutput; } @@ -139,6 +146,9 @@ namespace LANCommander.SDK var script = new PowerShellScript(Enums.ScriptType.BeforeStart); + if (debug) + script.OnDebugStart = OnDebugStart; + script.AddVariable("InstallDirectory", installDirectory); script.AddVariable("GameManifest", manifest); script.AddVariable("DefaultInstallDirectory", Client.DefaultInstallDirectory); @@ -150,7 +160,7 @@ namespace LANCommander.SDK if (debug) { script.EnableDebug(); - script.OnDebug = OnDebug; + script.OnDebugBreak = OnDebugBreak; script.OnOutput = OnOutput; } @@ -181,6 +191,9 @@ namespace LANCommander.SDK var script = new PowerShellScript(Enums.ScriptType.AfterStop); + if (debug) + script.OnDebugStart = OnDebugStart; + script.AddVariable("InstallDirectory", installDirectory); script.AddVariable("GameManifest", manifest); script.AddVariable("DefaultInstallDirectory", Client.DefaultInstallDirectory); @@ -192,7 +205,7 @@ namespace LANCommander.SDK if (debug) { script.EnableDebug(); - script.OnDebug = OnDebug; + script.OnDebugBreak = OnDebugBreak; script.OnOutput = OnOutput; } @@ -231,6 +244,9 @@ namespace LANCommander.SDK var script = new PowerShellScript(Enums.ScriptType.NameChange); + if (debug) + script.OnDebugStart = OnDebugStart; + script.AddVariable("InstallDirectory", installDirectory); script.AddVariable("GameManifest", manifest); script.AddVariable("DefaultInstallDirectory", Client.DefaultInstallDirectory); @@ -245,7 +261,7 @@ namespace LANCommander.SDK if (debug) { script.EnableDebug(); - script.OnDebug = OnDebug; + script.OnDebugBreak = OnDebugBreak; script.OnOutput = OnOutput; } @@ -277,6 +293,9 @@ namespace LANCommander.SDK var script = new PowerShellScript(Enums.ScriptType.KeyChange); + if (debug) + script.OnDebugStart = OnDebugStart; + Logger?.LogTrace("New key is {Key}", key); script.AddVariable("InstallDirectory", installDirectory); @@ -292,7 +311,7 @@ namespace LANCommander.SDK if (debug) { script.EnableDebug(); - script.OnDebug = OnDebug; + script.OnDebugBreak = OnDebugBreak; script.OnOutput = OnOutput; }