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

REM Copy HYP back to the recorded version folder so HoYoPlay can detect the installed version
REM Nothing to do when that version folder is already there
set "hyp_rec_file=%~dp0hyp_version.txt"
set "hyp_rec="
if exist "%hyp_rec_file%" set /p hyp_rec=<"%hyp_rec_file%"

if defined hyp_rec if not exist "%~dp0%hyp_rec%" xcopy /E /I /Y "%~dp0HYP" "%~dp0%hyp_rec%"

rmdir "%AppData%\Cognosphere" /S /Q
mkdir "%~dp0ICafeCloud\AppData\Cognosphere" 2>nul
mklink /J "%AppData%\Cognosphere" "%~dp0ICafeCloud\AppData\Cognosphere"

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"

set "script_dir=%~dp0"
set "shortcut_exe_path=%~dp0launcher.exe"
set "shortcut_work_dir=%script_dir:~0,-1%"
set "shortcut_name=HoYoPlay"
set "desktop_folder=%USERPROFILE%\Desktop"

powershell -Command "$s=(New-Object -ComObject WScript.Shell).CreateShortcut('%desktop_folder%\%shortcut_name%.lnk'); $s.TargetPath='%shortcut_exe_path%'; $s.WorkingDirectory='%shortcut_work_dir%'; $s.Save()"
