From de78a141bc7f3bf052b5ee5ea383d7a30c7c762f Mon Sep 17 00:00:00 2001 From: ArgusMagnus Date: Sat, 25 Sep 2021 16:36:51 +0200 Subject: [PATCH 1/6] Create README.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..92ce29e --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# CSharpScriptRunner +A C# script engine for Windows which does not require admin privileges. + +## Installation + +### PowerShell + + $dir=md "$Env:Temp\{$(New-Guid)}"; $bkp=$ProgressPreference; $ProgressPreference='SilentlyContinue'; Write-Host 'Downloading...'; Invoke-WebRequest (Invoke-RestMethod -Uri 'https://api.github.com/repos/ArgusMagnus/CSharpScriptRunner/releases/latest' | select -Expand assets | select-string -InputObject {$_.browser_download_url} -Pattern '-win\.zip$' | Select -Expand Line -First 1) -OutFile "$dir\CSX.zip"; Write-Host 'Expanding archive...'; Expand-Archive -Path "$dir\CSX.zip" -DestinationPath "$dir"; & "$dir\x64\CSharpScriptRunner.exe" 'install'; Remove-Item $dir -Recurse; $ProgressPreference=$bkp; Write-Host 'Done' + From 410188474968c8912ee5d345bef1af6ba2b597b4 Mon Sep 17 00:00:00 2001 From: ArgusMagnus Date: Thu, 28 Apr 2022 16:58:50 +0200 Subject: [PATCH 2/6] set tfm to net6.0-windows --- CSharpScriptRunner.csproj | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/CSharpScriptRunner.csproj b/CSharpScriptRunner.csproj index b641886..17d89b6 100644 --- a/CSharpScriptRunner.csproj +++ b/CSharpScriptRunner.csproj @@ -3,7 +3,7 @@ C# Script Runner Exe - net5.0-windows + net6.0-windows true true true @@ -16,18 +16,18 @@ - - - + + + - - - - - + + + + + - + From b5bbff5baab35709389a7227ddcf93046ce911bc Mon Sep 17 00:00:00 2001 From: ArgusMagnus Date: Thu, 28 Apr 2022 19:06:22 +0200 Subject: [PATCH 3/6] add --self-contained --- publish.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/publish.ps1 b/publish.ps1 index 5eee048..18ff5bd 100644 --- a/publish.ps1 +++ b/publish.ps1 @@ -1,3 +1,3 @@ -dotnet publish -c Release -r win-x86 -o publish/win/x86 -p:ReleaseTag=dev -dotnet publish -c Release -r win-x64 -o publish/win/x64 -p:ReleaseTag=dev +dotnet publish -c Release -r win-x86 -o publish/win/x86 -p:ReleaseTag=dev --self-contained +dotnet publish -c Release -r win-x64 -o publish/win/x64 -p:ReleaseTag=dev --self-contained # dotnet publish -c Release -o publish/any/any -p:ReleaseTag=dev \ No newline at end of file From e30e861f913dff8a198ff6d72297b2950f6c5559 Mon Sep 17 00:00:00 2001 From: ArgusMagnus Date: Fri, 29 Apr 2022 08:08:02 +0200 Subject: [PATCH 4/6] make sure scripts run on STA thread --- Program.CurrentThreadSynchronizationContext.cs | 13 ++++++++++++- Program.Repl.cs | 4 ++-- Program.RunScript.cs | 4 +++- Program.cs | 8 ++++++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Program.CurrentThreadSynchronizationContext.cs b/Program.CurrentThreadSynchronizationContext.cs index fd7d828..40bea18 100644 --- a/Program.CurrentThreadSynchronizationContext.cs +++ b/Program.CurrentThreadSynchronizationContext.cs @@ -10,16 +10,27 @@ static partial class Program sealed class SynchronizationContextScope : IDisposable { readonly SynchronizationContext _syncCtx; + bool _isDisposed = true; public SynchronizationContextScope() => _syncCtx = SynchronizationContext.Current; - public System.Runtime.CompilerServices.YieldAwaitable Install(SynchronizationContext synchronizationContext) + public void Install(SynchronizationContext synchronizationContext) { + Dispose(); SynchronizationContext.SetSynchronizationContext(synchronizationContext); + _isDisposed = false; + } + + public System.Runtime.CompilerServices.YieldAwaitable InstallAndYield(SynchronizationContext synchronizationContext) + { + Install(synchronizationContext); return Task.Yield(); } public void Dispose() { + if (_isDisposed) + return; + _isDisposed = true; var current = SynchronizationContext.Current; if (current == _syncCtx) return; diff --git a/Program.Repl.cs b/Program.Repl.cs index 422611d..c7944fc 100644 --- a/Program.Repl.cs +++ b/Program.Repl.cs @@ -5,10 +5,10 @@ namespace CSharpScriptRunner { static partial class Program { - static async Task DoRepl() + static void DoRepl() { using var scope = new SynchronizationContextScope(); - await scope.Install(null); + scope.Install(null); Application.Init(); Application.Top.Add(new ReplApp()); Application.Run(); diff --git a/Program.RunScript.cs b/Program.RunScript.cs index 0579418..e9a09ce 100644 --- a/Program.RunScript.cs +++ b/Program.RunScript.cs @@ -104,7 +104,9 @@ static async Task RunScript(string[] arguments) // Clear SynchronizationContext to mimic standard console app behavior in script using (var nullSyncCtxScope = new SynchronizationContextScope()) { - await nullSyncCtxScope.Install(null); + nullSyncCtxScope.Install(null); + + Debug.Assert(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA); var task = (Task)entryPoint.Invoke(null, new object[] { new object[] { new ScriptGlobals(args.Skip(1).ToArray()), assemblyLoader } }); var errorCode = (ErrorCodes?)((await task) as int?) ?? ErrorCodes.OK; diff --git a/Program.cs b/Program.cs index 3596f58..100c76e 100644 --- a/Program.cs +++ b/Program.cs @@ -11,6 +11,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; +using System.Threading; using Microsoft.CodeAnalysis.Emit; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Scripting; @@ -79,7 +80,8 @@ static int Main(string[] args) static async Task MainAsync(string[] args) { using var syncCtxScope = new SynchronizationContextScope(); - await syncCtxScope.Install(new CurrentThreadSynchronizationContext()); + await syncCtxScope.InstallAndYield(new CurrentThreadSynchronizationContext()); + Debug.Assert(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA); using var thisProcess = Process.GetCurrentProcess(); var dir = Path.Combine(Path.GetDirectoryName(thisProcess.MainModule.FileName), "running"); Directory.CreateDirectory(dir); @@ -108,7 +110,7 @@ static async Task MainAsync(string[] args) case Verbs.ClearCache: await ClearCache(); break; case Verbs.InitVSCode: InitVSCode(); break; case Verbs.ListRunning: await ListRunning(); break; - case Verbs.Repl: await DoRepl(); break; + case Verbs.Repl: DoRepl(); break; } } catch (Exception ex) @@ -141,6 +143,8 @@ static void PrintHelp() Console.WriteLine($" Cleares the cache of previously compiled scripts."); Console.WriteLine($"{CmdAlias} {Verbs.ListRunning}"); Console.WriteLine($" Lists the currently running script engine instances."); + Console.WriteLine($"{CmdAlias} {Verbs.Repl}"); + Console.WriteLine($" Experimental: Enter interactive (REPL) mode."); Console.WriteLine($"Returned error codes:"); foreach (var code in Enum.GetValues().Where(x => x != ErrorCodes.Reserved)) Console.WriteLine($" - {code,3:D} {code}"); From 41979979f74e030a1bd21a84bc606106ab65e856 Mon Sep 17 00:00:00 2001 From: ArgusMagnus Date: Fri, 29 Apr 2022 13:28:08 +0200 Subject: [PATCH 5/6] Update main.yml dotnet-version: '6.0.x' --- .github/workflows/main.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c3c934a..29a1116 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,7 +12,7 @@ jobs: - name: Setup .NET Core SDK uses: actions/setup-dotnet@v1 with: - dotnet-version: '5.0.x' + dotnet-version: '6.0.x' - name: Build run: dotnet build -c Release @@ -27,14 +27,14 @@ jobs: - name: Setup .NET Core SDK uses: actions/setup-dotnet@v1 with: - dotnet-version: '5.0.x' + dotnet-version: '6.0.x' - name: Build run: | $version = "${{ github.ref }}" $version = $version.Substring(10) Add-Content -Path $env:GITHUB_ENV -Value "RELEASE_TAG=$version" -Encoding UTF8 - dotnet publish -c Release -r win-x86 -o ./publish/win/x86 -p:ReleaseTag=$version - dotnet publish -c Release -r win-x64 -o ./publish/win/x64 -p:ReleaseTag=$version + dotnet publish -c Release -r win-x86 -o ./publish/win/x86 -p:ReleaseTag=$version --self-contained + dotnet publish -c Release -r win-x64 -o ./publish/win/x64 -p:ReleaseTag=$version --self-contained Compress-Archive -Path ./publish/win/* -DestinationPath ./publish/win.zip -CompressionLevel Optimal - name: Create Release id: create_release From 471af37cf1e842fd82ead79f5899ee32ef2eca40 Mon Sep 17 00:00:00 2001 From: ArgusMagnus Date: Fri, 8 Jul 2022 16:18:28 +0200 Subject: [PATCH 6/6] update templates --- Templates/Script.csx | 12 ++++++++++++ Templates/ScriptUI.csx | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Templates/Script.csx b/Templates/Script.csx index 5f8620a..deea2fe 100644 --- a/Templates/Script.csx +++ b/Templates/Script.csx @@ -46,6 +46,18 @@ static class Script static readonly IntPtr _consoleWindow = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; + [System.Runtime.InteropServices.DllImport("user32.dll")] + static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId); + + static bool GetIsConsoleOwner() + { + int processId; + GetWindowThreadProcessId(_consoleWindow, out processId); + return processId == System.Diagnostics.Process.GetCurrentProcess().Id; + } + + public static bool IsConsoleOwner { get; } = GetIsConsoleOwner(); + [System.Runtime.InteropServices.DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); diff --git a/Templates/ScriptUI.csx b/Templates/ScriptUI.csx index b4c8ff2..42c2091 100644 --- a/Templates/ScriptUI.csx +++ b/Templates/ScriptUI.csx @@ -69,6 +69,18 @@ static class Script static readonly IntPtr _consoleWindow = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; + [System.Runtime.InteropServices.DllImport("user32.dll")] + static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId); + + static bool GetIsConsoleOwner() + { + int processId; + GetWindowThreadProcessId(_consoleWindow, out processId); + return processId == System.Diagnostics.Process.GetCurrentProcess().Id; + } + + public static bool IsConsoleOwner { get; } = GetIsConsoleOwner(); + [System.Runtime.InteropServices.DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);