XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
UTF-8
param(
    [string]$BinPath,
    [string]$RootPath
)

$ErrorActionPreference = 'SilentlyContinue'
$candidates = [System.Collections.Generic.List[string]]::new()

function Add-Candidate([string]$Path) {
    if (-not [string]::IsNullOrWhiteSpace($Path)) {
        $candidates.Add([Environment]::ExpandEnvironmentVariables($Path.Trim().Trim('"')))
    }
}

function Get-GameRoot([string]$Path) {
    if ([string]::IsNullOrWhiteSpace($Path)) {
        return $null
    }

    $candidate = $Path.Trim().Trim('"')
    if ([IO.Path]::GetFileName($candidate) -eq 'SpaceEngineers.exe') {
        $candidate = Split-Path -Parent $candidate
    }
    if ([IO.Path]::GetFileName($candidate) -eq 'Bin64') {
        $candidate = Split-Path -Parent $candidate
    }

    $executable = Join-Path $candidate 'Bin64\SpaceEngineers.exe'
    $vrage = Join-Path $candidate 'Bin64\VRage.Game.dll'
    $sandbox = Join-Path $candidate 'Bin64\Sandbox.Game.dll'
    $content = Join-Path $candidate 'Content'
    if ((Test-Path -LiteralPath $executable) -and
        (Test-Path -LiteralPath $vrage) -and
        (Test-Path -LiteralPath $sandbox) -and
        (Test-Path -LiteralPath $content)) {
        return [IO.Path]::GetFullPath($candidate).TrimEnd('\')
    }

    return $null
}

function Add-Steam-Library([string]$SteamPath) {
    if ([string]::IsNullOrWhiteSpace($SteamPath)) {
        return
    }

    $steamRoot = $SteamPath.Trim().Trim('"')
    Add-Candidate (Join-Path $steamRoot 'steamapps\common\SpaceEngineers')

    $libraryFile = Join-Path $steamRoot 'steamapps\libraryfolders.vdf'
    if (-not (Test-Path -LiteralPath $libraryFile)) {
        return
    }

    $libraryText = Get-Content -LiteralPath $libraryFile -Raw
    $matches = [regex]::Matches($libraryText, '"(?:path|\d+)"\s+"(?<path>[^"]+)"')
    foreach ($match in $matches) {
        $libraryRoot = $match.Groups['path'].Value.Replace('\\', '\')
        $manifest = Join-Path $libraryRoot 'steamapps\appmanifest_244850.acf'
        $installDirectory = 'SpaceEngineers'
        if (Test-Path -LiteralPath $manifest) {
            $manifestText = Get-Content -LiteralPath $manifest -Raw
            $installMatch = [regex]::Match($manifestText, '"installdir"\s+"(?<name>[^"]+)"')
            if ($installMatch.Success) {
                $installDirectory = $installMatch.Groups['name'].Value
            }
        }
        Add-Candidate (Join-Path $libraryRoot "steamapps\common\$installDirectory")
    }
}

Add-Candidate $BinPath
Add-Candidate $RootPath
Add-Candidate $env:SPACE_ENGINEERS_BIN
Add-Candidate $env:SPACE_ENGINEERS_ROOT

$uninstallKeys = @(
    'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 244850',
    'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 244850',
    'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 244850'
)
foreach ($key in $uninstallKeys) {
    Add-Candidate (Get-ItemPropertyValue -LiteralPath $key -Name InstallLocation)
}

$steamKeys = @(
    'HKCU:\SOFTWARE\Valve\Steam',
    'HKLM:\SOFTWARE\Valve\Steam',
    'HKLM:\SOFTWARE\WOW6432Node\Valve\Steam'
)
foreach ($key in $steamKeys) {
    Add-Steam-Library (Get-ItemPropertyValue -LiteralPath $key -Name SteamPath)
    Add-Steam-Library (Get-ItemPropertyValue -LiteralPath $key -Name InstallPath)
}

Add-Steam-Library (Join-Path ${env:ProgramFiles(x86)} 'Steam')
foreach ($drive in [IO.DriveInfo]::GetDrives()) {
    if ($drive.IsReady -and $drive.DriveType -eq [IO.DriveType]::Fixed) {
        Add-Candidate (Join-Path $drive.RootDirectory.FullName 'SteamLibrary\steamapps\common\SpaceEngineers')
    }
}

foreach ($candidate in $candidates) {
    $gameRoot = Get-GameRoot $candidate
    if ($null -ne $gameRoot) {
        Write-Output (Join-Path $gameRoot 'Bin64')
        exit 0
    }
}

exit 0