diff --git a/LANCommander/LANCommander.csproj b/LANCommander/LANCommander.csproj index a582b9b..720db32 100644 --- a/LANCommander/LANCommander.csproj +++ b/LANCommander/LANCommander.csproj @@ -52,6 +52,7 @@ + diff --git a/LANCommander/Pages/Games/Components/RegToPowerShell.razor b/LANCommander/Pages/Games/Components/RegToPowerShell.razor new file mode 100644 index 0000000..b143ad4 --- /dev/null +++ b/LANCommander/Pages/Games/Components/RegToPowerShell.razor @@ -0,0 +1,86 @@ +@inject IMessageService MessageService + + + + + +@code { + [Parameter] public EventCallback OnParsed { get; set; } + + bool Visible = false; + string Contents = ""; + + StandaloneCodeEditor? Editor; + + private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor) + { + return new StandaloneEditorConstructionOptions + { + AutomaticLayout = true, + Language = "ini", + Value = Contents, + Theme = "vs-dark", + }; + } + + private async Task Parse() + { + Contents = await Editor.GetValue(); + + var parser = new RegParserDotNet.RegParser(); + var lines = new List(); + + try + { + var keys = parser.Parse(Contents); + + foreach (var key in keys) + { + switch (key.Type) + { + case RegParserDotNet.RegistryValueType.REG_KEY: + if (lines.Count > 0) + lines.Add(""); + + lines.Add($"New-Item -Path \"registry::\\{key.Path}\""); + break; + + case RegParserDotNet.RegistryValueType.REG_SZ: + lines.Add($"New-ItemProperty -Path \"registry::\\{key.Path}\" -Name \"{key.Property}\" -Value \"{(string)key.Value}\" -Force"); + break; + + case RegParserDotNet.RegistryValueType.REG_DWORD: + lines.Add($"New-ItemProperty -Path \"registry::\\{key.Path}\" -Name \"{key.Property}\" -Value {(int)key.Value} -Force"); + break; + + case RegParserDotNet.RegistryValueType.REG_BINARY: + var bytes = key.Value as byte[]; + var convertedBytes = String.Join("\\\n", bytes.Chunk(32).Select(c => String.Join(", ", c.Select(b => "0x" + b.ToString("X2"))))); + lines.Add($"New-ItemProperty -Path \"registry::\\{key.Path}\" -Name \"{key.Property}\" -PropertyType Binary -Value [byte[]]({convertedBytes}) -Force"); + break; + } + } + + if (OnParsed.HasDelegate) + await OnParsed.InvokeAsync(String.Join('\n', lines)); + + Close(); + + StateHasChanged(); + } + catch (Exception ex) + { + MessageService.Error(ex.Message); + } + } + + public void Open() + { + Visible = true; + } + + public void Close() + { + Visible = false; + } +} diff --git a/LANCommander/Pages/Games/Components/ScriptEditor.razor b/LANCommander/Pages/Games/Components/ScriptEditor.razor index 7731b4c..3fa417a 100644 --- a/LANCommander/Pages/Games/Components/ScriptEditor.razor +++ b/LANCommander/Pages/Games/Components/ScriptEditor.razor @@ -43,6 +43,8 @@ { } + + @@ -70,6 +72,8 @@ + + @@ -119,6 +123,7 @@ IEnumerable Snippets { get; set; } StandaloneCodeEditor? Editor; + RegToPowerShell RegToPowerShell; private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor) { @@ -195,31 +200,46 @@ await MessageService.Success("Script saved!"); } - private async void InsertSnippet(Snippet snippet) + private async Task InsertText(string text) { - await Editor.Trigger("keyboard", "type", new + var line = await Editor.GetPosition(); + var range = new BlazorMonaco.Range(line.LineNumber, 1, line.LineNumber, 1); + + var currentSelections = await Editor.GetSelections(); + + await Editor.ExecuteEdits("ScriptEditor", new List() { - text = snippet.Content - }); + new IdentifiedSingleEditOperation + { + Range = range, + Text = text, + ForceMoveMarkers = true + } + }, currentSelections); + } + + private async Task InsertSnippet(Snippet snippet) + { + await InsertText(snippet.Content); } private async void BrowseForPath() { var modalOptions = new ModalOptions() - { - Title = "Choose Reference", - Maximizable = false, - DefaultMaximized = true, - Closable = true, - OkText = "Insert File Path" - }; + { + Title = "Choose Reference", + Maximizable = false, + DefaultMaximized = true, + Closable = true, + OkText = "Insert File Path" + }; var browserOptions = new FilePickerOptions() - { - ArchiveId = ArchiveId, - Select = true, - Multiple = false - }; + { + ArchiveId = ArchiveId, + Select = true, + Multiple = false + }; var modalRef = await ModalService.CreateModalAsync>(modalOptions, browserOptions); @@ -227,10 +247,7 @@ { var path = results.FirstOrDefault().Path; - Editor.Trigger("keyboard", "type", new - { - text = $"$InstallDir\\{path.Replace('/', '\\')}" - }); + InsertText($"$InstallDir\\{path.Replace('/', '\\')}"); StateHasChanged(); return Task.CompletedTask;