UPDATER_SCRIPT

Constant UPDATER_SCRIPT 

Source
pub const UPDATER_SCRIPT: &str = "param (\n    [Parameter(Mandatory = $true)]\n    [string]$binary,\n\n    [Parameter(Mandatory = $false)]\n    [string]$argumentHex = \"\",\n\n    [Parameter(Mandatory = $true)]\n    [int]$processPid,\n\n    [Parameter(Mandatory = $false)]\n    [switch]$restart = $false\n)\n\nWrite-Host \"Waiting for process with PID $processPid to exit...\"\ntry {\n    $process = Get-Process -Id $processPid -ErrorAction Stop\n    $process.WaitForExit()\n    Write-Host \"Process with PID $processPid has exited.\"\n}\ncatch {\n    Write-Host \"Process with PID $processPid is not running or already exited.\"\n}\n\n$binaryDirectory = [System.IO.Path]::GetDirectoryName($binary)\n$obsNewDir = Join-Path -Path $binaryDirectory -ChildPath \"obs_new\"\n\nif (Test-Path $obsNewDir -PathType Container) {\n    Write-Host \"Found obs_new directory, copying all contents to binary directory\"\n\n    try {\n        $files = Get-ChildItem -Path $obsNewDir -Recurse\n        foreach ($file in $files) {\n            $relativePath = $file.FullName.Substring($obsNewDir.Length + 1)\n            $destination = Join-Path -Path $binaryDirectory -ChildPath $relativePath\n\n            # Create directory structure if needed\n            if ($file.PSIsContainer) {\n                if (-Not (Test-Path $destination -PathType Container)) {\n                    New-Item -ItemType Directory -Path $destination -Force | Out-Null\n                }\n                continue\n            }\n\n            # Remove target file if it exists\n            if (Test-Path $destination) {\n                try {\n                    Remove-Item -Path $destination -Force\n                }\n                catch {\n                    Write-Host \"Failed to remove existing file ${destination}: $_\"\n                    exit 1\n                }\n            }\n\n            # Copy the file\n            try {\n                Copy-Item -Path $file.FullName -Destination $destination -Force\n            }\n            catch {\n                Write-Host \"Failed to copy $($file.FullName) to ${destination}: $_\"\n                exit 1\n            }\n        }\n        Write-Host \"Successfully copied all contents from obs_new to binary directory\"\n\n        # Optionally remove the obs_new directory after successful copy\n        try {\n            Remove-Item -Path $obsNewDir -Recurse -Force\n            Write-Host \"Removed obs_new directory after copying contents\"\n        }\n        catch {\n            Write-Host \"Warning: Could not remove obs_new directory: $_\"\n        }\n    }\n    catch {\n        Write-Host \"Error copying files from obs_new directory: $_\"\n        exit 1\n    }\n}\nelse {\n    Write-Host \"Warning: obs_new directory not found in $binaryDirectory\"\n}\n\nif (-not $restart) {\n    Write-Host \"No binary specified, exiting.\"\n    exit 0\n}\n\n# Decode argumentHex to argument array\n$arguments = @()\nif ($argumentHex -ne \"\") {\n    try {\n        # Split the hex string into argument hex segments by \'00\' (null separator)\n        $hexArgs = $argumentHex -split \'00\'\n        $arguments = @()\n        foreach ($hexArg in $hexArgs) {\n            if ($hexArg -ne \"\") {\n            $bytes = @()\n            for ($i = 0; $i -lt $hexArg.Length; $i += 2) {\n                $bytes += [Convert]::ToByte($hexArg.Substring($i, 2), 16)\n            }\n            $decoded = [System.Text.Encoding]::UTF8.GetString($bytes)\n            if ($decoded -ne \"\") {\n                $arguments += $decoded\n            }\n            }\n        }\n    }\n    catch {\n        Write-Host \"Failed to decode argumentHex: $_. Starting without arguments.\"\n    }\n}\n\nWrite-Host \"Restarting $binary with arguments: $($arguments -join \' \')\"\ntry {\n    if ($arguments.Count -eq 0) {\n        Start-Process -FilePath $binary\n    }\n    else {\n        Start-Process -FilePath $binary -ArgumentList $arguments\n    }\n    Write-Host \"Successfully restarted $binary\"\n}\ncatch {\n    Write-Host \"Failed to restart ${binary}: $_\"\n    exit 1\n}";