$InstallDir = $PSScriptRoot
$NewName = $args[0].Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$OldName = ""
if ($args[1]) {
$OldName = $args[1].Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
}
function Write-ReplaceContentInFile([string]$Regex, [string]$Replacement, [string]$FilePath)
{
$content = (Get-Content $FilePath) -replace $Regex, $Replacement
[IO.File]::WriteAllLines($FilePath, $content)
}
# Trim a string down to a specified amount of characters
if ($OldName.Length -gt 15) {
$OldName = $OldName.Substring(0, 15);
}
if ($NewName.Length -gt 15) {
$NewName = $NewName.Substring(0, 15);
}
# Scaffold an "old" profile using the included template
if (!$OldName) {
$OldName = "ProfileTemplate";
Copy-Item -Path "$InstallDir\ProfileTemplate" -Destination "$InstallDir\player\$OldName" -Recurse
}
Copy-Item -Path "$InstallDir\player\$OldName" -Destination "$InstallDir\player\$NewName" -Recurse
Remove-Item "$InstallDir\player\$OldName" -Recurse -ErrorAction Ignore
Rename-Item "$InstallDir\player\$NewName\$OldName.plr" "$InstallDir\player\$NewName\$NewName.plr"
Rename-Item "$InstallDir\player\$NewName\$OldName.mpc" "$InstallDir\player\$NewName\$NewName.mpc"
# Use regex to replace text within a file. Quotes are escaped by double quoting ("")
Write-ReplaceContentInFile -Regex '"gameName": "(.+)",' -Replacement """gameName"": ""$NewName's Game""," -FilePath "$InstallDir\registry.json"
Write-ReplaceContentInFile -Regex '"playerShortname": "(.+)",' -Replacement """playerShortname"": ""$NewName""," -FilePath "$InstallDir\registry.json"