@echo off setlocal DisableDelayedExpansion title DanishGamers.com - ETS2 Convoy 128 set "CONVOY_SELF=%~f0" set "CONVOY_CONFIG=%~1" powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "$s=[IO.File]::ReadAllText($env:CONVOY_SELF); & ([ScriptBlock]::Create($s.Substring($s.LastIndexOf('# POWERSHELL START')+18)))" set "CONVOY_RESULT=%ERRORLEVEL%" echo. pause exit /b %CONVOY_RESULT% # POWERSHELL START $ErrorActionPreference = 'Stop' function Get-StreamBase64($stream) { $stream.Position = 0 $copy = New-Object IO.MemoryStream try { $stream.CopyTo($copy) return [Convert]::ToBase64String($copy.ToArray()) } finally { $copy.Dispose() } } function Write-ConfigBytes($stream, [byte[]]$contents) { $stream.Position = 0 $stream.Write($contents, 0, $contents.Length) $stream.SetLength($contents.Length) $stream.Flush($true) } try { Write-Host 'DanishGamers.com - ETS2 Convoy 128 (v1.0.1)' if (Get-Process -Name eurotrucks2 -ErrorAction SilentlyContinue) { throw 'Close Euro Truck Simulator 2 completely, then run this file again.' } $path = $env:CONVOY_CONFIG if ([string]::IsNullOrWhiteSpace($path)) { $path = Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'Euro Truck Simulator 2\config.cfg' if (-not [IO.File]::Exists($path)) { Write-Host 'The default ETS2 config.cfg was not found.' $path = (Read-Host 'Enter your ETS2 user folder or full config.cfg path').Trim().Trim('"') if ([string]::IsNullOrWhiteSpace($path)) { throw 'No path supplied. Nothing changed.' } } } if ([IO.Directory]::Exists($path)) { $path = Join-Path $path 'config.cfg' } $path = [IO.Path]::GetFullPath($path) if (-not [IO.File]::Exists($path)) { throw "Configuration not found: $path. Start ETS2 once first, or supply its actual config.cfg path." } $configInfo = Get-Item -LiteralPath $path -Force # Ordinary files, OneDrive cloud files and symbolic links are supported. $original = [IO.File]::ReadAllBytes($path) if ($original -contains 0) { throw 'Unexpected configuration encoding. Nothing changed.' } # A one-byte encoding preserves every unrelated byte, including UTF-8 and its BOM. $encoding = [Text.Encoding]::GetEncoding(28591) $text = $encoding.GetString($original) $pattern = '(?m)^(?:\xEF\xBB\xBF)?[ \t]*uset[ \t]+g_max_convoy_size[ \t]+"[^"\r\n]*"[^\r\n]*' $matches = [regex]::Matches($text, $pattern) if ($matches.Count -gt 1) { throw 'Multiple Convoy settings found. Nothing changed; inspect config.cfg manually.' } if ($matches.Count -eq 1) { $line = $matches[0].Value $updated = [regex]::Replace($line, '(g_max_convoy_size[ \t]+)"[^"\r\n]*"', '${1}"128"') $newText = $text.Substring(0, $matches[0].Index) + $updated + $text.Substring($matches[0].Index + $line.Length) } else { if ($text -match '(?m)^[ \t]*uset[ \t]+g_max_convoy_size\b') { throw 'Malformed Convoy setting. Nothing changed.' } $newline = "`r`n" if ($text.Contains("`n") -and -not $text.Contains("`r`n")) { $newline = "`n" } $separator = '' if ($text.Length -gt 0 -and -not $text.EndsWith("`n")) { $separator = $newline } $newText = $text + $separator + 'uset g_max_convoy_size "128"' + $newline } if ($text -ceq $newText) { Write-Host 'Already set to 128. Nothing needed changing.' -ForegroundColor Green exit 0 } $backup = $path + '.convoy128-' + [DateTime]::Now.ToString('yyyyMMdd-HHmmss') + '-' + [Guid]::NewGuid().ToString('N').Substring(0,8) + '.bak' $bytes = $encoding.GetBytes($newText) $originalBase64 = [Convert]::ToBase64String($original) # Open follows symbolic links and hydrates cloud files. Writing through the # same exclusive handle preserves the link and the existing file identity. $configStream = [IO.File]::Open($path, [IO.FileMode]::Open, [IO.FileAccess]::ReadWrite, [IO.FileShare]::None) $writeStarted = $false try { if ((Get-StreamBase64 $configStream) -cne $originalBase64) { throw 'Configuration changed during this operation. Run again with ETS2 closed.' } if (Get-Process -Name eurotrucks2 -ErrorAction SilentlyContinue) { throw 'ETS2 started during this operation. Close it and run again.' } $backupStream = [IO.File]::Open($backup, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write, [IO.FileShare]::None) try { $backupStream.Write($original, 0, $original.Length) $backupStream.Flush($true) } finally { $backupStream.Dispose() } if ([Convert]::ToBase64String([IO.File]::ReadAllBytes($backup)) -cne $originalBase64) { throw "Backup verification failed. Configuration unchanged. Backup: $backup" } $writeStarted = $true Write-ConfigBytes $configStream $bytes if ((Get-StreamBase64 $configStream) -cne [Convert]::ToBase64String($bytes)) { throw 'Configuration verification failed.' } } catch { $failure = $_.Exception.Message if ($writeStarted) { try { Write-ConfigBytes $configStream $original if ((Get-StreamBase64 $configStream) -cne $originalBase64) { throw 'Restore verification failed.' } } catch { throw "Update failed and automatic restore failed. Close ETS2 and restore the backup: $backup. Cause: $failure" } throw "Update failed; original settings were restored. Backup: $backup. Cause: $failure" } throw } finally { $configStream.Dispose() } Write-Host 'Done: g_max_convoy_size is now 128.' -ForegroundColor Green Write-Host "Config: $path" Write-Host "Backup: $backup" Write-Host 'Start ETS2 and open Convoy to find and join larger sessions.' Write-Host 'The server must allow the player count; game version, DLC and mods must match.' Write-Host 'To undo: close ETS2, then copy the backup over config.cfg.' exit 0 } catch { Write-Host ('ERROR: ' + $_.Exception.Message) -ForegroundColor Red exit 1 }