$InstallDir = $PSScriptRoot
# Accessible via $Display.ScreenWidth and $Display.ScreenHeight
Add-Type -AssemblyName System.Windows.Forms
$Display = [System.Windows.Forms.Screen]::AllScreens | Where-Object Primary | Select Bounds
function Write-ReplaceContentInFile([string]$Regex, [string]$Replacement, [string]$FilePath)
{
$content = (Get-Content $FilePath) -replace $Regex, $Replacement
[IO.File]::WriteAllLines($FilePath, $content)
}
# Use regex to replace text within a file. Quotes are escaped by double quoting ("")
Write-ReplaceContentInFile -Regex '^screenwidth=(.+)' -Replacement "screenwidth=$($Display.Width)" -FilePath "$InstallDir\local.ini"
Write-ReplaceContentInFile -Regex '^screenheight=(.+)' -Replacement "screenheight=$($Display.Height)" -FilePath "$InstallDir\local.ini"
$InstallDir = $PSScriptRoot
$NewName = $args[0]
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++
}
}
# Pad the end with 0x00 to meet our max length
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)
}
function Write-ReplaceContentInFile([string]$Regex, [string]$Replacement, [string]$FilePath)
{
$content = (Get-Content $FilePath) -replace $Regex, $Replacement
[IO.File]::WriteAllLines($FilePath, $content)
}
function Separate-AsciiBytes([byte[]]$Data)
{
$array = @()
foreach ($byte in $Data)
{
$array += $byte
$array += 0x00
}
return $array
}
# Convert an input string to ASCII-encoded byte[]. Shorter strings will pad out to 12 bytes, longer strings will be trimmed.
$bytes = Get-AsciiBytes -InputString $NewName -MaxLength 10
$bytes = Separate-AsciiBytes -Data $bytes
# Writes byte[] to a file at an offset
Patch-Binary -FilePath "$InstallDir\Profiles\Profile2\name.dat" -Offset 0x02 -Data $bytes