Added PowerShell script snippets to editor

dashboard
Pat Hartl 2023-01-26 20:42:33 -06:00
parent 7f93f806fd
commit 58e4886441
22 changed files with 154 additions and 7 deletions

View File

@ -9,7 +9,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LANCommander.PlaynitePlugin
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LANCommander.SDK", "LANCommander.SDK\LANCommander.SDK.csproj", "{4C2A71FD-A30B-4D62-888A-4EF843D8E506}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LANCommander.PCGamingWiki", "LANCommander.PCGamingWiki\LANCommander.PCGamingWiki.csproj", "{2436B817-4475-4E70-9BB2-E1E7866DB79F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LANCommander.PCGamingWiki", "LANCommander.PCGamingWiki\LANCommander.PCGamingWiki.csproj", "{2436B817-4475-4E70-9BB2-E1E7866DB79F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -0,0 +1,32 @@
@using LANCommander.Models;
@using LANCommander.Services;
@inject IJSRuntime JS
@foreach (var group in Snippets.Select(s => s.Group).Distinct())
{
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
@group
</button>
<ul class="dropdown-menu">
@foreach (var snippet in Snippets.Where(s => s.Group == group))
{
<li><a class="dropdown-item" @onclick="() => InsertSnippet(snippet)">@snippet.Name</a></li>
}
</ul>
</div>
}
@code {
public IEnumerable<Snippet> Snippets { get; set; }
protected override void OnInitialized()
{
Snippets = ScriptService.GetSnippets();
}
private async Task InsertSnippet(Snippet snippet) {
await JS.InvokeVoidAsync("Editor.trigger", "keyboard", "type", new { text = snippet.Content });
}
}

View File

@ -0,0 +1,9 @@
namespace LANCommander.Models
{
public class Snippet
{
public string Group { get; set; }
public string Name { get; set; }
public string Content { get; set; }
}
}

View File

@ -1,5 +1,7 @@
using LANCommander.Data;
using LANCommander.Data.Models;
using LANCommander.Models;
using System.Security.Cryptography.X509Certificates;
namespace LANCommander.Services
{
@ -8,5 +10,22 @@ namespace LANCommander.Services
public ScriptService(DatabaseContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
}
public static IEnumerable<Snippet> GetSnippets()
{
var files = Directory.GetFiles(@"Snippets", "*.ps1", SearchOption.AllDirectories);
return files.Select(f =>
{
var split = f.Split('\\');
return new Snippet()
{
Name = Path.GetFileNameWithoutExtension(f),
Group = split[1],
Content = File.ReadAllText(f)
};
});
}
}
}

View File

@ -0,0 +1 @@
Copy-Item -Path "$InstallDir\<Source Path>" -Destination "$InstallDir\<Destination Path>" -Recurse

View File

@ -0,0 +1 @@
New-Item -ItemType Directory -Force -Path "$InstallDir\<Path>"

View File

@ -0,0 +1,2 @@
# Non-destructively creates path in registry
New-Item -Path "HKLM:\SOFTWARE\WOW6432Node\<Path>"

View File

@ -0,0 +1,2 @@
# Writes byte[] to a file at an offset
Patch-Binary -FilePath "$InstallDir\<File Path>" -Offset 0x00 -Data $bytes

View File

@ -0,0 +1 @@
Remove-Item "$InstallDir\<DirectoryPath>" -Recurse -ErrorAction Ignore

View File

@ -0,0 +1,2 @@
# Use regex to replace text within a file. Quotes are escaped by double quoting ("")
Write-ReplaceContentInFile -Regex '^game.setPlayerName "(.+)"' -Replacement "game.setPlayerName ""$NewName""" -FilePath "$InstallDir\<File Path>"

View File

@ -0,0 +1,2 @@
# Creates or updates a key in the registry
New-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\<Path>" -Name "KeyName" -Value "New Value" -Force

View File

@ -0,0 +1,2 @@
# Convert an input string to ASCII-encoded byte[]. Shorter strings will pad out to 12 bytes, longer strings will be trimmed.
$bytes = Get-AsciiBytes -InputString "Hello world!" -MaxLength 12

View File

@ -0,0 +1,4 @@
# Trim a string down to a specified amount of characters
if ($NewName.Length -gt 10) {
$NewName = $NewName.Substring(0, 10);
}

View File

@ -0,0 +1,2 @@
# Write contents of a string to a file
Set-Content "$InstallDir\<File Path>" "Hello world!"

View File

@ -0,0 +1,30 @@
function Get-AsciiBytes([string]$InputString, [int]$MaxLength)
{
if ($InputString.Length -gt $MaxLength)
{
$InputString = $InputString.Substring(0, $MaxLength)
}
$bytes = [System.Text.Encoding]::ASCII.GetBytes($InputString)
$array = @()
$count = 0
$extraPadding = $MaxLength - $bytes.Length
foreach ($byte in $bytes)
{
if ($count -lt $MaxLength)
{
$array += $byte
$count++
}
}
# Pad the end with 0x00 to meet our max length
for ($i = $count; $i -lt $MaxLength; $i++)
{
$array += 0x00
}
return $array
}

View File

@ -0,0 +1,11 @@
function Patch-Binary([byte[]]$Data, [int]$Offset, [string]$FilePath)
{
$bytes = [System.IO.File]::ReadAllBytes($FilePath)
for ($i = 0; $i -lt $Data.Length; $i++)
{
$bytes[$Offset + $i] = $Data[$i]
}
[System.IO.File]::WriteAllBytes($FilePath, $bytes)
}

View File

@ -0,0 +1,5 @@
function Write-ReplaceContentInFile([string]$Regex, [string]$Replacement, [string]$FilePath)
{
$content = (Get-Content $FilePath) -replace $Regex, $Replacement
[IO.File]::WriteAllLines($FilePath, $content)
}

View File

@ -0,0 +1,2 @@
# Accessible via $Display.ScreenWidth and $Display.ScreenHeight
$Display = Get-WmiObject -Class Win32_DesktopMonitor | Select-Object ScreenWidth,ScreenHeight

View File

@ -0,0 +1 @@
$InstallDir = $PSScriptRoot

View File

@ -0,0 +1 @@
$NewName = $args[0]

View File

@ -1,4 +1,5 @@
@using LANCommander.Data.Enums
@using LANCommander.Components;
@using LANCommander.Data.Enums
@model LANCommander.Data.Models.Script
@{
@ -68,6 +69,12 @@
<input type="hidden" asp-for="GameId" />
</div>
</div>
<div class="row">
<div class="col btn-list mb-3">
<component type="typeof(SnippetBar)" render-mode="Server" />
</div>
</div>
</div>
<div id="ScriptEditor" style="height: 100%; min-height: 600px;"></div>
@ -90,9 +97,11 @@
<script src="~/lib/monaco-editor/min/vs/loader.js"></script>
<script>
window.Editor = {};
require.config({ paths: { vs: '/lib/monaco-editor/min/vs' } });
require(['vs/editor/editor.main'], function () {
var editor = monaco.editor.create(document.getElementById('ScriptEditor'), {
window.Editor = monaco.editor.create(document.getElementById('ScriptEditor'), {
value: $('#Contents').val(),
language: 'powershell',
readOnly: false,
@ -100,7 +109,7 @@
automaticLayout: true
});
editor.onDidChangeModelContent(function (e) {
window.Editor.onDidChangeModelContent(function (e) {
$('#Contents').val(editor.getModel().getValue());
});
});

View File

@ -1,4 +1,5 @@
@using LANCommander.Data.Enums
@using LANCommander.Components;
@using LANCommander.Data.Enums
@model LANCommander.Data.Models.Script
@{
@ -69,6 +70,12 @@
<input type="hidden" asp-for="Id" />
</div>
</div>
<div class="row">
<div class="col btn-list mb-3">
<component type="typeof(SnippetBar)" render-mode="Server" />
</div>
</div>
</div>
<div id="ScriptEditor" style="height: 100%; min-height: 600px;"></div>
@ -91,9 +98,11 @@
<script src="~/lib/monaco-editor/min/vs/loader.js"></script>
<script>
window.Editor = {};
require.config({ paths: { vs: '/lib/monaco-editor/min/vs' } });
require(['vs/editor/editor.main'], function () {
var editor = monaco.editor.create(document.getElementById('ScriptEditor'), {
window.Editor = monaco.editor.create(document.getElementById('ScriptEditor'), {
value: $('#Contents').val(),
language: 'powershell',
readOnly: false,
@ -101,7 +110,7 @@
automaticLayout: true
});
editor.onDidChangeModelContent(function (e) {
window.Editor.onDidChangeModelContent(function (e) {
$('#Contents').val(editor.getModel().getValue());
});
});