Created
January 29, 2026 14:06
-
-
Save Scott-Nx/8e66ba8576de8224dddbe0f89ee0fc72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ErrorActionPreference = "Stop" | |
| # Paths relative to this script (assumed in windows/assets/) | |
| $scriptDir = $PSScriptRoot | |
| $binDir = Join-Path $scriptDir "..\dist" | |
| $outputExe = Join-Path $binDir "launcher.exe" | |
| # The C# launcher will look for this file in the SAME directory as itself (which will be bin/) | |
| $targetExecutable = "main.exe" | |
| # Create bin dir if not exists | |
| if (-not (Test-Path $binDir)) { | |
| New-Item -Path $binDir -ItemType Directory -Force | Out-Null | |
| } | |
| $sourceCode = @" | |
| using System; | |
| using System.Diagnostics; | |
| using System.IO; | |
| using System.Text; | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // 1. Determine path to the target executable (same dir as this launcher) | |
| string exePath = Process.GetCurrentProcess().MainModule.FileName; | |
| string exeDir = Path.GetDirectoryName(exePath); | |
| string targetExePath = Path.Combine(exeDir, "$targetExecutable"); | |
| if (!File.Exists(targetExePath)) | |
| { | |
| // Fallback: try looking for main.exe | |
| targetExePath = Path.Combine(exeDir, "main.exe"); | |
| } | |
| if (!File.Exists(targetExePath)) | |
| { | |
| // Target executable missing. Exit silently with error code. | |
| Environment.Exit(1); | |
| } | |
| // 2. Build Arguments | |
| StringBuilder sbArgs = new StringBuilder(); | |
| if (args.Length > 0) | |
| { | |
| // Pass all arguments to the target executable | |
| for (int i = 0; i < args.Length; i++) | |
| { | |
| string arg = args[i]; | |
| if (arg.Contains(" ") || arg.Contains("\"")) | |
| { | |
| // Escape quotes and wrap in quotes | |
| sbArgs.Append("\"" + arg.Replace("\"", "\\\"") + "\""); | |
| } | |
| else | |
| { | |
| sbArgs.Append(arg); | |
| } | |
| if (i < args.Length - 1) sbArgs.Append(" "); | |
| } | |
| } | |
| // 3. Start the target executable | |
| ProcessStartInfo startInfo = new ProcessStartInfo(); | |
| startInfo.FileName = targetExePath; | |
| startInfo.Arguments = sbArgs.ToString(); | |
| startInfo.WorkingDirectory = exeDir; | |
| // Critical for hiding the window | |
| startInfo.WindowStyle = ProcessWindowStyle.Hidden; | |
| startInfo.CreateNoWindow = true; | |
| startInfo.UseShellExecute = false; | |
| try | |
| { | |
| Process.Start(startInfo); | |
| } | |
| catch (Exception) | |
| { | |
| Environment.Exit(1); | |
| } | |
| } | |
| } | |
| "@ | |
| # Define output path for temporary C# source (in bin dir to keep assets clean) | |
| $sourceFile = Join-Path $binDir "Launcher.cs" | |
| Set-Content -Path $sourceFile -Value $sourceCode -Encoding UTF8 | |
| # Compile | |
| Write-Host "Compiling C# Launcher..." -ForegroundColor Cyan | |
| $cscPath = Join-Path ([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) "csc.exe" | |
| if (-not (Test-Path $cscPath)) { | |
| $cscPath = Get-ChildItem -Path "C:\Windows\Microsoft.NET\Framework64" -Filter "csc.exe" -Recurse | Select-Object -Last 1 -ExpandProperty FullName | |
| } | |
| if (-not $cscPath) { | |
| Write-Error "C# Compiler (csc.exe) not found." | |
| } | |
| $buildArgs = @( | |
| "/target:winexe", # WinExe = Windows Application (no console) | |
| "/out:`"$outputExe`"", | |
| "`"$sourceFile`"" | |
| ) | |
| Start-Process -FilePath $cscPath -ArgumentList $buildArgs -Wait -NoNewWindow -PassThru | Out-Null | |
| # Cleanup | |
| if (Test-Path $outputExe) { | |
| Write-Host "Success! Launcher created at: $outputExe" -ForegroundColor Green | |
| Remove-Item $sourceFile | |
| } else { | |
| Write-Error "Compilation failed." | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment