@echo off setlocal DisableDelayedExpansion title DanishGamers.com - ETS2 Developer Console and Fly Mode set "DEVFLY_SELF=%~f0" set "DEVFLY_CONFIG=%~1" powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "$s=[IO.File]::ReadAllText($env:DEVFLY_SELF); & ([ScriptBlock]::Create($s.Substring($s.LastIndexOf('# POWERSHELL START')+18)))" set "DEVFLY_RESULT=%ERRORLEVEL%" echo. pause exit /b %DEVFLY_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 Developer Console and Fly Mode (v1.0.1)' if (Get-Process -Name eurotrucks2 -ErrorAction SilentlyContinue) { throw 'Close Euro Truck Simulator 2 completely, then run this file again.' } $path = $env:DEVFLY_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) $newText = $text foreach ($setting in @('g_console', 'g_developer')) { $name = [regex]::Escape($setting) $prefix = '(?m)^(?:\xEF\xBB\xBF)?[ \t]*uset[ \t]+' $pattern = $prefix + $name + '[ \t]+"[^"\r\n]*"[^\r\n]*' $matches = [regex]::Matches($newText, $pattern) $declarations = [regex]::Matches($newText, $prefix + $name + '\b') if ($declarations.Count -ne $matches.Count -or $matches.Count -gt 1) { throw "Malformed or duplicate $setting setting. Nothing changed; inspect config.cfg manually." } if ($matches.Count -eq 1) { $line = $matches[0].Value $updated = [regex]::Replace($line, '(' + $name + '[ \t]+)"[^"\r\n]*"', '${1}"1"') $newText = $newText.Substring(0, $matches[0].Index) + $updated + $newText.Substring($matches[0].Index + $line.Length) } else { $newline = "`r`n" if ($newText.Contains("`n") -and -not $newText.Contains("`r`n")) { $newline = "`n" } $separator = '' if ($newText.Length -gt 0 -and -not $newText.EndsWith("`n")) { $separator = $newline } $newText = $newText + $separator + 'uset ' + $setting + ' "1"' + $newline } } if ($text -ceq $newText) { Write-Host 'Developer console and Fly mode are already enabled. Nothing needed changing.' -ForegroundColor Green exit 0 } $backup = $path + '.devfly-' + [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_console and g_developer are now 1.' -ForegroundColor Green Write-Host "Config: $path" Write-Host "Backup: $backup" Write-Host 'Start ETS2. Open the console with the grave/tilde key (usually below Esc).' Write-Host 'Fly mode: top-row 0; move with the numeric keypad and look with the mouse.' Write-Host 'Mouse wheel adjusts flight speed. Press 1 to return to the cabin.' 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 }