Skip to content

Instantly share code, notes, and snippets.

@cetteup
Created April 6, 2022 18:45
Show Gist options
  • Save cetteup/087675f13702dddda0a7245d2fb4cab8 to your computer and use it in GitHub Desktop.
Save cetteup/087675f13702dddda0a7245d2fb4cab8 to your computer and use it in GitHub Desktop.
PowerShell script to register a URL handler for the paraworld:// protocol and act as the handler. Joining a Paraworld server is only one click away.
<#
PowerShell script to register a URL handler for the paraworld:// protocol and act as the handler. Joining a Paraworld server is only one click away.
Registers the URL handler if launched without any arguments.
Launches the game and joins the server if launched with valur -URI flag.
URL protocol format: paraworld://[server_ip]:[server_port]
#>
param(
[uri]$URI
)
function PressKeyToContinue(){
param(
[string]$Message
)
Write-Host $Message;
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
}
function Ensure-Admin() {
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if (! $myWindowsPrincipal.IsInRole($adminRole)) {
Write-Host 'Please run the tool as admin in order to register the Paraworld URL protocol handler'
PressKeyToContinue -Message 'Press any key to exit...'
Exit
}
}
function AddUpdate-RegistryItemProperty() {
param(
[string]$Path,
[string]$Name,
[string]$Value
)
if (-not $(Test-Path -Path $Path)) {
New-Item -Path $Path -Force -ErrorAction SilentlyContinue -ErrorVariable registryError | Out-Null
}
if ($Name) {
New-ItemProperty -Path $Path -Name $Name -Value $Value -Force -ErrorAction SilentlyContinue -ErrorVariable +registryError | Out-Null
}
if ($registryError) {
Write-Error "Failed to add registry item property: $($Path) # $($Name) = $($Value) ($($registryError))"
PressKeyToContinue -Message 'Press any key to exit...'
Exit
}
}
function Register-Handler() {
param(
[string]$Command
)
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\paraworld' -Name '(default)' -Value 'URL:Paraworld protocol'
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\paraworld' -Name 'URL Protocol' -Value ''
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\paraworld\shell'
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\paraworld\shell\open'
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\paraworld\shell\open\command' -Name '(default)' -Value $Command
}
function Launch-Game() {
param(
[string]$ServerIP,
[string]$ServerPort
)
$regItem = Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Sunflowers\ParaWorld" -ErrorAction SilentlyContinue -ErrorVariable registryError
if ($registryError) {
Write-Error 'Failed to find Paraworld registry entry. Ensure the game has been installed correctly or contact mister249#8356 for help'
PressKeyToContinue -Message 'Press any key to exit...'
Exit
}
if (! $regItem.InstallDir) {
Write-Error 'Paraworld registry entry does not reference the game directory. Ensure the game has been installed correctly or contact mister249#8356 for help'
PressKeyToContinue -Message 'Press any key to exit...'
Exit
}
Start-Process -FilePath "Paraworld.exe" -ArgumentList "-autoconnect $($ServerIP):$($ServerPort)" -WorkingDirectory $(Join-Path $regItem.InstallDir "bin")
}
if ($URI) {
if (! $URI.Host -or ! $URI.Port -or $URI.Port -eq -1) {
Write-Error 'Invalid URL (host or port missing/invalid)'
PressKeyToContinue -Message 'Press any key to exit...'
Exit
}
Launch-Game -ServerIP $uri.Host -ServerPort $uri.Port
}
else {
# Determine what command to register as handler
if ($MyInvocation.MyCommand.Path) {
# Register tool as Powershell script if run as script when registering
$command = "`"$($(Get-Command powershell.exe).Path)`" `"$($ScriptPath)`" -URI `"%1`""
}
else {
# Register tool as exectutable if run as such when registering
$command = "`"$([System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName)`" -URI `"%1`""
}
Ensure-Admin
Register-Handler -Command $command
Write-Host 'Successfully registered URL protocol handler for Paraworld'
Write-Host 'Note: If you ever move or rename this tool, you will need to run it again to re-register'
PressKeyToContinue -Message 'Press any key to exit...'
Exit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment