@using LANCommander.SDK.PowerShell
@using LANCommander.SDK.Services
@using LANCommander.Server.Services.PowerShell
@using XtermBlazor
@using LogLevel = Microsoft.Extensions.Logging.LogLevel
@inject ScriptDebugger ScriptDebugger
@inject ScriptClient ScriptClient
@inject GameService GameService
@code {
[Parameter] public Guid Id { get; set; }
[Parameter] public bool Active { get; set; }
[Parameter] public EventCallback ActiveChanged { get; set; }
private Terminal? _terminal;
private readonly TerminalOptions _options = new()
{
CursorBlink = true,
CursorStyle = CursorStyle.Bar,
};
private readonly HashSet _addons =
[
"readline",
"addon-fit"
];
async Task SetActive(bool active)
{
Active = active;
if (ActiveChanged.HasDelegate)
await ActiveChanged.InvokeAsync(Active);
}
public async Task DebugPackagingScript(Guid gameId)
{
ScriptDebugger.OnBreak = Break;
ScriptDebugger.OnStart = Start;
ScriptDebugger.OnEnd = End;
ScriptDebugger.OnOutput = Output;
ScriptClient.Debug = true;
await GameService.PackageAsync(gameId);
}
public async Task Start(IScriptDebugContext context)
{
if (_terminal is null)
return;
await _terminal.Clear();
await SetActive(true);
await InvokeAsync(StateHasChanged);
await _terminal.Focus();
await _terminal.FitAsync();
}
public async Task End(IScriptDebugContext context)
{
await SetActive(false);
await InvokeAsync(StateHasChanged);
}
public async Task Break(IScriptDebugContext context)
{
if (_terminal is null)
return;
while (true)
{
var input = await _terminal.ReadLineAsync();
if (input.Trim().Equals("exit", StringComparison.OrdinalIgnoreCase))
break;
if (input.StartsWith('$'))
input = "Write-Host " + input;
await context.ExecuteAsync(input);
}
}
public async Task Output(IScriptDebugContext context, LogLevel level, string message)
{
if (_terminal is null)
return;
try
{
await _terminal.WriteLine(message, level);
}
catch (Exception ex)
{
}
}
}