# Finds your latest working Star Rail warp history link in the local game cache. # Checks it with the official warp history service and copies it to the clipboard. # It does not install software, download scripts, or change system settings. param([string]$GameDirectory) # Windows PowerShell 5.1 may not offer TLS 1.2 by default. This only affects the current window. [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 function Get-GameDataDirectories { if ($GameDirectory) { $GameDirectory Join-Path $GameDirectory 'StarRail_Data' Join-Path $GameDirectory 'Games\StarRail_Data' return } $cnFolder = -join ([char]0x5D29, [char]0x574F, [char]0xFF1A, [char]0x661F, [char]0x7A79, [char]0x94C1, [char]0x9053) $localLow = Join-Path $env:USERPROFILE 'AppData\LocalLow' $logFolders = @( (Join-Path $localLow 'Cognosphere\Star Rail') (Join-Path $localLow ('miHoYo\' + $cnFolder)) ) $logs = foreach ($folder in $logFolders) { foreach ($name in @('Player.log', 'Player-prev.log')) { Get-Item -LiteralPath (Join-Path $folder $name) -ErrorAction SilentlyContinue } } foreach ($log in ($logs | Sort-Object LastWriteTimeUtc -Descending)) { try { $loadLines = @(Select-String -LiteralPath $log.FullName -Pattern 'Loading player data from\s+"?(.+?[\\/]data\.unity3d)' -ErrorAction Stop) for ($index = $loadLines.Count - 1; $index -ge 0; $index--) { [IO.Path]::GetDirectoryName($loadLines[$index].Matches[0].Groups[1].Value) } } catch { # A missing or unreadable log leaves the other logs available. continue } } } function Get-WarpCacheFile([string]$DataDirectory) { $cacheRoot = Join-Path $DataDirectory 'webCaches' if (-not (Test-Path -LiteralPath $cacheRoot -PathType Container)) { return } $latest = Get-ChildItem -LiteralPath $cacheRoot -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -match '^\d+\.\d+\.\d+\.\d+$' } | Sort-Object { [version]$_.Name } -Descending | Select-Object -First 1 $relativePath = 'Cache\Cache_Data\data_2' if ($latest) { $versionedFile = Join-Path $latest.FullName $relativePath if (Test-Path -LiteralPath $versionedFile -PathType Leaf) { return $versionedFile } } $unversionedFile = Join-Path $cacheRoot $relativePath if (Test-Path -LiteralPath $unversionedFile -PathType Leaf) { return $unversionedFile } } function Read-WarpCache([string]$CacheFile) { $tempFile = $null try { $tempFile = [IO.Path]::GetTempFileName() Copy-Item -LiteralPath $CacheFile -Destination $tempFile -Force -ErrorAction Stop [Text.Encoding]::UTF8.GetString([IO.File]::ReadAllBytes($tempFile)) } catch { Write-Host 'Could not read a local warp cache. Checking the other game logs.' } finally { if ($tempFile) { try { Remove-Item -LiteralPath $tempFile -Force -ErrorAction Stop } catch { Write-Host 'Could not remove the temporary warp cache file.' } } } } $officialHosts = @{ hkrpg_global = 'public-operation-hkrpg-sg.hoyoverse.com' hkrpg_cn = 'public-operation-hkrpg.mihoyo.com' } # Keeps only the authkey parameters and rebuilds the link on the official host for its server. function ConvertTo-OfficialWarpLink([string]$CachedLink) { $uri = $null if (-not [Uri]::TryCreate($CachedLink, [UriKind]::Absolute, [ref]$uri) -or $uri.Scheme -ne 'https') { return } $values = @{} foreach ($pair in ($uri.Query.TrimStart('?') -split '&')) { $name, $value = $pair -split '=', 2 if ($value -and $name -cin @('authkey', 'authkey_ver', 'sign_type', 'game_biz')) { $values[$name] = $value } } $gameBiz = [string]$values['game_biz'] if (-not $gameBiz) { if ($uri.Host -like '*.hoyoverse.com') { $gameBiz = 'hkrpg_global' } elseif ($uri.Host -like '*.mihoyo.com') { $gameBiz = 'hkrpg_cn' } } if (-not $values['authkey'] -or -not $officialHosts.ContainsKey($gameBiz)) { return } $collab = $uri.AbsolutePath -clike '*/getLdGachaLog' $endpoint = if ($collab) { 'getLdGachaLog' } else { 'getGachaLog' } $gachaType = if ($collab) { '21' } else { '11' } $authkeyVer = if ($values['authkey_ver']) { $values['authkey_ver'] } else { '1' } $signType = if ($values['sign_type']) { $values['sign_type'] } else { '2' } $query = "authkey_ver=$($authkeyVer)&sign_type=$($signType)&authkey=$($values['authkey'])&game_biz=$($gameBiz)&lang=en&gacha_type=$($gachaType)" [pscustomobject]@{ Authkey = $values['authkey'] Link = "https://$($officialHosts[$gameBiz])/common/gacha_record/api/$($endpoint)?$($query)" } } function Test-WarpHistoryLink([string]$Link) { try { $reply = Invoke-RestMethod -Uri "$($Link)&size=1&end_id=0" -Method Get -MaximumRedirection 0 -TimeoutSec 10 -UseBasicParsing -ErrorAction Stop -Verbose:$false -Debug:$false return $reply.retcode -eq 0 } catch { # Request errors may include the link, so keep them out of the output. return $false } } $seenAuthkeys = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::Ordinal) foreach ($directory in (Get-GameDataDirectories | Select-Object -Unique)) { $cacheFile = Get-WarpCacheFile $directory if (-not $cacheFile) { continue } $cache = Read-WarpCache $cacheFile if (-not $cache) { continue } $urlMatches = [regex]::Matches($cache, 'https://[^\p{C}\s<>"\uFFFD]+') for ($index = $urlMatches.Count - 1; $index -ge 0; $index--) { if ($urlMatches[$index].Value -cnotmatch '/get(?:Ld)?GachaLog\?') { continue } $candidate = ConvertTo-OfficialWarpLink $urlMatches[$index].Value if (-not $candidate -or -not $seenAuthkeys.Add($candidate.Authkey)) { continue } $link = $candidate.Link if (-not (Test-WarpHistoryLink $link)) { continue } try { Set-Clipboard -Value $link -ErrorAction Stop -Verbose:$false -Debug:$false Write-Host 'Warp history link copied. Paste it into the pull tracker.' } catch { Write-Host 'Could not copy the warp history link to the clipboard.' } return } } Write-Host 'No working link found. Open Warp Records in Star Rail and try again.'