Skip to content

Instantly share code, notes, and snippets.

@jogerj
Last active March 23, 2024 21:17
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jogerj/0339e61a92e0de2e360c5212a94854e8 to your computer and use it in GitHub Desktop.
Save jogerj/0339e61a92e0de2e360c5212a94854e8 to your computer and use it in GitHub Desktop.
Get Wish History URL in Genshin Impact 4.6 on PC from cache

Usage

Win+R and paste following

  • All versions (Global/China)
powershell iex (irm 'https://gist.githubusercontent.com/jogerj/0339e61a92e0de2e360c5212a94854e8/raw/get_wish_url_from_cache.ps1')

Report bugs/errors

Comment on this gist or send message on paimon.moe Discord server

Changelog

Version 0.12.0

  • Deprecated and removed fallback methods
  • Now cache path lookup checks for latest modified subfolder

Version 0.11.1

  • Fix for Genshin 4.0

Version 0.11.0

  • Fix for Genshin 3.8

Version 0.10.0

  • Now if a user has both global and china version of the game, it will load the URL from whichever is last open.

Version 0.9.0

  • Fix CN suffix to game_biz=hk4e_cn
  • Fix check validity for URLs beginning with https://webstatic...

Version 0.8.0

  • Added new method from MadeBaruna. Now supports 3 different methods (should be totally foolproof 🤞)
  • Automatically checks for expired/invalid link
  • URL date is now retrieved from URL timestamp parameter Removed URL time since it's unnecessary to check for URL expiry

Version 0.7.0

  • Combined Global and China server scripts. Now will check for Global first before China log files. Can be overriden to force check China server by adding china to the parameter like this:
    powershell iex "&{$(irm 'https://gist.githubusercontent.com/jogerj/0339e61a92e0de2e360c5212a94854e8/raw/get_wish_url_from_cache.ps1')} china"
  • Pass on args to elevated powershell correctly
  • Use more accurate file path pattern from here

Version 0.6.0

  • Added back old method as fallback option (when webCache gets destroyed/new install)

Version 0.5.0

  • Changed game path lookup to search in log file instead of install path
  • Added China version (needs testing)
  • adjusted URL lookup pattern

Version 0.4.0

  • ChromeCacheView no longer needed. Script will now read cache files directly
  • Credits to @PrimeCicada for finding an alternate path

Version 0.3.0

  • Added handling of different game path
  • Fixes issue with older installs of Genshin with different path
  • Added fallback option for manual entry of game path. Drag and drop your shortcut or exe file (either launcher or game works), the cache path will be grabbed correctly

Version 0.2.0

  • Added date of URL to output
  • Add warning for URL older than 24h

Version 0.1.0

  • Initial release
# script version 0.12.1
# author: jogerj
function processWishUrl($wishUrl) {
# check validity
if ($wishUrl -match "https:\/\/webstatic") {
if ($wishUrl -match "hk4e_global") {
$checkUrl = $wishUrl -replace "https:\/\/webstatic.+html\?", "https://hk4e-api-os.mihoyo.com/event/gacha_info/api/getGachaLog?"
} else {
$checkUrl = $wishUrl -replace "https:\/\/webstatic.+html\?", "https://hk4e-api.mihoyo.com/event/gacha_info/api/getGachaLog?"
}
$urlResponseMessage = Invoke-RestMethod -URI $checkUrl | % {$_.message}
} else {
$urlResponseMessage = Invoke-RestMethod -URI $wishUrl | % {$_.message}
}
if ($urlResponseMessage -ne "OK") {
Write-Host "Link found but it is expired/invalid! Open Wish History again to fetch a new link" -ForegroundColor Yellow
return $False
}
# OK
Write-Host $wishURL
Set-Clipboard -Value $wishURL
Write-Host "Link copied to clipboard, paste it back to paimon.moe" -ForegroundColor Green
return $True
}
$logPathGlobal = [System.Environment]::ExpandEnvironmentVariables("%userprofile%/AppData/LocalLow/miHoYo/Genshin Impact/output_log.txt");
$logPathChina = [System.Environment]::ExpandEnvironmentVariables("%userprofile%/AppData/LocalLow/miHoYo/$([char]0x539f)$([char]0x795e)/output_log.txt");
$globalExists = Test-Path $logPathGlobal;
$cnExists = Test-Path $logPathChina;
if ($globalExists) {
if ($cnExists) {
# both exists, pick newest one
if ((Get-Item $logPathGlobal).LastWriteTime -ge (Get-Item $logPathChina).LastWriteTime) {
$logPath = $logPathGlobal;
} else {
$logPath = $logPathChina;
}
} else {
$logPath = $logPathGlobal;
}
} else {
if ($cnExists) {
$logPath = $logPathChina;
} else {
Write-Host "Cannot find Genshin Impact log file! Make sure to run Genshin Impact and open the wish history at least once!" -ForegroundColor Red
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "Do you want to try to run the script as Administrator? Press [ENTER] to continue, or any key to cancel."
$keyInput = [Console]::ReadKey($true).Key
if ($keyInput -ne "13") {
return
}
$arguments = "& '" +$myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList "-noexit $arguments $reg"
break
}
return
}
}
$logs = Get-Content -Path $logPath
$regexPattern = "(?m).:/.+(GenshinImpact_Data|YuanShen_Data)"
$logMatch = $logs -match $regexPattern
if (-Not $logMatch) {
Write-Host "Cannot find Genshin Impact path in log file! Make sure to run Genshin Impact and open the wish history at least once!" -ForegroundColor Red
pause
return
}
$gameDataPath = ($logMatch | Select -Last 1) -match $regexPattern
$gameDataPath = Resolve-Path $Matches[0]
$webcachePath = Resolve-Path "$gameDataPath/webCaches"
$cacheVerPath = Get-Item (Get-ChildItem -Path $webcachePath | Sort-Object LastWriteTime -Descending | Select-Object -First 1).FullName
$cachePath = Resolve-Path "$cacheVerPath/Cache/Cache_Data/data_2"
if (Test-Path $cachePath) {
$tmpFile = "$env:TEMP/ch_data_2"
Copy-Item $cachePath -Destination $tmpFile
$content = Get-Content -Encoding UTF8 -Raw $tmpfile
$splitted = $content -split "1/0/" | Select -Last 1
$found = $splitted -match "https.+?game_biz=hk4e_(global|cn)"
Remove-Item $tmpFile
if ($found) {
$wishUrl = $Matches[0]
if (processWishUrl $wishUrl) {
return
}
}
Write-Host "No valid link found! Make sure Genshin Impact is installed and open Wish History page at least once." -ForegroundColor Red
pause
} else {
Write-Host "Genshin Impact cache not found! Make sure Genshin Impact is installed and open Wish History page at least once." -ForegroundColor Red
pause
}
@KitKit0w
Copy link

I'm getting this error even though history page is open in-game...
Link found but it is expired/invalid! Open Wish History again to fetch a new link
No valid link found! Make sure Genshin Impact is installed and open Wish History page at least once.
Press Enter to continue...:

@jogerj
Copy link
Author

jogerj commented Jan 29, 2024

@KitKit0w delete the webCaches folder inside GenshinImpact_Data folder. Then reopen wish history and retry.

@KitKit0w
Copy link

Thank you so much, it works now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment