Created
March 20, 2022 14:03
-
-
Save cetteup/c40d75c1b6d0eb32e5ebc1f329c7a5c8 to your computer and use it in GitHub Desktop.
PowerShell script to register a URL handler for the bfv:// protocol and act as the handler. Joining a Battlefield Vietnam 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 bfv:// protocol and act as the handler. Joining a Battlefield Vietnam 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: bfv://[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 Vietnam 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\bfv' -Name '(default)' -Value 'URL:Battlefield Vietnam protocol' | |
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\bfv' -Name 'URL Protocol' -Value '' | |
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\bfv\shell' | |
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\bfv\shell\open' | |
AddUpdate-RegistryItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\bfv\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 Vietnam" -ErrorAction SilentlyContinue -ErrorVariable registryError | |
if ($registryError) { | |
Write-Error 'Failed to find Battlefield Vietnam 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 Vietnam 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 "BfVietnam.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 Vietnam' | |
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