tasklist | find /i "HYP.exe"
if %errorlevel% equ 0 taskkill /F /IM HYP.exe /T
tasklist | find /i "HYPHelper.exe"
if %errorlevel% equ 0 taskkill /F /IM HYPHelper.exe /T

rmdir "%AppData%\Cognosphere" /S /Q
mkdir "%~dp0ICafeCloud\AppData\Cognosphere" 2>nul
mklink /J "%AppData%\Cognosphere" "%~dp0ICafeCloud\AppData\Cognosphere"

REM Switch HYP to the new version folder when the recorded version is older than every version folder here
set "hyp_rec_file=%~dp0hyp_version.txt"
set "hyp_out_file=%TEMP%\hyp_newver.txt"
set "hyp_rec="
if exist "%hyp_rec_file%" set /p hyp_rec=<"%hyp_rec_file%"
del /f /q "%hyp_out_file%" >nul 2>&1
powershell -NoProfile -Command "$d = '%~dp0'; $rec = '%hyp_rec%'.Trim(); $v = @(Get-ChildItem -LiteralPath $d -Directory -Name | Where-Object { $_ -match '^\d+(\.\d+)+$' } | Sort-Object { [version]$_ }); $pick = ''; if ($v.Count -gt 0) { if (-not (Test-Path -LiteralPath (Join-Path $d 'HYP'))) { $pick = $v[-1] } elseif ($rec -match '^\d+(\.\d+)+$' -and [version]$rec -lt [version]$v[0]) { $pick = $v[-1] } }; $pick | Out-File -Encoding ASCII -FilePath '%hyp_out_file%'"
set "hyp_new="
if exist "%hyp_out_file%" set /p hyp_new=<"%hyp_out_file%"
del /f /q "%hyp_out_file%" >nul 2>&1

if defined hyp_new (
    >"%hyp_rec_file%" echo %hyp_new%
    if exist "%~dp0HYP" rmdir /S /Q "%~dp0HYP"
    if not exist "%~dp0HYP" ren "%~dp0%hyp_new%" "HYP"
)

REM HoYoPlay URL protocol
Reg.exe add "HKCR\hoyoplay" /f /ve /t REG_SZ /d "URL:hoyoplay protocol"
Reg.exe add "HKCR\hoyoplay" /f /v "URL Protocol" /t REG_SZ /d ""
Reg.exe add "HKCR\hoyoplay\DefaultIcon" /f /ve /t REG_SZ /d "%~dp0launcher.exe,0"
Reg.exe add "HKCR\hoyoplay\shell\open\command" /f /ve /t REG_SZ /d "\"%~dp0launcher.exe\" \"%%1\""

REM HoYoPlay HYP-global URL protocol
Reg.exe add "HKCR\HYP-global" /f /v "URL Protocol" /t REG_SZ /d "HYP-global"
Reg.exe add "HKCR\HYP-global" /f /v "UseOriginalUrlEncoding" /t REG_DWORD /d 1
Reg.exe add "HKCR\HYP-global\shell\open\command" /f /ve /t REG_SZ /d "\"%~dp0launcher.exe\" \"--url=%%1\""

REM HoYoPlay HYP-global-1-0 URL protocol
Reg.exe add "HKCR\HYP-global-1-0" /f /v "URL Protocol" /t REG_SZ /d "HYP-global-1-0"
Reg.exe add "HKCR\HYP-global-1-0" /f /v "UseOriginalUrlEncoding" /t REG_DWORD /d 1
Reg.exe add "HKCR\HYP-global-1-0\shell\open\command" /f /ve /t REG_SZ /d "\"%~dp0launcher.exe\" \"--url=%%1\""

REM HoYoPlay HYP-global URL protocol - HKCU
Reg.exe add "HKCU\Software\Classes\HYP-global" /f /v "URL Protocol" /t REG_SZ /d "HYP-global"
Reg.exe add "HKCU\Software\Classes\HYP-global" /f /v "UseOriginalUrlEncoding" /t REG_DWORD /d 1
Reg.exe add "HKCU\Software\Classes\HYP-global\shell\open\command" /f /ve /t REG_SZ /d "\"%~dp0launcher.exe\" \"--url=%%1\""

REM HoYoPlay HYP-global-1-0 URL protocol - HKCU
Reg.exe add "HKCU\Software\Classes\HYP-global-1-0" /f /v "URL Protocol" /t REG_SZ /d "HYP-global-1-0"
Reg.exe add "HKCU\Software\Classes\HYP-global-1-0" /f /v "UseOriginalUrlEncoding" /t REG_DWORD /d 1
Reg.exe add "HKCU\Software\Classes\HYP-global-1-0\shell\open\command" /f /ve /t REG_SZ /d "\"%~dp0launcher.exe\" \"--url=%%1\""

REM HoYoPlay Cognosphere install info
Reg.exe add "HKCU\Software\Cognosphere\HYP\1_0" /f /v "InstallPath" /t REG_SZ /d "%~dp0"

REM HoYoPlay uninstall entry
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "DisplayIcon" /t REG_SZ /d "%~dp0launcher.exe"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "DisplayName" /t REG_SZ /d "HoYoPlay"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "UninstallString" /t REG_SZ /d "%~dp0uninstall.exe"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "ExeName" /t REG_SZ /d "launcher.exe"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "URLInfoAbout" /t REG_SZ /d "https://hoyoplay.hoyoverse.com"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "Channel" /t REG_SZ /d "1_0"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "GameBiz" /t REG_SZ /d "hyp_global"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "Region" /t REG_SZ /d "global"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "ClientEnv" /t REG_SZ /d "production"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "InstallPath" /t REG_SZ /d "%~dp0"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\HYP_1_0_global" /f /v "InstallLocation" /t REG_SZ /d "%~dp0"
