Created
March 20, 2022 14:04
-
-
Save cetteup/da31a6c2e53b872d96081e859ad178a4 to your computer and use it in GitHub Desktop.
PowerShell script to register a URL handler for the bf1942:// protocol and act as the handler. Joining a Battlefield 1942 server is only one click away.
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
<# | |
PowerShell script to register a URL handler for the bf1942:// protocol and act as the handler. Joining a Battlefield 1942 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: bf1942://[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 Battlefield 1942 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\bf1942' -Name '(default)' -Value 'URL:Battlefield 1942 protocol' | |
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\bf1942' -Name 'URL Protocol' -Value '' | |
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\bf1942\shell' | |
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\bf1942\shell\open' | |
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\bf1942\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\EA Games\Battlefield 1942" -ErrorAction SilentlyContinue -ErrorVariable registryError | |
if ($registryError) { | |
Write-Error 'Failed to find Battlefield 1942 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.GAMEDIR) { | |
Write-Error 'Battlefield 1942 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 "BF1942.exe" -ArgumentList "+restart 1", "+joinServer $($ServerIP):$($ServerPort)" -WorkingDirectory $regItem.GAMEDIR | |
} | |
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 Battlefield 1942' | |
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