$NewName = $args[0]
$InstallDir = $PSScriptRoot
function Get-AsciiBytes([string]$InputString, [int]$MaxLength)
{
if ($InputString.Length -gt $MaxLength)
{
$InputString = $InputString.Substring(0, $MaxLength)
}
$bytes = [System.Text.Encoding]::ASCII.GetBytes($InputString)
$array = @()
$count = 0
$extraPadding = $MaxLength - $bytes.Length
foreach ($byte in $bytes)
{
if ($count -lt $MaxLength)
{
$array += $byte
$count++
}
}
for ($i = $count; $i -lt $MaxLength; $i++)
{
$array += 0x00
}
return $array
}
function Patch-Binary([byte[]]$Data, [int]$Offset, [string]$FilePath)
{
$bytes = [System.IO.File]::ReadAllBytes($FilePath)
for ($i = 0; $i -lt $Data.Length; $i++)
{
$bytes[$Offset + $i] = $Data[$i]
}
[System.IO.File]::WriteAllBytes($FilePath, $bytes)
}
$nameBytes = Get-AsciiBytes -InputString $NewName -MaxLength 11
$teamBytes = Get-AsciiBytes -InputString "Vintage" -MaxLength 7
Copy-Item -Path "$InstallDir\ProfileTemplate" -Destination "$InstallDir\Players\Player0.plr"
Patch-Binary -FilePath "$InstallDir\Players\Player0.plr" -Offset 0x8 -Data $nameBytes
Patch-Binary -FilePath "$InstallDir\Players\Player0.plr" -Offset 0x17 -Data $teamBytes
$InstallDir = $PSScriptRoot
Add-Type -AssemblyName System.Windows.Forms
$Display = [System.Windows.Forms.Screen]::AllScreens | Where-Object Primary | Select Bounds
function Get-43Resolution([int]$Width, [int]$Height) {
$ratio = 4 / 3
if (($Width -gt $Height) -or ($Width -eq $Height)) {
return @{ Width = [math]::Round($ratio * $Height); Height = $Height }
}
if ($Width -lt $Height) {
return @{ Width = $Width; Height = [math]::Round($Width / $ratio) }
}
}
function Write-ReplaceContentInFile([string]$Regex, [string]$Replacement, [string]$FilePath)
{
$content = (Get-Content $FilePath) -replace $Regex, $Replacement
[IO.File]::WriteAllLines($FilePath, $content)
}
# Accessible via $Resolution.Height, $Resolution.Width
$Resolution = Get-43Resolution -Width $Display.Width -Height $Display.Height
# Use regex to replace text within a file. Quotes are escaped by double quoting ("")
Write-ReplaceContentInFile -Regex '^persistent extern INDEX sam_iScreenSizeI=\(INDEX\)(\d+);' -Replacement "persistent extern INDEX sam_iScreenSizeI=(INDEX)$($Resolution.Width);" -FilePath "$InstallDir\Scripts\PersistentSymbols.ini"
Write-ReplaceContentInFile -Regex '^persistent extern INDEX sam_iScreenSizeJ=\(INDEX\)(\d+);' -Replacement "persistent extern INDEX sam_iScreenSizeJ=(INDEX)$($Resolution.Height);" -FilePath "$InstallDir\Scripts\PersistentSymbols.ini"